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