ditch ThreadPoolExecutor for AsyncTask
This commit is contained in:
parent
2c8e219aa8
commit
9e079329d5
|
@ -94,17 +94,7 @@ public class KeyserverSyncWorker extends Worker {
|
|||
|
||||
return new Progressable() {
|
||||
@Override
|
||||
public void setProgress(String message, int current, int total) {
|
||||
setProgress(current, total);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProgress(int resourceId, int current, int total) {
|
||||
setProgress(current, total);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProgress(int current, int total) {
|
||||
public void setProgress(Integer ignored, int current, int total) {
|
||||
if (total == 0) {
|
||||
notificationManager.cancel(NotificationIds.KEYSERVER_SYNC);
|
||||
return;
|
||||
|
|
|
@ -222,8 +222,7 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
|
|||
}
|
||||
int numKeys = unifiedKeyInfos.size();
|
||||
|
||||
updateProgress(mContext.getResources().getQuantityString(R.plurals.progress_exporting_key, numKeys),
|
||||
0, numKeys);
|
||||
updateProgress(numKeys == 1 ? R.string.progress_exporting_key : R.string.progress_exporting_key, 0, numKeys);
|
||||
|
||||
// For each public masterKey id
|
||||
for (UnifiedKeyInfo keyInfo : unifiedKeyInfos) {
|
||||
|
|
|
@ -88,12 +88,6 @@ public abstract class BaseOperation<T extends Parcelable> implements PassphraseC
|
|||
}
|
||||
}
|
||||
|
||||
public void updateProgress(String message, int current, int total) {
|
||||
if (mProgressable != null) {
|
||||
mProgressable.setProgress(message, current, total);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateProgress(int current, int total) {
|
||||
if (mProgressable != null) {
|
||||
mProgressable.setProgress(current, total);
|
||||
|
|
|
@ -18,11 +18,9 @@
|
|||
package org.sufficientlysecure.keychain.pgp;
|
||||
|
||||
public interface Progressable {
|
||||
void setProgress(String message, int current, int total);
|
||||
|
||||
void setProgress(int resourceId, int current, int total);
|
||||
|
||||
void setProgress(int current, int total);
|
||||
|
||||
void setProgress(Integer resourceId, int current, int total);
|
||||
default void setProgress(int current, int total) {
|
||||
setProgress(null, current, total);
|
||||
}
|
||||
void setPreventCancel();
|
||||
}
|
||||
|
|
|
@ -1095,17 +1095,7 @@ public class OpenPgpService extends Service {
|
|||
return new Progressable() {
|
||||
boolean errorState = false;
|
||||
@Override
|
||||
public void setProgress(String message, int current, int total) {
|
||||
setProgress(current, total);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProgress(int resourceId, int current, int total) {
|
||||
setProgress(current, total);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProgress(int current, int total) {
|
||||
public void setProgress(Integer ignored, int current, int total) {
|
||||
if (errorState) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,135 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Schürmann & Breitmoser GbR
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.sufficientlysecure.keychain.service;
|
||||
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import org.sufficientlysecure.keychain.daos.KeyWritableRepository;
|
||||
import org.sufficientlysecure.keychain.operations.BackupOperation;
|
||||
import org.sufficientlysecure.keychain.operations.BaseOperation;
|
||||
import org.sufficientlysecure.keychain.operations.BenchmarkOperation;
|
||||
import org.sufficientlysecure.keychain.operations.CertifyOperation;
|
||||
import org.sufficientlysecure.keychain.operations.ChangeUnlockOperation;
|
||||
import org.sufficientlysecure.keychain.operations.DeleteOperation;
|
||||
import org.sufficientlysecure.keychain.operations.EditKeyOperation;
|
||||
import org.sufficientlysecure.keychain.operations.ImportOperation;
|
||||
import org.sufficientlysecure.keychain.operations.InputDataOperation;
|
||||
import org.sufficientlysecure.keychain.operations.KeySyncOperation;
|
||||
import org.sufficientlysecure.keychain.operations.KeySyncParcel;
|
||||
import org.sufficientlysecure.keychain.operations.KeybaseVerificationOperation;
|
||||
import org.sufficientlysecure.keychain.operations.PromoteKeyOperation;
|
||||
import org.sufficientlysecure.keychain.operations.RevokeOperation;
|
||||
import org.sufficientlysecure.keychain.operations.SignEncryptOperation;
|
||||
import org.sufficientlysecure.keychain.operations.UploadOperation;
|
||||
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
||||
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel;
|
||||
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyOperation;
|
||||
import org.sufficientlysecure.keychain.pgp.Progressable;
|
||||
import org.sufficientlysecure.keychain.pgp.SignEncryptParcel;
|
||||
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
|
||||
|
||||
|
||||
public class KeychainService {
|
||||
private static KeychainService keychainService;
|
||||
|
||||
public static KeychainService getInstance(Context context) {
|
||||
if (keychainService == null) {
|
||||
keychainService = new KeychainService(context.getApplicationContext());
|
||||
}
|
||||
return keychainService;
|
||||
}
|
||||
|
||||
private KeychainService(Context context) {
|
||||
this.context = context;
|
||||
this.threadPoolExecutor = new ThreadPoolExecutor(0, 4, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
|
||||
this.keyRepository = KeyWritableRepository.create(context);
|
||||
}
|
||||
|
||||
private final Context context;
|
||||
private final ThreadPoolExecutor threadPoolExecutor;
|
||||
private final KeyWritableRepository keyRepository;
|
||||
|
||||
// this attribute can possibly merged with the one above? not sure...
|
||||
private AtomicBoolean operationCancelledBoolean = new AtomicBoolean(false);
|
||||
|
||||
public void startOperationInBackground(Parcelable inputParcel, CryptoInputParcel cryptoInput,
|
||||
Progressable progressable, OperationCallback operationCallback) {
|
||||
operationCancelledBoolean.set(false);
|
||||
|
||||
Runnable actionRunnable = () -> {
|
||||
BaseOperation op;
|
||||
|
||||
if (inputParcel instanceof SignEncryptParcel) {
|
||||
op = new SignEncryptOperation(context, keyRepository, progressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof PgpDecryptVerifyInputParcel) {
|
||||
op = new PgpDecryptVerifyOperation(context, keyRepository, progressable);
|
||||
} else if (inputParcel instanceof SaveKeyringParcel) {
|
||||
op = new EditKeyOperation(context, keyRepository, progressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof ChangeUnlockParcel) {
|
||||
op = new ChangeUnlockOperation(context, keyRepository, progressable);
|
||||
} else if (inputParcel instanceof RevokeKeyringParcel) {
|
||||
op = new RevokeOperation(context, keyRepository, progressable);
|
||||
} else if (inputParcel instanceof CertifyActionsParcel) {
|
||||
op = new CertifyOperation(context, keyRepository, progressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof DeleteKeyringParcel) {
|
||||
op = new DeleteOperation(context, keyRepository, progressable);
|
||||
} else if (inputParcel instanceof PromoteKeyringParcel) {
|
||||
op = new PromoteKeyOperation(context, keyRepository, progressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof ImportKeyringParcel) {
|
||||
op = new ImportOperation(context, keyRepository, progressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof BackupKeyringParcel) {
|
||||
op = new BackupOperation(context, keyRepository, progressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof UploadKeyringParcel) {
|
||||
op = new UploadOperation(context, keyRepository, progressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof KeybaseVerificationParcel) {
|
||||
op = new KeybaseVerificationOperation(context, keyRepository, progressable);
|
||||
} else if (inputParcel instanceof InputDataParcel) {
|
||||
op = new InputDataOperation(context, keyRepository, progressable);
|
||||
} else if (inputParcel instanceof BenchmarkInputParcel) {
|
||||
op = new BenchmarkOperation(context, keyRepository, progressable);
|
||||
} else if (inputParcel instanceof KeySyncParcel) {
|
||||
op = new KeySyncOperation(context, keyRepository, progressable, operationCancelledBoolean);
|
||||
} else {
|
||||
throw new AssertionError("Unrecognized input parcel in KeychainService!");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // this is unchecked, we make sure it's the correct op above!
|
||||
OperationResult result = op.execute(inputParcel, cryptoInput);
|
||||
operationCallback.operationFinished(result);
|
||||
};
|
||||
|
||||
threadPoolExecutor.execute(actionRunnable);
|
||||
}
|
||||
|
||||
public void cancelRunningTask() {
|
||||
if (operationCancelledBoolean != null) {
|
||||
operationCancelledBoolean.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
public interface OperationCallback {
|
||||
void operationFinished(OperationResult data);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,171 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Schürmann & Breitmoser GbR
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.sufficientlysecure.keychain.service;
|
||||
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import org.sufficientlysecure.keychain.daos.KeyWritableRepository;
|
||||
import org.sufficientlysecure.keychain.operations.BackupOperation;
|
||||
import org.sufficientlysecure.keychain.operations.BaseOperation;
|
||||
import org.sufficientlysecure.keychain.operations.BenchmarkOperation;
|
||||
import org.sufficientlysecure.keychain.operations.CertifyOperation;
|
||||
import org.sufficientlysecure.keychain.operations.ChangeUnlockOperation;
|
||||
import org.sufficientlysecure.keychain.operations.DeleteOperation;
|
||||
import org.sufficientlysecure.keychain.operations.EditKeyOperation;
|
||||
import org.sufficientlysecure.keychain.operations.ImportOperation;
|
||||
import org.sufficientlysecure.keychain.operations.InputDataOperation;
|
||||
import org.sufficientlysecure.keychain.operations.KeySyncOperation;
|
||||
import org.sufficientlysecure.keychain.operations.KeySyncParcel;
|
||||
import org.sufficientlysecure.keychain.operations.KeybaseVerificationOperation;
|
||||
import org.sufficientlysecure.keychain.operations.PromoteKeyOperation;
|
||||
import org.sufficientlysecure.keychain.operations.RevokeOperation;
|
||||
import org.sufficientlysecure.keychain.operations.SignEncryptOperation;
|
||||
import org.sufficientlysecure.keychain.operations.UploadOperation;
|
||||
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
||||
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel;
|
||||
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyOperation;
|
||||
import org.sufficientlysecure.keychain.pgp.Progressable;
|
||||
import org.sufficientlysecure.keychain.pgp.SignEncryptParcel;
|
||||
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
|
||||
|
||||
|
||||
public class KeychainServiceTask {
|
||||
public static KeychainServiceTask create(Context context) {
|
||||
return new KeychainServiceTask(context.getApplicationContext());
|
||||
}
|
||||
|
||||
private KeychainServiceTask(Context context) {
|
||||
this.context = context;
|
||||
this.keyRepository = KeyWritableRepository.create(context);
|
||||
}
|
||||
|
||||
private final Context context;
|
||||
private final KeyWritableRepository keyRepository;
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
public void startOperationInBackground(
|
||||
Parcelable inputParcel, CryptoInputParcel cryptoInput, OperationCallback operationCallback) {
|
||||
new AsyncTask<Void,ProgressUpdate,OperationResult>() {
|
||||
private AtomicBoolean operationCancelledBoolean = new AtomicBoolean(false);
|
||||
|
||||
@Override
|
||||
protected OperationResult doInBackground(Void... voids) {
|
||||
BaseOperation op;
|
||||
|
||||
if (inputParcel instanceof SignEncryptParcel) {
|
||||
op = new SignEncryptOperation(context, keyRepository, asyncProgressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof PgpDecryptVerifyInputParcel) {
|
||||
op = new PgpDecryptVerifyOperation(context, keyRepository, asyncProgressable);
|
||||
} else if (inputParcel instanceof SaveKeyringParcel) {
|
||||
op = new EditKeyOperation(context, keyRepository, asyncProgressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof ChangeUnlockParcel) {
|
||||
op = new ChangeUnlockOperation(context, keyRepository, asyncProgressable);
|
||||
} else if (inputParcel instanceof RevokeKeyringParcel) {
|
||||
op = new RevokeOperation(context, keyRepository, asyncProgressable);
|
||||
} else if (inputParcel instanceof CertifyActionsParcel) {
|
||||
op = new CertifyOperation(context, keyRepository, asyncProgressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof DeleteKeyringParcel) {
|
||||
op = new DeleteOperation(context, keyRepository, asyncProgressable);
|
||||
} else if (inputParcel instanceof PromoteKeyringParcel) {
|
||||
op = new PromoteKeyOperation(context, keyRepository, asyncProgressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof ImportKeyringParcel) {
|
||||
op = new ImportOperation(context, keyRepository, asyncProgressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof BackupKeyringParcel) {
|
||||
op = new BackupOperation(context, keyRepository, asyncProgressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof UploadKeyringParcel) {
|
||||
op = new UploadOperation(context, keyRepository, asyncProgressable, operationCancelledBoolean);
|
||||
} else if (inputParcel instanceof KeybaseVerificationParcel) {
|
||||
op = new KeybaseVerificationOperation(context, keyRepository, asyncProgressable);
|
||||
} else if (inputParcel instanceof InputDataParcel) {
|
||||
op = new InputDataOperation(context, keyRepository, asyncProgressable);
|
||||
} else if (inputParcel instanceof BenchmarkInputParcel) {
|
||||
op = new BenchmarkOperation(context, keyRepository, asyncProgressable);
|
||||
} else if (inputParcel instanceof KeySyncParcel) {
|
||||
op = new KeySyncOperation(context, keyRepository, asyncProgressable, operationCancelledBoolean);
|
||||
} else {
|
||||
throw new AssertionError("Unrecognized input parcel in KeychainService!");
|
||||
}
|
||||
|
||||
if (isCancelled()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// noinspection unchecked, we make sure it's the correct op above
|
||||
return op.execute(inputParcel, cryptoInput);
|
||||
}
|
||||
|
||||
Progressable asyncProgressable = new Progressable() {
|
||||
@Override
|
||||
public void setPreventCancel() {
|
||||
publishProgress((ProgressUpdate) null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProgress(Integer resourceId, int current, int total) {
|
||||
publishProgress(new ProgressUpdate(resourceId, current, total));
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onProgressUpdate(ProgressUpdate... values) {
|
||||
ProgressUpdate progressUpdate = values[0];
|
||||
if (progressUpdate == null) {
|
||||
operationCallback.setPreventCancel();
|
||||
} else {
|
||||
operationCallback.setProgress(progressUpdate.resourceId, progressUpdate.current, progressUpdate.total);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
super.onCancelled();
|
||||
operationCancelledBoolean.set(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(OperationResult result) {
|
||||
operationCallback.operationFinished(result);
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
public interface OperationCallback {
|
||||
void setProgress(Integer message, int current, int total);
|
||||
void setPreventCancel();
|
||||
void operationFinished(OperationResult data);
|
||||
}
|
||||
|
||||
private static class ProgressUpdate {
|
||||
public final Integer resourceId;
|
||||
public final int current;
|
||||
public final int total;
|
||||
|
||||
ProgressUpdate(Integer resourceId, int current, int total) {
|
||||
this.resourceId = resourceId;
|
||||
this.current = current;
|
||||
this.total = total;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Schürmann & Breitmoser GbR
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.sufficientlysecure.keychain.service;
|
||||
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.os.Handler;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
|
||||
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
|
||||
|
||||
|
||||
public class ProgressDialogManager {
|
||||
public static final String TAG_PROGRESS_DIALOG = "progressDialog";
|
||||
|
||||
private FragmentActivity activity;
|
||||
|
||||
public ProgressDialogManager(FragmentActivity activity) {
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
public void showProgressDialog() {
|
||||
showProgressDialog("", ProgressDialog.STYLE_SPINNER, false);
|
||||
}
|
||||
|
||||
public void showProgressDialog(
|
||||
String progressDialogMessage, int progressDialogStyle, boolean cancelable) {
|
||||
|
||||
final ProgressDialogFragment frag = ProgressDialogFragment.newInstance(
|
||||
progressDialogMessage,
|
||||
progressDialogStyle,
|
||||
cancelable);
|
||||
|
||||
// TODO: This is a hack!, see
|
||||
// http://stackoverflow.com/questions/10114324/show-dialogfragment-from-onactivityresult
|
||||
final FragmentManager manager = activity.getSupportFragmentManager();
|
||||
Handler handler = new Handler();
|
||||
handler.post(() -> frag.show(manager, TAG_PROGRESS_DIALOG));
|
||||
|
||||
}
|
||||
|
||||
public void setPreventCancel() {
|
||||
ProgressDialogFragment progressDialogFragment =
|
||||
(ProgressDialogFragment) activity.getSupportFragmentManager()
|
||||
.findFragmentByTag(TAG_PROGRESS_DIALOG);
|
||||
|
||||
if (progressDialogFragment == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
progressDialogFragment.setPreventCancel();
|
||||
}
|
||||
|
||||
public void dismissAllowingStateLoss() {
|
||||
ProgressDialogFragment progressDialogFragment =
|
||||
(ProgressDialogFragment) activity.getSupportFragmentManager()
|
||||
.findFragmentByTag(TAG_PROGRESS_DIALOG);
|
||||
|
||||
if (progressDialogFragment == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
progressDialogFragment.dismissAllowingStateLoss();
|
||||
}
|
||||
|
||||
|
||||
public void onSetProgress(Integer resourceInt, int progress, int max) {
|
||||
|
||||
ProgressDialogFragment progressDialogFragment =
|
||||
(ProgressDialogFragment) activity.getSupportFragmentManager()
|
||||
.findFragmentByTag(TAG_PROGRESS_DIALOG);
|
||||
|
||||
if (progressDialogFragment == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (resourceInt != null) {
|
||||
progressDialogFragment.setProgress(resourceInt, progress, max);
|
||||
} else {
|
||||
progressDialogFragment.setProgress(progress, max);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,195 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Schürmann & Breitmoser GbR
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.sufficientlysecure.keychain.service;
|
||||
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
public class ServiceProgressHandler extends Handler {
|
||||
|
||||
// possible messages sent from this service to handler on ui
|
||||
public enum MessageStatus {
|
||||
UNKNOWN,
|
||||
OKAY,
|
||||
EXCEPTION,
|
||||
UPDATE_PROGRESS,
|
||||
PREVENT_CANCEL;
|
||||
|
||||
private static final MessageStatus[] values = values();
|
||||
|
||||
public static MessageStatus fromInt(int n) {
|
||||
if (n < 0 || n >= values.length) {
|
||||
return UNKNOWN;
|
||||
} else {
|
||||
return values[n];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// possible data keys for messages
|
||||
public static final String DATA_ERROR = "error";
|
||||
public static final String DATA_PROGRESS = "progress";
|
||||
public static final String DATA_PROGRESS_MAX = "max";
|
||||
public static final String DATA_MESSAGE = "message";
|
||||
public static final String DATA_MESSAGE_ID = "message_id";
|
||||
|
||||
// keybase proof specific
|
||||
public static final String KEYBASE_PROOF_URL = "keybase_proof_url";
|
||||
public static final String KEYBASE_PRESENCE_URL = "keybase_presence_url";
|
||||
public static final String KEYBASE_PRESENCE_LABEL = "keybase_presence_label";
|
||||
|
||||
public static final String TAG_PROGRESS_DIALOG = "progressDialog";
|
||||
|
||||
FragmentActivity mActivity;
|
||||
|
||||
public ServiceProgressHandler(FragmentActivity activity) {
|
||||
mActivity = activity;
|
||||
}
|
||||
|
||||
public void showProgressDialog() {
|
||||
showProgressDialog("", ProgressDialog.STYLE_SPINNER, false);
|
||||
}
|
||||
|
||||
public void showProgressDialog(
|
||||
String progressDialogMessage, int progressDialogStyle, boolean cancelable) {
|
||||
|
||||
final ProgressDialogFragment frag = ProgressDialogFragment.newInstance(
|
||||
progressDialogMessage,
|
||||
progressDialogStyle,
|
||||
cancelable);
|
||||
|
||||
// TODO: This is a hack!, see
|
||||
// http://stackoverflow.com/questions/10114324/show-dialogfragment-from-onactivityresult
|
||||
final FragmentManager manager = mActivity.getSupportFragmentManager();
|
||||
Handler handler = new Handler();
|
||||
handler.post(new Runnable() {
|
||||
public void run() {
|
||||
frag.show(manager, TAG_PROGRESS_DIALOG);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message message) {
|
||||
Bundle data = message.getData();
|
||||
|
||||
MessageStatus status = MessageStatus.fromInt(message.arg1);
|
||||
switch (status) {
|
||||
case OKAY:
|
||||
dismissAllowingStateLoss();
|
||||
|
||||
break;
|
||||
|
||||
case EXCEPTION:
|
||||
dismissAllowingStateLoss();
|
||||
|
||||
// show error from service
|
||||
if (data.containsKey(DATA_ERROR)) {
|
||||
Notify.create(mActivity,
|
||||
mActivity.getString(R.string.error_message, data.getString(DATA_ERROR)),
|
||||
Notify.Style.ERROR).show();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case UPDATE_PROGRESS:
|
||||
if (data.containsKey(DATA_PROGRESS) && data.containsKey(DATA_PROGRESS_MAX)) {
|
||||
|
||||
String msg = null;
|
||||
int progress = data.getInt(DATA_PROGRESS);
|
||||
int max = data.getInt(DATA_PROGRESS_MAX);
|
||||
|
||||
// update progress from service
|
||||
if (data.containsKey(DATA_MESSAGE)) {
|
||||
msg = data.getString(DATA_MESSAGE);
|
||||
} else if (data.containsKey(DATA_MESSAGE_ID)) {
|
||||
msg = mActivity.getString(data.getInt(DATA_MESSAGE_ID));
|
||||
}
|
||||
|
||||
onSetProgress(msg, progress, max);
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case PREVENT_CANCEL:
|
||||
setPreventCancel(true);
|
||||
break;
|
||||
|
||||
default:
|
||||
Timber.e("unknown handler message!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void setPreventCancel(boolean preventCancel) {
|
||||
ProgressDialogFragment progressDialogFragment =
|
||||
(ProgressDialogFragment) mActivity.getSupportFragmentManager()
|
||||
.findFragmentByTag("progressDialog");
|
||||
|
||||
if (progressDialogFragment == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
progressDialogFragment.setPreventCancel(preventCancel);
|
||||
}
|
||||
|
||||
protected void dismissAllowingStateLoss() {
|
||||
ProgressDialogFragment progressDialogFragment =
|
||||
(ProgressDialogFragment) mActivity.getSupportFragmentManager()
|
||||
.findFragmentByTag("progressDialog");
|
||||
|
||||
if (progressDialogFragment == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
progressDialogFragment.dismissAllowingStateLoss();
|
||||
}
|
||||
|
||||
|
||||
protected void onSetProgress(String msg, int progress, int max) {
|
||||
|
||||
ProgressDialogFragment progressDialogFragment =
|
||||
(ProgressDialogFragment) mActivity.getSupportFragmentManager()
|
||||
.findFragmentByTag("progressDialog");
|
||||
|
||||
if (progressDialogFragment == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg != null) {
|
||||
progressDialogFragment.setProgress(msg, progress, max);
|
||||
} else {
|
||||
progressDialogFragment.setProgress(progress, max);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -23,24 +23,19 @@ import java.util.Date;
|
|||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.os.Messenger;
|
||||
import android.os.Parcelable;
|
||||
import android.os.RemoteException;
|
||||
import android.os.SystemClock;
|
||||
import android.support.annotation.UiThread;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
|
||||
import org.sufficientlysecure.keychain.operations.results.InputPendingResult;
|
||||
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
||||
import org.sufficientlysecure.keychain.pgp.Progressable;
|
||||
import org.sufficientlysecure.keychain.service.KeychainService;
|
||||
import org.sufficientlysecure.keychain.service.KeychainService.OperationCallback;
|
||||
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
|
||||
import org.sufficientlysecure.keychain.service.ServiceProgressHandler.MessageStatus;
|
||||
import org.sufficientlysecure.keychain.service.KeychainServiceTask;
|
||||
import org.sufficientlysecure.keychain.service.KeychainServiceTask.OperationCallback;
|
||||
import org.sufficientlysecure.keychain.service.ProgressDialogManager;
|
||||
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
|
||||
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
|
||||
import org.sufficientlysecure.keychain.ui.OrbotRequiredDialogActivity;
|
||||
|
@ -281,7 +276,7 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu
|
|||
|
||||
ProgressDialogFragment progressDialogFragment =
|
||||
(ProgressDialogFragment) fragmentManager.findFragmentByTag(
|
||||
ServiceProgressHandler.TAG_PROGRESS_DIALOG);
|
||||
ProgressDialogManager.TAG_PROGRESS_DIALOG);
|
||||
|
||||
if (progressDialogFragment == null) {
|
||||
return;
|
||||
|
@ -300,78 +295,43 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu
|
|||
return;
|
||||
}
|
||||
|
||||
ServiceProgressHandler saveHandler = new ServiceProgressHandler(activity) {
|
||||
ProgressDialogManager progressDialogManager;
|
||||
if (mProgressMessageResource != null) {
|
||||
progressDialogManager = new ProgressDialogManager(activity);
|
||||
progressDialogManager.showProgressDialog(
|
||||
activity.getString(mProgressMessageResource), ProgressDialog.STYLE_HORIZONTAL, mCancellable);
|
||||
} else {
|
||||
progressDialogManager = null;
|
||||
}
|
||||
|
||||
KeychainServiceTask keychainServiceTask = KeychainServiceTask.create(activity);
|
||||
keychainServiceTask.startOperationInBackground(operationInput, cryptoInput, new OperationCallback() {
|
||||
@Override
|
||||
public void handleMessage(Message message) {
|
||||
// handle messages by standard KeychainIntentServiceHandler first
|
||||
super.handleMessage(message);
|
||||
|
||||
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
|
||||
|
||||
// get returned data bundle
|
||||
Bundle returnData = message.getData();
|
||||
if (returnData == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final OperationResult result =
|
||||
returnData.getParcelable(OperationResult.EXTRA_RESULT);
|
||||
|
||||
onHandleResult(result);
|
||||
public void operationFinished(OperationResult result) {
|
||||
if (progressDialogManager != null) {
|
||||
progressDialogManager.dismissAllowingStateLoss();
|
||||
}
|
||||
onHandleResult(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSetProgress(String msg, int progress, int max) {
|
||||
// allow handling of progress in fragment, or delegate upwards
|
||||
if (!mCallback.onCryptoSetProgress(msg, progress, max)) {
|
||||
super.onSetProgress(msg, progress, max);
|
||||
public void setProgress(Integer resourceId, int current, int total) {
|
||||
String msgString = resourceId != null ? activity.getString(resourceId) : null;
|
||||
if (mCallback.onCryptoSetProgress(msgString, current, total)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Messenger messenger = new Messenger(saveHandler);
|
||||
Communicator communicator = new Communicator(messenger);
|
||||
|
||||
Progressable progressable = new Progressable() {
|
||||
@Override
|
||||
public void setProgress(String message, int progress, int max) {
|
||||
Timber.d("Send message by setProgress with progress=" + progress + ", max=" + max);
|
||||
|
||||
Bundle data = new Bundle();
|
||||
if (message != null) {
|
||||
data.putString(ServiceProgressHandler.DATA_MESSAGE, message);
|
||||
if (progressDialogManager != null) {
|
||||
progressDialogManager.onSetProgress(resourceId, current, total);
|
||||
}
|
||||
data.putInt(ServiceProgressHandler.DATA_PROGRESS, progress);
|
||||
data.putInt(ServiceProgressHandler.DATA_PROGRESS_MAX, max);
|
||||
|
||||
communicator.sendMessageToHandler(MessageStatus.UPDATE_PROGRESS, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProgress(int resourceId, int progress, int max) {
|
||||
setProgress(activity.getString(resourceId), progress, max);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProgress(int progress, int max) {
|
||||
setProgress(null, progress, max);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPreventCancel() {
|
||||
communicator.sendMessageToHandler(MessageStatus.PREVENT_CANCEL, (Bundle) null);
|
||||
if (progressDialogManager != null) {
|
||||
progressDialogManager.setPreventCancel();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (mProgressMessageResource != null) {
|
||||
saveHandler.showProgressDialog(
|
||||
activity.getString(mProgressMessageResource),
|
||||
ProgressDialog.STYLE_HORIZONTAL, mCancellable);
|
||||
}
|
||||
|
||||
KeychainService keychainService = KeychainService.getInstance(activity);
|
||||
keychainService.startOperationInBackground(operationInput, cryptoInput, progressable, communicator);
|
||||
});
|
||||
}
|
||||
|
||||
public void cryptoOperation() {
|
||||
|
@ -379,6 +339,7 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu
|
|||
cryptoOperation(CryptoInputParcel.createCryptoInputParcel(new Date()));
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onHandleResult(final OperationResult result) {
|
||||
Timber.d("Handling result in OperationHelper success: " + result.success());
|
||||
|
||||
|
@ -391,20 +352,14 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu
|
|||
}
|
||||
}
|
||||
|
||||
dismissProgress();
|
||||
|
||||
long elapsedTime = SystemClock.elapsedRealtime() - operationStartTime;
|
||||
if (minimumOperationDelay == null || elapsedTime > minimumOperationDelay) {
|
||||
returnResultToCallback(result);
|
||||
return;
|
||||
}
|
||||
|
||||
new Handler().postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
returnResultToCallback(result);
|
||||
}
|
||||
}, minimumOperationDelay - elapsedTime);
|
||||
long artificialDelay = minimumOperationDelay - elapsedTime;
|
||||
new Handler().postDelayed(() -> returnResultToCallback(result), artificialDelay);
|
||||
}
|
||||
|
||||
private void returnResultToCallback(OperationResult result) {
|
||||
|
@ -421,37 +376,4 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu
|
|||
+ result.getClass().getSimpleName() + "), this is a programming error!");
|
||||
}
|
||||
}
|
||||
|
||||
public static class Communicator implements OperationCallback {
|
||||
final Messenger messenger;
|
||||
|
||||
Communicator(Messenger messenger) {
|
||||
this.messenger = messenger;
|
||||
}
|
||||
|
||||
public void sendMessageToHandler(MessageStatus status, Bundle data) {
|
||||
Message msg = Message.obtain();
|
||||
assert msg != null;
|
||||
msg.arg1 = status.ordinal();
|
||||
if (data != null) {
|
||||
msg.setData(data);
|
||||
}
|
||||
|
||||
try {
|
||||
messenger.send(msg);
|
||||
} catch (RemoteException e) {
|
||||
Timber.w(e, "Exception sending message, Is handler present?");
|
||||
} catch (NullPointerException e) {
|
||||
Timber.w(e, "Messenger is null!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operationFinished(OperationResult data) {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putParcelable(OperationResult.EXTRA_RESULT, data);
|
||||
sendMessageToHandler(MessageStatus.OKAY, bundle);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -22,7 +22,6 @@ import android.app.Dialog;
|
|||
import android.app.ProgressDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnKeyListener;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
|
@ -34,7 +33,7 @@ import android.view.View.OnClickListener;
|
|||
import android.widget.Button;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.service.KeychainService;
|
||||
import org.sufficientlysecure.keychain.service.KeychainServiceTask;
|
||||
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
|
||||
|
||||
/**
|
||||
|
@ -141,20 +140,20 @@ public class ProgressDialogFragment extends DialogFragment {
|
|||
return dialog;
|
||||
}
|
||||
|
||||
public void setPreventCancel(boolean preventCancel) {
|
||||
public void setPreventCancel() {
|
||||
// Don't care if we can't cancel anymore either way!
|
||||
if (mIsCancelled || ! mCanCancel) {
|
||||
return;
|
||||
}
|
||||
|
||||
mPreventCancel = preventCancel;
|
||||
mPreventCancel = true;
|
||||
ProgressDialog dialog = (ProgressDialog) getDialog();
|
||||
if (dialog == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Button negative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
|
||||
negative.setEnabled(mIsCancelled && !preventCancel);
|
||||
negative.setEnabled(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -176,8 +175,8 @@ public class ProgressDialogFragment extends DialogFragment {
|
|||
negative.setClickable(false);
|
||||
negative.setTextColor(Color.GRAY);
|
||||
|
||||
KeychainService keychainService = KeychainService.getInstance(requireContext());
|
||||
keychainService.cancelRunningTask();
|
||||
// TODO
|
||||
// KeychainServiceTask.cancelRunningTask();
|
||||
|
||||
// Set the progress bar accordingly
|
||||
ProgressDialog dialog = (ProgressDialog) getDialog();
|
||||
|
|
|
@ -39,27 +39,12 @@ public class ProgressScaler implements Progressable {
|
|||
this.mMax = max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set progress of ProgressDialog by sending message to handler on UI thread
|
||||
*/
|
||||
public void setProgress(String message, int progress, int max) {
|
||||
if (mWrapped != null) {
|
||||
mWrapped.setProgress(message, mFrom + progress * (mTo - mFrom) / max, mMax);
|
||||
}
|
||||
}
|
||||
|
||||
public void setProgress(int resourceId, int progress, int max) {
|
||||
public void setProgress(Integer resourceId, int progress, int max) {
|
||||
if (mWrapped != null) {
|
||||
mWrapped.setProgress(resourceId, mFrom + progress * (mTo - mFrom) / max, mMax);
|
||||
}
|
||||
}
|
||||
|
||||
public void setProgress(int progress, int max) {
|
||||
if (mWrapped != null) {
|
||||
mWrapped.setProgress(mFrom + progress * (mTo - mFrom) / max, mMax);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPreventCancel() {
|
||||
if (mWrapped != null) {
|
||||
|
|
|
@ -477,10 +477,8 @@
|
|||
<string name="progress_modify_pin">"changing PIN…"</string>
|
||||
<string name="progress_modify_admin_pin">"changing Admin PIN…"</string>
|
||||
|
||||
<plurals name="progress_exporting_key">
|
||||
<item quantity="one">"exporting key…"</item>
|
||||
<item quantity="other">"exporting keys…"</item>
|
||||
</plurals>
|
||||
<string name="progress_exporting_key">"exporting key…"</string>
|
||||
<string name="progress_exporting_keys">"exporting keys…"</string>
|
||||
|
||||
<string name="progress_start">"preparing operation…"</string>
|
||||
<string name="progress_extracting_signature_key">"extracting signature key…"</string>
|
||||
|
|
Loading…
Reference in a new issue