Files
open-keychain/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java

428 lines
18 KiB
Java
Raw Normal View History

/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2011 Senecaso
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2013-01-16 14:31:16 +01:00
package org.sufficientlysecure.keychain.ui;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Id;
2013-09-08 15:30:05 +02:00
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.ActionBarHelper;
2013-01-16 14:31:16 +01:00
import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler;
import org.sufficientlysecure.keychain.ui.dialog.DeleteFileDialogFragment;
import org.sufficientlysecure.keychain.ui.dialog.FileDialogFragment;
import org.sufficientlysecure.keychain.util.Log;
import android.app.AlertDialog;
import android.app.ProgressDialog;
2013-09-19 02:02:51 +02:00
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
2013-09-19 02:02:51 +02:00
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
2013-09-19 02:02:51 +02:00
import android.widget.ArrayAdapter;
import android.widget.Toast;
2013-09-19 02:02:51 +02:00
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
2013-09-19 02:02:51 +02:00
public class ImportKeysActivity extends SherlockFragmentActivity implements OnNavigationListener {
2013-09-09 14:33:37 +02:00
public static final String ACTION_IMPORT_KEY = Constants.INTENT_PREFIX + "IMPORT_KEY";
public static final String ACTION_IMPORT_KEY_FROM_QR_CODE = Constants.INTENT_PREFIX
+ "IMPORT_KEY_FROM_QR_CODE";
2013-09-09 14:03:58 +02:00
// Actions for internal use only:
2013-09-19 02:02:51 +02:00
public static final String ACTION_IMPORT_KEY_FROM_FILE = Constants.INTENT_PREFIX
2013-09-09 14:33:37 +02:00
+ "IMPORT_KEY_FROM_FILE";
public static final String ACTION_IMPORT_KEY_FROM_NFC = Constants.INTENT_PREFIX
+ "IMPORT_KEY_FROM_NFC";
// only used by IMPORT
2013-09-10 00:39:41 +02:00
public static final String EXTRA_KEY_BYTES = "key_bytes";
2013-09-09 14:03:58 +02:00
// TODO: import keys from server
// public static final String EXTRA_KEY_ID = "keyId";
protected boolean mDeleteAfterImport = false;
FileDialogFragment mFileDialog;
2013-09-19 02:02:51 +02:00
ImportKeysListFragment mListFragment;
OnNavigationListener mOnNavigationListener;
String[] mNavigationStrings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.import_keys);
// set actionbar without home button if called from another app
2013-09-08 15:30:05 +02:00
ActionBarHelper.setBackButton(this);
2013-09-19 02:02:51 +02:00
// set drop down navigation
mNavigationStrings = getResources().getStringArray(R.array.import_action_list);
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context,
R.array.import_action_list, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.import_keys_list_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create an instance of the fragment
mListFragment = ImportKeysListFragment.newInstance();
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.replace(R.id.import_keys_list_container, mListFragment).commit();
// do it immediately!
getSupportFragmentManager().executePendingTransactions();
}
handleActions(getIntent());
}
2013-09-19 02:02:51 +02:00
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
// Create new fragment from our own Fragment class
switch (itemPosition) {
case 0:
2013-09-19 02:17:49 +02:00
loadFragment(ImportKeysFileFragment.class, null, mNavigationStrings[itemPosition]);
2013-09-19 02:02:51 +02:00
break;
case 1:
2013-09-19 02:17:49 +02:00
loadFragment(ImportKeysServerFragment.class, null, mNavigationStrings[itemPosition]);
2013-09-19 02:02:51 +02:00
break;
case 2:
2013-09-19 02:17:49 +02:00
loadFragment(ImportKeysQrCodeFragment.class, null, mNavigationStrings[itemPosition]);
2013-09-19 02:02:51 +02:00
break;
case 3:
2013-09-19 02:17:49 +02:00
loadFragment(ImportKeysNFCFragment.class, null, mNavigationStrings[itemPosition]);
2013-09-19 02:02:51 +02:00
break;
default:
break;
}
return true;
}
private void loadFragment(Class<?> clss, Bundle args, String tag) {
Fragment fragment = Fragment.instantiate(this, clss.getName(), args);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment container with this fragment
// and give the fragment a tag name equal to the string at the position selected
ft.replace(R.id.import_navigation_fragment, fragment, tag);
// Apply changes
ft.commit();
}
public void loadCallback(byte[] importData, String importFilename) {
mListFragment.load(importData, importFilename);
}
/**
* ActionBar menu is created based on class variables to change it at runtime
*
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, Id.menu.option.key_server, 0, R.string.menu_keyServer).setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(1, Id.menu.option.import_from_file, 1, R.string.menu_importFromFile)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(1, Id.menu.option.import_from_qr_code, 2, R.string.menu_importFromQrCode)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(1, Id.menu.option.import_from_nfc, 3, R.string.menu_importFromNfc)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in Action Bar clicked; go home
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case Id.menu.option.key_server:
startActivityForResult(new Intent(this, KeyServerQueryActivity.class), 0);
return true;
2013-09-19 02:02:51 +02:00
// case Id.menu.option.import_from_file:
// showImportFromFileDialog();
// return true;
2013-09-19 02:02:51 +02:00
// case Id.menu.option.import_from_qr_code:
// importFromQrCode();
// return true;
//
// case Id.menu.option.import_from_nfc:
// importFromNfc();
// return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void handleActions(Intent intent) {
String action = intent.getAction();
Bundle extras = intent.getExtras();
if (extras == null) {
extras = new Bundle();
}
/**
* Android Standard Actions
*/
if (Intent.ACTION_VIEW.equals(action)) {
2013-09-09 14:03:58 +02:00
// Android's Action when opening file associated to Keychain (see AndroidManifest.xml)
2013-09-19 02:02:51 +02:00
// override action to delegate it to Keychain's ACTION_IMPORT_KEY
2013-09-09 14:33:37 +02:00
action = ACTION_IMPORT_KEY;
}
/**
2013-09-19 02:02:51 +02:00
* Keychain's own Actions
*/
2013-09-09 14:33:37 +02:00
if (ACTION_IMPORT_KEY.equals(action)) {
if ("file".equals(intent.getScheme()) && intent.getDataString() != null) {
2013-09-19 02:02:51 +02:00
String importFilename = intent.getData().getPath();
// display selected filename
getSupportActionBar().setSelectedNavigationItem(0);
Bundle args = new Bundle();
2013-09-19 02:17:49 +02:00
args.putString(ImportKeysFileFragment.ARG_PATH, importFilename);
loadFragment(ImportKeysFileFragment.class, args, mNavigationStrings[0]);
2013-09-19 02:02:51 +02:00
// directly load data
loadCallback(null, importFilename);
2013-09-10 00:39:41 +02:00
} else if (extras.containsKey(EXTRA_KEY_BYTES)) {
2013-09-19 02:02:51 +02:00
byte[] importData = intent.getByteArrayExtra(EXTRA_KEY_BYTES);
loadCallback(importData, null);
}
2013-09-19 02:02:51 +02:00
// Internal actions:
} else if (ACTION_IMPORT_KEY_FROM_FILE.equals(action)) {
getSupportActionBar().setSelectedNavigationItem(0);
2013-09-19 02:17:49 +02:00
loadFragment(ImportKeysFileFragment.class, null, mNavigationStrings[0]);
2013-09-09 14:33:37 +02:00
} else if (ACTION_IMPORT_KEY_FROM_QR_CODE.equals(action)) {
2013-09-19 02:02:51 +02:00
getSupportActionBar().setSelectedNavigationItem(2);
2013-09-19 02:17:49 +02:00
loadFragment(ImportKeysQrCodeFragment.class, null, mNavigationStrings[2]);
2013-09-09 14:33:37 +02:00
} else if (ACTION_IMPORT_KEY_FROM_NFC.equals(action)) {
2013-09-19 02:02:51 +02:00
getSupportActionBar().setSelectedNavigationItem(3);
2013-09-19 02:17:49 +02:00
loadFragment(ImportKeysNFCFragment.class, null, mNavigationStrings[3]);
}
}
// private void importAndSignOld(final long keyId, final String expectedFingerprint) {
// if (expectedFingerprint != null && expectedFingerprint.length() > 0) {
//
// Thread t = new Thread() {
// @Override
// public void run() {
// try {
// // TODO: display some sort of spinner here while the user waits
//
// // TODO: there should be only 1
// HkpKeyServer server = new HkpKeyServer(mPreferences.getKeyServers()[0]);
// String encodedKey = server.get(keyId);
//
// PGPKeyRing keyring = PGPHelper.decodeKeyRing(new ByteArrayInputStream(
// encodedKey.getBytes()));
// if (keyring != null && keyring instanceof PGPPublicKeyRing) {
// PGPPublicKeyRing publicKeyRing = (PGPPublicKeyRing) keyring;
//
// // make sure the fingerprints match before we cache this thing
// String actualFingerprint = PGPHelper.convertFingerprintToHex(publicKeyRing
// .getPublicKey().getFingerprint());
// if (expectedFingerprint.equals(actualFingerprint)) {
// // store the signed key in our local cache
// int retval = PGPMain.storeKeyRingInCache(publicKeyRing);
// if (retval != Id.return_value.ok
// && retval != Id.return_value.updated) {
// status.putString(EXTRA_ERROR,
// "Failed to store signed key in local cache");
// } else {
// Intent intent = new Intent(ImportFromQRCodeActivity.this,
// SignKeyActivity.class);
// intent.putExtra(EXTRA_KEY_ID, keyId);
// startActivityForResult(intent, Id.request.sign_key);
// }
// } else {
// status.putString(
// EXTRA_ERROR,
// "Scanned fingerprint does NOT match the fingerprint of the received key. You shouldnt trust this key.");
// }
// }
// } catch (QueryException e) {
// Log.e(TAG, "Failed to query KeyServer", e);
// status.putString(EXTRA_ERROR, "Failed to query KeyServer");
// status.putInt(Constants.extras.STATUS, Id.message.done);
// } catch (IOException e) {
// Log.e(TAG, "Failed to query KeyServer", e);
// status.putString(EXTRA_ERROR, "Failed to query KeyServer");
// status.putInt(Constants.extras.STATUS, Id.message.done);
// }
// }
// };
//
// t.setName("KeyExchange Download Thread");
// t.setDaemon(true);
// t.start();
// }
// }
/**
* Import keys with mImportData
*/
public void importKeys() {
2013-09-19 02:02:51 +02:00
if (mListFragment.getKeyBytes() != null || mListFragment.getImportFilename() != null) {
Log.d(Constants.TAG, "importKeys started");
// Send all information needed to service to import key in other thread
2013-01-16 14:31:16 +01:00
Intent intent = new Intent(this, KeychainIntentService.class);
intent.setAction(KeychainIntentService.ACTION_IMPORT_KEYRING);
// fill values for this action
Bundle data = new Bundle();
// TODO: check for key type?
2013-09-09 14:03:58 +02:00
// data.putInt(KeychainIntentService.IMPORT_KEY_TYPE, Id.type.secret_key);
2013-09-19 02:02:51 +02:00
if (mListFragment.getKeyBytes() != null) {
2013-01-16 14:31:16 +01:00
data.putInt(KeychainIntentService.TARGET, KeychainIntentService.TARGET_BYTES);
2013-09-19 02:02:51 +02:00
data.putByteArray(KeychainIntentService.IMPORT_BYTES, mListFragment.getKeyBytes());
} else {
2013-01-16 14:31:16 +01:00
data.putInt(KeychainIntentService.TARGET, KeychainIntentService.TARGET_FILE);
2013-09-19 02:02:51 +02:00
data.putString(KeychainIntentService.IMPORT_FILENAME,
mListFragment.getImportFilename());
}
2013-01-16 14:31:16 +01:00
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
// Message is received after importing is done in ApgService
2013-01-16 14:31:16 +01:00
KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this,
R.string.progress_importing, ProgressDialog.STYLE_HORIZONTAL) {
public void handleMessage(Message message) {
// handle messages by standard ApgHandler first
super.handleMessage(message);
2013-01-16 14:31:16 +01:00
if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) {
// get returned data bundle
Bundle returnData = message.getData();
2013-01-16 14:31:16 +01:00
int added = returnData.getInt(KeychainIntentService.RESULT_IMPORT_ADDED);
2013-09-09 14:03:58 +02:00
int updated = returnData
.getInt(KeychainIntentService.RESULT_IMPORT_UPDATED);
2013-01-16 14:31:16 +01:00
int bad = returnData.getInt(KeychainIntentService.RESULT_IMPORT_BAD);
String toastMessage;
if (added > 0 && updated > 0) {
toastMessage = getString(R.string.keysAddedAndUpdated, added, updated);
} else if (added > 0) {
toastMessage = getString(R.string.keysAdded, added);
} else if (updated > 0) {
toastMessage = getString(R.string.keysUpdated, updated);
} else {
toastMessage = getString(R.string.noKeysAddedOrUpdated);
}
Toast.makeText(ImportKeysActivity.this, toastMessage, Toast.LENGTH_SHORT)
.show();
if (bad > 0) {
AlertDialog.Builder alert = new AlertDialog.Builder(
ImportKeysActivity.this);
alert.setIcon(android.R.drawable.ic_dialog_alert);
alert.setTitle(R.string.warning);
alert.setMessage(ImportKeysActivity.this.getString(
R.string.badKeysEncountered, bad));
alert.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alert.setCancelable(true);
alert.create().show();
} else if (mDeleteAfterImport) {
// everything went well, so now delete, if that was turned on
DeleteFileDialogFragment deleteFileDialog = DeleteFileDialogFragment
2013-09-19 02:02:51 +02:00
.newInstance(mListFragment.getImportFilename());
deleteFileDialog.show(getSupportFragmentManager(), "deleteDialog");
}
}
};
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
2013-01-16 14:31:16 +01:00
intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);
// show progress dialog
saveHandler.showProgressDialog(this);
// start service with intent
startService(intent);
} else {
Toast.makeText(this, R.string.error_nothingImport, Toast.LENGTH_LONG).show();
}
}
2013-09-19 02:02:51 +02:00
public void importOnClick(View view) {
importKeys();
}
public void signAndUploadOnClick(View view) {
// first, import!
2013-09-19 02:02:51 +02:00
// importOnClick(view);
// TODO: implement sign and upload!
Toast.makeText(ImportKeysActivity.this, "Not implemented right now!", Toast.LENGTH_SHORT)
.show();
}
}