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