save instance state in DecryptListFragment
This commit is contained in:
parent
5e5febaee3
commit
d1ceb73eb0
|
@ -69,11 +69,16 @@ import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||||
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
|
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
|
||||||
import org.sufficientlysecure.keychain.util.FileHelper;
|
import org.sufficientlysecure.keychain.util.FileHelper;
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
|
import org.sufficientlysecure.keychain.util.ParcelableHashMap;
|
||||||
|
|
||||||
|
|
||||||
public class DecryptListFragment
|
public class DecryptListFragment
|
||||||
extends CryptoOperationFragment<PgpDecryptVerifyInputParcel,DecryptVerifyResult>
|
extends CryptoOperationFragment<PgpDecryptVerifyInputParcel,DecryptVerifyResult>
|
||||||
implements OnMenuItemClickListener {
|
implements OnMenuItemClickListener {
|
||||||
public static final String ARG_URIS = "uris";
|
|
||||||
|
public static final String ARG_INPUT_URIS = "input_uris";
|
||||||
|
public static final String ARG_OUTPUT_URIS = "output_uris";
|
||||||
|
public static final String ARG_RESULTS = "results";
|
||||||
|
|
||||||
private static final int REQUEST_CODE_OUTPUT = 0x00007007;
|
private static final int REQUEST_CODE_OUTPUT = 0x00007007;
|
||||||
|
|
||||||
|
@ -92,7 +97,7 @@ public class DecryptListFragment
|
||||||
DecryptListFragment frag = new DecryptListFragment();
|
DecryptListFragment frag = new DecryptListFragment();
|
||||||
|
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
args.putParcelableArrayList(ARG_URIS, uris);
|
args.putParcelableArrayList(ARG_INPUT_URIS, uris);
|
||||||
frag.setArguments(args);
|
frag.setArguments(args);
|
||||||
|
|
||||||
return frag;
|
return frag;
|
||||||
|
@ -123,14 +128,59 @@ public class DecryptListFragment
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
super.onSaveInstanceState(outState);
|
super.onSaveInstanceState(outState);
|
||||||
|
|
||||||
outState.putParcelableArrayList(ARG_URIS, mInputUris);
|
outState.putParcelableArrayList(ARG_INPUT_URIS, mInputUris);
|
||||||
|
|
||||||
|
HashMap<Uri,DecryptVerifyResult> results = new HashMap<>(mInputUris.size());
|
||||||
|
for (Uri uri : mInputUris) {
|
||||||
|
if (mPendingInputUris.contains(uri)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
DecryptVerifyResult result = mAdapter.getItemResult(uri);
|
||||||
|
if (result != null) {
|
||||||
|
results.put(uri, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
outState.putParcelable(ARG_RESULTS, new ParcelableHashMap<>(results));
|
||||||
|
outState.putParcelable(ARG_OUTPUT_URIS, new ParcelableHashMap<>(mOutputUris));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
|
||||||
displayInputUris(getArguments().<Uri>getParcelableArrayList(ARG_URIS));
|
Bundle args = savedInstanceState != null ? savedInstanceState : getArguments();
|
||||||
|
|
||||||
|
ArrayList<Uri> inputUris = getArguments().getParcelableArrayList(ARG_INPUT_URIS);
|
||||||
|
ParcelableHashMap<Uri,Uri> outputUris = args.getParcelable(ARG_OUTPUT_URIS);
|
||||||
|
ParcelableHashMap<Uri,DecryptVerifyResult> results = args.getParcelable(ARG_RESULTS);
|
||||||
|
|
||||||
|
displayInputUris(inputUris,
|
||||||
|
outputUris != null ? outputUris.getMap() : null,
|
||||||
|
results != null ? results.getMap() : null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void displayInputUris(ArrayList<Uri> inputUris, HashMap<Uri,Uri> outputUris,
|
||||||
|
HashMap<Uri,DecryptVerifyResult> results) {
|
||||||
|
|
||||||
|
mInputUris = inputUris;
|
||||||
|
mOutputUris = outputUris != null ? outputUris : new HashMap<Uri,Uri>(inputUris.size());
|
||||||
|
|
||||||
|
mPendingInputUris = new ArrayList<>();
|
||||||
|
|
||||||
|
for (Uri uri : inputUris) {
|
||||||
|
mAdapter.add(uri);
|
||||||
|
if (results != null && results.containsKey(uri)) {
|
||||||
|
processResult(uri, results.get(uri));
|
||||||
|
} else {
|
||||||
|
mOutputUris.put(uri, TemporaryStorageProvider.createFile(getActivity()));
|
||||||
|
mPendingInputUris.add(uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cryptoOperation();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String removeEncryptedAppend(String name) {
|
private String removeEncryptedAppend(String name) {
|
||||||
|
@ -175,19 +225,6 @@ public class DecryptListFragment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void displayInputUris(ArrayList<Uri> uris) {
|
|
||||||
mInputUris = uris;
|
|
||||||
mOutputUris = new HashMap<>(uris.size());
|
|
||||||
for (Uri uri : uris) {
|
|
||||||
mAdapter.add(uri);
|
|
||||||
mOutputUris.put(uri, TemporaryStorageProvider.createFile(getActivity()));
|
|
||||||
}
|
|
||||||
|
|
||||||
mPendingInputUris = uris;
|
|
||||||
|
|
||||||
cryptoOperation();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void cryptoOperation(CryptoInputParcel cryptoInput) {
|
protected void cryptoOperation(CryptoInputParcel cryptoInput) {
|
||||||
super.cryptoOperation(cryptoInput, false);
|
super.cryptoOperation(cryptoInput, false);
|
||||||
|
@ -216,9 +253,16 @@ public class DecryptListFragment
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCryptoOperationSuccess(DecryptVerifyResult result) {
|
protected void onCryptoOperationSuccess(DecryptVerifyResult result) {
|
||||||
final Uri uri = mCurrentInputUri;
|
Uri uri = mCurrentInputUri;
|
||||||
mCurrentInputUri = null;
|
mCurrentInputUri = null;
|
||||||
|
|
||||||
|
processResult(uri, result);
|
||||||
|
|
||||||
|
cryptoOperation();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processResult(final Uri uri, DecryptVerifyResult result) {
|
||||||
|
|
||||||
Drawable icon = null;
|
Drawable icon = null;
|
||||||
OnClickListener onFileClick = null, onKeyClick = null;
|
OnClickListener onFileClick = null, onKeyClick = null;
|
||||||
|
|
||||||
|
@ -256,7 +300,7 @@ public class DecryptListFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri outputUri = mOutputUris.get(uri);
|
Uri outputUri = mOutputUris.get(uri);
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||||
intent.setDataAndType(outputUri, metadata.getMimeType());
|
intent.setDataAndType(outputUri, metadata.getMimeType());
|
||||||
activity.startActivity(intent);
|
activity.startActivity(intent);
|
||||||
}
|
}
|
||||||
|
@ -265,8 +309,6 @@ public class DecryptListFragment
|
||||||
|
|
||||||
mAdapter.addResult(uri, result, icon, onFileClick, onKeyClick);
|
mAdapter.addResult(uri, result, icon, onFileClick, onKeyClick);
|
||||||
|
|
||||||
cryptoOperation();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -380,11 +422,15 @@ public class DecryptListFragment
|
||||||
// Depends on inputUri only
|
// Depends on inputUri only
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
ViewModel viewModel = (ViewModel) o;
|
ViewModel viewModel = (ViewModel) o;
|
||||||
return !(mResult != null ? !mResult.equals(viewModel.mResult)
|
return !(mInputUri != null ? !mInputUri.equals(viewModel.mInputUri)
|
||||||
: viewModel.mResult != null);
|
: viewModel.mInputUri != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Depends on inputUri only
|
// Depends on inputUri only
|
||||||
|
@ -439,7 +485,7 @@ public class DecryptListFragment
|
||||||
holder.vFilesize.setText(FileHelper.readableFileSize(size));
|
holder.vFilesize.setText(FileHelper.readableFileSize(size));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO thumbnail from OpenPgpMetadata
|
// TODO thumbnail from OpenPgpMetadata?
|
||||||
if (model.mIcon != null) {
|
if (model.mIcon != null) {
|
||||||
holder.vThumbnail.setImageDrawable(model.mIcon);
|
holder.vThumbnail.setImageDrawable(model.mIcon);
|
||||||
} else {
|
} else {
|
||||||
|
@ -485,6 +531,17 @@ public class DecryptListFragment
|
||||||
return mDataset.size();
|
return mDataset.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DecryptVerifyResult getItemResult(Uri uri) {
|
||||||
|
ViewModel model = new ViewModel(mContext, uri);
|
||||||
|
int pos = mDataset.indexOf(model);
|
||||||
|
if (pos == -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
model = mDataset.get(pos);
|
||||||
|
|
||||||
|
return model.mResult;
|
||||||
|
}
|
||||||
|
|
||||||
public void add(Uri uri) {
|
public void add(Uri uri) {
|
||||||
ViewModel newModel = new ViewModel(mContext, uri);
|
ViewModel newModel = new ViewModel(mContext, uri);
|
||||||
mDataset.add(newModel);
|
mDataset.add(newModel);
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package org.sufficientlysecure.keychain.util;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import org.sufficientlysecure.keychain.KeychainApplication;
|
||||||
|
|
||||||
|
|
||||||
|
public class ParcelableHashMap <K extends Parcelable, V extends Parcelable> implements Parcelable {
|
||||||
|
|
||||||
|
HashMap<K,V> mInner;
|
||||||
|
|
||||||
|
public ParcelableHashMap(HashMap<K,V> inner) {
|
||||||
|
mInner = inner;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ParcelableHashMap(@NonNull Parcel in) {
|
||||||
|
mInner = new HashMap<>();
|
||||||
|
ClassLoader loader = KeychainApplication.class.getClassLoader();
|
||||||
|
|
||||||
|
int num = in.readInt();
|
||||||
|
while (num-- > 0) {
|
||||||
|
K key = in.readParcelable(loader);
|
||||||
|
V val = in.readParcelable(loader);
|
||||||
|
mInner.put(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashMap<K,V> getMap() {
|
||||||
|
return mInner;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToParcel(Parcel parcel, int flags) {
|
||||||
|
parcel.writeInt(mInner.size());
|
||||||
|
for (HashMap.Entry<K,V> entry : mInner.entrySet()) {
|
||||||
|
parcel.writeParcelable(entry.getKey(), 0);
|
||||||
|
parcel.writeParcelable(entry.getValue(), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Creator<ParcelableHashMap> CREATOR = new Creator<ParcelableHashMap>() {
|
||||||
|
@Override
|
||||||
|
public ParcelableHashMap createFromParcel(Parcel in) {
|
||||||
|
return new ParcelableHashMap(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ParcelableHashMap[] newArray(int size) {
|
||||||
|
return new ParcelableHashMap[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int describeContents() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue