preserve state in DecryptFilesActivity/-Fragment

This commit is contained in:
Vincent Breitmoser 2015-05-29 21:38:20 +02:00
parent c9f9af6603
commit 8be6450a36
2 changed files with 26 additions and 37 deletions

View File

@ -21,7 +21,9 @@ import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View; import android.view.View;
import android.widget.Toast;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
@ -37,8 +39,6 @@ public class DecryptFilesActivity extends BaseActivity {
// intern // intern
public static final String ACTION_DECRYPT_DATA_OPEN = Constants.INTENT_PREFIX + "DECRYPT_DATA_OPEN"; public static final String ACTION_DECRYPT_DATA_OPEN = Constants.INTENT_PREFIX + "DECRYPT_DATA_OPEN";
DecryptFilesFragment mFragment;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -62,19 +62,12 @@ public class DecryptFilesActivity extends BaseActivity {
/** /**
* Handles all actions with this intent * Handles all actions with this intent
*
* @param intent
*/ */
private void handleActions(Bundle savedInstanceState, Intent intent) { private void handleActions(Bundle savedInstanceState, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
String type = intent.getType(); String type = intent.getType();
Uri uri = intent.getData(); Uri uri = intent.getData();
Bundle mFileFragmentBundle = new Bundle();
/*
* Android's Action
*/
if (Intent.ACTION_SEND.equals(action) && type != null) { if (Intent.ACTION_SEND.equals(action) && type != null) {
// When sending to Keychain Decrypt via share menu // When sending to Keychain Decrypt via share menu
// Binary via content provider (could also be files) // Binary via content provider (could also be files)
@ -88,39 +81,27 @@ public class DecryptFilesActivity extends BaseActivity {
action = ACTION_DECRYPT_DATA; action = ACTION_DECRYPT_DATA;
} }
/** // No need to initialize fragments if we are being restored
* Main Actions
*/
if (ACTION_DECRYPT_DATA.equals(action) && uri != null) {
mFileFragmentBundle.putParcelable(DecryptFilesFragment.ARG_URI, uri);
loadFragment(savedInstanceState, uri, false);
} else if (ACTION_DECRYPT_DATA_OPEN.equals(action)) {
loadFragment(savedInstanceState, null, true);
} else if (ACTION_DECRYPT_DATA.equals(action)) {
Log.e(Constants.TAG,
"Include an Uri with setInputData() in your Intent!");
}
}
private void loadFragment(Bundle savedInstanceState, Uri uri, boolean openDialog) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) { if (savedInstanceState != null) {
return; return;
} }
// Create an instance of the fragment // Definitely need a data uri with the decrypt_data intent
mFragment = DecryptFilesFragment.newInstance(uri, openDialog); if (ACTION_DECRYPT_DATA.equals(action) && uri == null) {
Toast.makeText(this, "No data to decrypt!", Toast.LENGTH_LONG).show();
setResult(Activity.RESULT_CANCELED);
finish();
}
boolean showOpenDialog = ACTION_DECRYPT_DATA_OPEN.equals(action);
DecryptFilesFragment frag = DecryptFilesFragment.newInstance(uri, showOpenDialog);
// Add the fragment to the 'fragment_container' FrameLayout // Add the fragment to the 'fragment_container' FrameLayout
// NOTE: We use commitAllowingStateLoss() to prevent weird crashes! // NOTE: We use commitAllowingStateLoss() to prevent weird crashes!
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.decrypt_files_fragment_container, mFragment) .replace(R.id.decrypt_files_fragment_container, frag)
.commitAllowingStateLoss(); .commitAllowingStateLoss();
// do it immediately!
getSupportFragmentManager().executePendingTransactions();
} }
} }

View File

@ -111,18 +111,26 @@ public class DecryptFilesFragment extends DecryptFragment {
return view; return view;
} }
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(ARG_URI, mInputUri);
}
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
setInputUri(getArguments().<Uri>getParcelable(ARG_URI)); Bundle state = savedInstanceState != null ? savedInstanceState : getArguments();
setInputUri(state.<Uri>getParcelable(ARG_URI));
if (getArguments().getBoolean(ARG_OPEN_DIRECTLY, false)) { // should only come from args
if (state.getBoolean(ARG_OPEN_DIRECTLY, false)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
FileHelper.openDocument(DecryptFilesFragment.this, "*/*", REQUEST_CODE_INPUT); FileHelper.openDocument(DecryptFilesFragment.this, "*/*", REQUEST_CODE_INPUT);
} else { } else {
FileHelper.openFile(DecryptFilesFragment.this, mInputUri, "*/*", FileHelper.openFile(DecryptFilesFragment.this, mInputUri, "*/*", REQUEST_CODE_INPUT);
REQUEST_CODE_INPUT);
} }
} }
} }