session-android/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java

255 lines
11 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
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.
*
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;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.widget.Toast;
2012-08-03 03:23:41 +00:00
import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.MenuItem;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
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;
2011-12-20 18:20:44 +00:00
/**
* The Activity for application preference display and management.
*
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;
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";
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";
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);
2011-12-20 18:20:44 +00:00
this.findPreference(IDENTITY_PREF).setOnPreferenceClickListener(new IdentityPreferenceClickListener());
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
@Override
public void onStart() {
super.onStart();
Intent intent = new Intent(this, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_START_EVENT);
startService(intent);
}
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);
}
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;
}
}
}
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;
}
2011-12-20 18:20:44 +00:00
private void handleIdentitySelection(Intent data) {
Uri contactData = data.getData();
if (contactData != null)
2011-12-20 18:20:44 +00:00
PreferenceManager.getDefaultSharedPreferences(this).edit().putString(IDENTITY_PREF, contactData.toString()).commit();
}
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);
2011-12-20 18:20:44 +00:00
if (identityKey == null) {
Dialogs.displayAlert(this,
getString(R.string.not_found_exclamation),
getString(R.string.no_valid_identity_key_was_found_in_the_specified_contact),
android.R.drawable.ic_dialog_alert);
2011-12-20 18:20:44 +00:00
return;
}
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);
}
2011-12-20 18:20:44 +00:00
private class IdentityPreferenceClickListener implements Preference.OnPreferenceClickListener {
public boolean onPreferenceClick(Preference preference) {
Intent intent = ContactAccessor.getInstance().getIntentForContactSelection();
startActivityForResult(intent, PICK_IDENTITY_CONTACT);
return true;
}
2011-12-20 18:20:44 +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;
}
}
2011-12-20 18:20:44 +00:00
private class ExportMyIdentityClickListener implements Preference.OnPreferenceClickListener {
public boolean onPreferenceClick(Preference preference) {
String contactUri = PreferenceManager.getDefaultSharedPreferences(ApplicationPreferencesActivity.this)
.getString(IDENTITY_PREF, null);
2011-12-20 18:20:44 +00:00
if (!IdentityKeyUtil.hasIdentityKey(ApplicationPreferencesActivity.this)) {
Toast.makeText(ApplicationPreferencesActivity.this,
R.string.you_don_t_have_an_identity_key_exclamation,
Toast.LENGTH_LONG).show();
2011-12-20 18:20:44 +00:00
return true;
}
2011-12-20 18:20:44 +00:00
if (contactUri == null) {
Toast.makeText(ApplicationPreferencesActivity.this,
R.string.you_have_not_yet_defined_a_contact_for_yourself,
Toast.LENGTH_LONG).show();
return true;
2011-12-20 18:20:44 +00:00
}
ContactAccessor.getInstance().insertIdentityKey(ApplicationPreferencesActivity.this, Uri.parse(contactUri),
2011-12-20 18:20:44 +00:00
IdentityKeyUtil.getIdentityKey(ApplicationPreferencesActivity.this));
Toast.makeText(ApplicationPreferencesActivity.this,
R.string.exported_to_contacts_database,
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");
if (masterSecret != null) {
2011-12-20 18:20:44 +00:00
Intent importIntent = ContactAccessor.getInstance().getIntentForContactSelection();
startActivityForResult(importIntent, IMPORT_IDENTITY_ID);
} else {
Toast.makeText(ApplicationPreferencesActivity.this,
R.string.you_need_to_have_entered_your_passphrase_before_importing_keys,
2011-12-20 18:20:44 +00:00
Toast.LENGTH_LONG).show();
}
2011-12-20 18:20:44 +00:00
return true;
}
}
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");
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 {
Toast.makeText(ApplicationPreferencesActivity.this,
R.string.you_need_to_have_entered_your_passphrase_before_managing_keys,
2011-12-20 18:20:44 +00:00
Toast.LENGTH_LONG).show();
}
return true;
2011-12-20 18:20:44 +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);
if (settings.getBoolean("passphrase_initialized", false)) {
2011-12-20 18:20:44 +00:00
startActivity(new Intent(ApplicationPreferencesActivity.this, PassphraseChangeActivity.class));
} else {
Toast.makeText(ApplicationPreferencesActivity.this,
R.string.you_havent_set_a_passphrase_yet,
Toast.LENGTH_LONG).show();
2011-12-20 18:20:44 +00:00
}
return true;
}
}
2011-12-20 18:20:44 +00:00
}