CameraActivity.java revision 4021c896985d9ba43db0199f259ce4b8bfc0dc88
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                    decrementPeekAnimPlayTimes();
541                    updateUiByData(mFilmstripController.getCurrentId());
542                }
543
544                @Override
545                public void onFocusedDataLongPressed(int dataId) {
546                    // Do nothing.
547                }
548
549                @Override
550                public void onFocusedDataPromoted(int dataID) {
551                    UsageStatistics.photoInteraction(
552                            UsageStatistics.hashFileName(fileNameFromDataID(dataID)),
553                            eventprotos.CameraEvent.InteractionType.DELETE,
554                            InteractionCause.SWIPE_UP);
555
556                    removeData(dataID);
557                }
558
559                @Override
560                public void onFocusedDataDemoted(int dataID) {
561                    UsageStatistics.photoInteraction(
562                            UsageStatistics.hashFileName(fileNameFromDataID(dataID)),
563                            eventprotos.CameraEvent.InteractionType.DELETE,
564                            InteractionCause.SWIPE_DOWN);
565
566                    removeData(dataID);
567                }
568
569                @Override
570                public void onEnterFullScreenUiShown(int dataId) {
571                    if (mFilmstripVisible) {
572                        CameraActivity.this.setFilmstripUiVisibility(true);
573                    }
574                }
575
576                @Override
577                public void onLeaveFullScreenUiShown(int dataId) {
578                    // Do nothing.
579                }
580
581                @Override
582                public void onEnterFullScreenUiHidden(int dataId) {
583                    if (mFilmstripVisible) {
584                        CameraActivity.this.setFilmstripUiVisibility(false);
585                    }
586                }
587
588                @Override
589                public void onLeaveFullScreenUiHidden(int dataId) {
590                    // Do nothing.
591                }
592
593                @Override
594                public void onEnterFilmstrip(int dataId) {
595                    if (mFilmstripVisible) {
596                        CameraActivity.this.setFilmstripUiVisibility(true);
597                    }
598                }
599
600                @Override
601                public void onLeaveFilmstrip(int dataId) {
602                    // Do nothing.
603                }
604
605                @Override
606                public void onDataReloaded() {
607                    if (!mFilmstripVisible) {
608                        return;
609                    }
610                    updateUiByData(mFilmstripController.getCurrentId());
611                }
612
613                @Override
614                public void onDataUpdated(int dataId) {
615                    if (!mFilmstripVisible) {
616                        return;
617                    }
618                    updateUiByData(mFilmstripController.getCurrentId());
619                }
620
621                @Override
622                public void onEnterZoomView(int dataID) {
623                    if (mFilmstripVisible) {
624                        CameraActivity.this.setFilmstripUiVisibility(false);
625                    }
626                }
627
628                @Override
629                public void onDataFocusChanged(final int prevDataId, final int newDataId) {
630                    if (!mFilmstripVisible) {
631                        return;
632                    }
633                    // TODO: This callback is UI event callback, should always
634                    // happen on UI thread. Find the reason for this
635                    // runOnUiThread() and fix it.
636                    runOnUiThread(new Runnable() {
637                        @Override
638                        public void run() {
639                            updateUiByData(newDataId);
640                        }
641                    });
642                }
643
644                @Override
645                public void onScroll(int firstVisiblePosition, int visibleItemCount, int totalItemCount) {
646                    mPreloader.onScroll(null /*absListView*/, firstVisiblePosition, visibleItemCount, totalItemCount);
647                }
648            };
649
650    private final LocalDataAdapter.LocalDataListener mLocalDataListener =
651            new LocalDataAdapter.LocalDataListener() {
652                @Override
653                public void onMetadataUpdated(List<Integer> updatedData) {
654                    int currentDataId = mFilmstripController.getCurrentId();
655                    for (Integer dataId : updatedData) {
656                        if (dataId == currentDataId) {
657                            updateBottomControlsByData(mDataAdapter.getLocalData(dataId));
658                        }
659                    }
660                }
661            };
662
663    public void gotoGallery() {
664        UsageStatistics.changeScreen(NavigationChange.Mode.FILMSTRIP,
665                InteractionCause.BUTTON);
666
667        mFilmstripController.goToNextItem();
668    }
669
670    /**
671     * If 'visible' is false, this hides the action bar and switches the
672     * filmstrip UI to lights-out mode.
673     *
674     * @param visible is false, this hides the action bar and switches the
675     *            filmstrip UI to lights-out mode.
676     */
677    private void setFilmstripUiVisibility(boolean visible) {
678        int currentSystemUIVisibility = mAboveFilmstripControlLayout.getSystemUiVisibility();
679        int newSystemUIVisibility = (visible ? View.SYSTEM_UI_FLAG_VISIBLE
680                : View.SYSTEM_UI_FLAG_FULLSCREEN);
681        if (newSystemUIVisibility != currentSystemUIVisibility) {
682            mAboveFilmstripControlLayout.setSystemUiVisibility(newSystemUIVisibility);
683        }
684
685        mCameraAppUI.getFilmstripBottomControls().setVisible(visible);
686        if (visible != mActionBar.isShowing()) {
687            if (visible) {
688                mActionBar.show();
689            } else {
690                mActionBar.hide();
691            }
692        }
693        mFilmstripCoversPreview = visible;
694        updatePreviewVisibility();
695    }
696
697    private void hideSessionProgress() {
698        mCameraAppUI.getFilmstripBottomControls().hideProgress();
699    }
700
701    private void showSessionProgress(CharSequence message) {
702        CameraAppUI.BottomPanel controls =  mCameraAppUI.getFilmstripBottomControls();
703        controls.setProgressText(message);
704        controls.hideControls();
705        controls.hideProgressError();
706        controls.showProgress();
707    }
708
709    private void showProcessError(CharSequence message) {
710        mCameraAppUI.getFilmstripBottomControls().showProgressError(message);
711    }
712
713    private void updateSessionProgress(int progress) {
714        mCameraAppUI.getFilmstripBottomControls().setProgress(progress);
715    }
716
717    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
718    private void setupNfcBeamPush() {
719        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mAppContext);
720        if (adapter == null) {
721            return;
722        }
723
724        if (!ApiHelper.HAS_SET_BEAM_PUSH_URIS) {
725            // Disable beaming
726            adapter.setNdefPushMessage(null, CameraActivity.this);
727            return;
728        }
729
730        adapter.setBeamPushUris(null, CameraActivity.this);
731        adapter.setBeamPushUrisCallback(new CreateBeamUrisCallback() {
732            @Override
733            public Uri[] createBeamUris(NfcEvent event) {
734                return mNfcPushUris;
735            }
736        }, CameraActivity.this);
737    }
738
739    @Override
740    public void onMenuVisibilityChanged(boolean isVisible) {
741        // TODO: Remove this or bring back the original implementation: cancel
742        // auto-hide actionbar.
743    }
744
745    @Override
746    public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent) {
747        int currentDataId = mFilmstripController.getCurrentId();
748        if (currentDataId < 0) {
749            return false;
750        }
751        UsageStatistics.photoInteraction(
752                UsageStatistics.hashFileName(fileNameFromDataID(currentDataId)),
753                eventprotos.CameraEvent.InteractionType.SHARE,
754                InteractionCause.BUTTON);
755        // TODO add intent.getComponent().getPackageName()
756        return true;
757    }
758
759    // Note: All callbacks come back on the main thread.
760    private final SessionListener mSessionListener =
761            new SessionListener() {
762                @Override
763                public void onSessionQueued(final Uri uri) {
764                    if (!Storage.isSessionUri(uri)) {
765                        return;
766                    }
767                    LocalSessionData newData = new LocalSessionData(uri);
768                    mDataAdapter.addData(newData);
769                }
770
771                @Override
772                public void onSessionDone(final Uri sessionUri) {
773                    Log.v(TAG, "onSessionDone:" + sessionUri);
774                    Uri contentUri = Storage.getContentUriForSessionUri(sessionUri);
775                    if (contentUri == null) {
776                        mDataAdapter.refresh(sessionUri);
777                        return;
778                    }
779                    LocalData newData = LocalMediaData.PhotoData.fromContentUri(
780                            getContentResolver(), contentUri);
781
782                    final int pos = mDataAdapter.findDataByContentUri(sessionUri);
783                    if (pos == -1) {
784                        // We do not have a placeholder for this image, perhaps due to the
785                        // activity crashing or being killed.
786                        mDataAdapter.addData(newData);
787                    }  else  {
788                        mDataAdapter.updateData(pos, newData);
789                    }
790                }
791
792                @Override
793                public void onSessionProgress(final Uri uri, final int progress) {
794                    if (progress < 0) {
795                        // Do nothing, there is no task for this URI.
796                        return;
797                    }
798                    int currentDataId = mFilmstripController.getCurrentId();
799                    if (currentDataId == -1) {
800                        return;
801                    }
802                    if (uri.equals(
803                            mDataAdapter.getLocalData(currentDataId).getUri())) {
804                        updateSessionProgress(progress);
805                    }
806                }
807
808                @Override
809                public void onSessionUpdated(Uri uri) {
810                    mDataAdapter.refresh(uri);
811                }
812
813                @Override
814                public void onSessionPreviewAvailable(Uri uri) {
815                    mDataAdapter.refresh(uri);
816                    int dataId = mDataAdapter.findDataByContentUri(uri);
817                    if (dataId != -1) {
818                        startPeekAnimation(mDataAdapter.getLocalData(dataId));
819                    }
820                }
821
822                @Override
823                public void onSessionFailed(Uri uri, CharSequence reason) {
824                    Log.v(TAG, "onSessionFailed:" + uri);
825
826                    int failedDataId = mDataAdapter.findDataByContentUri(uri);
827                    int currentDataId = mFilmstripController.getCurrentId();
828
829                    if (currentDataId == failedDataId) {
830                        updateSessionProgress(0);
831                        showProcessError(reason);
832                    }
833                    // HERE
834                    mDataAdapter.refresh(uri);
835                }
836            };
837
838    @Override
839    public Context getAndroidContext() {
840        return mAppContext;
841    }
842
843    @Override
844    public void launchActivityByIntent(Intent intent) {
845        startActivityForResult(intent, REQ_CODE_DONT_SWITCH_TO_PREVIEW);
846    }
847
848    @Override
849    public int getCurrentModuleIndex() {
850        return mCurrentModeIndex;
851    }
852
853    @Override
854    public ModuleController getCurrentModuleController() {
855        return mCurrentModule;
856    }
857
858    @Override
859    public int getQuickSwitchToModuleId(int currentModuleIndex) {
860        return mModuleManager.getQuickSwitchToModuleId(currentModuleIndex, mSettingsManager,
861                mAppContext);
862    }
863
864    @Override
865    public SurfaceTexture getPreviewBuffer() {
866        // TODO: implement this
867        return null;
868    }
869
870    @Override
871    public void onPreviewReadyToStart() {
872        mCameraAppUI.onPreviewReadyToStart();
873    }
874
875    @Override
876    public void onPreviewStarted() {
877        mCameraAppUI.onPreviewStarted();
878    }
879
880    @Override
881    public void addPreviewAreaSizeChangedListener(
882            PreviewStatusListener.PreviewAreaChangedListener listener) {
883        mCameraAppUI.addPreviewAreaChangedListener(listener);
884    }
885
886    @Override
887    public void removePreviewAreaSizeChangedListener(
888            PreviewStatusListener.PreviewAreaChangedListener listener) {
889        mCameraAppUI.removePreviewAreaChangedListener(listener);
890    }
891
892    @Override
893    public void setupOneShotPreviewListener() {
894        mCameraController.setOneShotPreviewCallback(mMainHandler,
895                new CameraManager.CameraPreviewDataCallback() {
896                    @Override
897                    public void onPreviewFrame(byte[] data, CameraManager.CameraProxy camera) {
898                        mCameraAppUI.onNewPreviewFrame();
899                    }
900                });
901    }
902
903    @Override
904    public void updatePreviewAspectRatio(float aspectRatio) {
905        mCameraAppUI.updatePreviewAspectRatio(aspectRatio);
906    }
907
908    @Override
909    public boolean shouldShowShimmy() {
910        int remainingTimes = mSettingsManager.getInt(
911                SettingsManager.SETTING_SHIMMY_REMAINING_PLAY_TIMES_INDEX);
912        return remainingTimes > 0;
913    }
914
915    @Override
916    public void decrementShimmyPlayTimes() {
917        int remainingTimes = mSettingsManager.getInt(
918                SettingsManager.SETTING_SHIMMY_REMAINING_PLAY_TIMES_INDEX) - 1;
919        if (remainingTimes >= 0) {
920            mSettingsManager.setInt(SettingsManager.SETTING_SHIMMY_REMAINING_PLAY_TIMES_INDEX,
921                    remainingTimes);
922        }
923    }
924
925    @Override
926    public void updatePreviewTransform(Matrix matrix) {
927        mCameraAppUI.updatePreviewTransform(matrix);
928    }
929
930    @Override
931    public void setPreviewStatusListener(PreviewStatusListener previewStatusListener) {
932        mCameraAppUI.setPreviewStatusListener(previewStatusListener);
933    }
934
935    @Override
936    public FrameLayout getModuleLayoutRoot() {
937        return mCameraAppUI.getModuleRootView();
938    }
939
940    @Override
941    public void setShutterEventsListener(ShutterEventsListener listener) {
942        // TODO: implement this
943    }
944
945    @Override
946    public void setShutterEnabled(boolean enabled) {
947        // TODO: implement this
948    }
949
950    @Override
951    public boolean isShutterEnabled() {
952        // TODO: implement this
953        return false;
954    }
955
956    @Override
957    public void startPreCaptureAnimation() {
958        mCameraAppUI.startPreCaptureAnimation();
959    }
960
961    @Override
962    public void cancelPreCaptureAnimation() {
963        // TODO: implement this
964    }
965
966    @Override
967    public void startPostCaptureAnimation() {
968        // TODO: implement this
969    }
970
971    @Override
972    public void startPostCaptureAnimation(Bitmap thumbnail) {
973        // TODO: implement this
974    }
975
976    @Override
977    public void cancelPostCaptureAnimation() {
978        // TODO: implement this
979    }
980
981    @Override
982    public OrientationManager getOrientationManager() {
983        return mOrientationManager;
984    }
985
986    @Override
987    public LocationManager getLocationManager() {
988        return mLocationManager;
989    }
990
991    @Override
992    public void lockOrientation() {
993        if (mOrientationManager != null) {
994            mOrientationManager.lockOrientation();
995        }
996    }
997
998    @Override
999    public void unlockOrientation() {
1000        if (mOrientationManager != null) {
1001            mOrientationManager.unlockOrientation();
1002        }
1003    }
1004
1005    /**
1006     * Decrement the remaining play times for peek animation.
1007     */
1008    private void decrementPeekAnimPlayTimes() {
1009        int remainingTimes = mSettingsManager.getInt(
1010                SettingsManager.SETTING_FILMSTRIP_PEEK_ANIM_REMAINING_PLAY_TIMES_INDEX) - 1;
1011        if (remainingTimes < 0) {
1012            return;
1013        }
1014        mSettingsManager
1015                .setInt(SettingsManager.SETTING_FILMSTRIP_PEEK_ANIM_REMAINING_PLAY_TIMES_INDEX,
1016                        remainingTimes);
1017    }
1018
1019    /**
1020     * Starts the filmstrip peek animation if the filmstrip is not visible.
1021     * Only {@link LocalData#LOCAL_IMAGE}, {@link
1022     * LocalData#LOCAL_IN_PROGRESS_DATA} and {@link
1023     * LocalData#LOCAL_VIDEO} are supported.
1024     *
1025     * @param data The data to peek.
1026     */
1027    private void startPeekAnimation(final LocalData data) {
1028        if (mFilmstripVisible || mPeekAnimationHandler == null) {
1029            return;
1030        }
1031
1032        int dataType = data.getLocalDataType();
1033        if (dataType != LocalData.LOCAL_IMAGE && dataType != LocalData.LOCAL_IN_PROGRESS_DATA &&
1034                dataType != LocalData.LOCAL_VIDEO) {
1035            return;
1036        }
1037
1038        int remainingTimes = mSettingsManager.getInt(
1039                SettingsManager.SETTING_FILMSTRIP_PEEK_ANIM_REMAINING_PLAY_TIMES_INDEX);
1040        if (remainingTimes <= 0) {
1041            return;
1042        }
1043        mPeekAnimationHandler.startDecodingJob(data, new Callback<Bitmap>() {
1044            @Override
1045            public void onCallback(Bitmap result) {
1046                mCameraAppUI.startPeekAnimation(result, true);
1047            }
1048        });
1049    }
1050
1051    @Override
1052    public void notifyNewMedia(Uri uri) {
1053        ContentResolver cr = getContentResolver();
1054        String mimeType = cr.getType(uri);
1055        if (LocalDataUtil.isMimeTypeVideo(mimeType)) {
1056            sendBroadcast(new Intent(CameraUtil.ACTION_NEW_VIDEO, uri));
1057            LocalData newData = LocalMediaData.VideoData.fromContentUri(getContentResolver(), uri);
1058            if (newData == null) {
1059                Log.e(TAG, "Can't find video data in content resolver:" + uri);
1060                return;
1061            }
1062            if (mDataAdapter.addData(newData)) {
1063                startPeekAnimation(newData);
1064            }
1065        } else if (LocalDataUtil.isMimeTypeImage(mimeType)) {
1066            CameraUtil.broadcastNewPicture(mAppContext, uri);
1067            LocalData newData = LocalMediaData.PhotoData.fromContentUri(getContentResolver(), uri);
1068            if (newData == null) {
1069                Log.e(TAG, "Can't find photo data in content resolver:" + uri);
1070                return;
1071            }
1072            if (mDataAdapter.addData(newData)) {
1073                startPeekAnimation(newData);
1074            }
1075        } else {
1076            android.util.Log.w(TAG, "Unknown new media with MIME type:" + mimeType + ", uri:" + uri);
1077        }
1078    }
1079
1080    @Override
1081    public void enableKeepScreenOn(boolean enabled) {
1082        if (mPaused) {
1083            return;
1084        }
1085
1086        mKeepScreenOn = enabled;
1087        if (mKeepScreenOn) {
1088            mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
1089            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1090        } else {
1091            keepScreenOnForAWhile();
1092        }
1093    }
1094
1095    @Override
1096    public CameraProvider getCameraProvider() {
1097        return mCameraController;
1098    }
1099
1100    private void removeData(int dataID) {
1101        mDataAdapter.removeData(dataID);
1102        if (mDataAdapter.getTotalNumber() > 1) {
1103            showUndoDeletionBar();
1104        } else {
1105            // If camera preview is the only view left in filmstrip,
1106            // no need to show undo bar.
1107            mPendingDeletion = true;
1108            performDeletion();
1109            if (mFilmstripVisible) {
1110                mCameraAppUI.getFilmstripContentPanel().animateHide();
1111            }
1112        }
1113    }
1114
1115    @Override
1116    public boolean onOptionsItemSelected(MenuItem item) {
1117        // Handle presses on the action bar items
1118        switch (item.getItemId()) {
1119            case android.R.id.home:
1120                if (mFilmstripVisible && startGallery()) {
1121                    return true;
1122                }
1123                onBackPressed();
1124                return true;
1125            case R.id.action_details:
1126                showDetailsDialog(mFilmstripController.getCurrentId());
1127                return true;
1128            default:
1129                return super.onOptionsItemSelected(item);
1130        }
1131    }
1132
1133    private boolean isCaptureIntent() {
1134        if (MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent().getAction())
1135                || MediaStore.ACTION_IMAGE_CAPTURE.equals(getIntent().getAction())
1136                || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(getIntent().getAction())) {
1137            return true;
1138        } else {
1139            return false;
1140        }
1141    }
1142
1143    private final SettingsManager.StrictUpgradeCallback mStrictUpgradeCallback
1144        = new SettingsManager.StrictUpgradeCallback() {
1145                @Override
1146                public void upgrade(SettingsManager settingsManager, int version) {
1147                    // Show the location dialog on upgrade if
1148                    //  (a) the user has never set this option (status quo).
1149                    //  (b) the user opt'ed out previously.
1150                    if (settingsManager.isSet(SettingsManager.SETTING_RECORD_LOCATION)) {
1151                        // Location is set in the source file defined for this setting.
1152                        // Remove the setting if the value is false to launch the dialog.
1153                        if (!settingsManager.getBoolean(SettingsManager.SETTING_RECORD_LOCATION)) {
1154                            settingsManager.remove(SettingsManager.SETTING_RECORD_LOCATION);
1155                        }
1156                    } else {
1157                        // Location is not set, check to see if we're upgrading from
1158                        // a different source file.
1159                        if (settingsManager.isSet(SettingsManager.SETTING_RECORD_LOCATION,
1160                                                  SettingsManager.SOURCE_GLOBAL)) {
1161                            boolean location = settingsManager.getBoolean(
1162                                SettingsManager.SETTING_RECORD_LOCATION,
1163                                SettingsManager.SOURCE_GLOBAL);
1164                            if (location) {
1165                                // Set the old setting only if the value is true, to prevent
1166                                // launching the dialog.
1167                                settingsManager.setBoolean(
1168                                    SettingsManager.SETTING_RECORD_LOCATION, location);
1169                            }
1170                        }
1171                    }
1172
1173                    settingsManager.remove(SettingsManager.SETTING_STARTUP_MODULE_INDEX);
1174                }
1175            };
1176
1177    private final CameraManager.CameraExceptionCallback mCameraDefaultExceptionCallback
1178        = new CameraManager.CameraExceptionCallback() {
1179                @Override
1180                public void onCameraException(RuntimeException e) {
1181                    Log.d(TAG, "Camera Exception", e);
1182                    CameraUtil.showErrorAndFinish(CameraActivity.this,
1183                            R.string.cannot_connect_camera);
1184                }
1185            };
1186
1187    @Override
1188    public void onCreate(Bundle state) {
1189        super.onCreate(state);
1190        final Glide glide = Glide.get();
1191        if (!glide.isImageManagerSet()) {
1192            // We load exclusively large images, so we want fewer threads to minimize jank.
1193            glide.setImageManager(new ImageManager.Builder(getApplicationContext())
1194                    .setResizeService(Executors.newSingleThreadExecutor()));
1195        }
1196        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.ACTIVITY_START);
1197        mOnCreateTime = System.currentTimeMillis();
1198        mAppContext = getApplicationContext();
1199        GcamHelper.init(getContentResolver());
1200
1201        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
1202        setContentView(R.layout.activity_main);
1203        mActionBar = getActionBar();
1204        mActionBar.addOnMenuVisibilityListener(this);
1205        mMainHandler = new MainHandler(this, getMainLooper());
1206        mCameraController =
1207                new CameraController(mAppContext, this, mMainHandler,
1208                        CameraManagerFactory.getAndroidCameraManager());
1209        mCameraController.setCameraDefaultExceptionCallback(mCameraDefaultExceptionCallback,
1210                mMainHandler);
1211
1212        mPreferences = new ComboPreferences(mAppContext);
1213
1214        mSettingsManager = new SettingsManager(mAppContext, this,
1215                mCameraController.getNumberOfCameras(), mStrictUpgradeCallback);
1216
1217        // Remove this after we get rid of ComboPreferences.
1218        int cameraId = Integer.parseInt(mSettingsManager.get(SettingsManager.SETTING_CAMERA_ID));
1219        mPreferences.setLocalId(mAppContext, cameraId);
1220        CameraSettings.upgradeGlobalPreferences(mPreferences,
1221                mCameraController.getNumberOfCameras());
1222        // TODO: Try to move all the resources allocation to happen as soon as
1223        // possible so we can call module.init() at the earliest time.
1224        mModuleManager = new ModuleManagerImpl();
1225        ModulesInfo.setupModules(mAppContext, mModuleManager);
1226
1227        mModeListView = (ModeListView) findViewById(R.id.mode_list_layout);
1228        mModeListView.init(mModuleManager.getSupportedModeIndexList());
1229        if (ApiHelper.HAS_ROTATION_ANIMATION) {
1230            setRotationAnimation();
1231        }
1232        mModeListView.setVisibilityChangedListener(new ModeListVisibilityChangedListener() {
1233            @Override
1234            public void onVisibilityChanged(boolean visible) {
1235                mModeListVisible = visible;
1236                updatePreviewVisibility();
1237            }
1238        });
1239
1240        // Check if this is in the secure camera mode.
1241        Intent intent = getIntent();
1242        String action = intent.getAction();
1243        if (INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action)
1244                || ACTION_IMAGE_CAPTURE_SECURE.equals(action)) {
1245            mSecureCamera = true;
1246        } else {
1247            mSecureCamera = intent.getBooleanExtra(SECURE_CAMERA_EXTRA, false);
1248        }
1249
1250        if (mSecureCamera) {
1251            // Foreground event caused by lock screen startup.
1252            // It is necessary to log this in onCreate, to avoid the
1253            // onResume->onPause->onResume sequence.
1254            UsageStatistics.foregrounded(
1255                    eventprotos.ForegroundEvent.ForegroundSource.LOCK_SCREEN);
1256
1257            // Change the window flags so that secure camera can show when
1258            // locked
1259            Window win = getWindow();
1260            WindowManager.LayoutParams params = win.getAttributes();
1261            params.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
1262            win.setAttributes(params);
1263
1264            // Filter for screen off so that we can finish secure camera
1265            // activity
1266            // when screen is off.
1267            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
1268            registerReceiver(mScreenOffReceiver, filter);
1269        }
1270        mCameraAppUI = new CameraAppUI(this,
1271                (MainActivityLayout) findViewById(R.id.activity_root_view), isCaptureIntent());
1272
1273        mCameraAppUI.setFilmstripBottomControlsListener(mMyFilmstripBottomControlListener);
1274
1275        mAboveFilmstripControlLayout =
1276                (FrameLayout) findViewById(R.id.camera_filmstrip_content_layout);
1277
1278        // Add the session listener so we can track the session progress
1279        // updates.
1280        getServices().getCaptureSessionManager().addSessionListener(mSessionListener);
1281        mFilmstripController = ((FilmstripView) findViewById(R.id.filmstrip_view)).getController();
1282        mFilmstripController.setImageGap(
1283                getResources().getDimensionPixelSize(R.dimen.camera_film_strip_gap));
1284        mPanoramaViewHelper = new PanoramaViewHelper(this);
1285        mPanoramaViewHelper.onCreate();
1286        // Set up the camera preview first so the preview shows up ASAP.
1287        mDataAdapter = new CameraDataAdapter(mAppContext, R.color.photo_placeholder);
1288        mDataAdapter.setLocalDataListener(mLocalDataListener);
1289
1290        mPreloader = new Preloader<Integer, AsyncTask>(FILMSTRIP_PRELOAD_AHEAD_ITEMS, mDataAdapter,
1291                mDataAdapter);
1292
1293        mCameraAppUI.getFilmstripContentPanel().setFilmstripListener(mFilmstripListener);
1294
1295        mLocationManager = new LocationManager(mAppContext);
1296
1297        int modeIndex = -1;
1298        int photoIndex = getResources().getInteger(R.integer.camera_mode_photo);
1299        int videoIndex = getResources().getInteger(R.integer.camera_mode_video);
1300        int gcamIndex = getResources().getInteger(R.integer.camera_mode_gcam);
1301        if (MediaStore.INTENT_ACTION_VIDEO_CAMERA.equals(getIntent().getAction())
1302                || MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent().getAction())) {
1303            modeIndex = videoIndex;
1304        } else if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(getIntent().getAction())
1305                || MediaStore.ACTION_IMAGE_CAPTURE.equals(getIntent().getAction())) {
1306            // TODO: synchronize mode options with photo module without losing
1307            // HDR+ preferences.
1308            modeIndex = photoIndex;
1309        } else if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(getIntent()
1310                        .getAction())
1311                || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(getIntent().getAction())) {
1312            modeIndex = mSettingsManager.getInt(
1313                SettingsManager.SETTING_KEY_CAMERA_MODULE_LAST_USED_INDEX);
1314        } else {
1315            // If the activity has not been started using an explicit intent,
1316            // read the module index from the last time the user changed modes
1317            modeIndex = mSettingsManager.getInt(SettingsManager.SETTING_STARTUP_MODULE_INDEX);
1318            if ((modeIndex == gcamIndex &&
1319                    !GcamHelper.hasGcamCapture()) || modeIndex < 0) {
1320                modeIndex = photoIndex;
1321            }
1322        }
1323
1324        mOrientationManager = new OrientationManagerImpl(this);
1325        mOrientationManager.addOnOrientationChangeListener(mMainHandler, this);
1326
1327        setModuleFromModeIndex(modeIndex);
1328        mCameraAppUI.prepareModuleUI();
1329        mCurrentModule.init(this, isSecureCamera(), isCaptureIntent());
1330
1331        if (!mSecureCamera) {
1332            mFilmstripController.setDataAdapter(mDataAdapter);
1333            if (!isCaptureIntent()) {
1334                mDataAdapter.requestLoad();
1335            }
1336        } else {
1337            // Put a lock placeholder as the last image by setting its date to
1338            // 0.
1339            ImageView v = (ImageView) getLayoutInflater().inflate(
1340                    R.layout.secure_album_placeholder, null);
1341            v.setOnClickListener(new View.OnClickListener() {
1342                @Override
1343                public void onClick(View view) {
1344                    UsageStatistics.changeScreen(NavigationChange.Mode.GALLERY,
1345                            InteractionCause.BUTTON);
1346                    startGallery();
1347                    finish();
1348                }
1349            });
1350            mDataAdapter = new FixedLastDataAdapter(
1351                    mAppContext,
1352                    mDataAdapter,
1353                    new SimpleViewData(
1354                            v,
1355                            LocalDataViewType.SECURE_ALBUM_PLACEHOLDER,
1356                            v.getDrawable().getIntrinsicWidth(),
1357                            v.getDrawable().getIntrinsicHeight(),
1358                            0, 0));
1359            // Flush out all the original data.
1360            mDataAdapter.flush();
1361            mFilmstripController.setDataAdapter(mDataAdapter);
1362        }
1363
1364        setupNfcBeamPush();
1365
1366        mLocalImagesObserver = new LocalMediaObserver();
1367        mLocalVideosObserver = new LocalMediaObserver();
1368
1369        getContentResolver().registerContentObserver(
1370                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true,
1371                mLocalImagesObserver);
1372        getContentResolver().registerContentObserver(
1373                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, true,
1374                mLocalVideosObserver);
1375        if (FeedbackHelper.feedbackAvailable()) {
1376            mFeedbackHelper = new FeedbackHelper(mAppContext);
1377        }
1378    }
1379
1380    /**
1381     * Call this whenever the mode drawer or filmstrip change the visibility
1382     * state.
1383     */
1384    private void updatePreviewVisibility() {
1385        if (mCurrentModule == null) {
1386            return;
1387        }
1388        int visibility;
1389        if (mFilmstripCoversPreview) {
1390            visibility = ModuleController.VISIBILITY_HIDDEN;
1391        } else if (mModeListVisible){
1392            visibility = ModuleController.VISIBILITY_COVERED;
1393        } else {
1394            visibility = ModuleController.VISIBILITY_VISIBLE;
1395        }
1396        mCurrentModule.onPreviewVisibilityChanged(visibility);
1397    }
1398
1399    private void setRotationAnimation() {
1400        int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE;
1401        rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
1402        Window win = getWindow();
1403        WindowManager.LayoutParams winParams = win.getAttributes();
1404        winParams.rotationAnimation = rotationAnimation;
1405        win.setAttributes(winParams);
1406    }
1407
1408    @Override
1409    public void onUserInteraction() {
1410        super.onUserInteraction();
1411        if (!isFinishing()) {
1412            keepScreenOnForAWhile();
1413        }
1414    }
1415
1416    @Override
1417    public boolean dispatchTouchEvent(MotionEvent ev) {
1418        boolean result = super.dispatchTouchEvent(ev);
1419        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
1420            // Real deletion is postponed until the next user interaction after
1421            // the gesture that triggers deletion. Until real deletion is
1422            // performed, users can click the undo button to bring back the
1423            // image that they chose to delete.
1424            if (mPendingDeletion && !mIsUndoingDeletion) {
1425                performDeletion();
1426            }
1427        }
1428        return result;
1429    }
1430
1431    @Override
1432    public void onPause() {
1433        mPaused = true;
1434        mPeekAnimationHandler = null;
1435        mPeekAnimationThread.quitSafely();
1436        mPeekAnimationThread = null;
1437        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.ACTIVITY_PAUSE);
1438
1439        // Delete photos that are pending deletion
1440        performDeletion();
1441        mCurrentModule.pause();
1442        mOrientationManager.pause();
1443        // Close the camera and wait for the operation done.
1444        mCameraController.closeCamera();
1445        mPanoramaViewHelper.onPause();
1446
1447        mLocalImagesObserver.setForegroundChangeListener(null);
1448        mLocalImagesObserver.setActivityPaused(true);
1449        mLocalVideosObserver.setActivityPaused(true);
1450        mPreloader.cancelAllLoads();
1451        resetScreenOn();
1452        super.onPause();
1453    }
1454
1455    @Override
1456    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1457        if (requestCode == REQ_CODE_DONT_SWITCH_TO_PREVIEW) {
1458            mResetToPreviewOnResume = false;
1459        } else {
1460            super.onActivityResult(requestCode, resultCode, data);
1461        }
1462    }
1463
1464    @Override
1465    public void onResume() {
1466        mPaused = false;
1467        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.ACTIVITY_RESUME);
1468
1469        mLastLayoutOrientation = getResources().getConfiguration().orientation;
1470
1471        // TODO: Handle this in OrientationManager.
1472        // Auto-rotate off
1473        if (Settings.System.getInt(getContentResolver(),
1474                Settings.System.ACCELEROMETER_ROTATION, 0) == 0) {
1475            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
1476            mAutoRotateScreen = false;
1477        } else {
1478            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
1479            mAutoRotateScreen = true;
1480        }
1481
1482        if (isCaptureIntent()) {
1483            // Foreground event caused by photo or video capure intent.
1484            UsageStatistics.foregrounded(
1485                    eventprotos.ForegroundEvent.ForegroundSource.INTENT_PICKER);
1486        } else if (!mSecureCamera) {
1487            // Foreground event that is not caused by an intent.
1488            UsageStatistics.foregrounded(
1489                    eventprotos.ForegroundEvent.ForegroundSource.ICON_LAUNCHER);
1490        }
1491
1492        Drawable galleryLogo;
1493        if (mSecureCamera) {
1494            mGalleryIntent = null;
1495            galleryLogo = null;
1496        } else {
1497            mGalleryIntent = IntentHelper.getDefaultGalleryIntent(mAppContext);
1498            galleryLogo = IntentHelper.getGalleryIcon(mAppContext, mGalleryIntent);
1499        }
1500        if (galleryLogo == null) {
1501            try {
1502                galleryLogo = getPackageManager().getActivityLogo(getComponentName());
1503            } catch (PackageManager.NameNotFoundException e) {
1504                Log.e(TAG, "Can't get the activity logo");
1505            }
1506        }
1507        if (mGalleryIntent != null) {
1508            mActionBar.setDisplayUseLogoEnabled(true);
1509        }
1510        mActionBar.setLogo(galleryLogo);
1511        mOrientationManager.resume();
1512        super.onResume();
1513        mPeekAnimationThread = new HandlerThread("Peek animation");
1514        mPeekAnimationThread.start();
1515        mPeekAnimationHandler = new PeekAnimationHandler(mPeekAnimationThread.getLooper());
1516        mCurrentModule.resume();
1517        setSwipingEnabled(true);
1518
1519        if (mResetToPreviewOnResume) {
1520            mCameraAppUI.resume();
1521        } else {
1522            LocalData data = mDataAdapter.getLocalData(mFilmstripController.getCurrentId());
1523            if (data != null) {
1524                mDataAdapter.refresh(data.getUri());
1525            }
1526        }
1527        // The share button might be disabled to avoid double tapping.
1528        mCameraAppUI.getFilmstripBottomControls().setShareEnabled(true);
1529        // Default is showing the preview, unless disabled by explicitly
1530        // starting an activity we want to return from to the filmstrip rather
1531        // than the preview.
1532        mResetToPreviewOnResume = true;
1533
1534        if (mLocalVideosObserver.isMediaDataChangedDuringPause()
1535                || mLocalImagesObserver.isMediaDataChangedDuringPause()) {
1536            if (!mSecureCamera) {
1537                // If it's secure camera, requestLoad() should not be called
1538                // as it will load all the data.
1539                if (!mFilmstripVisible) {
1540                    mDataAdapter.requestLoad();
1541                } else {
1542                    mDataAdapter.requestLoadNewPhotos();
1543                }
1544            }
1545        }
1546        mLocalImagesObserver.setActivityPaused(false);
1547        mLocalVideosObserver.setActivityPaused(false);
1548        if (!mSecureCamera) {
1549            mLocalImagesObserver.setForegroundChangeListener(
1550                    new LocalMediaObserver.ChangeListener() {
1551                @Override
1552                public void onChange() {
1553                    mDataAdapter.requestLoadNewPhotos();
1554                }
1555            });
1556        }
1557
1558        keepScreenOnForAWhile();
1559
1560        mPanoramaViewHelper.onResume();
1561        ReleaseDialogHelper.showReleaseInfoDialogOnStart(this, mSettingsManager);
1562        syncLocationManagerSetting();
1563    }
1564
1565    @Override
1566    public void onStart() {
1567        super.onStart();
1568        mPanoramaViewHelper.onStart();
1569        if (mResetToPreviewOnResume) {
1570            mCameraAppUI.resume();
1571            mResetToPreviewOnResume = false;
1572        }
1573    }
1574
1575    @Override
1576    protected void onStop() {
1577        mPanoramaViewHelper.onStop();
1578        if (mFeedbackHelper != null) {
1579            mFeedbackHelper.stopFeedback();
1580        }
1581
1582        mLocationManager.disconnect();
1583        super.onStop();
1584    }
1585
1586    @Override
1587    public void onDestroy() {
1588        if (mSecureCamera) {
1589            unregisterReceiver(mScreenOffReceiver);
1590        }
1591        mActionBar.removeOnMenuVisibilityListener(this);
1592        mSettingsManager.removeAllListeners();
1593        mCameraController.removeCallbackReceiver();
1594        getContentResolver().unregisterContentObserver(mLocalImagesObserver);
1595        getContentResolver().unregisterContentObserver(mLocalVideosObserver);
1596        getServices().getCaptureSessionManager().removeSessionListener(mSessionListener);
1597        mCameraAppUI.onDestroy();
1598        mCameraController = null;
1599        mSettingsManager = null;
1600        mCameraAppUI = null;
1601        mOrientationManager = null;
1602        mButtonManager = null;
1603        CameraManagerFactory.recycle();
1604        super.onDestroy();
1605    }
1606
1607    @Override
1608    public void onConfigurationChanged(Configuration config) {
1609        super.onConfigurationChanged(config);
1610        Log.v(TAG, "onConfigurationChanged");
1611        if (config.orientation == Configuration.ORIENTATION_UNDEFINED) {
1612            return;
1613        }
1614
1615        if (mLastLayoutOrientation != config.orientation) {
1616            mLastLayoutOrientation = config.orientation;
1617            mCurrentModule.onLayoutOrientationChanged(
1618                    mLastLayoutOrientation == Configuration.ORIENTATION_LANDSCAPE);
1619        }
1620    }
1621
1622    @Override
1623    public boolean onKeyDown(int keyCode, KeyEvent event) {
1624        if (!mFilmstripVisible) {
1625            if (mCurrentModule.onKeyDown(keyCode, event)) {
1626                return true;
1627            }
1628            // Prevent software keyboard or voice search from showing up.
1629            if (keyCode == KeyEvent.KEYCODE_SEARCH
1630                    || keyCode == KeyEvent.KEYCODE_MENU) {
1631                if (event.isLongPress()) {
1632                    return true;
1633                }
1634            }
1635        }
1636
1637        return super.onKeyDown(keyCode, event);
1638    }
1639
1640    @Override
1641    public boolean onKeyUp(int keyCode, KeyEvent event) {
1642        if (!mFilmstripVisible) {
1643            // If a module is in the middle of capture, it should
1644            // consume the key event.
1645            if (mCurrentModule.onKeyUp(keyCode, event)) {
1646                return true;
1647            } else if (keyCode == KeyEvent.KEYCODE_MENU) {
1648                // Let the mode list view consume the event.
1649                mModeListView.onMenuPressed();
1650                return true;
1651            }
1652        }
1653        return super.onKeyUp(keyCode, event);
1654    }
1655
1656    @Override
1657    public void onBackPressed() {
1658        if (!mCameraAppUI.onBackPressed()) {
1659            if (!mCurrentModule.onBackPressed()) {
1660                super.onBackPressed();
1661            }
1662        }
1663    }
1664
1665    @Override
1666    public boolean isAutoRotateScreen() {
1667        // TODO: Move to OrientationManager.
1668        return mAutoRotateScreen;
1669    }
1670
1671    @Override
1672    public boolean onCreateOptionsMenu(Menu menu) {
1673        MenuInflater inflater = getMenuInflater();
1674        inflater.inflate(R.menu.filmstrip_menu, menu);
1675        mActionBarMenu = menu;
1676        return super.onCreateOptionsMenu(menu);
1677    }
1678
1679    protected void updateStorageSpace() {
1680        mStorageSpaceBytes = Storage.getAvailableSpace();
1681    }
1682
1683    protected long getStorageSpaceBytes() {
1684        return mStorageSpaceBytes;
1685    }
1686
1687    protected void updateStorageSpaceAndHint() {
1688        updateStorageSpace();
1689        updateStorageHint(mStorageSpaceBytes);
1690    }
1691
1692    protected void updateStorageHint(long storageSpace) {
1693        String message = null;
1694        if (storageSpace == Storage.UNAVAILABLE) {
1695            message = getString(R.string.no_storage);
1696        } else if (storageSpace == Storage.PREPARING) {
1697            message = getString(R.string.preparing_sd);
1698        } else if (storageSpace == Storage.UNKNOWN_SIZE) {
1699            message = getString(R.string.access_sd_fail);
1700        } else if (storageSpace <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
1701            message = getString(R.string.spaceIsLow_content);
1702        }
1703
1704        if (message != null) {
1705            if (mStorageHint == null) {
1706                mStorageHint = OnScreenHint.makeText(mAppContext, message);
1707            } else {
1708                mStorageHint.setText(message);
1709            }
1710            mStorageHint.show();
1711        } else if (mStorageHint != null) {
1712            mStorageHint.cancel();
1713            mStorageHint = null;
1714        }
1715    }
1716
1717    protected void setResultEx(int resultCode) {
1718        mResultCodeForTesting = resultCode;
1719        setResult(resultCode);
1720    }
1721
1722    protected void setResultEx(int resultCode, Intent data) {
1723        mResultCodeForTesting = resultCode;
1724        mResultDataForTesting = data;
1725        setResult(resultCode, data);
1726    }
1727
1728    public int getResultCode() {
1729        return mResultCodeForTesting;
1730    }
1731
1732    public Intent getResultData() {
1733        return mResultDataForTesting;
1734    }
1735
1736    public boolean isSecureCamera() {
1737        return mSecureCamera;
1738    }
1739
1740    @Override
1741    public boolean isPaused() {
1742        return mPaused;
1743    }
1744
1745    @Override
1746    public int getPreferredChildModeIndex(int modeIndex) {
1747        if (modeIndex == getResources().getInteger(R.integer.camera_mode_photo)) {
1748            boolean hdrPlusOn = mSettingsManager.isHdrPlusOn();
1749            if (hdrPlusOn && GcamHelper.hasGcamCapture()) {
1750                modeIndex = getResources().getInteger(R.integer.camera_mode_gcam);
1751            }
1752        }
1753        return modeIndex;
1754    }
1755
1756    @Override
1757    public void onModeSelected(int modeIndex) {
1758        if (mCurrentModeIndex == modeIndex) {
1759            return;
1760        }
1761
1762        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.MODE_SWITCH_START);
1763        // Record last used camera mode for quick switching
1764        if (modeIndex == getResources().getInteger(R.integer.camera_mode_photo)
1765                || modeIndex == getResources().getInteger(R.integer.camera_mode_gcam)) {
1766            mSettingsManager.setInt(SettingsManager.SETTING_KEY_CAMERA_MODULE_LAST_USED_INDEX,
1767                    modeIndex);
1768        }
1769
1770        closeModule(mCurrentModule);
1771        int oldModuleIndex = mCurrentModeIndex;
1772
1773        // Select the correct module index from the mode switcher index.
1774        modeIndex = getPreferredChildModeIndex(modeIndex);
1775        setModuleFromModeIndex(modeIndex);
1776
1777        mCameraAppUI.resetBottomControls(mCurrentModule, modeIndex);
1778        mCameraAppUI.addShutterListener(mCurrentModule);
1779        openModule(mCurrentModule);
1780        mCurrentModule.onOrientationChanged(mLastRawOrientation);
1781        // Store the module index so we can use it the next time the Camera
1782        // starts up.
1783        SharedPreferences prefs = PreferenceManager
1784                .getDefaultSharedPreferences(mAppContext);
1785        prefs.edit().putInt(CameraSettings.KEY_STARTUP_MODULE_INDEX, modeIndex).apply();
1786    }
1787
1788    /**
1789     * Shows the settings dialog.
1790     */
1791    @Override
1792    public void onSettingsSelected() {
1793        Intent intent = new Intent(this, CameraSettingsActivity.class);
1794        startActivity(intent);
1795    }
1796
1797    /**
1798     * Sets the mCurrentModuleIndex, creates a new module instance for the given
1799     * index an sets it as mCurrentModule.
1800     */
1801    private void setModuleFromModeIndex(int modeIndex) {
1802        ModuleManagerImpl.ModuleAgent agent = mModuleManager.getModuleAgent(modeIndex);
1803        if (agent == null) {
1804            return;
1805        }
1806        if (!agent.requestAppForCamera()) {
1807            mCameraController.closeCamera();
1808        }
1809        mCurrentModeIndex = agent.getModuleId();
1810        mCurrentModule = (CameraModule) agent.createModule(this);
1811    }
1812
1813    @Override
1814    public SettingsManager getSettingsManager() {
1815        return mSettingsManager;
1816    }
1817
1818    @Override
1819    public CameraServices getServices() {
1820        return (CameraServices) getApplication();
1821    }
1822
1823    public List<String> getSupportedModeNames() {
1824        List<Integer> indices = mModuleManager.getSupportedModeIndexList();
1825        List<String> supported = new ArrayList<String>();
1826
1827        for (Integer modeIndex : indices) {
1828            String name = CameraUtil.getCameraModeText(modeIndex, mAppContext);
1829            if (name != null && !name.equals("")) {
1830                supported.add(name);
1831            }
1832        }
1833        return supported;
1834    }
1835
1836    @Override
1837    public ButtonManager getButtonManager() {
1838        if (mButtonManager == null) {
1839            mButtonManager = new ButtonManager(this);
1840        }
1841        return mButtonManager;
1842    }
1843
1844    /**
1845     * Creates an AlertDialog appropriate for choosing whether to enable
1846     * location on the first run of the app.
1847     */
1848    public AlertDialog getFirstTimeLocationAlert() {
1849        AlertDialog.Builder builder = new AlertDialog.Builder(this);
1850        builder = SettingsUtil.getFirstTimeLocationAlertBuilder(builder, new Callback<Boolean>() {
1851            @Override
1852            public void onCallback(Boolean locationOn) {
1853                mSettingsManager.setLocation(locationOn, mLocationManager);
1854            }
1855        });
1856        if (builder != null) {
1857            return builder.create();
1858        } else {
1859            return null;
1860        }
1861    }
1862
1863    /**
1864     * Launches an ACTION_EDIT intent for the given local data item. If
1865     * 'withTinyPlanet' is set, this will show a disambig dialog first to let
1866     * the user start either the tiny planet editor or another photo edior.
1867     *
1868     * @param data The data item to edit.
1869     */
1870    public void launchEditor(LocalData data) {
1871        Intent intent = new Intent(Intent.ACTION_EDIT)
1872                .setDataAndType(data.getUri(), data.getMimeType())
1873                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1874        try {
1875            launchActivityByIntent(intent);
1876        } catch (ActivityNotFoundException e) {
1877            launchActivityByIntent(Intent.createChooser(intent, null));
1878        }
1879    }
1880
1881    @Override
1882    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1883        super.onCreateContextMenu(menu, v, menuInfo);
1884
1885        MenuInflater inflater = getMenuInflater();
1886        inflater.inflate(R.menu.filmstrip_context_menu, menu);
1887    }
1888
1889    @Override
1890    public boolean onContextItemSelected(MenuItem item) {
1891        switch (item.getItemId()) {
1892            case R.id.tiny_planet_editor:
1893                mMyFilmstripBottomControlListener.onTinyPlanet();
1894                return true;
1895            case R.id.photo_editor:
1896                mMyFilmstripBottomControlListener.onEdit();
1897                return true;
1898        }
1899        return false;
1900    }
1901
1902    /**
1903     * Launch the tiny planet editor.
1904     *
1905     * @param data The data must be a 360 degree stereographically mapped
1906     *            panoramic image. It will not be modified, instead a new item
1907     *            with the result will be added to the filmstrip.
1908     */
1909    public void launchTinyPlanetEditor(LocalData data) {
1910        TinyPlanetFragment fragment = new TinyPlanetFragment();
1911        Bundle bundle = new Bundle();
1912        bundle.putString(TinyPlanetFragment.ARGUMENT_URI, data.getUri().toString());
1913        bundle.putString(TinyPlanetFragment.ARGUMENT_TITLE, data.getTitle());
1914        fragment.setArguments(bundle);
1915        fragment.show(getFragmentManager(), "tiny_planet");
1916    }
1917
1918    private void openModule(CameraModule module) {
1919        module.init(this, isSecureCamera(), isCaptureIntent());
1920        module.resume();
1921        updatePreviewVisibility();
1922    }
1923
1924    private void closeModule(CameraModule module) {
1925        module.pause();
1926        mCameraAppUI.clearModuleUI();
1927    }
1928
1929    private void performDeletion() {
1930        if (!mPendingDeletion) {
1931            return;
1932        }
1933        hideUndoDeletionBar(false);
1934        mDataAdapter.executeDeletion();
1935    }
1936
1937    public void showUndoDeletionBar() {
1938        if (mPendingDeletion) {
1939            performDeletion();
1940        }
1941        Log.v(TAG, "showing undo bar");
1942        mPendingDeletion = true;
1943        if (mUndoDeletionBar == null) {
1944            ViewGroup v = (ViewGroup) getLayoutInflater().inflate(R.layout.undo_bar,
1945                    mAboveFilmstripControlLayout, true);
1946            mUndoDeletionBar = (ViewGroup) v.findViewById(R.id.camera_undo_deletion_bar);
1947            View button = mUndoDeletionBar.findViewById(R.id.camera_undo_deletion_button);
1948            button.setOnClickListener(new View.OnClickListener() {
1949                @Override
1950                public void onClick(View view) {
1951                    mDataAdapter.undoDataRemoval();
1952                    hideUndoDeletionBar(true);
1953                }
1954            });
1955            // Setting undo bar clickable to avoid touch events going through
1956            // the bar to the buttons (eg. edit button, etc) underneath the bar.
1957            mUndoDeletionBar.setClickable(true);
1958            // When there is user interaction going on with the undo button, we
1959            // do not want to hide the undo bar.
1960            button.setOnTouchListener(new View.OnTouchListener() {
1961                @Override
1962                public boolean onTouch(View v, MotionEvent event) {
1963                    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
1964                        mIsUndoingDeletion = true;
1965                    } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
1966                        mIsUndoingDeletion = false;
1967                    }
1968                    return false;
1969                }
1970            });
1971        }
1972        mUndoDeletionBar.setAlpha(0f);
1973        mUndoDeletionBar.setVisibility(View.VISIBLE);
1974        mUndoDeletionBar.animate().setDuration(200).alpha(1f).setListener(null).start();
1975    }
1976
1977    private void hideUndoDeletionBar(boolean withAnimation) {
1978        Log.v(TAG, "Hiding undo deletion bar");
1979        mPendingDeletion = false;
1980        if (mUndoDeletionBar != null) {
1981            if (withAnimation) {
1982                mUndoDeletionBar.animate().setDuration(200).alpha(0f)
1983                        .setListener(new Animator.AnimatorListener() {
1984                            @Override
1985                            public void onAnimationStart(Animator animation) {
1986                                // Do nothing.
1987                            }
1988
1989                            @Override
1990                            public void onAnimationEnd(Animator animation) {
1991                                mUndoDeletionBar.setVisibility(View.GONE);
1992                            }
1993
1994                            @Override
1995                            public void onAnimationCancel(Animator animation) {
1996                                // Do nothing.
1997                            }
1998
1999                            @Override
2000                            public void onAnimationRepeat(Animator animation) {
2001                                // Do nothing.
2002                            }
2003                        }).start();
2004            } else {
2005                mUndoDeletionBar.setVisibility(View.GONE);
2006            }
2007        }
2008    }
2009
2010    @Override
2011    public void onOrientationChanged(int orientation) {
2012        // We keep the last known orientation. So if the user first orient
2013        // the camera then point the camera to floor or sky, we still have
2014        // the correct orientation.
2015        if (orientation == OrientationManager.ORIENTATION_UNKNOWN) {
2016            return;
2017        }
2018        mLastRawOrientation = orientation;
2019        if (mCurrentModule != null) {
2020            mCurrentModule.onOrientationChanged(orientation);
2021        }
2022    }
2023
2024    /**
2025     * Enable/disable swipe-to-filmstrip. Will always disable swipe if in
2026     * capture intent.
2027     *
2028     * @param enable {@code true} to enable swipe.
2029     */
2030    public void setSwipingEnabled(boolean enable) {
2031        // TODO: Bring back the functionality.
2032        if (isCaptureIntent()) {
2033            // lockPreview(true);
2034        } else {
2035            // lockPreview(!enable);
2036        }
2037    }
2038
2039    // Accessor methods for getting latency times used in performance testing
2040    public long getFirstPreviewTime() {
2041        if (mCurrentModule instanceof PhotoModule) {
2042            long coverHiddenTime = getCameraAppUI().getCoverHiddenTime();
2043            if (coverHiddenTime != -1) {
2044                return coverHiddenTime - mOnCreateTime;
2045            }
2046        }
2047        return -1;
2048    }
2049
2050    public long getAutoFocusTime() {
2051        return (mCurrentModule instanceof PhotoModule) ?
2052                ((PhotoModule) mCurrentModule).mAutoFocusTime : -1;
2053    }
2054
2055    public long getShutterLag() {
2056        return (mCurrentModule instanceof PhotoModule) ?
2057                ((PhotoModule) mCurrentModule).mShutterLag : -1;
2058    }
2059
2060    public long getShutterToPictureDisplayedTime() {
2061        return (mCurrentModule instanceof PhotoModule) ?
2062                ((PhotoModule) mCurrentModule).mShutterToPictureDisplayedTime : -1;
2063    }
2064
2065    public long getPictureDisplayedToJpegCallbackTime() {
2066        return (mCurrentModule instanceof PhotoModule) ?
2067                ((PhotoModule) mCurrentModule).mPictureDisplayedToJpegCallbackTime : -1;
2068    }
2069
2070    public long getJpegCallbackFinishTime() {
2071        return (mCurrentModule instanceof PhotoModule) ?
2072                ((PhotoModule) mCurrentModule).mJpegCallbackFinishTime : -1;
2073    }
2074
2075    public long getCaptureStartTime() {
2076        return (mCurrentModule instanceof PhotoModule) ?
2077                ((PhotoModule) mCurrentModule).mCaptureStartTime : -1;
2078    }
2079
2080    public boolean isRecording() {
2081        return (mCurrentModule instanceof VideoModule) ?
2082                ((VideoModule) mCurrentModule).isRecording() : false;
2083    }
2084
2085    public CameraManager.CameraOpenCallback getCameraOpenErrorCallback() {
2086        return mCameraController;
2087    }
2088
2089    // For debugging purposes only.
2090    public CameraModule getCurrentModule() {
2091        return mCurrentModule;
2092    }
2093
2094    @Override
2095    public void showTutorial(AbstractTutorialOverlay tutorial) {
2096        mCameraAppUI.showTutorial(tutorial, getLayoutInflater());
2097    }
2098
2099    /**
2100     * Reads the current location recording settings and passes it on to the
2101     * location manager.
2102     */
2103    public void syncLocationManagerSetting() {
2104        mSettingsManager.syncLocationManager(mLocationManager);
2105    }
2106
2107    private void keepScreenOnForAWhile() {
2108        if (mKeepScreenOn) {
2109            return;
2110        }
2111        mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
2112        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2113        mMainHandler.sendEmptyMessageDelayed(MSG_CLEAR_SCREEN_ON_FLAG, SCREEN_DELAY_MS);
2114    }
2115
2116    private void resetScreenOn() {
2117        mKeepScreenOn = false;
2118        mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
2119        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2120    }
2121
2122    /**
2123     * @return {@code true} if the Gallery is launched successfully.
2124     */
2125    private boolean startGallery() {
2126        if (mGalleryIntent == null) {
2127            return false;
2128        }
2129        try {
2130            UsageStatistics.changeScreen(NavigationChange.Mode.GALLERY, InteractionCause.BUTTON);
2131            Intent startGalleryIntent = new Intent(mGalleryIntent);
2132            int currentDataId = mFilmstripController.getCurrentId();
2133            LocalData currentLocalData = mDataAdapter.getLocalData(currentDataId);
2134            if (currentLocalData != null) {
2135                GalleryHelper.setContentUri(startGalleryIntent, currentLocalData.getUri());
2136            }
2137            launchActivityByIntent(startGalleryIntent);
2138        } catch (ActivityNotFoundException e) {
2139            Log.w(TAG, "Failed to launch gallery activity, closing");
2140        }
2141        return false;
2142    }
2143
2144    private void setNfcBeamPushUriFromData(LocalData data) {
2145        final Uri uri = data.getUri();
2146        if (uri != Uri.EMPTY) {
2147            mNfcPushUris[0] = uri;
2148        } else {
2149            mNfcPushUris[0] = null;
2150        }
2151    }
2152
2153    /**
2154     * Updates the visibility of the filmstrip bottom controls and action bar.
2155     */
2156    private void updateUiByData(final int dataId) {
2157        final LocalData currentData = mDataAdapter.getLocalData(dataId);
2158        if (currentData == null) {
2159            Log.w(TAG, "Current data ID not found.");
2160            hideSessionProgress();
2161            return;
2162        }
2163        updateActionBarMenu(currentData);
2164
2165        /* Bottom controls. */
2166        updateBottomControlsByData(currentData);
2167
2168        if (isSecureCamera()) {
2169            // We cannot show buttons in secure camera since go to other
2170            // activities might create a security hole.
2171            mCameraAppUI.getFilmstripBottomControls().hideControls();
2172            return;
2173        }
2174
2175
2176        setNfcBeamPushUriFromData(currentData);
2177
2178        if (!mDataAdapter.isMetadataUpdated(dataId)) {
2179            mDataAdapter.updateMetadata(dataId);
2180        }
2181    }
2182
2183    /**
2184     * Updates the bottom controls based on the data.
2185     */
2186    private void updateBottomControlsByData(final LocalData currentData) {
2187
2188        final CameraAppUI.BottomPanel filmstripBottomPanel =
2189                mCameraAppUI.getFilmstripBottomControls();
2190        filmstripBottomPanel.showControls();
2191        filmstripBottomPanel.setEditButtonVisibility(
2192                currentData.isDataActionSupported(LocalData.DATA_ACTION_EDIT));
2193        filmstripBottomPanel.setShareButtonVisibility(
2194                currentData.isDataActionSupported(LocalData.DATA_ACTION_SHARE));
2195        filmstripBottomPanel.setDeleteButtonVisibility(
2196                currentData.isDataActionSupported(LocalData.DATA_ACTION_DELETE));
2197
2198        /* Progress bar */
2199
2200        Uri contentUri = currentData.getUri();
2201        CaptureSessionManager sessionManager = getServices()
2202                .getCaptureSessionManager();
2203
2204        if (sessionManager.hasErrorMessage(contentUri)) {
2205            showProcessError(sessionManager.getErrorMesage(contentUri));
2206        } else {
2207            filmstripBottomPanel.hideProgressError();
2208            CaptureSession session = sessionManager.getSession(contentUri);
2209
2210            if (session != null) {
2211                int sessionProgress = session.getProgress();
2212
2213                if (sessionProgress < 0) {
2214                    hideSessionProgress();
2215                } else {
2216                    CharSequence progressMessage = session.getProgressMessage();
2217                    showSessionProgress(progressMessage);
2218                    updateSessionProgress(sessionProgress);
2219                }
2220            } else {
2221                hideSessionProgress();
2222            }
2223        }
2224
2225        /* View button */
2226
2227        // We need to add this to a separate DB.
2228        final int viewButtonVisibility;
2229        if (PanoramaMetadataLoader.isPanoramaAndUseViewer(currentData)) {
2230            viewButtonVisibility = CameraAppUI.BottomPanel.VIEWER_PHOTO_SPHERE;
2231        } else if (RgbzMetadataLoader.hasRGBZData(currentData)) {
2232            viewButtonVisibility = CameraAppUI.BottomPanel.VIEWER_REFOCUS;
2233        } else {
2234            viewButtonVisibility = CameraAppUI.BottomPanel.VIEWER_NONE;
2235        }
2236
2237        filmstripBottomPanel.setTinyPlanetEnabled(
2238                PanoramaMetadataLoader.isPanorama360(currentData));
2239        filmstripBottomPanel.setViewerButtonVisibility(viewButtonVisibility);
2240    }
2241
2242    private class PeekAnimationHandler extends Handler {
2243        private class DataAndCallback {
2244            LocalData mData;
2245            com.android.camera.util.Callback<Bitmap> mCallback;
2246
2247            public DataAndCallback(LocalData data, com.android.camera.util.Callback<Bitmap>
2248                    callback) {
2249                mData = data;
2250                mCallback = callback;
2251            }
2252        }
2253
2254        public PeekAnimationHandler(Looper looper) {
2255            super(looper);
2256        }
2257
2258        /**
2259         * Starts the animation decoding job and posts a {@code Runnable} back
2260         * when when the decoding is done.
2261         *
2262         * @param data The data item to decode the thumbnail for.
2263         * @param callback {@link com.android.camera.util.Callback} after the
2264         *                 decoding is done.
2265         */
2266        public void startDecodingJob(final LocalData data,
2267                final com.android.camera.util.Callback<Bitmap> callback) {
2268            PeekAnimationHandler.this.obtainMessage(0 /** dummy integer **/,
2269                    new DataAndCallback(data, callback)).sendToTarget();
2270        }
2271
2272        @Override
2273        public void handleMessage(Message msg) {
2274            final LocalData data = ((DataAndCallback) msg.obj).mData;
2275            final com.android.camera.util.Callback<Bitmap> callback =
2276                    ((DataAndCallback) msg.obj).mCallback;
2277            if (data == null || callback == null) {
2278                return;
2279            }
2280
2281            final Bitmap bitmap;
2282            switch (data.getLocalDataType()) {
2283                case LocalData.LOCAL_IN_PROGRESS_DATA:
2284                    byte[] jpegData = Storage.getJpegForSession(data.getUri());
2285                    if (jpegData != null) {
2286                        bitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
2287                    } else {
2288                        bitmap = null;
2289                    }
2290                    break;
2291
2292                case LocalData.LOCAL_IMAGE:
2293                    FileInputStream stream;
2294                    try {
2295                        stream = new FileInputStream(data.getPath());
2296                    } catch (FileNotFoundException e) {
2297                        Log.e(TAG, "File not found:" + data.getPath());
2298                        return;
2299                    }
2300                    Point dim = CameraUtil.resizeToFill(data.getWidth(), data.getHeight(),
2301                            data.getRotation(), mAboveFilmstripControlLayout.getWidth(),
2302                            mAboveFilmstripControlLayout.getMeasuredHeight());
2303                    if (data.getRotation() % 180 != 0) {
2304                        int dummy = dim.x;
2305                        dim.x = dim.y;
2306                        dim.y = dummy;
2307                    }
2308                    bitmap = LocalDataUtil
2309                            .loadImageThumbnailFromStream(stream, data.getWidth(), data.getHeight(),
2310                                    (int) (dim.x * 0.7f), (int) (dim.y * 0.7),
2311                                    data.getRotation(), MAX_PEEK_BITMAP_PIXELS);
2312                    break;
2313
2314                case LocalData.LOCAL_VIDEO:
2315                    bitmap = LocalDataUtil.loadVideoThumbnail(data.getPath());
2316                    break;
2317
2318                default:
2319                    bitmap = null;
2320                    break;
2321            }
2322
2323            if (bitmap == null) {
2324                return;
2325            }
2326
2327            mMainHandler.post(new Runnable() {
2328                @Override
2329                public void run() {
2330                    callback.onCallback(bitmap);
2331                }
2332            });
2333        }
2334    }
2335
2336    private void showDetailsDialog(int dataId) {
2337        final LocalData data = mDataAdapter.getLocalData(dataId);
2338        if (data == null) {
2339            return;
2340        }
2341        MediaDetails details = data.getMediaDetails(getAndroidContext());
2342        if (details == null) {
2343            return;
2344        }
2345        Dialog detailDialog = DetailsDialog.create(CameraActivity.this, details);
2346        detailDialog.show();
2347
2348        UsageStatistics.photoInteraction(
2349                UsageStatistics.hashFileName(fileNameFromDataID(dataId)),
2350                eventprotos.CameraEvent.InteractionType.DETAILS,
2351                InteractionCause.BUTTON);
2352    }
2353
2354    /**
2355     * Show or hide action bar items depending on current data type.
2356     */
2357    private void updateActionBarMenu(LocalData data) {
2358        if (mActionBarMenu == null) {
2359            return;
2360        }
2361
2362        MenuItem detailsMenuItem = mActionBarMenu.findItem(R.id.action_details);
2363        if (detailsMenuItem == null) {
2364            return;
2365        }
2366
2367        int type = data.getLocalDataType();
2368        boolean showDetails = (type == LocalData.LOCAL_IMAGE) || (type == LocalData.LOCAL_VIDEO);
2369        detailsMenuItem.setVisible(showDetails);
2370    }
2371}
2372