mirror of
https://github.com/moparisthebest/open-keychain
synced 2025-02-21 05:11:45 -05:00
introduced OperationHelper
This commit is contained in:
parent
83fc9d796f
commit
9f61359c43
@ -116,7 +116,7 @@ public class AccountSettingsFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute activity's onActivityResult to show log notify
|
// execute activity's handleActivityResult to show log notify
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,8 +103,7 @@ public class KeychainNewService extends Service implements Progressable {
|
|||||||
} else if (inputParcel instanceof ImportKeyringParcel) {
|
} else if (inputParcel instanceof ImportKeyringParcel) {
|
||||||
op = new ImportExportOperation(outerThis, new ProviderHelper(outerThis), outerThis,
|
op = new ImportExportOperation(outerThis, new ProviderHelper(outerThis), outerThis,
|
||||||
mActionCanceled);
|
mActionCanceled);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
package org.sufficientlysecure.keychain.ui;
|
package org.sufficientlysecure.keychain.ui;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.ProgressDialog;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -26,7 +25,6 @@ import android.os.Bundle;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.os.Messenger;
|
import android.os.Messenger;
|
||||||
import android.os.Parcelable;
|
|
||||||
import android.support.v4.app.LoaderManager;
|
import android.support.v4.app.LoaderManager;
|
||||||
import android.support.v4.content.CursorLoader;
|
import android.support.v4.content.CursorLoader;
|
||||||
import android.support.v4.content.Loader;
|
import android.support.v4.content.Loader;
|
||||||
@ -40,9 +38,6 @@ import android.widget.ListView;
|
|||||||
import org.sufficientlysecure.keychain.Constants;
|
import org.sufficientlysecure.keychain.Constants;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround;
|
import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround;
|
||||||
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
|
|
||||||
import org.sufficientlysecure.keychain.operations.results.EditKeyResult;
|
|
||||||
import org.sufficientlysecure.keychain.operations.results.InputPendingResult;
|
|
||||||
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
||||||
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
|
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
|
||||||
import org.sufficientlysecure.keychain.operations.results.SingletonResult;
|
import org.sufficientlysecure.keychain.operations.results.SingletonResult;
|
||||||
@ -54,8 +49,6 @@ import org.sufficientlysecure.keychain.provider.KeychainContract;
|
|||||||
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
|
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
|
||||||
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
||||||
import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException;
|
import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException;
|
||||||
import org.sufficientlysecure.keychain.service.KeychainService;
|
|
||||||
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
|
|
||||||
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
|
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
|
||||||
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.ChangeUnlockParcel;
|
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.ChangeUnlockParcel;
|
||||||
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyChange;
|
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyChange;
|
||||||
@ -64,13 +57,19 @@ import org.sufficientlysecure.keychain.ui.adapter.SubkeysAdapter;
|
|||||||
import org.sufficientlysecure.keychain.ui.adapter.SubkeysAddedAdapter;
|
import org.sufficientlysecure.keychain.ui.adapter.SubkeysAddedAdapter;
|
||||||
import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter;
|
import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter;
|
||||||
import org.sufficientlysecure.keychain.ui.adapter.UserIdsAddedAdapter;
|
import org.sufficientlysecure.keychain.ui.adapter.UserIdsAddedAdapter;
|
||||||
import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment;
|
import org.sufficientlysecure.keychain.ui.base.NewCryptoOperationFragment;
|
||||||
import org.sufficientlysecure.keychain.ui.dialog.*;
|
import org.sufficientlysecure.keychain.ui.dialog.AddSubkeyDialogFragment;
|
||||||
|
import org.sufficientlysecure.keychain.ui.dialog.AddUserIdDialogFragment;
|
||||||
|
import org.sufficientlysecure.keychain.ui.dialog.EditSubkeyDialogFragment;
|
||||||
|
import org.sufficientlysecure.keychain.ui.dialog.EditSubkeyExpiryDialogFragment;
|
||||||
|
import org.sufficientlysecure.keychain.ui.dialog.EditUserIdDialogFragment;
|
||||||
|
import org.sufficientlysecure.keychain.ui.dialog.SetPassphraseDialogFragment;
|
||||||
import org.sufficientlysecure.keychain.ui.util.Notify;
|
import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
|
import org.sufficientlysecure.keychain.util.OperationHelper;
|
||||||
import org.sufficientlysecure.keychain.util.Passphrase;
|
import org.sufficientlysecure.keychain.util.Passphrase;
|
||||||
|
|
||||||
public class EditKeyFragment extends CryptoOperationFragment<SaveKeyringParcel, OperationResult>
|
public class EditKeyFragment extends NewCryptoOperationFragment<SaveKeyringParcel, OperationResult>
|
||||||
implements LoaderManager.LoaderCallbacks<Cursor> {
|
implements LoaderManager.LoaderCallbacks<Cursor> {
|
||||||
|
|
||||||
public static final String ARG_DATA_URI = "uri";
|
public static final String ARG_DATA_URI = "uri";
|
||||||
@ -144,6 +143,27 @@ public class EditKeyFragment extends CryptoOperationFragment<SaveKeyringParcel,
|
|||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
|
||||||
|
setOperationHelper(
|
||||||
|
new OperationHelper<SaveKeyringParcel, OperationResult>(getActivity()) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SaveKeyringParcel createOperationInput() {
|
||||||
|
return mSaveKeyringParcel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCryptoOperationSuccess(OperationResult result) {
|
||||||
|
|
||||||
|
// if good -> finish, return result to showkey and display there!
|
||||||
|
Intent intent = new Intent();
|
||||||
|
intent.putExtra(OperationResult.EXTRA_RESULT, result);
|
||||||
|
getActivity().setResult(EditKeyActivity.RESULT_OK, intent);
|
||||||
|
getActivity().finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
((EditKeyActivity) getActivity()).setFullScreenDialogDoneClose(
|
((EditKeyActivity) getActivity()).setFullScreenDialogDoneClose(
|
||||||
R.string.btn_save,
|
R.string.btn_save,
|
||||||
new OnClickListener() {
|
new OnClickListener() {
|
||||||
@ -607,21 +627,4 @@ public class EditKeyFragment extends CryptoOperationFragment<SaveKeyringParcel,
|
|||||||
getActivity().setResult(EditKeyActivity.RESULT_OK, intent);
|
getActivity().setResult(EditKeyActivity.RESULT_OK, intent);
|
||||||
getActivity().finish();
|
getActivity().finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected SaveKeyringParcel createOperationInput() {
|
|
||||||
return mSaveKeyringParcel;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCryptoOperationSuccess(OperationResult result) {
|
|
||||||
|
|
||||||
// if good -> finish, return result to showkey and display there!
|
|
||||||
Intent intent = new Intent();
|
|
||||||
intent.putExtra(OperationResult.EXTRA_RESULT, result);
|
|
||||||
getActivity().setResult(EditKeyActivity.RESULT_OK, intent);
|
|
||||||
getActivity().finish();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -465,7 +465,7 @@ public class EncryptFilesFragment
|
|||||||
if (actionsParcel.isIncomplete()) {
|
if (actionsParcel.isIncomplete()) {
|
||||||
// if this is still null, prepare output streams again
|
// if this is still null, prepare output streams again
|
||||||
if (mOutputUris == null) {
|
if (mOutputUris == null) {
|
||||||
// this may interrupt the flow, and call us again from onActivityResult
|
// this may interrupt the flow, and call us again from handleActivityResult
|
||||||
if (prepareOutputStreams(mShareAfterEncrypt)) {
|
if (prepareOutputStreams(mShareAfterEncrypt)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,10 @@ import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
|
|||||||
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
|
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
|
||||||
import org.sufficientlysecure.keychain.operations.results.ImportKeyResult;
|
import org.sufficientlysecure.keychain.operations.results.ImportKeyResult;
|
||||||
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
||||||
|
import org.sufficientlysecure.keychain.service.ImportKeyringParcel;
|
||||||
|
import org.sufficientlysecure.keychain.service.KeychainNewService;
|
||||||
import org.sufficientlysecure.keychain.service.KeychainService;
|
import org.sufficientlysecure.keychain.service.KeychainService;
|
||||||
|
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
|
||||||
import org.sufficientlysecure.keychain.ui.base.BaseNfcActivity;
|
import org.sufficientlysecure.keychain.ui.base.BaseNfcActivity;
|
||||||
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
|
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
|
||||||
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
||||||
@ -373,8 +376,7 @@ public class ImportKeysActivity extends BaseNfcActivity {
|
|||||||
if (returnData == null) {
|
if (returnData == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final ImportKeyResult result =
|
final ImportKeyResult result = returnData.getParcelable(OperationResult.EXTRA_RESULT);
|
||||||
returnData.getParcelable(OperationResult.EXTRA_RESULT);
|
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
Log.e(Constants.TAG, "result == null");
|
Log.e(Constants.TAG, "result == null");
|
||||||
return;
|
return;
|
||||||
@ -421,12 +423,9 @@ public class ImportKeysActivity extends BaseNfcActivity {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Send all information needed to service to import key in other thread
|
// Send all information needed to service to import key in other thread
|
||||||
Intent intent = new Intent(this, KeychainService.class);
|
Intent intent = new Intent(this, KeychainNewService.class);
|
||||||
|
ImportKeyringParcel operationInput = null;
|
||||||
intent.setAction(KeychainService.ACTION_IMPORT_KEYRING);
|
CryptoInputParcel cryptoInput = null;
|
||||||
|
|
||||||
// fill values for this action
|
|
||||||
Bundle data = new Bundle();
|
|
||||||
|
|
||||||
ImportKeysListFragment.LoaderState ls = mListFragment.getLoaderState();
|
ImportKeysListFragment.LoaderState ls = mListFragment.getLoaderState();
|
||||||
if (ls instanceof ImportKeysListFragment.BytesLoaderState) {
|
if (ls instanceof ImportKeysListFragment.BytesLoaderState) {
|
||||||
@ -445,21 +444,9 @@ public class ImportKeysActivity extends BaseNfcActivity {
|
|||||||
new ParcelableFileCache<>(this, "key_import.pcl");
|
new ParcelableFileCache<>(this, "key_import.pcl");
|
||||||
cache.writeCache(selectedEntries);
|
cache.writeCache(selectedEntries);
|
||||||
|
|
||||||
intent.putExtra(KeychainService.EXTRA_DATA, data);
|
operationInput = new ImportKeyringParcel(null, null);
|
||||||
|
cryptoInput = new CryptoInputParcel();
|
||||||
|
|
||||||
// Create a new Messenger for the communication back
|
|
||||||
Messenger messenger = new Messenger(serviceHandler);
|
|
||||||
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
|
|
||||||
|
|
||||||
// show progress dialog
|
|
||||||
serviceHandler.showProgressDialog(
|
|
||||||
getString(R.string.progress_importing),
|
|
||||||
ProgressDialog.STYLE_HORIZONTAL,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
// start service with intent
|
|
||||||
startService(intent);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(Constants.TAG, "Problem writing cache file", e);
|
Log.e(Constants.TAG, "Problem writing cache file", e);
|
||||||
Notify.create(this, "Problem writing cache file!", Notify.Style.ERROR)
|
Notify.create(this, "Problem writing cache file!", Notify.Style.ERROR)
|
||||||
@ -468,10 +455,6 @@ public class ImportKeysActivity extends BaseNfcActivity {
|
|||||||
} else if (ls instanceof ImportKeysListFragment.CloudLoaderState) {
|
} else if (ls instanceof ImportKeysListFragment.CloudLoaderState) {
|
||||||
ImportKeysListFragment.CloudLoaderState sls = (ImportKeysListFragment.CloudLoaderState) ls;
|
ImportKeysListFragment.CloudLoaderState sls = (ImportKeysListFragment.CloudLoaderState) ls;
|
||||||
|
|
||||||
data.putString(KeychainService.IMPORT_KEY_SERVER, sls.mCloudPrefs.keyserver);
|
|
||||||
|
|
||||||
data.putParcelable(KeychainService.EXTRA_PARCELABLE_PROXY, mProxyPrefs.parcelableProxy);
|
|
||||||
|
|
||||||
// get selected key entries
|
// get selected key entries
|
||||||
ArrayList<ParcelableKeyRing> keys = new ArrayList<>();
|
ArrayList<ParcelableKeyRing> keys = new ArrayList<>();
|
||||||
{
|
{
|
||||||
@ -483,23 +466,30 @@ public class ImportKeysActivity extends BaseNfcActivity {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
data.putParcelableArrayList(KeychainService.IMPORT_KEY_LIST, keys);
|
|
||||||
|
|
||||||
intent.putExtra(KeychainService.EXTRA_DATA, data);
|
operationInput = new ImportKeyringParcel(keys, sls.mCloudPrefs.keyserver);
|
||||||
|
if (mProxyPrefs != null) { // if not null means we have specified an explicit proxy
|
||||||
// Create a new Messenger for the communication back
|
cryptoInput = new CryptoInputParcel(mProxyPrefs.parcelableProxy);
|
||||||
Messenger messenger = new Messenger(serviceHandler);
|
} else {
|
||||||
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
|
cryptoInput = new CryptoInputParcel();
|
||||||
|
}
|
||||||
// show progress dialog
|
|
||||||
serviceHandler.showProgressDialog(
|
|
||||||
getString(R.string.progress_importing),
|
|
||||||
ProgressDialog.STYLE_HORIZONTAL, true
|
|
||||||
);
|
|
||||||
|
|
||||||
// start service with intent
|
|
||||||
startService(intent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
intent.putExtra(KeychainNewService.EXTRA_OPERATION_INPUT, operationInput);
|
||||||
|
intent.putExtra(KeychainNewService.EXTRA_CRYPTO_INPUT, cryptoInput);
|
||||||
|
|
||||||
|
// Create a new Messenger for the communication back
|
||||||
|
Messenger messenger = new Messenger(serviceHandler);
|
||||||
|
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
|
||||||
|
|
||||||
|
// show progress dialog
|
||||||
|
serviceHandler.showProgressDialog(
|
||||||
|
getString(R.string.progress_importing),
|
||||||
|
ProgressDialog.STYLE_HORIZONTAL, true
|
||||||
|
);
|
||||||
|
|
||||||
|
// start service with intent
|
||||||
|
startService(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -36,10 +36,8 @@ import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
|
|||||||
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
|
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
|
||||||
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
|
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
|
||||||
import org.sufficientlysecure.keychain.ui.NfcOperationActivity;
|
import org.sufficientlysecure.keychain.ui.NfcOperationActivity;
|
||||||
import org.sufficientlysecure.keychain.ui.OrbotRequiredDialogActivity;
|
|
||||||
import org.sufficientlysecure.keychain.ui.PassphraseDialogActivity;
|
import org.sufficientlysecure.keychain.ui.PassphraseDialogActivity;
|
||||||
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
|
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
|
||||||
import org.sufficientlysecure.keychain.util.orbot.OrbotHelper;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,7 +48,6 @@ public abstract class CryptoOperationFragment <T extends Parcelable, S extends O
|
|||||||
|
|
||||||
public static final int REQUEST_CODE_PASSPHRASE = 0x00008001;
|
public static final int REQUEST_CODE_PASSPHRASE = 0x00008001;
|
||||||
public static final int REQUEST_CODE_NFC = 0x00008002;
|
public static final int REQUEST_CODE_NFC = 0x00008002;
|
||||||
public static final int REQUEST_ENABLE_ORBOT = 0x00008004;
|
|
||||||
|
|
||||||
private void initiateInputActivity(RequiredInputParcel requiredInput) {
|
private void initiateInputActivity(RequiredInputParcel requiredInput) {
|
||||||
|
|
||||||
@ -71,12 +68,6 @@ public abstract class CryptoOperationFragment <T extends Parcelable, S extends O
|
|||||||
startActivityForResult(intent, REQUEST_CODE_PASSPHRASE);
|
startActivityForResult(intent, REQUEST_CODE_PASSPHRASE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
case ENABLE_ORBOT: {
|
|
||||||
Intent intent = new Intent(getActivity(), OrbotRequiredDialogActivity.class);
|
|
||||||
startActivityForResult(intent, REQUEST_ENABLE_ORBOT);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new RuntimeException("Unhandled pending result!");
|
throw new RuntimeException("Unhandled pending result!");
|
||||||
@ -110,14 +101,6 @@ public abstract class CryptoOperationFragment <T extends Parcelable, S extends O
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case REQUEST_ENABLE_ORBOT: {
|
|
||||||
if (resultCode == Activity.RESULT_OK && data != null) {
|
|
||||||
if (data.getBooleanExtra(OrbotRequiredDialogActivity.RESULT_IGNORE_TOR, false)) {
|
|
||||||
cryptoOperation(new CryptoInputParcel());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
|
||||||
|
* Copyright (C) 2015 Vincent Breitmoser <v.breitmoser@mugenguild.com>
|
||||||
|
*
|
||||||
|
* 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.ui.base;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Message;
|
||||||
|
import android.os.Messenger;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
import android.support.v4.app.Fragment;
|
||||||
|
|
||||||
|
import org.sufficientlysecure.keychain.R;
|
||||||
|
import org.sufficientlysecure.keychain.operations.results.InputPendingResult;
|
||||||
|
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
||||||
|
import org.sufficientlysecure.keychain.service.KeychainNewService;
|
||||||
|
import org.sufficientlysecure.keychain.service.KeychainService;
|
||||||
|
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
|
||||||
|
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
|
||||||
|
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
|
||||||
|
import org.sufficientlysecure.keychain.ui.NfcOperationActivity;
|
||||||
|
import org.sufficientlysecure.keychain.ui.OrbotRequiredDialogActivity;
|
||||||
|
import org.sufficientlysecure.keychain.ui.PassphraseDialogActivity;
|
||||||
|
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
|
||||||
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
|
import org.sufficientlysecure.keychain.util.OperationHelper;
|
||||||
|
import org.sufficientlysecure.keychain.util.orbot.OrbotHelper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All fragments executing crypto operations need to extend this class.
|
||||||
|
*/
|
||||||
|
public abstract class NewCryptoOperationFragment <T extends Parcelable, S extends OperationResult>
|
||||||
|
extends Fragment {
|
||||||
|
|
||||||
|
private OperationHelper<T, S> mOperationHelper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
|
Log.d("PHILIP", "requestCode "+ requestCode + " resultCode" + resultCode);
|
||||||
|
if (!mOperationHelper.handleActivityResult(requestCode, resultCode, data)) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setOperationHelper(OperationHelper<T, S> helper) {
|
||||||
|
mOperationHelper = helper;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void cryptoOperation() {
|
||||||
|
mOperationHelper.cryptoOperation();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void cryptoOperation(CryptoInputParcel inputParcel) {
|
||||||
|
mOperationHelper.cryptoOperation(inputParcel);
|
||||||
|
}
|
||||||
|
}
|
@ -70,7 +70,7 @@ public class FileHelper {
|
|||||||
* @param last default selected Uri, not supported by all file managers
|
* @param last default selected Uri, not supported by all file managers
|
||||||
* @param mimeType can be text/plain for example
|
* @param mimeType can be text/plain for example
|
||||||
* @param requestCode requestCode used to identify the result coming back from file manager to
|
* @param requestCode requestCode used to identify the result coming back from file manager to
|
||||||
* onActivityResult() in your activity
|
* handleActivityResult() in your activity
|
||||||
*/
|
*/
|
||||||
public static void openFile(Fragment fragment, Uri last, String mimeType, int requestCode) {
|
public static void openFile(Fragment fragment, Uri last, String mimeType, int requestCode) {
|
||||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||||
@ -144,7 +144,7 @@ public class FileHelper {
|
|||||||
* @param fragment
|
* @param fragment
|
||||||
* @param mimeType can be text/plain for example
|
* @param mimeType can be text/plain for example
|
||||||
* @param multiple allow file chooser to return multiple files
|
* @param multiple allow file chooser to return multiple files
|
||||||
* @param requestCode used to identify the result coming back from storage browser onActivityResult() in your
|
* @param requestCode used to identify the result coming back from storage browser handleActivityResult() in your
|
||||||
*/
|
*/
|
||||||
@TargetApi(Build.VERSION_CODES.KITKAT)
|
@TargetApi(Build.VERSION_CODES.KITKAT)
|
||||||
public static void openDocument(Fragment fragment, String mimeType, boolean multiple, int requestCode) {
|
public static void openDocument(Fragment fragment, String mimeType, boolean multiple, int requestCode) {
|
||||||
@ -162,7 +162,7 @@ public class FileHelper {
|
|||||||
* @param fragment
|
* @param fragment
|
||||||
* @param mimeType can be text/plain for example
|
* @param mimeType can be text/plain for example
|
||||||
* @param suggestedName a filename desirable for the file to be saved
|
* @param suggestedName a filename desirable for the file to be saved
|
||||||
* @param requestCode used to identify the result coming back from storage browser onActivityResult() in your
|
* @param requestCode used to identify the result coming back from storage browser handleActivityResult() in your
|
||||||
*/
|
*/
|
||||||
@TargetApi(Build.VERSION_CODES.KITKAT)
|
@TargetApi(Build.VERSION_CODES.KITKAT)
|
||||||
public static void saveDocument(Fragment fragment, String mimeType, String suggestedName, int requestCode) {
|
public static void saveDocument(Fragment fragment, String mimeType, String suggestedName, int requestCode) {
|
||||||
|
@ -0,0 +1,247 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
|
||||||
|
* Copyright (C) 2015 Vincent Breitmoser <v.breitmoser@mugenguild.com>
|
||||||
|
* Copyright (C) 2015 Adithya Abraham Philip <adithyaphilip@gmail.com>
|
||||||
|
*
|
||||||
|
* 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.util;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Message;
|
||||||
|
import android.os.Messenger;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
import android.support.v4.app.FragmentActivity;
|
||||||
|
import org.sufficientlysecure.keychain.R;
|
||||||
|
import org.sufficientlysecure.keychain.operations.results.InputPendingResult;
|
||||||
|
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
||||||
|
import org.sufficientlysecure.keychain.service.KeychainNewService;
|
||||||
|
import org.sufficientlysecure.keychain.service.KeychainService;
|
||||||
|
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
|
||||||
|
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
|
||||||
|
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
|
||||||
|
import org.sufficientlysecure.keychain.ui.NfcOperationActivity;
|
||||||
|
import org.sufficientlysecure.keychain.ui.OrbotRequiredDialogActivity;
|
||||||
|
import org.sufficientlysecure.keychain.ui.PassphraseDialogActivity;
|
||||||
|
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
|
||||||
|
|
||||||
|
public abstract class OperationHelper <T extends Parcelable, S extends OperationResult> {
|
||||||
|
public static final int REQUEST_CODE_PASSPHRASE = 0x00008001;
|
||||||
|
public static final int REQUEST_CODE_NFC = 0x00008002;
|
||||||
|
public static final int REQUEST_ENABLE_ORBOT = 0x00008004;
|
||||||
|
|
||||||
|
private FragmentActivity mActivity;
|
||||||
|
|
||||||
|
public OperationHelper(FragmentActivity activity) {
|
||||||
|
mActivity = activity;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initiateInputActivity(RequiredInputParcel requiredInput) {
|
||||||
|
|
||||||
|
Log.d("PHILIP", "Initating input " + requiredInput.mType);
|
||||||
|
switch (requiredInput.mType) {
|
||||||
|
case NFC_KEYTOCARD:
|
||||||
|
case NFC_DECRYPT:
|
||||||
|
case NFC_SIGN: {
|
||||||
|
Intent intent = new Intent(mActivity, NfcOperationActivity.class);
|
||||||
|
intent.putExtra(NfcOperationActivity.EXTRA_REQUIRED_INPUT, requiredInput);
|
||||||
|
mActivity.startActivityForResult(intent, REQUEST_CODE_NFC);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PASSPHRASE:
|
||||||
|
case PASSPHRASE_SYMMETRIC: {
|
||||||
|
Intent intent = new Intent(mActivity, PassphraseDialogActivity.class);
|
||||||
|
intent.putExtra(PassphraseDialogActivity.EXTRA_REQUIRED_INPUT, requiredInput);
|
||||||
|
mActivity.startActivityForResult(intent, REQUEST_CODE_PASSPHRASE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case ENABLE_ORBOT: {
|
||||||
|
Intent intent = new Intent(mActivity, OrbotRequiredDialogActivity.class);
|
||||||
|
mActivity.startActivityForResult(intent, REQUEST_ENABLE_ORBOT);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException("Unhandled pending result!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts the result of an activity started by this helper. Returns true if request code is recognized,
|
||||||
|
* false otherwise.
|
||||||
|
*
|
||||||
|
* @param requestCode
|
||||||
|
* @param resultCode
|
||||||
|
* @param data
|
||||||
|
* @return true if requestCode was recognized, false otherwise
|
||||||
|
*/
|
||||||
|
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
|
if (resultCode == Activity.RESULT_CANCELED) {
|
||||||
|
onCryptoOperationCancelled();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (requestCode) {
|
||||||
|
case REQUEST_CODE_PASSPHRASE: {
|
||||||
|
if (resultCode == Activity.RESULT_OK && data != null) {
|
||||||
|
CryptoInputParcel cryptoInput =
|
||||||
|
data.getParcelableExtra(PassphraseDialogActivity.RESULT_CRYPTO_INPUT);
|
||||||
|
cryptoOperation(cryptoInput);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case REQUEST_CODE_NFC: {
|
||||||
|
if (resultCode == Activity.RESULT_OK && data != null) {
|
||||||
|
CryptoInputParcel cryptoInput =
|
||||||
|
data.getParcelableExtra(NfcOperationActivity.RESULT_DATA);
|
||||||
|
cryptoOperation(cryptoInput);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case REQUEST_ENABLE_ORBOT: {
|
||||||
|
if (resultCode == Activity.RESULT_OK && data != null) {
|
||||||
|
if (data.getBooleanExtra(OrbotRequiredDialogActivity.RESULT_IGNORE_TOR, false)) {
|
||||||
|
cryptoOperation(new CryptoInputParcel());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default: {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void dismissProgress() {
|
||||||
|
ProgressDialogFragment progressDialogFragment =
|
||||||
|
(ProgressDialogFragment) mActivity.getSupportFragmentManager().findFragmentByTag("progressDialog");
|
||||||
|
|
||||||
|
if (progressDialogFragment == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
progressDialogFragment.dismissAllowingStateLoss();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract T createOperationInput();
|
||||||
|
|
||||||
|
public void cryptoOperation(CryptoInputParcel cryptoInput) {
|
||||||
|
|
||||||
|
T operationInput = createOperationInput();
|
||||||
|
if (operationInput == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send all information needed to service to edit key in other thread
|
||||||
|
Intent intent = new Intent(mActivity, KeychainNewService.class);
|
||||||
|
|
||||||
|
intent.putExtra(KeychainNewService.EXTRA_OPERATION_INPUT, operationInput);
|
||||||
|
intent.putExtra(KeychainNewService.EXTRA_CRYPTO_INPUT, cryptoInput);
|
||||||
|
|
||||||
|
ServiceProgressHandler saveHandler = new ServiceProgressHandler(mActivity) {
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create a new Messenger for the communication back
|
||||||
|
Messenger messenger = new Messenger(saveHandler);
|
||||||
|
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
|
||||||
|
|
||||||
|
saveHandler.showProgressDialog(
|
||||||
|
mActivity.getString(R.string.progress_building_key),
|
||||||
|
ProgressDialog.STYLE_HORIZONTAL, false);
|
||||||
|
|
||||||
|
mActivity.startService(intent);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cryptoOperation() {
|
||||||
|
cryptoOperation(new CryptoInputParcel());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onCryptoOperationResult(S result) {
|
||||||
|
Log.d("PHILIP", "cryptoResult " + result.success());
|
||||||
|
if (result.success()) {
|
||||||
|
onCryptoOperationSuccess(result);
|
||||||
|
} else {
|
||||||
|
onCryptoOperationError(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract protected void onCryptoOperationSuccess(S result);
|
||||||
|
|
||||||
|
protected void onCryptoOperationError(S result) {
|
||||||
|
result.createNotify(mActivity).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onCryptoOperationCancelled() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onHandleResult(OperationResult result) {
|
||||||
|
Log.d("PHILIP", "Handling result in OperationHelper");
|
||||||
|
|
||||||
|
if (result instanceof InputPendingResult) {
|
||||||
|
Log.d("PHILIP", "is pending result");
|
||||||
|
InputPendingResult pendingResult = (InputPendingResult) result;
|
||||||
|
if (pendingResult.isPending()) {
|
||||||
|
|
||||||
|
Log.d("PHILIP", "Is pending");
|
||||||
|
RequiredInputParcel requiredInput = pendingResult.getRequiredInputParcel();
|
||||||
|
initiateInputActivity(requiredInput);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dismissProgress();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// noinspection unchecked, because type erasure :(
|
||||||
|
onCryptoOperationResult((S) result);
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
throw new AssertionError("bad return class ("
|
||||||
|
+ result.getClass().getSimpleName() + "), this is a programming error!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user