aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/media/MediaPickerActivity.java
blob: ea4664f6776a7eb24cc6fc58364a1ee835134ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package org.wordpress.android.ui.media;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Surface;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

import com.android.volley.toolbox.ImageLoader;

import org.wordpress.android.BuildConfig;
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.ui.RequestCodes;
import org.wordpress.android.util.MediaUtils;
import org.wordpress.android.widgets.WPViewPager;
import org.wordpress.mediapicker.MediaItem;
import org.wordpress.mediapicker.MediaPickerFragment;
import org.wordpress.mediapicker.source.MediaSource;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Allows users to select a variety of videos and images from any source.
 *
 * Title can be set either by defining a string resource, R.string.media_picker_title, or passing
 * a String extra in the {@link android.content.Intent} with the key ACTIVITY_TITLE_KEY.
 *
 * Accepts Image and Video sources as arguments and displays each in a tab.
 *  - Use DEVICE_IMAGE_MEDIA_SOURCES_KEY with a {@link java.util.List} of {@link org.wordpress.mediapicker.source.MediaSource}'s to pass image sources via the Intent
 *  - Use DEVICE_VIDEO_MEDIA_SOURCES_KEY with a {@link java.util.List} of {@link org.wordpress.mediapicker.source.MediaSource}'s to pass video sources via the Intent
 */

