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