Added copy to clipboard onClick

This commit is contained in:
Michael Schättgen 2016-10-26 00:29:21 +02:00
parent 32ae66374b
commit 099250dd49
3 changed files with 47 additions and 48 deletions

View file

@ -22,14 +22,15 @@ import java.util.List;
import me.impy.aegis.crypto.OTP; import me.impy.aegis.crypto.OTP;
import me.impy.aegis.db.Database; import me.impy.aegis.db.Database;
import me.impy.aegis.helpers.ItemClickListener;
import me.impy.aegis.helpers.ItemTouchHelperAdapter; import me.impy.aegis.helpers.ItemTouchHelperAdapter;
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> implements ItemTouchHelperAdapter { public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> implements ItemTouchHelperAdapter {
private final List<KeyProfileHolder> lstHolders; private final List<KeyProfileHolder> lstHolders;
private ArrayList<KeyProfile> mKeyProfiles; private ArrayList<KeyProfile> mKeyProfiles;
private Handler uiHandler; private Handler uiHandler;
private static ItemClickListener itemClickListener;
// Provide a suitable constructor (depends on the kind of dataset)
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) { public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) {
mKeyProfiles = keyProfiles; mKeyProfiles = keyProfiles;
lstHolders = new ArrayList<>(); lstHolders = new ArrayList<>();
@ -41,7 +42,6 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
return; return;
} }
// Helper functions you might want to implement to make changes in the list as an event is fired
private void remove(int position) { private void remove(int position) {
mKeyProfiles.remove(position); mKeyProfiles.remove(position);
notifyItemRemoved(position); notifyItemRemoved(position);
@ -56,8 +56,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
adjustOrder(secondPosition); adjustOrder(secondPosition);
} }
private void adjustOrder(int startPosition) private void adjustOrder(int startPosition) {
{
Comparator<KeyProfile> comparator = new Comparator<KeyProfile>() { Comparator<KeyProfile> comparator = new Comparator<KeyProfile>() {
@Override @Override
public int compare(KeyProfile keyProfile, KeyProfile t1) { public int compare(KeyProfile keyProfile, KeyProfile t1) {
@ -65,29 +64,23 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
} }
}; };
for(int i = startPosition; i < mKeyProfiles.size(); i++) for (int i = startPosition; i < mKeyProfiles.size(); i++) {
{
mKeyProfiles.get(i).Order = i + 1; mKeyProfiles.get(i).Order = i + 1;
} }
} }
// Create new views (invoked by the layout manager) @Override
@Override public KeyProfileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public KeyProfileHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_keyprofile, parent, false);
// create a new view KeyProfileHolder vh = new KeyProfileHolder(v);
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_keyprofile, parent, false); return vh;
// set the view's size, margins, paddings and layout parameters }
KeyProfileHolder vh = new KeyProfileHolder(v); @Override
return vh; public void onBindViewHolder(final KeyProfileHolder holder, int position) {
} holder.setData(mKeyProfiles.get(position));
holder.updateCode();
// Replace the contents of a view (invoked by the layout manager) lstHolders.add(holder);
@Override
public void onBindViewHolder(final KeyProfileHolder holder, int position) {
holder.setData(mKeyProfiles.get(position));
holder.updateCode();
lstHolders.add(holder);
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
@Override @Override
@ -103,13 +96,12 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
uiHandler.postDelayed(runnable, holder.keyProfile.Info.getMillisTillNextRotation()); uiHandler.postDelayed(runnable, holder.keyProfile.Info.getMillisTillNextRotation());
} }
// Return the size of your dataset (invoked by the layout manager)
@Override @Override
public int getItemCount() { public int getItemCount() {
return mKeyProfiles.size(); return mKeyProfiles.size();
} }
public static class KeyProfileHolder extends RecyclerView.ViewHolder { public static class KeyProfileHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView profileName; TextView profileName;
TextView profileCode; TextView profileCode;
ImageView profileDrawable; ImageView profileDrawable;
@ -122,6 +114,8 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
profileCode = (TextView) itemView.findViewById(R.id.profile_code); profileCode = (TextView) itemView.findViewById(R.id.profile_code);
profileDrawable = (ImageView) itemView.findViewById(R.id.ivTextDrawable); profileDrawable = (ImageView) itemView.findViewById(R.id.ivTextDrawable);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar); progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
itemView.setOnClickListener(this);
} }
public void setData(KeyProfile profile) { public void setData(KeyProfile profile) {
@ -148,16 +142,15 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
long millisTillRotation = keyProfile.Info.getMillisTillNextRotation(); long millisTillRotation = keyProfile.Info.getMillisTillNextRotation();
long period = keyProfile.Info.getPeriod() * 1000; long period = keyProfile.Info.getPeriod() * 1000;
int currentProgress = 1000 - (int)((((double)period - millisTillRotation) / period) * 1000); int currentProgress = 1000 - (int) ((((double) period - millisTillRotation) / period) * 1000);
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", currentProgress, 0); ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", currentProgress, 0);
animation.setDuration(millisTillRotation); animation.setDuration(millisTillRotation);
animation.setInterpolator(new LinearInterpolator()); animation.setInterpolator(new LinearInterpolator());
animation.start(); animation.start();
} }
private TextDrawable generateTextDrawable(KeyProfile profile) private TextDrawable generateTextDrawable(KeyProfile profile) {
{ if (profileName == null)
if(profileName == null)
return null; return null;
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
@ -167,5 +160,19 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
TextDrawable newDrawable = TextDrawable.builder().buildRound(profile.Name.substring(0, 1).toUpperCase(), profileKeyColor); TextDrawable newDrawable = TextDrawable.builder().buildRound(profile.Name.substring(0, 1).toUpperCase(), profileKeyColor);
return newDrawable; return newDrawable;
} }
@Override
public void onClick(View view) {
itemClickListener.onItemClick(getAdapterPosition(), view);
}
}
public void setOnItemClickListener(ItemClickListener clickListener) {
KeyProfileAdapter.itemClickListener = clickListener;
}
public interface ItemClickListener
{
void onItemClick(int position, View v);
} }
} }

