Refactor MasterSecret initialization, access, and timeout paths.

1) Consolidate all of the KeyCachingService interaction into a single
   mixin. Activities extend delegates which call through to the mixin.

2) Switch Activity increment/decrement triggers from onStop to onPause
   in order to account for some screen locks that don't stop activities.
This commit is contained in:
Moxie Marlinspike 2013-02-10 17:30:51 -08:00
parent 90280a62ae
commit c2dcf7ae74
15 changed files with 412 additions and 250 deletions

View File

@ -30,9 +30,6 @@ import android.provider.ContactsContract;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.MenuItem;
import org.thoughtcrime.securesms.contacts.ContactAccessor; import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.contacts.ContactIdentityManager; import org.thoughtcrime.securesms.contacts.ContactIdentityManager;
import org.thoughtcrime.securesms.crypto.IdentityKey; import org.thoughtcrime.securesms.crypto.IdentityKey;
@ -43,6 +40,8 @@ import org.thoughtcrime.securesms.util.Dialogs;
import org.thoughtcrime.securesms.util.MemoryCleaner; import org.thoughtcrime.securesms.util.MemoryCleaner;
import org.thoughtcrime.securesms.util.Trimmer; import org.thoughtcrime.securesms.util.Trimmer;
import com.actionbarsherlock.view.MenuItem;
import java.util.List; import java.util.List;
/** /**
@ -52,7 +51,7 @@ import java.util.List;
* *
*/ */
public class ApplicationPreferencesActivity extends SherlockPreferenceActivity { public class ApplicationPreferencesActivity extends PassphraseRequiredSherlockPreferenceActivity {
private static final int PICK_IDENTITY_CONTACT = 1; private static final int PICK_IDENTITY_CONTACT = 1;
private static final int IMPORT_IDENTITY_ID = 2; private static final int IMPORT_IDENTITY_ID = 2;
@ -119,19 +118,9 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
} }
@Override @Override
public void onStart() { public void onDestroy() {
super.onStart(); MemoryCleaner.clean((MasterSecret)getIntent().getParcelableExtra("master_secret"));
Intent intent = new Intent(this, KeyCachingService.class); super.onDestroy();
intent.setAction(KeyCachingService.ACTIVITY_START_EVENT);
startService(intent);
}
@Override
public void onStop() {
super.onStop();
Intent intent = new Intent(this, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_STOP_EVENT);
startService(intent);
} }
@Override @Override
@ -146,12 +135,6 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
} }
} }
@Override
public void onDestroy() {
MemoryCleaner.clean((MasterSecret)getIntent().getParcelableExtra("master_secret"));
super.onDestroy();
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -237,6 +220,7 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
} }
private class IdentityPreferenceClickListener implements Preference.OnPreferenceClickListener { private class IdentityPreferenceClickListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Intent.ACTION_PICK); Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE); intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
@ -246,6 +230,7 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
} }
private class ViewMyIdentityClickListener implements Preference.OnPreferenceClickListener { private class ViewMyIdentityClickListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
Intent viewIdentityIntent = new Intent(ApplicationPreferencesActivity.this, ViewIdentityActivity.class); Intent viewIdentityIntent = new Intent(ApplicationPreferencesActivity.this, ViewIdentityActivity.class);
viewIdentityIntent.putExtra("identity_key", IdentityKeyUtil.getIdentityKey(ApplicationPreferencesActivity.this)); viewIdentityIntent.putExtra("identity_key", IdentityKeyUtil.getIdentityKey(ApplicationPreferencesActivity.this));
@ -256,6 +241,7 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
} }
private class ExportMyIdentityClickListener implements Preference.OnPreferenceClickListener { private class ExportMyIdentityClickListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
if (!IdentityKeyUtil.hasIdentityKey(ApplicationPreferencesActivity.this)) { if (!IdentityKeyUtil.hasIdentityKey(ApplicationPreferencesActivity.this)) {
Toast.makeText(ApplicationPreferencesActivity.this, Toast.makeText(ApplicationPreferencesActivity.this,
@ -287,6 +273,7 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
} }
private class ImportContactIdentityClickListener implements Preference.OnPreferenceClickListener { private class ImportContactIdentityClickListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
MasterSecret masterSecret = (MasterSecret)getIntent().getParcelableExtra("master_secret"); MasterSecret masterSecret = (MasterSecret)getIntent().getParcelableExtra("master_secret");
@ -305,6 +292,7 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
} }
private class ManageIdentitiesClickListener implements Preference.OnPreferenceClickListener { private class ManageIdentitiesClickListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
MasterSecret masterSecret = (MasterSecret)getIntent().getParcelableExtra("master_secret"); MasterSecret masterSecret = (MasterSecret)getIntent().getParcelableExtra("master_secret");
@ -323,6 +311,7 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
} }
private class ChangePassphraseClickListener implements Preference.OnPreferenceClickListener { private class ChangePassphraseClickListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
SharedPreferences settings = getSharedPreferences(KeyCachingService.PREFERENCES_NAME, 0); SharedPreferences settings = getSharedPreferences(KeyCachingService.PREFERENCES_NAME, 0);
@ -393,5 +382,4 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
} }
} }
} }

