diff options
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui')
5 files changed, 568 insertions, 276 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index f78c19d13..b41ca58b4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -20,13 +20,13 @@ package org.sufficientlysecure.keychain.ui; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; -import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.intents.OpenKeychainIntents; @@ -43,8 +43,8 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache; import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; -import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.io.IOException; import java.net.Proxy; @@ -353,18 +353,33 @@ public class ImportKeysActivity extends BaseNfcActivity } } - public void loadCallback(ImportKeysListFragment.LoaderState loaderState) { + public void loadCallback(final ImportKeysListFragment.LoaderState loaderState) { if (loaderState instanceof ImportKeysListFragment.CloudLoaderState) { // do the tor check - OrbotHelper helper = new OrbotHelper(this); - // TODO: Add callbacks by modifying OrbotHelper so we know if the user wants to not use Tor + // this handle will set tor to be ignored whenever a message is received + Handler ignoreTorHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + // disables Tor until Activity is recreated + mProxyPrefs = new Preferences.ProxyPrefs(false, false, null, -1, null); + mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); + } + }; + + if(mProxyPrefs.torEnabled && !OrbotHelper.isOrbotInstalled(this)) { + + OrbotHelper.getInstallDialogFragmentWithThirdButton( + new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor + ).show(getSupportFragmentManager(), "orbotInstallDialog"); - if(mProxyPrefs.torEnabled && !helper.isOrbotInstalled()) { - helper.promptToInstall(this); return; } - if(mProxyPrefs.torEnabled && !helper.isOrbotRunning()) { - helper.requestOrbotStart(this); + + if(mProxyPrefs.torEnabled && !OrbotHelper.isOrbotRunning()) { + OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor) + .show(getSupportFragmentManager(), "orbotStartDialog"); return; } } @@ -372,13 +387,6 @@ public class ImportKeysActivity extends BaseNfcActivity mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy); } - /** - * disables use of Tor as proxy for this session - */ - private void disableTorForSession() { - mProxyPrefs = new Preferences.ProxyPrefs(false, false, null, -1, null); - } - private void handleMessage(Message message) { if (message.arg1 == ServiceProgressHandler.MessageStatus.OKAY.ordinal()) { // get returned data bundle diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 17df91ba6..6605995eb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -18,6 +18,7 @@ package org.sufficientlysecure.keychain.ui; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; @@ -34,7 +35,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; -import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity; @@ -42,6 +42,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; import java.util.List; @@ -49,6 +50,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { public static final String ACTION_PREFS_CLOUD = "org.sufficientlysecure.keychain.ui.PREFS_CLOUD"; public static final String ACTION_PREFS_ADV = "org.sufficientlysecure.keychain.ui.PREFS_ADV"; + public static final String ACTION_PREFS_PROXY = "org.sufficientlysecure.keychain.ui.PREFS_PROXY"; public static final int REQUEST_CODE_KEYSERVER_PREF = 0x00007005; @@ -216,305 +218,202 @@ public class SettingsActivity extends AppCompatPreferenceActivity { } public static class ProxyPrefsFragment extends PreferenceFragment { - private CheckBoxPreference mUseTor; - private CheckBoxPreference mUseNormalProxy; - private EditTextPreference mProxyHost; - private EditTextPreference mProxyPort; - private ListPreference mProxyType; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // makes android's preference framework write to our file instead of default - // This allows us to use the "persistent" attribute to simplify code - sPreferences.setPreferenceManagerFileAndMode(getPreferenceManager()); - // Load the preferences from an XML resource - addPreferencesFromResource(R.xml.proxy_prefs); + new Initializer(this).initialize(); - mUseTor = (CheckBoxPreference) findPreference(Constants.Pref.USE_TOR_PROXY); - mUseNormalProxy = (CheckBoxPreference) findPreference(Constants.Pref.USE_NORMAL_PROXY); - mProxyHost = (EditTextPreference) findPreference(Constants.Pref.PROXY_HOST); - mProxyPort = (EditTextPreference) findPreference(Constants.Pref.PROXY_PORT); - mProxyType = (ListPreference) findPreference(Constants.Pref.PROXY_TYPE); + } - initializeUseTorPref(); - initializeUseNormalProxyPref(); - initializeEditTextPreferences(); - initializeProxyTypePreference(); + public static class Initializer { + private CheckBoxPreference mUseTor; + private CheckBoxPreference mUseNormalProxy; + private EditTextPreference mProxyHost; + private EditTextPreference mProxyPort; + private ListPreference mProxyType; + private PreferenceActivity mActivity; + private PreferenceFragment mFragment; + + public Initializer(PreferenceFragment fragment) { + mFragment = fragment; + } - if (mUseTor.isChecked()) disableNormalProxyPrefs(); - else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); - } + public Initializer(PreferenceActivity activity) { + mActivity = activity; + } - private void initializeUseTorPref() { - mUseTor.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if ((Boolean)newValue) { - OrbotHelper orbotHelper = new OrbotHelper(ProxyPrefsFragment.this.getActivity()); - boolean installed = orbotHelper.isOrbotInstalled(); - if (!installed) { - Log.d(Constants.TAG, "Prompting to install Tor"); - orbotHelper.promptToInstall(ProxyPrefsFragment.this.getActivity()); - // don't let the user check the box until he's installed orbot - return false; - } else { - disableNormalProxyPrefs(); - // let the enable tor box be checked - return true; - } - } - else { - // we're unchecking Tor, so enable other proxy - enableNormalProxyPrefs(); - return true; - } + public Preference automaticallyFindPreference(String key) { + if(mFragment != null) { + return mFragment.findPreference(key); + } else { + return mActivity.findPreference(key); } - }); - } + } - private void initializeUseNormalProxyPref() { - mUseNormalProxy.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if ((Boolean) newValue) { - disableUseTorPrefs(); - } else { - enableUseTorPrefs(); - } - return true; + public void initialize() { + // makes android's preference framework write to our file instead of default + // This allows us to use the "persistent" attribute to simplify code + if (mFragment != null) { + Preferences.setPreferenceManagerFileAndMode(mFragment.getPreferenceManager()); + // Load the preferences from an XML resource + mFragment.addPreferencesFromResource(R.xml.proxy_prefs); + } + else { + Preferences.setPreferenceManagerFileAndMode(mActivity.getPreferenceManager()); + // Load the preferences from an XML resource + mActivity.addPreferencesFromResource(R.xml.proxy_prefs); } - }); - } - private void initializeEditTextPreferences() { - mProxyHost.setSummary(mProxyHost.getText()); - mProxyPort.setSummary(mProxyPort.getText()); - - mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (TextUtils.isEmpty((String) newValue)) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_host_err_invalid, - Notify.Style.ERROR - ).show(); - return false; - } else { - mProxyHost.setSummary((CharSequence) newValue); - return true; + mUseTor = (CheckBoxPreference) automaticallyFindPreference(Constants.Pref.USE_TOR_PROXY); + mUseNormalProxy = (CheckBoxPreference) automaticallyFindPreference(Constants.Pref.USE_NORMAL_PROXY); + mProxyHost = (EditTextPreference) automaticallyFindPreference(Constants.Pref.PROXY_HOST); + mProxyPort = (EditTextPreference) automaticallyFindPreference(Constants.Pref.PROXY_PORT); + mProxyType = (ListPreference) automaticallyFindPreference(Constants.Pref.PROXY_TYPE); + initializeUseTorPref(); + initializeUseNormalProxyPref(); + initializeEditTextPreferences(); + initializeProxyTypePreference(); + + if (mUseTor.isChecked()) disableNormalProxyPrefs(); + else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); + } + + private void initializeUseTorPref() { + mUseTor.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Activity activity = mFragment != null ? mFragment.getActivity() : mActivity; + if ((Boolean)newValue) { + boolean installed = OrbotHelper.isOrbotInstalled(activity); + if (!installed) { + Log.d(Constants.TAG, "Prompting to install Tor"); + OrbotHelper.getPreferenceInstallDialogFragment().show(activity.getFragmentManager(), + "installDialog"); + // don't let the user check the box until he's installed orbot + return false; + } else { + disableNormalProxyPrefs(); + // let the enable tor box be checked + return true; + } + } + else { + // we're unchecking Tor, so enable other proxy + enableNormalProxyPrefs(); + return true; + } } - } - }); - - mProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - try { - int port = Integer.parseInt((String) newValue); - if(port < 0 || port > 65535) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_port_err_invalid, - Notify.Style.ERROR - ).show(); - return false; + }); + } + + private void initializeUseNormalProxyPref() { + mUseNormalProxy.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if ((Boolean) newValue) { + disableUseTorPrefs(); + } else { + enableUseTorPrefs(); } - // no issues, save port - mProxyPort.setSummary("" + port); return true; - } catch (NumberFormatException e) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_port_err_invalid, - Notify.Style.ERROR - ).show(); - return false; } - } - }); - } - - private void initializeProxyTypePreference() { - mProxyType.setSummary(mProxyType.getEntry()); - - mProxyType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - CharSequence entry = mProxyType.getEntries()[mProxyType.findIndexOfValue((String) newValue)]; - mProxyType.setSummary(entry); - return true; - } - }); - } - - private void disableNormalProxyPrefs() { - mUseNormalProxy.setChecked(false); - mUseNormalProxy.setEnabled(false); - mProxyHost.setEnabled(false); - mProxyPort.setEnabled(false); - mProxyType.setEnabled(false); - } - - private void enableNormalProxyPrefs() { - mUseNormalProxy.setEnabled(true); - mProxyHost.setEnabled(true); - mProxyPort.setEnabled(true); - mProxyType.setEnabled(true); - } - - private void disableUseTorPrefs() { - mUseTor.setChecked(false); - mUseTor.setEnabled(false); - } - - private void enableUseTorPrefs() { - mUseTor.setEnabled(true); - } - } - - @TargetApi(Build.VERSION_CODES.KITKAT) - public static class ProxyPrefsFragment extends PreferenceFragment { - private CheckBoxPreference mUseTor; - private CheckBoxPreference mUseNormalProxy; - private EditTextPreference mProxyHost; - private EditTextPreference mProxyPort; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Load the preferences from an XML resource - addPreferencesFromResource(R.xml.proxy_prefs); - - mUseTor = (CheckBoxPreference) findPreference(Constants.Pref.USE_TOR_PROXY); - mUseNormalProxy = (CheckBoxPreference) findPreference(Constants.Pref.USE_NORMAL_PROXY); - mProxyHost = (EditTextPreference) findPreference(Constants.Pref.PROXY_HOST); - mProxyPort = (EditTextPreference) findPreference(Constants.Pref.PROXY_PORT); - - initializeUseTorPref(); - initializeUseNormalProxyPref(); - initialiseEditTextPreferences(); + }); + } - if (mUseTor.isChecked()) disableNormalProxyPrefs(); - else if (mUseNormalProxy.isChecked()) disableUseTorPrefs(); - } + private void initializeEditTextPreferences() { + mProxyHost.setSummary(mProxyHost.getText()); + mProxyPort.setSummary(mProxyPort.getText()); - private void initializeUseTorPref() { - mUseTor.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if ((Boolean)newValue) { - OrbotHelper orbotHelper = new OrbotHelper(ProxyPrefsFragment.this.getActivity()); - boolean installed = orbotHelper.isOrbotInstalled(); - if (!installed) { - Log.d(Constants.TAG, "Prompting to install Tor"); - orbotHelper.promptToInstall(ProxyPrefsFragment.this.getActivity()); - // don't let the user check the box until he's installed orbot + mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Activity activity = mFragment != null ? mFragment.getActivity() : mActivity; + if (TextUtils.isEmpty((String) newValue)) { + Notify.create( + activity, + R.string.pref_proxy_host_err_invalid, + Notify.Style.ERROR + ).show(); return false; } else { - disableNormalProxyPrefs(); - // let the enable tor box be checked + mProxyHost.setSummary((CharSequence) newValue); return true; } } - else { - // we're unchecking Tor, so enable other proxy - enableNormalProxyPrefs(); - return true; - } - } - }); - } - - private void initializeUseNormalProxyPref() { - mUseNormalProxy.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if ((Boolean) newValue) { - disableUseTorPrefs(); - } else { - enableUseTorPrefs(); - } - return true; - } - }); - } + }); - private void initialiseEditTextPreferences() { - mProxyHost.setSummary(mProxyHost.getText()); - mProxyPort.setSummary(mProxyPort.getText()); - - mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (newValue.equals("")) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_host_err_invalid, - Notify.Style.ERROR - ).show(); - return false; - } else { - mProxyHost.setSummary((CharSequence) newValue); - return true; - } - } - }); - - mProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - try { - int port = Integer.parseInt((String) newValue); - if(port < 0 || port > 65535) { + mProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Activity activity = mFragment != null ? mFragment.getActivity() : mActivity; + try { + int port = Integer.parseInt((String) newValue); + if(port < 0 || port > 65535) { + Notify.create( + activity, + R.string.pref_proxy_port_err_invalid, + Notify.Style.ERROR + ).show(); + return false; + } + // no issues, save port + mProxyPort.setSummary("" + port); + return true; + } catch (NumberFormatException e) { Notify.create( - ProxyPrefsFragment.this.getActivity(), + activity, R.string.pref_proxy_port_err_invalid, Notify.Style.ERROR ).show(); return false; } - // no issues, save port - mProxyPort.setSummary("" + port); + } + }); + } + + private void initializeProxyTypePreference() { + mProxyType.setSummary(mProxyType.getEntry()); + + mProxyType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + CharSequence entry = mProxyType.getEntries()[mProxyType.findIndexOfValue((String) newValue)]; + mProxyType.setSummary(entry); return true; - } catch (NumberFormatException e) { - Notify.create( - ProxyPrefsFragment.this.getActivity(), - R.string.pref_proxy_port_err_invalid, - Notify.Style.ERROR - ).show(); - return false; } - } - }); - } + }); + } - private void disableNormalProxyPrefs() { - mUseNormalProxy.setChecked(false); - mUseNormalProxy.setEnabled(false); - mProxyHost.setEnabled(false); - mProxyPort.setEnabled(false); - } + private void disableNormalProxyPrefs() { + mUseNormalProxy.setChecked(false); + mUseNormalProxy.setEnabled(false); + mProxyHost.setEnabled(false); + mProxyPort.setEnabled(false); + mProxyType.setEnabled(false); + } - private void enableNormalProxyPrefs() { - mUseNormalProxy.setEnabled(true); - mProxyHost.setEnabled(true); - mProxyPort.setEnabled(true); - } + private void enableNormalProxyPrefs() { + mUseNormalProxy.setEnabled(true); + mProxyHost.setEnabled(true); + mProxyPort.setEnabled(true); + mProxyType.setEnabled(true); + } - private void disableUseTorPrefs() { - mUseTor.setChecked(false); - mUseTor.setEnabled(false); - } + private void disableUseTorPrefs() { + mUseTor.setChecked(false); + mUseTor.setEnabled(false); + } - private void enableUseTorPrefs() { - mUseTor.setEnabled(true); + private void enableUseTorPrefs() { + mUseTor.setEnabled(true); + } } + } + @TargetApi(Build.VERSION_CODES.KITKAT) protected boolean isValidFragment(String fragmentName) { return AdvancedPrefsFragment.class.getName().equals(fragmentName) || CloudSearchPrefsFragment.class.getName().equals(fragmentName) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java new file mode 100644 index 000000000..01ff4715d --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/InstallDialogFragment.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.app.Activity; +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.support.v4.app.DialogFragment; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; + +public class InstallDialogFragment extends DialogFragment { + private static final String ARG_MESSENGER = "messenger"; + private static final String ARG_TITLE = "title"; + private static final String ARG_MESSAGE = "message"; + private static final String ARG_MIDDLE_BUTTON = "middleButton"; + private static final String ARG_INSTALL_PATH = "installPath"; + private static final String ARG_USE_MIDDLE_BUTTON = "useMiddleButton"; + + public static final String PLAY_STORE_PATH = "market://search?q=pname:"; + + public static final int MESSAGE_MIDDLE_CLICKED = 1; + + /** + * Creates a dialog which prompts the user to install an application. Consists of two default buttons ("Install" + * and "Cancel") and an optional third button. Callbacks are provided only for the middle button, if set. + * + * @param messenger required only for callback from middle button if it has been set + * @param title + * @param message content of dialog + * @param packageToInstall package name of application to install + * @param middleButton if not null, adds a third button to the app with a call back + * @return The dialog to display + */ + public static InstallDialogFragment newInstance(Messenger messenger, int title, int message, + String packageToInstall, int middleButton, boolean useMiddleButton) { + InstallDialogFragment frag = new InstallDialogFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + + args.putInt(ARG_TITLE, title); + args.putInt(ARG_MESSAGE, message); + args.putInt(ARG_MIDDLE_BUTTON, middleButton); + args.putString(ARG_INSTALL_PATH, PLAY_STORE_PATH + packageToInstall); + args.putBoolean(ARG_USE_MIDDLE_BUTTON, useMiddleButton); + + frag.setArguments(args); + + return frag; + } + + /** + * To create a DialogFragment with only two buttons + * + * @param title + * @param message + * @param packageToInstall + * @return + */ + public static InstallDialogFragment newInstance(int title, int message, + String packageToInstall) { + return newInstance(null, title, message, packageToInstall, -1, false); + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + final Activity activity = getActivity(); + + final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER); + + final int title = getArguments().getInt(ARG_TITLE); + final int message = getArguments().getInt(ARG_MESSAGE); + final int middleButton = getArguments().getInt(ARG_MIDDLE_BUTTON); + final String installPath = getArguments().getString(ARG_INSTALL_PATH); + final boolean useMiddleButton = getArguments().getBoolean(ARG_USE_MIDDLE_BUTTON); + + CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(activity); + + builder.setTitle(title).setMessage(message); + + builder.setNegativeButton(R.string.orbot_install_dialog_cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + + builder.setPositiveButton(R.string.orbot_install_dialog_install, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Uri uri = Uri.parse(installPath); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + activity.startActivity(intent); + } + } + ); + + if (useMiddleButton) { + builder.setNeutralButton(middleButton, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Message msg = new Message(); + msg.what=MESSAGE_MIDDLE_CLICKED; + try { + messenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } + } + ); + } + + return builder.show(); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java new file mode 100644 index 000000000..e9092b14a --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.app.Dialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.support.v4.app.DialogFragment; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; + +/** + * displays a dialog asking the user to enable Tor + */ +public class OrbotStartDialogFragment extends DialogFragment { + private static final String ARG_MESSENGER = "messenger"; + private static final String ARG_TITLE = "title"; + private static final String ARG_MESSAGE = "message"; + private static final String ARG_MIDDLE_BUTTON = "middleButton"; + + public static final int MESSAGE_MIDDLE_BUTTON = 1; + + public static OrbotStartDialogFragment newInstance(Messenger messenger, int title, int message, int middleButton) { + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + args.putInt(ARG_TITLE, title); + args.putInt(ARG_MESSAGE, message); + args.putInt(ARG_MIDDLE_BUTTON, middleButton); + + OrbotStartDialogFragment fragment = new OrbotStartDialogFragment(); + fragment.setArguments(args); + + return fragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + + final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER); + int title = getArguments().getInt(ARG_TITLE); + final int message = getArguments().getInt(ARG_MESSAGE); + int middleButton = getArguments().getInt(ARG_MIDDLE_BUTTON); + + CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(getActivity()); + builder.setTitle(title).setMessage(message); + + builder.setNegativeButton(R.string.orbot_start_dialog_cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + + builder.setPositiveButton(R.string.orbot_start_dialog_start, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + getActivity().startActivityForResult(OrbotHelper.getOrbotStartIntent(), 1); + } + }); + + builder.setNeutralButton(middleButton, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Message msg = new Message(); + msg.what = MESSAGE_MIDDLE_BUTTON; + try { + messenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } + }); + + return builder.show(); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java new file mode 100644 index 000000000..8236ff9d7 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PreferenceInstallDialogFragment.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.app.Activity; +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.app.DialogFragment; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; + +public class PreferenceInstallDialogFragment extends DialogFragment { + private static final String ARG_MESSENGER = "messenger"; + private static final String ARG_TITLE = "title"; + private static final String ARG_MESSAGE = "message"; + private static final String ARG_MIDDLE_BUTTON = "middleButton"; + private static final String ARG_INSTALL_PATH = "installPath"; + private static final String ARG_USE_MIDDLE_BUTTON = "installPath"; + + public static final String PLAY_STORE_PATH = "market://search?q=pname:"; + + public static final int MESSAGE_MIDDLE_CLICKED = 1; + + /** + * Creates a dialog which prompts the user to install an application. Consists of two default buttons ("Install" + * and "Cancel") and an optional third button. Callbacks are provided only for the middle button, if set. + * + * @param messenger required only for callback from middle button if it has been set + * @param title + * @param message content of dialog + * @param packageToInstall package name of application to install + * @param middleButton if not null, adds a third button to the app with a call back + * @return The dialog to display + */ + public static PreferenceInstallDialogFragment newInstance(Messenger messenger, int title, int message, + String packageToInstall, int middleButton, boolean useMiddleButton) { + PreferenceInstallDialogFragment frag = new PreferenceInstallDialogFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + + args.putInt(ARG_TITLE, title); + args.putInt(ARG_MESSAGE, message); + args.putInt(ARG_MIDDLE_BUTTON, middleButton); + args.putString(ARG_INSTALL_PATH, PLAY_STORE_PATH + packageToInstall); + args.putBoolean(ARG_USE_MIDDLE_BUTTON, useMiddleButton); + + frag.setArguments(args); + + return frag; + } + + /** + * To create a DialogFragment with only two buttons + * + * @param title + * @param message + * @param packageToInstall + * @return + */ + public static PreferenceInstallDialogFragment newInstance(int title, int message, + String packageToInstall) { + return newInstance(null, title, message, packageToInstall, -1, false); + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + final Activity activity = getActivity(); + + final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER); + + final String title = getArguments().getString(ARG_TITLE); + final String message = getArguments().getString(ARG_MESSAGE); + final String installPath = getArguments().getString(ARG_INSTALL_PATH); + final String middleButton = getArguments().getString(ARG_MIDDLE_BUTTON); + final boolean useMiddleButton = getArguments().getBoolean(ARG_USE_MIDDLE_BUTTON); + + CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(activity); + + builder.setTitle(title).setMessage(message); + + builder.setNegativeButton(R.string.orbot_install_dialog_cancel, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + + builder.setPositiveButton(R.string.orbot_install_dialog_install, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Uri uri = Uri.parse(installPath); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + activity.startActivity(intent); + } + } + ); + + if (useMiddleButton) { + builder.setNeutralButton(middleButton, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Message msg = new Message(); + msg.what=MESSAGE_MIDDLE_CLICKED; + try { + messenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } + } + ); + } + + return builder.show(); + } +} |