Put logic into fragment for reuse

This commit is contained in:
Dominik Schürmann 2013-09-06 09:44:56 +02:00
parent 4030739a99
commit c9549befb2
6 changed files with 234 additions and 201 deletions

View File

@ -462,7 +462,7 @@
<activity
android:name="org.sufficientlysecure.keychain.remote_api.ServiceActivity"
android:exported="false"
android:label="TODO crypto activity"
android:label="@string/app_name"
android:process=":crypto" >
<!-- Don't publish intents, they are only used internally! -->

View File

@ -6,6 +6,14 @@
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="@+id/api_register_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="3dip"
android:text="@string/api_register_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<fragment
android:id="@+id/api_app_settings_fragment"
android:name="org.sufficientlysecure.keychain.remote_api.AppSettingsFragment"
@ -13,39 +21,4 @@
android:layout_height="wrap_content"
tools:layout="@layout/api_app_settings_fragment" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="3dip" >
<TextView
android:id="@+id/api_register_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TODO: use string"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="3dip" >
<Button
android:id="@+id/api_register_disallow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/api_register_disallow" />
<Button
android:id="@+id/api_register_allow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/api_register_allow" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

View File

@ -369,7 +369,7 @@
<string name="api_settings_save">Save</string>
<string name="api_settings_cancel">Cancel</string>
<string name="api_settings_revoke">Revoke access</string>
<string name="api_register_text">%1$s (%2$s) requests access to OpenPGP Keychain\'s API.\n\nAllow permanent access?</string>
<string name="api_register_text">The following application requests access to OpenPGP Keychain\'s API.\n\nAllow permanent access?</string>
<string name="api_register_allow">Allow access</string>
<string name="api_register_disallow">Disallow access</string>

View File