View File

@ -24,8 +24,6 @@ import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import com.actionbarsherlock.app.SherlockActivity;
import org.thoughtcrime.securesms.crypto.KeyExchangeInitiator; import org.thoughtcrime.securesms.crypto.KeyExchangeInitiator;
import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.LocalKeyRecord; import org.thoughtcrime.securesms.database.LocalKeyRecord;
@ -42,7 +40,7 @@ import org.thoughtcrime.securesms.util.MemoryCleaner;
* @author Moxie Marlinspike * @author Moxie Marlinspike
* *
*/ */
public class AutoInitiateActivity extends SherlockActivity { public class AutoInitiateActivity extends PassphraseRequiredSherlockActivity {
private long threadId; private long threadId;
private Recipient recipient; private Recipient recipient;
@ -77,12 +75,14 @@ public class AutoInitiateActivity extends SherlockActivity {
} }
private class OkListener implements View.OnClickListener { private class OkListener implements View.OnClickListener {
@Override
public void onClick(View v) { public void onClick(View v) {
initiateKeyExchange(); initiateKeyExchange();
} }
} }
private class CancelListener implements View.OnClickListener { private class CancelListener implements View.OnClickListener {
@Override
public void onClick(View v) { public void onClick(View v) {
Log.w("AutoInitiateActivity", "Exempting threadID: " + threadId); Log.w("AutoInitiateActivity", "Exempting threadID: " + threadId);
exemptThread(AutoInitiateActivity.this, threadId); exemptThread(AutoInitiateActivity.this, threadId);
@ -114,5 +114,4 @@ public class AutoInitiateActivity extends SherlockActivity {
(new RemoteKeyRecord(context,recipient).getCurrentRemoteKey() == null) && (new RemoteKeyRecord(context,recipient).getCurrentRemoteKey() == null) &&
(new LocalKeyRecord(context, masterSecret, recipient).getCurrentKeyPair() == null); (new LocalKeyRecord(context, masterSecret, recipient).getCurrentKeyPair() == null);
} }
} }

View File

@ -22,17 +22,15 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import org.thoughtcrime.securesms.recipients.Recipients;
import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener; import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.service.KeyCachingService;
/** /**
* Activity container for selecting a list of contacts. Provides a tab frame for * Activity container for selecting a list of contacts. Provides a tab frame for
* contact, group, and "recent contact" activity tabs. Used by ComposeMessageActivity * contact, group, and "recent contact" activity tabs. Used by ComposeMessageActivity
@ -41,7 +39,7 @@ import org.thoughtcrime.securesms.service.KeyCachingService;
* @author Moxie Marlinspike * @author Moxie Marlinspike
* *
*/ */
public class ContactSelectionActivity extends SherlockFragmentActivity { public class ContactSelectionActivity extends PassphraseRequiredSherlockFragmentActivity {
private ContactSelectionListFragment contactsFragment; private ContactSelectionListFragment contactsFragment;
private ContactSelectionGroupsFragment groupsFragment; private ContactSelectionGroupsFragment groupsFragment;
@ -64,18 +62,6 @@ public class ContactSelectionActivity extends SherlockFragmentActivity {
setupRecentTab(); setupRecentTab();
} }
@Override
protected void onStart() {
super.onStart();
registerPassphraseActivityStarted();
}
@Override
protected void onStop() {
super.onStop();
registerPassphraseActivityStopped();
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSupportMenuInflater(); MenuInflater inflater = this.getSupportMenuInflater();
@ -160,19 +146,4 @@ public class ContactSelectionActivity extends SherlockFragmentActivity {
recentTab.setIcon(R.drawable.ic_tab_recent); recentTab.setIcon(R.drawable.ic_tab_recent);
this.getSupportActionBar().addTab(recentTab); this.getSupportActionBar().addTab(recentTab);
} }
private void registerPassphraseActivityStarted() {
Intent intent = new Intent(this, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_START_EVENT);
startService(intent);
}
private void registerPassphraseActivityStopped() {
Intent intent = new Intent(this, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_STOP_EVENT);
startService(intent);
}
} }

View File

@ -75,7 +75,6 @@ import org.thoughtcrime.securesms.util.Util;
import ws.com.google.android.mms.MmsException; import ws.com.google.android.mms.MmsException;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
@ -91,7 +90,7 @@ import java.util.List;
* @author Moxie Marlinspike * @author Moxie Marlinspike
* *
*/ */
public class ConversationActivity extends SherlockFragmentActivity public class ConversationActivity extends PassphraseRequiredSherlockFragmentActivity
implements ConversationFragment.ConversationFragmentListener implements ConversationFragment.ConversationFragmentListener
{ {
@ -116,7 +115,6 @@ public class ConversationActivity extends SherlockFragmentActivity
private AttachmentTypeSelectorAdapter attachmentAdapter; private AttachmentTypeSelectorAdapter attachmentAdapter;
private AttachmentManager attachmentManager; private AttachmentManager attachmentManager;
private BroadcastReceiver killActivityReceiver;
private BroadcastReceiver securityUpdateReceiver; private BroadcastReceiver securityUpdateReceiver;
private Recipients recipients; private Recipients recipients;
@ -161,20 +159,10 @@ public class ConversationActivity extends SherlockFragmentActivity
if (!isExistingConversation()) if (!isExistingConversation())
initializeRecipientsInput(); initializeRecipientsInput();
registerPassphraseActivityStarted();
}
@Override
protected void onStop() {
super.onStop();
registerPassphraseActivityStopped();
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
unregisterReceiver(killActivityReceiver);
unregisterReceiver(securityUpdateReceiver); unregisterReceiver(securityUpdateReceiver);
saveDraft(); saveDraft();
MemoryCleaner.clean(masterSecret); MemoryCleaner.clean(masterSecret);
@ -518,13 +506,6 @@ public class ConversationActivity extends SherlockFragmentActivity
private void initializeReceivers() { private void initializeReceivers() {
killActivityReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
securityUpdateReceiver = new BroadcastReceiver() { securityUpdateReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@ -539,10 +520,6 @@ public class ConversationActivity extends SherlockFragmentActivity
} }
}; };
registerReceiver(killActivityReceiver,
new IntentFilter(KeyCachingService.CLEAR_KEY_EVENT),
KeyCachingService.KEY_PERMISSION, null);
registerReceiver(securityUpdateReceiver, registerReceiver(securityUpdateReceiver,
new IntentFilter(KeyExchangeProcessor.SECURITY_UPDATE_EVENT), new IntentFilter(KeyExchangeProcessor.SECURITY_UPDATE_EVENT),
KeyCachingService.KEY_PERMISSION, null); KeyCachingService.KEY_PERMISSION, null);
@ -767,18 +744,6 @@ public class ConversationActivity extends SherlockFragmentActivity
} }
} }
private void registerPassphraseActivityStarted() {
Intent intent = new Intent(this, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_START_EVENT);
startService(intent);
}
private void registerPassphraseActivityStopped() {
Intent intent = new Intent(this, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_STOP_EVENT);
startService(intent);
}
// Listeners // Listeners
private class AddRecipientButtonListener implements OnClickListener { private class AddRecipientButtonListener implements OnClickListener {

View File

@ -1,18 +1,12 @@
package org.thoughtcrime.securesms; package org.thoughtcrime.securesms;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable; import android.os.Parcelable;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.util.Log; import android.util.Log;
@ -31,20 +25,17 @@ import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.service.SendReceiveService; import org.thoughtcrime.securesms.service.SendReceiveService;
import org.thoughtcrime.securesms.util.MemoryCleaner; import org.thoughtcrime.securesms.util.MemoryCleaner;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
public class ConversationListActivity extends SherlockFragmentActivity public class ConversationListActivity extends PassphraseRequiredSherlockFragmentActivity
implements ConversationListFragment.ConversationSelectedListener implements ConversationListFragment.ConversationSelectedListener
{ {
private ConversationListFragment fragment; private ConversationListFragment fragment;
private MasterSecret masterSecret; private MasterSecret masterSecret;
private BroadcastReceiver killActivityReceiver;
private BroadcastReceiver newKeyReceiver;
private ApplicationMigrationManager migrationManager; private ApplicationMigrationManager migrationManager;
private boolean havePromptedForPassphrase = false; private boolean havePromptedForPassphrase = false;
@ -56,7 +47,6 @@ public class ConversationListActivity extends SherlockFragmentActivity
setContentView(R.layout.conversation_list_activity); setContentView(R.layout.conversation_list_activity);
getSupportActionBar().setTitle("TextSecure"); getSupportActionBar().setTitle("TextSecure");
initializeKillReceiver();
initializeSenderReceiverService(); initializeSenderReceiverService();
initializeResources(); initializeResources();
initializeContactUpdatesReceiver(); initializeContactUpdatesReceiver();
@ -65,51 +55,71 @@ public class ConversationListActivity extends SherlockFragmentActivity
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(Intent intent) {
super.onNewIntent(intent); super.onNewIntent(intent);
createConversationIfNecessary(intent); this.setIntent(intent);
}
@Override
public void onPause() {
super.onPause();
if (newKeyReceiver != null) {
unregisterReceiver(newKeyReceiver);
newKeyReceiver = null;
}
isVisible = false;
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
clearNotifications();
initializeKeyCachingServiceRegistration();
isVisible = true; isVisible = true;
} }
@Override @Override
public void onStart() { public void onPause() {
super.onStart(); super.onPause();
registerPassphraseActivityStarted();
isVisible = false;
} }
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
havePromptedForPassphrase = false; havePromptedForPassphrase = false;
registerPassphraseActivityStopped();
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
Log.w("SecureSMS", "onDestroy..."); Log.w("ConversationListActivity", "onDestroy...");
unregisterReceiver(killActivityReceiver);
MemoryCleaner.clean(masterSecret); MemoryCleaner.clean(masterSecret);
super.onDestroy(); super.onDestroy();
} }
@Override
public void onMasterSecretCleared() {
this.masterSecret = null;
this.fragment.setMasterSecret(null);
this.invalidateOptionsMenu();
if (!havePromptedForPassphrase && isVisible) {
promptForPassphrase();
}
}
@Override
public void onNewMasterSecret(MasterSecret masterSecret) {
this.masterSecret = masterSecret;
if (masterSecret != null) {
if (!IdentityKeyUtil.hasIdentityKey(this)) {
new Thread(new IdentityKeyInitializer()).start();
}
if (!MasterSecretUtil.hasAsymmericMasterSecret(this)) {
new Thread(new AsymmetricMasteSecretInitializer()).start();
}
if (!isDatabaseMigrated()) initializeDatabaseMigration();
else DecryptingQueue.schedulePendingDecrypts(this, masterSecret);
}
this.fragment.setMasterSecret(masterSecret);
this.invalidateOptionsMenu();
this.havePromptedForPassphrase = false;
createConversationIfNecessary(this.getIntent());
}
@Override @Override
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {
Log.w("ConversationListActivity", "onPrepareOptionsMenu..."); Log.w("ConversationListActivity", "onPrepareOptionsMenu...");
@ -190,11 +200,8 @@ public class ConversationListActivity extends SherlockFragmentActivity
ApplicationExportListener listener = new ApplicationExportManager.ApplicationExportListener() { ApplicationExportListener listener = new ApplicationExportManager.ApplicationExportListener() {
@Override @Override
public void onPrepareForImport() { public void onPrepareForImport() {
initializeWithMasterSecret(null); onMasterSecretCleared();
handleClearPassphrase();
Intent clearKeyIntent = new Intent(KeyCachingService.CLEAR_KEY_ACTION, null,
ConversationListActivity.this, KeyCachingService.class);
startService(clearKeyIntent);
} }
}; };
@ -208,45 +215,6 @@ public class ConversationListActivity extends SherlockFragmentActivity
startService(intent); startService(intent);
} }
private void initializeWithMasterSecret(MasterSecret masterSecret) {
this.masterSecret = masterSecret;
if (masterSecret != null) {
if (!IdentityKeyUtil.hasIdentityKey(this)) {
new Thread(new IdentityKeyInitializer()).start();
}
if (!MasterSecretUtil.hasAsymmericMasterSecret(this)) {
new Thread(new AsymmetricMasteSecretInitializer()).start();
}
if (!isDatabaseMigrated()) initializeDatabaseMigration();
else DecryptingQueue.schedulePendingDecrypts(this, masterSecret);
}
this.fragment.setMasterSecret(masterSecret);
this.invalidateOptionsMenu();
createConversationIfNecessary(this.getIntent());
}
private void initializeKillReceiver() {
this.killActivityReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConversationListActivity.this.masterSecret = null;
fragment.setMasterSecret(null);
if (isVisible) {
promptForPassphrase();
}
}
};
registerReceiver(this.killActivityReceiver,
new IntentFilter(KeyCachingService.CLEAR_KEY_EVENT),
KeyCachingService.KEY_PERMISSION, null);
}
private void initializeContactUpdatesReceiver() { private void initializeContactUpdatesReceiver() {
ContentObserver observer = new ContentObserver(null) { ContentObserver observer = new ContentObserver(null) {
@Override @Override
@ -288,26 +256,10 @@ public class ConversationListActivity extends SherlockFragmentActivity
} }
} }
private void initializeKeyCachingServiceRegistration() {
Log.w("ConversationListActivity", "Checking caching service...");
this.newKeyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("ConversationListActivity", "Got a key broadcast...");
initializeWithMasterSecret((MasterSecret)intent.getParcelableExtra("master_secret"));
}
};
IntentFilter filter = new IntentFilter(KeyCachingService.NEW_KEY_EVENT);
registerReceiver(newKeyReceiver, filter, KeyCachingService.KEY_PERMISSION, null);
Intent bindIntent = new Intent(this, KeyCachingService.class);
bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
private void initializeResources() { private void initializeResources() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
} }
this.fragment = (ConversationListFragment)this.getSupportFragmentManager() this.fragment = (ConversationListFragment)this.getSupportFragmentManager()
@ -319,13 +271,6 @@ public class ConversationListActivity extends SherlockFragmentActivity
.getBoolean("migrated", false); .getBoolean("migrated", false);
} }
private void clearNotifications() {
NotificationManager manager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(KeyCachingService.NOTIFICATION_ID);
}
private void createConversationIfNecessary(Intent intent) { private void createConversationIfNecessary(Intent intent) {
long thread = intent.getLongExtra("thread_id", -1L); long thread = intent.getLongExtra("thread_id", -1L);
String type = intent.getType(); String type = intent.getType();
@ -364,43 +309,6 @@ public class ConversationListActivity extends SherlockFragmentActivity
} }
} }
private void registerPassphraseActivityStarted() {
Intent intent = new Intent(this, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_START_EVENT);
startService(intent);
}
private void registerPassphraseActivityStopped() {
Intent intent = new Intent(this, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_STOP_EVENT);
startService(intent);
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
KeyCachingService keyCachingService = ((KeyCachingService.KeyCachingBinder)service).getService();
MasterSecret masterSecret = keyCachingService.getMasterSecret();
initializeWithMasterSecret(masterSecret);
if (masterSecret == null && !havePromptedForPassphrase)
promptForPassphrase();
Intent cachingIntent = new Intent(ConversationListActivity.this, KeyCachingService.class);
startService(cachingIntent);
try {
ConversationListActivity.this.unbindService(this);
} catch (IllegalArgumentException iae) {
Log.w("SecureSMS", iae);
}
}
@Override
public void onServiceDisconnected(ComponentName name) {}
};
private class IdentityKeyInitializer implements Runnable { private class IdentityKeyInitializer implements Runnable {
@Override @Override
public void run() { public void run() {
@ -414,5 +322,4 @@ public class ConversationListActivity extends SherlockFragmentActivity
MasterSecretUtil.generateAsymmetricMasterSecret(ConversationListActivity.this, masterSecret); MasterSecretUtil.generateAsymmetricMasterSecret(ConversationListActivity.this, masterSecret);
} }
} }
} }

