Files
open-keychain/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsActivity.java

245 lines
8.6 KiB
Java
Raw Normal View History

package org.sufficientlysecure.keychain.remote_api;
2013-09-06 08:29:56 +02:00
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.sufficientlysecure.keychain.Constants;
2013-09-06 08:29:56 +02:00
import org.sufficientlysecure.keychain.Id;
import org.sufficientlysecure.keychain.R;
2013-09-06 08:29:56 +02:00
import org.sufficientlysecure.keychain.helper.PgpHelper;
2013-09-04 23:47:40 +02:00
import org.sufficientlysecure.keychain.provider.KeychainContract;
2013-09-06 08:29:56 +02:00
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.SelectSecretKeyActivity;
import org.sufficientlysecure.keychain.util.Log;
2013-09-05 00:02:48 +02:00
import android.content.ContentValues;
import android.content.Intent;
2013-09-04 23:47:40 +02:00
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
2013-09-06 08:29:56 +02:00
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
2013-09-05 21:25:49 +02:00
import android.view.LayoutInflater;
2013-09-05 00:02:48 +02:00
import android.view.View;
2013-09-06 08:29:56 +02:00
import android.view.View.OnClickListener;
2013-09-04 23:47:40 +02:00
import android.widget.Button;
import android.widget.CheckBox;
2013-09-06 08:29:56 +02:00
import android.widget.ImageView;
2013-09-04 23:47:40 +02:00
import android.widget.TextView;
2013-09-05 21:25:49 +02:00
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
2013-09-05 21:25:49 +02:00
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class AppSettingsActivity extends SherlockFragmentActivity {
2013-09-04 23:47:40 +02:00
private PackageManager pm;
2013-09-05 00:02:48 +02:00
// model
2013-09-06 08:29:56 +02:00
Uri mAppUri;
String mPackageName;
long mSecretKeyId = Id.key.none;
2013-09-04 23:47:40 +02:00
// view
2013-09-06 08:29:56 +02:00
TextView appNameView;
ImageView appIconView;
TextView keyUserId;
TextView keyUserIdRest;
2013-09-04 23:47:40 +02:00
Button selectKeyButton;
CheckBox asciiArmorCheckBox;
2013-09-05 00:02:48 +02:00
Button saveButton;
Button revokeButton;
2013-09-04 23:47:40 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2013-09-04 23:47:40 +02:00
pm = getApplicationContext().getPackageManager();
2013-09-05 21:25:49 +02:00
// BEGIN_INCLUDE (inflate_set_custom_view)
// Inflate a "Done" custom action bar view to serve as the "Up" affordance.
final LayoutInflater inflater = (LayoutInflater) getSupportActionBar().getThemedContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater
.inflate(R.layout.actionbar_custom_view_done, null);
((TextView) customActionBarView.findViewById(R.id.actionbar_done_text))
.setText(R.string.api_settings_save);
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// "Done"
save();
}
});
// Show the custom action bar view and hide the normal Home icon and title.
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView);
// END_INCLUDE (inflate_set_custom_view)
setContentView(R.layout.api_app_settings_activity);
2013-09-06 08:29:56 +02:00
appNameView = (TextView) findViewById(R.id.api_app_settings_app_name);
appIconView = (ImageView) findViewById(R.id.api_app_settings_app_icon);
keyUserId = (TextView) findViewById(R.id.api_app_settings_user_id);
keyUserIdRest = (TextView) findViewById(R.id.api_app_settings_user_id_rest);
2013-09-04 23:47:40 +02:00
selectKeyButton = (Button) findViewById(R.id.api_app_settings_select_key_button);
asciiArmorCheckBox = (CheckBox) findViewById(R.id.api_app_ascii_armor);
2013-09-06 08:29:56 +02:00
selectKeyButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectSecretKey();
}
});
Intent intent = getIntent();
2013-09-06 08:29:56 +02:00
mAppUri = intent.getData();
if (mAppUri == null) {
Log.e(Constants.TAG, "Intent data missing. Should be Uri of app!");
finish();
return;
2013-09-04 23:47:40 +02:00
} else {
2013-09-06 08:29:56 +02:00
Log.d(Constants.TAG, "uri: " + mAppUri);
loadData(mAppUri);
2013-09-04 23:47:40 +02:00
}
}
2013-09-05 21:25:49 +02:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.api_app_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_api_settings_revoke:
revokeAccess();
return true;
case R.id.menu_api_settings_cancel:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
2013-09-04 23:47:40 +02:00
private void loadData(Uri appUri) {
Cursor cur = getContentResolver().query(appUri, null, null, null, null);
if (cur.moveToFirst()) {
2013-09-06 08:29:56 +02:00
mPackageName = cur.getString(cur.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME));
// get application name and icon from package manager
String appName = null;
Drawable appIcon = null;
2013-09-05 00:27:17 +02:00
try {
2013-09-06 08:29:56 +02:00
ApplicationInfo ai = pm.getApplicationInfo(mPackageName, 0);
2013-09-05 00:27:17 +02:00
appName = (String) pm.getApplicationLabel(ai);
2013-09-06 08:29:56 +02:00
appIcon = pm.getApplicationIcon(ai);
2013-09-05 00:27:17 +02:00
} catch (final NameNotFoundException e) {
2013-09-06 08:40:13 +02:00
// fallback
appName = mPackageName;
2013-09-05 00:27:17 +02:00
}
2013-09-06 08:29:56 +02:00
appNameView.setText(appName);
appIconView.setImageDrawable(appIcon);
2013-09-05 00:27:17 +02:00
try {
2013-09-06 08:29:56 +02:00
mSecretKeyId = (cur.getLong(cur
.getColumnIndexOrThrow(KeychainContract.ApiApps.KEY_ID)));
Log.d(Constants.TAG, "mSecretKeyId: " + mSecretKeyId);
updateSelectedKeyView(mSecretKeyId);
2013-09-05 00:27:17 +02:00
boolean asciiArmor = (cur.getInt(cur
.getColumnIndexOrThrow(KeychainContract.ApiApps.ASCII_ARMOR)) == 1);
asciiArmorCheckBox.setChecked(asciiArmor);
} catch (IllegalArgumentException e) {
Log.e(Constants.TAG, "AppSettingsActivity", e);
}
}
2013-09-05 00:02:48 +02:00
}
private void revokeAccess() {
2013-09-06 08:29:56 +02:00
if (getContentResolver().delete(mAppUri, null, null) <= 0) {
2013-09-05 00:27:17 +02:00
throw new RuntimeException();
}
2013-09-05 00:02:48 +02:00
finish();
}
private void save() {
final ContentValues cv = new ContentValues();
2013-09-06 08:29:56 +02:00
cv.put(KeychainContract.ApiApps.KEY_ID, mSecretKeyId);
2013-09-05 00:27:17 +02:00
cv.put(KeychainContract.ApiApps.ASCII_ARMOR, asciiArmorCheckBox.isChecked());
// TODO: other parameters
2013-09-06 08:29:56 +02:00
if (getContentResolver().update(mAppUri, cv, null, null) <= 0) {
2013-09-05 00:27:17 +02:00
throw new RuntimeException();
}
2013-09-05 00:02:48 +02:00
finish();
}
2013-09-06 08:29:56 +02:00
private void selectSecretKey() {
Intent intent = new Intent(this, SelectSecretKeyActivity.class);
startActivityForResult(intent, Id.request.secret_keys);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case Id.request.secret_keys: {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
mSecretKeyId = bundle.getLong(SelectSecretKeyActivity.RESULT_EXTRA_MASTER_KEY_ID);
} else {
mSecretKeyId = Id.key.none;
}
updateSelectedKeyView(mSecretKeyId);
break;
}
default: {
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void updateSelectedKeyView(long secretKeyId) {
if (secretKeyId == Id.key.none) {
keyUserId.setText(R.string.api_settings_no_key);
keyUserIdRest.setText("");
} else {
String uid = getResources().getString(R.string.unknownUserId);
String uidExtra = "";
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(this,
secretKeyId);
if (keyRing != null) {
PGPSecretKey key = PgpHelper.getMasterKey(keyRing);
if (key != null) {
String userId = PgpHelper.getMainUserIdSafe(this, key);
String chunks[] = userId.split(" <", 2);
uid = chunks[0];
if (chunks.length > 1) {
uidExtra = "<" + chunks[1];
}
}
}
keyUserId.setText(uid);
keyUserIdRest.setText(uidExtra);
}
}
}