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