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