Add a button to copy a token to the clipboard

This commit is contained in:
Jakob Nixdorf 2017-07-04 08:33:40 +02:00
parent 3ae95c50cc
commit d78e630015
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
4 changed files with 74 additions and 28 deletions

View file

@ -23,6 +23,8 @@
package org.shadowice.flocke.andotp;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.PopupMenu;
@ -34,9 +36,10 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.shadowice.flocke.andotp.ItemTouchHelper.ItemTouchHelperAdapter;
import org.shadowice.flocke.andotp.ItemTouchHelper.ItemTouchHelperViewHolder;
@ -192,6 +195,14 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
popup.show();
}
private void copyToClipboard(String text) {
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(context.getString(R.string.label_clipboard), text);
clipboard.setPrimaryClip(clip);
Toast.makeText(context, R.string.msg_copied_to_clipboard, Toast.LENGTH_LONG).show();
}
public void setMoveEventCallback(ViewHolderEventCallback cb) {
this.viewHolderEventCallback = cb;
}
@ -205,7 +216,8 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
protected TextView OTPLabel;
protected LinearLayout customPeriodLayout;
protected TextView customPeriod;
protected ImageView menuButton;
protected ImageButton menuButton;
protected ImageButton copyButton;
public EntryViewHolder(final View v) {
super(v);
@ -214,7 +226,8 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
OTPLabel = (TextView) v.findViewById(R.id.textViewLabel);
customPeriodLayout = (LinearLayout) v.findViewById(R.id.customPeriodLayout);
customPeriod = (TextView) v.findViewById(R.id.customPeriod);
menuButton = (ImageView) v.findViewById(R.id.menuButton);
menuButton = (ImageButton) v.findViewById(R.id.menuButton);
copyButton = (ImageButton) v.findViewById(R.id.copyButton);
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
@ -222,6 +235,13 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
showPopupMenu(view, getAdapterPosition());
}
});
copyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
copyToClipboard(OTPValue.getText().toString());
}
});
}
@Override

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF727272"
android:pathData="M16,1L4,1c-1.1,0 -2,0.9 -2,2v14h2L4,3h12L16,1zM19,5L8,5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2L21,7c0,-1.1 -0.9,-2 -2,-2zM19,21L8,21L8,7h11v14z"/>
</vector>

View file

@ -31,11 +31,17 @@
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="@+id/customPeriodLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/menuButton"
android:layout_alignParentEnd="true"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/customPeriodLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:visibility="gone"
android:orientation="horizontal" >
@ -52,14 +58,22 @@
android:textStyle="bold" />
</LinearLayout>
<ImageView
<ImageButton
android:id="@+id/copyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_margin_small"
android:src="@drawable/ic_content_copy_gray"
android:background="?android:attr/selectableItemBackground"/>
<ImageButton
android:id="@+id/menuButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:padding="@dimen/activity_margin_small"
android:src="@drawable/ic_more_vert_gray"/>
android:src="@drawable/ic_more_vert_gray"
android:background="?android:attr/selectableItemBackground"/>
</LinearLayout>
</RelativeLayout>

View file

@ -9,6 +9,8 @@
<string name="format_custom_period">%d s</string>
<string name="label_clipboard" translatable="false">OTP Token</string>
<string name="menu_about">About</string>
<string name="menu_edit_label">Edit label</string>
<string name="menu_export">Export (JSON)</string>
@ -26,6 +28,7 @@
</string>
<string name="msg_camera_permission">Camera permission not granted</string>
<string name="msg_confirm_delete">Are you sure you want do remove this account?</string>
<string name="msg_copied_to_clipboard">Copied to clipboard</string>
<string name="msg_export_success">Export to external storage successful</string>
<string name="msg_export_failed">Export to external storage failed</string>
<string name="msg_export_warning">Do you really want to export the database as plain-text JSON