2012-07-20 02:23:49 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-20 02:23:49 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00: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-20 02:23:49 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00: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;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2012-12-09 00:46:38 +00:00
|
|
|
import android.preference.EditTextPreference;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceManager;
|
2012-10-23 02:17:08 +00:00
|
|
|
import android.provider.ContactsContract;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2012-08-03 03:23:41 +00:00
|
|
|
import com.actionbarsherlock.app.SherlockPreferenceActivity;
|
|
|
|
import com.actionbarsherlock.view.MenuItem;
|
|
|
|
|
2012-07-20 02:23:49 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
2012-10-23 02:17:08 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactIdentityManager;
|
2012-07-20 02:23:49 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKey;
|
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
|
|
|
import org.thoughtcrime.securesms.util.Dialogs;
|
|
|
|
import org.thoughtcrime.securesms.util.MemoryCleaner;
|
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/**
|
|
|
|
* The Activity for application preference display and management.
|
2012-07-20 02:23:49 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-08-03 03:23:41 +00:00
|
|
|
public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
private static final int PICK_IDENTITY_CONTACT = 1;
|
|
|
|
private static final int IMPORT_IDENTITY_ID = 2;
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String WHITESPACE_PREF = "pref_key_tag_whitespace";
|
|
|
|
public static final String RINGTONE_PREF = "pref_key_ringtone";
|
|
|
|
public static final String VIBRATE_PREF = "pref_key_vibrate";
|
|
|
|
public static final String NOTIFICATION_PREF = "pref_key_enable_notifications";
|
|
|
|
public static final String LED_COLOR_PREF = "pref_led_color";
|
|
|
|
public static final String LED_BLINK_PREF = "pref_led_blink";
|
|
|
|
public static final String LED_BLINK_PREF_CUSTOM = "pref_led_blink_custom";
|
|
|
|
public static final String IDENTITY_PREF = "pref_choose_identity";
|
|
|
|
public static final String SEND_IDENTITY_PREF = "pref_send_identity_key";
|
|
|
|
public static final String ALL_MMS_PERF = "pref_all_mms";
|
|
|
|
public static final String PASSPHRASE_TIMEOUT_INTERVAL_PREF = "pref_timeout_interval";
|
|
|
|
public static final String PASSPHRASE_TIMEOUT_PREF = "pref_timeout_passphrase";
|
|
|
|
public static final String AUTO_KEY_EXCHANGE_PREF = "pref_auto_complete_key_exchange";
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
private static final String DISPLAY_CATEGORY_PREF = "pref_display_category";
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static final String VIEW_MY_IDENTITY_PREF = "pref_view_identity";
|
|
|
|
private static final String EXPORT_MY_IDENTITY_PREF = "pref_export_identity";
|
|
|
|
private static final String IMPORT_CONTACT_IDENTITY_PREF = "pref_import_identity";
|
|
|
|
private static final String MANAGE_IDENTITIES_PREF = "pref_manage_identity";
|
|
|
|
private static final String CHANGE_PASSPHRASE_PREF = "pref_change_passphrase";
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2012-12-09 00:46:38 +00:00
|
|
|
public static final String USE_LOCAL_MMS_APNS_PREF = "pref_use_local_apns";
|
|
|
|
public static final String MMSC_HOST_PREF = "pref_apn_mmsc_host";
|
2013-01-05 23:48:29 +00:00
|
|
|
public static final String MMSC_PROXY_HOST_PREF = "pref_apn_mms_proxy";
|
|
|
|
public static final String MMSC_PROXY_PORT_PREF = "pref_apn_mms_proxy_port";
|
2012-12-09 00:46:38 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle icicle) {
|
|
|
|
super.onCreate(icicle);
|
2012-08-03 03:23:41 +00:00
|
|
|
|
|
|
|
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
addPreferencesFromResource(R.xml.preferences);
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
initializeIdentitySelection();
|
2012-12-09 00:46:38 +00:00
|
|
|
initializeEditTextSummaries();
|
2012-10-23 02:17:08 +00:00
|
|
|
|
|
|
|
this.findPreference(VIEW_MY_IDENTITY_PREF)
|
|
|
|
.setOnPreferenceClickListener(new ViewMyIdentityClickListener());
|
|
|
|
this.findPreference(EXPORT_MY_IDENTITY_PREF)
|
|
|
|
.setOnPreferenceClickListener(new ExportMyIdentityClickListener());
|
|
|
|
this.findPreference(IMPORT_CONTACT_IDENTITY_PREF)
|
|
|
|
.setOnPreferenceClickListener(new ImportContactIdentityClickListener());
|
|
|
|
this.findPreference(MANAGE_IDENTITIES_PREF)
|
|
|
|
.setOnPreferenceClickListener(new ManageIdentitiesClickListener());
|
|
|
|
this.findPreference(CHANGE_PASSPHRASE_PREF)
|
|
|
|
.setOnPreferenceClickListener(new ChangePassphraseClickListener());
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
Intent intent = new Intent(this, KeyCachingService.class);
|
|
|
|
intent.setAction(KeyCachingService.ACTIVITY_START_EVENT);
|
|
|
|
startService(intent);
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
Intent intent = new Intent(this, KeyCachingService.class);
|
|
|
|
intent.setAction(KeyCachingService.ACTIVITY_STOP_EVENT);
|
|
|
|
startService(intent);
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int reqCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(reqCode, resultCode, data);
|
|
|
|
|
|
|
|
if (resultCode == Activity.RESULT_OK) {
|
|
|
|
switch (reqCode) {
|
|
|
|
case (PICK_IDENTITY_CONTACT) : handleIdentitySelection(data); break;
|
|
|
|
case IMPORT_IDENTITY_ID: importIdentityKey(data.getData()); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
MemoryCleaner.clean((MasterSecret)getIntent().getParcelableExtra("master_secret"));
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2012-08-03 03:23:41 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home: finish(); return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-05 23:48:29 +00:00
|
|
|
|
|
|
|
private void initializeEditTextSummary(final EditTextPreference preference) {
|
|
|
|
if (preference.getText() == null) {
|
|
|
|
preference.setSummary("Not set");
|
2012-12-09 00:46:38 +00:00
|
|
|
} else {
|
2013-01-05 23:48:29 +00:00
|
|
|
preference.setSummary(preference.getText());
|
2012-12-09 00:46:38 +00:00
|
|
|
}
|
2013-01-05 23:48:29 +00:00
|
|
|
|
|
|
|
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
2012-12-09 00:46:38 +00:00
|
|
|
@Override
|
2013-01-05 23:48:29 +00:00
|
|
|
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
|
|
|
preference.setSummary(newValue == null ? "Not set" : (String)newValue);
|
2012-12-09 00:46:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2013-01-05 23:48:29 +00:00
|
|
|
private void initializeEditTextSummaries() {
|
|
|
|
final EditTextPreference mmscUrlPreference = (EditTextPreference)this.findPreference(MMSC_HOST_PREF);
|
|
|
|
final EditTextPreference mmsProxyHostPreference = (EditTextPreference)this.findPreference(MMSC_PROXY_HOST_PREF);
|
|
|
|
final EditTextPreference mmsProxyPortPreference = (EditTextPreference)this.findPreference(MMSC_PROXY_PORT_PREF);
|
|
|
|
|
|
|
|
initializeEditTextSummary(mmscUrlPreference);
|
|
|
|
initializeEditTextSummary(mmsProxyHostPreference);
|
|
|
|
initializeEditTextSummary(mmsProxyPortPreference);
|
|
|
|
}
|
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
private void initializeIdentitySelection() {
|
|
|
|
ContactIdentityManager identity = ContactIdentityManager.getInstance(this);
|
|
|
|
|
|
|
|
if (identity.isSelfIdentityAutoDetected()) {
|
|
|
|
Preference preference = this.findPreference(DISPLAY_CATEGORY_PREF);
|
|
|
|
this.getPreferenceScreen().removePreference(preference);
|
|
|
|
} else {
|
|
|
|
Uri contactUri = identity.getSelfIdentityUri();
|
|
|
|
|
|
|
|
if (contactUri != null) {
|
|
|
|
String contactName = ContactAccessor.getInstance().getNameFromContact(this, contactUri);
|
|
|
|
this.findPreference(IDENTITY_PREF)
|
|
|
|
.setSummary(String.format(getString(R.string.ApplicationPreferencesActivity_currently_s),
|
|
|
|
contactName));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.findPreference(IDENTITY_PREF)
|
|
|
|
.setOnPreferenceClickListener(new IdentityPreferenceClickListener());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void handleIdentitySelection(Intent data) {
|
2012-10-23 02:17:08 +00:00
|
|
|
Uri contactUri = data.getData();
|
|
|
|
|
|
|
|
if (contactUri != null) {
|
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
String contactUriString = contactUri.toString();
|
|
|
|
|
|
|
|
preferences.edit().putString(IDENTITY_PREF, contactUriString).commit();
|
|
|
|
|
|
|
|
initializeIdentitySelection();
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void importIdentityKey(Uri uri) {
|
|
|
|
IdentityKey identityKey = ContactAccessor.getInstance().importIdentityKey(this, uri);
|
|
|
|
String contactName = ContactAccessor.getInstance().getNameFromContact(this, uri);
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (identityKey == null) {
|
2012-09-08 03:03:23 +00:00
|
|
|
Dialogs.displayAlert(this,
|
2012-09-20 02:56:04 +00:00
|
|
|
getString(R.string.ApplicationPreferenceActivity_not_found_exclamation),
|
|
|
|
getString(R.string.ApplicationPreferenceActivity_no_valid_identity_key_was_found_in_the_specified_contact),
|
2012-09-08 03:03:23 +00:00
|
|
|
android.R.drawable.ic_dialog_alert);
|
2011-12-20 18:20:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
Intent verifyImportedKeyIntent = new Intent(this, VerifyImportedIdentityActivity.class);
|
|
|
|
verifyImportedKeyIntent.putExtra("master_secret", getIntent().getParcelableExtra("master_secret"));
|
|
|
|
verifyImportedKeyIntent.putExtra("identity_key", identityKey);
|
|
|
|
verifyImportedKeyIntent.putExtra("contact_name", contactName);
|
|
|
|
startActivity(verifyImportedKeyIntent);
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class IdentityPreferenceClickListener implements Preference.OnPreferenceClickListener {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2012-10-23 02:17:08 +00:00
|
|
|
Intent intent = new Intent(Intent.ACTION_PICK);
|
|
|
|
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
|
2011-12-20 18:20:44 +00:00
|
|
|
startActivityForResult(intent, PICK_IDENTITY_CONTACT);
|
|
|
|
return true;
|
2012-07-20 02:23:49 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class ViewMyIdentityClickListener implements Preference.OnPreferenceClickListener {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
Intent viewIdentityIntent = new Intent(ApplicationPreferencesActivity.this, ViewIdentityActivity.class);
|
|
|
|
viewIdentityIntent.putExtra("identity_key", IdentityKeyUtil.getIdentityKey(ApplicationPreferencesActivity.this));
|
|
|
|
startActivity(viewIdentityIntent);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class ExportMyIdentityClickListener implements Preference.OnPreferenceClickListener {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
if (!IdentityKeyUtil.hasIdentityKey(ApplicationPreferencesActivity.this)) {
|
2012-09-08 03:03:23 +00:00
|
|
|
Toast.makeText(ApplicationPreferencesActivity.this,
|
2012-09-20 02:56:04 +00:00
|
|
|
R.string.ApplicationPreferenceActivity_you_don_t_have_an_identity_key_exclamation,
|
2012-09-08 03:03:23 +00:00
|
|
|
Toast.LENGTH_LONG).show();
|
2011-12-20 18:20:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
List<Long> rawContactIds = ContactIdentityManager
|
|
|
|
.getInstance(ApplicationPreferencesActivity.this)
|
|
|
|
.getSelfIdentityRawContactIds();
|
|
|
|
|
|
|
|
if (rawContactIds== null) {
|
2012-07-20 02:23:49 +00:00
|
|
|
Toast.makeText(ApplicationPreferencesActivity.this,
|
2012-09-20 02:56:04 +00:00
|
|
|
R.string.ApplicationPreferenceActivity_you_have_not_yet_defined_a_contact_for_yourself,
|
2012-07-20 02:23:49 +00:00
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
return true;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
ContactAccessor.getInstance().insertIdentityKey(ApplicationPreferencesActivity.this, rawContactIds,
|
2011-12-20 18:20:44 +00:00
|
|
|
IdentityKeyUtil.getIdentityKey(ApplicationPreferencesActivity.this));
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2012-09-08 03:03:23 +00:00
|
|
|
Toast.makeText(ApplicationPreferencesActivity.this,
|
2012-09-20 02:56:04 +00:00
|
|
|
R.string.ApplicationPreferenceActivity_exported_to_contacts_database,
|
2012-09-08 03:03:23 +00:00
|
|
|
Toast.LENGTH_LONG).show();
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ImportContactIdentityClickListener implements Preference.OnPreferenceClickListener {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
MasterSecret masterSecret = (MasterSecret)getIntent().getParcelableExtra("master_secret");
|
2012-07-20 02:23:49 +00:00
|
|
|
|
|
|
|
if (masterSecret != null) {
|
2012-10-23 02:17:08 +00:00
|
|
|
Intent importIntent = new Intent(Intent.ACTION_PICK);
|
|
|
|
importIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
|
2011-12-20 18:20:44 +00:00
|
|
|
startActivityForResult(importIntent, IMPORT_IDENTITY_ID);
|
|
|
|
} else {
|
2012-07-20 02:23:49 +00:00
|
|
|
Toast.makeText(ApplicationPreferencesActivity.this,
|
2012-09-20 02:56:04 +00:00
|
|
|
R.string.ApplicationPreferenceActivity_you_need_to_have_entered_your_passphrase_before_importing_keys,
|
2011-12-20 18:20:44 +00:00
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class ManageIdentitiesClickListener implements Preference.OnPreferenceClickListener {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
MasterSecret masterSecret = (MasterSecret)getIntent().getParcelableExtra("master_secret");
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (masterSecret != null) {
|
|
|
|
Intent manageIntent = new Intent(ApplicationPreferencesActivity.this, ReviewIdentitiesActivity.class);
|
|
|
|
manageIntent.putExtra("master_secret", masterSecret);
|
|
|
|
startActivity(manageIntent);
|
|
|
|
} else {
|
2012-07-20 02:23:49 +00:00
|
|
|
Toast.makeText(ApplicationPreferencesActivity.this,
|
2012-09-20 02:56:04 +00:00
|
|
|
R.string.ApplicationPreferenceActivity_you_need_to_have_entered_your_passphrase_before_managing_keys,
|
2011-12-20 18:20:44 +00:00
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
|
|
|
return true;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class ChangePassphraseClickListener implements Preference.OnPreferenceClickListener {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
SharedPreferences settings = getSharedPreferences(KeyCachingService.PREFERENCES_NAME, 0);
|
2012-07-20 02:23:49 +00:00
|
|
|
|
|
|
|
if (settings.getBoolean("passphrase_initialized", false)) {
|
2011-12-20 18:20:44 +00:00
|
|
|
startActivity(new Intent(ApplicationPreferencesActivity.this, PassphraseChangeActivity.class));
|
|
|
|
} else {
|
2012-09-08 03:03:23 +00:00
|
|
|
Toast.makeText(ApplicationPreferencesActivity.this,
|
2012-09-20 02:56:04 +00:00
|
|
|
R.string.ApplicationPreferenceActivity_you_havent_set_a_passphrase_yet,
|
2012-09-08 03:03:23 +00:00
|
|
|
Toast.LENGTH_LONG).show();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|