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