2013-02-11 01:30:51 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2014-06-17 03:32:54 +00:00
|
|
|
import android.app.Activity;
|
2013-02-11 01:30:51 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2014-06-17 03:32:54 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.view.WindowManager;
|
2013-02-11 01:30:51 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2013-02-11 01:30:51 +00:00
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2013-02-11 01:30:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
public class PassphraseRequiredMixin {
|
|
|
|
|
|
|
|
private BroadcastReceiver clearKeyReceiver;
|
|
|
|
private BroadcastReceiver newKeyReceiver;
|
|
|
|
|
2014-06-17 03:32:54 +00:00
|
|
|
public <T extends Activity & PassphraseRequiredActivity> void onCreate(T activity) {
|
|
|
|
initializeClearKeyReceiver(activity);
|
2013-02-11 01:30:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 03:32:54 +00:00
|
|
|
public <T extends Activity & PassphraseRequiredActivity> void onResume(T activity) {
|
|
|
|
initializeScreenshotSecurity(activity);
|
|
|
|
initializeNewKeyReceiver(activity);
|
2014-11-03 23:16:04 +00:00
|
|
|
initializeFromMasterSecret(activity);
|
2014-06-17 03:32:54 +00:00
|
|
|
KeyCachingService.registerPassphraseActivityStarted(activity);
|
2013-02-11 01:30:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 03:32:54 +00:00
|
|
|
public <T extends Activity & PassphraseRequiredActivity> void onPause(T activity) {
|
|
|
|
removeNewKeyReceiver(activity);
|
|
|
|
KeyCachingService.registerPassphraseActivityStopped(activity);
|
2013-02-11 01:30:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 03:32:54 +00:00
|
|
|
public <T extends Activity & PassphraseRequiredActivity> void onDestroy(T activity) {
|
|
|
|
removeClearKeyReceiver(activity);
|
2013-02-11 01:30:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 03:32:54 +00:00
|
|
|
private <T extends Activity & PassphraseRequiredActivity> void initializeScreenshotSecurity(T activity) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
|
|
|
if (TextSecurePreferences.isScreenSecurityEnabled(activity)) {
|
|
|
|
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
|
|
|
} else {
|
|
|
|
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private <T extends Activity & PassphraseRequiredActivity> void initializeClearKeyReceiver(final T activity) {
|
2013-02-11 01:30:51 +00:00
|
|
|
this.clearKeyReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
activity.onMasterSecretCleared();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter(KeyCachingService.CLEAR_KEY_EVENT);
|
2014-06-17 03:32:54 +00:00
|
|
|
|
|
|
|
activity.registerReceiver(clearKeyReceiver, filter, KeyCachingService.KEY_PERMISSION, null);
|
2013-02-11 01:30:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 03:32:54 +00:00
|
|
|
private <T extends Activity & PassphraseRequiredActivity> void initializeNewKeyReceiver(final T activity) {
|
2013-02-11 01:30:51 +00:00
|
|
|
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);
|
2014-06-17 03:32:54 +00:00
|
|
|
activity.registerReceiver(newKeyReceiver, filter, KeyCachingService.KEY_PERMISSION, null);
|
2013-02-11 01:30:51 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
private <T extends Activity & PassphraseRequiredActivity> void initializeFromMasterSecret(T activity) {
|
|
|
|
MasterSecret masterSecret = KeyCachingService.getMasterSecret(activity);
|
2013-02-11 01:30:51 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
if (masterSecret == null) {
|
|
|
|
activity.onMasterSecretCleared();
|
|
|
|
} else {
|
|
|
|
activity.onNewMasterSecret(masterSecret);
|
|
|
|
}
|
2013-02-11 01:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|