Fix key profile order and clean up KeyProfileAdapter a bit

This commit is contained in:
Alexander Bakker 2017-12-12 01:50:00 +01:00
parent f26dfac1b6
commit 461f321626
7 changed files with 66 additions and 81 deletions

View file

@ -26,13 +26,13 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
private final List<KeyProfileHolder> _holders; private final List<KeyProfileHolder> _holders;
private ArrayList<KeyProfile> _keyProfiles; private ArrayList<KeyProfile> _keyProfiles;
private Handler _uiHandler; private Handler _uiHandler;
private static ItemClickListener _itemClickListener; private static Listener _listener;
private static LongItemClickListener _longItemClickListener;
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) { public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles, Listener listener) {
_keyProfiles = keyProfiles; _keyProfiles = keyProfiles;
_holders = new ArrayList<>(); _holders = new ArrayList<>();
_uiHandler = new Handler(); _uiHandler = new Handler();
_listener = listener;
} }
@Override @Override
@ -42,20 +42,18 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
@Override @Override
public void onItemMove(int firstPosition, int secondPosition) { public void onItemMove(int firstPosition, int secondPosition) {
// notify the database first
_listener.onKeyProfileMove(_keyProfiles.get(firstPosition), _keyProfiles.get(secondPosition));
// update our side of things
Collections.swap(_keyProfiles, firstPosition, secondPosition); Collections.swap(_keyProfiles, firstPosition, secondPosition);
notifyItemMoved(firstPosition, secondPosition); notifyItemMoved(firstPosition, secondPosition);
// update the order of all key profiles
for (int i = 0; i < _keyProfiles.size(); i++) {
_keyProfiles.get(i).getEntry().setOrder(i + 1);
}
} }
@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); View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_keyprofile, parent, false);
KeyProfileHolder vh = new KeyProfileHolder(v); return new KeyProfileHolder(v);
return vh;
} }
@Override @Override
@ -64,7 +62,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
holder.updateCode(); holder.updateCode();
_holders.add(holder); _holders.add(holder);
Runnable runnable = new Runnable() { _uiHandler.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
// check if this key profile still exists // check if this key profile still exists
@ -74,8 +72,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
_uiHandler.postDelayed(this, holder._keyProfile.getEntry().getInfo().getPeriod() * 1000); _uiHandler.postDelayed(this, holder._keyProfile.getEntry().getInfo().getPeriod() * 1000);
} }
}; }, holder._keyProfile.getEntry().getInfo().getMillisTillNextRotation());
_uiHandler.postDelayed(runnable, holder._keyProfile.getEntry().getInfo().getMillisTillNextRotation());
} }
@Override @Override
@ -83,14 +80,14 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
return _keyProfiles.size(); return _keyProfiles.size();
} }
public static class KeyProfileHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { public class KeyProfileHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
TextView _profileName; private TextView _profileName;
TextView _profileCode; private TextView _profileCode;
TextView _profileIssuer; private TextView _profileIssuer;
ImageView _profileDrawable; private ImageView _profileDrawable;
KeyProfile _keyProfile; private KeyProfile _keyProfile;
ProgressBar _progressBar; private ProgressBar _progressBar;
View _itemView; private View _itemView;
KeyProfileHolder(final View itemView) { KeyProfileHolder(final View itemView) {
super(itemView); super(itemView);
@ -114,8 +111,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
_profileIssuer.setText(""); _profileIssuer.setText("");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(_itemView.getContext()); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(_itemView.getContext());
if(sharedPreferences.getBoolean("pref_issuer", false)) if (sharedPreferences.getBoolean("pref_issuer", false)) {
{
_profileIssuer.setText(" - " + profile.getEntry().getInfo().getIssuer()); _profileIssuer.setText(" - " + profile.getEntry().getInfo().getIssuer());
} }
@ -140,8 +136,9 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
} }
private TextDrawable generateTextDrawable(KeyProfile profile) { private TextDrawable generateTextDrawable(KeyProfile profile) {
if (_profileName == null) if (_profileName == null) {
return null; return null;
}
ColorGenerator generator = ColorGenerator.MATERIAL; ColorGenerator generator = ColorGenerator.MATERIAL;
int profileKeyColor = generator.getColor(profile.getEntry().getName()); int profileKeyColor = generator.getColor(profile.getEntry().getName());
@ -151,33 +148,18 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if (_itemClickListener != null) { _listener.onKeyProfileClick(_keyProfiles.get(getAdapterPosition()));
_itemClickListener.onItemClick(getAdapterPosition(), view);
}
} }
@Override @Override
public boolean onLongClick(View view) { public boolean onLongClick(View view) {
if (_longItemClickListener != null) { return _listener.onLongKeyProfileClick(_keyProfiles.get(getAdapterPosition()));
_longItemClickListener.onLongItemClick(getAdapterPosition(), view);
}
return true;
} }
} }
public void setOnItemClickListener(ItemClickListener clickListener) { public interface Listener {
KeyProfileAdapter._itemClickListener = clickListener; void onKeyProfileClick(KeyProfile profile);
} boolean onLongKeyProfileClick(KeyProfile profile);
void onKeyProfileMove(KeyProfile profile1, KeyProfile profile2);
public void setOnLongItemClickListener(LongItemClickListener clickListener) {
KeyProfileAdapter._longItemClickListener = clickListener;
}
public interface ItemClickListener {
void onItemClick(int position, View v);
}
public interface LongItemClickListener {
void onLongItemClick(int position, View v);
} }
} }