View File

@ -19,23 +19,22 @@ package org.thoughtcrime.securesms;
import android.content.Intent; import android.content.Intent;
import android.widget.Toast; import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity; import org.thoughtcrime.securesms.crypto.SerializableKey;
import org.thoughtcrime.securesms.util.Base64;
import org.thoughtcrime.securesms.util.Dialogs;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult; import com.google.zxing.integration.android.IntentResult;
import org.thoughtcrime.securesms.crypto.SerializableKey;
import org.thoughtcrime.securesms.util.Base64;
import org.thoughtcrime.securesms.util.Dialogs;
/** /**
* Activity for initiating/receiving key QR code scans. * Activity for initiating/receiving key QR code scans.
* *
* @author Moxie Marlinspike * @author Moxie Marlinspike
*/ */
public abstract class KeyScanningActivity extends SherlockActivity { public abstract class KeyScanningActivity extends PassphraseRequiredSherlockActivity {
@Override @Override
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {

View File

@ -0,0 +1,8 @@
package org.thoughtcrime.securesms;
import org.thoughtcrime.securesms.crypto.MasterSecret;
public interface PassphraseRequiredActivity {
public void onMasterSecretCleared();
public void onNewMasterSecret(MasterSecret masterSecret);
}

View File

@ -0,0 +1,128 @@
package org.thoughtcrime.securesms;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.service.KeyCachingService;
public class PassphraseRequiredMixin {
private KeyCachingServiceConnection serviceConnection;
private BroadcastReceiver clearKeyReceiver;
private BroadcastReceiver newKeyReceiver;
public void onCreate(Context context, PassphraseRequiredActivity activity) {
initializeClearKeyReceiver(context, activity);
}
public void onResume(Context context, PassphraseRequiredActivity activity) {
initializeNewKeyReceiver(context, activity);
initializeServiceConnection(context, activity);
KeyCachingService.registerPassphraseActivityStarted(context);
}
public void onPause(Context context, PassphraseRequiredActivity activity) {
removeNewKeyReceiver(context);
removeServiceConnection(context);
KeyCachingService.registerPassphraseActivityStopped(context);
}
public void onDestroy(Context context, PassphraseRequiredActivity activity) {
removeClearKeyReceiver(context);
}
private void initializeClearKeyReceiver(Context context, final PassphraseRequiredActivity activity) {
this.clearKeyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
activity.onMasterSecretCleared();
}
};
IntentFilter filter = new IntentFilter(KeyCachingService.CLEAR_KEY_EVENT);
context.registerReceiver(clearKeyReceiver, filter, KeyCachingService.KEY_PERMISSION, null);
}
private void initializeNewKeyReceiver(Context context, final PassphraseRequiredActivity activity) {
this.newKeyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
activity.onNewMasterSecret((MasterSecret)intent.getParcelableExtra("master_secret"));
}
};
IntentFilter filter = new IntentFilter(KeyCachingService.NEW_KEY_EVENT);
context.registerReceiver(newKeyReceiver, filter, KeyCachingService.KEY_PERMISSION, null);
}
private void initializeServiceConnection(Context context, PassphraseRequiredActivity activity) {
Intent cachingIntent = new Intent(context, KeyCachingService.class);
context.startService(cachingIntent);
this.serviceConnection = new KeyCachingServiceConnection(activity);
Intent bindIntent = new Intent(context, KeyCachingService.class);
context.bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
private void removeClearKeyReceiver(Context context) {
if (clearKeyReceiver != null) {
context.unregisterReceiver(clearKeyReceiver);
clearKeyReceiver = null;
}
}
private void removeNewKeyReceiver(Context context) {
if (newKeyReceiver != null) {
context.unregisterReceiver(newKeyReceiver);
newKeyReceiver = null;
}
}
private void removeServiceConnection(Context context) {
if (this.serviceConnection != null && this.serviceConnection.isBound()) {
context.unbindService(this.serviceConnection);
}
}
private static class KeyCachingServiceConnection implements ServiceConnection {
private final PassphraseRequiredActivity activity;
private boolean isBound;
public KeyCachingServiceConnection(PassphraseRequiredActivity activity) {
this.activity = activity;
this.isBound = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
KeyCachingService keyCachingService = ((KeyCachingService.KeyCachingBinder)service).getService();
MasterSecret masterSecret = keyCachingService.getMasterSecret();
this.isBound = true;
if (masterSecret == null) {
activity.onMasterSecretCleared();
} else {
activity.onNewMasterSecret(masterSecret);
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
this.isBound = false;
}
public boolean isBound() {
return this.isBound;
}
}
}

View File

@ -0,0 +1,45 @@
package org.thoughtcrime.securesms;
import android.os.Bundle;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import com.actionbarsherlock.app.SherlockActivity;
public class PassphraseRequiredSherlockActivity extends SherlockActivity implements PassphraseRequiredActivity {
private final PassphraseRequiredMixin delegate = new PassphraseRequiredMixin();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
delegate.onCreate(this, this);
}
@Override
protected void onResume() {
super.onResume();
delegate.onResume(this, this);
}
@Override
protected void onPause() {
super.onPause();
delegate.onPause(this, this);
}
@Override
protected void onDestroy() {
super.onDestroy();
delegate.onDestroy(this, this);
}
@Override
public void onMasterSecretCleared() {
finish();
}
@Override
public void onNewMasterSecret(MasterSecret masterSecret) {}
}

