CameraActivity.java revision 6607dae6c4d1ee5bbf572695c2042d4d9129a730
1/*
2 * Copyright (C) 2012 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
17package com.android.camera;
18
19import android.animation.Animator;
20import android.annotation.TargetApi;
21import android.app.ActionBar;
22import android.app.Activity;
23import android.app.AlertDialog;
24import android.app.Dialog;
25import android.content.ActivityNotFoundException;
26import android.content.BroadcastReceiver;
27import android.content.ContentResolver;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.pm.ActivityInfo;
32import android.content.pm.PackageManager;
33import android.content.res.Configuration;
34import android.graphics.Bitmap;
35import android.graphics.BitmapFactory;
36import android.graphics.Matrix;
37import android.graphics.Point;
38import android.graphics.RectF;
39import android.graphics.SurfaceTexture;
40import android.graphics.drawable.Drawable;
41import android.hardware.Camera;
42import android.net.Uri;
43import android.nfc.NfcAdapter;
44import android.nfc.NfcAdapter.CreateBeamUrisCallback;
45import android.nfc.NfcEvent;
46import android.os.AsyncTask;
47import android.os.Build;
48import android.os.Bundle;
49import android.os.Handler;
50import android.os.HandlerThread;
51import android.os.Looper;
52import android.os.Message;
53import android.provider.MediaStore;
54import android.provider.Settings;
55import android.util.CameraPerformanceTracker;
56import android.view.ContextMenu;
57import android.view.ContextMenu.ContextMenuInfo;
58import android.view.KeyEvent;
59import android.view.Menu;
60import android.view.MenuInflater;
61import android.view.MenuItem;
62import android.view.MotionEvent;
63import android.view.View;
64import android.view.View.OnSystemUiVisibilityChangeListener;
65import android.view.ViewGroup;
66import android.view.Window;
67import android.view.WindowManager;
68import android.widget.FrameLayout;
69import android.widget.ImageView;
70import android.widget.ShareActionProvider;
71
72import com.android.camera.app.AppController;
73import com.android.camera.app.CameraAppUI;
74import com.android.camera.app.CameraController;
75import com.android.camera.app.CameraProvider;
76import com.android.camera.app.CameraServices;
77import com.android.camera.app.LocationManager;
78import com.android.camera.app.MemoryManager;
79import com.android.camera.app.MemoryQuery;
80import com.android.camera.app.ModuleManagerImpl;
81import com.android.camera.app.OrientationManager;
82import com.android.camera.app.OrientationManagerImpl;
83import com.android.camera.cameradevice.CameraManager;
84import com.android.camera.cameradevice.CameraManagerFactory;
85import com.android.camera.cameradevice.CameraSettings;
86import com.android.camera.data.CameraDataAdapter;
87import com.android.camera.data.FixedLastDataAdapter;
88import com.android.camera.data.LocalData;
89import com.android.camera.data.LocalDataAdapter;
90import com.android.camera.data.LocalDataUtil;
91import com.android.camera.data.LocalDataViewType;
92import com.android.camera.data.LocalMediaData;
93import com.android.camera.data.LocalMediaObserver;
94import com.android.camera.data.LocalSessionData;
95import com.android.camera.data.MediaDetails;
96import com.android.camera.data.MetadataLoader;
97import com.android.camera.data.PanoramaMetadataLoader;
98import com.android.camera.data.RgbzMetadataLoader;
99import com.android.camera.data.SimpleViewData;
100import com.android.camera.debug.Log;
101import com.android.camera.filmstrip.FilmstripContentPanel;
102import com.android.camera.filmstrip.FilmstripController;
103import com.android.camera.hardware.HardwareSpec;
104import com.android.camera.hardware.HardwareSpecImpl;
105import com.android.camera.module.ModuleController;
106import com.android.camera.module.ModulesInfo;
107import com.android.camera.session.CaptureSession;
108import com.android.camera.session.CaptureSessionManager;
109import com.android.camera.session.CaptureSessionManager.SessionListener;
110import com.android.camera.settings.CameraSettingsActivity;
111import com.android.camera.settings.SettingsManager;
112import com.android.camera.settings.SettingsManager.SettingsCapabilities;
113import com.android.camera.settings.SettingsUtil;
114import com.android.camera.tinyplanet.TinyPlanetFragment;
115import com.android.camera.ui.AbstractTutorialOverlay;
116import com.android.camera.ui.DetailsDialog;
117import com.android.camera.ui.MainActivityLayout;
118import com.android.camera.ui.ModeListView;
119import com.android.camera.ui.ModeListView.ModeListVisibilityChangedListener;
120import com.android.camera.ui.PreviewStatusListener;
121import com.android.camera.util.ApiHelper;
122import com.android.camera.util.Callback;
123import com.android.camera.util.CameraUtil;
124import com.android.camera.util.FeedbackHelper;
125import com.android.camera.util.GalleryHelper;
126import com.android.camera.util.GcamHelper;
127import com.android.camera.util.IntentHelper;
128import com.android.camera.util.PhotoSphereHelper.PanoramaViewHelper;
129import com.android.camera.util.ReleaseDialogHelper;
130import com.android.camera.util.UsageStatistics;
131import com.android.camera.widget.FilmstripView;
132import com.android.camera.widget.Preloader;
133import com.android.camera2.R;
134import com.bumptech.glide.Glide;
135import com.bumptech.glide.resize.ImageManager;
136import com.google.common.logging.eventprotos;
137import com.google.common.logging.eventprotos.ForegroundEvent.ForegroundSource;
138import com.google.common.logging.eventprotos.MediaInteraction;
139import com.google.common.logging.eventprotos.NavigationChange;
140
141import java.io.File;
142import java.io.FileInputStream;
143import java.io.FileNotFoundException;
144import java.lang.ref.WeakReference;
145import java.util.ArrayList;
146import java.util.HashMap;
147import java.util.List;
148import java.util.concurrent.Executors;
149
150public class CameraActivity extends Activity
151        implements AppController, CameraManager.CameraOpenCallback,
152        ActionBar.OnMenuVisibilityListener, ShareActionProvider.OnShareTargetSelectedListener,
153        OrientationManager.OnOrientationChangeListener {
154
155    private static final Log.Tag TAG = new Log.Tag("CameraActivity");
156
157    private static final String INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE =
158            "android.media.action.STILL_IMAGE_CAMERA_SECURE";
159    public static final String ACTION_IMAGE_CAPTURE_SECURE =
160            "android.media.action.IMAGE_CAPTURE_SECURE";
161
162    // The intent extra for camera from secure lock screen. True if the gallery
163    // should only show newly captured pictures. sSecureAlbumId does not
164    // increment. This is used when switching between camera, camcorder, and
165    // panorama. If the extra is not set, it is in the normal camera mode.
166    public static final String SECURE_CAMERA_EXTRA = "secure_camera";
167
168    /**
169     * Request code from an activity we started that indicated that we do not
170     * want to reset the view to the preview in onResume.
171     */
172    public static final int REQ_CODE_DONT_SWITCH_TO_PREVIEW = 142;
173
174    public static final int REQ_CODE_GCAM_DEBUG_POSTCAPTURE = 999;
175
176    private static final int MSG_CLEAR_SCREEN_ON_FLAG = 2;
177    private static final long SCREEN_DELAY_MS = 2 * 60 * 1000; // 2 mins.
178    private static final int MAX_PEEK_BITMAP_PIXELS = 1600000; // 1.6 * 4 MBs.
179    /** Load metadata for 10 items ahead of our current. */
180    private static final int FILMSTRIP_PRELOAD_AHEAD_ITEMS = 10;
181
182    /** Should be used wherever a context is needed. */
183    private Context mAppContext;
184
185    /**
186     * Whether onResume should reset the view to the preview.
187     */
188    private boolean mResetToPreviewOnResume = true;
189
190    /**
191     * This data adapter is used by FilmStripView.
192     */
193    private LocalDataAdapter mDataAdapter;
194
195    private SettingsManager mSettingsManager;
196    private ModeListView mModeListView;
197    private boolean mModeListVisible = false;
198    private int mCurrentModeIndex;
199    private CameraModule mCurrentModule;
200    private ModuleManagerImpl mModuleManager;
201    private FrameLayout mAboveFilmstripControlLayout;
202    private FilmstripController mFilmstripController;
203    private boolean mFilmstripVisible;
204    /** Whether the filmstrip fully covers the preview. */
205    private boolean mFilmstripCoversPreview = false;
206    private int mResultCodeForTesting;
207    private Intent mResultDataForTesting;
208    private OnScreenHint mStorageHint;
209    private final Object mStorageSpaceLock = new Object();
210    private long mStorageSpaceBytes = Storage.LOW_STORAGE_THRESHOLD_BYTES;
211    private boolean mAutoRotateScreen;
212    private boolean mSecureCamera;
213    private int mLastRawOrientation;
214    private OrientationManagerImpl mOrientationManager;
215    private LocationManager mLocationManager;
216    private ButtonManager mButtonManager;
217    private Handler mMainHandler;
218    private PanoramaViewHelper mPanoramaViewHelper;
219    private ActionBar mActionBar;
220    private ViewGroup mUndoDeletionBar;
221    private boolean mIsUndoingDeletion = false;
222
223    private final Uri[] mNfcPushUris = new Uri[1];
224
225    private LocalMediaObserver mLocalImagesObserver;
226    private LocalMediaObserver mLocalVideosObserver;
227
228    private boolean mPendingDeletion = false;
229
230    private CameraController mCameraController;
231    private boolean mPaused;
232    private CameraAppUI mCameraAppUI;
233
234    private PeekAnimationHandler mPeekAnimationHandler;
235    private HandlerThread mPeekAnimationThread;
236
237    private FeedbackHelper mFeedbackHelper;
238
239    private Intent mGalleryIntent;
240    private long mOnCreateTime;
241
242    private Menu mActionBarMenu;
243    private Preloader<Integer, AsyncTask> mPreloader;
244
245    private static final int LIGHTS_OUT_DELAY_MS = 4000;
246    private final int BASE_SYS_UI_VISIBILITY = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
247            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
248    private final Runnable mLightsOutRunnable = new Runnable() {
249        @Override
250        public void run() {
251            getWindow().getDecorView().setSystemUiVisibility(
252                    BASE_SYS_UI_VISIBILITY | View.SYSTEM_UI_FLAG_LOW_PROFILE);
253        }
254    };
255    private MemoryManager mMemoryManager;
256
257    @Override
258    public CameraAppUI getCameraAppUI() {
259        return mCameraAppUI;
260    }
261
262    // close activity when screen turns off
263    private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
264        @Override
265        public void onReceive(Context context, Intent intent) {
266            finish();
267        }
268    };
269
270    /**
271     * Whether the screen is kept turned on.
272     */
273    private boolean mKeepScreenOn;
274    private int mLastLayoutOrientation;
275    private final CameraAppUI.BottomPanel.Listener mMyFilmstripBottomControlListener =
276            new CameraAppUI.BottomPanel.Listener() {
277
278                /**
279                 * If the current photo is a photo sphere, this will launch the
280                 * Photo Sphere panorama viewer.
281                 */
282                @Override
283                public void onExternalViewer() {
284                    if (mPanoramaViewHelper == null) {
285                        return;
286                    }
287                    final LocalData data = getCurrentLocalData();
288                    if (data == null) {
289                        return;
290                    }
291                    final Uri contentUri = data.getUri();
292                    if (contentUri == Uri.EMPTY) {
293                        return;
294                    }
295
296                    if (PanoramaMetadataLoader.isPanoramaAndUseViewer(data)) {
297                        mPanoramaViewHelper.showPanorama(CameraActivity.this, contentUri);
298                    } else if (RgbzMetadataLoader.hasRGBZData(data)) {
299                        mPanoramaViewHelper.showRgbz(contentUri);
300                        if (mSettingsManager.getBoolean(
301                                SettingsManager.SETTING_SHOULD_SHOW_REFOCUS_VIEWER_CLING)) {
302                            mSettingsManager.setBoolean(
303                                    SettingsManager.SETTING_SHOULD_SHOW_REFOCUS_VIEWER_CLING,
304                                    false);
305                            mCameraAppUI.clearClingForViewer(
306                                    CameraAppUI.BottomPanel.VIEWER_REFOCUS);
307                        }
308                    }
309                }
310
311                @Override
312                public void onEdit() {
313                    LocalData data = getCurrentLocalData();
314                    if (data == null) {
315                        return;
316                    }
317                    final int currentDataId = getCurrentDataId();
318                    UsageStatistics.instance().mediaInteraction(fileNameFromDataID(currentDataId),
319                            MediaInteraction.InteractionType.EDIT,
320                            NavigationChange.InteractionCause.BUTTON,
321                            fileAgeFromDataID(currentDataId));
322                    launchEditor(data);
323                }
324
325                @Override
326                public void onTinyPlanet() {
327                    LocalData data = getCurrentLocalData();
328                    if (data == null) {
329                        return;
330                    }
331                    launchTinyPlanetEditor(data);
332                }
333
334                @Override
335                public void onDelete() {
336                    final int currentDataId = getCurrentDataId();
337                    UsageStatistics.instance().mediaInteraction(fileNameFromDataID(currentDataId),
338                            MediaInteraction.InteractionType.DELETE,
339                            NavigationChange.InteractionCause.BUTTON,
340                            fileAgeFromDataID(currentDataId));
341                    removeData(currentDataId);
342                }
343
344                @Override
345                public void onShare() {
346                    final LocalData data = getCurrentLocalData();
347                    final int currentDataId = getCurrentDataId();
348                    UsageStatistics.instance().mediaInteraction(fileNameFromDataID(currentDataId),
349                            MediaInteraction.InteractionType.SHARE,
350                            NavigationChange.InteractionCause.BUTTON,
351                            fileAgeFromDataID(currentDataId));
352                    // If applicable, show release information before this item
353                    // is shared.
354                    if (ReleaseDialogHelper.shouldShowReleaseInfoDialogOnShare(data)) {
355                        ReleaseDialogHelper.showReleaseInfoDialog(CameraActivity.this,
356                                new Callback<Void>() {
357                                    @Override
358                                    public void onCallback(Void result) {
359                                        share(data);
360                                    }
361                                });
362                    } else {
363                        share(data);
364                    }
365                }
366
367                private void share(LocalData data) {
368                    Intent shareIntent = getShareIntentByData(data);
369                    if (shareIntent != null) {
370                        try {
371                            launchActivityByIntent(shareIntent);
372                            mCameraAppUI.getFilmstripBottomControls().setShareEnabled(false);
373                        } catch (ActivityNotFoundException ex) {
374                            // Nothing.
375                        }
376                    }
377                }
378
379                private int getCurrentDataId() {
380                    return mFilmstripController.getCurrentId();
381                }
382
383                private LocalData getCurrentLocalData() {
384                    return mDataAdapter.getLocalData(getCurrentDataId());
385                }
386
387                /**
388                 * Sets up the share intent and NFC properly according to the
389                 * data.
390                 *
391                 * @param data The data to be shared.
392                 */
393                private Intent getShareIntentByData(final LocalData data) {
394                    Intent intent = null;
395                    final Uri contentUri = data.getUri();
396                    final String msgShareTo = getResources().getString(R.string.share_to);
397
398                    if (PanoramaMetadataLoader.isPanorama360(data) &&
399                            data.getUri() != Uri.EMPTY) {
400                        intent = new Intent(Intent.ACTION_SEND);
401                        intent.setType("application/vnd.google.panorama360+jpg");
402                        intent.putExtra(Intent.EXTRA_STREAM, contentUri);
403                    } else if (data.isDataActionSupported(LocalData.DATA_ACTION_SHARE)) {
404                        final String mimeType = data.getMimeType();
405                        intent = getShareIntentFromType(mimeType);
406                        if (intent != null) {
407                            intent.putExtra(Intent.EXTRA_STREAM, contentUri);
408                            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
409                        }
410                        intent = Intent.createChooser(intent, msgShareTo);
411                    }
412                    return intent;
413                }
414
415                /**
416                 * Get the share intent according to the mimeType
417                 *
418                 * @param mimeType The mimeType of current data.
419                 * @return the video/image's ShareIntent or null if mimeType is
420                 *         invalid.
421                 */
422                private Intent getShareIntentFromType(String mimeType) {
423                    // Lazily create the intent object.
424                    Intent intent = new Intent(Intent.ACTION_SEND);
425                    if (mimeType.startsWith("video/")) {
426                        intent.setType("video/*");
427                    } else {
428                        if (mimeType.startsWith("image/")) {
429                            intent.setType("image/*");
430                        } else {
431                            Log.w(TAG, "unsupported mimeType " + mimeType);
432                        }
433                    }
434                    return intent;
435                }
436
437                @Override
438                public void onProgressErrorClicked() {
439                    LocalData data = getCurrentLocalData();
440                    getServices().getCaptureSessionManager().removeErrorMessage(
441                            data.getUri());
442                    updateBottomControlsByData(data);
443                }
444            };
445
446    @Override
447    public void onCameraOpened(CameraManager.CameraProxy camera) {
448        if (mPaused) {
449            return;
450        }
451        /**
452         * The current UI requires that the flash option visibility in front-facing
453         * camera be
454         *   * disabled if back facing camera supports flash
455         *   * hidden if back facing camera does not support flash
456         * We save whether back facing camera supports flash because we cannot get
457         * this in front facing camera without a camera switch.
458         *
459         * If this preference is cleared, we also need to clear the camera facing
460         * setting so we default to opening the camera in back facing camera, and
461         * can save this flash support value again.
462         */
463        if (!mSettingsManager.isSet(SettingsManager.SETTING_FLASH_SUPPORTED_BACK_CAMERA)) {
464            HardwareSpec hardware = new HardwareSpecImpl(camera.getCapabilities());
465            mSettingsManager.setBoolean(SettingsManager.SETTING_FLASH_SUPPORTED_BACK_CAMERA,
466                hardware.isFlashSupported());
467        }
468
469        if (!mModuleManager.getModuleAgent(mCurrentModeIndex).requestAppForCamera()) {
470            // We shouldn't be here. Just close the camera and leave.
471            mCameraController.closeCamera(false);
472            throw new IllegalStateException("Camera opened but the module shouldn't be " +
473                    "requesting");
474        }
475        if (mCurrentModule != null) {
476            SettingsCapabilities capabilities =
477                    SettingsUtil.getSettingsCapabilities(camera);
478            mSettingsManager.changeCamera(camera.getCameraId(), capabilities);
479            resetExposureCompensationToDefault(camera);
480            mCurrentModule.onCameraAvailable(camera);
481        }
482        mCameraAppUI.onChangeCamera();
483    }
484
485    private void resetExposureCompensationToDefault(CameraManager.CameraProxy camera) {
486        // Reset the exposure compensation before handing the camera to module.
487        CameraSettings cameraSettings = camera.getSettings();
488        cameraSettings.setExposureCompensationIndex(0);
489        camera.applySettings(cameraSettings);
490    }
491
492    @Override
493    public void onCameraDisabled(int cameraId) {
494        UsageStatistics.instance().cameraFailure(eventprotos.CameraFailure.FailureReason.SECURITY,
495                null);
496        Log.w(TAG, "Camera disabled: " + cameraId);
497        CameraUtil.showErrorAndFinish(this, R.string.camera_disabled);
498    }
499
500    @Override
501    public void onDeviceOpenFailure(int cameraId, String info) {
502        UsageStatistics.instance().cameraFailure(
503                eventprotos.CameraFailure.FailureReason.OPEN_FAILURE, info);
504        Log.w(TAG, "Camera open failure: " + info);
505        CameraUtil.showErrorAndFinish(this, R.string.cannot_connect_camera);
506    }
507
508    @Override
509    public void onDeviceOpenedAlready(int cameraId, String info) {
510        Log.w(TAG, "Camera open already: " + cameraId + "," + info);
511        CameraUtil.showErrorAndFinish(this, R.string.cannot_connect_camera);
512    }
513
514    @Override
515    public void onReconnectionFailure(CameraManager mgr, String info) {
516        UsageStatistics.instance().cameraFailure(
517                eventprotos.CameraFailure.FailureReason.RECONNECT_FAILURE, null);
518        Log.w(TAG, "Camera reconnection failure:" + info);
519        CameraUtil.showErrorAndFinish(this, R.string.cannot_connect_camera);
520    }
521
522    private static class MainHandler extends Handler {
523        final WeakReference<CameraActivity> mActivity;
524
525        public MainHandler(CameraActivity activity, Looper looper) {
526            super(looper);
527            mActivity = new WeakReference<CameraActivity>(activity);
528        }
529
530        @Override
531        public void handleMessage(Message msg) {
532            CameraActivity activity = mActivity.get();
533            if (activity == null) {
534                return;
535            }
536            switch (msg.what) {
537
538                case MSG_CLEAR_SCREEN_ON_FLAG: {
539                    if (!activity.mPaused) {
540                        activity.getWindow().clearFlags(
541                                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
542                    }
543                    break;
544                }
545            }
546        }
547    }
548
549    private String fileNameFromDataID(int dataID) {
550        final LocalData localData = mDataAdapter.getLocalData(dataID);
551
552        File localFile = new File(localData.getPath());
553        return localFile.getName();
554    }
555
556    private float fileAgeFromDataID(int dataID) {
557        final LocalData localData = mDataAdapter.getLocalData(dataID);
558
559        File localFile = new File(localData.getPath());
560        return 0.001f * (System.currentTimeMillis() - localFile.lastModified());
561    }
562
563    private final FilmstripContentPanel.Listener mFilmstripListener =
564            new FilmstripContentPanel.Listener() {
565
566                @Override
567                public void onSwipeOut() {
568                }
569
570                @Override
571                public void onSwipeOutBegin() {
572                    mActionBar.hide();
573                    mFilmstripCoversPreview = false;
574                    updatePreviewVisibility();
575                }
576
577                @Override
578                public void onFilmstripHidden() {
579                    mFilmstripVisible = false;
580                    UsageStatistics.instance().changeScreen(currentUserInterfaceMode(),
581                            NavigationChange.InteractionCause.SWIPE_RIGHT);
582                    CameraActivity.this.setFilmstripUiVisibility(false);
583                    // When the user hide the filmstrip (either swipe out or
584                    // tap on back key) we move to the first item so next time
585                    // when the user swipe in the filmstrip, the most recent
586                    // one is shown.
587                    mFilmstripController.goToFirstItem();
588                }
589
590                @Override
591                public void onFilmstripShown() {
592                    mFilmstripVisible = true;
593                    UsageStatistics.instance().changeScreen(currentUserInterfaceMode(),
594                            NavigationChange.InteractionCause.SWIPE_LEFT);
595                    updateUiByData(mFilmstripController.getCurrentId());
596                }
597
598                @Override
599                public void onFocusedDataLongPressed(int dataId) {
600                    // Do nothing.
601                }
602
603                @Override
604                public void onFocusedDataPromoted(int dataID) {
605                    UsageStatistics.instance().mediaInteraction(fileNameFromDataID(dataID),
606                            MediaInteraction.InteractionType.DELETE,
607                            NavigationChange.InteractionCause.SWIPE_UP, fileAgeFromDataID(dataID));
608                    removeData(dataID);
609                }
610
611                @Override
612                public void onFocusedDataDemoted(int dataID) {
613                    UsageStatistics.instance().mediaInteraction(fileNameFromDataID(dataID),
614                            MediaInteraction.InteractionType.DELETE,
615                            NavigationChange.InteractionCause.SWIPE_DOWN,
616                            fileAgeFromDataID(dataID));
617                    removeData(dataID);
618                }
619
620                @Override
621                public void onEnterFullScreenUiShown(int dataId) {
622                    if (mFilmstripVisible) {
623                        CameraActivity.this.setFilmstripUiVisibility(true);
624                    }
625                }
626
627                @Override
628                public void onLeaveFullScreenUiShown(int dataId) {
629                    // Do nothing.
630                }
631
632                @Override
633                public void onEnterFullScreenUiHidden(int dataId) {
634                    if (mFilmstripVisible) {
635                        CameraActivity.this.setFilmstripUiVisibility(false);
636                    }
637                }
638
639                @Override
640                public void onLeaveFullScreenUiHidden(int dataId) {
641                    // Do nothing.
642                }
643
644                @Override
645                public void onEnterFilmstrip(int dataId) {
646                    if (mFilmstripVisible) {
647                        CameraActivity.this.setFilmstripUiVisibility(true);
648                    }
649                }
650
651                @Override
652                public void onLeaveFilmstrip(int dataId) {
653                    // Do nothing.
654                }
655
656                @Override
657                public void onDataReloaded() {
658                    if (!mFilmstripVisible) {
659                        return;
660                    }
661                    updateUiByData(mFilmstripController.getCurrentId());
662                }
663
664                @Override
665                public void onDataUpdated(int dataId) {
666                    if (!mFilmstripVisible) {
667                        return;
668                    }
669                    updateUiByData(mFilmstripController.getCurrentId());
670                }
671
672                @Override
673                public void onEnterZoomView(int dataID) {
674                    if (mFilmstripVisible) {
675                        CameraActivity.this.setFilmstripUiVisibility(false);
676                    }
677                }
678
679                @Override
680                public void onDataFocusChanged(final int prevDataId, final int newDataId) {
681                    if (!mFilmstripVisible) {
682                        return;
683                    }
684                    // TODO: This callback is UI event callback, should always
685                    // happen on UI thread. Find the reason for this
686                    // runOnUiThread() and fix it.
687                    runOnUiThread(new Runnable() {
688                        @Override
689                        public void run() {
690                            updateUiByData(newDataId);
691                        }
692                    });
693                }
694
695                @Override
696                public void onScroll(int firstVisiblePosition, int visibleItemCount, int totalItemCount) {
697                    mPreloader.onScroll(null /*absListView*/, firstVisiblePosition, visibleItemCount, totalItemCount);
698                }
699            };
700
701    private final LocalDataAdapter.LocalDataListener mLocalDataListener =
702            new LocalDataAdapter.LocalDataListener() {
703                @Override
704                public void onMetadataUpdated(List<Integer> updatedData) {
705                    if (mPaused) {
706                        // Callback after the activity is paused.
707                        return;
708                    }
709                    int currentDataId = mFilmstripController.getCurrentId();
710                    for (Integer dataId : updatedData) {
711                        if (dataId == currentDataId) {
712                            updateBottomControlsByData(mDataAdapter.getLocalData(dataId));
713                            // Currently we have only 1 data can be matched.
714                            // No need to look for more, break.
715                            break;
716                        }
717                    }
718                }
719            };
720
721    public void gotoGallery() {
722        UsageStatistics.instance().changeScreen(NavigationChange.Mode.FILMSTRIP,
723                NavigationChange.InteractionCause.BUTTON);
724
725        mFilmstripController.goToNextItem();
726    }
727
728    /**
729     * If 'visible' is false, this hides the action bar. Also maintains
730     * lights-out at all times.
731     *
732     * @param visible is false, this hides the action bar and filmstrip bottom
733     *            controls.
734     */
735    private void setFilmstripUiVisibility(boolean visible) {
736        mLightsOutRunnable.run();
737        mCameraAppUI.getFilmstripBottomControls().setVisible(visible);
738        if (visible != mActionBar.isShowing()) {
739            if (visible) {
740                mActionBar.show();
741            } else {
742                mActionBar.hide();
743            }
744        }
745        mFilmstripCoversPreview = visible;
746        updatePreviewVisibility();
747    }
748
749    private void hideSessionProgress() {
750        mCameraAppUI.getFilmstripBottomControls().hideProgress();
751    }
752
753    private void showSessionProgress(CharSequence message) {
754        CameraAppUI.BottomPanel controls = mCameraAppUI.getFilmstripBottomControls();
755        controls.setProgressText(message);
756        controls.hideControls();
757        controls.hideProgressError();
758        controls.showProgress();
759    }
760
761    private void showProcessError(CharSequence message) {
762        mCameraAppUI.getFilmstripBottomControls().showProgressError(message);
763    }
764
765    private void updateSessionProgress(int progress) {
766        mCameraAppUI.getFilmstripBottomControls().setProgress(progress);
767    }
768
769    private void updateSessionProgressText(CharSequence message) {
770        mCameraAppUI.getFilmstripBottomControls().setProgressText(message);
771    }
772
773    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
774    private void setupNfcBeamPush() {
775        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mAppContext);
776        if (adapter == null) {
777            return;
778        }
779
780        if (!ApiHelper.HAS_SET_BEAM_PUSH_URIS) {
781            // Disable beaming
782            adapter.setNdefPushMessage(null, CameraActivity.this);
783            return;
784        }
785
786        adapter.setBeamPushUris(null, CameraActivity.this);
787        adapter.setBeamPushUrisCallback(new CreateBeamUrisCallback() {
788            @Override
789            public Uri[] createBeamUris(NfcEvent event) {
790                return mNfcPushUris;
791            }
792        }, CameraActivity.this);
793    }
794
795    @Override
796    public void onMenuVisibilityChanged(boolean isVisible) {
797        // TODO: Remove this or bring back the original implementation: cancel
798        // auto-hide actionbar.
799    }
800
801    @Override
802    public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent) {
803        int currentDataId = mFilmstripController.getCurrentId();
804        if (currentDataId < 0) {
805            return false;
806        }
807        UsageStatistics.instance().mediaInteraction(fileNameFromDataID(currentDataId),
808                MediaInteraction.InteractionType.SHARE,
809                NavigationChange.InteractionCause.BUTTON, fileAgeFromDataID(currentDataId));
810        // TODO add intent.getComponent().getPackageName()
811        return true;
812    }
813
814    // Note: All callbacks come back on the main thread.
815    private final SessionListener mSessionListener =
816            new SessionListener() {
817                @Override
818                public void onSessionQueued(final Uri uri) {
819                    if (!Storage.isSessionUri(uri)) {
820                        return;
821                    }
822                    LocalSessionData newData = new LocalSessionData(uri);
823                    mDataAdapter.addData(newData);
824                }
825
826                @Override
827                public void onSessionDone(final Uri sessionUri) {
828                    Log.v(TAG, "onSessionDone:" + sessionUri);
829                    Uri contentUri = Storage.getContentUriForSessionUri(sessionUri);
830                    if (contentUri == null) {
831                        mDataAdapter.refresh(sessionUri);
832                        return;
833                    }
834                    LocalData newData = LocalMediaData.PhotoData.fromContentUri(
835                            getContentResolver(), contentUri);
836
837                    final int pos = mDataAdapter.findDataByContentUri(sessionUri);
838                    if (pos == -1) {
839                        // We do not have a placeholder for this image, perhaps
840                        // due to the activity crashing or being killed.
841                        mDataAdapter.addData(newData);
842                    } else {
843                        mDataAdapter.updateData(pos, newData);
844                    }
845                }
846
847                @Override
848                public void onSessionProgress(final Uri uri, final int progress) {
849                    if (progress < 0) {
850                        // Do nothing, there is no task for this URI.
851                        return;
852                    }
853                    int currentDataId = mFilmstripController.getCurrentId();
854                    if (currentDataId == -1) {
855                        return;
856                    }
857                    if (uri.equals(
858                            mDataAdapter.getLocalData(currentDataId).getUri())) {
859                        updateSessionProgress(progress);
860                    }
861                }
862
863                @Override
864                public void onSessionProgressText(final Uri uri, final CharSequence message) {
865                    int currentDataId = mFilmstripController.getCurrentId();
866                    if (currentDataId == -1) {
867                        return;
868                    }
869                    if (uri.equals(
870                            mDataAdapter.getLocalData(currentDataId).getUri())) {
871                        updateSessionProgressText(message);
872                    }
873                }
874
875                @Override
876                public void onSessionUpdated(Uri uri) {
877                    mDataAdapter.refresh(uri);
878                }
879
880                @Override
881                public void onSessionPreviewAvailable(Uri uri) {
882                    mDataAdapter.refresh(uri);
883                    int dataId = mDataAdapter.findDataByContentUri(uri);
884                    if (dataId != -1) {
885                        startPeekAnimation(mDataAdapter.getLocalData(dataId));
886                    }
887                }
888
889                @Override
890                public void onSessionFailed(Uri uri, CharSequence reason) {
891                    Log.v(TAG, "onSessionFailed:" + uri);
892
893                    int failedDataId = mDataAdapter.findDataByContentUri(uri);
894                    int currentDataId = mFilmstripController.getCurrentId();
895
896                    if (currentDataId == failedDataId) {
897                        updateSessionProgress(0);
898                        showProcessError(reason);
899                    }
900                    // HERE
901                    mDataAdapter.refresh(uri);
902                }
903            };
904
905    @Override
906    public Context getAndroidContext() {
907        return mAppContext;
908    }
909
910    @Override
911    public void launchActivityByIntent(Intent intent) {
912        startActivityForResult(intent, REQ_CODE_DONT_SWITCH_TO_PREVIEW);
913    }
914
915    @Override
916    public int getCurrentModuleIndex() {
917        return mCurrentModeIndex;
918    }
919
920    @Override
921    public ModuleController getCurrentModuleController() {
922        return mCurrentModule;
923    }
924
925    @Override
926    public int getQuickSwitchToModuleId(int currentModuleIndex) {
927        return mModuleManager.getQuickSwitchToModuleId(currentModuleIndex, mSettingsManager,
928                mAppContext);
929    }
930
931    @Override
932    public SurfaceTexture getPreviewBuffer() {
933        // TODO: implement this
934        return null;
935    }
936
937    @Override
938    public void onPreviewReadyToStart() {
939        mCameraAppUI.onPreviewReadyToStart();
940    }
941
942    @Override
943    public void onPreviewStarted() {
944        mCameraAppUI.onPreviewStarted();
945    }
946
947    @Override
948    public void addPreviewAreaSizeChangedListener(
949            PreviewStatusListener.PreviewAreaChangedListener listener) {
950        mCameraAppUI.addPreviewAreaChangedListener(listener);
951    }
952
953    @Override
954    public void removePreviewAreaSizeChangedListener(
955            PreviewStatusListener.PreviewAreaChangedListener listener) {
956        mCameraAppUI.removePreviewAreaChangedListener(listener);
957    }
958
959    @Override
960    public void setupOneShotPreviewListener() {
961        mCameraController.setOneShotPreviewCallback(mMainHandler,
962                new CameraManager.CameraPreviewDataCallback() {
963                    @Override
964                    public void onPreviewFrame(byte[] data, CameraManager.CameraProxy camera) {
965                        mCurrentModule.onPreviewInitialDataReceived();
966                        mCameraAppUI.onNewPreviewFrame();
967                    }
968                }
969        );
970    }
971
972    @Override
973    public void updatePreviewAspectRatio(float aspectRatio) {
974        mCameraAppUI.updatePreviewAspectRatio(aspectRatio);
975    }
976
977    @Override
978    public void updatePreviewTransformFullscreen(Matrix matrix, float aspectRatio) {
979        mCameraAppUI.updatePreviewTransformFullscreen(matrix, aspectRatio);
980    }
981
982    @Override
983    public RectF getFullscreenRect() {
984        return mCameraAppUI.getFullscreenRect();
985    }
986
987    @Override
988    public void updatePreviewTransform(Matrix matrix) {
989        mCameraAppUI.updatePreviewTransform(matrix);
990    }
991
992    @Override
993    public void setPreviewStatusListener(PreviewStatusListener previewStatusListener) {
994        mCameraAppUI.setPreviewStatusListener(previewStatusListener);
995    }
996
997    @Override
998    public FrameLayout getModuleLayoutRoot() {
999        return mCameraAppUI.getModuleRootView();
1000    }
1001
1002    @Override
1003    public void setShutterEventsListener(ShutterEventsListener listener) {
1004        // TODO: implement this
1005    }
1006
1007    @Override
1008    public void setShutterEnabled(boolean enabled) {
1009        mCameraAppUI.setShutterButtonEnabled(enabled);
1010    }
1011
1012    @Override
1013    public boolean isShutterEnabled() {
1014        return mCameraAppUI.isShutterButtonEnabled();
1015    }
1016
1017    @Override
1018    public void startPreCaptureAnimation() {
1019        mCameraAppUI.startPreCaptureAnimation();
1020    }
1021
1022    @Override
1023    public void cancelPreCaptureAnimation() {
1024        // TODO: implement this
1025    }
1026
1027    @Override
1028    public void startPostCaptureAnimation() {
1029        // TODO: implement this
1030    }
1031
1032    @Override
1033    public void startPostCaptureAnimation(Bitmap thumbnail) {
1034        // TODO: implement this
1035    }
1036
1037    @Override
1038    public void cancelPostCaptureAnimation() {
1039        // TODO: implement this
1040    }
1041
1042    @Override
1043    public OrientationManager getOrientationManager() {
1044        return mOrientationManager;
1045    }
1046
1047    @Override
1048    public LocationManager getLocationManager() {
1049        return mLocationManager;
1050    }
1051
1052    @Override
1053    public void lockOrientation() {
1054        if (mOrientationManager != null) {
1055            mOrientationManager.lockOrientation();
1056        }
1057    }
1058
1059    @Override
1060    public void unlockOrientation() {
1061        if (mOrientationManager != null) {
1062            mOrientationManager.unlockOrientation();
1063        }
1064    }
1065
1066    /**
1067     * Starts the filmstrip peek animation if the filmstrip is not visible.
1068     * Only {@link LocalData#LOCAL_IMAGE}, {@link
1069     * LocalData#LOCAL_IN_PROGRESS_DATA} and {@link
1070     * LocalData#LOCAL_VIDEO} are supported.
1071     *
1072     * @param data The data to peek.
1073     */
1074    private void startPeekAnimation(final LocalData data) {
1075        if (mFilmstripVisible || mPeekAnimationHandler == null) {
1076            return;
1077        }
1078
1079        int dataType = data.getLocalDataType();
1080        if (dataType != LocalData.LOCAL_IMAGE && dataType != LocalData.LOCAL_IN_PROGRESS_DATA &&
1081                dataType != LocalData.LOCAL_VIDEO) {
1082            return;
1083        }
1084
1085        mPeekAnimationHandler.startDecodingJob(data, new Callback<Bitmap>() {
1086            @Override
1087            public void onCallback(Bitmap result) {
1088                mCameraAppUI.startPeekAnimation(result, true);
1089            }
1090        });
1091    }
1092
1093    @Override
1094    public void notifyNewMedia(Uri uri) {
1095        updateStorageSpaceAndHint(null);
1096        ContentResolver cr = getContentResolver();
1097        String mimeType = cr.getType(uri);
1098        LocalData newData = null;
1099        if (LocalDataUtil.isMimeTypeVideo(mimeType)) {
1100            sendBroadcast(new Intent(CameraUtil.ACTION_NEW_VIDEO, uri));
1101            newData = LocalMediaData.VideoData.fromContentUri(getContentResolver(), uri);
1102            if (newData == null) {
1103                Log.e(TAG, "Can't find video data in content resolver:" + uri);
1104                return;
1105            }
1106        } else if (LocalDataUtil.isMimeTypeImage(mimeType)) {
1107            CameraUtil.broadcastNewPicture(mAppContext, uri);
1108            newData = LocalMediaData.PhotoData.fromContentUri(getContentResolver(), uri);
1109            if (newData == null) {
1110                Log.e(TAG, "Can't find photo data in content resolver:" + uri);
1111                return;
1112            }
1113        } else {
1114            Log.w(TAG, "Unknown new media with MIME type:" + mimeType + ", uri:" + uri);
1115            return;
1116        }
1117        // We are preloading the metadata for new video since we need the
1118        // rotation info for the thumbnail.
1119        new AsyncTask<LocalData, Void, LocalData>() {
1120
1121            @Override
1122            protected LocalData doInBackground(LocalData... params) {
1123                LocalData data = params[0];
1124                MetadataLoader.loadMetadata(getAndroidContext(), data);
1125                return data;
1126            }
1127
1128            @Override
1129            protected void onPostExecute(LocalData data) {
1130                if (mDataAdapter.addData(data)) {
1131                    startPeekAnimation(data);
1132                }
1133            }
1134        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, newData);
1135    }
1136
1137    @Override
1138    public void enableKeepScreenOn(boolean enabled) {
1139        if (mPaused) {
1140            return;
1141        }
1142
1143        mKeepScreenOn = enabled;
1144        if (mKeepScreenOn) {
1145            mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
1146            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1147        } else {
1148            keepScreenOnForAWhile();
1149        }
1150    }
1151
1152    @Override
1153    public CameraProvider getCameraProvider() {
1154        return mCameraController;
1155    }
1156
1157    private void removeData(int dataID) {
1158        mDataAdapter.removeData(dataID);
1159        if (mDataAdapter.getTotalNumber() > 1) {
1160            showUndoDeletionBar();
1161        } else {
1162            // If camera preview is the only view left in filmstrip,
1163            // no need to show undo bar.
1164            mPendingDeletion = true;
1165            performDeletion();
1166            if (mFilmstripVisible) {
1167                mCameraAppUI.getFilmstripContentPanel().animateHide();
1168            }
1169        }
1170    }
1171
1172    @Override
1173    public boolean onOptionsItemSelected(MenuItem item) {
1174        // Handle presses on the action bar items
1175        switch (item.getItemId()) {
1176            case android.R.id.home:
1177                if (mFilmstripVisible && startGallery()) {
1178                    return true;
1179                }
1180                onBackPressed();
1181                return true;
1182            case R.id.action_details:
1183                showDetailsDialog(mFilmstripController.getCurrentId());
1184                return true;
1185            default:
1186                return super.onOptionsItemSelected(item);
1187        }
1188    }
1189
1190    private boolean isCaptureIntent() {
1191        if (MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent().getAction())
1192                || MediaStore.ACTION_IMAGE_CAPTURE.equals(getIntent().getAction())
1193                || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(getIntent().getAction())) {
1194            return true;
1195        } else {
1196            return false;
1197        }
1198    }
1199
1200    private final CameraManager.CameraExceptionCallback mCameraDefaultExceptionCallback
1201        = new CameraManager.CameraExceptionCallback() {
1202                @Override
1203                public void onCameraException(RuntimeException e) {
1204                    Log.e(TAG, "Camera Exception", e);
1205                    CameraUtil.showErrorAndFinish(CameraActivity.this,
1206                            R.string.cannot_connect_camera);
1207                }
1208            };
1209
1210    @Override
1211    public void onCreate(Bundle state) {
1212        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.ACTIVITY_START);
1213
1214        super.onCreate(state);
1215        final Glide glide = Glide.get();
1216        if (!glide.isImageManagerSet()) {
1217            // We load exclusively large images, so we want fewer threads to minimize jank.
1218            glide.setImageManager(new ImageManager.Builder(getApplicationContext())
1219                    .setResizeService(Executors.newSingleThreadExecutor()));
1220        }
1221
1222        mOnCreateTime = System.currentTimeMillis();
1223        mAppContext = getApplicationContext();
1224        mSettingsManager = new SettingsManager(mAppContext, this);
1225        GcamHelper.init(getContentResolver());
1226
1227        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
1228        setContentView(R.layout.activity_main);
1229        mActionBar = getActionBar();
1230        mActionBar.addOnMenuVisibilityListener(this);
1231        mMainHandler = new MainHandler(this, getMainLooper());
1232        mCameraController =
1233                new CameraController(mAppContext, this, mMainHandler,
1234                        CameraManagerFactory.getAndroidCameraManager());
1235        mCameraController.setCameraDefaultExceptionCallback(mCameraDefaultExceptionCallback,
1236                mMainHandler);
1237
1238        // TODO: Try to move all the resources allocation to happen as soon as
1239        // possible so we can call module.init() at the earliest time.
1240        mModuleManager = new ModuleManagerImpl();
1241        ModulesInfo.setupModules(mAppContext, mModuleManager);
1242
1243        mModeListView = (ModeListView) findViewById(R.id.mode_list_layout);
1244        mModeListView.init(mModuleManager.getSupportedModeIndexList());
1245        if (ApiHelper.HAS_ROTATION_ANIMATION) {
1246            setRotationAnimation();
1247        }
1248        mModeListView.setVisibilityChangedListener(new ModeListVisibilityChangedListener() {
1249            @Override
1250            public void onVisibilityChanged(boolean visible) {
1251                mModeListVisible = visible;
1252                updatePreviewVisibility();
1253            }
1254        });
1255
1256        // Check if this is in the secure camera mode.
1257        Intent intent = getIntent();
1258        String action = intent.getAction();
1259        if (INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action)
1260                || ACTION_IMAGE_CAPTURE_SECURE.equals(action)) {
1261            mSecureCamera = true;
1262        } else {
1263            mSecureCamera = intent.getBooleanExtra(SECURE_CAMERA_EXTRA, false);
1264        }
1265
1266        if (mSecureCamera) {
1267            // Foreground event caused by lock screen startup.
1268            // It is necessary to log this in onCreate, to avoid the
1269            // onResume->onPause->onResume sequence.
1270            UsageStatistics.instance().foregrounded(ForegroundSource.ACTION_IMAGE_CAPTURE_SECURE,
1271                    currentUserInterfaceMode());
1272
1273            // Change the window flags so that secure camera can show when
1274            // locked
1275            Window win = getWindow();
1276            WindowManager.LayoutParams params = win.getAttributes();
1277            params.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
1278            win.setAttributes(params);
1279
1280            // Filter for screen off so that we can finish secure camera
1281            // activity
1282            // when screen is off.
1283            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
1284            registerReceiver(mScreenOffReceiver, filter);
1285        }
1286        mCameraAppUI = new CameraAppUI(this,
1287                (MainActivityLayout) findViewById(R.id.activity_root_view), isCaptureIntent());
1288
1289        mCameraAppUI.setFilmstripBottomControlsListener(mMyFilmstripBottomControlListener);
1290
1291        mAboveFilmstripControlLayout =
1292                (FrameLayout) findViewById(R.id.camera_filmstrip_content_layout);
1293
1294        // Add the session listener so we can track the session progress
1295        // updates.
1296        getServices().getCaptureSessionManager().addSessionListener(mSessionListener);
1297        mFilmstripController = ((FilmstripView) findViewById(R.id.filmstrip_view)).getController();
1298        mFilmstripController.setImageGap(
1299                getResources().getDimensionPixelSize(R.dimen.camera_film_strip_gap));
1300        mPanoramaViewHelper = new PanoramaViewHelper(this);
1301        mPanoramaViewHelper.onCreate();
1302        // Set up the camera preview first so the preview shows up ASAP.
1303        mDataAdapter = new CameraDataAdapter(mAppContext, R.color.photo_placeholder);
1304        mDataAdapter.setLocalDataListener(mLocalDataListener);
1305
1306        mPreloader = new Preloader<Integer, AsyncTask>(FILMSTRIP_PRELOAD_AHEAD_ITEMS, mDataAdapter,
1307                mDataAdapter);
1308
1309        mCameraAppUI.getFilmstripContentPanel().setFilmstripListener(mFilmstripListener);
1310        if (mSettingsManager.getBoolean(SettingsManager.SETTING_SHOULD_SHOW_REFOCUS_VIEWER_CLING)) {
1311            mCameraAppUI.setupClingForViewer(CameraAppUI.BottomPanel.VIEWER_REFOCUS);
1312        }
1313
1314        mLocationManager = new LocationManager(mAppContext);
1315
1316        mOrientationManager = new OrientationManagerImpl(this);
1317        mOrientationManager.addOnOrientationChangeListener(mMainHandler, this);
1318
1319        setModuleFromModeIndex(getModeIndex());
1320        mCameraAppUI.prepareModuleUI();
1321        mCurrentModule.init(this, isSecureCamera(), isCaptureIntent());
1322
1323        if (!mSecureCamera) {
1324            mFilmstripController.setDataAdapter(mDataAdapter);
1325            if (!isCaptureIntent()) {
1326                mDataAdapter.requestLoad(new Callback<Void>() {
1327                    @Override
1328                    public void onCallback(Void result) {
1329                        fillTemporarySessions();
1330                    }
1331                });
1332            }
1333        } else {
1334            // Put a lock placeholder as the last image by setting its date to
1335            // 0.
1336            ImageView v = (ImageView) getLayoutInflater().inflate(
1337                    R.layout.secure_album_placeholder, null);
1338            v.setTag(R.id.mediadata_tag_viewtype, LocalDataViewType.SECURE_ALBUM_PLACEHOLDER.ordinal());
1339            v.setOnClickListener(new View.OnClickListener() {
1340                @Override
1341                public void onClick(View view) {
1342                    UsageStatistics.instance().changeScreen(NavigationChange.Mode.GALLERY,
1343                            NavigationChange.InteractionCause.BUTTON);
1344                    startGallery();
1345                    finish();
1346                }
1347            });
1348            v.setContentDescription(getString(R.string.accessibility_unlock_to_camera));
1349            mDataAdapter = new FixedLastDataAdapter(
1350                    mAppContext,
1351                    mDataAdapter,
1352                    new SimpleViewData(
1353                            v,
1354                            LocalDataViewType.SECURE_ALBUM_PLACEHOLDER,
1355                            v.getDrawable().getIntrinsicWidth(),
1356                            v.getDrawable().getIntrinsicHeight(),
1357                            0, 0));
1358            // Flush out all the original data.
1359            mDataAdapter.flush();
1360            mFilmstripController.setDataAdapter(mDataAdapter);
1361        }
1362
1363        setupNfcBeamPush();
1364
1365        mLocalImagesObserver = new LocalMediaObserver();
1366        mLocalVideosObserver = new LocalMediaObserver();
1367
1368        getContentResolver().registerContentObserver(
1369                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true,
1370                mLocalImagesObserver);
1371        getContentResolver().registerContentObserver(
1372                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, true,
1373                mLocalVideosObserver);
1374        if (FeedbackHelper.feedbackAvailable()) {
1375            mFeedbackHelper = new FeedbackHelper(mAppContext);
1376        }
1377        mMemoryManager = getServices().getMemoryManager();
1378
1379        AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1380            @Override
1381            public void run() {
1382                HashMap memoryData = mMemoryManager.queryMemory();
1383                UsageStatistics.instance().reportMemoryConsumed(memoryData,
1384                        MemoryQuery.REPORT_LABEL_LAUNCH);
1385            }
1386        });
1387    }
1388
1389    /**
1390     * Get the current mode index from the Intent or from persistent
1391     * settings.
1392     */
1393    public int getModeIndex() {
1394        int modeIndex = -1;
1395        int photoIndex = getResources().getInteger(R.integer.camera_mode_photo);
1396        int videoIndex = getResources().getInteger(R.integer.camera_mode_video);
1397        int gcamIndex = getResources().getInteger(R.integer.camera_mode_gcam);
1398        if (MediaStore.INTENT_ACTION_VIDEO_CAMERA.equals(getIntent().getAction())
1399                || MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent().getAction())) {
1400            modeIndex = videoIndex;
1401        } else if (MediaStore.ACTION_IMAGE_CAPTURE.equals(getIntent().getAction())) {
1402            // Capture intent.
1403            modeIndex = photoIndex;
1404        } else if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(getIntent().getAction())
1405                ||MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(getIntent()
1406                        .getAction())
1407                || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(getIntent().getAction())) {
1408            modeIndex = mSettingsManager.getInt(
1409                SettingsManager.SETTING_KEY_CAMERA_MODULE_LAST_USED_INDEX);
1410
1411            // For upgraders who have not seen the aspect ratio selection screen,
1412            // we need to drop them back in the photo module and have them select
1413            // aspect ratio.
1414            // TODO: Move this to SettingsManager as an upgrade procedure.
1415            if (!mSettingsManager.getBoolean(SettingsManager.SETTING_USER_SELECTED_ASPECT_RATIO)) {
1416                modeIndex = photoIndex;
1417            }
1418        } else {
1419            // If the activity has not been started using an explicit intent,
1420            // read the module index from the last time the user changed modes
1421            modeIndex = mSettingsManager.getInt(SettingsManager.SETTING_STARTUP_MODULE_INDEX);
1422            if ((modeIndex == gcamIndex &&
1423                    !GcamHelper.hasGcamCapture()) || modeIndex < 0) {
1424                modeIndex = photoIndex;
1425            }
1426        }
1427        return modeIndex;
1428    }
1429
1430    /**
1431     * Call this whenever the mode drawer or filmstrip change the visibility
1432     * state.
1433     */
1434    private void updatePreviewVisibility() {
1435        if (mCurrentModule == null) {
1436            return;
1437        }
1438
1439        int visibility = getPreviewVisibility();
1440        mCameraAppUI.onPreviewVisiblityChanged(visibility);
1441        updatePreviewRendering(visibility);
1442        mCurrentModule.onPreviewVisibilityChanged(visibility);
1443    }
1444
1445    private void updatePreviewRendering(int visibility) {
1446        if (visibility == ModuleController.VISIBILITY_HIDDEN) {
1447            mCameraAppUI.pausePreviewRendering();
1448        } else {
1449            mCameraAppUI.resumePreviewRendering();
1450        }
1451    }
1452
1453    private int getPreviewVisibility() {
1454        if (mFilmstripCoversPreview) {
1455            return ModuleController.VISIBILITY_HIDDEN;
1456        } else if (mModeListVisible){
1457            return ModuleController.VISIBILITY_COVERED;
1458        } else {
1459            return ModuleController.VISIBILITY_VISIBLE;
1460        }
1461    }
1462
1463    private void setRotationAnimation() {
1464        int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE;
1465        rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
1466        Window win = getWindow();
1467        WindowManager.LayoutParams winParams = win.getAttributes();
1468        winParams.rotationAnimation = rotationAnimation;
1469        win.setAttributes(winParams);
1470    }
1471
1472    @Override
1473    public void onUserInteraction() {
1474        super.onUserInteraction();
1475        if (!isFinishing()) {
1476            keepScreenOnForAWhile();
1477        }
1478    }
1479
1480    @Override
1481    public boolean dispatchTouchEvent(MotionEvent ev) {
1482        boolean result = super.dispatchTouchEvent(ev);
1483        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
1484            // Real deletion is postponed until the next user interaction after
1485            // the gesture that triggers deletion. Until real deletion is
1486            // performed, users can click the undo button to bring back the
1487            // image that they chose to delete.
1488            if (mPendingDeletion && !mIsUndoingDeletion) {
1489                performDeletion();
1490            }
1491        }
1492        return result;
1493    }
1494
1495    @Override
1496    public void onPause() {
1497        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.ACTIVITY_PAUSE);
1498
1499        /*
1500         * Save the last module index after all secure camera and icon launches,
1501         * not just on mode switches.
1502         *
1503         * Right now we exclude capture intents from this logic, because we also
1504         * ignore the cross-Activity recovery logic in onStart for capture intents.
1505         */
1506        if (!isCaptureIntent()) {
1507            mSettingsManager.setInt(SettingsManager.SETTING_STARTUP_MODULE_INDEX,
1508                mCurrentModeIndex);
1509        }
1510
1511        mPaused = true;
1512        mPeekAnimationHandler = null;
1513        mPeekAnimationThread.quitSafely();
1514        mPeekAnimationThread = null;
1515
1516        // Delete photos that are pending deletion
1517        performDeletion();
1518        mCurrentModule.pause();
1519        mOrientationManager.pause();
1520        // Close the camera and wait for the operation done.
1521        mCameraController.closeCamera(true);
1522        mPanoramaViewHelper.onPause();
1523
1524        mLocalImagesObserver.setForegroundChangeListener(null);
1525        mLocalImagesObserver.setActivityPaused(true);
1526        mLocalVideosObserver.setActivityPaused(true);
1527        mPreloader.cancelAllLoads();
1528        resetScreenOn();
1529
1530        UsageStatistics.instance().backgrounded();
1531
1532        super.onPause();
1533    }
1534
1535    @Override
1536    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1537        if (requestCode == REQ_CODE_DONT_SWITCH_TO_PREVIEW) {
1538            mResetToPreviewOnResume = false;
1539        } else {
1540            super.onActivityResult(requestCode, resultCode, data);
1541        }
1542    }
1543
1544    @Override
1545    public void onResume() {
1546        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.ACTIVITY_RESUME);
1547        Log.v(TAG, "Build info: " + Build.DISPLAY);
1548
1549        mPaused = false;
1550        updateStorageSpaceAndHint(null);
1551
1552        mLastLayoutOrientation = getResources().getConfiguration().orientation;
1553
1554        // TODO: Handle this in OrientationManager.
1555        // Auto-rotate off
1556        if (Settings.System.getInt(getContentResolver(),
1557                Settings.System.ACCELEROMETER_ROTATION, 0) == 0) {
1558            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
1559            mAutoRotateScreen = false;
1560        } else {
1561            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
1562            mAutoRotateScreen = true;
1563        }
1564
1565        String action = getIntent().getAction();
1566        // Foreground event logging
1567        if (MediaStore.ACTION_VIDEO_CAPTURE.equals(action)) {
1568            UsageStatistics.instance().foregrounded(ForegroundSource.ACTION_VIDEO_CAPTURE,
1569                    currentUserInterfaceMode());
1570        } else if (MediaStore.ACTION_IMAGE_CAPTURE.equals(action)) {
1571            UsageStatistics.instance().foregrounded(ForegroundSource.ACTION_IMAGE_CAPTURE,
1572                    currentUserInterfaceMode());
1573        } else if (MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(action) ||
1574                INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action)) {
1575            // logged in onCreate()
1576        } else if (Intent.ACTION_MAIN.equals(action)) {
1577            UsageStatistics.instance().foregrounded(ForegroundSource.ACTION_MAIN,
1578                    currentUserInterfaceMode());
1579        } else {
1580            UsageStatistics.instance().foregrounded(ForegroundSource.UNKNOWN_SOURCE,
1581                    currentUserInterfaceMode());
1582        }
1583
1584        mGalleryIntent = IntentHelper.getPhotosGalleryIntent(mAppContext);
1585        Drawable galleryLogo = IntentHelper.getGalleryIcon(mAppContext, mGalleryIntent);
1586        if (galleryLogo == null) {
1587            try {
1588                galleryLogo = getPackageManager().getActivityLogo(getComponentName());
1589            } catch (PackageManager.NameNotFoundException e) {
1590                Log.e(TAG, "Can't get the activity logo");
1591            }
1592        }
1593        if (mGalleryIntent != null) {
1594            mActionBar.setDisplayUseLogoEnabled(true);
1595        }
1596        mActionBar.setLogo(galleryLogo);
1597        mOrientationManager.resume();
1598        super.onResume();
1599        mPeekAnimationThread = new HandlerThread("Peek animation");
1600        mPeekAnimationThread.start();
1601        mPeekAnimationHandler = new PeekAnimationHandler(mPeekAnimationThread.getLooper());
1602
1603        mCurrentModule.hardResetSettings(mSettingsManager);
1604        mCurrentModule.resume();
1605        UsageStatistics.instance().changeScreen(currentUserInterfaceMode(),
1606                NavigationChange.InteractionCause.BUTTON);
1607        setSwipingEnabled(true);
1608
1609        if (!mResetToPreviewOnResume) {
1610            LocalData data = mDataAdapter.getLocalData(mFilmstripController.getCurrentId());
1611            if (data != null) {
1612                mDataAdapter.refresh(data.getUri());
1613            }
1614        }
1615        // The share button might be disabled to avoid double tapping.
1616        mCameraAppUI.getFilmstripBottomControls().setShareEnabled(true);
1617        // Default is showing the preview, unless disabled by explicitly
1618        // starting an activity we want to return from to the filmstrip rather
1619        // than the preview.
1620        mResetToPreviewOnResume = true;
1621
1622        if (mLocalVideosObserver.isMediaDataChangedDuringPause()
1623                || mLocalImagesObserver.isMediaDataChangedDuringPause()) {
1624            if (!mSecureCamera) {
1625                // If it's secure camera, requestLoad() should not be called
1626                // as it will load all the data.
1627                if (!mFilmstripVisible) {
1628                    mDataAdapter.requestLoad(new Callback<Void>() {
1629                        @Override
1630                        public void onCallback(Void result) {
1631                            fillTemporarySessions();
1632                        }
1633                    });
1634                } else {
1635                    mDataAdapter.requestLoadNewPhotos();
1636                }
1637            }
1638        }
1639        mLocalImagesObserver.setActivityPaused(false);
1640        mLocalVideosObserver.setActivityPaused(false);
1641        if (!mSecureCamera) {
1642            mLocalImagesObserver.setForegroundChangeListener(
1643                    new LocalMediaObserver.ChangeListener() {
1644                @Override
1645                public void onChange() {
1646                    mDataAdapter.requestLoadNewPhotos();
1647                }
1648            });
1649        }
1650
1651        keepScreenOnForAWhile();
1652
1653        // Lights-out mode at all times.
1654        final View rootView = findViewById(R.id.activity_root_view);
1655        mLightsOutRunnable.run();
1656        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(
1657                new OnSystemUiVisibilityChangeListener() {
1658                    @Override
1659                    public void onSystemUiVisibilityChange(int visibility) {
1660                        mMainHandler.removeCallbacks(mLightsOutRunnable);
1661                        mMainHandler.postDelayed(mLightsOutRunnable, LIGHTS_OUT_DELAY_MS);
1662                    }
1663                });
1664
1665        mPanoramaViewHelper.onResume();
1666        ReleaseDialogHelper.showReleaseInfoDialogOnStart(this, mSettingsManager);
1667        syncLocationManagerSetting();
1668
1669        final int previewVisibility = getPreviewVisibility();
1670        updatePreviewRendering(previewVisibility);
1671    }
1672
1673    private void fillTemporarySessions() {
1674        if (mSecureCamera) {
1675            return;
1676        }
1677        // There might be sessions still in flight (processed by our service).
1678        // Make sure they're added to the filmstrip.
1679        getServices().getCaptureSessionManager().fillTemporarySession(mSessionListener);
1680    }
1681
1682    @Override
1683    public void onStart() {
1684        super.onStart();
1685        mPanoramaViewHelper.onStart();
1686
1687        /*
1688         * If we're starting after launching a different Activity (lockscreen),
1689         * we need to use the last mode used in the other Activity, and
1690         * not the old one from this Activity.
1691         *
1692         * This needs to happen before CameraAppUI.resume() in order to set the
1693         * mode cover icon to the actual last mode used.
1694         *
1695         * Right now we exclude capture intents from this logic.
1696         */
1697        int modeIndex = getModeIndex();
1698        if (!isCaptureIntent() && mCurrentModeIndex != modeIndex) {
1699            onModeSelected(modeIndex);
1700        }
1701
1702        if (mResetToPreviewOnResume) {
1703            mCameraAppUI.resume();
1704            mResetToPreviewOnResume = false;
1705        }
1706    }
1707
1708    @Override
1709    protected void onStop() {
1710        mPanoramaViewHelper.onStop();
1711        if (mFeedbackHelper != null) {
1712            mFeedbackHelper.stopFeedback();
1713        }
1714
1715        mLocationManager.disconnect();
1716        super.onStop();
1717    }
1718
1719    @Override
1720    public void onDestroy() {
1721        if (mSecureCamera) {
1722            unregisterReceiver(mScreenOffReceiver);
1723        }
1724        mActionBar.removeOnMenuVisibilityListener(this);
1725        mSettingsManager.removeAllListeners();
1726        mCameraController.removeCallbackReceiver();
1727        getContentResolver().unregisterContentObserver(mLocalImagesObserver);
1728        getContentResolver().unregisterContentObserver(mLocalVideosObserver);
1729        getServices().getCaptureSessionManager().removeSessionListener(mSessionListener);
1730        mCameraAppUI.onDestroy();
1731        mModeListView.setVisibilityChangedListener(null);
1732        mCameraController = null;
1733        mSettingsManager = null;
1734        mCameraAppUI = null;
1735        mOrientationManager = null;
1736        mButtonManager = null;
1737        CameraManagerFactory.recycle();
1738        super.onDestroy();
1739    }
1740
1741    @Override
1742    public void onConfigurationChanged(Configuration config) {
1743        super.onConfigurationChanged(config);
1744        Log.v(TAG, "onConfigurationChanged");
1745        if (config.orientation == Configuration.ORIENTATION_UNDEFINED) {
1746            return;
1747        }
1748
1749        if (mLastLayoutOrientation != config.orientation) {
1750            mLastLayoutOrientation = config.orientation;
1751            mCurrentModule.onLayoutOrientationChanged(
1752                    mLastLayoutOrientation == Configuration.ORIENTATION_LANDSCAPE);
1753        }
1754    }
1755
1756    @Override
1757    public boolean onKeyDown(int keyCode, KeyEvent event) {
1758        if (!mFilmstripVisible) {
1759            if (mCurrentModule.onKeyDown(keyCode, event)) {
1760                return true;
1761            }
1762            // Prevent software keyboard or voice search from showing up.
1763            if (keyCode == KeyEvent.KEYCODE_SEARCH
1764                    || keyCode == KeyEvent.KEYCODE_MENU) {
1765                if (event.isLongPress()) {
1766                    return true;
1767                }
1768            }
1769        }
1770
1771        return super.onKeyDown(keyCode, event);
1772    }
1773
1774    @Override
1775    public boolean onKeyUp(int keyCode, KeyEvent event) {
1776        if (!mFilmstripVisible) {
1777            // If a module is in the middle of capture, it should
1778            // consume the key event.
1779            if (mCurrentModule.onKeyUp(keyCode, event)) {
1780                return true;
1781            } else if (keyCode == KeyEvent.KEYCODE_MENU
1782                    || keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
1783                // Let the mode list view consume the event.
1784                mCameraAppUI.openModeList();
1785                return true;
1786            } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
1787                mCameraAppUI.showFilmstrip();
1788                return true;
1789            }
1790        } else {
1791            if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
1792                mFilmstripController.goToNextItem();
1793                return true;
1794            } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
1795                boolean wentToPrevious = mFilmstripController.goToPreviousItem();
1796                if (!wentToPrevious) {
1797                  // at beginning of filmstrip, hide and go back to preview
1798                  mCameraAppUI.hideFilmstrip();
1799                }
1800                return true;
1801            }
1802        }
1803        return super.onKeyUp(keyCode, event);
1804    }
1805
1806    @Override
1807    public void onBackPressed() {
1808        if (!mCameraAppUI.onBackPressed()) {
1809            if (!mCurrentModule.onBackPressed()) {
1810                super.onBackPressed();
1811            }
1812        }
1813    }
1814
1815    @Override
1816    public boolean isAutoRotateScreen() {
1817        // TODO: Move to OrientationManager.
1818        return mAutoRotateScreen;
1819    }
1820
1821    @Override
1822    public boolean onCreateOptionsMenu(Menu menu) {
1823        MenuInflater inflater = getMenuInflater();
1824        inflater.inflate(R.menu.filmstrip_menu, menu);
1825        mActionBarMenu = menu;
1826        return super.onCreateOptionsMenu(menu);
1827    }
1828
1829    protected long getStorageSpaceBytes() {
1830        synchronized (mStorageSpaceLock) {
1831            return mStorageSpaceBytes;
1832        }
1833    }
1834
1835    protected interface OnStorageUpdateDoneListener {
1836        public void onStorageUpdateDone(long bytes);
1837    }
1838
1839    protected void updateStorageSpaceAndHint(final OnStorageUpdateDoneListener callback) {
1840        /*
1841         * We execute disk operations on a background thread in order to
1842         * free up the UI thread.  Synchronizing on the lock below ensures
1843         * that when getStorageSpaceBytes is called, the main thread waits
1844         * until this method has completed.
1845         *
1846         * However, .execute() does not ensure this execution block will be
1847         * run right away (.execute() schedules this AsyncTask for sometime
1848         * in the future. executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
1849         * tries to execute the task in parellel with other AsyncTasks, but
1850         * there's still no guarantee).
1851         * e.g. don't call this then immediately call getStorageSpaceBytes().
1852         * Instead, pass in an OnStorageUpdateDoneListener.
1853         */
1854        (new AsyncTask<Void, Void, Long>() {
1855            @Override
1856            protected Long doInBackground(Void ... arg) {
1857                synchronized (mStorageSpaceLock) {
1858                    mStorageSpaceBytes = Storage.getAvailableSpace();
1859                    return mStorageSpaceBytes;
1860                }
1861            }
1862
1863            @Override
1864            protected void onPostExecute(Long bytes) {
1865                updateStorageHint(bytes);
1866                if (callback != null) {
1867                    callback.onStorageUpdateDone(bytes);
1868                }
1869            }
1870        }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
1871    }
1872
1873    protected void updateStorageHint(long storageSpace) {
1874        String message = null;
1875        if (storageSpace == Storage.UNAVAILABLE) {
1876            message = getString(R.string.no_storage);
1877        } else if (storageSpace == Storage.PREPARING) {
1878            message = getString(R.string.preparing_sd);
1879        } else if (storageSpace == Storage.UNKNOWN_SIZE) {
1880            message = getString(R.string.access_sd_fail);
1881        } else if (storageSpace <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
1882            message = getString(R.string.spaceIsLow_content);
1883        }
1884
1885        if (message != null) {
1886            Log.w(TAG, "Storage warning: " + message);
1887            if (mStorageHint == null) {
1888                mStorageHint = OnScreenHint.makeText(CameraActivity.this, message);
1889            } else {
1890                mStorageHint.setText(message);
1891            }
1892            mStorageHint.show();
1893            UsageStatistics.instance().storageWarning(storageSpace);
1894        } else if (mStorageHint != null) {
1895            mStorageHint.cancel();
1896            mStorageHint = null;
1897        }
1898    }
1899
1900    protected void setResultEx(int resultCode) {
1901        mResultCodeForTesting = resultCode;
1902        setResult(resultCode);
1903    }
1904
1905    protected void setResultEx(int resultCode, Intent data) {
1906        mResultCodeForTesting = resultCode;
1907        mResultDataForTesting = data;
1908        setResult(resultCode, data);
1909    }
1910
1911    public int getResultCode() {
1912        return mResultCodeForTesting;
1913    }
1914
1915    public Intent getResultData() {
1916        return mResultDataForTesting;
1917    }
1918
1919    public boolean isSecureCamera() {
1920        return mSecureCamera;
1921    }
1922
1923    @Override
1924    public boolean isPaused() {
1925        return mPaused;
1926    }
1927
1928    @Override
1929    public int getPreferredChildModeIndex(int modeIndex) {
1930        if (modeIndex == getResources().getInteger(R.integer.camera_mode_photo)) {
1931            boolean hdrPlusOn = mSettingsManager.isHdrPlusOn();
1932            if (hdrPlusOn && GcamHelper.hasGcamCapture()) {
1933                modeIndex = getResources().getInteger(R.integer.camera_mode_gcam);
1934            }
1935        }
1936        return modeIndex;
1937    }
1938
1939    @Override
1940    public void onModeSelected(int modeIndex) {
1941        if (mCurrentModeIndex == modeIndex) {
1942            return;
1943        }
1944
1945        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.MODE_SWITCH_START);
1946        // Record last used camera mode for quick switching
1947        if (modeIndex == getResources().getInteger(R.integer.camera_mode_photo)
1948                || modeIndex == getResources().getInteger(R.integer.camera_mode_gcam)) {
1949            mSettingsManager.setInt(SettingsManager.SETTING_KEY_CAMERA_MODULE_LAST_USED_INDEX,
1950                    modeIndex);
1951        }
1952
1953        closeModule(mCurrentModule);
1954
1955        // Select the correct module index from the mode switcher index.
1956        modeIndex = getPreferredChildModeIndex(modeIndex);
1957        setModuleFromModeIndex(modeIndex);
1958
1959        mCameraAppUI.resetBottomControls(mCurrentModule, modeIndex);
1960        mCameraAppUI.addShutterListener(mCurrentModule);
1961        mCameraAppUI.hideLetterboxing();
1962        openModule(mCurrentModule);
1963        mCurrentModule.onOrientationChanged(mLastRawOrientation);
1964        // Store the module index so we can use it the next time the Camera
1965        // starts up.
1966        mSettingsManager.setInt(SettingsManager.SETTING_STARTUP_MODULE_INDEX, modeIndex);
1967    }
1968
1969    /**
1970     * Shows the settings dialog.
1971     */
1972    @Override
1973    public void onSettingsSelected() {
1974        UsageStatistics.instance().controlUsed(
1975                eventprotos.ControlEvent.ControlType.OVERALL_SETTINGS);
1976        Intent intent = new Intent(this, CameraSettingsActivity.class);
1977        startActivity(intent);
1978    }
1979
1980    @Override
1981    public void freezeScreenUntilPreviewReady() {
1982        mCameraAppUI.freezeScreenUntilPreviewReady();
1983    }
1984
1985    /**
1986     * Sets the mCurrentModuleIndex, creates a new module instance for the given
1987     * index an sets it as mCurrentModule.
1988     */
1989    private void setModuleFromModeIndex(int modeIndex) {
1990        ModuleManagerImpl.ModuleAgent agent = mModuleManager.getModuleAgent(modeIndex);
1991        if (agent == null) {
1992            return;
1993        }
1994        if (!agent.requestAppForCamera()) {
1995            mCameraController.closeCamera(true);
1996        }
1997        mCurrentModeIndex = agent.getModuleId();
1998        mCurrentModule = (CameraModule) agent.createModule(this);
1999    }
2000
2001    @Override
2002    public SettingsManager getSettingsManager() {
2003        return mSettingsManager;
2004    }
2005
2006    @Override
2007    public CameraServices getServices() {
2008        return (CameraServices) getApplication();
2009    }
2010
2011    public List<String> getSupportedModeNames() {
2012        List<Integer> indices = mModuleManager.getSupportedModeIndexList();
2013        List<String> supported = new ArrayList<String>();
2014
2015        for (Integer modeIndex : indices) {
2016            String name = CameraUtil.getCameraModeText(modeIndex, mAppContext);
2017            if (name != null && !name.equals("")) {
2018                supported.add(name);
2019            }
2020        }
2021        return supported;
2022    }
2023
2024    @Override
2025    public ButtonManager getButtonManager() {
2026        if (mButtonManager == null) {
2027            mButtonManager = new ButtonManager(this);
2028        }
2029        return mButtonManager;
2030    }
2031
2032    /**
2033     * Creates an AlertDialog appropriate for choosing whether to enable
2034     * location on the first run of the app.
2035     */
2036    public AlertDialog getFirstTimeLocationAlert() {
2037        AlertDialog.Builder builder = new AlertDialog.Builder(this);
2038        builder = SettingsUtil.getFirstTimeLocationAlertBuilder(builder, new Callback<Boolean>() {
2039            @Override
2040            public void onCallback(Boolean locationOn) {
2041                mSettingsManager.setLocation(locationOn, mLocationManager);
2042            }
2043        });
2044        if (builder != null) {
2045            return builder.create();
2046        } else {
2047            return null;
2048        }
2049    }
2050
2051    /**
2052     * Launches an ACTION_EDIT intent for the given local data item. If
2053     * 'withTinyPlanet' is set, this will show a disambig dialog first to let
2054     * the user start either the tiny planet editor or another photo edior.
2055     *
2056     * @param data The data item to edit.
2057     */
2058    public void launchEditor(LocalData data) {
2059        Intent intent = new Intent(Intent.ACTION_EDIT)
2060                .setDataAndType(data.getUri(), data.getMimeType())
2061                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
2062        try {
2063            launchActivityByIntent(intent);
2064        } catch (ActivityNotFoundException e) {
2065            final String msgEditWith = getResources().getString(R.string.edit_with);
2066            launchActivityByIntent(Intent.createChooser(intent, msgEditWith));
2067        }
2068    }
2069
2070    @Override
2071    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
2072        super.onCreateContextMenu(menu, v, menuInfo);
2073
2074        MenuInflater inflater = getMenuInflater();
2075        inflater.inflate(R.menu.filmstrip_context_menu, menu);
2076    }
2077
2078    @Override
2079    public boolean onContextItemSelected(MenuItem item) {
2080        switch (item.getItemId()) {
2081            case R.id.tiny_planet_editor:
2082                mMyFilmstripBottomControlListener.onTinyPlanet();
2083                return true;
2084            case R.id.photo_editor:
2085                mMyFilmstripBottomControlListener.onEdit();
2086                return true;
2087        }
2088        return false;
2089    }
2090
2091    /**
2092     * Launch the tiny planet editor.
2093     *
2094     * @param data The data must be a 360 degree stereographically mapped
2095     *            panoramic image. It will not be modified, instead a new item
2096     *            with the result will be added to the filmstrip.
2097     */
2098    public void launchTinyPlanetEditor(LocalData data) {
2099        TinyPlanetFragment fragment = new TinyPlanetFragment();
2100        Bundle bundle = new Bundle();
2101        bundle.putString(TinyPlanetFragment.ARGUMENT_URI, data.getUri().toString());
2102        bundle.putString(TinyPlanetFragment.ARGUMENT_TITLE, data.getTitle());
2103        fragment.setArguments(bundle);
2104        fragment.show(getFragmentManager(), "tiny_planet");
2105    }
2106
2107    /**
2108     * Returns what UI mode (capture mode or filmstrip) we are in.
2109     * Returned number one of {@link com.google.common.logging.eventprotos.NavigationChange.Mode}
2110     */
2111    private int currentUserInterfaceMode() {
2112        int mode = NavigationChange.Mode.UNKNOWN_MODE;
2113        if (mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_photo)) {
2114            mode = NavigationChange.Mode.PHOTO_CAPTURE;
2115        }
2116        if (mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_video)) {
2117            mode = NavigationChange.Mode.VIDEO_CAPTURE;
2118        }
2119        if (mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_refocus)) {
2120            mode = NavigationChange.Mode.LENS_BLUR;
2121        }
2122        if (mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_gcam)) {
2123            mode = NavigationChange.Mode.HDR_PLUS;
2124        }
2125        if (mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_photosphere)) {
2126            mode = NavigationChange.Mode.PHOTO_SPHERE;
2127        }
2128        if (mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_panorama)) {
2129            mode = NavigationChange.Mode.PANORAMA;
2130        }
2131        if (mFilmstripVisible) {
2132            mode = NavigationChange.Mode.FILMSTRIP;
2133        }
2134        return mode;
2135    }
2136
2137    private void openModule(CameraModule module) {
2138        module.init(this, isSecureCamera(), isCaptureIntent());
2139        module.hardResetSettings(mSettingsManager);
2140        module.resume();
2141        UsageStatistics.instance().changeScreen(currentUserInterfaceMode(),
2142                NavigationChange.InteractionCause.BUTTON);
2143        updatePreviewVisibility();
2144    }
2145
2146    private void closeModule(CameraModule module) {
2147        module.pause();
2148        mCameraAppUI.clearModuleUI();
2149    }
2150
2151    private void performDeletion() {
2152        if (!mPendingDeletion) {
2153            return;
2154        }
2155        hideUndoDeletionBar(false);
2156        mDataAdapter.executeDeletion();
2157    }
2158
2159    public void showUndoDeletionBar() {
2160        if (mPendingDeletion) {
2161            performDeletion();
2162        }
2163        Log.v(TAG, "showing undo bar");
2164        mPendingDeletion = true;
2165        if (mUndoDeletionBar == null) {
2166            ViewGroup v = (ViewGroup) getLayoutInflater().inflate(R.layout.undo_bar,
2167                    mAboveFilmstripControlLayout, true);
2168            mUndoDeletionBar = (ViewGroup) v.findViewById(R.id.camera_undo_deletion_bar);
2169            View button = mUndoDeletionBar.findViewById(R.id.camera_undo_deletion_button);
2170            button.setOnClickListener(new View.OnClickListener() {
2171                @Override
2172                public void onClick(View view) {
2173                    mDataAdapter.undoDataRemoval();
2174                    hideUndoDeletionBar(true);
2175                }
2176            });
2177            // Setting undo bar clickable to avoid touch events going through
2178            // the bar to the buttons (eg. edit button, etc) underneath the bar.
2179            mUndoDeletionBar.setClickable(true);
2180            // When there is user interaction going on with the undo button, we
2181            // do not want to hide the undo bar.
2182            button.setOnTouchListener(new View.OnTouchListener() {
2183                @Override
2184                public boolean onTouch(View v, MotionEvent event) {
2185                    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
2186                        mIsUndoingDeletion = true;
2187                    } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
2188                        mIsUndoingDeletion = false;
2189                    }
2190                    return false;
2191                }
2192            });
2193        }
2194        mUndoDeletionBar.setAlpha(0f);
2195        mUndoDeletionBar.setVisibility(View.VISIBLE);
2196        mUndoDeletionBar.animate().setDuration(200).alpha(1f).setListener(null).start();
2197    }
2198
2199    private void hideUndoDeletionBar(boolean withAnimation) {
2200        Log.v(TAG, "Hiding undo deletion bar");
2201        mPendingDeletion = false;
2202        if (mUndoDeletionBar != null) {
2203            if (withAnimation) {
2204                mUndoDeletionBar.animate().setDuration(200).alpha(0f)
2205                        .setListener(new Animator.AnimatorListener() {
2206                            @Override
2207                            public void onAnimationStart(Animator animation) {
2208                                // Do nothing.
2209                            }
2210
2211                            @Override
2212                            public void onAnimationEnd(Animator animation) {
2213                                mUndoDeletionBar.setVisibility(View.GONE);
2214                            }
2215
2216                            @Override
2217                            public void onAnimationCancel(Animator animation) {
2218                                // Do nothing.
2219                            }
2220
2221                            @Override
2222                            public void onAnimationRepeat(Animator animation) {
2223                                // Do nothing.
2224                            }
2225                        }).start();
2226            } else {
2227                mUndoDeletionBar.setVisibility(View.GONE);
2228            }
2229        }
2230    }
2231
2232    @Override
2233    public void onOrientationChanged(int orientation) {
2234        // We keep the last known orientation. So if the user first orient
2235        // the camera then point the camera to floor or sky, we still have
2236        // the correct orientation.
2237        if (orientation == OrientationManager.ORIENTATION_UNKNOWN) {
2238            return;
2239        }
2240        mLastRawOrientation = orientation;
2241        if (mCurrentModule != null) {
2242            mCurrentModule.onOrientationChanged(orientation);
2243        }
2244    }
2245
2246    /**
2247     * Enable/disable swipe-to-filmstrip. Will always disable swipe if in
2248     * capture intent.
2249     *
2250     * @param enable {@code true} to enable swipe.
2251     */
2252    public void setSwipingEnabled(boolean enable) {
2253        // TODO: Bring back the functionality.
2254        if (isCaptureIntent()) {
2255            // lockPreview(true);
2256        } else {
2257            // lockPreview(!enable);
2258        }
2259    }
2260
2261    // Accessor methods for getting latency times used in performance testing
2262    public long getFirstPreviewTime() {
2263        if (mCurrentModule instanceof PhotoModule) {
2264            long coverHiddenTime = getCameraAppUI().getCoverHiddenTime();
2265            if (coverHiddenTime != -1) {
2266                return coverHiddenTime - mOnCreateTime;
2267            }
2268        }
2269        return -1;
2270    }
2271
2272    public long getAutoFocusTime() {
2273        return (mCurrentModule instanceof PhotoModule) ?
2274                ((PhotoModule) mCurrentModule).mAutoFocusTime : -1;
2275    }
2276
2277    public long getShutterLag() {
2278        return (mCurrentModule instanceof PhotoModule) ?
2279                ((PhotoModule) mCurrentModule).mShutterLag : -1;
2280    }
2281
2282    public long getShutterToPictureDisplayedTime() {
2283        return (mCurrentModule instanceof PhotoModule) ?
2284                ((PhotoModule) mCurrentModule).mShutterToPictureDisplayedTime : -1;
2285    }
2286
2287    public long getPictureDisplayedToJpegCallbackTime() {
2288        return (mCurrentModule instanceof PhotoModule) ?
2289                ((PhotoModule) mCurrentModule).mPictureDisplayedToJpegCallbackTime : -1;
2290    }
2291
2292    public long getJpegCallbackFinishTime() {
2293        return (mCurrentModule instanceof PhotoModule) ?
2294                ((PhotoModule) mCurrentModule).mJpegCallbackFinishTime : -1;
2295    }
2296
2297    public long getCaptureStartTime() {
2298        return (mCurrentModule instanceof PhotoModule) ?
2299                ((PhotoModule) mCurrentModule).mCaptureStartTime : -1;
2300    }
2301
2302    public boolean isRecording() {
2303        return (mCurrentModule instanceof VideoModule) ?
2304                ((VideoModule) mCurrentModule).isRecording() : false;
2305    }
2306
2307    public CameraManager.CameraOpenCallback getCameraOpenErrorCallback() {
2308        return mCameraController;
2309    }
2310
2311    // For debugging purposes only.
2312    public CameraModule getCurrentModule() {
2313        return mCurrentModule;
2314    }
2315
2316    @Override
2317    public void showTutorial(AbstractTutorialOverlay tutorial) {
2318        mCameraAppUI.showTutorial(tutorial, getLayoutInflater());
2319    }
2320
2321    /**
2322     * Reads the current location recording settings and passes it on to the
2323     * location manager.
2324     */
2325    public void syncLocationManagerSetting() {
2326        mSettingsManager.syncLocationManager(mLocationManager);
2327    }
2328
2329    private void keepScreenOnForAWhile() {
2330        if (mKeepScreenOn) {
2331            return;
2332        }
2333        mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
2334        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2335        mMainHandler.sendEmptyMessageDelayed(MSG_CLEAR_SCREEN_ON_FLAG, SCREEN_DELAY_MS);
2336    }
2337
2338    private void resetScreenOn() {
2339        mKeepScreenOn = false;
2340        mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
2341        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2342    }
2343
2344    /**
2345     * @return {@code true} if the Gallery is launched successfully.
2346     */
2347    private boolean startGallery() {
2348        if (mGalleryIntent == null) {
2349            return false;
2350        }
2351        try {
2352            UsageStatistics.instance().changeScreen(NavigationChange.Mode.GALLERY,
2353                    NavigationChange.InteractionCause.BUTTON);
2354            Intent startGalleryIntent = new Intent(mGalleryIntent);
2355            int currentDataId = mFilmstripController.getCurrentId();
2356            LocalData currentLocalData = mDataAdapter.getLocalData(currentDataId);
2357            if (currentLocalData != null) {
2358                GalleryHelper.setContentUri(startGalleryIntent, currentLocalData.getUri());
2359            }
2360            launchActivityByIntent(startGalleryIntent);
2361        } catch (ActivityNotFoundException e) {
2362            Log.w(TAG, "Failed to launch gallery activity, closing");
2363        }
2364        return false;
2365    }
2366
2367    private void setNfcBeamPushUriFromData(LocalData data) {
2368        final Uri uri = data.getUri();
2369        if (uri != Uri.EMPTY) {
2370            mNfcPushUris[0] = uri;
2371        } else {
2372            mNfcPushUris[0] = null;
2373        }
2374    }
2375
2376    /**
2377     * Updates the visibility of the filmstrip bottom controls and action bar.
2378     */
2379    private void updateUiByData(final int dataId) {
2380        final LocalData currentData = mDataAdapter.getLocalData(dataId);
2381        if (currentData == null) {
2382            Log.w(TAG, "Current data ID not found.");
2383            hideSessionProgress();
2384            return;
2385        }
2386        updateActionBarMenu(currentData);
2387
2388        /* Bottom controls. */
2389        updateBottomControlsByData(currentData);
2390
2391        if (isSecureCamera()) {
2392            // We cannot show buttons in secure camera since go to other
2393            // activities might create a security hole.
2394            mCameraAppUI.getFilmstripBottomControls().hideControls();
2395            return;
2396        }
2397
2398
2399        setNfcBeamPushUriFromData(currentData);
2400
2401        if (!mDataAdapter.isMetadataUpdated(dataId)) {
2402            mDataAdapter.updateMetadata(dataId);
2403        }
2404    }
2405
2406    /**
2407     * Updates the bottom controls based on the data.
2408     */
2409    private void updateBottomControlsByData(final LocalData currentData) {
2410
2411        final CameraAppUI.BottomPanel filmstripBottomPanel =
2412                mCameraAppUI.getFilmstripBottomControls();
2413        filmstripBottomPanel.showControls();
2414        filmstripBottomPanel.setEditButtonVisibility(
2415                currentData.isDataActionSupported(LocalData.DATA_ACTION_EDIT));
2416        filmstripBottomPanel.setShareButtonVisibility(
2417                currentData.isDataActionSupported(LocalData.DATA_ACTION_SHARE));
2418        filmstripBottomPanel.setDeleteButtonVisibility(
2419                currentData.isDataActionSupported(LocalData.DATA_ACTION_DELETE));
2420
2421        /* Progress bar */
2422
2423        Uri contentUri = currentData.getUri();
2424        CaptureSessionManager sessionManager = getServices()
2425                .getCaptureSessionManager();
2426
2427        if (sessionManager.hasErrorMessage(contentUri)) {
2428            showProcessError(sessionManager.getErrorMesage(contentUri));
2429        } else {
2430            filmstripBottomPanel.hideProgressError();
2431            CaptureSession session = sessionManager.getSession(contentUri);
2432
2433            if (session != null) {
2434                int sessionProgress = session.getProgress();
2435
2436                if (sessionProgress < 0) {
2437                    hideSessionProgress();
2438                } else {
2439                    CharSequence progressMessage = session.getProgressMessage();
2440                    showSessionProgress(progressMessage);
2441                    updateSessionProgress(sessionProgress);
2442                }
2443            } else {
2444                hideSessionProgress();
2445            }
2446        }
2447
2448        /* View button */
2449
2450        // We need to add this to a separate DB.
2451        final int viewButtonVisibility;
2452        if (PanoramaMetadataLoader.isPanoramaAndUseViewer(currentData)) {
2453            viewButtonVisibility = CameraAppUI.BottomPanel.VIEWER_PHOTO_SPHERE;
2454        } else if (RgbzMetadataLoader.hasRGBZData(currentData)) {
2455            viewButtonVisibility = CameraAppUI.BottomPanel.VIEWER_REFOCUS;
2456        } else {
2457            viewButtonVisibility = CameraAppUI.BottomPanel.VIEWER_NONE;
2458        }
2459
2460        filmstripBottomPanel.setTinyPlanetEnabled(
2461                PanoramaMetadataLoader.isPanorama360(currentData));
2462        filmstripBottomPanel.setViewerButtonVisibility(viewButtonVisibility);
2463    }
2464
2465    private class PeekAnimationHandler extends Handler {
2466        private class DataAndCallback {
2467            LocalData mData;
2468            com.android.camera.util.Callback<Bitmap> mCallback;
2469
2470            public DataAndCallback(LocalData data, com.android.camera.util.Callback<Bitmap>
2471                    callback) {
2472                mData = data;
2473                mCallback = callback;
2474            }
2475        }
2476
2477        public PeekAnimationHandler(Looper looper) {
2478            super(looper);
2479        }
2480
2481        /**
2482         * Starts the animation decoding job and posts a {@code Runnable} back
2483         * when when the decoding is done.
2484         *
2485         * @param data The data item to decode the thumbnail for.
2486         * @param callback {@link com.android.camera.util.Callback} after the
2487         *                 decoding is done.
2488         */
2489        public void startDecodingJob(final LocalData data,
2490                final com.android.camera.util.Callback<Bitmap> callback) {
2491            PeekAnimationHandler.this.obtainMessage(0 /** dummy integer **/,
2492                    new DataAndCallback(data, callback)).sendToTarget();
2493        }
2494
2495        @Override
2496        public void handleMessage(Message msg) {
2497            final LocalData data = ((DataAndCallback) msg.obj).mData;
2498            final com.android.camera.util.Callback<Bitmap> callback =
2499                    ((DataAndCallback) msg.obj).mCallback;
2500            if (data == null || callback == null) {
2501                return;
2502            }
2503
2504            final Bitmap bitmap;
2505            switch (data.getLocalDataType()) {
2506                case LocalData.LOCAL_IN_PROGRESS_DATA:
2507                    byte[] jpegData = Storage.getJpegForSession(data.getUri());
2508                    if (jpegData != null) {
2509                        bitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
2510                    } else {
2511                        bitmap = null;
2512                    }
2513                    break;
2514
2515                case LocalData.LOCAL_IMAGE:
2516                    FileInputStream stream;
2517                    try {
2518                        stream = new FileInputStream(data.getPath());
2519                    } catch (FileNotFoundException e) {
2520                        Log.e(TAG, "File not found:" + data.getPath());
2521                        return;
2522                    }
2523                    Point dim = CameraUtil.resizeToFill(data.getWidth(), data.getHeight(),
2524                            data.getRotation(), mAboveFilmstripControlLayout.getWidth(),
2525                            mAboveFilmstripControlLayout.getMeasuredHeight());
2526                    if (data.getRotation() % 180 != 0) {
2527                        int dummy = dim.x;
2528                        dim.x = dim.y;
2529                        dim.y = dummy;
2530                    }
2531                    bitmap = LocalDataUtil
2532                            .loadImageThumbnailFromStream(stream, data.getWidth(), data.getHeight(),
2533                                    (int) (dim.x * 0.7f), (int) (dim.y * 0.7),
2534                                    data.getRotation(), MAX_PEEK_BITMAP_PIXELS);
2535                    break;
2536
2537                case LocalData.LOCAL_VIDEO:
2538                    bitmap = LocalDataUtil.loadVideoThumbnail(data.getPath());
2539                    break;
2540
2541                default:
2542                    bitmap = null;
2543                    break;
2544            }
2545
2546            if (bitmap == null) {
2547                return;
2548            }
2549
2550            mMainHandler.post(new Runnable() {
2551                @Override
2552                public void run() {
2553                    callback.onCallback(bitmap);
2554                }
2555            });
2556        }
2557    }
2558
2559    private void showDetailsDialog(int dataId) {
2560        final LocalData data = mDataAdapter.getLocalData(dataId);
2561        if (data == null) {
2562            return;
2563        }
2564        MediaDetails details = data.getMediaDetails(getAndroidContext());
2565        if (details == null) {
2566            return;
2567        }
2568        Dialog detailDialog = DetailsDialog.create(CameraActivity.this, details);
2569        detailDialog.show();
2570        UsageStatistics.instance().mediaInteraction(
2571                fileNameFromDataID(dataId), MediaInteraction.InteractionType.DETAILS,
2572                NavigationChange.InteractionCause.BUTTON, fileAgeFromDataID(dataId));
2573    }
2574
2575    /**
2576     * Show or hide action bar items depending on current data type.
2577     */
2578    private void updateActionBarMenu(LocalData data) {
2579        if (mActionBarMenu == null) {
2580            return;
2581        }
2582
2583        MenuItem detailsMenuItem = mActionBarMenu.findItem(R.id.action_details);
2584        if (detailsMenuItem == null) {
2585            return;
2586        }
2587
2588        int type = data.getLocalDataType();
2589        boolean showDetails = (type == LocalData.LOCAL_IMAGE) || (type == LocalData.LOCAL_VIDEO);
2590        detailsMenuItem.setVisible(showDetails);
2591    }
2592}
2593