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