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