() {
254 | public MediaSourceCaptureImage createFromParcel(Parcel in) {
255 | return new MediaSourceCaptureImage();
256 | }
257 |
258 | public MediaSourceCaptureImage[] newArray(int size) {
259 | return new MediaSourceCaptureImage[size];
260 | }
261 | };
262 |
263 | @Override
264 | public int describeContents() {
265 | return 0;
266 | }
267 |
268 | @Override
269 | public void writeToParcel(Parcel dest, int flags) {
270 | if (mHasTakenPicture) {
271 | dest.writeString(HAS_PICTURE_KEY);
272 | }
273 |
274 | if (mCameraPreview != null) {
275 | mCameraPreview.releaseCamera();
276 | }
277 |
278 | // TODO: save captured images
279 | }
280 | }
281 |
--------------------------------------------------------------------------------
/sample/src/main/java/org/wordpress/mediapickersample/SlidingTabLayout.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.wordpress.mediapickersample;
18 |
19 | import android.content.Context;
20 | import android.graphics.Typeface;
21 | import android.os.Build;
22 | import android.support.v4.view.PagerAdapter;
23 | import android.support.v4.view.ViewPager;
24 | import android.util.AttributeSet;
25 | import android.util.TypedValue;
26 | import android.view.Gravity;
27 | import android.view.LayoutInflater;
28 | import android.view.View;
29 | import android.widget.HorizontalScrollView;
30 | import android.widget.TextView;
31 |
32 | /**
33 | * To be used with ViewPager to provide a tab indicator component which give constant feedback as to
34 | * the user's scroll progress.
35 | *
36 | * To use the component, simply add it to your view hierarchy. Then in your
37 | * {@link android.app.Activity} or {@link android.support.v4.app.Fragment} call
38 | * {@link #setViewPager(ViewPager)} providing it the ViewPager this layout is being used for.
39 | *
40 | * The colors can be customized in two ways. The first and simplest is to provide an array of colors
41 | * via {@link #setSelectedIndicatorColors(int...)} and {@link #setDividerColors(int...)}. The
42 | * alternative is via the {@link TabColorizer} interface which provides you complete control over
43 | * which color is used for any individual position.
44 | *
45 | * The views used as tabs can be customized by calling {@link #setCustomTabView(int, int)},
46 | * providing the layout ID of your custom layout.
47 | */
48 | public class SlidingTabLayout extends HorizontalScrollView {
49 |
50 | /**
51 | * Allows complete control over the colors drawn in the tab layout. Set with
52 | * {@link #setCustomTabColorizer(TabColorizer)}.
53 | */
54 | public interface TabColorizer {
55 |
56 | /**
57 | * @return return the color of the indicator used when {@code position} is selected.
58 | */
59 | int getIndicatorColor(int position);
60 |
61 | /**
62 | * @return return the color of the divider drawn to the right of {@code position}.
63 | */
64 | int getDividerColor(int position);
65 |
66 | }
67 |
68 | private static final int TITLE_OFFSET_DIPS = 24;
69 | private static final int TAB_VIEW_PADDING_DIPS = 16;
70 | private static final int TAB_VIEW_TEXT_SIZE_SP = 12;
71 |
72 | private int mTitleOffset;
73 |
74 | private int mTabViewLayoutId;
75 | private int mTabViewTextViewId;
76 |
77 | private ViewPager mViewPager;
78 | private ViewPager.OnPageChangeListener mViewPagerPageChangeListener;
79 |
80 | private final SlidingTabStrip mTabStrip;
81 |
82 | public SlidingTabLayout(Context context) {
83 | this(context, null);
84 | }
85 |
86 | public SlidingTabLayout(Context context, AttributeSet attrs) {
87 | this(context, attrs, 0);
88 | }
89 |
90 | public SlidingTabLayout(Context context, AttributeSet attrs, int defStyle) {
91 | super(context, attrs, defStyle);
92 |
93 | // Disable the Scroll Bar
94 | setHorizontalScrollBarEnabled(false);
95 | // Make sure that the Tab Strips fills this View
96 | setFillViewport(true);
97 |
98 | mTitleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density);
99 |
100 | mTabStrip = new SlidingTabStrip(context);
101 | addView(mTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
102 | }
103 |
104 | /**
105 | * Set the custom {@link TabColorizer} to be used.
106 | *
107 | * If you only require simple custmisation then you can use
108 | * {@link #setSelectedIndicatorColors(int...)} and {@link #setDividerColors(int...)} to achieve
109 | * similar effects.
110 | */
111 | public void setCustomTabColorizer(TabColorizer tabColorizer) {
112 | mTabStrip.setCustomTabColorizer(tabColorizer);
113 | }
114 |
115 | /**
116 | * Sets the colors to be used for indicating the selected tab. These colors are treated as a
117 | * circular array. Providing one color will mean that all tabs are indicated with the same color.
118 | */
119 | public void setSelectedIndicatorColors(int... colors) {
120 | mTabStrip.setSelectedIndicatorColors(colors);
121 | }
122 |
123 | /**
124 | * Sets the colors to be used for tab dividers. These colors are treated as a circular array.
125 | * Providing one color will mean that all tabs are indicated with the same color.
126 | */
127 | public void setDividerColors(int... colors) {
128 | mTabStrip.setDividerColors(colors);
129 | }
130 |
131 | /**
132 | * Set the {@link ViewPager.OnPageChangeListener}. When using {@link SlidingTabLayout} you are
133 | * required to set any {@link ViewPager.OnPageChangeListener} through this method. This is so
134 | * that the layout can update it's scroll position correctly.
135 | *
136 | * @see ViewPager#setOnPageChangeListener(ViewPager.OnPageChangeListener)
137 | */
138 | public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
139 | mViewPagerPageChangeListener = listener;
140 | }
141 |
142 | /**
143 | * Set the custom layout to be inflated for the tab views.
144 | *
145 | * @param layoutResId Layout id to be inflated
146 | * @param textViewId id of the {@link TextView} in the inflated view
147 | */
148 | public void setCustomTabView(int layoutResId, int textViewId) {
149 | mTabViewLayoutId = layoutResId;
150 | mTabViewTextViewId = textViewId;
151 | }
152 |
153 | /**
154 | * Sets the associated view pager. Note that the assumption here is that the pager content
155 | * (number of tabs and tab titles) does not change after this call has been made.
156 | */
157 | public void setViewPager(ViewPager viewPager) {
158 | mTabStrip.removeAllViews();
159 |
160 | mViewPager = viewPager;
161 | if (viewPager != null) {
162 | viewPager.setOnPageChangeListener(new InternalViewPagerListener());
163 | populateTabStrip();
164 | }
165 | }
166 |
167 | /**
168 | * Create a default view to be used for tabs. This is called if a custom tab view is not set via
169 | * {@link #setCustomTabView(int, int)}.
170 | */
171 | protected TextView createDefaultTabView(Context context) {
172 | TextView textView = new TextView(context);
173 | textView.setGravity(Gravity.CENTER);
174 | textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
175 | textView.setTypeface(Typeface.DEFAULT_BOLD);
176 |
177 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
178 | // If we're running on Honeycomb or newer, then we can use the Theme's
179 | // selectableItemBackground to ensure that the View has a pressed state
180 | TypedValue outValue = new TypedValue();
181 | getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
182 | outValue, true);
183 | textView.setBackgroundResource(outValue.resourceId);
184 | }
185 |
186 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
187 | // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
188 | textView.setAllCaps(true);
189 | }
190 |
191 | int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
192 | textView.setPadding(padding, padding, padding, padding);
193 |
194 | return textView;
195 | }
196 |
197 | private void populateTabStrip() {
198 | final PagerAdapter adapter = mViewPager.getAdapter();
199 | final View.OnClickListener tabClickListener = new TabClickListener();
200 |
201 | for (int i = 0; i < adapter.getCount(); i++) {
202 | View tabView = null;
203 | TextView tabTitleView = null;
204 |
205 | if (mTabViewLayoutId != 0) {
206 | // If there is a custom tab view layout id set, try and inflate it
207 | tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
208 | false);
209 | tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
210 | }
211 |
212 | if (tabView == null) {
213 | tabView = createDefaultTabView(getContext());
214 | }
215 |
216 | if (tabTitleView == null && TextView.class.isInstance(tabView)) {
217 | tabTitleView = (TextView) tabView;
218 | }
219 |
220 | tabTitleView.setText(adapter.getPageTitle(i));
221 | tabView.setOnClickListener(tabClickListener);
222 |
223 | mTabStrip.addView(tabView);
224 | }
225 | }
226 |
227 | @Override
228 | protected void onAttachedToWindow() {
229 | super.onAttachedToWindow();
230 |
231 | if (mViewPager != null) {
232 | scrollToTab(mViewPager.getCurrentItem(), 0);
233 | }
234 | }
235 |
236 | private void scrollToTab(int tabIndex, int positionOffset) {
237 | final int tabStripChildCount = mTabStrip.getChildCount();
238 | if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
239 | return;
240 | }
241 |
242 | View selectedChild = mTabStrip.getChildAt(tabIndex);
243 | if (selectedChild != null) {
244 | int targetScrollX = selectedChild.getLeft() + positionOffset;
245 |
246 | if (tabIndex > 0 || positionOffset > 0) {
247 | // If we're not at the first child and are mid-scroll, make sure we obey the offset
248 | targetScrollX -= mTitleOffset;
249 | }
250 |
251 | scrollTo(targetScrollX, 0);
252 | }
253 | }
254 |
255 | private class InternalViewPagerListener implements ViewPager.OnPageChangeListener {
256 | private int mScrollState;
257 |
258 | @Override
259 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
260 | int tabStripChildCount = mTabStrip.getChildCount();
261 | if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) {
262 | return;
263 | }
264 |
265 | mTabStrip.onViewPagerPageChanged(position, positionOffset);
266 |
267 | View selectedTitle = mTabStrip.getChildAt(position);
268 | int extraOffset = (selectedTitle != null)
269 | ? (int) (positionOffset * selectedTitle.getWidth())
270 | : 0;
271 | scrollToTab(position, extraOffset);
272 |
273 | if (mViewPagerPageChangeListener != null) {
274 | mViewPagerPageChangeListener.onPageScrolled(position, positionOffset,
275 | positionOffsetPixels);
276 | }
277 | }
278 |
279 | @Override
280 | public void onPageScrollStateChanged(int state) {
281 | mScrollState = state;
282 |
283 | if (mViewPagerPageChangeListener != null) {
284 | mViewPagerPageChangeListener.onPageScrollStateChanged(state);
285 | }
286 | }
287 |
288 | @Override
289 | public void onPageSelected(int position) {
290 | if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
291 | mTabStrip.onViewPagerPageChanged(position, 0f);
292 | scrollToTab(position, 0);
293 | }
294 |
295 | if (mViewPagerPageChangeListener != null) {
296 | mViewPagerPageChangeListener.onPageSelected(position);
297 | }
298 | }
299 |
300 | }
301 |
302 | private class TabClickListener implements View.OnClickListener {
303 | @Override
304 | public void onClick(View v) {
305 | for (int i = 0; i < mTabStrip.getChildCount(); i++) {
306 | if (v == mTabStrip.getChildAt(i)) {
307 | mViewPager.setCurrentItem(i);
308 | return;
309 | }
310 | }
311 | }
312 | }
313 |
314 | }
315 |
--------------------------------------------------------------------------------
/mediapicker/src/main/java/org/wordpress/mediapicker/MediaUtils.java:
--------------------------------------------------------------------------------
1 | package org.wordpress.mediapicker;
2 |
3 | import android.content.ContentResolver;
4 | import android.database.Cursor;
5 | import android.graphics.Bitmap;
6 | import android.graphics.BitmapFactory;
7 | import android.graphics.Matrix;
8 | import android.media.ThumbnailUtils;
9 | import android.net.Uri;
10 | import android.os.AsyncTask;
11 | import android.provider.MediaStore;
12 | import android.view.animation.AlphaAnimation;
13 | import android.view.animation.Animation;
14 | import android.widget.ImageView;
15 |
16 | import com.android.volley.toolbox.ImageLoader;
17 |
18 | import java.lang.ref.WeakReference;
19 | import java.util.ArrayList;
20 | import java.util.HashMap;
21 | import java.util.LinkedList;
22 | import java.util.List;
23 | import java.util.Map;
24 | import java.util.Queue;
25 |
26 | public class MediaUtils {
27 | private static final long FADE_TIME_MS = 250;
28 |
29 | public static void fadeInImage(ImageView imageView, Bitmap image) {
30 | fadeInImage(imageView, image, FADE_TIME_MS);
31 | }
32 |
33 | public static void fadeInImage(ImageView imageView, Bitmap image, long duration) {
34 | if (imageView != null) {
35 | imageView.setImageBitmap(image);
36 | Animation alpha = new AlphaAnimation(0.25f, 1.0f);
37 | alpha.setDuration(duration);
38 | imageView.startAnimation(alpha);
39 | // Use the implementation below if you can figure out how to make it work on all devices
40 | // My Galaxy S3 (4.1.2) would not animate
41 | // imageView.setImageBitmap(image);
42 | // ObjectAnimator.ofFloat(imageView, View.ALPHA, 0.25f, 1.0f).setDuration(duration).start();
43 | }
44 | }
45 |
46 | public static Cursor getMediaStoreThumbnails(ContentResolver contentResolver, String[] columns) {
47 | if (contentResolver == null) return null;
48 | Uri thumbnailUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
49 | return MediaStore.Images.Thumbnails.query(contentResolver, thumbnailUri, columns);
50 | }
51 |
52 | public static Cursor getDeviceMediaStoreVideos(ContentResolver contentResolver, String[] columns) {
53 | if (contentResolver == null) return null;
54 | Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
55 | return MediaStore.Video.query(contentResolver, videoUri, columns);
56 | }
57 |
58 | public static Map getMediaStoreThumbnailData(Cursor thumbnailCursor,
59 | String dataColumnName,
60 | String idColumnName) {
61 | final Map data = new HashMap<>();
62 |
63 | if (thumbnailCursor != null) {
64 | if (thumbnailCursor.moveToFirst()) {
65 | do {
66 | int dataColumnIndex = thumbnailCursor.getColumnIndex(dataColumnName);
67 | int imageIdColumnIndex = thumbnailCursor.getColumnIndex(idColumnName);
68 |
69 | if (dataColumnIndex != -1 && imageIdColumnIndex != -1) {
70 | data.put(thumbnailCursor.getString(imageIdColumnIndex),
71 | thumbnailCursor.getString(dataColumnIndex));
72 | }
73 | } while (thumbnailCursor.moveToNext());
74 | }
75 |
76 | thumbnailCursor.close();
77 | }
78 |
79 | return data;
80 | }
81 |
82 | public static List createMediaItems(Map thumbnailData, Cursor mediaCursor, int type) {
83 | final List mediaItems = new ArrayList<>();
84 | final List ids = new ArrayList<>();
85 |
86 | if (mediaCursor != null) {
87 | if (mediaCursor.moveToFirst()) {
88 | do {
89 | MediaItem newContent = type == BackgroundFetchThumbnail.TYPE_IMAGE ?
90 | getMediaItemFromImageCursor(mediaCursor, thumbnailData) :
91 | getMediaItemFromVideoCursor(mediaCursor, thumbnailData);
92 |
93 | if (newContent != null && !ids.contains(newContent.getTag())) {
94 | mediaItems.add(newContent);
95 | ids.add(newContent.getTag());
96 | }
97 | } while (mediaCursor.moveToNext());
98 | }
99 |
100 | mediaCursor.close();
101 | }
102 |
103 | return mediaItems;
104 | }
105 |
106 | public static void fadeMediaItemImageIntoView(Uri imageSource, ImageLoader.ImageCache cache,
107 | ImageView imageView, MediaItem mediaItem,
108 | int width, int height, int type) {
109 | if (imageSource != null && !imageSource.toString().isEmpty()) {
110 | Bitmap imageBitmap = null;
111 | if (cache != null) {
112 | imageBitmap = cache.getBitmap(imageSource.toString());
113 | }
114 |
115 | if (imageBitmap == null) {
116 | imageView.setImageResource(R.drawable.media_item_placeholder);
117 | BackgroundFetchThumbnail bgDownload =
118 | new MediaUtils.BackgroundFetchThumbnail(imageView,
119 | cache,
120 | type,
121 | width,
122 | height,
123 | mediaItem.getRotation());
124 | imageView.setTag(bgDownload);
125 | bgDownload.executeWithLimit(imageSource);
126 | } else {
127 | fadeInImage(imageView, imageBitmap);
128 | }
129 | } else {
130 | imageView.setTag(null);
131 | imageView.setImageResource(R.drawable.ic_now_wallpaper_white);
132 | }
133 | }
134 |
135 | /**
136 | * Implementation of AsyncTask that limits the number of executions. Child classes must call
137 | * super.* methods for all of onPreExecute/doInBackground/onPostExecute/onCancelled or none.
138 | *
139 | * Subclasses are passed a generic Object in startExecution and it is expected to be cast to
140 | * the appropriate type when calling execute/executeOnExecutor.
141 | */
142 | public static abstract class LimitedBackgroundOperation
143 | extends AsyncTask {
144 | private static final int MAX_FETCHES = 16;
145 | private static final Queue sFetchQueue = new LinkedList<>();
146 |
147 | private static int sNumFetching = 0;
148 |
149 | private Params mParams;
150 |
151 | @Override
152 | protected final void onPreExecute() {
153 | performPreExecute();
154 | ++sNumFetching;
155 | }
156 |
157 | @Override
158 | protected final Result doInBackground(Params... params) {
159 | return performBackgroundOperation(params);
160 | }
161 |
162 | @Override
163 | protected final void onPostExecute(Result result) {
164 | performPostExecute(result);
165 | continueExclusiveExecution();
166 | }
167 |
168 | @Override
169 | protected final void onCancelled(Result result) {
170 | performCancelled(result);
171 | continueExclusiveExecution();
172 | }
173 |
174 | protected void performPreExecute() {
175 | }
176 |
177 | // Required
178 | protected abstract Result performBackgroundOperation(Params... params);
179 |
180 | protected void performPostExecute(Result result) {
181 | }
182 |
183 | protected void performCancelled(Result result) {
184 | }
185 |
186 | // Should invoke execute or executeOnExecutor
187 | public abstract void startExecution(Object params);
188 |
189 | public void executeWithLimit(Params params) {
190 | mParams = params;
191 | startExclusiveExecution();
192 | }
193 |
194 | private void startExclusiveExecution() {
195 | if (sNumFetching < MAX_FETCHES) {
196 | startExecution(mParams);
197 | } else {
198 | sFetchQueue.add(this);
199 | }
200 | }
201 |
202 | private void continueExclusiveExecution() {
203 | if (--sNumFetching < MAX_FETCHES && sFetchQueue.size() > 0) {
204 | LimitedBackgroundOperation next = sFetchQueue.remove();
205 | if (next != null) {
206 | next.startExecution(next.mParams);
207 | }
208 | }
209 | }
210 | }
211 |
212 | public static class BackgroundFetchThumbnail extends LimitedBackgroundOperation {
213 | public static final int TYPE_IMAGE = 0;
214 | public static final int TYPE_VIDEO = 1;
215 |
216 | private WeakReference mReference;
217 | private ImageLoader.ImageCache mCache;
218 | private int mType;
219 | private int mWidth;
220 | private int mHeight;
221 | private int mRotation;
222 |
223 | public BackgroundFetchThumbnail(ImageView resultStore, ImageLoader.ImageCache cache, int type, int width, int height, int rotation) {
224 | mReference = new WeakReference<>(resultStore);
225 | mCache = cache;
226 | mType = type;
227 | mWidth = width;
228 | mHeight = height;
229 | mRotation = rotation;
230 | }
231 |
232 | @Override
233 | protected Bitmap performBackgroundOperation(Uri... params) {
234 | String uri = params[0].toString();
235 | Bitmap bitmap = null;
236 |
237 | if (mType == TYPE_IMAGE) {
238 | BitmapFactory.Options options = new BitmapFactory.Options();
239 | options.inJustDecodeBounds = true;
240 | BitmapFactory.decodeFile(uri, options);
241 | options.inJustDecodeBounds = false;
242 | options.inSampleSize = calculateInSampleSize(options);
243 | bitmap = BitmapFactory.decodeFile(uri, options);
244 |
245 | if (bitmap != null) {
246 | Matrix rotation = new Matrix();
247 | rotation.setRotate(mRotation, bitmap.getWidth() / 2.0f, bitmap.getHeight() / 2.0f);
248 | bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotation, false);
249 | }
250 | } else if (mType == TYPE_VIDEO) {
251 | // MICRO_KIND = 96 x 96
252 | // MINI_KIND = 512 x 384
253 | bitmap = ThumbnailUtils.createVideoThumbnail(uri, MediaStore.Video.Thumbnails.MINI_KIND);
254 | }
255 |
256 | if (mCache != null && bitmap != null) {
257 | mCache.putBitmap(uri, bitmap);
258 | }
259 |
260 | return bitmap;
261 | }
262 |
263 | @Override
264 | protected void performPostExecute(Bitmap result) {
265 | ImageView imageView = mReference.get();
266 |
267 | if (imageView != null) {
268 | if (imageView.getTag() == this) {
269 | imageView.setTag(null);
270 | if (result == null) {
271 | imageView.setImageResource(R.drawable.ic_now_wallpaper_white);
272 | } else {
273 | fadeInImage(imageView, result);
274 | }
275 | }
276 | }
277 | }
278 |
279 | @Override
280 | public void startExecution(Object params) {
281 | if (!(params instanceof Uri)) {
282 | throw new IllegalArgumentException("Params must of type Uri");
283 | }
284 |
285 | executeOnExecutor(THREAD_POOL_EXECUTOR, (Uri) params);
286 | }
287 |
288 | // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
289 | private int calculateInSampleSize(BitmapFactory.Options options) {
290 | // Raw height and width of image
291 | final int height = options.outHeight;
292 | final int width = options.outWidth;
293 | int inSampleSize = 1;
294 |
295 | if (height > mHeight || width > mWidth) {
296 |
297 | final int halfHeight = height / 2;
298 | final int halfWidth = width / 2;
299 |
300 | // Calculate the largest inSampleSize value that is a power of 2 and keeps both
301 | // height and width larger than the requested height and width.
302 | while ((halfHeight / inSampleSize) > mHeight
303 | && (halfWidth / inSampleSize) > mWidth) {
304 | inSampleSize *= 2;
305 | }
306 | }
307 |
308 | return inSampleSize;
309 | }
310 | }
311 |
312 | private static MediaItem getMediaItemFromVideoCursor(Cursor videoCursor, Map thumbnailData) {
313 | MediaItem newContent = null;
314 |
315 | int videoIdColumnIndex = videoCursor.getColumnIndex(MediaStore.Video.Media._ID);
316 | int videoDataColumnIndex = videoCursor.getColumnIndex(MediaStore.Video.Media.DATA);
317 |
318 | if (videoIdColumnIndex != -1) {
319 | newContent = new MediaItem();
320 | newContent.setTag(videoCursor.getString(videoIdColumnIndex));
321 | newContent.setTitle("");
322 |
323 | if (videoDataColumnIndex != -1) {
324 | String videoSource = videoCursor.getString(videoDataColumnIndex);
325 | if (videoSource == null) {
326 | return null;
327 | }
328 | newContent.setSource(Uri.parse(videoSource));
329 | }
330 | if (thumbnailData.containsKey(newContent.getTag())) {
331 | String thumbnailSource = thumbnailData.get(newContent.getTag());
332 | if (thumbnailSource == null) {
333 | return null;
334 | }
335 | newContent.setPreviewSource(Uri.parse(thumbnailSource));
336 | }
337 | }
338 |
339 | return newContent;
340 | }
341 |
342 | private static MediaItem getMediaItemFromImageCursor(Cursor imageCursor, Map thumbnailData) {
343 | MediaItem newContent = null;
344 |
345 | int imageIdColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
346 | int imageDataColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
347 | int imageOrientationColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
348 |
349 | if (imageIdColumnIndex != -1) {
350 | newContent = new MediaItem();
351 | newContent.setTag(imageCursor.getString(imageIdColumnIndex));
352 | newContent.setTitle("");
353 |
354 | if (imageDataColumnIndex != -1) {
355 | String imageSource = imageCursor.getString(imageDataColumnIndex);
356 | if (imageSource == null) {
357 | return null;
358 | }
359 | newContent.setSource(Uri.parse(imageSource));
360 | }
361 | if (thumbnailData.containsKey(newContent.getTag())) {
362 | String thumbnailSource = thumbnailData.get(newContent.getTag());
363 | if (thumbnailSource == null) {
364 | return null;
365 | }
366 | newContent.setPreviewSource(Uri.parse(thumbnailSource));
367 | }
368 | if (imageOrientationColumnIndex != -1) {
369 | newContent.setRotation(imageCursor.getInt(imageOrientationColumnIndex));
370 | }
371 | }
372 |
373 | return newContent;
374 | }
375 | }
376 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
341 |
--------------------------------------------------------------------------------
/mediapicker/src/main/java/org/wordpress/mediapicker/MediaPickerFragment.java:
--------------------------------------------------------------------------------
1 | package org.wordpress.mediapicker;
2 |
3 | import android.app.Activity;
4 | import android.app.Fragment;
5 | import android.content.res.Resources;
6 | import android.graphics.drawable.Drawable;
7 | import android.os.Bundle;
8 | import android.view.ActionMode;
9 | import android.view.LayoutInflater;
10 | import android.view.Menu;
11 | import android.view.MenuInflater;
12 | import android.view.MenuItem;
13 | import android.view.View;
14 | import android.view.ViewGroup;
15 | import android.widget.AbsListView;
16 | import android.widget.AdapterView;
17 | import android.widget.TextView;
18 |
19 | import com.android.volley.toolbox.ImageLoader;
20 |
21 | import org.wordpress.mediapicker.source.MediaSource;
22 |
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | /**
27 | * MediaPickerFragment tracks a collection of MediaSources and responds to changes via the
28 | * {@link org.wordpress.mediapicker.source.MediaSource.OnMediaChange} interface.
29 | *
30 | * If the host Activity implements {@link org.wordpress.mediapicker.MediaPickerFragment.OnMediaSelected}
31 | * it will automatically be set as the listener, otherwise a setter is provided. This interface is
32 | * how user intent is delivered, while not having a listener won't break anything it's not very useful.
33 | *
34 | * By default the {@link org.wordpress.mediapicker.MediaSourceAdapter} is shown within
35 | * a {@link android.widget.GridView}, but a subclass of {@link android.widget.AbsListView} may be provided
36 | * with id=media_adapter_view. A subclass of {@link android.widget.TextView} may also be provided
37 | * for the empty view; id=media_empty_view. You can even provide a subclass of
38 | * {@link org.wordpress.mediapicker.MediaSourceAdapter}.
39 | *
40 | * Menu items may be provided for Action Mode and their selection will be alerted with onMenuItemSelected.
41 | * A selection confirmation button is automatically added and will call onMediaSelectionConfirmed when selected.
42 | */
43 |
44 | public class MediaPickerFragment extends Fragment
45 | implements AdapterView.OnItemClickListener,
46 | AbsListView.MultiChoiceModeListener,
47 | MediaSource.OnMediaChange {
48 | public static final String KEY_SELECTED_CONTENT = "key-selected-content";
49 | public static final String KEY_MEDIA_SOURCES = "key-media-sources";
50 | public static final String KEY_CUSTOM_LAYOUT = "key-custom-view";
51 | public static final String KEY_ACTION_MODE_MENU = "key-action-mode-menu";
52 | public static final String KEY_LOADING_TEXT = "key-loading-text";
53 | public static final String KEY_EMPTY_TEXT = "key-empty-text";
54 | public static final String KEY_ERROR_TEXT = "key-error-text";
55 |
56 | // Default layout to be used if a custom layout is not provided
57 | private static final int DEFAULT_VIEW = R.layout.media_picker_fragment;
58 |
59 | /**
60 | * Interface to respond to user intent and provide a caching mechanism for the fragment.
61 | */
62 | public interface OnMediaSelected {
63 | // Called when the first item is selected
64 | public void onMediaSelectionStarted();
65 | // Called when a new item is selected
66 | public void onMediaSelected(MediaItem mediaContent, boolean selected);
67 | // Called when the user confirms content selection
68 | public void onMediaSelectionConfirmed(ArrayList mediaContent);
69 | // Called when the last selected item is deselected
70 | public void onMediaSelectionCancelled();
71 | // Called when a menu item has been tapped
72 | public boolean onMenuItemSelected(MenuItem menuItem, ArrayList selectedContent);
73 | // Should handle null image cache
74 | public ImageLoader.ImageCache getImageCache();
75 | }
76 |
77 | // Current media sources and selected content from the sources
78 | private final ArrayList mMediaSources;
79 | private final ArrayList mSelectedContent;
80 |
81 | // Callbacks for media selection events, use to track user intent
82 | private OnMediaSelected mListener;
83 |
84 | // Required state tracking to prevent OnMediaSelectionCancelled from being called erroneously
85 | private boolean mConfirmed;
86 |
87 | // Views utilized by this fragment
88 | private TextView mEmptyView;
89 | private AbsListView mAdapterView;
90 |
91 | // Adapter for showing MediaSource content in the AdapterView
92 | private MediaSourceAdapter mAdapter;
93 |
94 | // Customizable view resources, some default behavior is defined as described in the docs
95 | private int mCustomLayout;
96 | private int mActionModeMenu;
97 |
98 | // Customizable status text messages, default values are provided
99 | private String mLoadingText;
100 | private String mEmptyText;
101 | private String mErrorText;
102 |
103 | public MediaPickerFragment() {
104 | super();
105 |
106 | mCustomLayout = -1;
107 | mActionModeMenu = -1;
108 | mMediaSources = new ArrayList<>();
109 | mSelectedContent = new ArrayList<>();
110 | }
111 |
112 | @Override
113 | public void onAttach(Activity activity) {
114 | super.onAttach(activity);
115 |
116 | // Per the documentation, the host Activity is the default listener
117 | if (mListener == null && activity instanceof OnMediaSelected) {
118 | mListener = (OnMediaSelected) activity;
119 | }
120 | }
121 |
122 | @Override
123 | public void onCreate(Bundle savedInstanceState) {
124 | super.onCreate(savedInstanceState);
125 |
126 | // If this is not being restored from a previous state use arguments to set state
127 | if (savedInstanceState == null) {
128 | savedInstanceState = getArguments();
129 | }
130 |
131 | restoreFromBundle(savedInstanceState);
132 |
133 | setDefaultTextValues();
134 | }
135 |
136 | @Override
137 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
138 | super.onCreateView(inflater, container, savedInstanceState);
139 |
140 | restoreFromBundle(savedInstanceState);
141 |
142 | int viewToInflate = mCustomLayout < 0 ? DEFAULT_VIEW : mCustomLayout;
143 | View mediaPickerView = inflater.inflate(viewToInflate, container, false);
144 | if (mediaPickerView != null) {
145 | if (mEmptyView == null) {
146 | mEmptyView = (TextView) mediaPickerView.findViewById(R.id.media_empty_view);
147 | if (mMediaSources.size() == 0) {
148 | updateEmptyView(getString(R.string.no_media_sources));
149 | } else {
150 | updateEmptyView(mLoadingText);
151 | }
152 | } else {
153 | mEmptyView = (TextView) mediaPickerView.findViewById(R.id.media_empty_view);
154 | if (mAdapter.getCount() == 0) {
155 | updateEmptyView(mEmptyText);
156 | }
157 | }
158 |
159 | mAdapterView = (AbsListView) mediaPickerView.findViewById(R.id.media_adapter_view);
160 | if (mAdapterView != null) {
161 | layoutAdapterView();
162 |
163 | if (mAdapter == null) {
164 | generateAdapter();
165 | } else {
166 | mAdapterView.setAdapter(mAdapter);
167 | mAdapter.notifyDataSetChanged();
168 | }
169 | toggleEmptyVisibility();
170 | }
171 | }
172 |
173 | return mediaPickerView;
174 | }
175 |
176 | @Override
177 | public void onDestroy() {
178 | super.onDestroy();
179 |
180 | cleanupMediaSources();
181 | }
182 |
183 | @Override
184 | public void onSaveInstanceState(Bundle outState) {
185 | super.onSaveInstanceState(outState);
186 |
187 | if (mSelectedContent.size() > 0) {
188 | outState.putParcelableArrayList(KEY_SELECTED_CONTENT, mSelectedContent);
189 | }
190 |
191 | if (mMediaSources.size() > 0) {
192 | outState.putParcelableArrayList(KEY_MEDIA_SOURCES, mMediaSources);
193 | }
194 |
195 | if (mCustomLayout > -1) {
196 | outState.putInt(KEY_CUSTOM_LAYOUT, mCustomLayout);
197 | }
198 |
199 | if (mActionModeMenu > -1) {
200 | outState.putInt(KEY_ACTION_MODE_MENU, mActionModeMenu);
201 | }
202 |
203 | if (mLoadingText != null) {
204 | outState.putString(KEY_LOADING_TEXT, mLoadingText);
205 | }
206 |
207 | if (mErrorText != null) {
208 | outState.putString(KEY_ERROR_TEXT, mErrorText);
209 | }
210 |
211 | if (mEmptyText != null) {
212 | outState.putString(KEY_EMPTY_TEXT, mEmptyText);
213 | }
214 | }
215 |
216 | @Override
217 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
218 | if (!notifyMediaSelected(position, true)) {
219 | notifyMediaSelectionConfirmed();
220 | }
221 | }
222 |
223 | @Override
224 | public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
225 | notifyMediaSelected(position, checked);
226 |
227 | if (checked) {
228 | if (!mSelectedContent.contains(mAdapter.getItem(position))) {
229 | mSelectedContent.add(mAdapter.getItem(position));
230 | }
231 | } else {
232 | mSelectedContent.remove(mAdapter.getItem(position));
233 | }
234 |
235 | mode.setTitle(getActivity().getTitle() + " (" + mSelectedContent.size() + ")");
236 | }
237 |
238 | @Override
239 | public boolean onCreateActionMode(ActionMode mode, Menu menu) {
240 | mode.setTitle(getActivity().getTitle());
241 | getActivity().onActionModeStarted(mode);
242 | inflateActionModeMenu(menu);
243 |
244 | return true;
245 | }
246 |
247 | @Override
248 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
249 | notifyMediaSelectionStarted();
250 | mConfirmed = false;
251 |
252 | return true;
253 | }
254 |
255 | @Override
256 | public boolean onActionItemClicked(ActionMode mode, MenuItem menuItem) {
257 | if (menuItem.getItemId() == R.id.menu_media_selection_confirmed) {
258 | notifyMediaSelectionConfirmed();
259 | mode.finish();
260 | return true;
261 | } else if (mListener != null) {
262 | return mListener.onMenuItemSelected(menuItem, mSelectedContent);
263 | }
264 |
265 | return false;
266 | }
267 |
268 | @Override
269 | public void onDestroyActionMode(ActionMode mode) {
270 | if (!mConfirmed) {
271 | notifyMediaSelectionCancelled();
272 | }
273 |
274 | mSelectedContent.clear();
275 |
276 | getActivity().onActionModeFinished(mode);
277 | }
278 |
279 | @Override
280 | public void onMediaLoaded(boolean success) {
281 | if (success) {
282 | if (mAdapter != null && mAdapter.getCount() > 0) {
283 | toggleEmptyVisibility();
284 | mAdapter.notifyDataSetChanged();
285 | } else {
286 | updateEmptyView(mEmptyText);
287 | }
288 | } else {
289 | updateEmptyView(mErrorText);
290 | }
291 | }
292 |
293 | @Override
294 | public void onMediaAdded(MediaSource source, List addedItems) {
295 | toggleEmptyVisibility();
296 | mAdapter.notifyDataSetChanged();
297 | }
298 |
299 | @Override
300 | public void onMediaRemoved(MediaSource source, List removedItems) {
301 | toggleEmptyVisibility();
302 | mAdapter.notifyDataSetChanged();
303 | }
304 |
305 | @Override
306 | public void onMediaChanged(MediaSource source, List changedItems) {
307 | mAdapter.notifyDataSetChanged();
308 | }
309 |
310 | /**
311 | * Sets the listener. Calling this method will overwrite the current listener.
312 | *
313 | * @param listener
314 | * the new listener, can be null
315 | */
316 | public void setListener(OnMediaSelected listener) {
317 | mListener = listener;
318 | }
319 |
320 | /**
321 | * Sets the {@link org.wordpress.mediapicker.source.MediaSource}'s to be presented. The current
322 | * sources and selected content are cleaned up and cleared before the new list added.
323 | *
324 | * @param mediaSources
325 | * the new sources
326 | */
327 | public void setMediaSources(ArrayList mediaSources) {
328 | mSelectedContent.clear();
329 |
330 | cleanupMediaSources();
331 | mMediaSources.clear();
332 |
333 | // a null parameter results in a NullPointerException
334 | if (mediaSources != null) {
335 | mMediaSources.addAll(mediaSources);
336 | generateAdapter();
337 | }
338 | }
339 |
340 | /**
341 | * Sets the menu resource to be inflated when in Action Mode.
342 | *
343 | * @param id
344 | * the ID of the menu resource, any value < 0 will use the default menu
345 | */
346 | public void setActionModeMenu(int id) {
347 | mActionModeMenu = id;
348 | }
349 |
350 | /**
351 | * Sets the layout resource to be used to display media content.
352 | *
353 | * @param customLayout
354 | * the ID of the layout resource, any value < 0 will use the default layout
355 | */
356 | public void setCustomLayout(int customLayout) {
357 | mCustomLayout = customLayout;
358 | }
359 |
360 | /**
361 | * Sets the text to be displayed while media content is loading.
362 | *
363 | * @param loadingText
364 | * the text to display while media is loading, can be null
365 | */
366 | public void setLoadingText(String loadingText) {
367 | mLoadingText = loadingText;
368 | }
369 |
370 | /**
371 | * Same as calling {@link #setLoadingText(String)} with {@link #getString(int)} for resId.
372 | * Passing resId < 0 will set the text to null.
373 | *
374 | * @param resId
375 | * resource ID of the string to display while media is loading
376 | */
377 | public void setLoadingText(int resId) {
378 | if (resId < 0) {
379 | setLoadingText(null);
380 | } else {
381 | setLoadingText(getString(resId));
382 | }
383 | }
384 |
385 | /**
386 | * Sets the text to be displayed if there is no media content to show.
387 | *
388 | * @param emptyText
389 | * the text to display when there is no media, can be null
390 | */
391 | public void setEmptyText(String emptyText) {
392 | mEmptyText = emptyText;
393 | }
394 |
395 | /**
396 | * Same as calling {@link #setEmptyText(String)} with {@link #getString(int)} for resId.
397 | * Passing resId < 0 will set the text to null.
398 | *
399 | * @param resId
400 | * resource ID of the string to display when there is no media
401 | */
402 | public void setEmptyText(int resId) {
403 | if (resId < 0) {
404 | setEmptyText(null);
405 | } else {
406 | setEmptyText(getString(resId));
407 | }
408 | }
409 |
410 | /**
411 | * Sets the text to be displayed if an error occurs while loading media.
412 | *
413 | * @param errorText
414 | * the text to display when an error occurs while loading, can be null
415 | */
416 | public void setErrorText(String errorText) {
417 | mErrorText = errorText;
418 | }
419 |
420 | /**
421 | * Same as calling {@link #setErrorText(String)} with {@link #getString(int)} for resId.
422 | * Passing resId < 0 will set the text to null.
423 | *
424 | * @param resId
425 | * resource ID of the string to display when there is an error loading media
426 | */
427 | public void setErrorText(int resId) {
428 | if (resId < 0) {
429 | setErrorText(null);
430 | } else {
431 | setErrorText(getString(resId));
432 | }
433 | }
434 |
435 | /**
436 | * Sets the adapter.
437 | *
438 | * @param adapter
439 | * the new adapter
440 | */
441 | public void setAdapter(MediaSourceAdapter adapter) {
442 | mAdapter = adapter;
443 |
444 | if (mAdapterView != null) {
445 | mAdapterView.setAdapter(mAdapter);
446 | }
447 | }
448 |
449 | private void updateEmptyView(String text) {
450 | if (mEmptyView != null) {
451 | mEmptyView.setText(text);
452 | }
453 | }
454 |
455 | /**
456 | * Restores state from a given {@link android.os.Bundle}. Checks for media sources, selected
457 | * content, custom view, custom action mode menu, and custom empty text.
458 | *
459 | * @param bundle
460 | * Bundle containing all the data, can be null
461 | */
462 | private void restoreFromBundle(Bundle bundle) {
463 | if (bundle != null) {
464 | if (bundle.containsKey(KEY_MEDIA_SOURCES)) {
465 | ArrayList mediaSources = bundle.getParcelableArrayList(KEY_MEDIA_SOURCES);
466 | setMediaSources(mediaSources);
467 |
468 | if (bundle.containsKey(KEY_SELECTED_CONTENT)) {
469 | ArrayList mediaItems = bundle.getParcelableArrayList(KEY_SELECTED_CONTENT);
470 |
471 | if (mediaItems != null) {
472 | mSelectedContent.addAll(mediaItems);
473 | }
474 | }
475 | }
476 |
477 | if (bundle.containsKey(KEY_CUSTOM_LAYOUT)) {
478 | setCustomLayout(bundle.getInt(KEY_CUSTOM_LAYOUT, -1));
479 | }
480 |
481 | if (bundle.containsKey(KEY_ACTION_MODE_MENU)) {
482 | setActionModeMenu(bundle.getInt(KEY_ACTION_MODE_MENU, -1));
483 | }
484 |
485 | if (bundle.containsKey(KEY_LOADING_TEXT)) {
486 | mLoadingText = bundle.getString(KEY_LOADING_TEXT, mLoadingText);
487 | }
488 |
489 | if (bundle.containsKey(KEY_EMPTY_TEXT)) {
490 | mEmptyText = bundle.getString(KEY_EMPTY_TEXT, mEmptyText);
491 | }
492 |
493 | if (bundle.containsKey(KEY_ERROR_TEXT)) {
494 | mErrorText = bundle.getString(KEY_ERROR_TEXT, mErrorText);
495 | }
496 | }
497 | }
498 |
499 | /**
500 | * Sets the default empty text strings if they are not already set to something.
501 | */
502 | private void setDefaultTextValues() {
503 | if (mLoadingText == null) setLoadingText(R.string.fetching_media);
504 | if (mEmptyText == null) setEmptyText(R.string.no_media);
505 | if (mErrorText == null) setErrorText(R.string.error_fetching_media);
506 | }
507 |
508 | /**
509 | * Calls {@link org.wordpress.mediapicker.source.MediaSource.OnMediaChange#cleanup()} on all
510 | * non-null sources.
511 | */
512 | private void cleanupMediaSources() {
513 | for (MediaSource source : mMediaSources) {
514 | if (source != null) {
515 | source.cleanup();
516 | }
517 | }
518 | }
519 |
520 | /**
521 | * Constructs the {@link org.wordpress.mediapicker.MediaSourceAdapter} and attaches it to the
522 | * adapter view if possible.
523 | */
524 | private void generateAdapter() {
525 | Activity activity = getActivity();
526 |
527 | if (activity != null) {
528 | ImageLoader.ImageCache imageCache = mListener != null ? mListener.getImageCache() : null;
529 |
530 | MediaSourceAdapter adapter = new MediaSourceAdapter(activity, mMediaSources, imageCache);
531 | adapter.gatherFromSources(this);
532 |
533 | setAdapter(adapter);
534 | }
535 | }
536 |
537 | /**
538 | * Creates the {@link org.wordpress.mediapicker.MediaSourceAdapter} and initializes the adapter
539 | * view to display it.
540 | */
541 | private void layoutAdapterView() {
542 | // Safe to assume non-null since this is only called in onCreateView
543 | Activity activity = getActivity();
544 | Resources resources = activity.getResources();
545 | Drawable background = resources.getDrawable(R.drawable.media_picker_background);
546 |
547 | // Use setBackground(Drawable) when API min is >= 16
548 | mAdapterView.setBackgroundDrawable(background);
549 | mAdapterView.setClipToPadding(false);
550 | mAdapterView.setMultiChoiceModeListener(this);
551 | mAdapterView.setOnItemClickListener(this);
552 | mAdapterView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
553 | mAdapterView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
554 | }
555 |
556 | /**
557 | * Inflates custom mActionModeMenu if set, otherwise inflates default media_picker_action_mode.
558 | *
559 | * @param menu
560 | * the menu to inflate to
561 | */
562 | private void inflateActionModeMenu(Menu menu) {
563 | MenuInflater menuInflater = getActivity().getMenuInflater();
564 |
565 | if (mActionModeMenu != -1) {
566 | menuInflater.inflate(mActionModeMenu, menu);
567 | addSelectionConfirmationButtonMenuItem(menu);
568 | } else {
569 | menuInflater.inflate(R.menu.media_picker_action_mode, menu);
570 | }
571 | }
572 |
573 | /**
574 | * Adds a menu item to confirm media selection during Action Mode. Only adds one if one is not
575 | * defined.
576 | *
577 | * @param menu
578 | * the menu to add a confirm option to
579 | */
580 | private void addSelectionConfirmationButtonMenuItem(Menu menu) {
581 | if (menu != null && menu.findItem(R.id.menu_media_selection_confirmed) == null) {
582 | menu.add(Menu.NONE, R.id.menu_media_selection_confirmed, Menu.FIRST, R.string.confirm)
583 | .setIcon(R.drawable.action_mode_confirm_checkmark);
584 | }
585 | }
586 |
587 | /**
588 | * If the current adapter does not have any items the empty view will be shown and the adapter
589 | * view will be hidden. Otherwise the empty view will be hidden and the adapter view presented.
590 | */
591 | private void toggleEmptyVisibility() {
592 | int empty = (mAdapter != null && mAdapter.getCount() == 0) ? View.VISIBLE : View.GONE;
593 | int adapter = (mAdapter != null && mAdapter.getCount() == 0) ? View.GONE : View.VISIBLE;
594 |
595 | if (mEmptyView != null) {
596 | mEmptyView.setVisibility(empty);
597 | }
598 | if (mAdapterView != null) {
599 | mAdapterView.setVisibility(adapter);
600 | }
601 | }
602 |
603 | /**
604 | * Notifies non-null listener that media selection has started.
605 | */
606 | private void notifyMediaSelectionStarted() {
607 | if (mListener != null) {
608 | mListener.onMediaSelectionStarted();
609 | }
610 | }
611 |
612 | /**
613 | * Notifies non-null listener when selection state changes on a media item.
614 | */
615 | private boolean notifyMediaSelected(int position, boolean selected) {
616 | MediaItem mediaItem = mAdapter.getItem(position);
617 |
618 | if (mediaItem != null) {
619 | MediaSource mediaSource = mAdapter.sourceAtPosition(position);
620 |
621 | if (mediaSource == null || !mediaSource.onMediaItemSelected(mediaItem, selected)) {
622 | if (mListener != null) {
623 | mListener.onMediaSelected(mediaItem, selected);
624 | }
625 |
626 | mSelectedContent.add(mediaItem);
627 |
628 | return false;
629 | }
630 | }
631 |
632 | return true;
633 | }
634 |
635 | /**
636 | * Notifies non-null listener that media selection has been confirmed.
637 | */
638 | private void notifyMediaSelectionConfirmed() {
639 | if (mListener != null) {
640 | mListener.onMediaSelectionConfirmed(mSelectedContent);
641 | }
642 |
643 | mConfirmed = true;
644 | }
645 |
646 | /**
647 | * Notifies non-null listener that media selection has been cancelled.
648 | */
649 | private void notifyMediaSelectionCancelled() {
650 | if (mListener != null) {
651 | mListener.onMediaSelectionCancelled();
652 | }
653 | }
654 | }
655 |
--------------------------------------------------------------------------------