mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Refactor launch method
This commit is contained in:
@@ -139,6 +139,24 @@ public class GroupActivity extends LockingActivity
|
||||
|
||||
private int iconColor;
|
||||
|
||||
private static void buildAndLaunchIntent(Activity activity, PwGroup group, boolean readOnly,
|
||||
IntentBuildLauncher intentBuildLauncher) {
|
||||
if (TimeoutHelper.INSTANCE.checkTime(activity)) {
|
||||
Intent intent = new Intent(activity, GroupActivity.class);
|
||||
if (group != null) {
|
||||
intent.putExtra(GROUP_ID_KEY, group.getId());
|
||||
}
|
||||
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly);
|
||||
intentBuildLauncher.launchActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* -------------------------
|
||||
* Standard Launch
|
||||
* -------------------------
|
||||
*/
|
||||
|
||||
// After a database creation
|
||||
public static void launch(Activity act) {
|
||||
launch(act, READ_ONLY_DEFAULT);
|
||||
@@ -149,23 +167,18 @@ public class GroupActivity extends LockingActivity
|
||||
launch(act, null, readOnly);
|
||||
}
|
||||
|
||||
private static void buildAndLaunchIntent(Activity activity, PwGroup group, boolean readOnly,
|
||||
IntentBuildLauncher intentBuildLauncher) {
|
||||
if (TimeoutHelper.INSTANCE.checkTime(activity)) {
|
||||
Intent intent = new Intent(activity, GroupActivity.class);
|
||||
if (group != null) {
|
||||
intent.putExtra(GROUP_ID_KEY, group.getId());
|
||||
}
|
||||
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly);
|
||||
intentBuildLauncher.startActivityForResult(intent);
|
||||
}
|
||||
}
|
||||
|
||||
public static void launch(Activity activity, PwGroup group, boolean readOnly) {
|
||||
buildAndLaunchIntent(activity, group, readOnly,
|
||||
(intent) -> activity.startActivityForResult(intent, 0));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* -------------------------
|
||||
* Keyboard Launch
|
||||
* -------------------------
|
||||
*/
|
||||
|
||||
public static void launchForKeyboardResult(Activity activity, boolean readOnly) {
|
||||
TimeoutHelper.INSTANCE.recordTime(activity);
|
||||
launchForKeyboardResult(activity, null, readOnly);
|
||||
@@ -179,6 +192,12 @@ public class GroupActivity extends LockingActivity
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* -------------------------
|
||||
* Autofill Launch
|
||||
* -------------------------
|
||||
*/
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public static void launchForAutofillResult(Activity activity, AssistStructure assistStructure, boolean readOnly) {
|
||||
if ( assistStructure != null ) {
|
||||
|
||||
@@ -3,5 +3,5 @@ package com.kunzisoft.keepass.activities;
|
||||
import android.content.Intent;
|
||||
|
||||
public interface IntentBuildLauncher {
|
||||
void startActivityForResult(Intent intent);
|
||||
void launchActivity(Intent intent);
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ import com.kunzisoft.keepass.R;
|
||||
import com.kunzisoft.keepass.activities.GroupActivity;
|
||||
import com.kunzisoft.keepass.activities.IntentBuildLauncher;
|
||||
import com.kunzisoft.keepass.activities.ReadOnlyHelper;
|
||||
import com.kunzisoft.keepass.activities.lock.LockingActivity;
|
||||
import com.kunzisoft.keepass.app.App;
|
||||
import com.kunzisoft.keepass.autofill.AutofillHelper;
|
||||
import com.kunzisoft.keepass.compat.ClipDataCompat;
|
||||
@@ -69,7 +70,6 @@ import com.kunzisoft.keepass.fileselect.KeyFileHelper;
|
||||
import com.kunzisoft.keepass.fingerprint.FingerPrintAnimatedVector;
|
||||
import com.kunzisoft.keepass.fingerprint.FingerPrintExplanationDialog;
|
||||
import com.kunzisoft.keepass.fingerprint.FingerPrintHelper;
|
||||
import com.kunzisoft.keepass.activities.lock.LockingActivity;
|
||||
import com.kunzisoft.keepass.selection.EntrySelectionHelper;
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil;
|
||||
import com.kunzisoft.keepass.stylish.StylishActivity;
|
||||
@@ -136,6 +136,37 @@ public class PasswordActivity extends StylishActivity
|
||||
protected boolean entrySelectionMode;
|
||||
private AutofillHelper autofillHelper;
|
||||
|
||||
private static void buildAndLaunchIntent(Activity activity, String fileName, String keyFile,
|
||||
IntentBuildLauncher intentBuildLauncher) {
|
||||
Intent intent = new Intent(activity, PasswordActivity.class);
|
||||
intent.putExtra(UriIntentInitTask.KEY_FILENAME, fileName);
|
||||
intent.putExtra(UriIntentInitTask.KEY_KEYFILE, keyFile);
|
||||
intentBuildLauncher.launchActivity(intent);
|
||||
}
|
||||
|
||||
private static void verifyFileNameUriFromLaunch(String fileName) throws FileNotFoundException {
|
||||
if (EmptyUtils.isNullOrEmpty(fileName)) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
Uri uri = UriUtil.parseDefaultFile(fileName);
|
||||
assert uri != null;
|
||||
String scheme = uri.getScheme();
|
||||
|
||||
if (!EmptyUtils.isNullOrEmpty(scheme) && scheme.equalsIgnoreCase("file")) {
|
||||
File dbFile = new File(uri.getPath());
|
||||
if (!dbFile.exists()) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* -------------------------
|
||||
* Standard Launch
|
||||
* -------------------------
|
||||
*/
|
||||
|
||||
public static void launch(
|
||||
Activity act,
|
||||
String fileName) throws FileNotFoundException {
|
||||
@@ -147,20 +178,14 @@ public class PasswordActivity extends StylishActivity
|
||||
String fileName,
|
||||
String keyFile) throws FileNotFoundException {
|
||||
verifyFileNameUriFromLaunch(fileName);
|
||||
|
||||
buildAndLaunchIntent(activity, fileName, keyFile, (intent) -> {
|
||||
// only to avoid visible flickering when redirecting
|
||||
activity.startActivityForResult(intent, RESULT_CANCELED);
|
||||
});
|
||||
buildAndLaunchIntent(activity, fileName, keyFile, activity::startActivity);
|
||||
}
|
||||
|
||||
private static void buildAndLaunchIntent(Activity activity, String fileName, String keyFile,
|
||||
IntentBuildLauncher intentBuildLauncher) {
|
||||
Intent intent = new Intent(activity, PasswordActivity.class);
|
||||
intent.putExtra(UriIntentInitTask.KEY_FILENAME, fileName);
|
||||
intent.putExtra(UriIntentInitTask.KEY_KEYFILE, keyFile);
|
||||
intentBuildLauncher.startActivityForResult(intent);
|
||||
}
|
||||
/*
|
||||
* -------------------------
|
||||
* Keyboard Launch
|
||||
* -------------------------
|
||||
*/
|
||||
|
||||
public static void launchForKeyboardResult(
|
||||
Activity act,
|
||||
@@ -181,6 +206,12 @@ public class PasswordActivity extends StylishActivity
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* -------------------------
|
||||
* Autofill Launch
|
||||
* -------------------------
|
||||
*/
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public static void launchForAutofillResult(
|
||||
Activity act,
|
||||
@@ -207,57 +238,6 @@ public class PasswordActivity extends StylishActivity
|
||||
}
|
||||
}
|
||||
|
||||
private static void verifyFileNameUriFromLaunch(String fileName) throws FileNotFoundException {
|
||||
if (EmptyUtils.isNullOrEmpty(fileName)) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
Uri uri = UriUtil.parseDefaultFile(fileName);
|
||||
assert uri != null;
|
||||
String scheme = uri.getScheme();
|
||||
|
||||
if (!EmptyUtils.isNullOrEmpty(scheme) && scheme.equalsIgnoreCase("file")) {
|
||||
File dbFile = new File(uri.getPath());
|
||||
if (!dbFile.exists()) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(
|
||||
int requestCode,
|
||||
int resultCode,
|
||||
Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
// To get entry in result
|
||||
EntrySelectionHelper.onActivityResultSetResultAndFinish(this, requestCode, resultCode, data);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
AutofillHelper.onActivityResultSetResultAndFinish(this, requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
boolean keyFileResult = false;
|
||||
if (keyFileHelper != null) {
|
||||
keyFileResult = keyFileHelper.onActivityResultCallback(requestCode, resultCode, data,
|
||||
uri -> {
|
||||
if (uri != null) {
|
||||
populateKeyFileTextView(uri.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!keyFileResult) {
|
||||
// this block if not a key file response
|
||||
switch (resultCode) {
|
||||
case LockingActivity.RESULT_EXIT_LOCK:
|
||||
case Activity.RESULT_CANCELED:
|
||||
setEmptyViews();
|
||||
App.getDB().clear(getApplicationContext());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -1072,6 +1052,40 @@ public class PasswordActivity extends StylishActivity
|
||||
PasswordActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(
|
||||
int requestCode,
|
||||
int resultCode,
|
||||
Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
// To get entry in result
|
||||
EntrySelectionHelper.onActivityResultSetResultAndFinish(this, requestCode, resultCode, data);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
AutofillHelper.onActivityResultSetResultAndFinish(this, requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
boolean keyFileResult = false;
|
||||
if (keyFileHelper != null) {
|
||||
keyFileResult = keyFileHelper.onActivityResultCallback(requestCode, resultCode, data,
|
||||
uri -> {
|
||||
if (uri != null) {
|
||||
populateKeyFileTextView(uri.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!keyFileResult) {
|
||||
// this block if not a key file response
|
||||
switch (resultCode) {
|
||||
case LockingActivity.RESULT_EXIT_LOCK:
|
||||
case Activity.RESULT_CANCELED:
|
||||
setEmptyViews();
|
||||
App.getDB().clear(getApplicationContext());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class UriIntentInitTask extends AsyncTask<Intent, Void, Integer> {
|
||||
|
||||
static final String KEY_FILENAME = "fileName";
|
||||
|
||||
Reference in New Issue
Block a user