OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.photo_picker; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.content.Context; |
| 9 import android.graphics.Rect; |
| 10 import android.support.v7.widget.GridLayoutManager; |
| 11 import android.support.v7.widget.RecyclerView; |
| 12 import android.support.v7.widget.Toolbar.OnMenuItemClickListener; |
| 13 import android.util.AttributeSet; |
| 14 import android.view.LayoutInflater; |
| 15 import android.view.MenuItem; |
| 16 import android.view.View; |
| 17 import android.widget.RelativeLayout; |
| 18 |
| 19 import org.chromium.chrome.R; |
| 20 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; |
| 21 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
| 22 import org.chromium.ui.PhotoPickerListener; |
| 23 |
| 24 import java.util.ArrayList; |
| 25 import java.util.List; |
| 26 |
| 27 /** |
| 28 * A class for keeping track of common data associated with showing photos in |
| 29 * the photo picker, for example the RecyclerView and the bitmap caches. |
| 30 */ |
| 31 public class PickerCategoryView extends RelativeLayout implements OnMenuItemClic
kListener { |
| 32 private static final int KILOBYTE = 1024; |
| 33 |
| 34 // The dialog that owns us. |
| 35 private PhotoPickerDialog mDialog; |
| 36 |
| 37 // The view containing the RecyclerView and the toolbar, etc. |
| 38 private SelectableListLayout<PickerBitmap> mSelectableListLayout; |
| 39 |
| 40 // Our context. |
| 41 private Context mContext; |
| 42 |
| 43 // The list of images on disk, sorted by last-modified first. |
| 44 private List<PickerBitmap> mPickerBitmaps; |
| 45 |
| 46 // True if multi-selection is allowed in the picker. |
| 47 private boolean mMultiSelectionAllowed; |
| 48 |
| 49 // The callback to notify the listener of decisions reached in the picker. |
| 50 private PhotoPickerListener mListener; |
| 51 |
| 52 // The RecyclerView showing the images. |
| 53 private RecyclerView mRecyclerView; |
| 54 |
| 55 // The {@link PickerAdapter} for the RecyclerView. |
| 56 private PickerAdapter mPickerAdapter; |
| 57 |
| 58 // The {@link SelectionDelegate} keeping track of which images are selected. |
| 59 private SelectionDelegate<PickerBitmap> mSelectionDelegate; |
| 60 |
| 61 /** |
| 62 * The number of columns to show. Note: mColumns and mPadding (see below) sh
ould both be even |
| 63 * numbers or both odd, not a mix (the column padding will not be of uniform
thickness if they |
| 64 * are a mix). |
| 65 */ |
| 66 private int mColumns; |
| 67 |
| 68 // The padding between columns. See also comment for mColumns. |
| 69 private int mPadding; |
| 70 |
| 71 // The size of the bitmaps (equal length for width and height). |
| 72 private int mImageSize; |
| 73 |
| 74 public PickerCategoryView(Context context) { |
| 75 super(context); |
| 76 postConstruction(context); |
| 77 } |
| 78 |
| 79 public PickerCategoryView(Context context, AttributeSet attrs) { |
| 80 super(context, attrs); |
| 81 postConstruction(context); |
| 82 } |
| 83 |
| 84 public PickerCategoryView(Context context, AttributeSet attrs, int defStyle)
{ |
| 85 super(context, attrs, defStyle); |
| 86 postConstruction(context); |
| 87 } |
| 88 |
| 89 /** |
| 90 * A helper function for initializing the PickerCategoryView. |
| 91 * @param context The context to use. |
| 92 */ |
| 93 @SuppressWarnings("unchecked") // mSelectableListLayout |
| 94 private void postConstruction(Context context) { |
| 95 mContext = context; |
| 96 |
| 97 mSelectionDelegate = new SelectionDelegate<PickerBitmap>(); |
| 98 |
| 99 View root = LayoutInflater.from(context).inflate(R.layout.photo_picker_d
ialog, this); |
| 100 mSelectableListLayout = |
| 101 (SelectableListLayout<PickerBitmap>) root.findViewById(R.id.sele
ctable_list); |
| 102 |
| 103 mPickerAdapter = new PickerAdapter(this); |
| 104 mRecyclerView = mSelectableListLayout.initializeRecyclerView(mPickerAdap
ter); |
| 105 mSelectableListLayout.initializeToolbar(R.layout.photo_picker_toolbar, m
SelectionDelegate, |
| 106 R.string.photo_picker_select_images, null, R.id.photo_picker_nor
mal_menu_group, |
| 107 R.id.photo_picker_selection_mode_menu_group, R.color.default_pri
mary_color, false, |
| 108 this); |
| 109 |
| 110 Rect appRect = new Rect(); |
| 111 ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayF
rame(appRect); |
| 112 calculateGridMetrics(appRect.width()); |
| 113 |
| 114 RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mConte
xt, mColumns); |
| 115 mRecyclerView.setHasFixedSize(true); |
| 116 mRecyclerView.setLayoutManager(mLayoutManager); |
| 117 // TODO(finnur): Implement grid spacing. |
| 118 // TODO(finnur): Implement caching. |
| 119 // TODO(finnur): Remove this once the decoder service is in place. |
| 120 prepareBitmaps(); |
| 121 } |
| 122 |
| 123 /** |
| 124 * Initializes the PickerCategoryView object. |
| 125 * @param dialog The dialog showing us. |
| 126 * @param listener The listener who should be notified of actions. |
| 127 * @param multiSelectionAllowed Whether to allow the user to select more tha
n one image. |
| 128 */ |
| 129 public void initialize( |
| 130 PhotoPickerDialog dialog, PhotoPickerListener listener, boolean mult
iSelectionAllowed) { |
| 131 // TODO(finnur): Change selection delegate to support single-selection. |
| 132 |
| 133 mDialog = dialog; |
| 134 mMultiSelectionAllowed = multiSelectionAllowed; |
| 135 mListener = listener; |
| 136 } |
| 137 |
| 138 // OnMenuItemClickListener: |
| 139 |
| 140 @Override |
| 141 public boolean onMenuItemClick(MenuItem item) { |
| 142 if (item.getItemId() == R.id.close_menu_id) { |
| 143 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null
); |
| 144 mDialog.dismiss(); |
| 145 return true; |
| 146 } else if (item.getItemId() == R.id.selection_mode_done_menu_id) { |
| 147 notifyPhotosSelected(); |
| 148 mDialog.dismiss(); |
| 149 return true; |
| 150 } |
| 151 return false; |
| 152 } |
| 153 |
| 154 // Simple accessors: |
| 155 |
| 156 public int getImageSize() { |
| 157 return mImageSize; |
| 158 } |
| 159 |
| 160 public SelectionDelegate<PickerBitmap> getSelectionDelegate() { |
| 161 return mSelectionDelegate; |
| 162 } |
| 163 |
| 164 public List<PickerBitmap> getPickerBitmaps() { |
| 165 return mPickerBitmaps; |
| 166 } |
| 167 |
| 168 public boolean isMultiSelectAllowed() { |
| 169 return mMultiSelectionAllowed; |
| 170 } |
| 171 |
| 172 /** |
| 173 * Notifies the listener that the user selected to launch the gallery. |
| 174 */ |
| 175 public void showGallery() { |
| 176 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY,
null); |
| 177 } |
| 178 |
| 179 /** |
| 180 * Notifies the listener that the user selected to launch the camera intent. |
| 181 */ |
| 182 public void showCamera() { |
| 183 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_CAMERA, n
ull); |
| 184 } |
| 185 |
| 186 /** |
| 187 * Calculates image size and how many columns can fit on-screen. |
| 188 * @param width The total width of the boundary to show the images in. |
| 189 */ |
| 190 private void calculateGridMetrics(int width) { |
| 191 int minSize = |
| 192 mContext.getResources().getDimensionPixelSize(R.dimen.photo_pick
er_tile_min_size); |
| 193 mPadding = mContext.getResources().getDimensionPixelSize(R.dimen.photo_p
icker_tile_gap); |
| 194 mColumns = Math.max(1, (width - mPadding) / (minSize + mPadding)); |
| 195 mImageSize = (width - mPadding * (mColumns + 1)) / (mColumns); |
| 196 |
| 197 // Make sure columns and padding are either both even or both odd. |
| 198 if (((mColumns % 2) == 0) != ((mPadding % 2) == 0)) { |
| 199 mPadding++; |
| 200 } |
| 201 } |
| 202 |
| 203 /** |
| 204 * Prepares bitmaps for loading. |
| 205 */ |
| 206 private void prepareBitmaps() { |
| 207 // TODO(finnur): Use worker thread to fetch bitmaps instead of hard-codi
ng. |
| 208 mPickerBitmaps = new ArrayList<>(); |
| 209 mPickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.TileTypes.GAL
LERY)); |
| 210 mPickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.TileTypes.CAM
ERA)); |
| 211 mPickerBitmaps.add(new PickerBitmap("foo/bar1.jpg", 1, PickerBitmap.Tile
Types.PICTURE)); |
| 212 mPickerBitmaps.add(new PickerBitmap("foo/bar2.jpg", 2, PickerBitmap.Tile
Types.PICTURE)); |
| 213 mPickerBitmaps.add(new PickerBitmap("foo/bar3.jpg", 3, PickerBitmap.Tile
Types.PICTURE)); |
| 214 mPickerBitmaps.add(new PickerBitmap("foo/bar4.jpg", 4, PickerBitmap.Tile
Types.PICTURE)); |
| 215 mPickerAdapter.notifyDataSetChanged(); |
| 216 } |
| 217 |
| 218 /** |
| 219 * Notifies any listeners that one or more photos have been selected. |
| 220 */ |
| 221 private void notifyPhotosSelected() { |
| 222 List<PickerBitmap> selectedFiles = mSelectionDelegate.getSelectedItems()
; |
| 223 String[] photos = new String[selectedFiles.size()]; |
| 224 int i = 0; |
| 225 for (PickerBitmap bitmap : selectedFiles) { |
| 226 photos[i++] = bitmap.getFilePath(); |
| 227 } |
| 228 |
| 229 mListener.onPickerUserAction(PhotoPickerListener.Action.PHOTOS_SELECTED,
photos); |
| 230 } |
| 231 } |
OLD | NEW |