Skip to content

Commit 9bae4d6

Browse files
Add ability to sort based on last used timestamp
1 parent dfd720b commit 9bae4d6

File tree

11 files changed

+130
-3
lines changed

11 files changed

+130
-3
lines changed

app/src/main/java/com/beemdevelopment/aegis/Preferences.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,51 @@ public void resetUsageCount(UUID uuid) {
235235
setUsageCount(usageCounts);
236236
}
237237

238+
public long getLastUsedTimestamp(UUID uuid) {
239+
Map<UUID, Long> timestamps = getLastUsedTimestamps();
240+
if (timestamps != null && timestamps.size() > 0){
241+
Long timestamp = timestamps.get(uuid);
242+
return timestamp != null ? timestamp : 0;
243+
}
244+
245+
return 0;
246+
}
247+
238248
public void clearUsageCount() {
239249
_prefs.edit().remove("pref_usage_count").apply();
240250
}
241251

252+
public Map<UUID, Long> getLastUsedTimestamps() {
253+
Map<UUID, Long> lastUsedTimestamps = new HashMap<>();
254+
String lastUsedTimestamp = _prefs.getString("pref_last_used_timestamps", "");
255+
try {
256+
JSONArray arr = new JSONArray(lastUsedTimestamp);
257+
for (int i = 0; i < arr.length(); i++) {
258+
JSONObject json = arr.getJSONObject(i);
259+
lastUsedTimestamps.put(UUID.fromString(json.getString("uuid")), json.getLong("timestamp"));
260+
}
261+
} catch (JSONException ignored) {
262+
}
263+
264+
return lastUsedTimestamps;
265+
}
266+
267+
public void setLastUsedTimestamps(Map<UUID, Long> lastUsedTimestamps) {
268+
JSONArray lastUsedTimestampJson = new JSONArray();
269+
for (Map.Entry<UUID, Long> entry : lastUsedTimestamps.entrySet()) {
270+
JSONObject entryJson = new JSONObject();
271+
try {
272+
entryJson.put("uuid", entry.getKey());
273+
entryJson.put("timestamp", entry.getValue());
274+
lastUsedTimestampJson.put(entryJson);
275+
} catch (JSONException e) {
276+
e.printStackTrace();
277+
}
278+
}
279+
280+
_prefs.edit().putString("pref_last_used_timestamps", lastUsedTimestampJson.toString()).apply();
281+
}
282+
242283
public Map<UUID, Integer> getUsageCounts() {
243284
Map<UUID, Integer> usageCounts = new HashMap<>();
244285
String usageCount = _prefs.getString("pref_usage_count", "");

app/src/main/java/com/beemdevelopment/aegis/SortCategory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.beemdevelopment.aegis;
22

3+
import com.beemdevelopment.aegis.helpers.comparators.LastUsedComparator;
34
import com.beemdevelopment.aegis.helpers.comparators.UsageCountComparator;
45
import com.beemdevelopment.aegis.vault.VaultEntry;
56
import com.beemdevelopment.aegis.helpers.comparators.AccountNameComparator;
@@ -14,7 +15,8 @@ public enum SortCategory {
1415
ACCOUNT_REVERSED,
1516
ISSUER,
1617
ISSUER_REVERSED,
17-
USAGE_COUNT;
18+
USAGE_COUNT,
19+
LAST_USED;
1820

1921
private static SortCategory[] _values;
2022

@@ -45,6 +47,8 @@ public Comparator<VaultEntry> getComparator() {
4547
case USAGE_COUNT:
4648
comparator = Collections.reverseOrder(new UsageCountComparator());
4749
break;
50+
case LAST_USED:
51+
comparator = Collections.reverseOrder(new LastUsedComparator());
4852
}
4953

5054
return comparator;
@@ -64,6 +68,8 @@ public int getMenuItem() {
6468
return R.id.menu_sort_alphabetically_reverse;
6569
case USAGE_COUNT:
6670
return R.id.menu_sort_usage_count;
71+
case LAST_USED:
72+
return R.id.menu_sort_last_used;
6773
default:
6874
return R.id.menu_sort_custom;
6975
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.beemdevelopment.aegis.helpers.comparators;
2+
3+
import com.beemdevelopment.aegis.vault.VaultEntry;
4+
5+
import java.util.Comparator;
6+
7+
public class LastUsedComparator implements Comparator<VaultEntry> {
8+
@Override
9+
public int compare(VaultEntry a, VaultEntry b) {
10+
return Long.compare(a.getLastUsedTimestamp(), b.getLastUsedTimestamp());
11+
}
12+
}

app/src/main/java/com/beemdevelopment/aegis/ui/EditEntryActivity.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.widget.ImageView;
2020
import android.widget.LinearLayout;
2121
import android.widget.RelativeLayout;
22+
import android.widget.TextView;
2223

2324
import androidx.activity.OnBackPressedCallback;
2425
import androidx.activity.result.ActivityResultLauncher;
@@ -75,10 +76,12 @@
7576
import java.io.File;
7677
import java.io.FileInputStream;
7778
import java.io.IOException;
79+
import java.text.DateFormat;
7880
import java.util.ArrayList;
7981
import java.util.Collection;
8082
import java.util.Collections;
8183
import java.util.Comparator;
84+
import java.util.Date;
8285
import java.util.HashSet;
8386
import java.util.List;
8487
import java.util.Locale;
@@ -113,6 +116,7 @@ public class EditEntryActivity extends AegisActivity {
113116
private LinearLayout _textPinLayout;
114117
private TextInputEditText _textUsageCount;
115118
private TextInputEditText _textNote;
119+
private TextView _textLastUsed;
116120

117121
private AutoCompleteTextView _dropdownType;
118122
private AutoCompleteTextView _dropdownAlgo;
@@ -200,6 +204,7 @@ protected void onCreate(Bundle savedInstanceState) {
200204
_textPinLayout = findViewById(R.id.layout_pin);
201205
_textUsageCount = findViewById(R.id.text_usage_count);
202206
_textNote = findViewById(R.id.text_note);
207+
_textLastUsed = findViewById(R.id.text_last_used);
203208
_dropdownType = findViewById(R.id.dropdown_type);
204209
DropdownHelper.fillDropdown(this, _dropdownType, R.array.otp_types_array);
205210
_dropdownAlgoLayout = findViewById(R.id.dropdown_algo_layout);
@@ -378,6 +383,7 @@ protected void onCreate(Bundle savedInstanceState) {
378383
});
379384

380385
_textUsageCount.setText(_prefs.getUsageCount(entryUUID).toString());
386+
setLastUsedTimestamp(_prefs.getLastUsedTimestamp(entryUUID));
381387
}
382388

383389
private void updateAdvancedFieldStatus(String otpType) {
@@ -608,6 +614,16 @@ private void addAndFinish(VaultEntry entry) {
608614
saveAndFinish(entry, false);
609615
}
610616

617+
private void setLastUsedTimestamp(long timestamp) {
618+
String readableDate = getString(R.string.last_used_never);
619+
if (timestamp != 0) {
620+
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.getDefault());
621+
readableDate = dateFormat.format(new Date(timestamp));
622+
}
623+
624+
_textLastUsed.setText(String.format("%s: %s", getString(R.string.last_used), readableDate));
625+
}
626+
611627
private void deleteAndFinish(VaultEntry entry) {
612628
_vaultManager.getVault().removeEntry(entry);
613629
saveAndFinish(entry, true);

app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ protected void onPause() {
241241
_prefs.setUsageCount(usageMap);
242242
}
243243

244+
Map<UUID, Long> lastUsedMap = _entryListView.getLastUsedTimestamps();
245+
if (lastUsedMap != null) {
246+
_prefs.setLastUsedTimestamps(lastUsedMap);
247+
}
248+
244249
super.onPause();
245250
}
246251

@@ -696,6 +701,8 @@ protected void onResume() {
696701
// update the usage counts in case they are edited outside of the EntryListView
697702
_entryListView.setUsageCounts(_prefs.getUsageCounts());
698703

704+
_entryListView.setLastUsedTimestamps(_prefs.getLastUsedTimestamps());
705+
699706
// refresh all codes to prevent showing old ones
700707
_entryListView.refresh(false);
701708
} else {
@@ -825,6 +832,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
825832
sortCategory = SortCategory.ACCOUNT_REVERSED;
826833
} else if (subItemId == R.id.menu_sort_usage_count) {
827834
sortCategory = SortCategory.USAGE_COUNT;
835+
} else if (subItemId == R.id.menu_sort_last_used) {
836+
sortCategory = SortCategory.LAST_USED;
828837
} else {
829838
sortCategory = SortCategory.CUSTOM;
830839
}
@@ -847,6 +856,7 @@ private void collapseSearchView() {
847856
private void loadEntries() {
848857
if (!_loaded) {
849858
_entryListView.setUsageCounts(_prefs.getUsageCounts());
859+
_entryListView.setLastUsedTimestamps(_prefs.getLastUsedTimestamps());
850860
_entryListView.addEntries(_vaultManager.getVault().getEntries());
851861
_entryListView.runEntriesAnimation();
852862
_loaded = true;

app/src/main/java/com/beemdevelopment/aegis/ui/views/EntryAdapter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Collection;
3838
import java.util.Collections;
3939
import java.util.Comparator;
40+
import java.util.Date;
4041
import java.util.HashMap;
4142
import java.util.List;
4243
import java.util.Map;
@@ -51,6 +52,7 @@ public class EntryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
5152
private List<VaultEntry> _shownEntries;
5253
private List<VaultEntry> _selectedEntries;
5354
private Map<UUID, Integer> _usageCounts;
55+
private Map<UUID, Long> _lastUsedTimestamps;
5456
private VaultEntry _focusedEntry;
5557
private VaultEntry _clickedEntry;
5658
private Preferences.CodeGrouping _codeGroupSize;
@@ -190,6 +192,7 @@ public int addEntry(VaultEntry entry) {
190192
public void addEntries(Collection<VaultEntry> entries) {
191193
for (VaultEntry entry: entries) {
192194
entry.setUsageCount(_usageCounts.containsKey(entry.getUUID()) ? _usageCounts.get(entry.getUUID()) : 0);
195+
entry.setLastUsedTimestamp(_lastUsedTimestamps.containsKey(entry.getUUID()) ? _lastUsedTimestamps.get(entry.getUUID()) : 0);
193196
}
194197

195198
_entries.addAll(entries);
@@ -407,6 +410,10 @@ public void setViewMode(ViewMode viewMode) {
407410

408411
public Map<UUID, Integer> getUsageCounts() { return _usageCounts; }
409412

413+
public void setLastUsedTimestamps(Map<UUID, Long> lastUsedTimestamps) { _lastUsedTimestamps = lastUsedTimestamps; }
414+
415+
public Map<UUID, Long> getLastUsedTimestamps() { return _lastUsedTimestamps; }
416+
410417
public int getShownFavoritesCount() {
411418
return (int) _shownEntries.stream().filter(VaultEntry::isFavorite).count();
412419
}
@@ -805,6 +812,8 @@ private void incrementUsageCount(VaultEntry entry) {
805812
int usageCount = _usageCounts.get(entry.getUUID());
806813
_usageCounts.put(entry.getUUID(), ++usageCount);
807814
}
815+
816+
_lastUsedTimestamps.put(entry.getUUID(), new Date().getTime());
808817
}
809818

810819
public boolean isDragAndDropAllowed() {

app/src/main/java/com/beemdevelopment/aegis/ui/views/EntryListView.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ public Map<UUID, Integer> getUsageCounts() {
223223
return _adapter.getUsageCounts();
224224
}
225225

226+
public void setLastUsedTimestamps(Map<UUID, Long> lastUsedTimestamps) {
227+
_adapter.setLastUsedTimestamps(lastUsedTimestamps);
228+
}
229+
230+
public Map<UUID, Long> getLastUsedTimestamps() {
231+
return _adapter.getLastUsedTimestamps();
232+
}
233+
226234
public void setSearchFilter(String search) {
227235
_adapter.setSearchFilter(search);
228236
_touchCallback.setIsLongPressDragEnabled(_adapter.isDragAndDropAllowed());

app/src/main/java/com/beemdevelopment/aegis/vault/VaultEntry.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class VaultEntry extends UUIDMap.Value {
2323
private VaultEntryIcon _icon;
2424
private boolean _isFavorite;
2525
private int _usageCount;
26+
private long _lastUsedTimestamp;
2627
private String _note = "";
2728
private String _oldGroup;
2829
private Set<UUID> _groups = new TreeSet<>();
@@ -135,6 +136,10 @@ public int getUsageCount() {
135136
return _usageCount;
136137
}
137138

139+
public long getLastUsedTimestamp() {
140+
return _lastUsedTimestamp;
141+
}
142+
138143
public String getNote() {
139144
return _note;
140145
}
@@ -143,8 +148,6 @@ public boolean isFavorite() {
143148
return _isFavorite;
144149
}
145150

146-
;
147-
148151
public void setName(String name) {
149152
_name = name;
150153
}
@@ -187,6 +190,8 @@ public void setUsageCount(int usageCount) {
187190
_usageCount = usageCount;
188191
}
189192

193+
public void setLastUsedTimestamp(long lastUsedTimestamp) { _lastUsedTimestamp = lastUsedTimestamp; }
194+
190195
public void setNote(String note) {
191196
_note = note;
192197
}

app/src/main/res/layout/activity_edit_entry.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,22 @@
376376
</com.google.android.material.textfield.TextInputLayout>
377377
</LinearLayout>
378378
</LinearLayout>
379+
<TextView
380+
android:id="@+id/text_last_used"
381+
android:layout_width="wrap_content"
382+
android:layout_height="wrap_content"
383+
android:paddingTop="16dp"
384+
android:paddingBottom="16dp"
385+
android:layout_centerInParent="true"
386+
android:layout_gravity="bottom|center"
387+
android:textSize="14sp" />
379388
</LinearLayout>
389+
390+
380391
</RelativeLayout>
392+
381393
</LinearLayout>
382394
</ScrollView>
395+
396+
383397
</androidx.coordinatorlayout.widget.CoordinatorLayout>

app/src/main/res/menu/menu_main.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
<item
4141
android:id="@+id/menu_sort_usage_count"
4242
android:title="@string/sort_usage_count"/>
43+
<item
44+
android:id="@+id/menu_sort_last_used"
45+
android:title="@string/sort_last_used"/>
4346
</group>
4447
</menu>
4548
</item>

0 commit comments

Comments
 (0)