2019-02-07 22:39:33 +01:00
|
|
|
package com.beemdevelopment.aegis.ui;
|
2018-12-16 22:57:04 +01:00
|
|
|
|
|
|
|
import android.os.Bundle;
|
2022-10-10 22:32:30 +02:00
|
|
|
import android.view.Menu;
|
2018-12-17 23:33:13 +01:00
|
|
|
import android.view.MenuItem;
|
2020-08-13 18:16:23 +02:00
|
|
|
import android.view.View;
|
2018-12-16 22:57:04 +01:00
|
|
|
|
2022-10-10 22:32:30 +02:00
|
|
|
import androidx.activity.OnBackPressedCallback;
|
|
|
|
import androidx.annotation.NonNull;
|
2021-01-17 13:12:43 +01:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2019-04-04 14:07:36 +02:00
|
|
|
import com.beemdevelopment.aegis.R;
|
2021-01-27 13:54:11 +01:00
|
|
|
import com.beemdevelopment.aegis.ui.dialogs.Dialogs;
|
2019-04-04 14:07:36 +02:00
|
|
|
import com.beemdevelopment.aegis.ui.views.GroupAdapter;
|
2022-10-10 22:32:30 +02:00
|
|
|
import com.beemdevelopment.aegis.vault.VaultEntry;
|
2019-04-04 14:07:36 +02:00
|
|
|
|
2018-12-18 22:46:35 +01:00
|
|
|
import java.util.ArrayList;
|
2022-10-10 22:32:30 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
2018-12-16 22:57:04 +01:00
|
|
|
|
|
|
|
public class GroupManagerActivity extends AegisActivity implements GroupAdapter.Listener {
|
|
|
|
private GroupAdapter _adapter;
|
2022-10-10 22:32:30 +02:00
|
|
|
private HashSet<String> _removedGroups;
|
2023-09-09 18:26:40 +02:00
|
|
|
private RecyclerView _groupsView;
|
2020-08-13 18:16:23 +02:00
|
|
|
private View _emptyStateView;
|
2022-10-10 22:32:30 +02:00
|
|
|
private BackPressHandler _backPressHandler;
|
2018-12-16 22:57:04 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2022-02-06 19:00:01 +01:00
|
|
|
if (abortIfOrphan(savedInstanceState)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-16 22:57:04 +01:00
|
|
|
setContentView(R.layout.activity_groups);
|
2021-01-17 13:12:43 +01:00
|
|
|
setSupportActionBar(findViewById(R.id.toolbar));
|
2020-05-24 15:09:54 +02:00
|
|
|
if (getSupportActionBar() != null) {
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
|
|
|
}
|
2022-10-10 22:32:30 +02:00
|
|
|
_backPressHandler = new BackPressHandler();
|
|
|
|
getOnBackPressedDispatcher().addCallback(this, _backPressHandler);
|
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
List<String> groups = savedInstanceState.getStringArrayList("removedGroups");
|
|
|
|
_removedGroups = new HashSet<>(Objects.requireNonNull(groups));
|
|
|
|
} else {
|
|
|
|
_removedGroups = new HashSet<>();
|
|
|
|
}
|
2018-12-16 22:57:04 +01:00
|
|
|
|
|
|
|
_adapter = new GroupAdapter(this);
|
2023-09-09 18:26:40 +02:00
|
|
|
_groupsView = findViewById(R.id.list_groups);
|
2018-12-16 22:57:04 +01:00
|
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
2023-09-09 18:26:40 +02:00
|
|
|
_groupsView.setLayoutManager(layoutManager);
|
|
|
|
_groupsView.setAdapter(_adapter);
|
|
|
|
_groupsView.setNestedScrollingEnabled(false);
|
2018-12-16 22:57:04 +01:00
|
|
|
|
2022-10-10 22:32:30 +02:00
|
|
|
for (String group : _vaultManager.getVault().getGroups()) {
|
2018-12-16 22:57:04 +01:00
|
|
|
_adapter.addGroup(group);
|
|
|
|
}
|
2020-08-13 18:16:23 +02:00
|
|
|
|
|
|
|
_emptyStateView = findViewById(R.id.vEmptyList);
|
|
|
|
updateEmptyState();
|
|
|
|
}
|
|
|
|
|
2022-10-10 22:32:30 +02:00
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.putStringArrayList("removedGroups", new ArrayList<>(_removedGroups));
|
2018-12-16 22:57:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRemoveGroup(String group) {
|
2018-12-16 23:08:53 +01:00
|
|
|
Dialogs.showSecureDialog(new AlertDialog.Builder(this)
|
|
|
|
.setTitle(R.string.remove_group)
|
|
|
|
.setMessage(R.string.remove_group_description)
|
|
|
|
.setPositiveButton(android.R.string.yes, (dialog, whichButton) -> {
|
2022-10-10 22:32:30 +02:00
|
|
|
_removedGroups.add(group);
|
2018-12-16 23:08:53 +01:00
|
|
|
_adapter.removeGroup(group);
|
2022-10-10 22:32:30 +02:00
|
|
|
_backPressHandler.setEnabled(true);
|
2020-08-13 18:16:23 +02:00
|
|
|
updateEmptyState();
|
2018-12-16 23:08:53 +01:00
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.no, null)
|
|
|
|
.create());
|
2018-12-16 22:57:04 +01:00
|
|
|
}
|
2018-12-17 23:33:13 +01:00
|
|
|
|
2022-10-10 22:32:30 +02:00
|
|
|
private void saveAndFinish() {
|
|
|
|
if (!_removedGroups.isEmpty()) {
|
|
|
|
for (VaultEntry entry : _vaultManager.getVault().getEntries()) {
|
|
|
|
if (_removedGroups.contains(entry.getGroup())) {
|
|
|
|
entry.setGroup(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
saveAndBackupVault();
|
|
|
|
}
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void discardAndFinish() {
|
|
|
|
if (_removedGroups.isEmpty()) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dialogs.showDiscardDialog(this,
|
|
|
|
(dialog, which) -> saveAndFinish(),
|
|
|
|
(dialog, which) -> finish());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.menu_groups, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-17 23:33:13 +01:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
2022-10-10 22:32:30 +02:00
|
|
|
discardAndFinish();
|
|
|
|
break;
|
|
|
|
case R.id.action_save:
|
|
|
|
saveAndFinish();
|
2018-12-17 23:33:13 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-18 22:46:35 +01:00
|
|
|
|
2022-10-10 22:32:30 +02:00
|
|
|
private void updateEmptyState() {
|
|
|
|
if (_adapter.getItemCount() > 0) {
|
2023-09-09 18:26:40 +02:00
|
|
|
_groupsView.setVisibility(View.VISIBLE);
|
2022-10-10 22:32:30 +02:00
|
|
|
_emptyStateView.setVisibility(View.GONE);
|
|
|
|
} else {
|
2023-09-09 18:26:40 +02:00
|
|
|
_groupsView.setVisibility(View.GONE);
|
2022-10-10 22:32:30 +02:00
|
|
|
_emptyStateView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class BackPressHandler extends OnBackPressedCallback {
|
|
|
|
public BackPressHandler() {
|
|
|
|
super(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleOnBackPressed() {
|
|
|
|
discardAndFinish();
|
|
|
|
}
|
2018-12-18 22:46:35 +01:00
|
|
|
}
|
2018-12-16 22:57:04 +01:00
|
|
|
}
|