@ -1,31 +1,18 @@
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;
import android.content.Intent;
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;
@ -34,28 +21,21 @@ import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class AppSettingsActivity extends SherlockFragmentActivity {
private PackageManager pm;
// model
Uri mAppUri;
String mPackageName;
long mSecretKeyId = Id.key.none;
// view
TextView appNameView;
ImageView appIconView;
TextView keyUserId;
TextView keyUserIdRest;
Button selectKeyButton;
CheckBox asciiArmorCheckBox;
Button saveButton;
Button revokeButton;
AppSettingsFragment settingsFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pm = getApplicationContext().getPackageManager();
// pm = getApplicationContext().getPackageManager();
// BEGIN_INCLUDE (inflate_set_custom_view)
// Inflate a "Done" custom action bar view to serve as the "Up" affordance.
@ -84,20 +64,8 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
setContentView(R.layout.api_app_settings_activity);
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();
}
});
settingsFragment = (AppSettingsFragment) getSupportFragmentManager().findFragmentById(
R.id.api_app_settings_fragment);
Intent intent = getIntent();
mAppUri = intent.getData();
@ -136,30 +104,17 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
if (cur.moveToFirst()) {
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(mPackageName, 0);
appName = (String) pm.getApplicationLabel(ai);
appIcon = pm.getApplicationIcon(ai);
} catch (final NameNotFoundException e) {
// fallback
appName = mPackageName;
}
appNameView.setText(appName);
appIconView.setImageDrawable(appIcon);
settingsFragment.setPackage(mPackageName);
try {
mSecretKeyId = (cur.getLong(cur
long secretKeyId = (cur.getLong(cur
.getColumnIndexOrThrow(KeychainContract.ApiApps.KEY_ID)));
Log.d(Constants.TAG, "mSecretKeyId: " + mSecretKeyId);
updateSelectedKeyView(mSecretKeyId);
Log.d(Constants.TAG, "mSecretKeyId: " + secretKeyId);
settingsFragment.setSecretKey(secretKeyId);
boolean asciiArmor = (cur.getInt(cur
.getColumnIndexOrThrow(KeychainContract.ApiApps.ASCII_ARMOR)) == 1);
asciiArmorCheckBox.setChecked(asciiArmor);
settingsFragment.setAsciiArmor(asciiArmor);
} catch (IllegalArgumentException e) {
Log.e(Constants.TAG, "AppSettingsActivity", e);
@ -176,9 +131,9 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
private void save() {
final ContentValues cv = new ContentValues();
cv.put(KeychainContract.ApiApps.KEY_ID, mSecretKeyId);
cv.put(KeychainContract.ApiApps.KEY_ID, settingsFragment.getSecretKeyId());
cv.put(KeychainContract.ApiApps.ASCII_ARMOR, asciiArmorCheckBox.isChecked());
cv.put(KeychainContract.ApiApps.ASCII_ARMOR, settingsFragment.isAsciiArmor());
// TODO: other parameters
if (getContentResolver().update(mAppUri, cv, null, null) <= 0) {
@ -188,57 +143,4 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
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

@ -1,10 +1,21 @@
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.ProviderHelper;
import org.sufficientlysecure.keychain.ui.SelectSecretKeyActivity;
import org.sufficientlysecure.keychain.util.Log;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
@ -14,29 +25,72 @@ import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AppSettingsFragment extends Fragment {
private LinearLayout advancedSettingsContainer;
private Button advancedSettingsButton;
// model
private long mSecretKeyId = Id.key.none;
// view
private LinearLayout mAdvancedSettingsContainer;
private Button mAdvancedSettingsButton;
private TextView mAppNameView;
private ImageView mAppIconView;
private TextView mKeyUserId;
private TextView mKeyUserIdRest;
private Button mSelectKeyButton;
private CheckBox mAsciiArmorCheckBox;
public void setSecretKey(long keyId) {
mSecretKeyId = keyId;
updateSelectedKeyView(keyId);
}
public long getSecretKeyId() {
return mSecretKeyId;
}
public void setAsciiArmor(boolean value) {
mAsciiArmorCheckBox.setChecked(value);
}
public boolean isAsciiArmor() {
return mAsciiArmorCheckBox.isChecked();
}
/**
* Inflate the layout for this fragment
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.api_app_settings_fragment, container, false);
View view = inflater.inflate(R.layout.api_app_settings_fragment, container, false);
initView(view);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
private void initView(View view) {
mAdvancedSettingsButton = (Button) view.findViewById(R.id.api_app_settings_advanced_button);
mAdvancedSettingsContainer = (LinearLayout) view
.findViewById(R.id.api_app_settings_advanced);
advancedSettingsButton = (Button) getActivity().findViewById(
R.id.api_app_settings_advanced_button);
advancedSettingsContainer = (LinearLayout) getActivity().findViewById(
R.id.api_app_settings_advanced);
mAppNameView = (TextView) view.findViewById(R.id.api_app_settings_app_name);
mAppIconView = (ImageView) view.findViewById(R.id.api_app_settings_app_icon);
mKeyUserId = (TextView) view.findViewById(R.id.api_app_settings_user_id);
mKeyUserIdRest = (TextView) view.findViewById(R.id.api_app_settings_user_id_rest);
mSelectKeyButton = (Button) view.findViewById(R.id.api_app_settings_select_key_button);
mAsciiArmorCheckBox = (CheckBox) view.findViewById(R.id.api_app_ascii_armor);
mSelectKeyButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectSecretKey();
}
});
final Animation visibleAnimation = new AlphaAnimation(0.0f, 1.0f);
visibleAnimation.setDuration(250);
@ -49,21 +103,102 @@ public class AppSettingsFragment extends Fragment {
// Animation.RELATIVE_TO_SELF, 0.0f);
// animation2.setDuration(150);
advancedSettingsButton.setOnClickListener(new OnClickListener() {
mAdvancedSettingsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (advancedSettingsContainer.getVisibility() == View.VISIBLE) {
advancedSettingsContainer.startAnimation(invisibleAnimation);
advancedSettingsContainer.setVisibility(View.INVISIBLE);
advancedSettingsButton.setText(R.string.api_settings_show_advanced);
if (mAdvancedSettingsContainer.getVisibility() == View.VISIBLE) {
mAdvancedSettingsContainer.startAnimation(invisibleAnimation);
mAdvancedSettingsContainer.setVisibility(View.INVISIBLE);
mAdvancedSettingsButton.setText(R.string.api_settings_show_advanced);
} else {
advancedSettingsContainer.startAnimation(visibleAnimation);
advancedSettingsContainer.setVisibility(View.VISIBLE);
advancedSettingsButton.setText(R.string.api_settings_hide_advanced);
mAdvancedSettingsContainer.startAnimation(visibleAnimation);
mAdvancedSettingsContainer.setVisibility(View.VISIBLE);
mAdvancedSettingsButton.setText(R.string.api_settings_hide_advanced);
}
}
});
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void selectSecretKey() {
Intent intent = new Intent(getActivity(), SelectSecretKeyActivity.class);
startActivityForResult(intent, Id.request.secret_keys);
}
public void setPackage(String packageName) {
PackageManager pm = getActivity().getApplicationContext().getPackageManager();
// get application name and icon from package manager
String appName = null;
Drawable appIcon = null;
try {
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
appName = (String) pm.getApplicationLabel(ai);
appIcon = pm.getApplicationIcon(ai);
} catch (final NameNotFoundException e) {
// fallback
appName = packageName;
}
mAppNameView.setText(appName);
mAppIconView.setImageDrawable(appIcon);
}
private void updateSelectedKeyView(long secretKeyId) {
if (secretKeyId == Id.key.none) {
mKeyUserId.setText(R.string.api_settings_no_key);
mKeyUserIdRest.setText("");
} else {
String uid = getResources().getString(R.string.unknownUserId);
String uidExtra = "";
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(
getActivity(), secretKeyId);
if (keyRing != null) {
PGPSecretKey key = PgpHelper.getMasterKey(keyRing);
if (key != null) {
String userId = PgpHelper.getMainUserIdSafe(getActivity(), key);
String chunks[] = userId.split(" <", 2);
uid = chunks[0];
if (chunks.length > 1) {
uidExtra = "<" + chunks[1];
}
}
}
mKeyUserId.setText(uid);
mKeyUserIdRest.setText(uidExtra);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(Constants.TAG, "onactivityresult " + requestCode + " " + resultCode);
switch (requestCode) {
case Id.request.secret_keys: {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
mSecretKeyId = bundle.getLong(SelectSecretKeyActivity.RESULT_EXTRA_MASTER_KEY_ID);
Log.d(Constants.TAG, "jo " + mSecretKeyId);
} else {
mSecretKeyId = Id.key.none;
}
updateSelectedKeyView(mSecretKeyId);
break;
}
default: {
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}

View File

@ -16,8 +16,6 @@
package org.sufficientlysecure.keychain.remote_api;
import java.util.ArrayList;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.remote_api.IServiceActivityCallback;
@ -26,6 +24,7 @@ import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment;
import org.sufficientlysecure.keychain.util.Log;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.content.ComponentName;
@ -38,10 +37,10 @@ import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.view.ViewGroup;
import android.widget.TextView;
public class ServiceActivity extends SherlockFragmentActivity {
@ -133,8 +132,63 @@ public class ServiceActivity extends SherlockFragmentActivity {
if (ACTION_REGISTER.equals(action)) {
final String packageName = extras.getString(EXTRA_PACKAGE_NAME);
// BEGIN_INCLUDE (inflate_set_custom_view)
// Inflate a "Done"/"Cancel" custom action bar view
final LayoutInflater inflater = (LayoutInflater) getSupportActionBar()
.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_done_cancel, null);
((TextView) customActionBarView.findViewById(R.id.actionbar_done_text))
.setText(R.string.api_register_allow);
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// Allow
ProviderHelper.addCryptoConsumer(ServiceActivity.this, packageName);
// Intent data = new Intent();
try {
mServiceCallback.onRegistered(true, packageName);
} catch (RemoteException e) {
Log.e(Constants.TAG, "ServiceActivity");
}
finish();
}
});
((TextView) customActionBarView.findViewById(R.id.actionbar_cancel_text))
.setText(R.string.api_register_disallow);
customActionBarView.findViewById(R.id.actionbar_cancel).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// Disallow
try {
mServiceCallback.onRegistered(false, packageName);
} catch (RemoteException e) {
Log.e(Constants.TAG, "ServiceActivity");
}
finish();
}
});
// 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, new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// END_INCLUDE (inflate_set_custom_view)
setContentView(R.layout.api_app_register_activity);
AppSettingsFragment settingsFragment = (AppSettingsFragment) getSupportFragmentManager()
.findFragmentById(R.id.api_app_settings_fragment);
settingsFragment.setPackage(packageName);
// TODO: handle if app is already registered
// LinearLayout layoutRegister = (LinearLayout)
// findViewById(R.id.register_crypto_consumer_register_layout);
@ -152,37 +206,6 @@ public class ServiceActivity extends SherlockFragmentActivity {
// layoutEdit.setVisibility(View.GONE);
// }
Button allowButton = (Button) findViewById(R.id.api_register_allow);
Button disallowButton = (Button) findViewById(R.id.api_register_disallow);
allowButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ProviderHelper.addCryptoConsumer(ServiceActivity.this, packageName);
// Intent data = new Intent();
try {
mServiceCallback.onRegistered(true, packageName);
} catch (RemoteException e) {
Log.e(Constants.TAG, "ServiceActivity");
}
finish();
}
});
disallowButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
mServiceCallback.onRegistered(false, packageName);
} catch (RemoteException e) {
Log.e(Constants.TAG, "ServiceActivity");
}
finish();
}
});
} else if (ACTION_CACHE_PASSPHRASE.equals(action)) {
long secretKeyId = extras.getLong(EXTRA_SECRET_KEY_ID);