public class MediaPickerActivity extends AppCompatActivity
                              implements MediaPickerFragment.OnMediaSelected {
    /**
     * Request code for the {@link android.content.Intent} to start media selection.
     */
    public static final int ACTIVITY_REQUEST_CODE_MEDIA_SELECTION = 6000;
    /**
     * Result code signaling that media has been selected.
     */
    public static final int ACTIVITY_RESULT_CODE_MEDIA_SELECTED   = 6001;
    /**
     * Result code signaling that a gallery should be created with the results.
     */
    public static final int ACTIVITY_RESULT_CODE_GALLERY_CREATED  = 6002;

    /**
     * Pass a {@link String} with this key in the {@link android.content.Intent} to set the title.
     */
    public static final String ACTIVITY_TITLE_KEY           = "activity-title";
    /**
     * Pass an {@link java.util.ArrayList} of {@link org.wordpress.mediapicker.source.MediaSource}'s
     * in the {@link android.content.Intent} to set image sources for selection.
     */
    public static final String DEVICE_IMAGE_MEDIA_SOURCES_KEY = "device-image-media-sources";
    /**
     * Pass an {@link java.util.ArrayList} of {@link org.wordpress.mediapicker.source.MediaSource}'s
     * in the {@link android.content.Intent} to set video sources for selection.
     */
    public static final String DEVICE_VIDEO_MEDIA_SOURCES_KEY = "device- video=media-sources";
    public static final String BLOG_IMAGE_MEDIA_SOURCES_KEY = "blog-image-media-sources";
    public static final String BLOG_VIDEO_MEDIA_SOURCES_KEY = "blog-video-media-sources";
    /**
     * Key to extract the {@link java.util.ArrayList} of {@link org.wordpress.mediapicker.MediaItem}'s
     * that were selected by the user.
     */
    public static final String SELECTED_CONTENT_RESULTS_KEY = "selected-content";

    private static final String CAPTURE_PATH_KEY = "capture-path";

    private static final long   TAB_ANIMATION_DURATION_MS = 250L;

    private MediaPickerAdapter     mMediaPickerAdapter;
    private ArrayList<MediaSource>[] mMediaSources;
    private TabLayout              mTabLayout;
    private WPViewPager            mViewPager;
    private ActionMode             mActionMode;
    private String                 mCapturePath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lockRotation();
        addMediaSources();
        setTitle();
        initializeContentView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.media_picker, menu);

        return true;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putString(CAPTURE_PATH_KEY, mCapturePath);
    }

    @Override
    public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        if (savedInstanceState.containsKey(CAPTURE_PATH_KEY)) {
            mCapturePath = savedInstanceState.getString(CAPTURE_PATH_KEY);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        } else if (item.getItemId() == R.id.capture_image) {
            WordPressMediaUtils.launchCamera(this, BuildConfig.APPLICATION_ID,
                    new WordPressMediaUtils.LaunchCameraCallback() {
                        @Override
                        public void onMediaCapturePathReady(String mediaCapturePath) {
                            mCapturePath = mediaCapturePath;
                        }
                    });
            return true;
        } else if (item.getItemId() == R.id.capture_video) {
            WordPressMediaUtils.launchVideoCamera(this);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case RequestCodes.TAKE_PHOTO:
                File file = new File(mCapturePath);
                Uri imageUri = Uri.fromFile(file);

                if (file.exists() && MediaUtils.isValidImage(imageUri.toString())) {
                    // Notify MediaStore of new content
                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, imageUri));

                    MediaItem newImage = new MediaItem();
                    newImage.setSource(imageUri);
                    newImage.setPreviewSource(imageUri);
                    ArrayList<MediaItem> imageResult = new ArrayList<>();
                    imageResult.add(newImage);
                    finishWithResults(imageResult, ACTIVITY_RESULT_CODE_MEDIA_SELECTED);
                }
                break;
            case RequestCodes.TAKE_VIDEO:
                Uri videoUri = data != null ? data.getData() : null;

                if (videoUri != null) {
                    // Notify MediaStore of new content
                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, videoUri));

                    MediaItem newVideo = new MediaItem();
                    newVideo.setSource(videoUri);
                    newVideo.setPreviewSource(videoUri);
                    ArrayList<MediaItem> videoResult = new ArrayList<>();
                    videoResult.add(newVideo);
                    finishWithResults(videoResult, ACTIVITY_RESULT_CODE_MEDIA_SELECTED);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (mActionMode != null) {
            mActionMode.finish();
        } else {
            finishWithResults(null, ACTIVITY_RESULT_CODE_MEDIA_SELECTED);
            super.onBackPressed();
        }
    }

    @Override
    public void onActionModeStarted(ActionMode mode) {
        super.onActionModeStarted(mode);

        mViewPager.setPagingEnabled(false);
        mActionMode = mode;

        animateTabGone();
    }

    @Override
    public void onActionModeFinished(ActionMode mode) {
        super.onActionModeFinished(mode);

        mViewPager.setPagingEnabled(true);
        mActionMode = null;

        animateTabAppear();
    }

    /*
        OnMediaSelected interface
     */

    @Override
    public void onMediaSelectionStarted() {
    }

    @Override
    public void onMediaSelected(MediaItem mediaContent, boolean selected) {
    }

    @Override
    public void onMediaSelectionConfirmed(ArrayList<MediaItem> mediaContent) {
        if (mediaContent != null) {
            finishWithResults(mediaContent, ACTIVITY_RESULT_CODE_MEDIA_SELECTED);
        } else {
            finish();
        }
    }

    @Override
    public void onMediaSelectionCancelled() {
    }

    @Override
    public boolean onMenuItemSelected(MenuItem menuItem, ArrayList<MediaItem> selectedContent) {
        if (menuItem.getItemId() == R.id.menu_media_content_selection_gallery) {
            finishWithResults(selectedContent, ACTIVITY_RESULT_CODE_GALLERY_CREATED);
        }

        return false;
    }

    @Override
    public ImageLoader.ImageCache getImageCache() {
        return WordPress.getBitmapCache();
    }

    /**
     * Finishes the activity after the user has confirmed media selection.
     *
     * @param results
     *  list of selected media items
     */
    private void finishWithResults(ArrayList<MediaItem> results, int resultCode) {
        Intent result = new Intent();
        result.putParcelableArrayListExtra(SELECTED_CONTENT_RESULTS_KEY, results);
        setResult(resultCode, result);
        finish();
    }

    /**
     * Helper method; sets title to R.string.media_picker_title unless intent defines one
     */
    private void setTitle() {
        final Intent intent = getIntent();

        if (intent != null && intent.hasExtra(ACTIVITY_TITLE_KEY)) {
            String activityTitle = intent.getStringExtra(ACTIVITY_TITLE_KEY);
            setTitle(activityTitle);
        } else {
            setTitle(getString(R.string.media_picker_title));
        }
    }

    /**
     * Helper method; gathers {@link org.wordpress.mediapicker.source.MediaSource}'s from intent
     */
    private void addMediaSources() {
        final Intent intent = getIntent();

        if (intent != null) {
            mMediaSources = new ArrayList[4];

            List<MediaSource> mediaSources = intent.getParcelableArrayListExtra(DEVICE_IMAGE_MEDIA_SOURCES_KEY);
            if (mediaSources != null) {
                mMediaSources[0] = new ArrayList<>();
                mMediaSources[0].addAll(mediaSources);
            }

            mediaSources = intent.getParcelableArrayListExtra(DEVICE_VIDEO_MEDIA_SOURCES_KEY);
            if (mediaSources != null) {
                mMediaSources[1] = new ArrayList<>();
                mMediaSources[1].addAll(mediaSources);
            }

            mediaSources = intent.getParcelableArrayListExtra(BLOG_IMAGE_MEDIA_SOURCES_KEY);
            if (mediaSources != null) {
                mMediaSources[2] = new ArrayList<>();
                mMediaSources[2].addAll(mediaSources);
            }

            mediaSources = intent.getParcelableArrayListExtra(BLOG_VIDEO_MEDIA_SOURCES_KEY);
            if (mediaSources != null) {
                mMediaSources[3] = new ArrayList<>();
                mMediaSources[3].addAll(mediaSources);
            }
        }
    }

    /**
     * Helper method; locks device orientation to its current state while media is being selected
     */
    private void lockRotation() {
        switch (getWindowManager().getDefaultDisplay().getRotation()) {
            case Surface.ROTATION_0:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
            case Surface.ROTATION_90:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case Surface.ROTATION_180:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
            case Surface.ROTATION_270:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
        }
    }

    /**
     * Helper method; sets up the tab bar, media adapter, and ViewPager for displaying media content
     */
    private void initializeContentView() {
        setContentView(R.layout.media_picker_activity);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.show();
        }

        mMediaPickerAdapter = new MediaPickerAdapter(getFragmentManager());
        mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
        mViewPager = (WPViewPager) findViewById(R.id.media_picker_pager);

        if (mViewPager != null) {
            mViewPager.setPagingEnabled(true);

            mMediaPickerAdapter.addTab(mMediaSources[0] != null ? mMediaSources[0] :
                    new ArrayList<MediaSource>(),
                    getString(R.string.tab_title_device_images),
                    getString(R.string.loading_images),
                    getString(R.string.error_loading_images),
                    getString(R.string.no_device_images));
            mMediaPickerAdapter.addTab(mMediaSources[1] != null ? mMediaSources[1] :
                    new ArrayList<MediaSource>(),
                    getString(R.string.tab_title_device_videos),
                    getString(R.string.loading_videos),
                    getString(R.string.error_loading_videos),
                    getString(R.string.no_device_videos));
            mMediaPickerAdapter.addTab(mMediaSources[2] != null ? mMediaSources[2] :
                    new ArrayList<MediaSource>(),
                    getString(R.string.tab_title_site_images),
                    getString(R.string.loading_blog_images),
                    getString(R.string.error_loading_blog_images),
                    getString(R.string.no_blog_images));
            mMediaPickerAdapter.addTab(mMediaSources[3] != null ? mMediaSources[3] :
                    new ArrayList<MediaSource>(),
                    getString(R.string.tab_title_site_videos),
                    getString(R.string.loading_blog_videos),
                    getString(R.string.error_loading_blog_videos),
                    getString(R.string.no_blog_videos));

            mViewPager.setAdapter(mMediaPickerAdapter);

            if (mTabLayout != null) {
                int normalColor = getResources().getColor(R.color.blue_light);
                int selectedColor = getResources().getColor(R.color.white);
                mTabLayout.setTabTextColors(normalColor, selectedColor);
                mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                mTabLayout.setupWithViewPager(mViewPager);
            }
        }
    }

    /**
     * Helper method; animates the tab bar and ViewPager in when ActionMode ends
     */
    private void animateTabAppear() {
        TranslateAnimation tabAppearAnimation = new TranslateAnimation(0, 0, -mTabLayout.getHeight(), 0);
        TranslateAnimation pagerAppearAnimation = new TranslateAnimation(0, 0, -mTabLayout.getHeight(), 0);

        tabAppearAnimation.setDuration(TAB_ANIMATION_DURATION_MS);
        pagerAppearAnimation.setDuration(TAB_ANIMATION_DURATION_MS);
        pagerAppearAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mViewPager.getWidth(), mViewPager.getHeight() - mTabLayout.getHeight());
                mViewPager.setLayoutParams(params);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        mTabLayout.setVisibility(View.VISIBLE);
        mViewPager.startAnimation(pagerAppearAnimation);
        mTabLayout.startAnimation(tabAppearAnimation);
    }

    /**
     * Helper method; animates the tab bar and ViewPager out when ActionMode begins
     */
    private void animateTabGone() {
        TranslateAnimation tabGoneAnimation = new TranslateAnimation(0, 0, 0, -mTabLayout.getHeight());
        TranslateAnimation pagerGoneAnimation = new TranslateAnimation(0, 0, 0, -mTabLayout.getHeight());
        tabGoneAnimation.setDuration(TAB_ANIMATION_DURATION_MS);
        pagerGoneAnimation.setDuration(TAB_ANIMATION_DURATION_MS);
        tabGoneAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mTabLayout.setVisibility(View.GONE);
                mTabLayout.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        pagerGoneAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(mViewPager.getWidth(), mViewPager.getHeight() + mTabLayout.getHeight());
                mViewPager.setLayoutParams(newParams);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mViewPager.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        mTabLayout.startAnimation(tabGoneAnimation);
        mViewPager.startAnimation(pagerGoneAnimation);
    }

    /**
     * Shows {@link org.wordpress.mediapicker.MediaPickerFragment}'s in a tabbed layout.
     */
    public class MediaPickerAdapter extends FragmentPagerAdapter {
        private class MediaPicker {
            public String loadingText;
            public String errorText;
            public String emptyText;
            public String pickerTitle;
            public ArrayList<MediaSource> mediaSources;

            public MediaPicker(String name, String loading, String error, String empty, ArrayList<MediaSource> sources) {
                loadingText = loading;
                errorText = error;
                emptyText = empty;
                pickerTitle = name;
                mediaSources = sources;
            }
        }

        private final List<MediaPicker> mMediaPickers;

        private MediaPickerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);

            mMediaPickers = new ArrayList<>();
        }

        @Override
        public Fragment getItem(int position) {
            if (position < mMediaPickers.size()) {
                MediaPicker mediaPicker = mMediaPickers.get(position);
                MediaPickerFragment fragment = new MediaPickerFragment();
                fragment.setLoadingText(mediaPicker.loadingText);
                fragment.setErrorText(mediaPicker.errorText);
                fragment.setEmptyText(mediaPicker.emptyText);
                fragment.setActionModeMenu(R.menu.menu_media_picker_action_mode);
                fragment.setMediaSources(mediaPicker.mediaSources);

                return fragment;
            }

            return null;
        }

        @Override
        public int getCount() {
            return mMediaPickers.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mMediaPickers.get(position).pickerTitle;
        }

        public void addTab(ArrayList<MediaSource> mediaSources, String tabName, String loading, String error, String empty) {
            mMediaPickers.add(new MediaPicker(tabName, loading, error, empty, mediaSources));
        }
    }
}