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