View File

@ -0,0 +1,44 @@
package org.thoughtcrime.securesms;
import android.os.Bundle;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class PassphraseRequiredSherlockFragmentActivity extends SherlockFragmentActivity implements PassphraseRequiredActivity {
private final PassphraseRequiredMixin delegate = new PassphraseRequiredMixin();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
delegate.onCreate(this, this);
}
@Override
protected void onResume() {
super.onResume();
delegate.onResume(this, this);
}
@Override
protected void onPause() {
super.onPause();
delegate.onPause(this, this);
}
@Override
protected void onDestroy() {
super.onDestroy();
delegate.onDestroy(this, this);
}
@Override
public void onMasterSecretCleared() {
finish();
}
@Override
public void onNewMasterSecret(MasterSecret masterSecret) {}
}

View File

@ -0,0 +1,45 @@
package org.thoughtcrime.securesms;
import android.os.Bundle;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import com.actionbarsherlock.app.SherlockListActivity;
public class PassphraseRequiredSherlockListActivity extends SherlockListActivity implements PassphraseRequiredActivity {
private final PassphraseRequiredMixin delegate = new PassphraseRequiredMixin();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
delegate.onCreate(this, this);
}
@Override
protected void onResume() {
super.onResume();
delegate.onResume(this, this);
}
@Override
protected void onPause() {
super.onPause();
delegate.onPause(this, this);
}
@Override
protected void onDestroy() {
super.onDestroy();
delegate.onDestroy(this, this);
}
@Override
public void onMasterSecretCleared() {
finish();
}
@Override
public void onNewMasterSecret(MasterSecret masterSecret) {}
}

