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