OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
Theresa
2017/04/04 15:48:34
s/2016/2017
Finnur
2017/04/04 18:05:37
Done.
| |
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.support.v7.widget.GridLayoutManager; | |
10 import android.support.v7.widget.RecyclerView; | |
11 import android.support.v7.widget.Toolbar.OnMenuItemClickListener; | |
12 import android.util.AttributeSet; | |
13 import android.view.LayoutInflater; | |
14 import android.view.MenuItem; | |
15 import android.view.View; | |
16 import android.widget.RelativeLayout; | |
17 | |
18 import org.chromium.chrome.R; | |
19 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; | |
20 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; | |
21 import org.chromium.ui.PhotoPickerListener; | |
22 | |
23 import java.util.ArrayList; | |
24 import java.util.List; | |
25 | |
26 /** | |
27 * A class for keeping track of common data associated with showing photos in | |
28 * the photo picker, for example the RecyclerView and the bitmap caches. | |
29 */ | |
30 public class PickerCategoryView extends RelativeLayout implements OnMenuItemClic kListener { | |
31 private static final int KILOBYTE = 1024; | |
32 | |
33 // The dialog that owns us. | |
34 private PhotoPickerDialog mDialog; | |
35 | |
36 // The view containing the recycler view and the toolbar, etc. | |
Theresa
2017/04/04 15:48:34
nit: s/recycler view/RecyclerView
Finnur
2017/04/04 18:05:37
Done.
| |
37 private SelectableListLayout<PickerBitmap> mSelectableListLayout; | |
38 | |
39 // Our context. | |
40 private Context mContext; | |
41 | |
42 // The list of images on disk, sorted by last-modified first. | |
43 private List<PickerBitmap> mPickerBitmaps; | |
44 | |
45 // True if multi-selection is allowed in the picker. | |
46 private boolean mMultiSelectionAllowed; | |
47 | |
48 // The callback to notify the listener of decisions reached in the picker. | |
49 private PhotoPickerListener mListener; | |
50 | |
51 // The recycler view showing the images. | |
Theresa
2017/04/04 15:48:34
s/recycler view/RecyclerView
Finnur
2017/04/04 18:05:37
Done.
| |
52 private RecyclerView mRecyclerView; | |
53 | |
54 // The picker adapter for the RecyclerView. | |
Theresa
2017/04/04 15:48:34
nit: s/picker adapter/PickerAdapter
Finnur
2017/04/04 18:05:37
Done.
| |
55 private PickerAdapter mPickerAdapter; | |
56 | |
57 // The selection delegate keeping track of which images are selected. | |
Theresa
2017/04/04 15:48:34
nit: s/selection delegate/SelectionDelegate
Finnur
2017/04/04 18:05:37
Done.
| |
58 private SelectionDelegate<PickerBitmap> mSelectionDelegate; | |
59 | |
60 /** | |
61 * The number of columns to show. Note: mColumns and mPadding (see below) sh ould both be even | |
62 * numbers or both odd, not a mix (the column padding will not be of uniform thickness if they | |
63 * are a mix). | |
64 */ | |
65 private int mColumns; | |
66 | |
67 // The padding between columns. See also comment for mColumns. | |
68 private int mPadding; | |
69 | |
70 // The size of the bitmaps (equal length for width and height). | |
71 private int mImageSize; | |
72 | |
73 public PickerCategoryView(Context context) { | |
74 super(context); | |
75 init(context); | |
76 } | |
77 | |
78 public PickerCategoryView(Context context, AttributeSet attrs) { | |
79 super(context, attrs); | |
80 init(context); | |
81 } | |
82 | |
83 public PickerCategoryView(Context context, AttributeSet attrs, int defStyle) { | |
84 super(context, attrs, defStyle); | |
85 init(context); | |
86 } | |
87 | |
88 /** | |
89 * A helper function for initializing the PickerCategoryView. | |
90 * @param context The context to use. | |
91 */ | |
92 @SuppressWarnings("unchecked") // mSelectableListLayout | |
93 private void init(Context context) { | |
94 mContext = context; | |
95 | |
96 mSelectionDelegate = new SelectionDelegate<PickerBitmap>(); | |
97 | |
98 View root = LayoutInflater.from(context).inflate(R.layout.photo_picker_d ialog, this); | |
99 mSelectableListLayout = | |
100 (SelectableListLayout<PickerBitmap>) root.findViewById(R.id.sele ctable_list); | |
101 | |
102 mPickerAdapter = new PickerAdapter(this); | |
103 mRecyclerView = mSelectableListLayout.initializeRecyclerView(mPickerAdap ter); | |
104 mSelectableListLayout.initializeToolbar(R.layout.photo_picker_toolbar, m SelectionDelegate, | |
105 R.string.menu_history, null, R.id.file_picker_normal_menu_group, | |
106 R.id.file_picker_selection_mode_menu_group, R.color.default_prim ary_color, false, | |
107 this); | |
108 | |
109 View view = ((Activity) context).getWindow().getDecorView(); | |
110 int width = view.getWidth() - view.getPaddingLeft() - view.getPaddingRig ht(); | |
111 | |
112 calculateGridMetrics(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 * Sets the starting state for 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 setup( | |
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 | |
Theresa
2017/04/04 15:48:34
If close_menu_id is clicked rather than selection_
Finnur
2017/04/04 18:05:37
Done.
| |
143 || item.getItemId() == R.id.selection_mode_done_menu_id) { | |
144 notfiyPhotosSelected(); | |
145 mDialog.dismiss(); | |
146 return true; | |
147 } | |
148 return false; | |
149 } | |
150 | |
151 // Simple accessors: | |
152 | |
153 public int getImageSize() { | |
154 return mImageSize; | |
155 } | |
156 | |
157 public SelectionDelegate<PickerBitmap> getSelectionDelegate() { | |
158 return mSelectionDelegate; | |
159 } | |
160 | |
161 public List<PickerBitmap> getPickerBitmaps() { | |
162 return mPickerBitmaps; | |
163 } | |
164 | |
165 public boolean isMultiSelectAllowed() { | |
166 return mMultiSelectionAllowed; | |
167 } | |
168 | |
169 /** | |
170 * Notifies the caller that the user selected to launch the gallery. | |
171 */ | |
172 public void showGallery() { | |
173 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); | |
174 } | |
175 | |
176 /** | |
177 * Notifies the caller that the user selected to launch the camera intent. | |
178 */ | |
179 public void showCamera() { | |
180 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_CAMERA, n ull); | |
181 } | |
182 | |
183 /** | |
184 * Calculates image size and how many columns can fit on-screen. | |
185 * @param width The total width of the boundary to show the images in. | |
186 */ | |
187 private void calculateGridMetrics(int width) { | |
188 int minSize = | |
189 mContext.getResources().getDimensionPixelSize(R.dimen.file_picke r_tile_min_size); | |
190 mPadding = mContext.getResources().getDimensionPixelSize(R.dimen.file_pi cker_tile_gap); | |
191 mColumns = Math.max(1, (width - mPadding) / (minSize + mPadding)); | |
192 mImageSize = (width - mPadding * (mColumns + 1)) / (mColumns); | |
193 | |
194 // Make sure columns and padding are either both even or both odd. | |
195 if (((mColumns % 2) == 0) != ((mPadding % 2) == 0)) { | |
196 mPadding++; | |
197 } | |
198 } | |
199 | |
200 /** | |
201 * Prepares bitmaps for loading. | |
202 */ | |
203 private void prepareBitmaps() { | |
204 // TODO(finnur): Use worker thread to fetch bitmaps instead of hard-codi ng. | |
205 mPickerBitmaps = new ArrayList<>(); | |
206 mPickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.TileTypes.GAL LERY)); | |
207 mPickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.TileTypes.CAM ERA)); | |
208 mPickerBitmaps.add(new PickerBitmap("foo/bar1.jpg", 1, PickerBitmap.Tile Types.PICTURE)); | |
209 mPickerBitmaps.add(new PickerBitmap("foo/bar2.jpg", 2, PickerBitmap.Tile Types.PICTURE)); | |
210 mPickerBitmaps.add(new PickerBitmap("foo/bar3.jpg", 3, PickerBitmap.Tile Types.PICTURE)); | |
211 mPickerBitmaps.add(new PickerBitmap("foo/bar4.jpg", 4, PickerBitmap.Tile Types.PICTURE)); | |
212 mPickerAdapter.notifyDataSetChanged(); | |
213 } | |
214 | |
215 /** | |
216 * Notifies any listeners that one or more photos have been selected. | |
217 */ | |
218 private void notfiyPhotosSelected() { | |
Theresa
2017/04/04 15:48:34
nit: fix typo with "notfiy"
Finnur
2017/04/04 18:05:37
Done.
| |
219 List<PickerBitmap> selectedFiles = mSelectionDelegate.getSelectedItems() ; | |
220 String[] photos = new String[selectedFiles.size()]; | |
221 int i = 0; | |
222 for (PickerBitmap bitmap : selectedFiles) { | |
223 photos[i++] = bitmap.getFilePath(); | |
224 } | |
225 | |
226 mListener.onPickerUserAction(PhotoPickerListener.Action.PHOTOS_SELECTED, photos); | |
227 } | |
228 } | |
OLD | NEW |