Load database in background thread to prevent delay messages in the UI thread.

This commit is contained in:
Brian Pellin
2009-04-02 00:18:01 -05:00
parent e2494d477a
commit 0aa8df75ee
3 changed files with 124 additions and 24 deletions

View File

@@ -152,18 +152,6 @@ public class EntryActivity extends LockingActivity {
// Setup to allow the toast to happen in the foreground // Setup to allow the toast to happen in the foreground
final Handler uiThreadCallback = new Handler(); 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. // Task which clears the clipboard, and sends a toast to the foreground.
private class ClearClipboardTask extends TimerTask { private class ClearClipboardTask extends TimerTask {
@@ -181,7 +169,7 @@ public class EntryActivity extends LockingActivity {
if ( currentClip.equals(mClearText) ) { if ( currentClip.equals(mClearText) ) {
Util.copyToClipboard(mCtx, ""); Util.copyToClipboard(mCtx, "");
uiThreadCallback.post(runInUIThread); uiThreadCallback.post(new UIToastTask(mCtx, R.string.ClearClipboard));
} }
} }
} }

View File

@@ -26,8 +26,10 @@ import java.io.IOException;
import org.bouncycastle1.crypto.InvalidCipherTextException; import org.bouncycastle1.crypto.InvalidCipherTextException;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@@ -47,6 +49,7 @@ public class PasswordActivity extends Activity {
private String mFileName; private String mFileName;
private String mKeyFile; private String mKeyFile;
private ProgressDialog mPd;
public static void Launch(Activity act, String fileName) throws FileNotFoundException { public static void Launch(Activity act, String fileName) throws FileNotFoundException {
Launch(act,fileName,""); Launch(act,fileName,"");
@@ -148,21 +151,17 @@ public class PasswordActivity extends Activity {
return; return;
} }
try {
String fileName = getEditText(R.id.pass_filename); 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); Database.LoadData(fileName, pass, key);
saveFileData(fileName, key); saveFileData(fileName, key);
GroupActivity.Launch(mAct, null); 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); 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()));
}
}
}
} }

View 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();
}
}