View file

@ -42,7 +42,7 @@ import me.impy.aegis.importers.DatabaseImporter;
import me.impy.aegis.helpers.SimpleItemTouchHelperCallback; import me.impy.aegis.helpers.SimpleItemTouchHelperCallback;
import me.impy.aegis.util.ByteInputStream; import me.impy.aegis.util.ByteInputStream;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity implements KeyProfileAdapter.Listener {
private static final int CODE_GET_KEYINFO = 0; private static final int CODE_GET_KEYINFO = 0;
private static final int CODE_ADD_KEYINFO = 1; private static final int CODE_ADD_KEYINFO = 1;
private static final int CODE_DO_INTRO = 2; private static final int CODE_DO_INTRO = 2;
@ -114,8 +114,7 @@ public class MainActivity extends AppCompatActivity {
rvKeyProfiles.setLayoutManager(mLayoutManager); rvKeyProfiles.setLayoutManager(mLayoutManager);
_keyProfiles = new ArrayList<>(); _keyProfiles = new ArrayList<>();
_keyProfileAdapter = new KeyProfileAdapter(_keyProfiles); _keyProfileAdapter = new KeyProfileAdapter(_keyProfiles, this);
_keyProfileAdapter.setOnItemClickListener((position, v) -> createBottomSheet(position).show());
if (_db.isDecrypted()) { if (_db.isDecrypted()) {
loadKeyProfiles(); loadKeyProfiles();
} }
@ -275,7 +274,6 @@ public class MainActivity extends AppCompatActivity {
DatabaseEntry entry = profile.getEntry(); DatabaseEntry entry = profile.getEntry();
entry.setName(entry.getInfo().getAccountName()); entry.setName(entry.getInfo().getAccountName());
entry.setOrder(_keyProfiles.size() + 1);
try { try {
_db.addKey(entry); _db.addKey(entry);
} catch (Exception e) { } catch (Exception e) {
@ -356,7 +354,7 @@ public class MainActivity extends AppCompatActivity {
super.onStop(); super.onStop();
} }
private BottomSheetDialog createBottomSheet(int position) { private BottomSheetDialog createBottomSheet(KeyProfile profile) {
View bottomSheetView = getLayoutInflater().inflate(R.layout.bottom_sheet_edit_profile, null); View bottomSheetView = getLayoutInflater().inflate(R.layout.bottom_sheet_edit_profile, null);
LinearLayout copyLayout = (LinearLayout) bottomSheetView.findViewById(R.id.copy_button); LinearLayout copyLayout = (LinearLayout) bottomSheetView.findViewById(R.id.copy_button);
LinearLayout deleteLayout = (LinearLayout) bottomSheetView.findViewById(R.id.delete_button); LinearLayout deleteLayout = (LinearLayout) bottomSheetView.findViewById(R.id.delete_button);
@ -372,14 +370,14 @@ public class MainActivity extends AppCompatActivity {
copyLayout.setOnClickListener(view -> { copyLayout.setOnClickListener(view -> {
bottomDialog.dismiss(); bottomDialog.dismiss();
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("text/plain", _keyProfiles.get(position).getCode()); ClipData clip = ClipData.newPlainText("text/plain", profile.getCode());
clipboard.setPrimaryClip(clip); clipboard.setPrimaryClip(clip);
Toast.makeText(this.getApplicationContext(), "Code copied to the clipboard", Toast.LENGTH_SHORT).show(); Toast.makeText(this.getApplicationContext(), "Code copied to the clipboard", Toast.LENGTH_SHORT).show();
}); });
deleteLayout.setOnClickListener(view -> { deleteLayout.setOnClickListener(view -> {
bottomDialog.dismiss(); bottomDialog.dismiss();
deleteProfile(position); deleteProfile(profile);
}); });
editLayout.setOnClickListener(view -> { editLayout.setOnClickListener(view -> {
@ -390,8 +388,7 @@ public class MainActivity extends AppCompatActivity {
return bottomDialog; return bottomDialog;
} }
private void deleteProfile(int position) { private void deleteProfile(KeyProfile profile) {
KeyProfile profile = _keyProfiles.get(position);
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(MainActivity.this)
.setTitle("Delete entry") .setTitle("Delete entry")
.setMessage("Are you sure you want to delete this profile?") .setMessage("Are you sure you want to delete this profile?")
@ -403,7 +400,8 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(this, "An error occurred while trying to delete an entry", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "An error occurred while trying to delete an entry", Toast.LENGTH_SHORT).show();
return; return;
} }
_keyProfiles.remove(position); int position = _keyProfiles.indexOf(profile);
_keyProfiles.remove(profile);
_keyProfileAdapter.notifyItemRemoved(position); _keyProfileAdapter.notifyItemRemoved(position);
}) })
.setNegativeButton(android.R.string.no, null) .setNegativeButton(android.R.string.no, null)
@ -513,9 +511,6 @@ public class MainActivity extends AppCompatActivity {
return; return;
} }
Collections.sort(_keyProfiles, (p1, p2) -> {
return p1.getEntry().getOrder() >= p2.getEntry().getOrder() ? 1 : -1;
});
_keyProfileAdapter.notifyDataSetChanged(); _keyProfileAdapter.notifyDataSetChanged();
} }
@ -526,4 +521,24 @@ public class MainActivity extends AppCompatActivity {
item.setVisible(_db.getFile().isEncrypted()); item.setVisible(_db.getFile().isEncrypted());
} }
} }
@Override
public void onKeyProfileClick(KeyProfile profile) {
createBottomSheet(profile).show();
}
@Override
public boolean onLongKeyProfileClick(KeyProfile profile) {
return false;
}
@Override
public void onKeyProfileMove(KeyProfile profile1, KeyProfile profile2) {
try {
_db.swapKeys(profile1.getEntry(), profile2.getEntry());
} catch (Exception e) {
e.printStackTrace();
throw new UndeclaredThrowableException(e);
}
}
} }

