From a3276a448528cee3ed392bf01e36835bbe8246ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 30 Mar 2015 20:41:29 +0200 Subject: [PATCH] Use RecyclerView in EncryptFilesFragment --- .../keychain/ui/CreateKeyEmailFragment.java | 25 +- .../keychain/ui/EncryptFilesFragment.java | 319 ++++++++++++------ .../ui/EncryptModeAsymmetricFragment.java | 2 +- .../ui/adapter/SpacesItemDecoration.java | 93 +++++ .../ui/dialog/DeleteFileDialogFragment.java | 21 +- .../res/layout/encrypt_files_fragment.xml | 7 +- .../src/main/res/layout/file_list_entry.xml | 90 +++-- .../main/res/layout/file_list_entry_add.xml | 25 +- 8 files changed, 394 insertions(+), 188 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SpacesItemDecoration.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java index 7e2e1c31c..85e2f8e9d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java @@ -126,7 +126,7 @@ public class CreateKeyEmailFragment extends Fragment { if (mAdditionalEmailModels == null) { mAdditionalEmailModels = new ArrayList<>(); if (mCreateKeyActivity.mAdditionalEmails != null) { - setAdditionalEmails(mCreateKeyActivity.mAdditionalEmails); + mEmailAdapter.addAll(mCreateKeyActivity.mAdditionalEmails); } } @@ -209,12 +209,6 @@ public class CreateKeyEmailFragment extends Fragment { return emails; } - private void setAdditionalEmails(ArrayList emails) { - for (String email : emails) { - mAdditionalEmailModels.add(new EmailAdapter.ViewModel(email)); - } - } - @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); @@ -244,8 +238,7 @@ public class CreateKeyEmailFragment extends Fragment { // 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 - public static class ViewHolder extends RecyclerView.ViewHolder { - // each data item is just a string in this case + class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ImageButton mDeleteButton; @@ -289,7 +282,10 @@ public class CreateKeyEmailFragment extends Fragment { // Replace the contents of a view (invoked by the layout manager) @Override 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; // - get element from your dataset at this position // - replace the contents of the view with that element @@ -302,9 +298,6 @@ public class CreateKeyEmailFragment extends Fragment { 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); } + private void addAll(ArrayList emails) { + for (String email : emails) { + mDataset.add(new EmailAdapter.ViewModel(email)); + } + } + public void remove(ViewModel model) { int position = mDataset.indexOf(model); mDataset.remove(position); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java index 5af353524..dad8704fb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014 Dominik Schürmann + * Copyright (C) 2014-2015 Dominik Schürmann * * 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 @@ -17,7 +17,6 @@ package org.sufficientlysecure.keychain.ui; -import android.annotation.TargetApi; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; @@ -28,15 +27,17 @@ import android.os.Build; import android.os.Bundle; import android.os.Message; 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.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.BaseAdapter; +import android.widget.Button; import android.widget.ImageView; -import android.widget.ListView; import android.widget.TextView; 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.ServiceProgressHandler; 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.ProgressDialogFragment; import org.sufficientlysecure.keychain.ui.util.FormattingUtils; @@ -61,9 +63,8 @@ import org.sufficientlysecure.keychain.util.ShareHelper; import java.io.File; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; +import java.util.List; import java.util.Set; public class EncryptFilesFragment extends CryptoOperationFragment { @@ -93,12 +94,12 @@ public class EncryptFilesFragment extends CryptoOperationFragment { private long mSigningKeyId = Constants.key.none; private Passphrase mPassphrase = new Passphrase(); - private ArrayList mInputUris = new ArrayList(); - private ArrayList mOutputUris = new ArrayList(); + private ArrayList mOutputUris = new ArrayList<>(); - private ListView mSelectedFiles; - private SelectedFilesAdapter mAdapter = new SelectedFilesAdapter(); - private final Map thumbnailCache = new HashMap<>(); + private RecyclerView mSelectedFiles; + + ArrayList mFilesModels; + FilesAdapter mFilesAdapter; /** * Creates new instance of this fragment @@ -146,17 +147,28 @@ public class EncryptFilesFragment extends CryptoOperationFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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); - addView.setOnClickListener(new View.OnClickListener() { + mSelectedFiles.addItemDecoration(new SpacesItemDecoration( + 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 public void onClick(View v) { addInputUri(); } }); - mSelectedFiles = (ListView) view.findViewById(R.id.selected_files_list); - mSelectedFiles.addFooterView(addView); - mSelectedFiles.setAdapter(mAdapter); + + ArrayList inputUris = getArguments().getParcelableArrayList(ARG_URIS); + if (inputUris != null) { + mFilesAdapter.addAll(inputUris); + } + mUseArmor = getArguments().getBoolean(ARG_USE_ASCII_ARMOR); + mSelectedFiles.setAdapter(mFilesAdapter); return view; } @@ -165,17 +177,14 @@ public class EncryptFilesFragment extends CryptoOperationFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); - - mInputUris = getArguments().getParcelableArrayList(ARG_URIS); - mUseArmor = getArguments().getBoolean(ARG_USE_ASCII_ARMOR); } private void addInputUri() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { FileHelper.openDocument(EncryptFilesFragment.this, "*/*", true, REQUEST_CODE_INPUT); } else { - FileHelper.openFile(EncryptFilesFragment.this, mInputUris.isEmpty() ? - null : mInputUris.get(mInputUris.size() - 1), + FileHelper.openFile(EncryptFilesFragment.this, mFilesModels.isEmpty() ? + null : mFilesModels.get(mFilesModels.size() - 1).inputUri, "*/*", REQUEST_CODE_INPUT); } } @@ -185,32 +194,27 @@ public class EncryptFilesFragment extends CryptoOperationFragment { return; } - if (mInputUris.contains(inputUri)) { + if (mFilesAdapter.getAsArrayList().contains(inputUri)) { Notify.create(getActivity(), getActivity().getString(R.string.error_file_added_already, FileHelper.getFilename(getActivity(), inputUri)), Notify.Style.ERROR).show(); return; } - mInputUris.add(inputUri); - mSelectedFiles.requestFocus(); - } - - private void delInputUri(int position) { - mInputUris.remove(position); + mFilesAdapter.add(inputUri); mSelectedFiles.requestFocus(); } private void showOutputFileDialog() { - if (mInputUris.size() > 1 || mInputUris.isEmpty()) { + if (mFilesModels.size() > 1 || mFilesModels.isEmpty()) { throw new IllegalStateException(); } - Uri inputUri = mInputUris.get(0); + FilesAdapter.ViewModel model = mFilesModels.get(0); 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); 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 targetFile = new File(parentDir, targetName); FileHelper.saveFile(this, getString(R.string.title_encrypt_to_file), @@ -221,40 +225,48 @@ public class EncryptFilesFragment extends CryptoOperationFragment { } private void encryptClicked(boolean share) { - if (mInputUris.isEmpty()) { - Notify.create(getActivity(), R.string.error_no_file_selected, Notify.Style.ERROR).show(); + if (mFilesModels.isEmpty()) { + Notify.create(getActivity(), R.string.error_no_file_selected, + Notify.Style.ERROR).show(); return; } if (share) { mOutputUris.clear(); int filenameCounter = 1; - for (Uri uri : mInputUris) { + for (FilesAdapter.ViewModel model : mFilesModels) { 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); mOutputUris.add(TemporaryStorageProvider.createFile(getActivity(), targetName)); filenameCounter++; } startEncrypt(true); } else { - if (mInputUris.size() > 1) { - Notify.create(getActivity(), R.string.error_multi_not_supported, Notify.Style.ERROR).show(); + if (mFilesModels.size() > 1) { + Notify.create(getActivity(), R.string.error_multi_not_supported, + Notify.Style.ERROR).show(); return; } showOutputFileDialog(); } } - @TargetApi(Build.VERSION_CODES.KITKAT) - public boolean handleClipData(Intent data) { - if (data.getClipData() != null && data.getClipData().getItemCount() > 0) { - for (int i = 0; i < data.getClipData().getItemCount(); i++) { - Uri uri = data.getClipData().getItemAt(i).getUri(); - if (uri != null) addInputUri(uri); + public void addFile(Intent data) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { + addInputUri(data.getData()); + } else { + if (data.getClipData() != null && data.getClipData().getItemCount() > 0) { + 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 @@ -284,22 +296,18 @@ public class EncryptFilesFragment extends CryptoOperationFragment { } case R.id.check_use_armor: { mUseArmor = item.isChecked(); -// notifyUpdate(); break; } case R.id.check_delete_after_encrypt: { mDeleteAfterEncrypt = item.isChecked(); -// notifyUpdate(); break; } case R.id.check_enable_compression: { mUseCompression = item.isChecked(); -// onNotifyUpdate(); break; } case R.id.check_encrypt_filenames: { mEncryptFilenames = item.isChecked(); -// onNotifyUpdate(); break; } // case R.id.check_hidden_recipients: { @@ -317,15 +325,15 @@ public class EncryptFilesFragment extends CryptoOperationFragment { protected boolean inputIsValid() { // file checks - if (mInputUris.isEmpty()) { + if (mFilesModels.isEmpty()) { Notify.create(getActivity(), R.string.no_file_selected, Notify.Style.ERROR) .show(); return false; - } else if (mInputUris.size() > 1 && !mShareAfterEncrypt) { + } else if (mFilesModels.size() > 1 && !mShareAfterEncrypt) { Log.e(Constants.TAG, "Aborting: mInputUris.size() > 1 && !mShareAfterEncrypt"); // This should be impossible... return false; - } else if (mInputUris.size() != mOutputUris.size()) { + } else if (mFilesModels.size() != mOutputUris.size()) { Log.e(Constants.TAG, "Aborting: mInputUris.size() != mOutputUris.size()"); // This as well return false; @@ -368,8 +376,8 @@ public class EncryptFilesFragment extends CryptoOperationFragment { public void onEncryptSuccess(final SignEncryptResult result) { if (mDeleteAfterEncrypt) { - final Uri[] inputUris = mInputUris.toArray(new Uri[mInputUris.size()]); - DeleteFileDialogFragment deleteFileDialog = DeleteFileDialogFragment.newInstance(inputUris); + DeleteFileDialogFragment deleteFileDialog = + DeleteFileDialogFragment.newInstance(mFilesAdapter.getAsArrayList()); deleteFileDialog.setOnDeletedListener(new DeleteFileDialogFragment.OnDeletedListener() { @Override @@ -386,8 +394,7 @@ public class EncryptFilesFragment extends CryptoOperationFragment { }); deleteFileDialog.show(getActivity().getSupportFragmentManager(), "deleteDialog"); - mInputUris.clear(); - onNotifyUpdate(); + mFilesModels.clear(); } else { if (mShareAfterEncrypt) { // Share encrypted message/file @@ -403,7 +410,7 @@ public class EncryptFilesFragment extends CryptoOperationFragment { // fill values for this action SignEncryptParcel data = new SignEncryptParcel(); - data.addInputUris(mInputUris); + data.addInputUris(mFilesAdapter.getAsArrayList()); data.addOutputUris(mOutputUris); if (mUseCompression) { @@ -563,9 +570,7 @@ public class EncryptFilesFragment extends CryptoOperationFragment { switch (requestCode) { case REQUEST_CODE_INPUT: { if (resultCode == Activity.RESULT_OK && data != null) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT || !handleClipData(data)) { - addInputUri(data.getData()); - } + addFile(data); } return; } @@ -574,7 +579,6 @@ public class EncryptFilesFragment extends CryptoOperationFragment { if (resultCode == Activity.RESULT_OK && data != null) { mOutputUris.clear(); mOutputUris.add(data.getData()); - onNotifyUpdate(); startEncrypt(false); } return; @@ -588,66 +592,167 @@ public class EncryptFilesFragment extends CryptoOperationFragment { } } - public void onNotifyUpdate() { - // Clear cache if needed - for (Uri uri : new HashSet<>(thumbnailCache.keySet())) { - if (!mInputUris.contains(uri)) { - thumbnailCache.remove(uri); + public static class FilesAdapter extends RecyclerView.Adapter { + private Activity mActivity; + private List mDataset; + private View.OnClickListener mFooterOnClickListener; + 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 { - @Override - public int getCount() { - return mInputUris.size(); + public ViewHolder(View itemView) { + super(itemView); + filename = (TextView) itemView.findViewById(R.id.filename); + 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 - public Object getItem(int position) { - return mInputUris.get(position); + class FooterHolder extends RecyclerView.ViewHolder { + public Button mAddButton; + + public FooterHolder(View itemView) { + super(itemView); + mAddButton = (Button) itemView.findViewById(R.id.file_list_entry_add); + } } - @Override - public long getItemId(int position) { - return getItem(position).hashCode(); + // Provide a suitable constructor (depends on the kind of dataset) + public FilesAdapter(Activity activity, List myDataset, View.OnClickListener onFooterClickListener) { + mActivity = activity; + mDataset = myDataset; + mFooterOnClickListener = onFooterClickListener; } + // Create new views (invoked by the layout manager) @Override - public View getView(final int position, View convertView, ViewGroup parent) { - Uri inputUri = mInputUris.get(position); - View view; - if (convertView == null) { - view = getActivity().getLayoutInflater().inflate(R.layout.file_list_entry, null); + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + if (viewType == TYPE_FOOTER) { + View v = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.file_list_entry_add, parent, false); + return new FooterHolder(v); } 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) { - ((TextView) view.findViewById(R.id.filesize)).setText(""); - } else { - ((TextView) view.findViewById(R.id.filesize)).setText(FileHelper.readableFileSize(size)); - } - view.findViewById(R.id.action_remove_file_from_list).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - delInputUri(position); + } + + // Replace the contents of a view (invoked by the layout manager) + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { + if (holder instanceof FooterHolder) { + FooterHolder thisHolder = (FooterHolder) holder; + thisHolder.mAddButton.setOnClickListener(mFooterOnClickListener); + } else if (holder instanceof ViewHolder) { + ViewHolder thisHolder = (ViewHolder) holder; + // - 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 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 getAsArrayList() { + ArrayList uris = new ArrayList<>(); + for (ViewModel model : mDataset) { + uris.add(model.inputUri); + } + return uris; + } + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java index 87c6c477c..6f56f2dc4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java @@ -65,7 +65,7 @@ public class EncryptModeAsymmetricFragment extends Fragment { private IAsymmetric mEncryptInterface; // @Override -// public void onNotifyUpdate() { +// public void updateUi() { // if (mSign != null) { // mSign.setSelectedKeyId(mEncryptInterface.getSignatureKey()); // } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SpacesItemDecoration.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SpacesItemDecoration.java new file mode 100644 index 000000000..bc595803b --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SpacesItemDecoration.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2015 Dominik Schürmann + * + * 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 . + */ + +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; + + } +} \ No newline at end of file diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java index bd4e5577b..956171349 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java @@ -44,11 +44,24 @@ public class DeleteFileDialogFragment extends DialogFragment { /** * Creates new instance of this delete file dialog fragment */ - public static DeleteFileDialogFragment newInstance(Uri... deleteUris) { + public static DeleteFileDialogFragment newInstance(ArrayList deleteUris) { DeleteFileDialogFragment frag = new DeleteFileDialogFragment(); 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 list = new ArrayList<>(); + list.add(deleteUri); + args.putParcelableArrayList(ARG_DELETE_URIS, list); frag.setArguments(args); @@ -62,7 +75,7 @@ public class DeleteFileDialogFragment extends DialogFragment { public Dialog onCreateDialog(Bundle savedInstanceState) { final FragmentActivity activity = getActivity(); - final Uri[] deleteUris = (Uri[]) getArguments().getParcelableArray(ARG_DELETE_URIS); + final ArrayList deleteUris = getArguments().getParcelableArrayList(ARG_DELETE_URIS); final StringBuilder deleteFileNames = new StringBuilder(); //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 // would not show up! 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(); if (onDeletedListener != null) { diff --git a/OpenKeychain/src/main/res/layout/encrypt_files_fragment.xml b/OpenKeychain/src/main/res/layout/encrypt_files_fragment.xml index 5831d52e1..cb9391b6d 100644 --- a/OpenKeychain/src/main/res/layout/encrypt_files_fragment.xml +++ b/OpenKeychain/src/main/res/layout/encrypt_files_fragment.xml @@ -4,15 +4,10 @@ android:layout_height="match_parent" android:orientation="vertical"> - diff --git a/OpenKeychain/src/main/res/layout/file_list_entry.xml b/OpenKeychain/src/main/res/layout/file_list_entry.xml index ca78ad161..7f0e1e89e 100644 --- a/OpenKeychain/src/main/res/layout/file_list_entry.xml +++ b/OpenKeychain/src/main/res/layout/file_list_entry.xml @@ -1,60 +1,58 @@ - - + + android:id="@+id/thumbnail" + android:layout_alignParentLeft="true" + android:layout_centerVertical="true" + android:scaleType="center" + android:layout_width="48dip" + android:layout_height="48dip" /> + + + android:textColor="?android:attr/textColorSecondary" + android:textAppearance="?android:attr/textAppearanceSmall" + android:ellipsize="end" /> - - + 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" /> + android:id="@+id/action_remove_file_from_list" + android:layout_width="48dip" + android:layout_height="48dip" + android:layout_alignParentRight="true" + android:paddingRight="16dip" + android:paddingLeft="16dip" + android:src="@drawable/ic_close_grey_24dp" + android:clickable="true" + android:layout_centerVertical="true" + android:background="?android:selectableItemBackground" /> \ No newline at end of file diff --git a/OpenKeychain/src/main/res/layout/file_list_entry_add.xml b/OpenKeychain/src/main/res/layout/file_list_entry_add.xml index d7f4513d9..e5eb4c44f 100644 --- a/OpenKeychain/src/main/res/layout/file_list_entry_add.xml +++ b/OpenKeychain/src/main/res/layout/file_list_entry_add.xml @@ -1,21 +1,24 @@ - + android:minHeight="16dp" + android:orientation="horizontal" + android:singleLine="true"> - - \ No newline at end of file + android:drawablePadding="16dp" + android:gravity="left|center_vertical" /> + + \ No newline at end of file