ImportKeys: Add import single key from file
- Refactoring needed
This commit is contained in:
@@ -37,6 +37,7 @@ import java.util.HashSet;
|
|||||||
public class ImportKeysListEntry implements Serializable, Parcelable {
|
public class ImportKeysListEntry implements Serializable, Parcelable {
|
||||||
private static final long serialVersionUID = -7797972103284992662L;
|
private static final long serialVersionUID = -7797972103284992662L;
|
||||||
|
|
||||||
|
private byte[] mEncodedRing;
|
||||||
private ArrayList<String> mUserIds;
|
private ArrayList<String> mUserIds;
|
||||||
private HashMap<String, HashSet<String>> mMergedUserIds;
|
private HashMap<String, HashSet<String>> mMergedUserIds;
|
||||||
private long mKeyId;
|
private long mKeyId;
|
||||||
@@ -140,6 +141,10 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
|
|||||||
mExpired = expired;
|
mExpired = expired;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] getEncodedRing() {
|
||||||
|
return mEncodedRing;
|
||||||
|
}
|
||||||
|
|
||||||
public long getKeyId() {
|
public long getKeyId() {
|
||||||
return mKeyId;
|
return mKeyId;
|
||||||
}
|
}
|
||||||
@@ -272,7 +277,8 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
|
|||||||
* Constructor based on key object, used for import from NFC, QR Codes, files
|
* Constructor based on key object, used for import from NFC, QR Codes, files
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public ImportKeysListEntry(Context context, UncachedKeyRing ring) {
|
public ImportKeysListEntry(Context context, UncachedKeyRing ring, byte[] encodedRing) {
|
||||||
|
mEncodedRing = encodedRing;
|
||||||
mSecretKey = ring.isSecret();
|
mSecretKey = ring.isSecret();
|
||||||
UncachedPublicKey key = ring.getPublicKey();
|
UncachedPublicKey key = ring.getPublicKey();
|
||||||
|
|
||||||
|
|||||||
@@ -18,13 +18,6 @@
|
|||||||
package org.sufficientlysecure.keychain.keyimport.processing;
|
package org.sufficientlysecure.keychain.keyimport.processing;
|
||||||
|
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v4.content.AsyncTaskLoader;
|
import android.support.v4.content.AsyncTaskLoader;
|
||||||
@@ -44,6 +37,13 @@ import org.sufficientlysecure.keychain.util.InputData;
|
|||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
import org.sufficientlysecure.keychain.util.PositionAwareInputStream;
|
import org.sufficientlysecure.keychain.util.PositionAwareInputStream;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class ImportKeysListLoader
|
public class ImportKeysListLoader
|
||||||
extends AsyncTaskLoader<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> {
|
extends AsyncTaskLoader<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> {
|
||||||
|
|
||||||
@@ -117,7 +117,9 @@ public class ImportKeysListLoader
|
|||||||
return mParcelableRings;
|
return mParcelableRings;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Reads all PGPKeyRing objects from the bytes of an InputData object. */
|
/**
|
||||||
|
* Reads all PGPKeyRing objects from the bytes of an InputData object.
|
||||||
|
*/
|
||||||
private void generateListOfKeyrings(InputData inputData) {
|
private void generateListOfKeyrings(InputData inputData) {
|
||||||
PositionAwareInputStream progressIn = new PositionAwareInputStream(
|
PositionAwareInputStream progressIn = new PositionAwareInputStream(
|
||||||
inputData.getInputStream());
|
inputData.getInputStream());
|
||||||
@@ -131,9 +133,10 @@ public class ImportKeysListLoader
|
|||||||
IteratorWithIOThrow<UncachedKeyRing> it = UncachedKeyRing.fromStream(bufferedInput);
|
IteratorWithIOThrow<UncachedKeyRing> it = UncachedKeyRing.fromStream(bufferedInput);
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
UncachedKeyRing ring = it.next();
|
UncachedKeyRing ring = it.next();
|
||||||
ImportKeysListEntry item = new ImportKeysListEntry(getContext(), ring);
|
byte[] encodedRing = ring.getEncoded();
|
||||||
|
ImportKeysListEntry item = new ImportKeysListEntry(getContext(), ring, encodedRing);
|
||||||
mData.add(item);
|
mData.add(item);
|
||||||
mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(ring.getEncoded()));
|
mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(encodedRing));
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(Constants.TAG, "IOException on parsing key file! Return NoValidKeysException!", e);
|
Log.e(Constants.TAG, "IOException on parsing key file! Return NoValidKeysException!", e);
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ import org.sufficientlysecure.keychain.keyimport.processing.BytesLoaderState;
|
|||||||
import org.sufficientlysecure.keychain.keyimport.processing.CloudLoaderState;
|
import org.sufficientlysecure.keychain.keyimport.processing.CloudLoaderState;
|
||||||
import org.sufficientlysecure.keychain.keyimport.processing.ImportKeysListCloudLoader;
|
import org.sufficientlysecure.keychain.keyimport.processing.ImportKeysListCloudLoader;
|
||||||
import org.sufficientlysecure.keychain.keyimport.processing.ImportKeysListLoader;
|
import org.sufficientlysecure.keychain.keyimport.processing.ImportKeysListLoader;
|
||||||
|
import org.sufficientlysecure.keychain.keyimport.processing.ImportKeysListener;
|
||||||
import org.sufficientlysecure.keychain.keyimport.processing.LoaderState;
|
import org.sufficientlysecure.keychain.keyimport.processing.LoaderState;
|
||||||
import org.sufficientlysecure.keychain.operations.results.GetKeyResult;
|
import org.sufficientlysecure.keychain.operations.results.GetKeyResult;
|
||||||
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
|
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
|
||||||
@@ -70,6 +71,7 @@ public class ImportKeysListFragment extends Fragment implements
|
|||||||
public static final String ARG_CLOUD_SEARCH_PREFS = "cloud_search_prefs";
|
public static final String ARG_CLOUD_SEARCH_PREFS = "cloud_search_prefs";
|
||||||
|
|
||||||
private FragmentActivity mActivity;
|
private FragmentActivity mActivity;
|
||||||
|
private ImportKeysListener mCallback;
|
||||||
|
|
||||||
private ImportKeysListFragmentBinding binding;
|
private ImportKeysListFragmentBinding binding;
|
||||||
private ParcelableProxy mParcelableProxy;
|
private ParcelableProxy mParcelableProxy;
|
||||||
@@ -200,7 +202,7 @@ public class ImportKeysListFragment extends Fragment implements
|
|||||||
mRecyclerView.setLayoutManager(layoutManager);
|
mRecyclerView.setLayoutManager(layoutManager);
|
||||||
|
|
||||||
// Create an empty adapter we will use to display the loaded data.
|
// Create an empty adapter we will use to display the loaded data.
|
||||||
mAdapter = new ImportKeysAdapter(mActivity, nonInteractive);
|
mAdapter = new ImportKeysAdapter(mActivity, mCallback, nonInteractive);
|
||||||
mRecyclerView.setAdapter(mAdapter);
|
mRecyclerView.setAdapter(mAdapter);
|
||||||
|
|
||||||
if (dataUri != null || bytes != null) {
|
if (dataUri != null || bytes != null) {
|
||||||
@@ -222,6 +224,18 @@ public class ImportKeysListFragment extends Fragment implements
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Activity activity) {
|
||||||
|
super.onAttach(activity);
|
||||||
|
|
||||||
|
try {
|
||||||
|
mCallback = (ImportKeysListener) activity;
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
throw new ClassCastException(activity.toString()
|
||||||
|
+ " must implement ImportKeysListener");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRequestPermissionsResult(int requestCode,
|
public void onRequestPermissionsResult(int requestCode,
|
||||||
@NonNull String[] permissions,
|
@NonNull String[] permissions,
|
||||||
@@ -297,6 +311,8 @@ public class ImportKeysListFragment extends Fragment implements
|
|||||||
) {
|
) {
|
||||||
ArrayList<ImportKeysListEntry> result = data.getResult();
|
ArrayList<ImportKeysListEntry> result = data.getResult();
|
||||||
binding.setStatus(result.size() > 0 ? STATUS_LOADED : STATUS_EMPTY);
|
binding.setStatus(result.size() > 0 ? STATUS_LOADED : STATUS_EMPTY);
|
||||||
|
|
||||||
|
mAdapter.setLoaderState(mLoaderState);
|
||||||
mAdapter.setData(result);
|
mAdapter.setData(result);
|
||||||
|
|
||||||
// free old cached key data
|
// free old cached key data
|
||||||
|
|||||||
@@ -19,12 +19,11 @@ package org.sufficientlysecure.keychain.ui.adapter;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.support.v4.content.ContextCompat;
|
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.Button;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
@@ -32,6 +31,11 @@ import android.widget.TextView;
|
|||||||
import org.openintents.openpgp.util.OpenPgpUtils;
|
import org.openintents.openpgp.util.OpenPgpUtils;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
|
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
|
||||||
|
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
|
||||||
|
import org.sufficientlysecure.keychain.keyimport.processing.BytesLoaderState;
|
||||||
|
import org.sufficientlysecure.keychain.keyimport.processing.CloudLoaderState;
|
||||||
|
import org.sufficientlysecure.keychain.keyimport.processing.ImportKeysListener;
|
||||||
|
import org.sufficientlysecure.keychain.keyimport.processing.LoaderState;
|
||||||
import org.sufficientlysecure.keychain.operations.ImportOperation;
|
import org.sufficientlysecure.keychain.operations.ImportOperation;
|
||||||
import org.sufficientlysecure.keychain.pgp.KeyRing;
|
import org.sufficientlysecure.keychain.pgp.KeyRing;
|
||||||
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
|
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
|
||||||
@@ -49,11 +53,16 @@ import java.util.Map;
|
|||||||
public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.ViewHolder> {
|
public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.ViewHolder> {
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
private ImportKeysListener mListener;
|
||||||
private boolean mNonInteractive;
|
private boolean mNonInteractive;
|
||||||
|
|
||||||
|
private LoaderState mLoaderState;
|
||||||
private List<ImportKeysListEntry> mData;
|
private List<ImportKeysListEntry> mData;
|
||||||
|
|
||||||
public ImportKeysAdapter(Context mContext, boolean mNonInteractive) {
|
public ImportKeysAdapter(Context mContext, ImportKeysListener listener, boolean mNonInteractive) {
|
||||||
|
|
||||||
this.mContext = mContext;
|
this.mContext = mContext;
|
||||||
|
this.mListener = listener;
|
||||||
this.mNonInteractive = mNonInteractive;
|
this.mNonInteractive = mNonInteractive;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,6 +77,7 @@ public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.Vi
|
|||||||
public ImageView status;
|
public ImageView status;
|
||||||
public View userIdsDivider;
|
public View userIdsDivider;
|
||||||
public LinearLayout userIdsList;
|
public LinearLayout userIdsList;
|
||||||
|
public Button importButton;
|
||||||
|
|
||||||
public ViewHolder(View container) {
|
public ViewHolder(View container) {
|
||||||
super(container);
|
super(container);
|
||||||
@@ -80,6 +90,10 @@ public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.Vi
|
|||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setLoaderState(LoaderState loaderState) {
|
||||||
|
this.mLoaderState = loaderState;
|
||||||
|
}
|
||||||
|
|
||||||
public void setData(List<ImportKeysListEntry> data) {
|
public void setData(List<ImportKeysListEntry> data) {
|
||||||
this.mData = data;
|
this.mData = data;
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
@@ -119,6 +133,7 @@ public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.Vi
|
|||||||
vh.status = (ImageView) v.findViewById(R.id.import_item_status);
|
vh.status = (ImageView) v.findViewById(R.id.import_item_status);
|
||||||
vh.userIdsDivider = v.findViewById(R.id.import_item_status_divider);
|
vh.userIdsDivider = v.findViewById(R.id.import_item_status_divider);
|
||||||
vh.userIdsList = (LinearLayout) v.findViewById(R.id.import_item_user_ids_list);
|
vh.userIdsList = (LinearLayout) v.findViewById(R.id.import_item_user_ids_list);
|
||||||
|
vh.importButton = (Button) v.findViewById(R.id.import_item_button);
|
||||||
|
|
||||||
return vh;
|
return vh;
|
||||||
}
|
}
|
||||||
@@ -126,8 +141,19 @@ public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.Vi
|
|||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||||
final ImportKeysListEntry entry = mData.get(position);
|
final ImportKeysListEntry entry = mData.get(position);
|
||||||
Highlighter highlighter = new Highlighter(mContext, entry.getQuery());
|
|
||||||
|
|
||||||
|
holder.importButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
if (mLoaderState instanceof BytesLoaderState) {
|
||||||
|
mListener.importKey(new ParcelableKeyRing(entry.getEncodedRing()));
|
||||||
|
} else if (mLoaderState instanceof CloudLoaderState) {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Highlighter highlighter = new Highlighter(mContext, entry.getQuery());
|
||||||
// main user id
|
// main user id
|
||||||
String userId = entry.getUserIds().get(0);
|
String userId = entry.getUserIds().get(0);
|
||||||
OpenPgpUtils.UserId userIdSplit = KeyRing.splitUserId(userId);
|
OpenPgpUtils.UserId userIdSplit = KeyRing.splitUserId(userId);
|
||||||
|
|||||||
@@ -105,6 +105,7 @@
|
|||||||
android:padding="8dp">
|
android:padding="8dp">
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
|
android:id="@+id/import_item_button"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:background="?android:attr/selectableItemBackground"
|
android:background="?android:attr/selectableItemBackground"
|
||||||
|
|||||||
Reference in New Issue
Block a user