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