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