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.db.Database;
import me.impy.aegis.helpers.ItemClickListener;
import me.impy.aegis.helpers.ItemTouchHelperAdapter;
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> implements ItemTouchHelperAdapter {
private final List<KeyProfileHolder> lstHolders;
private ArrayList<KeyProfile> mKeyProfiles;
private Handler uiHandler;
private static ItemClickListener itemClickListener;
// Provide a suitable constructor (depends on the kind of dataset)
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) {
mKeyProfiles = keyProfiles;
lstHolders = new ArrayList<>();
@ -41,7 +42,6 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
return;
}
// Helper functions you might want to implement to make changes in the list as an event is fired
private void remove(int position) {
mKeyProfiles.remove(position);
notifyItemRemoved(position);
@ -56,8 +56,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
adjustOrder(secondPosition);
}
private void adjustOrder(int startPosition)
{
private void adjustOrder(int startPosition) {
Comparator<KeyProfile> comparator = new Comparator<KeyProfile>() {
@Override
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;
}
}
// Create new views (invoked by the layout manager)
@Override
public KeyProfileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_keyprofile, parent, false);
// set the view's size, margins, paddings and layout parameters
@Override
public KeyProfileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_keyprofile, parent, false);
KeyProfileHolder vh = new KeyProfileHolder(v);
return vh;
}
KeyProfileHolder vh = new KeyProfileHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final KeyProfileHolder holder, int position) {
holder.setData(mKeyProfiles.get(position));
holder.updateCode();
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() {
@Override
@ -103,13 +96,12 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
uiHandler.postDelayed(runnable, holder.keyProfile.Info.getMillisTillNextRotation());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mKeyProfiles.size();
}
public static class KeyProfileHolder extends RecyclerView.ViewHolder {
public static class KeyProfileHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView profileName;
TextView profileCode;
ImageView profileDrawable;
@ -122,6 +114,8 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
profileCode = (TextView) itemView.findViewById(R.id.profile_code);
profileDrawable = (ImageView) itemView.findViewById(R.id.ivTextDrawable);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
itemView.setOnClickListener(this);
}
public void setData(KeyProfile profile) {
@ -148,16 +142,15 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
long millisTillRotation = keyProfile.Info.getMillisTillNextRotation();
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);
animation.setDuration(millisTillRotation);
animation.setInterpolator(new LinearInterpolator());
animation.start();
}
private TextDrawable generateTextDrawable(KeyProfile profile)
{
if(profileName == null)
private TextDrawable generateTextDrawable(KeyProfile profile) {
if (profileName == null)
return null;
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);
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.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.yarolegovich.lovelydialog.LovelyTextInputDialog;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -85,18 +81,14 @@ public class MainActivity extends AppCompatActivity {
rvKeyProfiles.setLayoutManager(mLayoutManager);
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.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 touchHelper = new ItemTouchHelper(callback);
@ -112,9 +104,7 @@ public class MainActivity extends AppCompatActivity {
Collections.sort(mKeyProfiles, comparator);
try {
for (KeyProfile profile : database.getKeys()) {
mKeyProfiles.add(profile);
}
mKeyProfiles.addAll(database.getKeys());
mKeyProfileAdapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();

View file

@ -1,6 +1,8 @@
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);
}