Merge branch 'v/crypto-input-parcel' of github.com:open-keychain/open-keychain into v/crypto-input-parcel

Conflicts:
	OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java
This commit is contained in:
Vincent Breitmoser 2015-03-30 23:42:38 +02:00
commit 19ecd5f427
9 changed files with 396 additions and 187 deletions

View File

@ -126,7 +126,7 @@ public class CreateKeyEmailFragment extends Fragment {
if (mAdditionalEmailModels == null) { if (mAdditionalEmailModels == null) {
mAdditionalEmailModels = new ArrayList<>(); mAdditionalEmailModels = new ArrayList<>();
if (mCreateKeyActivity.mAdditionalEmails != null) { if (mCreateKeyActivity.mAdditionalEmails != null) {
setAdditionalEmails(mCreateKeyActivity.mAdditionalEmails); mEmailAdapter.addAll(mCreateKeyActivity.mAdditionalEmails);
} }
} }
@ -209,12 +209,6 @@ public class CreateKeyEmailFragment extends Fragment {
return emails; return emails;
} }
private void setAdditionalEmails(ArrayList<String> emails) {
for (String email : emails) {
mAdditionalEmailModels.add(new EmailAdapter.ViewModel(email));
}
}
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
@ -244,8 +238,7 @@ public class CreateKeyEmailFragment extends Fragment {
// Provide a reference to the views for each data item // Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and // Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder // you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder { class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView; public TextView mTextView;
public ImageButton mDeleteButton; public ImageButton mDeleteButton;
@ -289,7 +282,10 @@ public class CreateKeyEmailFragment extends Fragment {
// Replace the contents of a view (invoked by the layout manager) // Replace the contents of a view (invoked by the layout manager)
@Override @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolder) { if (holder instanceof FooterHolder) {
FooterHolder thisHolder = (FooterHolder) holder;
thisHolder.mAddButton.setOnClickListener(mFooterOnClickListener);
} else if (holder instanceof ViewHolder) {
ViewHolder thisHolder = (ViewHolder) holder; ViewHolder thisHolder = (ViewHolder) holder;
// - get element from your dataset at this position // - get element from your dataset at this position
// - replace the contents of the view with that element // - replace the contents of the view with that element
@ -302,9 +298,6 @@ public class CreateKeyEmailFragment extends Fragment {
remove(model); remove(model);
} }
}); });
} else if (holder instanceof FooterHolder) {
FooterHolder thisHolder = (FooterHolder) holder;
thisHolder.mAddButton.setOnClickListener(mFooterOnClickListener);
} }
} }
@ -332,6 +325,12 @@ public class CreateKeyEmailFragment extends Fragment {
notifyItemInserted(mDataset.size() - 1); notifyItemInserted(mDataset.size() - 1);
} }
private void addAll(ArrayList<String> emails) {
for (String email : emails) {
mDataset.add(new EmailAdapter.ViewModel(email));
}
}
public void remove(ViewModel model) { public void remove(ViewModel model) {
int position = mDataset.indexOf(model); int position = mDataset.indexOf(model);
mDataset.remove(position); mDataset.remove(position);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de> * Copyright (C) 2014-2015 Dominik Schürmann <dominik@dominikschuermann.de>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -17,7 +17,6 @@
package org.sufficientlysecure.keychain.ui; package org.sufficientlysecure.keychain.ui;
import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Intent; import android.content.Intent;
@ -28,15 +27,17 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Message; import android.os.Message;
import android.os.Messenger; import android.os.Messenger;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import org.spongycastle.bcpg.CompressionAlgorithmTags; import org.spongycastle.bcpg.CompressionAlgorithmTags;
@ -50,6 +51,7 @@ import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider;
import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler; import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.ui.adapter.SpacesItemDecoration;
import org.sufficientlysecure.keychain.ui.dialog.DeleteFileDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.DeleteFileDialogFragment;
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
import org.sufficientlysecure.keychain.ui.util.FormattingUtils; import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
@ -61,9 +63,8 @@ import org.sufficientlysecure.keychain.util.ShareHelper;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.List;
import java.util.Set; import java.util.Set;
public class EncryptFilesFragment extends CryptoOperationFragment { public class EncryptFilesFragment extends CryptoOperationFragment {
@ -93,12 +94,12 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
private long mSigningKeyId = Constants.key.none; private long mSigningKeyId = Constants.key.none;
private Passphrase mPassphrase = new Passphrase(); private Passphrase mPassphrase = new Passphrase();
private ArrayList<Uri> mInputUris = new ArrayList<>();
private ArrayList<Uri> mOutputUris = new ArrayList<>(); private ArrayList<Uri> mOutputUris = new ArrayList<>();
private ListView mSelectedFiles; private RecyclerView mSelectedFiles;
private SelectedFilesAdapter mAdapter = new SelectedFilesAdapter();
private final Map<Uri, Bitmap> thumbnailCache = new HashMap<>(); ArrayList<FilesAdapter.ViewModel> mFilesModels;
FilesAdapter mFilesAdapter;
/** /**
* Creates new instance of this fragment * Creates new instance of this fragment
@ -146,17 +147,28 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.encrypt_files_fragment, container, false); View view = inflater.inflate(R.layout.encrypt_files_fragment, container, false);
mSelectedFiles = (RecyclerView) view.findViewById(R.id.selected_files_list);
View addView = inflater.inflate(R.layout.file_list_entry_add, null); mSelectedFiles.addItemDecoration(new SpacesItemDecoration(
addView.setOnClickListener(new View.OnClickListener() { FormattingUtils.dpToPx(getActivity(), 4)));
mSelectedFiles.setHasFixedSize(true);
mSelectedFiles.setLayoutManager(new LinearLayoutManager(getActivity()));
mSelectedFiles.setItemAnimator(new DefaultItemAnimator());
mFilesModels = new ArrayList<>();
mFilesAdapter = new FilesAdapter(getActivity(), mFilesModels, new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
addInputUri(); addInputUri();
} }
}); });
mSelectedFiles = (ListView) view.findViewById(R.id.selected_files_list);
mSelectedFiles.addFooterView(addView); ArrayList<Uri> inputUris = getArguments().getParcelableArrayList(ARG_URIS);
mSelectedFiles.setAdapter(mAdapter); if (inputUris != null) {
mFilesAdapter.addAll(inputUris);
}
mUseArmor = getArguments().getBoolean(ARG_USE_ASCII_ARMOR);
mSelectedFiles.setAdapter(mFilesAdapter);
return view; return view;
} }
@ -165,17 +177,14 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setHasOptionsMenu(true); setHasOptionsMenu(true);
mInputUris = getArguments().getParcelableArrayList(ARG_URIS);
mUseArmor = getArguments().getBoolean(ARG_USE_ASCII_ARMOR);
} }
private void addInputUri() { private void addInputUri() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
FileHelper.openDocument(EncryptFilesFragment.this, "*/*", true, REQUEST_CODE_INPUT); FileHelper.openDocument(EncryptFilesFragment.this, "*/*", true, REQUEST_CODE_INPUT);
} else { } else {
FileHelper.openFile(EncryptFilesFragment.this, mInputUris.isEmpty() ? FileHelper.openFile(EncryptFilesFragment.this, mFilesModels.isEmpty() ?
null : mInputUris.get(mInputUris.size() - 1), null : mFilesModels.get(mFilesModels.size() - 1).inputUri,
"*/*", REQUEST_CODE_INPUT); "*/*", REQUEST_CODE_INPUT);
} }
} }
@ -185,32 +194,27 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
return; return;
} }
if (mInputUris.contains(inputUri)) { if (mFilesAdapter.getAsArrayList().contains(inputUri)) {
Notify.create(getActivity(), Notify.create(getActivity(),
getActivity().getString(R.string.error_file_added_already, FileHelper.getFilename(getActivity(), inputUri)), getActivity().getString(R.string.error_file_added_already, FileHelper.getFilename(getActivity(), inputUri)),
Notify.Style.ERROR).show(); Notify.Style.ERROR).show();
return; return;
} }
mInputUris.add(inputUri); mFilesAdapter.add(inputUri);
mSelectedFiles.requestFocus();
}
private void delInputUri(int position) {
mInputUris.remove(position);
mSelectedFiles.requestFocus(); mSelectedFiles.requestFocus();
} }
private void showOutputFileDialog() { private void showOutputFileDialog() {
if (mInputUris.size() > 1 || mInputUris.isEmpty()) { if (mFilesModels.size() > 1 || mFilesModels.isEmpty()) {
throw new IllegalStateException(); throw new IllegalStateException();
} }
Uri inputUri = mInputUris.get(0); FilesAdapter.ViewModel model = mFilesModels.get(0);
String targetName = String targetName =
(mEncryptFilenames ? "1" : FileHelper.getFilename(getActivity(), inputUri)) (mEncryptFilenames ? "1" : FileHelper.getFilename(getActivity(), model.inputUri))
+ (mUseArmor ? Constants.FILE_EXTENSION_ASC : Constants.FILE_EXTENSION_PGP_MAIN); + (mUseArmor ? Constants.FILE_EXTENSION_ASC : Constants.FILE_EXTENSION_PGP_MAIN);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
File file = new File(inputUri.getPath()); File file = new File(model.inputUri.getPath());
File parentDir = file.exists() ? file.getParentFile() : Constants.Path.APP_DIR; File parentDir = file.exists() ? file.getParentFile() : Constants.Path.APP_DIR;
File targetFile = new File(parentDir, targetName); File targetFile = new File(parentDir, targetName);
FileHelper.saveFile(this, getString(R.string.title_encrypt_to_file), FileHelper.saveFile(this, getString(R.string.title_encrypt_to_file),
@ -221,40 +225,48 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
} }
private void encryptClicked(boolean share) { private void encryptClicked(boolean share) {
if (mInputUris.isEmpty()) { if (mFilesModels.isEmpty()) {
Notify.create(getActivity(), R.string.error_no_file_selected, Notify.Style.ERROR).show(); Notify.create(getActivity(), R.string.error_no_file_selected,
Notify.Style.ERROR).show();
return; return;
} }
if (share) { if (share) {
mOutputUris.clear(); mOutputUris.clear();
int filenameCounter = 1; int filenameCounter = 1;
for (Uri uri : mInputUris) { for (FilesAdapter.ViewModel model : mFilesModels) {
String targetName = String targetName =
(mEncryptFilenames ? String.valueOf(filenameCounter) : FileHelper.getFilename(getActivity(), uri)) (mEncryptFilenames ? String.valueOf(filenameCounter) : FileHelper.getFilename(getActivity(), model.inputUri))
+ (mUseArmor ? Constants.FILE_EXTENSION_ASC : Constants.FILE_EXTENSION_PGP_MAIN); + (mUseArmor ? Constants.FILE_EXTENSION_ASC : Constants.FILE_EXTENSION_PGP_MAIN);
mOutputUris.add(TemporaryStorageProvider.createFile(getActivity(), targetName)); mOutputUris.add(TemporaryStorageProvider.createFile(getActivity(), targetName));
filenameCounter++; filenameCounter++;
} }
startEncrypt(true); startEncrypt(true);
} else { } else {
if (mInputUris.size() > 1) { if (mFilesModels.size() > 1) {
Notify.create(getActivity(), R.string.error_multi_not_supported, Notify.Style.ERROR).show(); Notify.create(getActivity(), R.string.error_multi_not_supported,
Notify.Style.ERROR).show();
return; return;
} }
showOutputFileDialog(); showOutputFileDialog();
} }
} }
@TargetApi(Build.VERSION_CODES.KITKAT) public void addFile(Intent data) {
public boolean handleClipData(Intent data) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
if (data.getClipData() != null && data.getClipData().getItemCount() > 0) { addInputUri(data.getData());
for (int i = 0; i < data.getClipData().getItemCount(); i++) { } else {
Uri uri = data.getClipData().getItemAt(i).getUri(); if (data.getClipData() != null && data.getClipData().getItemCount() > 0) {
if (uri != null) addInputUri(uri); for (int i = 0; i < data.getClipData().getItemCount(); i++) {
Uri uri = data.getClipData().getItemAt(i).getUri();
if (uri != null) {
addInputUri(uri);
}
}
} else {
// fallback, try old method to get single uri
addInputUri(data.getData());
} }
return true;
} }
return false;
} }
@Override @Override
@ -284,22 +296,18 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
} }
case R.id.check_use_armor: { case R.id.check_use_armor: {
mUseArmor = item.isChecked(); mUseArmor = item.isChecked();
// notifyUpdate();
break; break;
} }
case R.id.check_delete_after_encrypt: { case R.id.check_delete_after_encrypt: {
mDeleteAfterEncrypt = item.isChecked(); mDeleteAfterEncrypt = item.isChecked();
// notifyUpdate();
break; break;
} }
case R.id.check_enable_compression: { case R.id.check_enable_compression: {
mUseCompression = item.isChecked(); mUseCompression = item.isChecked();
// onNotifyUpdate();
break; break;
} }
case R.id.check_encrypt_filenames: { case R.id.check_encrypt_filenames: {
mEncryptFilenames = item.isChecked(); mEncryptFilenames = item.isChecked();
// onNotifyUpdate();
break; break;
} }
// case R.id.check_hidden_recipients: { // case R.id.check_hidden_recipients: {
@ -317,15 +325,15 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
protected boolean inputIsValid() { protected boolean inputIsValid() {
// file checks // file checks
if (mInputUris.isEmpty()) { if (mFilesModels.isEmpty()) {
Notify.create(getActivity(), R.string.no_file_selected, Notify.Style.ERROR) Notify.create(getActivity(), R.string.no_file_selected, Notify.Style.ERROR)
.show(); .show();
return false; return false;
} else if (mInputUris.size() > 1 && !mShareAfterEncrypt) { } else if (mFilesModels.size() > 1 && !mShareAfterEncrypt) {
Log.e(Constants.TAG, "Aborting: mInputUris.size() > 1 && !mShareAfterEncrypt"); Log.e(Constants.TAG, "Aborting: mInputUris.size() > 1 && !mShareAfterEncrypt");
// This should be impossible... // This should be impossible...
return false; return false;
} else if (mInputUris.size() != mOutputUris.size()) { } else if (mFilesModels.size() != mOutputUris.size()) {
Log.e(Constants.TAG, "Aborting: mInputUris.size() != mOutputUris.size()"); Log.e(Constants.TAG, "Aborting: mInputUris.size() != mOutputUris.size()");
// This as well // This as well
return false; return false;
@ -363,8 +371,8 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
public void onEncryptSuccess(final SignEncryptResult result) { public void onEncryptSuccess(final SignEncryptResult result) {
if (mDeleteAfterEncrypt) { if (mDeleteAfterEncrypt) {
final Uri[] inputUris = mInputUris.toArray(new Uri[mInputUris.size()]); DeleteFileDialogFragment deleteFileDialog =
DeleteFileDialogFragment deleteFileDialog = DeleteFileDialogFragment.newInstance(inputUris); DeleteFileDialogFragment.newInstance(mFilesAdapter.getAsArrayList());
deleteFileDialog.setOnDeletedListener(new DeleteFileDialogFragment.OnDeletedListener() { deleteFileDialog.setOnDeletedListener(new DeleteFileDialogFragment.OnDeletedListener() {
@Override @Override
@ -381,8 +389,7 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
}); });
deleteFileDialog.show(getActivity().getSupportFragmentManager(), "deleteDialog"); deleteFileDialog.show(getActivity().getSupportFragmentManager(), "deleteDialog");
mInputUris.clear(); mFilesModels.clear();
onNotifyUpdate();
} else { } else {
if (mShareAfterEncrypt) { if (mShareAfterEncrypt) {
// Share encrypted message/file // Share encrypted message/file
@ -398,7 +405,7 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
// fill values for this action // fill values for this action
SignEncryptParcel data = new SignEncryptParcel(); SignEncryptParcel data = new SignEncryptParcel();
data.addInputUris(mInputUris); data.addInputUris(mFilesAdapter.getAsArrayList());
data.addOutputUris(mOutputUris); data.addOutputUris(mOutputUris);
if (mUseCompression) { if (mUseCompression) {
@ -536,9 +543,7 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
switch (requestCode) { switch (requestCode) {
case REQUEST_CODE_INPUT: { case REQUEST_CODE_INPUT: {
if (resultCode == Activity.RESULT_OK && data != null) { if (resultCode == Activity.RESULT_OK && data != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT || !handleClipData(data)) { addFile(data);
addInputUri(data.getData());
}
} }
return; return;
} }
@ -547,7 +552,6 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
if (resultCode == Activity.RESULT_OK && data != null) { if (resultCode == Activity.RESULT_OK && data != null) {
mOutputUris.clear(); mOutputUris.clear();
mOutputUris.add(data.getData()); mOutputUris.add(data.getData());
onNotifyUpdate();
startEncrypt(false); startEncrypt(false);
} }
return; return;
@ -561,66 +565,167 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
} }
} }
public void onNotifyUpdate() { public static class FilesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// Clear cache if needed private Activity mActivity;
for (Uri uri : new HashSet<>(thumbnailCache.keySet())) { private List<ViewModel> mDataset;
if (!mInputUris.contains(uri)) { private View.OnClickListener mFooterOnClickListener;
thumbnailCache.remove(uri); private static final int TYPE_FOOTER = 0;
private static final int TYPE_ITEM = 1;
public static class ViewModel {
Uri inputUri;
Bitmap thumbnail;
String filename;
long fileSize;
ViewModel(Uri inputUri, Bitmap thumbnail, String filename, long fileSize) {
this.inputUri = inputUri;
this.thumbnail = thumbnail;
this.filename = filename;
this.fileSize = fileSize;
}
@Override
public String toString() {
return inputUri.toString();
} }
} }
mAdapter.notifyDataSetChanged(); // Provide a reference to the views for each data item
} // Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
class ViewHolder extends RecyclerView.ViewHolder {
public TextView filename;
public TextView fileSize;
public View removeButton;
public ImageView thumbnail;
private class SelectedFilesAdapter extends BaseAdapter { public ViewHolder(View itemView) {
@Override super(itemView);
public int getCount() { filename = (TextView) itemView.findViewById(R.id.filename);
return mInputUris.size(); fileSize = (TextView) itemView.findViewById(R.id.filesize);
removeButton = itemView.findViewById(R.id.action_remove_file_from_list);
thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
}
} }
@Override class FooterHolder extends RecyclerView.ViewHolder {
public Object getItem(int position) { public Button mAddButton;
return mInputUris.get(position);
public FooterHolder(View itemView) {
super(itemView);
mAddButton = (Button) itemView.findViewById(R.id.file_list_entry_add);
}
} }
@Override // Provide a suitable constructor (depends on the kind of dataset)
public long getItemId(int position) { public FilesAdapter(Activity activity, List<ViewModel> myDataset, View.OnClickListener onFooterClickListener) {
return getItem(position).hashCode(); mActivity = activity;
mDataset = myDataset;
mFooterOnClickListener = onFooterClickListener;
} }
// Create new views (invoked by the layout manager)
@Override @Override
public View getView(final int position, View convertView, ViewGroup parent) { public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Uri inputUri = mInputUris.get(position); if (viewType == TYPE_FOOTER) {
View view; View v = LayoutInflater.from(parent.getContext())
if (convertView == null) { .inflate(R.layout.file_list_entry_add, parent, false);
view = getActivity().getLayoutInflater().inflate(R.layout.file_list_entry, null); return new FooterHolder(v);
} else { } else {
view = convertView; //inflate your layout and pass it to view holder
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.file_list_entry, parent, false);
return new ViewHolder(v);
} }
((TextView) view.findViewById(R.id.filename)).setText(FileHelper.getFilename(getActivity(), inputUri)); }
long size = FileHelper.getFileSize(getActivity(), inputUri);
if (size == -1) { // Replace the contents of a view (invoked by the layout manager)
((TextView) view.findViewById(R.id.filesize)).setText(""); @Override
} else { public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
((TextView) view.findViewById(R.id.filesize)).setText(FileHelper.readableFileSize(size)); if (holder instanceof FooterHolder) {
} FooterHolder thisHolder = (FooterHolder) holder;
view.findViewById(R.id.action_remove_file_from_list).setOnClickListener(new View.OnClickListener() { thisHolder.mAddButton.setOnClickListener(mFooterOnClickListener);
@Override } else if (holder instanceof ViewHolder) {
public void onClick(View v) { ViewHolder thisHolder = (ViewHolder) holder;
delInputUri(position); // - get element from your dataset at this position
// - replace the contents of the view with that element
final ViewModel model = mDataset.get(position);
thisHolder.filename.setText(model.filename);
if (model.fileSize == -1) {
thisHolder.fileSize.setText("");
} else {
thisHolder.fileSize.setText(FileHelper.readableFileSize(model.fileSize));
}
thisHolder.removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(model);
}
});
if (model.thumbnail != null) {
thisHolder.thumbnail.setImageBitmap(model.thumbnail);
} else {
thisHolder.thumbnail.setImageResource(R.drawable.ic_doc_generic_am);
} }
});
int px = FormattingUtils.dpToPx(getActivity(), 48);
if (!thumbnailCache.containsKey(inputUri)) {
thumbnailCache.put(inputUri, FileHelper.getThumbnail(getActivity(), inputUri, new Point(px, px)));
} }
Bitmap bitmap = thumbnailCache.get(inputUri);
if (bitmap != null) {
((ImageView) view.findViewById(R.id.thumbnail)).setImageBitmap(bitmap);
} else {
((ImageView) view.findViewById(R.id.thumbnail)).setImageResource(R.drawable.ic_doc_generic_am);
}
return view;
} }
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size() + 1;
}
@Override
public int getItemViewType(int position) {
if (isPositionFooter(position)) {
return TYPE_FOOTER;
} else {
return TYPE_ITEM;
}
}
private boolean isPositionFooter(int position) {
return position == mDataset.size();
}
public void add(Uri inputUri) {
mDataset.add(createModel(inputUri));
notifyItemInserted(mDataset.size() - 1);
}
public void addAll(ArrayList<Uri> inputUris) {
if (inputUris != null) {
for (Uri inputUri : inputUris) {
mDataset.add(createModel(inputUri));
}
}
// TODO: notifyItemInserted?
}
private ViewModel createModel(Uri inputUri) {
int px = FormattingUtils.dpToPx(mActivity, 48);
Bitmap thumbnail = FileHelper.getThumbnail(mActivity, inputUri, new Point(px, px));
String filename = FileHelper.getFilename(mActivity, inputUri);
long size = FileHelper.getFileSize(mActivity, inputUri);
return new ViewModel(inputUri, thumbnail, filename, size);
}
public void remove(ViewModel model) {
int position = mDataset.indexOf(model);
mDataset.remove(position);
notifyItemRemoved(position);
}
public ArrayList<Uri> getAsArrayList() {
ArrayList<Uri> uris = new ArrayList<>();
for (ViewModel model : mDataset) {
uris.add(model.inputUri);
}
return uris;
}
} }
} }

View File

@ -65,7 +65,7 @@ public class EncryptModeAsymmetricFragment extends Fragment {
private IAsymmetric mEncryptInterface; private IAsymmetric mEncryptInterface;
// @Override // @Override
// public void onNotifyUpdate() { // public void updateUi() {
// if (mSign != null) { // if (mSign != null) {
// mSign.setSelectedKeyId(mEncryptInterface.getSignatureKey()); // mSign.setSelectedKeyId(mEncryptInterface.getSignatureKey());
// } // }

View File

@ -147,6 +147,8 @@ public class KeyAdapter extends CursorAdapter {
mStatus.setVisibility(View.GONE); mStatus.setVisibility(View.GONE);
if (mSlingerButton.hasOnClickListeners()) { if (mSlingerButton.hasOnClickListeners()) {
mSlinger.setVisibility(View.VISIBLE); mSlinger.setVisibility(View.VISIBLE);
} else {
mSlinger.setVisibility(View.GONE);
} }
mMainUserId.setTextColor(context.getResources().getColor(R.color.black)); mMainUserId.setTextColor(context.getResources().getColor(R.color.black));
mMainUserIdRest.setTextColor(context.getResources().getColor(R.color.black)); mMainUserIdRest.setTextColor(context.getResources().getColor(R.color.black));

View File

@ -0,0 +1,93 @@
/*
* Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.ui.adapter;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* https://gist.github.com/yrom/3b4bcbc2370ca2290434
*/
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
private int spanCount;
private int lastItemInFirstLane = -1;
public SpacesItemDecoration(int space) {
this(space, 1);
}
/**
* @param space
* @param spanCount spans count of one lane
*/
public SpacesItemDecoration(int space, int spanCount) {
this.space = space;
this.spanCount = spanCount;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
final int position = params.getViewPosition();
final int spanSize;
final int index;
if (params instanceof GridLayoutManager.LayoutParams) {
GridLayoutManager.LayoutParams gridParams = (GridLayoutManager.LayoutParams) params;
spanSize = gridParams.getSpanSize();
index = gridParams.getSpanIndex();
} else {
spanSize = 1;
index = position % spanCount;
}
// invalid value
if (spanSize < 1 || index < 0) return;
if (spanSize == spanCount) { // full span
outRect.left = space;
outRect.right = space;
} else {
if (index == 0) { // left one
outRect.left = space;
}
// spanCount >= 1
if (index == spanCount - 1) { // right one
outRect.right = space;
}
if (outRect.left == 0) {
outRect.left = space / 2;
}
if (outRect.right == 0) {
outRect.right = space / 2;
}
}
// set top to all in first lane
if (position < spanCount && spanSize <= spanCount) {
if (lastItemInFirstLane < 0) { // lay out at first time
lastItemInFirstLane = position + spanSize == spanCount ? position : lastItemInFirstLane;
outRect.top = space;
} else if (position <= lastItemInFirstLane) { // scroll to first lane again
outRect.top = space;
}
}
outRect.bottom = space;
}
}

View File

@ -44,11 +44,24 @@ public class DeleteFileDialogFragment extends DialogFragment {
/** /**
* Creates new instance of this delete file dialog fragment * Creates new instance of this delete file dialog fragment
*/ */
public static DeleteFileDialogFragment newInstance(Uri... deleteUris) { public static DeleteFileDialogFragment newInstance(ArrayList<Uri> deleteUris) {
DeleteFileDialogFragment frag = new DeleteFileDialogFragment(); DeleteFileDialogFragment frag = new DeleteFileDialogFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putParcelableArray(ARG_DELETE_URIS, deleteUris); args.putParcelableArrayList(ARG_DELETE_URIS, deleteUris);
frag.setArguments(args);
return frag;
}
public static DeleteFileDialogFragment newInstance(Uri deleteUri) {
DeleteFileDialogFragment frag = new DeleteFileDialogFragment();
Bundle args = new Bundle();
ArrayList<Uri> list = new ArrayList<>();
list.add(deleteUri);
args.putParcelableArrayList(ARG_DELETE_URIS, list);
frag.setArguments(args); frag.setArguments(args);
@ -62,7 +75,7 @@ public class DeleteFileDialogFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final FragmentActivity activity = getActivity(); final FragmentActivity activity = getActivity();
final Uri[] deleteUris = (Uri[]) getArguments().getParcelableArray(ARG_DELETE_URIS); final ArrayList<Uri> deleteUris = getArguments().getParcelableArrayList(ARG_DELETE_URIS);
final StringBuilder deleteFileNames = new StringBuilder(); final StringBuilder deleteFileNames = new StringBuilder();
//Retrieving file names after deletion gives unexpected results //Retrieving file names after deletion gives unexpected results
@ -127,7 +140,7 @@ public class DeleteFileDialogFragment extends DialogFragment {
// NOTE: Use Toasts, not Snackbars. When sharing to another application snackbars // NOTE: Use Toasts, not Snackbars. When sharing to another application snackbars
// would not show up! // would not show up!
Toast.makeText(getActivity(), getActivity().getString(R.string.file_delete_successful, Toast.makeText(getActivity(), getActivity().getString(R.string.file_delete_successful,
deleteUris.length - failedFileNameList.size(), deleteUris.length, failedFileNames.toString()), deleteUris.size() - failedFileNameList.size(), deleteUris.size(), failedFileNames.toString()),
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
if (onDeletedListener != null) { if (onDeletedListener != null) {

View File

@ -4,15 +4,11 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<ListView <android.support.v7.widget.RecyclerView
android:id="@+id/selected_files_list" android:id="@+id/selected_files_list"
android:dividerHeight="4dip"
android:divider="@android:color/transparent"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingLeft="16dp" android:paddingLeft="16dp"
android:paddingRight="16dp" android:paddingRight="16dp"
android:layout_marginTop="8dp" android:scrollbars="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />

View File

@ -1,60 +1,58 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="48dip"
android:layout_width="match_parent" android:background="@drawable/attachment_bg_holo">
android:layout_height="48dip"
android:background="@drawable/attachment_bg_holo">
<ImageView <ImageView
android:id="@+id/thumbnail" android:id="@+id/thumbnail"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:scaleType="center" android:scaleType="center"
android:layout_width="48dip" android:layout_width="48dip"
android:layout_height="48dip"/> android:layout_height="48dip" />
<LinearLayout <LinearLayout
android:orientation="vertical" android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/thumbnail"
android:layout_centerVertical="true">
<TextView
android:id="@+id/filename"
android:layout_marginLeft="8dip"
android:layout_marginRight="32dip"
android:maxLines="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@+id/thumbnail" android:textColor="?android:attr/textColorSecondary"
android:layout_centerVertical="true"> android:textAppearance="?android:attr/textAppearanceSmall"
android:ellipsize="end" />
<TextView <TextView
android:id="@+id/filename" android:id="@+id/filesize"
android:layout_marginLeft="8dip" android:layout_marginLeft="8dip"
android:layout_marginRight="32dip" android:layout_marginRight="32dip"
android:maxLines="1" android:maxLines="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textColor="?android:attr/textColorSecondary" android:textColor="?android:attr/textColorTertiary"
android:textAppearance="?android:attr/textAppearanceSmall" android:textAppearance="?android:attr/textAppearanceSmall"
android:ellipsize="end"/> android:textSize="12sp"
android:ellipsize="end" />
<TextView
android:id="@+id/filesize"
android:layout_marginLeft="8dip"
android:layout_marginRight="32dip"
android:maxLines="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorTertiary"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12sp"
android:ellipsize="end"/>
</LinearLayout> </LinearLayout>
<ImageButton <ImageButton
android:id="@+id/action_remove_file_from_list" android:id="@+id/action_remove_file_from_list"
android:layout_width="48dip" android:layout_width="48dip"
android:layout_height="48dip" android:layout_height="48dip"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:paddingRight="16dip" android:paddingRight="16dip"
android:paddingLeft="16dip" android:paddingLeft="16dip"
android:src="@drawable/ic_close_grey_24dp" android:src="@drawable/ic_close_grey_24dp"
android:clickable="true" android:clickable="true"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:background="?android:selectableItemBackground"/> android:background="?android:selectableItemBackground" />
</RelativeLayout> </RelativeLayout>

View File

@ -1,21 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dp"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:clickable="true" android:minHeight="16dp"
android:minHeight="?android:attr/listPreferredItemHeight" android:orientation="horizontal"
style="?android:attr/borderlessButtonStyle"> android:singleLine="true">
<TextView <Button
android:id="@+id/file_list_entry_add"
android:paddingLeft="8dp" android:paddingLeft="8dp"
android:paddingRight="8dp" android:paddingRight="8dp"
android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_gravity="center" android:layout_gravity="center"
android:text="@string/btn_add_files" android:text="@string/btn_add_files"
style="?android:attr/borderlessButtonStyle"
android:drawableLeft="@drawable/ic_folder_grey_24dp" android:drawableLeft="@drawable/ic_folder_grey_24dp"
android:drawablePadding="8dp" android:drawablePadding="16dp"
android:gravity="center" /> android:gravity="left|center_vertical" />
</FrameLayout>
</LinearLayout>