From cf026e8eaae30d00d30d8d71564f1da85e249d23 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Tue, 1 May 2018 20:03:43 +0200 Subject: [PATCH] Update progress bar and message progress --- .../kunzisoft/keepass/database/Database.java | 60 ++++---- .../database/action/OnFinishRunnable.java | 3 +- .../database/action/RunnableOnFinish.java | 6 +- .../keepass/database/load/Importer.java | 5 +- .../keepass/database/load/ImporterV3.java | 20 +-- .../database/load/ImporterV3Debug.java | 4 +- .../keepass/database/load/ImporterV4.java | 12 +- .../keepass/password/PasswordActivity.java | 121 ++++++++-------- .../tasks/ProgressTaskDialogFragment.java | 129 ++++++++++++++++++ .../keepass/tasks/ProgressTaskUpdater.java | 24 ++++ .../tasks/UpdateProgressTaskStatus.java | 54 ++++++++ .../kunzisoft/keepass/tasks/UpdateStatus.java | 10 +- app/src/main/res/layout/password.xml | 11 ++ app/src/main/res/layout/progress_dialog.xml | 21 +++ 14 files changed, 365 insertions(+), 115 deletions(-) create mode 100644 app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.java create mode 100644 app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskUpdater.java create mode 100644 app/src/main/java/com/kunzisoft/keepass/tasks/UpdateProgressTaskStatus.java create mode 100644 app/src/main/res/layout/progress_dialog.xml diff --git a/app/src/main/java/com/kunzisoft/keepass/database/Database.java b/app/src/main/java/com/kunzisoft/keepass/database/Database.java index 433a973c4..db27121a0 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/Database.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/Database.java @@ -36,7 +36,7 @@ import com.kunzisoft.keepass.database.load.ImporterFactory; import com.kunzisoft.keepass.database.save.PwDbOutput; import com.kunzisoft.keepass.icons.IconDrawableFactory; import com.kunzisoft.keepass.search.SearchDbHelper; -import com.kunzisoft.keepass.tasks.UpdateStatus; +import com.kunzisoft.keepass.tasks.ProgressTaskUpdater; import com.kunzisoft.keepass.utils.UriUtil; import java.io.BufferedInputStream; @@ -99,19 +99,11 @@ public class Database { loaded = true; } - public void loadData(Context ctx, InputStream is, String password, InputStream keyInputStream) throws IOException, InvalidDBException { - loadData(ctx, is, password, keyInputStream, new UpdateStatus(), !Importer.DEBUG); - } - - public void loadData(Context ctx, Uri uri, String password, Uri keyfile) throws IOException, FileNotFoundException, InvalidDBException { - loadData(ctx, uri, password, keyfile, new UpdateStatus(), !Importer.DEBUG); - } - - public void loadData(Context ctx, Uri uri, String password, Uri keyfile, UpdateStatus status) throws IOException, FileNotFoundException, InvalidDBException { + public void loadData(Context ctx, Uri uri, String password, Uri keyfile, ProgressTaskUpdater status) throws IOException, FileNotFoundException, InvalidDBException { loadData(ctx, uri, password, keyfile, status, !Importer.DEBUG); } - public void loadData(Context ctx, Uri uri, String password, Uri keyfile, UpdateStatus status, boolean debug) throws IOException, FileNotFoundException, InvalidDBException { + public void loadData(Context ctx, Uri uri, String password, Uri keyfile, ProgressTaskUpdater status, boolean debug) throws IOException, FileNotFoundException, InvalidDBException { mUri = uri; readOnly = false; if (uri.getScheme().equals("file")) { @@ -138,7 +130,7 @@ public class Database { } - private void passUrisAsInputStreams(Context ctx, Uri uri, String password, Uri keyfile, UpdateStatus status, boolean debug, long roundsFix) throws IOException, FileNotFoundException, InvalidDBException { + private void passUrisAsInputStreams(Context ctx, Uri uri, String password, Uri keyfile, ProgressTaskUpdater status, boolean debug, long roundsFix) throws IOException, FileNotFoundException, InvalidDBException { InputStream is, kfIs; try { is = UriUtil.getUriInputStream(ctx, uri); @@ -157,14 +149,10 @@ public class Database { } public void loadData(Context ctx, InputStream is, String password, InputStream kfIs, boolean debug) throws IOException, InvalidDBException { - loadData(ctx, is, password, kfIs, new UpdateStatus(), debug); + loadData(ctx, is, password, kfIs, null, debug, 0); } - public void loadData(Context ctx, InputStream is, String password, InputStream kfIs, UpdateStatus status, boolean debug) throws IOException, InvalidDBException { - loadData(ctx, is, password, kfIs, status, debug, 0); - } - - public void loadData(Context ctx, InputStream is, String password, InputStream kfIs, UpdateStatus status, boolean debug, long roundsFix) throws IOException, InvalidDBException { + private void loadData(Context ctx, InputStream is, String password, InputStream kfIs, ProgressTaskUpdater progressTaskUpdater, boolean debug, long roundsFix) throws IOException, InvalidDBException { BufferedInputStream bis = new BufferedInputStream(is); if ( ! bis.markSupported() ) { @@ -178,7 +166,7 @@ public class Database { bis.reset(); // Return to the start - pm = imp.openDatabase(bis, password, kfIs, status, roundsFix); + pm = imp.openDatabase(bis, password, kfIs, progressTaskUpdater, roundsFix); if ( pm != null ) { try { switch (pm.getVersion()) { @@ -222,19 +210,23 @@ public class Database { saveData(ctx, mUri); } - public void saveData(Context ctx, Uri uri) throws IOException, PwDbOutputException { + private void saveData(Context ctx, Uri uri) throws IOException, PwDbOutputException { if (uri.getScheme().equals("file")) { String filename = uri.getPath(); File tempFile = new File(filename + ".tmp"); - FileOutputStream fos = new FileOutputStream(tempFile); - //BufferedOutputStream bos = new BufferedOutputStream(fos); - //PwDbV3Output pmo = new PwDbV3Output(pm, bos, App.getCalendar()); - PwDbOutput pmo = PwDbOutput.getInstance(pm, fos); - pmo.output(); - //bos.flush(); - //bos.close(); - fos.close(); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(tempFile); + PwDbOutput pmo = PwDbOutput.getInstance(pm, fos); + if (pmo != null) + pmo.output(); + } catch (Exception e) { + throw new IOException("Failed to store database."); + } finally { + if (fos != null) + fos.close(); + } // Force data to disk before continuing try { @@ -250,16 +242,18 @@ public class Database { } } else { - OutputStream os; + OutputStream os = null; try { os = ctx.getContentResolver().openOutputStream(uri); + PwDbOutput pmo = PwDbOutput.getInstance(pm, os); + if (pmo != null) + pmo.output(); } catch (Exception e) { throw new IOException("Failed to store database."); + } finally { + if (os != null) + os.close(); } - - PwDbOutput pmo = PwDbOutput.getInstance(pm, os); - pmo.output(); - os.close(); } mUri = uri; } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/OnFinishRunnable.java b/app/src/main/java/com/kunzisoft/keepass/database/action/OnFinishRunnable.java index 82d34d87c..6db343ecf 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/OnFinishRunnable.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/OnFinishRunnable.java @@ -75,7 +75,8 @@ public class OnFinishRunnable implements Runnable { } } } - + + // TODO Move protected void displayMessage(Context ctx) { if ( mMessage != null && mMessage.length() > 0 ) { Toast.makeText(ctx, mMessage, Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/RunnableOnFinish.java b/app/src/main/java/com/kunzisoft/keepass/database/action/RunnableOnFinish.java index e509122bc..7fa909f3f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/RunnableOnFinish.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/RunnableOnFinish.java @@ -19,13 +19,13 @@ */ package com.kunzisoft.keepass.database.action; -import com.kunzisoft.keepass.tasks.UpdateStatus; +import com.kunzisoft.keepass.tasks.ProgressTaskUpdater; public abstract class RunnableOnFinish implements Runnable { public OnFinishRunnable mFinish; - public UpdateStatus mStatus; + public ProgressTaskUpdater mStatus; public RunnableOnFinish(OnFinishRunnable finish) { mFinish = finish; @@ -45,7 +45,7 @@ public abstract class RunnableOnFinish implements Runnable { } } - public void setStatus(UpdateStatus status) { + public void setStatus(ProgressTaskUpdater status) { mStatus = status; } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/load/Importer.java b/app/src/main/java/com/kunzisoft/keepass/database/load/Importer.java index 2fe04613c..2c56f2122 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/load/Importer.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/load/Importer.java @@ -21,7 +21,7 @@ package com.kunzisoft.keepass.database.load; import com.kunzisoft.keepass.database.PwDatabase; import com.kunzisoft.keepass.database.exception.InvalidDBException; -import com.kunzisoft.keepass.tasks.UpdateStatus; +import com.kunzisoft.keepass.tasks.ProgressTaskUpdater; import java.io.IOException; import java.io.InputStream; @@ -33,8 +33,7 @@ public abstract class Importer { public abstract PwDatabase openDatabase(InputStream inStream, String password, InputStream keyInputStream) throws IOException, InvalidDBException; - public abstract PwDatabase openDatabase(InputStream inStream, String password, InputStream keyInputStream, UpdateStatus status, long roundsFix) + public abstract PwDatabase openDatabase(InputStream inStream, String password, InputStream keyInputStream, ProgressTaskUpdater updater, long roundsFix) throws IOException, InvalidDBException; - } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3.java b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3.java index ee15c2df2..f56f11b49 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3.java @@ -65,7 +65,7 @@ import com.kunzisoft.keepass.database.exception.InvalidPasswordException; import com.kunzisoft.keepass.stream.LEDataInputStream; import com.kunzisoft.keepass.stream.LEDataOutputStream; import com.kunzisoft.keepass.stream.NullOutputStream; -import com.kunzisoft.keepass.tasks.UpdateStatus; +import com.kunzisoft.keepass.tasks.ProgressTaskUpdater; import com.kunzisoft.keepass.utils.Types; import java.io.IOException; @@ -124,15 +124,15 @@ public class ImporterV3 extends Importer { * @throws InvalidAlgorithmParameterException if error decrypting main file body. * @throws ShortBufferException if error decrypting main file body. */ + @Override public PwDatabaseV3 openDatabase( InputStream inStream, String password, InputStream kfIs) - throws IOException, InvalidDBException - { - return openDatabase(inStream, password, kfIs, new UpdateStatus(), 0); + throws IOException, InvalidDBException { + return openDatabase(inStream, password, kfIs, null, 0); } - public PwDatabaseV3 openDatabase( InputStream inStream, String password, InputStream kfIs, UpdateStatus status, long roundsFix) - throws IOException, InvalidDBException - { + @Override + public PwDatabaseV3 openDatabase(InputStream inStream, String password, InputStream kfIs, ProgressTaskUpdater progressTaskUpdater, long roundsFix) + throws IOException, InvalidDBException { PwDatabaseV3 newManager; @@ -156,7 +156,8 @@ public class ImporterV3 extends Importer { throw new InvalidDBVersionException(); } - status.updateMessage(R.string.creating_db_key); + if (progressTaskUpdater != null) + progressTaskUpdater.updateMessage(R.string.creating_db_key); newManager = createDB(); newManager.setMasterKey(password, kfIs); @@ -177,7 +178,8 @@ public class ImporterV3 extends Importer { // Generate transformedMasterKey from masterKey newManager.makeFinalKey(hdr.masterSeed, hdr.transformSeed, newManager.getNumberKeyEncryptionRounds()); - status.updateMessage(R.string.decrypting_db); + if (progressTaskUpdater != null) + progressTaskUpdater.updateMessage(R.string.decrypting_db); // Initialize Rijndael algorithm Cipher cipher; try { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3Debug.java b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3Debug.java index df7a4609b..2ad980ed3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3Debug.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3Debug.java @@ -21,7 +21,7 @@ package com.kunzisoft.keepass.database.load; import com.kunzisoft.keepass.database.PwDatabaseV3Debug; import com.kunzisoft.keepass.database.exception.InvalidDBException; -import com.kunzisoft.keepass.tasks.UpdateStatus; +import com.kunzisoft.keepass.tasks.ProgressTaskUpdater; import java.io.IOException; import java.io.InputStream; @@ -35,7 +35,7 @@ public class ImporterV3Debug extends ImporterV3 { @Override public PwDatabaseV3Debug openDatabase(InputStream inStream, String password, - InputStream keyInputStream, UpdateStatus status, long roundsFix) throws IOException, + InputStream keyInputStream, ProgressTaskUpdater status, long roundsFix) throws IOException, InvalidDBException { return (PwDatabaseV3Debug) super.openDatabase(inStream, password, keyInputStream, status, roundsFix); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV4.java b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV4.java index 5d98c2a46..0d35e1be6 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV4.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV4.java @@ -19,6 +19,7 @@ */ package com.kunzisoft.keepass.database.load; +import com.kunzisoft.keepass.R; import com.kunzisoft.keepass.crypto.CipherFactory; import com.kunzisoft.keepass.crypto.PwStreamCipherFactory; import com.kunzisoft.keepass.crypto.engine.CipherEngine; @@ -42,7 +43,7 @@ import com.kunzisoft.keepass.stream.BetterCipherInputStream; import com.kunzisoft.keepass.stream.HashedBlockInputStream; import com.kunzisoft.keepass.stream.HmacBlockInputStream; import com.kunzisoft.keepass.stream.LEDataInputStream; -import com.kunzisoft.keepass.tasks.UpdateStatus; +import com.kunzisoft.keepass.tasks.ProgressTaskUpdater; import com.kunzisoft.keepass.utils.DateUtil; import com.kunzisoft.keepass.utils.EmptyUtils; import com.kunzisoft.keepass.utils.MemUtil; @@ -97,13 +98,16 @@ public class ImporterV4 extends Importer { public PwDatabaseV4 openDatabase(InputStream inStream, String password, InputStream keyInputStream) throws IOException, InvalidDBException { - return openDatabase(inStream, password, keyInputStream, new UpdateStatus(), 0); + return openDatabase(inStream, password, keyInputStream, null, 0); } @Override public PwDatabaseV4 openDatabase(InputStream inStream, String password, - InputStream keyInputStream, UpdateStatus status, long roundsFix) throws IOException, + InputStream keyInputStream, ProgressTaskUpdater progressTaskUpdater, long roundsFix) throws IOException, InvalidDBException { + + if (progressTaskUpdater != null) + progressTaskUpdater.updateMessage(R.string.creating_db_key); db = createDB(); PwDbHeaderV4 header = new PwDbHeaderV4(db); @@ -118,6 +122,8 @@ public class ImporterV4 extends Importer { db.setMasterKey(password, keyInputStream); db.makeFinalKey(header.masterSeed, db.getKdfParameters(), roundsFix); + if (progressTaskUpdater != null) + progressTaskUpdater.updateMessage(R.string.decrypting_db); CipherEngine engine; Cipher cipher; try { diff --git a/app/src/main/java/com/kunzisoft/keepass/password/PasswordActivity.java b/app/src/main/java/com/kunzisoft/keepass/password/PasswordActivity.java index e916bfff6..cc2554fa4 100644 --- a/app/src/main/java/com/kunzisoft/keepass/password/PasswordActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/password/PasswordActivity.java @@ -71,7 +71,8 @@ import com.kunzisoft.keepass.fingerprint.FingerPrintDialog; import com.kunzisoft.keepass.fingerprint.FingerPrintHelper; import com.kunzisoft.keepass.settings.PreferencesUtil; import com.kunzisoft.keepass.stylish.StylishActivity; -import com.kunzisoft.keepass.tasks.ProgressTask; +import com.kunzisoft.keepass.tasks.ProgressTaskDialogFragment; +import com.kunzisoft.keepass.tasks.UpdateProgressTaskStatus; import com.kunzisoft.keepass.utils.EmptyUtils; import com.kunzisoft.keepass.utils.MenuUtil; import com.kunzisoft.keepass.utils.UriUtil; @@ -122,6 +123,8 @@ public class PasswordActivity extends StylishActivity private CompoundButton checkboxKeyfileView; private CompoundButton checkboxDefaultDatabaseView; + private ProgressTaskDialogFragment loadingDatabaseDialog; + private DefaultCheckChange defaultCheckChange; private ValidateButtonViewClickListener validateButtonViewClickListener; @@ -812,16 +815,74 @@ public class PasswordActivity extends StylishActivity // Clear before we load Database db = App.getDB(); db.clear(); - // Clear the shutdown flag App.clearShutdown(); + // Show the progress dialog Handler handler = new Handler(); - AfterLoad afterLoad = new AfterLoad(handler, db); + AfterLoadingDatabase afterLoad = new AfterLoadingDatabase(handler, db); + LoadDBRunnable databaseLoadingTask = new LoadDBRunnable(db, PasswordActivity.this, mDbUri, pass, keyfile, afterLoad); + databaseLoadingTask.setStatus( + new UpdateProgressTaskStatus(this, + handler, + ProgressTaskDialogFragment.start(getSupportFragmentManager(), R.string.loading_database) + )); + Thread t = new Thread(databaseLoadingTask); + t.start(); + } - LoadDBRunnable task = new LoadDBRunnable(db, PasswordActivity.this, mDbUri, pass, keyfile, afterLoad); - ProgressTask pt = new ProgressTask(PasswordActivity.this, task, R.string.loading_database); - pt.run(); + /** + * Called after verify and try to opening the database + */ + private final class AfterLoadingDatabase extends OnFinishRunnable { + + protected Database db; + + AfterLoadingDatabase( + Handler handler, + Database db) { + super(handler); + + this.db = db; + } + + @Override + public void run() { + runOnUiThread(() -> { + // Recheck fingerprint if error + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + // Stay with the same mode + reInitWithFingerprintMode(); + } + + if (db.isPasswordEncodingError()) { + PasswordEncodingDialogHelper dialog = new PasswordEncodingDialogHelper(); + dialog.show(PasswordActivity.this, (dialog1, which) -> launchGroupActivity()); + } else if (mSuccess) { + launchGroupActivity(); + } else { + if ( mMessage != null && mMessage.length() > 0 ) { + Toast.makeText(PasswordActivity.this, mMessage, Toast.LENGTH_LONG).show(); + } + } + + // To remove progress task + ProgressTaskDialogFragment.stop(getSupportFragmentManager()); + }); + } + } + + private void launchGroupActivity() { + AssistStructure assistStructure = null; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + assistStructure = autofillHelper.getAssistStructure(); + if (assistStructure != null) { + GroupActivity.launch(PasswordActivity.this, assistStructure); + } + } + if (assistStructure == null) { + GroupActivity.launch(PasswordActivity.this); + } } @Override @@ -863,54 +924,6 @@ public class PasswordActivity extends StylishActivity PasswordActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults); } - /** - * Called after verify and try to opening the database - */ - private final class AfterLoad extends OnFinishRunnable { - - protected Database db; - - AfterLoad( - Handler handler, - Database db) { - super(handler); - - this.db = db; - } - - @Override - public void run() { - - // Recheck fingerprint if error - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - // Stay with the same mode - reInitWithFingerprintMode(); - } - - if (db.isPasswordEncodingError()) { - PasswordEncodingDialogHelper dialog = new PasswordEncodingDialogHelper(); - dialog.show(PasswordActivity.this, (dialog1, which) -> launchGroupActivity()); - } else if (mSuccess) { - launchGroupActivity(); - } else { - displayMessage(PasswordActivity.this); - } - } - } - - private void launchGroupActivity() { - AssistStructure assistStructure = null; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - assistStructure = autofillHelper.getAssistStructure(); - if (assistStructure != null) { - GroupActivity.launch(PasswordActivity.this, assistStructure); - } - } - if (assistStructure == null) { - GroupActivity.launch(PasswordActivity.this); - } - } - private static class UriIntentInitTask extends AsyncTask { static final String KEY_FILENAME = "fileName"; diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.java b/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.java new file mode 100644 index 000000000..75098d38d --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.java @@ -0,0 +1,129 @@ +/* + * Copyright 2018 Jeremy Jamet / Kunzisoft. + * + * This file is part of KeePass DX. + * + * KeePass DX is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * KeePass DX is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeePass DX. If not, see . + * + */ +package com.kunzisoft.keepass.tasks; + +import android.annotation.SuppressLint; +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.pm.ActivityInfo; +import android.content.res.Configuration; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.StringRes; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v7.app.AlertDialog; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ProgressBar; +import android.widget.TextView; + +import com.kunzisoft.keepass.R; + +public class ProgressTaskDialogFragment extends DialogFragment implements ProgressTaskUpdater{ + + public static final String PROGRESS_TASK_DIALOG_TAG = "progressDialogFragment"; + + private static final int UNDEFINED = -1; + + private @StringRes int title = UNDEFINED; + private @StringRes int message = UNDEFINED; + + private TextView messageView; + private ProgressBar progressView; + + public static ProgressTaskDialogFragment start(FragmentManager fragmentManager, @StringRes int titleId) { + // Create an instance of the dialog fragment and show it + ProgressTaskDialogFragment dialog = new ProgressTaskDialogFragment(); + dialog.setTitle(titleId); + dialog.show(fragmentManager, PROGRESS_TASK_DIALOG_TAG); + return dialog; + } + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + assert getActivity() != null; + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + // Get the layout inflater + LayoutInflater inflater = getActivity().getLayoutInflater(); + + // Inflate and set the layout for the dialog + // Pass null as the parent view because its going in the dialog layout + @SuppressLint("InflateParams") + View root = inflater.inflate(R.layout.progress_dialog, null); + builder.setView(root); + if (title != UNDEFINED) + builder.setTitle(title); + + messageView = root.findViewById(R.id.progress_dialog_message); + progressView = root.findViewById(R.id.progress_dialog_bar); + + if (message != UNDEFINED) + messageView.setText(message); + + setCancelable(false); + lockScreenOrientation(); + + return builder.create(); + } + + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + unlockScreenOrientation(); + } + + public static void stop(FragmentManager fragmentManager) { + Fragment fragmentTask = fragmentManager.findFragmentByTag(PROGRESS_TASK_DIALOG_TAG); + if (fragmentTask != null) { + ProgressTaskDialogFragment loadingDatabaseDialog = (ProgressTaskDialogFragment) fragmentTask; + loadingDatabaseDialog.dismissAllowingStateLoss(); + } + } + + private void lockScreenOrientation() { + if (getActivity() != null) { + int currentOrientation = getResources().getConfiguration().orientation; + if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) { + getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } else { + getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + } + } + } + + private void unlockScreenOrientation() { + if (getActivity() != null) + getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); + } + + public void setTitle(@StringRes int titleId) { + this.title = titleId; + } + + @Override + public void updateMessage(int resId) { + this.message = resId; + if (messageView != null) + this.messageView.setText(message); + } +} diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskUpdater.java b/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskUpdater.java new file mode 100644 index 000000000..23188c78b --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskUpdater.java @@ -0,0 +1,24 @@ +/* + * Copyright 2018 Jeremy Jamet / Kunzisoft. + * + * This file is part of KeePass DX. + * + * KeePass DX is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * KeePass DX is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeePass DX. If not, see . + * + */ +package com.kunzisoft.keepass.tasks; + +public interface ProgressTaskUpdater { + void updateMessage(int resId); +} diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/UpdateProgressTaskStatus.java b/app/src/main/java/com/kunzisoft/keepass/tasks/UpdateProgressTaskStatus.java new file mode 100644 index 000000000..64430cb6c --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/tasks/UpdateProgressTaskStatus.java @@ -0,0 +1,54 @@ +/* + * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. + * + * This file is part of KeePass DX. + * + * KeePass DX is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * KeePass DX is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeePass DX. If not, see . + * + */ +package com.kunzisoft.keepass.tasks; + +import android.content.Context; +import android.os.Handler; + +public class UpdateProgressTaskStatus implements ProgressTaskUpdater { + private Context mContext; + private ProgressTaskUpdater mProgressTaskUpdater; + private Handler mHandler; + + public UpdateProgressTaskStatus(Context context, Handler handler, ProgressTaskUpdater progressTaskUpdater) { + this.mContext = context; + this.mProgressTaskUpdater = progressTaskUpdater; + this.mHandler = handler; + } + + @Override + public void updateMessage(int resId) { + if ( mContext != null && mProgressTaskUpdater != null && mHandler != null ) { + mHandler.post(new UpdateMessage(resId)); + } + } + + private class UpdateMessage implements Runnable { + private int mResId; + + UpdateMessage(int resId) { + mResId = resId; + } + + public void run() { + mProgressTaskUpdater.updateMessage(mResId); + } + } +} diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/UpdateStatus.java b/app/src/main/java/com/kunzisoft/keepass/tasks/UpdateStatus.java index 43488a9ed..4461d495f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/tasks/UpdateStatus.java +++ b/app/src/main/java/com/kunzisoft/keepass/tasks/UpdateStatus.java @@ -23,16 +23,12 @@ import android.app.ProgressDialog; import android.content.Context; import android.os.Handler; -public class UpdateStatus { +public class UpdateStatus implements ProgressTaskUpdater { private ProgressDialog mPD; private Context mCtx; private Handler mHandler; - public UpdateStatus() { - - } - - public UpdateStatus(Context ctx, Handler handler, ProgressDialog pd) { + UpdateStatus(Context ctx, Handler handler, ProgressDialog pd) { mCtx = ctx; mPD = pd; mHandler = handler; @@ -47,7 +43,7 @@ public class UpdateStatus { private class UpdateMessage implements Runnable { private int mResId; - public UpdateMessage(int resId) { + UpdateMessage(int resId) { mResId = resId; } diff --git a/app/src/main/res/layout/password.xml b/app/src/main/res/layout/password.xml index e00840e8a..641525cab 100644 --- a/app/src/main/res/layout/password.xml +++ b/app/src/main/res/layout/password.xml @@ -190,6 +190,17 @@ + + + + + + + + + \ No newline at end of file