diff options
author | Dominik Schürmann <dominik@dominikschuermann.de> | 2013-09-06 09:44:56 +0200 |
---|---|---|
committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2013-09-06 09:44:56 +0200 |
commit | c9549befb250826912d153d40c459fc495aa7fb5 (patch) | |
tree | 246d7a3f9b33b580d57cbbe7b318a116c19ec696 /OpenPGP-Keychain/src/org/sufficientlysecure | |
parent | 4030739a99a2e1ce232c6c3ab92ab21ad5bc3b4d (diff) | |
download | open-keychain-c9549befb250826912d153d40c459fc495aa7fb5.tar.gz open-keychain-c9549befb250826912d153d40c459fc495aa7fb5.tar.bz2 open-keychain-c9549befb250826912d153d40c459fc495aa7fb5.zip |
Put logic into fragment for reuse
Diffstat (limited to 'OpenPGP-Keychain/src/org/sufficientlysecure')
3 files changed, 224 insertions, 164 deletions
diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsActivity.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsActivity.java index 44212b5b3..4e069f3b0 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsActivity.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsActivity.java @@ -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); - } - } - } diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsFragment.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsFragment.java index 46cea389a..69d4e68ea 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsFragment.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsFragment.java @@ -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); + + 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() { - advancedSettingsButton = (Button) getActivity().findViewById( - R.id.api_app_settings_advanced_button); - advancedSettingsContainer = (LinearLayout) getActivity().findViewById( - R.id.api_app_settings_advanced); + @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); + } + } diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/ServiceActivity.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/ServiceActivity.java index 00bba7757..5f36075f5 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/ServiceActivity.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/ServiceActivity.java @@ -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); |