Added boilerplate bundle encryption code to HelloWorldActivity.

It would be nice to be able to put this code in a superclass, since in
the case of an activity like this it only deals with superclass state.
This commit is contained in:
akwizgran
2013-02-19 00:54:21 +00:00
parent d136964bea
commit 020e4df8d9
2 changed files with 26 additions and 4 deletions

View File

@@ -8,9 +8,13 @@ import static java.util.logging.Level.INFO;
import java.util.logging.Logger; import java.util.logging.Logger;
import com.google.inject.Inject;
import com.google.inject.Provider;
import net.sf.briar.R; import net.sf.briar.R;
import net.sf.briar.android.BriarService; import net.sf.briar.android.BriarService;
import net.sf.briar.android.invitation.AddContactActivity; import net.sf.briar.android.invitation.AddContactActivity;
import net.sf.briar.api.android.BundleEncrypter;
import roboguice.activity.RoboActivity; import roboguice.activity.RoboActivity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
@@ -27,8 +31,14 @@ implements OnClickListener {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(HelloWorldActivity.class.getName()); Logger.getLogger(HelloWorldActivity.class.getName());
@Inject private static Provider<BundleEncrypter> bundleEncrypterProvider;
private final BundleEncrypter bundleEncrypter =
bundleEncrypterProvider.get();
@Override @Override
public void onCreate(Bundle state) { public void onCreate(Bundle state) {
if(state != null && !bundleEncrypter.decrypt(state)) state = null;
super.onCreate(state); super.onCreate(state);
if(LOG.isLoggable(INFO)) LOG.info("Created"); if(LOG.isLoggable(INFO)) LOG.info("Created");
LinearLayout layout = new LinearLayout(this); LinearLayout layout = new LinearLayout(this);
@@ -60,6 +70,18 @@ implements OnClickListener {
startService(new Intent(BriarService.class.getName())); startService(new Intent(BriarService.class.getName()));
} }
@Override
public void onRestoreInstanceState(Bundle state) {
if(bundleEncrypter.decrypt(state))
super.onRestoreInstanceState(state);
}
@Override
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
bundleEncrypter.encrypt(state);
}
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();

View File

@@ -53,13 +53,13 @@ implements InvitationListener {
@Override @Override
public void onCreate(Bundle state) { public void onCreate(Bundle state) {
if(state == null || !bundleEncrypter.decrypt(state)) { if(state != null && !bundleEncrypter.decrypt(state)) state = null;
// This is a new activity or the process has restarted super.onCreate(state);
super.onCreate(null); if(state == null) {
// This is a new activity or the app has restarted
setView(new NetworkSetupView(this)); setView(new NetworkSetupView(this));
} else { } else {
// Restore the activity's state // Restore the activity's state
super.onCreate(state);
networkName = state.getString("net.sf.briar.NETWORK_NAME"); networkName = state.getString("net.sf.briar.NETWORK_NAME");
useBluetooth = state.getBoolean("net.sf.briar.USE_BLUETOOTH"); useBluetooth = state.getBoolean("net.sf.briar.USE_BLUETOOTH");
taskHandle = state.getLong("net.sf.briar.TASK_HANDLE", -1); taskHandle = state.getLong("net.sf.briar.TASK_HANDLE", -1);