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