2012-07-19 19:23:49 -07:00
|
|
|
/**
|
2011-12-20 10:20:44 -08:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-19 19:23:49 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* 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.
|
2012-07-19 19:23:49 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* 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.thoughtcrime.securesms;
|
|
|
|
|
2013-01-09 21:06:56 -08:00
|
|
|
import android.app.AlertDialog;
|
2013-07-11 14:58:40 -07:00
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.Context;
|
2013-01-09 21:06:56 -08:00
|
|
|
import android.content.DialogInterface;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2013-07-11 14:58:40 -07:00
|
|
|
import android.os.AsyncTask;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.os.Bundle;
|
2013-07-01 10:15:36 -07:00
|
|
|
import android.preference.CheckBoxPreference;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.preference.Preference;
|
2013-06-29 18:03:55 -07:00
|
|
|
import android.preference.PreferenceScreen;
|
2014-06-28 20:40:57 -07:00
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.FragmentManager;
|
|
|
|
import android.support.v4.app.FragmentTransaction;
|
|
|
|
import android.support.v4.preference.PreferenceFragment;
|
2013-01-09 21:06:56 -08:00
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2014-07-23 15:40:45 -07:00
|
|
|
import com.google.android.gms.gcm.GoogleCloudMessaging;
|
2013-11-27 11:08:58 -08:00
|
|
|
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2014-11-04 15:19:48 +01:00
|
|
|
import org.thoughtcrime.securesms.preferences.AdvancedPreferenceFragment;
|
|
|
|
import org.thoughtcrime.securesms.preferences.AppProtectionPreferenceFragment;
|
|
|
|
import org.thoughtcrime.securesms.preferences.AppearancePreferenceFragment;
|
|
|
|
import org.thoughtcrime.securesms.preferences.NotificationsPreferenceFragment;
|
|
|
|
import org.thoughtcrime.securesms.preferences.SmsMmsPreferenceFragment;
|
|
|
|
import org.thoughtcrime.securesms.preferences.StoragePreferenceFragment;
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
|
2014-03-01 01:32:00 +01:00
|
|
|
import org.thoughtcrime.securesms.util.Dialogs;
|
2013-06-29 18:03:55 -07:00
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
2013-06-21 11:56:59 -07:00
|
|
|
import org.thoughtcrime.securesms.util.DynamicTheme;
|
2012-07-19 19:23:49 -07:00
|
|
|
import org.thoughtcrime.securesms.util.MemoryCleaner;
|
2014-11-04 15:19:48 +01:00
|
|
|
import org.thoughtcrime.securesms.util.ProgressDialogAsyncTask;
|
2013-07-11 14:58:40 -07:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
|
|
|
import org.whispersystems.textsecure.api.TextSecureAccountManager;
|
2014-11-12 11:35:54 -08:00
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.AuthorizationFailedException;
|
2013-07-11 14:58:40 -07:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2012-10-22 19:17:08 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
/**
|
|
|
|
* The Activity for application preference display and management.
|
2012-07-19 19:23:49 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-06-28 20:40:57 -07:00
|
|
|
public class ApplicationPreferencesActivity extends PassphraseRequiredActionBarActivity
|
2013-06-21 11:56:59 -07:00
|
|
|
implements SharedPreferences.OnSharedPreferenceChangeListener
|
|
|
|
{
|
2014-11-04 15:19:48 +01:00
|
|
|
private static final String TAG = ApplicationPreferencesActivity.class.getSimpleName();
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
private static final String PREFERENCE_CATEGORY_SMS_MMS = "preference_category_sms_mms";
|
|
|
|
private static final String PREFERENCE_CATEGORY_NOTIFICATIONS = "preference_category_notifications";
|
|
|
|
private static final String PREFERENCE_CATEGORY_APP_PROTECTION = "preference_category_app_protection";
|
|
|
|
private static final String PREFERENCE_CATEGORY_APPEARANCE = "preference_category_appearance";
|
|
|
|
private static final String PREFERENCE_CATEGORY_STORAGE = "preference_category_storage";
|
|
|
|
private static final String PREFERENCE_CATEGORY_ADVANCED = "preference_category_advanced";
|
2012-07-19 19:23:49 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
private static final String PUSH_MESSAGING_PREF = "pref_toggle_push_messaging";
|
2013-03-25 21:26:03 -07:00
|
|
|
|
2013-06-29 18:03:55 -07:00
|
|
|
private final DynamicTheme dynamicTheme = new DynamicTheme();
|
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
2013-06-21 11:56:59 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle icicle) {
|
2013-06-21 11:56:59 -07:00
|
|
|
dynamicTheme.onCreate(this);
|
2013-06-29 18:03:55 -07:00
|
|
|
dynamicLanguage.onCreate(this);
|
2011-12-20 10:20:44 -08:00
|
|
|
super.onCreate(icicle);
|
2012-08-02 20:23:41 -07:00
|
|
|
|
|
|
|
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
2014-06-28 20:40:57 -07:00
|
|
|
Fragment fragment = new ApplicationPreferenceFragment();
|
|
|
|
FragmentManager fragmentManager = getSupportFragmentManager();
|
|
|
|
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
|
|
|
fragmentTransaction.replace(android.R.id.content, fragment);
|
|
|
|
fragmentTransaction.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicTheme.onResume(this);
|
|
|
|
dynamicLanguage.onResume(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-22 13:31:54 +01:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data)
|
|
|
|
{
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
Fragment fragment = getSupportFragmentManager().findFragmentById(android.R.id.content);
|
|
|
|
fragment.onActivityResult(requestCode, resultCode, data);
|
|
|
|
}
|
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
@Override
|
|
|
|
public boolean onSupportNavigateUp() {
|
|
|
|
FragmentManager fragmentManager = getSupportFragmentManager();
|
|
|
|
if (fragmentManager.getBackStackEntryCount() > 0) {
|
|
|
|
fragmentManager.popBackStack();
|
|
|
|
} else {
|
|
|
|
Intent intent = new Intent(this, ConversationListActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-22 13:31:54 +01:00
|
|
|
@Override
|
2014-06-28 20:40:57 -07:00
|
|
|
public void onDestroy() {
|
|
|
|
MemoryCleaner.clean((MasterSecret) getIntent().getParcelableExtra("master_secret"));
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
|
|
if (key.equals(TextSecurePreferences.THEME_PREF)) {
|
|
|
|
dynamicTheme.onResume(this);
|
|
|
|
} else if (key.equals(TextSecurePreferences.LANGUAGE_PREF)) {
|
|
|
|
dynamicLanguage.onResume(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class ApplicationPreferenceFragment extends PreferenceFragment {
|
2014-11-04 15:19:48 +01:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle icicle) {
|
|
|
|
super.onCreate(icicle);
|
|
|
|
|
|
|
|
addPreferencesFromResource(R.xml.preferences);
|
|
|
|
|
|
|
|
initializePushMessagingToggle();
|
|
|
|
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_SMS_MMS)
|
|
|
|
.setOnPreferenceClickListener(new CategoryClickListener(PREFERENCE_CATEGORY_SMS_MMS));
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_NOTIFICATIONS)
|
|
|
|
.setOnPreferenceClickListener(new CategoryClickListener(PREFERENCE_CATEGORY_NOTIFICATIONS));
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_APP_PROTECTION)
|
|
|
|
.setOnPreferenceClickListener(new CategoryClickListener(PREFERENCE_CATEGORY_APP_PROTECTION));
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_APPEARANCE)
|
|
|
|
.setOnPreferenceClickListener(new CategoryClickListener(PREFERENCE_CATEGORY_APPEARANCE));
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_STORAGE)
|
|
|
|
.setOnPreferenceClickListener(new CategoryClickListener(PREFERENCE_CATEGORY_STORAGE));
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_ADVANCED)
|
|
|
|
.setOnPreferenceClickListener(new CategoryClickListener(PREFERENCE_CATEGORY_ADVANCED));
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-07-19 19:23:49 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
((ApplicationPreferencesActivity) getActivity()).getSupportActionBar().setTitle(R.string.text_secure_normal__menu_settings);
|
|
|
|
setCategorySummaries();
|
2014-02-20 14:49:31 -08:00
|
|
|
}
|
2014-03-30 02:28:38 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
private void setCategorySummaries() {
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_SMS_MMS)
|
|
|
|
.setSummary(SmsMmsPreferenceFragment.getSummary(getActivity()));
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_NOTIFICATIONS)
|
|
|
|
.setSummary(NotificationsPreferenceFragment.getSummary(getActivity()));
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_APP_PROTECTION)
|
|
|
|
.setSummary(AppProtectionPreferenceFragment.getSummary(getActivity()));
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_APPEARANCE)
|
|
|
|
.setSummary(AppearancePreferenceFragment.getSummary(getActivity()));
|
|
|
|
this.findPreference(PREFERENCE_CATEGORY_STORAGE)
|
|
|
|
.setSummary(StoragePreferenceFragment.getSummary(getActivity()));
|
2014-03-30 02:28:38 -07:00
|
|
|
}
|
2014-02-20 14:49:31 -08:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
private class CategoryClickListener implements Preference.OnPreferenceClickListener {
|
|
|
|
private String category;
|
2013-07-11 14:58:40 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
public CategoryClickListener(String category) {
|
|
|
|
this.category = category;
|
2013-07-11 14:58:40 -07:00
|
|
|
}
|
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
Fragment fragment;
|
2013-07-11 14:58:40 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
switch (category) {
|
|
|
|
case PREFERENCE_CATEGORY_SMS_MMS:
|
|
|
|
fragment = new SmsMmsPreferenceFragment();
|
|
|
|
break;
|
|
|
|
case PREFERENCE_CATEGORY_NOTIFICATIONS:
|
|
|
|
fragment = new NotificationsPreferenceFragment();
|
|
|
|
break;
|
|
|
|
case PREFERENCE_CATEGORY_APP_PROTECTION:
|
|
|
|
fragment = new AppProtectionPreferenceFragment();
|
|
|
|
break;
|
|
|
|
case PREFERENCE_CATEGORY_APPEARANCE:
|
|
|
|
fragment = new AppearancePreferenceFragment();
|
|
|
|
break;
|
|
|
|
case PREFERENCE_CATEGORY_STORAGE:
|
|
|
|
fragment = new StoragePreferenceFragment();
|
|
|
|
break;
|
|
|
|
case PREFERENCE_CATEGORY_ADVANCED:
|
|
|
|
fragment = new AdvancedPreferenceFragment();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new AssertionError();
|
|
|
|
}
|
2012-10-22 19:17:08 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
|
|
|
|
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
|
|
|
fragmentTransaction.replace(android.R.id.content, fragment);
|
|
|
|
fragmentTransaction.addToBackStack(null);
|
|
|
|
fragmentTransaction.commit();
|
2012-10-22 19:17:08 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
return true;
|
2012-10-22 19:17:08 -07:00
|
|
|
}
|
|
|
|
}
|
2013-12-03 09:10:59 +02:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
private void initializePushMessagingToggle() {
|
|
|
|
CheckBoxPreference preference = (CheckBoxPreference)this.findPreference(PUSH_MESSAGING_PREF);
|
|
|
|
preference.setChecked(TextSecurePreferences.isPushRegistered(getActivity()));
|
|
|
|
preference.setOnPreferenceChangeListener(new PushMessagingClickListener());
|
2012-10-22 19:17:08 -07:00
|
|
|
}
|
2013-07-11 14:58:40 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
private class PushMessagingClickListener implements Preference.OnPreferenceChangeListener {
|
|
|
|
private static final int SUCCESS = 0;
|
|
|
|
private static final int NETWORK_ERROR = 1;
|
2013-07-11 14:58:40 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
private class DisablePushMessagesTask extends ProgressDialogAsyncTask<Void, Void, Integer> {
|
|
|
|
private final CheckBoxPreference checkBoxPreference;
|
2013-07-11 14:58:40 -07:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
public DisablePushMessagesTask(final CheckBoxPreference checkBoxPreference) {
|
|
|
|
super(getActivity(), R.string.ApplicationPreferencesActivity_unregistering, R.string.ApplicationPreferencesActivity_unregistering_for_data_based_communication);
|
|
|
|
this.checkBoxPreference = checkBoxPreference;
|
|
|
|
}
|
2014-02-27 15:24:44 +01:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
super.onPostExecute(result);
|
|
|
|
switch (result) {
|
2014-02-27 15:24:44 +01:00
|
|
|
case NETWORK_ERROR:
|
2014-06-28 20:40:57 -07:00
|
|
|
Toast.makeText(getActivity(),
|
2014-11-04 15:19:48 +01:00
|
|
|
R.string.ApplicationPreferencesActivity_error_connecting_to_server,
|
2014-02-27 15:24:44 +01:00
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case SUCCESS:
|
2014-11-04 15:19:48 +01:00
|
|
|
checkBoxPreference.setChecked(false);
|
2014-06-28 20:40:57 -07:00
|
|
|
TextSecurePreferences.setPushRegistered(getActivity(), false);
|
2014-02-27 15:24:44 +01:00
|
|
|
break;
|
2013-07-11 14:58:40 -07:00
|
|
|
}
|
2013-01-09 21:06:56 -08:00
|
|
|
}
|
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(Void... params) {
|
|
|
|
try {
|
|
|
|
Context context = getActivity();
|
|
|
|
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
|
|
|
|
|
|
|
|
accountManager.setGcmId(Optional.<String>absent());
|
|
|
|
GoogleCloudMessaging.getInstance(context).unregister();
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
} catch (AuthorizationFailedException afe) {
|
|
|
|
Log.w(TAG, afe);
|
|
|
|
return SUCCESS;
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, ioe);
|
|
|
|
return NETWORK_ERROR;
|
2013-07-01 10:15:36 -07:00
|
|
|
}
|
2013-12-03 09:10:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(final Preference preference, Object newValue) {
|
|
|
|
if (((CheckBoxPreference)preference).isChecked()) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
|
|
builder.setIcon(Dialogs.resolveIcon(getActivity(), R.attr.dialog_info_icon));
|
|
|
|
builder.setTitle(R.string.ApplicationPreferencesActivity_disable_push_messages);
|
|
|
|
builder.setMessage(R.string.ApplicationPreferencesActivity_this_will_disable_push_messages);
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null);
|
|
|
|
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
new DisablePushMessagesTask((CheckBoxPreference)preference).execute();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.show();
|
|
|
|
} else {
|
|
|
|
Intent nextIntent = new Intent(getActivity(), ApplicationPreferencesActivity.class);
|
|
|
|
nextIntent.putExtra("master_secret", getActivity().getIntent().getParcelableExtra("master_secret"));
|
|
|
|
|
|
|
|
Intent intent = new Intent(getActivity(), RegistrationActivity.class);
|
|
|
|
intent.putExtra("cancel_button", true);
|
|
|
|
intent.putExtra("next_intent", nextIntent);
|
|
|
|
intent.putExtra("master_secret", getActivity().getIntent().getParcelableExtra("master_secret"));
|
|
|
|
startActivity(intent);
|
2013-12-03 09:10:59 +02:00
|
|
|
}
|
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-25 23:11:00 -08:00
|
|
|
}
|
2014-03-01 14:17:55 -08:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
/* http://code.google.com/p/android/issues/detail?id=4611#c35 */
|
|
|
|
@SuppressWarnings("deprecation")
|
2014-03-01 14:17:55 -08:00
|
|
|
@Override
|
2014-11-04 15:19:48 +01:00
|
|
|
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
|
|
|
|
{
|
|
|
|
super.onPreferenceTreeClick(preferenceScreen, preference);
|
|
|
|
if (preference != null && preference instanceof PreferenceScreen && ((PreferenceScreen)preference).getDialog() != null)
|
|
|
|
((PreferenceScreen) preference).getDialog().getWindow().getDecorView().setBackgroundDrawable(getActivity().getWindow().getDecorView().getBackground().getConstantState().newDrawable());
|
2014-03-01 14:17:55 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|