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:drawablePadding="8dp"
android:gravity="center_vertical" android:gravity="center_vertical"
android:paddingRight="20dp" android:paddingRight="20dp"
android:text="Save (set in-code!)" /> android:text="Done (set in-code!)" />
</FrameLayout> </FrameLayout>

View File

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

View File

@ -4,20 +4,38 @@
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" > android:orientation="vertical" >
<LinearLayout <RelativeLayout
android:layout_width="fill_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_horizontal"
android:orientation="horizontal" android:orientation="horizontal"
android:paddingBottom="3dip" > 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 <TextView
android:id="@+id/api_app_settings_selected_key" android:id="@+id/api_app_settings_app_name"
android:layout_width="0dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_centerVertical="true"
android:layout_weight="1" android:layout_toRightOf="@+id/api_app_settings_app_icon"
android:text="@string/api_settings_no_key" android:gravity="center_vertical"
android:orientation="vertical"
android:text="Name (set in-code)"
android:textAppearance="?android:attr/textAppearanceMedium" /> android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button <Button
android:id="@+id/api_app_settings_select_key_button" android:id="@+id/api_app_settings_select_key_button"
@ -25,6 +43,33 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:text="@string/api_settings_select_key" /> 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> </LinearLayout>
<Button <Button

View File

@ -55,7 +55,7 @@ public class KeychainContract {
interface ApiAppsColumns { interface ApiAppsColumns {
String PACKAGE_NAME = "package_name"; 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 ASCII_ARMOR = "ascii_armor";
String ENCRYPTION_ALGORITHM = "encryption_algorithm"; String ENCRYPTION_ALGORITHM = "encryption_algorithm";
String HASH_ALORITHM = "hash_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 " private static final String CREATE_API_APPS = "CREATE TABLE IF NOT EXISTS "
+ Tables.API_APPS + " (" + BaseColumns._ID + Tables.API_APPS + " (" + BaseColumns._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + ApiAppsColumns.PACKAGE_NAME + " 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.ASCII_ARMOR + " INTEGER, "
+ ApiAppsColumns.ENCRYPTION_ALGORITHM + " INTEGER, " + ApiAppsColumns.ENCRYPTION_ALGORITHM + " INTEGER, "
+ ApiAppsColumns.HASH_ALORITHM + " INTEGER, " + ApiAppsColumns.HASH_ALORITHM + " INTEGER, "

View File

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