View File

@ -0,0 +1,48 @@
package org.thoughtcrime.securesms;
import android.os.Bundle;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import com.actionbarsherlock.app.SherlockPreferenceActivity;
public abstract class PassphraseRequiredSherlockPreferenceActivity
extends SherlockPreferenceActivity
implements PassphraseRequiredActivity
{
private final PassphraseRequiredMixin delegate = new PassphraseRequiredMixin();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
delegate.onCreate(this, this);
}
@Override
protected void onResume() {
super.onResume();
delegate.onResume(this, this);
}
@Override
protected void onPause() {
super.onPause();
delegate.onPause(this, this);
}
@Override
protected void onDestroy() {
super.onDestroy();
delegate.onDestroy(this, this);
}
@Override
public void onMasterSecretCleared() {
finish();
}
@Override
public void onNewMasterSecret(MasterSecret masterSecret) {}
}

View File

@ -16,7 +16,6 @@
*/ */
package org.thoughtcrime.securesms; package org.thoughtcrime.securesms;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
@ -40,7 +39,7 @@ import org.thoughtcrime.securesms.util.MemoryCleaner;
* @author Moxie Marlinspike * @author Moxie Marlinspike
*/ */
public class ReceiveKeyActivity extends Activity { public class ReceiveKeyActivity extends PassphraseRequiredSherlockActivity {
private TextView descriptionText; private TextView descriptionText;
private TextView signatureText; private TextView signatureText;
@ -171,6 +170,7 @@ public class ReceiveKeyActivity extends Activity {
} }
private class VerifyIdentityListener implements View.OnClickListener { private class VerifyIdentityListener implements View.OnClickListener {
@Override
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(ReceiveKeyActivity.this, VerifyIdentityActivity.class); Intent intent = new Intent(ReceiveKeyActivity.this, VerifyIdentityActivity.class);
intent.putExtra("recipient", recipient); intent.putExtra("recipient", recipient);
@ -181,6 +181,7 @@ public class ReceiveKeyActivity extends Activity {
} }
private class VerifySessionListener implements View.OnClickListener { private class VerifySessionListener implements View.OnClickListener {
@Override
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(ReceiveKeyActivity.this, VerifyKeysActivity.class); Intent intent = new Intent(ReceiveKeyActivity.this, VerifyKeysActivity.class);
intent.putExtra("recipient", recipient); intent.putExtra("recipient", recipient);
@ -191,6 +192,7 @@ public class ReceiveKeyActivity extends Activity {
} }
private class OkListener implements View.OnClickListener { private class OkListener implements View.OnClickListener {
@Override
public void onClick(View v) { public void onClick(View v) {
keyExchangeProcessor.processKeyExchangeMessage(keyExchangeMessage, threadId); keyExchangeProcessor.processKeyExchangeMessage(keyExchangeMessage, threadId);
finish(); finish();
@ -198,6 +200,7 @@ public class ReceiveKeyActivity extends Activity {
} }
private class CancelListener implements View.OnClickListener { private class CancelListener implements View.OnClickListener {
@Override
public void onClick(View v) { public void onClick(View v) {
ReceiveKeyActivity.this.finish(); ReceiveKeyActivity.this.finish();
} }

View File

@ -25,8 +25,6 @@ import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import org.thoughtcrime.securesms.crypto.IdentityKey; import org.thoughtcrime.securesms.crypto.IdentityKey;
import org.thoughtcrime.securesms.crypto.InvalidKeyException; import org.thoughtcrime.securesms.crypto.InvalidKeyException;
import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.crypto.MasterSecret;
@ -39,7 +37,7 @@ import org.thoughtcrime.securesms.util.MemoryCleaner;
* *
* @author Moxie Marlinspike * @author Moxie Marlinspike
*/ */
public class SaveIdentityActivity extends SherlockActivity { public class SaveIdentityActivity extends PassphraseRequiredSherlockActivity {
private MasterSecret masterSecret; private MasterSecret masterSecret;
private IdentityKey identityKey; private IdentityKey identityKey;
@ -83,6 +81,7 @@ public class SaveIdentityActivity extends SherlockActivity {
} }
private class OkListener implements View.OnClickListener { private class OkListener implements View.OnClickListener {
@Override
public void onClick(View v) { public void onClick(View v) {
if (identityName.getText() == null || identityName.getText().toString().trim().length() == 0) { if (identityName.getText() == null || identityName.getText().toString().trim().length() == 0) {
Toast.makeText(SaveIdentityActivity.this, Toast.makeText(SaveIdentityActivity.this,
@ -99,6 +98,7 @@ public class SaveIdentityActivity extends SherlockActivity {
builder.setMessage(R.string.SaveIdentityActivity_an_identity_key_with_the_specified_name_already_exists); builder.setMessage(R.string.SaveIdentityActivity_an_identity_key_with_the_specified_name_already_exists);
builder.setPositiveButton(R.string.SaveIdentityActivity_manage_identities, builder.setPositiveButton(R.string.SaveIdentityActivity_manage_identities,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(SaveIdentityActivity.this, ReviewIdentitiesActivity.class); Intent intent = new Intent(SaveIdentityActivity.this, ReviewIdentitiesActivity.class);
intent.putExtra("master_secret", masterSecret); intent.putExtra("master_secret", masterSecret);
@ -115,9 +115,9 @@ public class SaveIdentityActivity extends SherlockActivity {
} }
private class CancelListener implements View.OnClickListener { private class CancelListener implements View.OnClickListener {
@Override
public void onClick(View v) { public void onClick(View v) {
finish(); finish();
} }
} }
} }

View File

@ -20,6 +20,7 @@ import android.app.AlarmManager;
import android.app.Notification; import android.app.Notification;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Binder; import android.os.Binder;
@ -44,8 +45,7 @@ import org.thoughtcrime.securesms.notifications.MessageNotifier;
public class KeyCachingService extends Service { public class KeyCachingService extends Service {
public static final int NOTIFICATION_ID = 1337; public static final int SERVICE_RUNNING_ID = 4141;
public static final int SERVICE_RUNNING_ID = 4141;
public static final String KEY_PERMISSION = "org.thoughtcrime.securesms.ACCESS_SECRETS"; public static final String KEY_PERMISSION = "org.thoughtcrime.securesms.ACCESS_SECRETS";
public static final String NEW_KEY_EVENT = "org.thoughtcrime.securesms.service.action.NEW_KEY_EVENT"; public static final String NEW_KEY_EVENT = "org.thoughtcrime.securesms.service.action.NEW_KEY_EVENT";
@ -215,4 +215,16 @@ public class KeyCachingService extends Service {
return KeyCachingService.this; return KeyCachingService.this;
} }
} }
public static void registerPassphraseActivityStarted(Context activity) {
Intent intent = new Intent(activity, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_START_EVENT);
activity.startService(intent);
}
public static void registerPassphraseActivityStopped(Context activity) {
Intent intent = new Intent(activity, KeyCachingService.class);
intent.setAction(KeyCachingService.ACTIVITY_STOP_EVENT);
activity.startService(intent);
}
} }