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.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Toast;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
@ -37,8 +39,6 @@ public class DecryptFilesActivity extends BaseActivity {
// intern
public static final String ACTION_DECRYPT_DATA_OPEN = Constants.INTENT_PREFIX + "DECRYPT_DATA_OPEN";
DecryptFilesFragment mFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -62,19 +62,12 @@ public class DecryptFilesActivity extends BaseActivity {
/**
* Handles all actions with this intent
*
* @param intent
*/
private void handleActions(Bundle savedInstanceState, Intent intent) {
String action = intent.getAction();
String type = intent.getType();
Uri uri = intent.getData();
Bundle mFileFragmentBundle = new Bundle();
/*
* Android's Action
*/
if (Intent.ACTION_SEND.equals(action) && type != null) {
// When sending to Keychain Decrypt via share menu
// Binary via content provider (could also be files)
@ -88,39 +81,27 @@ public class DecryptFilesActivity extends BaseActivity {
action = ACTION_DECRYPT_DATA;
}
/**
* 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.
// No need to initialize fragments if we are being restored
if (savedInstanceState != null) {
return;
}
// Create an instance of the fragment
mFragment = DecryptFilesFragment.newInstance(uri, openDialog);
// Definitely need a data uri with the decrypt_data intent
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
// NOTE: We use commitAllowingStateLoss() to prevent weird crashes!
getSupportFragmentManager().beginTransaction()
.replace(R.id.decrypt_files_fragment_container, mFragment)
.replace(R.id.decrypt_files_fragment_container, frag)
.commitAllowingStateLoss();
// do it immediately!
getSupportFragmentManager().executePendingTransactions();
}
}

View File

@ -111,18 +111,26 @@ public class DecryptFilesFragment extends DecryptFragment {
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(ARG_URI, mInputUri);
}
@Override
public void onActivityCreated(Bundle 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) {
FileHelper.openDocument(DecryptFilesFragment.this, "*/*", REQUEST_CODE_INPUT);
} else {
FileHelper.openFile(DecryptFilesFragment.this, mInputUri, "*/*",
REQUEST_CODE_INPUT);
FileHelper.openFile(DecryptFilesFragment.this, mInputUri, "*/*", REQUEST_CODE_INPUT);
}
}
}