select key for api apps

This commit is contained in:
Dominik Schürmann 2013-09-06 08:29:56 +02:00
parent 00d03d452e
commit 2b303d3558
7 changed files with 165 additions and 49 deletions

View File

@ -31,6 +31,6 @@
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:paddingRight="20dp"
android:text="Save (set in-code!)" />
android:text="Done (set in-code!)" />
</FrameLayout>

View File

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="8dp" >
<fragment
android:id="@+id/api_app_settings_fragment"
@ -11,4 +13,4 @@
android:layout_height="wrap_content"
tools:layout="@layout/api_app_settings_fragment" />
</RelativeLayout>
</LinearLayout>

View File

@ -4,20 +4,38 @@
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingBottom="3dip" >
<ImageView
android:id="@+id/api_app_settings_app_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dp"
android:src="@drawable/icon" />
<TextView
android:id="@+id/api_app_settings_selected_key"
android:layout_width="0dip"
android:id="@+id/api_app_settings_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="@string/api_settings_no_key"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/api_app_settings_app_icon"
android:gravity="center_vertical"
android:orientation="vertical"
android:text="Name (set in-code)"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/api_app_settings_select_key_button"
@ -25,6 +43,33 @@
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/api_settings_select_key" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="16dp" >
<TextView
android:id="@+id/api_app_settings_user_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:ellipsize="end"
android:singleLine="true"
android:text="@string/api_settings_no_key"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/api_app_settings_user_id_rest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:ellipsize="end"
android:singleLine="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
<Button

View File

@ -55,7 +55,7 @@ public class KeychainContract {
interface ApiAppsColumns {
String PACKAGE_NAME = "package_name";
String PRIVATE_KEY_ID = "private_key_id"; // not a database id
String KEY_ID = "key_id"; // not a database id
String ASCII_ARMOR = "ascii_armor";
String ENCRYPTION_ALGORITHM = "encryption_algorithm";
String HASH_ALORITHM = "hash_algorithm";

View File

@ -67,7 +67,7 @@ public class KeychainDatabase extends SQLiteOpenHelper {
private static final String CREATE_API_APPS = "CREATE TABLE IF NOT EXISTS "
+ Tables.API_APPS + " (" + BaseColumns._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + ApiAppsColumns.PACKAGE_NAME
+ " TEXT UNIQUE, " + ApiAppsColumns.PRIVATE_KEY_ID + " INT64, "
+ " TEXT UNIQUE, " + ApiAppsColumns.KEY_ID + " INT64, "
+ ApiAppsColumns.ASCII_ARMOR + " INTEGER, "
+ ApiAppsColumns.ENCRYPTION_ALGORITHM + " INTEGER, "
+ ApiAppsColumns.HASH_ALORITHM + " INTEGER, "

View File

@ -1,8 +1,14 @@
package org.sufficientlysecure.keychain.remote_api;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Id;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.PgpHelper;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.SelectSecretKeyActivity;
import org.sufficientlysecure.keychain.util.Log;
import android.content.ContentValues;
@ -11,12 +17,15 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
@ -28,15 +37,15 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
private PackageManager pm;
// model
Uri appUri;
String packageName;
long keyId;
// model, derived
String appName;
Uri mAppUri;
String mPackageName;
long mSecretKeyId = Id.key.none;
// view
TextView selectedKey;
TextView appNameView;
ImageView appIconView;
TextView keyUserId;
TextView keyUserIdRest;
Button selectKeyButton;
CheckBox asciiArmorCheckBox;
Button saveButton;
@ -75,19 +84,30 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
setContentView(R.layout.api_app_settings_activity);
selectedKey = (TextView) findViewById(R.id.api_app_settings_selected_key);
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);
selectKeyButton = (Button) findViewById(R.id.api_app_settings_select_key_button);
asciiArmorCheckBox = (CheckBox) findViewById(R.id.api_app_ascii_armor);
selectKeyButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectSecretKey();
}
});
Intent intent = getIntent();
appUri = intent.getData();
if (appUri == null) {
mAppUri = intent.getData();
if (mAppUri == null) {
Log.e(Constants.TAG, "Intent data missing. Should be Uri of app!");
finish();
return;
} else {
Log.d(Constants.TAG, "uri: " + appUri);
loadData(appUri);
Log.d(Constants.TAG, "uri: " + mAppUri);
loadData(mAppUri);
}
}
@ -114,18 +134,28 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
private void loadData(Uri appUri) {
Cursor cur = getContentResolver().query(appUri, null, null, null, null);
if (cur.moveToFirst()) {
packageName = cur.getString(cur.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME));
// get application name
mPackageName = cur.getString(cur.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME));
// get application name and icon from package manager
String appName = null;
Drawable appIcon = null;
try {
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
ApplicationInfo ai = pm.getApplicationInfo(mPackageName, 0);
appName = (String) pm.getApplicationLabel(ai);
appIcon = pm.getApplicationIcon(ai);
} catch (final NameNotFoundException e) {
appName = getString(R.string.api_unknown_app);
}
setTitle(appName);
appNameView.setText(appName);
appIconView.setImageDrawable(appIcon);
try {
mSecretKeyId = (cur.getLong(cur
.getColumnIndexOrThrow(KeychainContract.ApiApps.KEY_ID)));
Log.d(Constants.TAG, "mSecretKeyId: " + mSecretKeyId);
updateSelectedKeyView(mSecretKeyId);
boolean asciiArmor = (cur.getInt(cur
.getColumnIndexOrThrow(KeychainContract.ApiApps.ASCII_ARMOR)) == 1);
asciiArmorCheckBox.setChecked(asciiArmor);
@ -137,7 +167,7 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
}
private void revokeAccess() {
if (getContentResolver().delete(appUri, null, null) <= 0) {
if (getContentResolver().delete(mAppUri, null, null) <= 0) {
throw new RuntimeException();
}
finish();
@ -145,14 +175,69 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
private void save() {
final ContentValues cv = new ContentValues();
// cv.put(KeychainContract.ApiApps.PACKAGE_NAME, packageName);
cv.put(KeychainContract.ApiApps.KEY_ID, mSecretKeyId);
cv.put(KeychainContract.ApiApps.ASCII_ARMOR, asciiArmorCheckBox.isChecked());
// TODO: other parameters
if (getContentResolver().update(appUri, cv, null, null) <= 0) {
if (getContentResolver().update(mAppUri, cv, null, null) <= 0) {
throw new RuntimeException();
}
finish();
}
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);
}
}
}

View File

@ -21,8 +21,6 @@ public class AppSettingsFragment extends Fragment {
private LinearLayout advancedSettingsContainer;
private Button advancedSettingsButton;
private Button selectKeyButton;
/**
* Inflate the layout for this fragment
*/
@ -39,8 +37,6 @@ public class AppSettingsFragment extends Fragment {
R.id.api_app_settings_advanced_button);
advancedSettingsContainer = (LinearLayout) getActivity().findViewById(
R.id.api_app_settings_advanced);
selectKeyButton = (Button) getActivity().findViewById(
R.id.api_app_settings_select_key_button);
final Animation visibleAnimation = new AlphaAnimation(0.0f, 1.0f);
visibleAnimation.setDuration(250);
@ -68,18 +64,6 @@ public class AppSettingsFragment extends Fragment {
}
}
});
selectKeyButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectSecretKey();
}
});
}
private void selectSecretKey() {
Intent intent = new Intent(getActivity(), SelectSecretKeyActivity.class);
startActivityForResult(intent, Id.request.secret_keys);
}
}