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

224 lines
7.6 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
2018-08-01 15:09:24 +00:00
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob;
import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess;
2019-02-13 19:52:55 +00:00
import org.thoughtcrime.securesms.registration.WelcomeActivity;
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import java.util.Locale;
public abstract class PassphraseRequiredActionBarActivity extends BaseActionBarActivity implements MasterSecretListener {
private static final String TAG = PassphraseRequiredActionBarActivity.class.getSimpleName();
public static final String LOCALE_EXTRA = "locale_extra";
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 static final int STATE_EXPERIENCE_UPGRADE = 5;
2019-02-13 19:52:55 +00:00
private static final int STATE_WELCOME_SCREEN = 6;
private SignalServiceNetworkAccess networkAccess;
private BroadcastReceiver clearKeyReceiver;
@Override
protected final void onCreate(Bundle savedInstanceState) {
2018-08-02 13:25:33 +00:00
Log.i(TAG, "onCreate(" + savedInstanceState + ")");
this.networkAccess = new SignalServiceNetworkAccess(this);
onPreCreate();
2018-02-02 03:22:48 +00:00
final boolean locked = KeyCachingService.isLocked(this);
routeApplicationState(locked);
super.onCreate(savedInstanceState);
2018-02-02 03:22:48 +00:00
if (!isFinishing()) {
initializeClearKeyReceiver();
2018-02-02 03:22:48 +00:00
onCreate(savedInstanceState, true);
}
}
protected void onPreCreate() {}
2018-02-02 03:22:48 +00:00
protected void onCreate(Bundle savedInstanceState, boolean ready) {}
@Override
protected void onResume() {
2018-08-02 13:25:33 +00:00
Log.i(TAG, "onResume()");
super.onResume();
if (networkAccess.isCensored(this)) {
ApplicationContext.getInstance(this).getJobManager().add(new PushNotificationReceiveJob(this));
}
}
@Override
protected void onPause() {
2018-08-02 13:25:33 +00:00
Log.i(TAG, "onPause()");
super.onPause();
}
@Override
protected void onDestroy() {
2018-08-02 13:25:33 +00:00
Log.i(TAG, "onDestroy()");
super.onDestroy();
removeClearKeyReceiver(this);
}
@Override
public void onMasterSecretCleared() {
2018-08-02 13:25:33 +00:00
Log.i(TAG, "onMasterSecretCleared()");
if (ApplicationContext.getInstance(this).isAppVisible()) routeApplicationState(true);
else finish();
}
protected <T extends Fragment> T initFragment(@IdRes int target,
2018-02-02 03:22:48 +00:00
@NonNull T fragment)
{
2018-02-02 03:22:48 +00:00
return initFragment(target, fragment, null);
}
protected <T extends Fragment> T initFragment(@IdRes int target,
@NonNull T fragment,
@Nullable Locale locale)
{
2018-02-02 03:22:48 +00:00
return initFragment(target, fragment, locale, null);
}
protected <T extends Fragment> T initFragment(@IdRes int target,
@NonNull T fragment,
@Nullable Locale locale,
@Nullable Bundle extras)
{
Bundle args = new Bundle();
args.putSerializable(LOCALE_EXTRA, locale);
if (extras != null) {
args.putAll(extras);
}
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(target, fragment)
.commitAllowingStateLoss();
return fragment;
}
2018-02-02 03:22:48 +00:00
private void routeApplicationState(boolean locked) {
Intent intent = getIntentForState(getApplicationState(locked));
if (intent != null) {
startActivity(intent);
finish();
}
}
2018-02-02 03:22:48 +00:00
private Intent getIntentForState(int state) {
2018-08-02 13:25:33 +00:00
Log.i(TAG, "routeApplicationState(), state: " + state);
switch (state) {
case STATE_CREATE_PASSPHRASE: return getCreatePassphraseIntent();
case STATE_PROMPT_PASSPHRASE: return getPromptPassphraseIntent();
2018-02-02 03:22:48 +00:00
case STATE_UPGRADE_DATABASE: return getUpgradeDatabaseIntent();
2019-02-13 19:52:55 +00:00
case STATE_WELCOME_SCREEN: return getWelcomeIntent();
2018-02-02 03:22:48 +00:00
case STATE_PROMPT_PUSH_REGISTRATION: return getPushRegistrationIntent();
case STATE_EXPERIENCE_UPGRADE: return getExperienceUpgradeIntent();
default: return null;
}
}
2018-02-02 03:22:48 +00:00
private int getApplicationState(boolean locked) {
if (!MasterSecretUtil.isPassphraseInitialized(this)) {
return STATE_CREATE_PASSPHRASE;
2018-02-02 03:22:48 +00:00
} else if (locked) {
return STATE_PROMPT_PASSPHRASE;
} else if (DatabaseUpgradeActivity.isUpdate(this)) {
return STATE_UPGRADE_DATABASE;
2019-02-13 19:52:55 +00:00
} else if (!TextSecurePreferences.hasSeenWelcomeScreen(this)) {
return STATE_WELCOME_SCREEN;
} else if (!TextSecurePreferences.hasPromptedPushRegistration(this)) {
return STATE_PROMPT_PUSH_REGISTRATION;
} else if (ExperienceUpgradeActivity.isUpdate(this)) {
return STATE_EXPERIENCE_UPGRADE;
} else {
return STATE_NORMAL;
}
}
private Intent getCreatePassphraseIntent() {
2018-02-02 03:22:48 +00:00
return getRoutedIntent(PassphraseCreateActivity.class, getIntent());
}
private Intent getPromptPassphraseIntent() {
2018-02-02 03:22:48 +00:00
return getRoutedIntent(PassphrasePromptActivity.class, getIntent());
}
2018-02-02 03:22:48 +00:00
private Intent getUpgradeDatabaseIntent() {
return getRoutedIntent(DatabaseUpgradeActivity.class,
TextSecurePreferences.hasPromptedPushRegistration(this)
? getConversationListIntent()
2018-02-02 03:22:48 +00:00
: getPushRegistrationIntent());
}
private Intent getExperienceUpgradeIntent() {
2018-02-02 03:22:48 +00:00
return getRoutedIntent(ExperienceUpgradeActivity.class, getIntent());
}
2019-02-13 19:52:55 +00:00
private Intent getWelcomeIntent() {
return getRoutedIntent(WelcomeActivity.class, getPushRegistrationIntent());
}
2018-02-02 03:22:48 +00:00
private Intent getPushRegistrationIntent() {
return getRoutedIntent(RegistrationActivity.class, getCreateProfileIntent());
}
2018-02-02 03:22:48 +00:00
private Intent getCreateProfileIntent() {
return getRoutedIntent(CreateProfileActivity.class, getConversationListIntent());
}
2018-02-02 03:22:48 +00:00
private Intent getRoutedIntent(Class<?> destination, @Nullable Intent nextIntent) {
final Intent intent = new Intent(this, destination);
if (nextIntent != null) intent.putExtra("next_intent", nextIntent);
return intent;
}
private Intent getConversationListIntent() {
return new Intent(this, ConversationListActivity.class);
}
private void initializeClearKeyReceiver() {
2018-08-02 13:25:33 +00:00
Log.i(TAG, "initializeClearKeyReceiver()");
this.clearKeyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
2018-08-02 13:25:33 +00:00
Log.i(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;
}
}
}