diff --git a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java index 6240c383..d80ddbb5 100644 --- a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java +++ b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java @@ -26,13 +26,13 @@ public class KeyProfileAdapter extends RecyclerView.Adapter _holders; private ArrayList _keyProfiles; private Handler _uiHandler; - private static ItemClickListener _itemClickListener; - private static LongItemClickListener _longItemClickListener; + private static Listener _listener; - public KeyProfileAdapter(ArrayList keyProfiles) { + public KeyProfileAdapter(ArrayList 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 = 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); + } + } } diff --git a/app/src/main/java/me/impy/aegis/db/Database.java b/app/src/main/java/me/impy/aegis/db/Database.java index 67d4bca9..406d3157 100644 --- a/app/src/main/java/me/impy/aegis/db/Database.java +++ b/app/src/main/java/me/impy/aegis/db/Database.java @@ -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 getKeys() { return Collections.unmodifiableList(_entries); } diff --git a/app/src/main/java/me/impy/aegis/db/DatabaseEntry.java b/app/src/main/java/me/impy/aegis/db/DatabaseEntry.java index fdbe77dc..1f617bd8 100644 --- a/app/src/main/java/me/impy/aegis/db/DatabaseEntry.java +++ b/app/src/main/java/me/impy/aegis/db/DatabaseEntry.java @@ -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; - } } diff --git a/app/src/main/java/me/impy/aegis/db/DatabaseFile.java b/app/src/main/java/me/impy/aegis/db/DatabaseFile.java index 07beb9ce..98c5df1f 100644 --- a/app/src/main/java/me/impy/aegis/db/DatabaseFile.java +++ b/app/src/main/java/me/impy/aegis/db/DatabaseFile.java @@ -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; diff --git a/app/src/main/java/me/impy/aegis/db/DatabaseManager.java b/app/src/main/java/me/impy/aegis/db/DatabaseManager.java index 31733cdc..8e372f0f 100644 --- a/app/src/main/java/me/impy/aegis/db/DatabaseManager.java +++ b/app/src/main/java/me/impy/aegis/db/DatabaseManager.java @@ -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 getKeys() throws Exception { assertDecrypted(); return _db.getKeys(); diff --git a/build.gradle b/build.gradle index ef163662..f81445ae 100644 --- a/build.gradle +++ b/build.gradle @@ -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' - } } }