mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Load database in background thread to prevent delay messages in the UI thread.
This commit is contained in:
@@ -152,18 +152,6 @@ public class EntryActivity extends LockingActivity {
|
||||
// Setup to allow the toast to happen in the foreground
|
||||
final Handler uiThreadCallback = new Handler();
|
||||
|
||||
// This task will be run in the UI thread
|
||||
final Runnable runInUIThread = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
uiClearClipToast();
|
||||
}
|
||||
};
|
||||
|
||||
private void uiClearClipToast() {
|
||||
Toast.makeText(this, R.string.ClearClipboard, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
// Task which clears the clipboard, and sends a toast to the foreground.
|
||||
private class ClearClipboardTask extends TimerTask {
|
||||
|
||||
@@ -181,7 +169,7 @@ public class EntryActivity extends LockingActivity {
|
||||
|
||||
if ( currentClip.equals(mClearText) ) {
|
||||
Util.copyToClipboard(mCtx, "");
|
||||
uiThreadCallback.post(runInUIThread);
|
||||
uiThreadCallback.post(new UIToastTask(mCtx, R.string.ClearClipboard));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,10 @@ import java.io.IOException;
|
||||
import org.bouncycastle1.crypto.InvalidCipherTextException;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
@@ -47,6 +49,7 @@ public class PasswordActivity extends Activity {
|
||||
|
||||
private String mFileName;
|
||||
private String mKeyFile;
|
||||
private ProgressDialog mPd;
|
||||
|
||||
public static void Launch(Activity act, String fileName) throws FileNotFoundException {
|
||||
Launch(act,fileName,"");
|
||||
@@ -148,21 +151,17 @@ public class PasswordActivity extends Activity {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String fileName = getEditText(R.id.pass_filename);
|
||||
|
||||
mPd = ProgressDialog.show(mAct, "Working...", "Loading database", true, false);
|
||||
Thread bkgLoad = new Thread(new BackgroundLoad(fileName, pass, key));
|
||||
bkgLoad.start();
|
||||
|
||||
/*
|
||||
Database.LoadData(fileName, pass, key);
|
||||
saveFileData(fileName, key);
|
||||
GroupActivity.Launch(mAct, null);
|
||||
|
||||
} catch (InvalidCipherTextException e) {
|
||||
errorMessage(R.string.InvalidPassword);
|
||||
} catch (FileNotFoundException e) {
|
||||
errorMessage(R.string.FileNotFound);
|
||||
} catch (IOException e) {
|
||||
errorMessage("Unknown error.");
|
||||
} catch (InvalidKeyFileException e) {
|
||||
errorMessage(e.getMessage());
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,4 +208,70 @@ public class PasswordActivity extends Activity {
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private final Handler uiHandler = new Handler();
|
||||
|
||||
private final class AfterLoad implements Runnable {
|
||||
private boolean mLaunch;
|
||||
private CharSequence mMsg;
|
||||
|
||||
public AfterLoad() {
|
||||
mLaunch = true;
|
||||
mMsg = "";
|
||||
}
|
||||
|
||||
public AfterLoad(CharSequence errorMsg) {
|
||||
mLaunch = false;
|
||||
mMsg = errorMsg;
|
||||
}
|
||||
|
||||
public AfterLoad(int resId) {
|
||||
mLaunch = false;
|
||||
mMsg = PasswordActivity.this.getText(resId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
mPd.dismiss();
|
||||
|
||||
if ( mMsg.length() > 0 ) {
|
||||
Toast.makeText(PasswordActivity.this, mMsg, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
if ( mLaunch ) {
|
||||
GroupActivity.Launch(PasswordActivity.this, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final class BackgroundLoad implements Runnable {
|
||||
private String mFileName;
|
||||
private String mPass;
|
||||
private String mKey;
|
||||
|
||||
public BackgroundLoad(String fileName, String pass, String key) {
|
||||
mFileName = fileName;
|
||||
mPass = pass;
|
||||
mKey = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Database.LoadData(mFileName, mPass, mKey);
|
||||
saveFileData(mFileName, mKey);
|
||||
uiHandler.post(new AfterLoad());
|
||||
|
||||
} catch (InvalidCipherTextException e) {
|
||||
uiHandler.post(new AfterLoad(R.string.InvalidPassword));
|
||||
} catch (FileNotFoundException e) {
|
||||
uiHandler.post(new AfterLoad(R.string.FileNotFound));
|
||||
} catch (IOException e) {
|
||||
uiHandler.post(new AfterLoad("Unknown error."));
|
||||
} catch (InvalidKeyFileException e) {
|
||||
uiHandler.post(new AfterLoad(e.getMessage()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
47
src/com/android/keepass/UIToastTask.java
Normal file
47
src/com/android/keepass/UIToastTask.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2009 Brian Pellin.
|
||||
*
|
||||
* This file is part of KeePassDroid.
|
||||
*
|
||||
* KeePassDroid 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 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* KeePassDroid 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 KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.android.keepass;
|
||||
|
||||
import android.content.Context;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class UIToastTask implements Runnable {
|
||||
|
||||
private String mText;
|
||||
private Context mCtx;
|
||||
|
||||
public UIToastTask(Context ctx, int resId) {
|
||||
mCtx = ctx;
|
||||
mText = ctx.getString(resId);
|
||||
}
|
||||
|
||||
public UIToastTask(Context ctx, String text) {
|
||||
mCtx = ctx;
|
||||
mText = text;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Toast.makeText(mCtx, mText, Toast.LENGTH_LONG).show();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user