View file

@ -49,6 +49,10 @@ public class Database {
_entries.remove(entry); _entries.remove(entry);
} }
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) {
Collections.swap(_entries, _entries.indexOf(entry1), _entries.indexOf(entry2));
}
public List<DatabaseEntry> getKeys() { public List<DatabaseEntry> getKeys() {
return Collections.unmodifiableList(_entries); return Collections.unmodifiableList(_entries);
} }

View file

@ -11,7 +11,6 @@ public class DatabaseEntry implements Serializable {
public String _name = ""; public String _name = "";
public String _icon = ""; public String _icon = "";
public KeyInfo _info; public KeyInfo _info;
public int _order;
public DatabaseEntry(KeyInfo info) { public DatabaseEntry(KeyInfo info) {
_info = info; _info = info;
@ -21,14 +20,12 @@ public class DatabaseEntry implements Serializable {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("name", _name); obj.put("name", _name);
obj.put("url", _info.getURL()); obj.put("url", _info.getURL());
obj.put("order", _order);
return obj; return obj;
} }
public void deserialize(JSONObject obj) throws Exception { public void deserialize(JSONObject obj) throws Exception {
_name = obj.getString("name"); _name = obj.getString("name");
_info = KeyInfo.fromURL(obj.getString("url")); _info = KeyInfo.fromURL(obj.getString("url"));
_order = obj.getInt("order");
} }
public String getName() { public String getName() {
@ -40,9 +37,6 @@ public class DatabaseEntry implements Serializable {
public KeyInfo getInfo() { public KeyInfo getInfo() {
return _info; return _info;
} }
public int getOrder() {
return _order;
}
public void setName(String name) { public void setName(String name) {
_name = name; _name = name;
@ -53,7 +47,4 @@ public class DatabaseEntry implements Serializable {
public void setInfo(KeyInfo info) { public void setInfo(KeyInfo info) {
_info = info; _info = info;
} }
public void setOrder(int order) {
_order = order;
}
} }

View file

@ -1,13 +1,7 @@
package me.impy.aegis.db; package me.impy.aegis.db;
import android.content.Context;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException; import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays; import java.util.Arrays;

View file

@ -136,6 +136,11 @@ public class DatabaseManager {
_db.removeKey(entry); _db.removeKey(entry);
} }
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) throws Exception {
assertDecrypted();
_db.swapKeys(entry1, entry2);
}
public List<DatabaseEntry> getKeys() throws Exception { public List<DatabaseEntry> getKeys() throws Exception {
assertDecrypted(); assertDecrypted();
return _db.getKeys(); return _db.getKeys();

View file

@ -3,10 +3,7 @@
buildscript { buildscript {
repositories { repositories {
jcenter() jcenter()
maven { google()
url 'https://maven.google.com/'
name 'Google'
}
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.0.1' classpath 'com.android.tools.build:gradle:3.0.1'
@ -19,11 +16,8 @@ buildscript {
allprojects { allprojects {
repositories { repositories {
jcenter() jcenter()
google()
maven { url 'https://jitpack.io' } maven { url 'https://jitpack.io' }
maven {
url 'https://maven.google.com/'
name 'Google'
}
} }
} }