2013-02-11 02:30:51 +01:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2014-12-15 21:25:55 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.os.Build;
|
2013-02-11 02:30:51 +01:00
|
|
|
import android.os.Bundle;
|
2014-12-15 21:25:55 +01:00
|
|
|
import android.support.annotation.IdRes;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.WindowManager;
|
2013-02-11 02:30:51 +01:00
|
|
|
|
2014-11-04 00:16:04 +01:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2014-12-15 21:25:55 +01:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
|
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
|
|
|
import org.thoughtcrime.securesms.service.MessageRetrievalService;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2013-02-11 02:30:51 +01:00
|
|
|
|
2015-03-19 21:08:48 +01:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2014-12-15 21:25:55 +01:00
|
|
|
public abstract class PassphraseRequiredActionBarActivity extends BaseActionBarActivity implements MasterSecretListener {
|
|
|
|
private static final String TAG = PassphraseRequiredActionBarActivity.class.getSimpleName();
|
2013-02-11 02:30:51 +01:00
|
|
|
|
2015-03-19 21:08:48 +01:00
|
|
|
public static final String LOCALE_EXTRA = "locale_extra";
|
|
|
|
|
2014-12-15 21:25:55 +01:00
|
|
|
private static final int STATE_NORMAL = 0;
|
|
|
|
private static final int STATE_CREATE_PASSPHRASE = 1;
|
|
|
|
private static final int STATE_PROMPT_PASSPHRASE = 2;
|
|
|
|
private static final int STATE_UPGRADE_DATABASE = 3;
|
|
|
|
private static final int STATE_PROMPT_PUSH_REGISTRATION = 4;
|
|
|
|
|
|
|
|
private BroadcastReceiver clearKeyReceiver;
|
|
|
|
private boolean isVisible;
|
2013-02-11 02:30:51 +01:00
|
|
|
|
|
|
|
@Override
|
2014-12-15 21:25:55 +01:00
|
|
|
protected final void onCreate(Bundle savedInstanceState) {
|
|
|
|
onPreCreate();
|
|
|
|
final MasterSecret masterSecret = KeyCachingService.getMasterSecret(this);
|
|
|
|
routeApplicationState(masterSecret);
|
2013-02-11 02:30:51 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
2014-12-15 21:25:55 +01:00
|
|
|
if (!isFinishing()) {
|
|
|
|
initializeClearKeyReceiver();
|
|
|
|
onCreate(savedInstanceState, masterSecret);
|
|
|
|
}
|
2013-02-11 02:30:51 +01:00
|
|
|
}
|
|
|
|
|
2014-12-15 21:25:55 +01:00
|
|
|
protected void onPreCreate() {}
|
|
|
|
protected void onCreate(Bundle savedInstanceState, @NonNull MasterSecret masterSecret) {}
|
|
|
|
|
2013-02-11 02:30:51 +01:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
2014-12-15 21:25:55 +01:00
|
|
|
initializeScreenshotSecurity();
|
|
|
|
KeyCachingService.registerPassphraseActivityStarted(this);
|
|
|
|
MessageRetrievalService.registerActivityStarted(this);
|
|
|
|
isVisible = true;
|
2013-02-11 02:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
2014-12-15 21:25:55 +01:00
|
|
|
KeyCachingService.registerPassphraseActivityStopped(this);
|
|
|
|
MessageRetrievalService.registerActivityStopped(this);
|
|
|
|
isVisible = false;
|
2013-02-11 02:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
2014-12-15 21:25:55 +01:00
|
|
|
removeClearKeyReceiver(this);
|
2013-02-11 02:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMasterSecretCleared() {
|
2014-12-15 21:25:55 +01:00
|
|
|
Log.w(TAG, "onMasterSecretCleared()");
|
|
|
|
if (isVisible) routeApplicationState(null);
|
|
|
|
else finish();
|
2013-02-11 02:30:51 +01:00
|
|
|
}
|
|
|
|
|
2015-06-09 16:37:20 +02:00
|
|
|
protected <T extends Fragment> T initFragment(@IdRes int target,
|
|
|
|
@NonNull T fragment,
|
|
|
|
@NonNull MasterSecret masterSecret)
|
|
|
|
{
|
|
|
|
return initFragment(target, fragment, masterSecret, null);
|
|
|
|
}
|
|
|
|
|
2014-12-15 21:25:55 +01:00
|
|
|
protected <T extends Fragment> T initFragment(@IdRes int target,
|
|
|
|
@NonNull T fragment,
|
2015-03-19 21:08:48 +01:00
|
|
|
@NonNull MasterSecret masterSecret,
|
2015-06-09 16:37:20 +02:00
|
|
|
@Nullable Locale locale)
|
|
|
|
{
|
|
|
|
return initFragment(target, fragment, masterSecret, locale, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected <T extends Fragment> T initFragment(@IdRes int target,
|
|
|
|
@NonNull T fragment,
|
|
|
|
@NonNull MasterSecret masterSecret,
|
|
|
|
@Nullable Locale locale,
|
|
|
|
@Nullable Bundle extras)
|
|
|
|
{
|
2014-12-15 21:25:55 +01:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putParcelable("master_secret", masterSecret);
|
2015-03-19 21:08:48 +01:00
|
|
|
args.putSerializable(LOCALE_EXTRA, locale);
|
|
|
|
|
2015-06-09 16:37:20 +02:00
|
|
|
if (extras != null) {
|
|
|
|
args.putAll(extras);
|
|
|
|
}
|
|
|
|
|
2014-12-15 21:25:55 +01:00
|
|
|
fragment.setArguments(args);
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.replace(target, fragment)
|
|
|
|
.commit();
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void routeApplicationState(MasterSecret masterSecret) {
|
|
|
|
Intent intent = getIntentForState(masterSecret, getApplicationState(masterSecret));
|
|
|
|
if (intent != null) {
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Intent getIntentForState(MasterSecret masterSecret, int state) {
|
|
|
|
Log.w(TAG, "routeApplicationState(), state: " + state);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case STATE_CREATE_PASSPHRASE: return getCreatePassphraseIntent();
|
|
|
|
case STATE_PROMPT_PASSPHRASE: return getPromptPassphraseIntent();
|
|
|
|
case STATE_UPGRADE_DATABASE: return getUpgradeDatabaseIntent(masterSecret);
|
2015-04-03 02:14:08 +02:00
|
|
|
case STATE_PROMPT_PUSH_REGISTRATION: return getPushRegistrationIntent(masterSecret);
|
2014-12-15 21:25:55 +01:00
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getApplicationState(MasterSecret masterSecret) {
|
|
|
|
if (!MasterSecretUtil.isPassphraseInitialized(this)) {
|
|
|
|
return STATE_CREATE_PASSPHRASE;
|
|
|
|
} else if (masterSecret == null) {
|
|
|
|
return STATE_PROMPT_PASSPHRASE;
|
|
|
|
} else if (DatabaseUpgradeActivity.isUpdate(this)) {
|
|
|
|
return STATE_UPGRADE_DATABASE;
|
|
|
|
} else if (!TextSecurePreferences.hasPromptedPushRegistration(this)) {
|
|
|
|
return STATE_PROMPT_PUSH_REGISTRATION;
|
|
|
|
} else {
|
|
|
|
return STATE_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Intent getCreatePassphraseIntent() {
|
|
|
|
return getRoutedIntent(PassphraseCreateActivity.class, getIntent(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Intent getPromptPassphraseIntent() {
|
|
|
|
return getRoutedIntent(PassphrasePromptActivity.class, getIntent(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Intent getUpgradeDatabaseIntent(MasterSecret masterSecret) {
|
|
|
|
return getRoutedIntent(DatabaseUpgradeActivity.class,
|
|
|
|
TextSecurePreferences.hasPromptedPushRegistration(this)
|
|
|
|
? getConversationListIntent()
|
2015-04-03 02:14:08 +02:00
|
|
|
: getPushRegistrationIntent(masterSecret),
|
2014-12-15 21:25:55 +01:00
|
|
|
masterSecret);
|
|
|
|
}
|
|
|
|
|
2015-04-03 02:14:08 +02:00
|
|
|
private Intent getPushRegistrationIntent(MasterSecret masterSecret) {
|
|
|
|
return getRoutedIntent(RegistrationActivity.class, getConversationListIntent(), masterSecret);
|
2014-12-15 21:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private Intent getRoutedIntent(Class<?> destination, @Nullable Intent nextIntent, @Nullable MasterSecret masterSecret) {
|
|
|
|
final Intent intent = new Intent(this, destination);
|
|
|
|
if (nextIntent != null) intent.putExtra("next_intent", nextIntent);
|
|
|
|
if (masterSecret != null) intent.putExtra("master_secret", masterSecret);
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Intent getConversationListIntent() {
|
|
|
|
return new Intent(this, ConversationListActivity.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeScreenshotSecurity() {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
|
|
|
|
TextSecurePreferences.isScreenSecurityEnabled(this))
|
|
|
|
{
|
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
|
|
|
} else {
|
|
|
|
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeClearKeyReceiver() {
|
|
|
|
Log.w(TAG, "initializeClearKeyReceiver()");
|
|
|
|
this.clearKeyReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
Log.w(TAG, "onReceive() for clear key event");
|
|
|
|
onMasterSecretCleared();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter(KeyCachingService.CLEAR_KEY_EVENT);
|
|
|
|
registerReceiver(clearKeyReceiver, filter, KeyCachingService.KEY_PERMISSION, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void removeClearKeyReceiver(Context context) {
|
|
|
|
if (clearKeyReceiver != null) {
|
|
|
|
context.unregisterReceiver(clearKeyReceiver);
|
|
|
|
clearKeyReceiver = null;
|
|
|
|
}
|
|
|
|
}
|
2013-02-11 02:30:51 +01:00
|
|
|
}
|