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