View file

@ -8,7 +8,6 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
@ -17,11 +16,8 @@ import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper; import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.Toast; import android.widget.Toast;
import com.yarolegovich.lovelydialog.LovelyTextInputDialog;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -85,18 +81,14 @@ public class MainActivity extends AppCompatActivity {
rvKeyProfiles.setLayoutManager(mLayoutManager); rvKeyProfiles.setLayoutManager(mLayoutManager);
final Context context = this.getApplicationContext(); final Context context = this.getApplicationContext();
/*rvKeyProfiles.addOnItemTouchListener(new RVHItemClickListener(this, new RVHItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("text/plain", mKeyProfiles.get(position).Code);
clipboard.setPrimaryClip(clip);
Toast.makeText(context, "Code successfully copied to the clipboard", Toast.LENGTH_SHORT).show();
}
}));*/
mKeyProfileAdapter = new KeyProfileAdapter(mKeyProfiles); mKeyProfileAdapter = new KeyProfileAdapter(mKeyProfiles);
mKeyProfileAdapter.setOnItemClickListener((position, v) -> {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("text/plain", mKeyProfiles.get(position).Code);
clipboard.setPrimaryClip(clip);
Toast.makeText(context, "Code successfully copied to the clipboard", Toast.LENGTH_SHORT).show();
});
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mKeyProfileAdapter); ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mKeyProfileAdapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback); ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
@ -112,9 +104,7 @@ public class MainActivity extends AppCompatActivity {
Collections.sort(mKeyProfiles, comparator); Collections.sort(mKeyProfiles, comparator);
try { try {
for (KeyProfile profile : database.getKeys()) { mKeyProfiles.addAll(database.getKeys());
mKeyProfiles.add(profile);
}
mKeyProfileAdapter.notifyDataSetChanged(); mKeyProfileAdapter.notifyDataSetChanged();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -1,6 +1,8 @@
package me.impy.aegis.helpers; package me.impy.aegis.helpers;
public interface ItemClickListener<M, V> import android.view.View;
public interface ItemClickListener
{ {
void onItemClicked(M item, V view); void onItemClick(int position, View v);
} }