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