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 ArrayList<KeyProfile> _keyProfiles;
private Handler _uiHandler;
private static ItemClickListener _itemClickListener;
private static LongItemClickListener _longItemClickListener;
private static Listener _listener;
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) {
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles, Listener listener) {
_keyProfiles = keyProfiles;
_holders = new ArrayList<>();
_uiHandler = new Handler();
_listener = listener;
}
@Override
@ -42,20 +42,18 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
@Override
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);
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
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;
return new KeyProfileHolder(v);
}
@Override
@ -64,7 +62,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
holder.updateCode();
_holders.add(holder);
Runnable runnable = new Runnable() {
_uiHandler.postDelayed(new Runnable() {
@Override
public void run() {
// 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(runnable, holder._keyProfile.getEntry().getInfo().getMillisTillNextRotation());
}, holder._keyProfile.getEntry().getInfo().getMillisTillNextRotation());
}
@Override
@ -83,14 +80,14 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
return _keyProfiles.size();
}
public static class KeyProfileHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
TextView _profileName;
TextView _profileCode;
TextView _profileIssuer;
ImageView _profileDrawable;
KeyProfile _keyProfile;
ProgressBar _progressBar;
View _itemView;
public class KeyProfileHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private TextView _profileName;
private TextView _profileCode;
private TextView _profileIssuer;
private ImageView _profileDrawable;
private KeyProfile _keyProfile;
private ProgressBar _progressBar;
private View _itemView;
KeyProfileHolder(final View itemView) {
super(itemView);
@ -114,8 +111,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
_profileIssuer.setText("");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(_itemView.getContext());
if(sharedPreferences.getBoolean("pref_issuer", false))
{
if (sharedPreferences.getBoolean("pref_issuer", false)) {
_profileIssuer.setText(" - " + profile.getEntry().getInfo().getIssuer());
}
@ -140,8 +136,9 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
}
private TextDrawable generateTextDrawable(KeyProfile profile) {
if (_profileName == null)
if (_profileName == null) {
return null;
}
ColorGenerator generator = ColorGenerator.MATERIAL;
int profileKeyColor = generator.getColor(profile.getEntry().getName());
@ -151,33 +148,18 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
@Override
public void onClick(View view) {
if (_itemClickListener != null) {
_itemClickListener.onItemClick(getAdapterPosition(), view);
}
_listener.onKeyProfileClick(_keyProfiles.get(getAdapterPosition()));
}
@Override
public boolean onLongClick(View view) {
if (_longItemClickListener != null) {
_longItemClickListener.onLongItemClick(getAdapterPosition(), view);
}
return true;
return _listener.onLongKeyProfileClick(_keyProfiles.get(getAdapterPosition()));
}
}
public void setOnItemClickListener(ItemClickListener clickListener) {
KeyProfileAdapter._itemClickListener = clickListener;
}
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);
public interface Listener {
void onKeyProfileClick(KeyProfile profile);
boolean onLongKeyProfileClick(KeyProfile profile);
void onKeyProfileMove(KeyProfile profile1, KeyProfile profile2);
}
}

View File

@ -42,7 +42,7 @@ import me.impy.aegis.importers.DatabaseImporter;
import me.impy.aegis.helpers.SimpleItemTouchHelperCallback;
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_ADD_KEYINFO = 1;
private static final int CODE_DO_INTRO = 2;
@ -114,8 +114,7 @@ public class MainActivity extends AppCompatActivity {
rvKeyProfiles.setLayoutManager(mLayoutManager);
_keyProfiles = new ArrayList<>();
_keyProfileAdapter = new KeyProfileAdapter(_keyProfiles);
_keyProfileAdapter.setOnItemClickListener((position, v) -> createBottomSheet(position).show());
_keyProfileAdapter = new KeyProfileAdapter(_keyProfiles, this);
if (_db.isDecrypted()) {
loadKeyProfiles();
}
@ -275,7 +274,6 @@ public class MainActivity extends AppCompatActivity {
DatabaseEntry entry = profile.getEntry();
entry.setName(entry.getInfo().getAccountName());
entry.setOrder(_keyProfiles.size() + 1);
try {
_db.addKey(entry);
} catch (Exception e) {
@ -356,7 +354,7 @@ public class MainActivity extends AppCompatActivity {
super.onStop();
}
private BottomSheetDialog createBottomSheet(int position) {
private BottomSheetDialog createBottomSheet(KeyProfile profile) {
View bottomSheetView = getLayoutInflater().inflate(R.layout.bottom_sheet_edit_profile, null);
LinearLayout copyLayout = (LinearLayout) bottomSheetView.findViewById(R.id.copy_button);
LinearLayout deleteLayout = (LinearLayout) bottomSheetView.findViewById(R.id.delete_button);
@ -372,14 +370,14 @@ public class MainActivity extends AppCompatActivity {
copyLayout.setOnClickListener(view -> {
bottomDialog.dismiss();
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);
Toast.makeText(this.getApplicationContext(), "Code copied to the clipboard", Toast.LENGTH_SHORT).show();
});
deleteLayout.setOnClickListener(view -> {
bottomDialog.dismiss();
deleteProfile(position);
deleteProfile(profile);
});
editLayout.setOnClickListener(view -> {
@ -390,8 +388,7 @@ public class MainActivity extends AppCompatActivity {
return bottomDialog;
}
private void deleteProfile(int position) {
KeyProfile profile = _keyProfiles.get(position);
private void deleteProfile(KeyProfile profile) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Delete entry")
.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();
return;
}
_keyProfiles.remove(position);
int position = _keyProfiles.indexOf(profile);
_keyProfiles.remove(profile);
_keyProfileAdapter.notifyItemRemoved(position);
})
.setNegativeButton(android.R.string.no, null)
@ -513,9 +511,6 @@ public class MainActivity extends AppCompatActivity {
return;
}
Collections.sort(_keyProfiles, (p1, p2) -> {
return p1.getEntry().getOrder() >= p2.getEntry().getOrder() ? 1 : -1;
});
_keyProfileAdapter.notifyDataSetChanged();
}
@ -526,4 +521,24 @@ public class MainActivity extends AppCompatActivity {
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);
}
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) {
Collections.swap(_entries, _entries.indexOf(entry1), _entries.indexOf(entry2));
}
public List<DatabaseEntry> getKeys() {
return Collections.unmodifiableList(_entries);
}

View File

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

View File

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

View File

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

View File

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