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