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;
|
2013-02-11 02:30:51 +01:00
|
|
|
import android.os.Bundle;
|
2020-12-15 06:44:38 +01:00
|
|
|
|
2020-08-19 02:06:26 +02:00
|
|
|
import androidx.annotation.IdRes;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.fragment.app.Fragment;
|
2013-02-11 02:30:51 +01:00
|
|
|
|
2021-05-18 01:12:33 +02:00
|
|
|
import org.session.libsignal.utilities.Log;
|
2021-07-09 03:14:21 +02:00
|
|
|
import org.thoughtcrime.securesms.home.HomeActivity;
|
|
|
|
import org.thoughtcrime.securesms.onboarding.LandingActivity;
|
2014-12-15 21:25:55 +01:00
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
2021-01-15 06:51:53 +01:00
|
|
|
import org.session.libsession.utilities.TextSecurePreferences;
|
2013-02-11 02:30:51 +01:00
|
|
|
|
2015-03-19 21:08:48 +01:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2020-12-15 06:44:38 +01:00
|
|
|
//TODO AC: Rename to ScreenLockActionBarActivity.
|
2020-12-07 03:45:36 +01:00
|
|
|
public abstract class PassphraseRequiredActionBarActivity extends BaseActionBarActivity {
|
2014-12-15 21:25:55 +01:00
|
|
|
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;
|
2020-12-15 06:44:38 +01:00
|
|
|
private static final int STATE_PROMPT_PASSPHRASE = 1; //TODO AC: Rename to STATE_SCREEN_LOCKED
|
|
|
|
private static final int STATE_UPGRADE_DATABASE = 2; //TODO AC: Rename to STATE_MIGRATE_DATA
|
|
|
|
private static final int STATE_WELCOME_SCREEN = 3;
|
2014-12-15 21:25:55 +01:00
|
|
|
|
2016-12-30 05:54:05 +01:00
|
|
|
private BroadcastReceiver clearKeyReceiver;
|
2013-02-11 02:30:51 +01:00
|
|
|
|
|
|
|
@Override
|
2014-12-15 21:25:55 +01:00
|
|
|
protected final void onCreate(Bundle savedInstanceState) {
|
2018-08-02 15:25:33 +02:00
|
|
|
Log.i(TAG, "onCreate(" + savedInstanceState + ")");
|
2014-12-15 21:25:55 +01:00
|
|
|
onPreCreate();
|
2018-02-02 04:22:48 +01:00
|
|
|
|
2020-12-15 01:31:09 +01:00
|
|
|
final boolean locked = KeyCachingService.isLocked(this) &&
|
|
|
|
TextSecurePreferences.isScreenLockEnabled(this) &&
|
|
|
|
TextSecurePreferences.getLocalNumber(this) != null;
|
2018-02-02 04:22:48 +01:00
|
|
|
routeApplicationState(locked);
|
|
|
|
|
2013-02-11 02:30:51 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
2018-02-02 04:22:48 +01:00
|
|
|
|
2014-12-15 21:25:55 +01:00
|
|
|
if (!isFinishing()) {
|
|
|
|
initializeClearKeyReceiver();
|
2018-02-02 04:22:48 +01:00
|
|
|
onCreate(savedInstanceState, true);
|
2014-12-15 21:25:55 +01:00
|
|
|
}
|
2013-02-11 02:30:51 +01:00
|
|
|
}
|
|
|
|
|
2014-12-15 21:25:55 +01:00
|
|
|
protected void onPreCreate() {}
|
2018-02-02 04:22:48 +01:00
|
|
|
protected void onCreate(Bundle savedInstanceState, boolean ready) {}
|
2014-12-15 21:25:55 +01:00
|
|
|
|
2013-02-11 02:30:51 +01:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
2018-08-02 15:25:33 +02:00
|
|
|
Log.i(TAG, "onPause()");
|
2013-02-11 02:30:51 +01:00
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2018-08-02 15:25:33 +02:00
|
|
|
Log.i(TAG, "onDestroy()");
|
2013-02-11 02:30:51 +01:00
|
|
|
super.onDestroy();
|
2014-12-15 21:25:55 +01:00
|
|
|
removeClearKeyReceiver(this);
|
2013-02-11 02:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onMasterSecretCleared() {
|
2018-08-02 15:25:33 +02:00
|
|
|
Log.i(TAG, "onMasterSecretCleared()");
|
2018-10-15 22:27:21 +02:00
|
|
|
if (ApplicationContext.getInstance(this).isAppVisible()) routeApplicationState(true);
|
|
|
|
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,
|
2018-02-02 04:22:48 +01:00
|
|
|
@NonNull T fragment)
|
2015-06-09 16:37:20 +02:00
|
|
|
{
|
2018-02-02 04:22:48 +01:00
|
|
|
return initFragment(target, fragment, null);
|
2015-06-09 16:37:20 +02:00
|
|
|
}
|
|
|
|
|
2014-12-15 21:25:55 +01:00
|
|
|
protected <T extends Fragment> T initFragment(@IdRes int target,
|
|
|
|
@NonNull T fragment,
|
2015-06-09 16:37:20 +02:00
|
|
|
@Nullable Locale locale)
|
|
|
|
{
|
2018-02-02 04:22:48 +01:00
|
|
|
return initFragment(target, fragment, locale, null);
|
2015-06-09 16:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected <T extends Fragment> T initFragment(@IdRes int target,
|
|
|
|
@NonNull T fragment,
|
|
|
|
@Nullable Locale locale,
|
|
|
|
@Nullable Bundle extras)
|
|
|
|
{
|
2014-12-15 21:25:55 +01:00
|
|
|
Bundle args = new Bundle();
|
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)
|
2017-09-21 03:14:28 +02:00
|
|
|
.commitAllowingStateLoss();
|
2014-12-15 21:25:55 +01:00
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2018-02-02 04:22:48 +01:00
|
|
|
private void routeApplicationState(boolean locked) {
|
|
|
|
Intent intent = getIntentForState(getApplicationState(locked));
|
2014-12-15 21:25:55 +01:00
|
|
|
if (intent != null) {
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 04:22:48 +01:00
|
|
|
private Intent getIntentForState(int state) {
|
2018-08-02 15:25:33 +02:00
|
|
|
Log.i(TAG, "routeApplicationState(), state: " + state);
|
2014-12-15 21:25:55 +01:00
|
|
|
|
|
|
|
switch (state) {
|
2015-09-29 22:14:22 +02:00
|
|
|
case STATE_PROMPT_PASSPHRASE: return getPromptPassphraseIntent();
|
2018-02-02 04:22:48 +01:00
|
|
|
case STATE_UPGRADE_DATABASE: return getUpgradeDatabaseIntent();
|
2019-02-13 20:52:55 +01:00
|
|
|
case STATE_WELCOME_SCREEN: return getWelcomeIntent();
|
2015-09-29 22:14:22 +02:00
|
|
|
default: return null;
|
2014-12-15 21:25:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 04:22:48 +01:00
|
|
|
private int getApplicationState(boolean locked) {
|
2020-12-14 08:16:16 +01:00
|
|
|
if (locked) {
|
2014-12-15 21:25:55 +01:00
|
|
|
return STATE_PROMPT_PASSPHRASE;
|
|
|
|
} else if (DatabaseUpgradeActivity.isUpdate(this)) {
|
|
|
|
return STATE_UPGRADE_DATABASE;
|
2019-02-13 20:52:55 +01:00
|
|
|
} else if (!TextSecurePreferences.hasSeenWelcomeScreen(this)) {
|
|
|
|
return STATE_WELCOME_SCREEN;
|
2020-12-15 06:44:38 +01:00
|
|
|
} else {
|
2014-12-15 21:25:55 +01:00
|
|
|
return STATE_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Intent getPromptPassphraseIntent() {
|
2018-02-02 04:22:48 +01:00
|
|
|
return getRoutedIntent(PassphrasePromptActivity.class, getIntent());
|
2014-12-15 21:25:55 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 04:22:48 +01:00
|
|
|
private Intent getUpgradeDatabaseIntent() {
|
2020-12-15 06:44:38 +01:00
|
|
|
return getRoutedIntent(DatabaseUpgradeActivity.class, getConversationListIntent());
|
2015-09-29 22:14:22 +02:00
|
|
|
}
|
|
|
|
|
2019-02-13 20:52:55 +01:00
|
|
|
private Intent getWelcomeIntent() {
|
2020-12-15 06:44:38 +01:00
|
|
|
return getRoutedIntent(LandingActivity.class, getConversationListIntent());
|
2019-02-13 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
2020-12-15 06:44:38 +01:00
|
|
|
private Intent getConversationListIntent() {
|
|
|
|
return new Intent(this, HomeActivity.class);
|
2014-12-15 21:25:55 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 04:22:48 +01:00
|
|
|
private Intent getRoutedIntent(Class<?> destination, @Nullable Intent nextIntent) {
|
2014-12-15 21:25:55 +01:00
|
|
|
final Intent intent = new Intent(this, destination);
|
|
|
|
if (nextIntent != null) intent.putExtra("next_intent", nextIntent);
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeClearKeyReceiver() {
|
2018-08-02 15:25:33 +02:00
|
|
|
Log.i(TAG, "initializeClearKeyReceiver()");
|
2014-12-15 21:25:55 +01:00
|
|
|
this.clearKeyReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2018-08-02 15:25:33 +02:00
|
|
|
Log.i(TAG, "onReceive() for clear key event");
|
2014-12-15 21:25:55 +01: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-11 02:30:51 +01:00
|
|
|
}
|