CameraActivity.java revision f495ea8ca078fc752b1ed1045ba22817d75e968e
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        updateCaptureControls(visibility);
1346        mCurrentModule.onPreviewVisibilityChanged(visibility);
1347    }
1348
1349    private void updateCaptureControls(int visibility) {
1350        if (visibility == ModuleController.VISIBILITY_HIDDEN) {
1351            mCameraAppUI.setBottomBarVisible(false);
1352            mCameraAppUI.setModeOptionsVisible(false);
1353        } else {
1354            mCameraAppUI.setBottomBarVisible(true);
1355            mCameraAppUI.setModeOptionsVisible(true);
1356        }
1357    }
1358
1359    private void updatePreviewRendering(int visibility) {
1360        if (visibility == ModuleController.VISIBILITY_HIDDEN) {
1361            mCameraAppUI.pausePreviewRendering();
1362        } else {
1363            mCameraAppUI.resumePreviewRendering();
1364        }
1365    }
1366
1367    private int getPreviewVisibility() {
1368        if (mFilmstripCoversPreview) {
1369            return ModuleController.VISIBILITY_HIDDEN;
1370        } else if (mModeListVisible){
1371            return ModuleController.VISIBILITY_COVERED;
1372        } else {
1373            return ModuleController.VISIBILITY_VISIBLE;
1374        }
1375    }
1376
1377    private void setRotationAnimation() {
1378        int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE;
1379        rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
1380        Window win = getWindow();
1381        WindowManager.LayoutParams winParams = win.getAttributes();
1382        winParams.rotationAnimation = rotationAnimation;
1383        win.setAttributes(winParams);
1384    }
1385
1386    @Override
1387    public void onUserInteraction() {
1388        super.onUserInteraction();
1389        if (!isFinishing()) {
1390            keepScreenOnForAWhile();
1391        }
1392    }
1393
1394    @Override
1395    public boolean dispatchTouchEvent(MotionEvent ev) {
1396        boolean result = super.dispatchTouchEvent(ev);
1397        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
1398            // Real deletion is postponed until the next user interaction after
1399            // the gesture that triggers deletion. Until real deletion is
1400            // performed, users can click the undo button to bring back the
1401            // image that they chose to delete.
1402            if (mPendingDeletion && !mIsUndoingDeletion) {
1403                performDeletion();
1404            }
1405        }
1406        return result;
1407    }
1408
1409    @Override
1410    public void onPause() {
1411        mPaused = true;
1412        mPeekAnimationHandler = null;
1413        mPeekAnimationThread.quitSafely();
1414        mPeekAnimationThread = null;
1415        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.ACTIVITY_PAUSE);
1416
1417        // Delete photos that are pending deletion
1418        performDeletion();
1419        mCurrentModule.pause();
1420        mOrientationManager.pause();
1421        // Close the camera and wait for the operation done.
1422        mCameraController.closeCamera();
1423        mPanoramaViewHelper.onPause();
1424
1425        mLocalImagesObserver.setForegroundChangeListener(null);
1426        mLocalImagesObserver.setActivityPaused(true);
1427        mLocalVideosObserver.setActivityPaused(true);
1428        mPreloader.cancelAllLoads();
1429        resetScreenOn();
1430        super.onPause();
1431    }
1432
1433    @Override
1434    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1435        if (requestCode == REQ_CODE_DONT_SWITCH_TO_PREVIEW) {
1436            mResetToPreviewOnResume = false;
1437        } else {
1438            super.onActivityResult(requestCode, resultCode, data);
1439        }
1440    }
1441
1442    @Override
1443    public void onResume() {
1444        mPaused = false;
1445        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.ACTIVITY_RESUME);
1446
1447        mLastLayoutOrientation = getResources().getConfiguration().orientation;
1448
1449        // TODO: Handle this in OrientationManager.
1450        // Auto-rotate off
1451        if (Settings.System.getInt(getContentResolver(),
1452                Settings.System.ACCELEROMETER_ROTATION, 0) == 0) {
1453            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
1454            mAutoRotateScreen = false;
1455        } else {
1456            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
1457            mAutoRotateScreen = true;
1458        }
1459
1460        if (isCaptureIntent()) {
1461            // Foreground event caused by photo or video capure intent.
1462            UsageStatistics.foregrounded(
1463                    eventprotos.ForegroundEvent.ForegroundSource.INTENT_PICKER);
1464        } else if (!mSecureCamera) {
1465            // Foreground event that is not caused by an intent.
1466            UsageStatistics.foregrounded(
1467                    eventprotos.ForegroundEvent.ForegroundSource.ICON_LAUNCHER);
1468        }
1469
1470        Drawable galleryLogo;
1471        if (mSecureCamera) {
1472            mGalleryIntent = null;
1473            galleryLogo = null;
1474        } else {
1475            mGalleryIntent = IntentHelper.getDefaultGalleryIntent(mAppContext);
1476            galleryLogo = IntentHelper.getGalleryIcon(mAppContext, mGalleryIntent);
1477        }
1478        if (galleryLogo == null) {
1479            try {
1480                galleryLogo = getPackageManager().getActivityLogo(getComponentName());
1481            } catch (PackageManager.NameNotFoundException e) {
1482                Log.e(TAG, "Can't get the activity logo");
1483            }
1484        }
1485        if (mGalleryIntent != null) {
1486            mActionBar.setDisplayUseLogoEnabled(true);
1487        }
1488        mActionBar.setLogo(galleryLogo);
1489        mOrientationManager.resume();
1490        super.onResume();
1491        mPeekAnimationThread = new HandlerThread("Peek animation");
1492        mPeekAnimationThread.start();
1493        mPeekAnimationHandler = new PeekAnimationHandler(mPeekAnimationThread.getLooper());
1494        mCurrentModule.resume();
1495        setSwipingEnabled(true);
1496
1497        if (mResetToPreviewOnResume) {
1498            mCameraAppUI.resume();
1499        } else {
1500            LocalData data = mDataAdapter.getLocalData(mFilmstripController.getCurrentId());
1501            if (data != null) {
1502                mDataAdapter.refresh(data.getUri());
1503            }
1504        }
1505        // The share button might be disabled to avoid double tapping.
1506        mCameraAppUI.getFilmstripBottomControls().setShareEnabled(true);
1507        // Default is showing the preview, unless disabled by explicitly
1508        // starting an activity we want to return from to the filmstrip rather
1509        // than the preview.
1510        mResetToPreviewOnResume = true;
1511
1512        if (mLocalVideosObserver.isMediaDataChangedDuringPause()
1513                || mLocalImagesObserver.isMediaDataChangedDuringPause()) {
1514            if (!mSecureCamera) {
1515                // If it's secure camera, requestLoad() should not be called
1516                // as it will load all the data.
1517                if (!mFilmstripVisible) {
1518                    mDataAdapter.requestLoad();
1519                } else {
1520                    mDataAdapter.requestLoadNewPhotos();
1521                }
1522            }
1523        }
1524        mLocalImagesObserver.setActivityPaused(false);
1525        mLocalVideosObserver.setActivityPaused(false);
1526        if (!mSecureCamera) {
1527            mLocalImagesObserver.setForegroundChangeListener(
1528                    new LocalMediaObserver.ChangeListener() {
1529                @Override
1530                public void onChange() {
1531                    mDataAdapter.requestLoadNewPhotos();
1532                }
1533            });
1534        }
1535
1536        keepScreenOnForAWhile();
1537
1538        mPanoramaViewHelper.onResume();
1539        ReleaseDialogHelper.showReleaseInfoDialogOnStart(this, mSettingsManager);
1540        syncLocationManagerSetting();
1541
1542        final int previewVisibility = getPreviewVisibility();
1543        updatePreviewRendering(previewVisibility);
1544    }
1545
1546    @Override
1547    public void onStart() {
1548        super.onStart();
1549        mPanoramaViewHelper.onStart();
1550        if (mResetToPreviewOnResume) {
1551            mCameraAppUI.resume();
1552            mResetToPreviewOnResume = false;
1553        }
1554    }
1555
1556    @Override
1557    protected void onStop() {
1558        mPanoramaViewHelper.onStop();
1559        if (mFeedbackHelper != null) {
1560            mFeedbackHelper.stopFeedback();
1561        }
1562
1563        mLocationManager.disconnect();
1564        super.onStop();
1565    }
1566
1567    @Override
1568    public void onDestroy() {
1569        if (mSecureCamera) {
1570            unregisterReceiver(mScreenOffReceiver);
1571        }
1572        mActionBar.removeOnMenuVisibilityListener(this);
1573        mSettingsManager.removeAllListeners();
1574        mCameraController.removeCallbackReceiver();
1575        getContentResolver().unregisterContentObserver(mLocalImagesObserver);
1576        getContentResolver().unregisterContentObserver(mLocalVideosObserver);
1577        getServices().getCaptureSessionManager().removeSessionListener(mSessionListener);
1578        mCameraAppUI.onDestroy();
1579        mCameraController = null;
1580        mSettingsManager = null;
1581        mCameraAppUI = null;
1582        mOrientationManager = null;
1583        mButtonManager = null;
1584        CameraManagerFactory.recycle();
1585        super.onDestroy();
1586    }
1587
1588    @Override
1589    public void onConfigurationChanged(Configuration config) {
1590        super.onConfigurationChanged(config);
1591        Log.v(TAG, "onConfigurationChanged");
1592        if (config.orientation == Configuration.ORIENTATION_UNDEFINED) {
1593            return;
1594        }
1595
1596        if (mLastLayoutOrientation != config.orientation) {
1597            mLastLayoutOrientation = config.orientation;
1598            mCurrentModule.onLayoutOrientationChanged(
1599                    mLastLayoutOrientation == Configuration.ORIENTATION_LANDSCAPE);
1600        }
1601    }
1602
1603    @Override
1604    public boolean onKeyDown(int keyCode, KeyEvent event) {
1605        if (!mFilmstripVisible) {
1606            if (mCurrentModule.onKeyDown(keyCode, event)) {
1607                return true;
1608            }
1609            // Prevent software keyboard or voice search from showing up.
1610            if (keyCode == KeyEvent.KEYCODE_SEARCH
1611                    || keyCode == KeyEvent.KEYCODE_MENU) {
1612                if (event.isLongPress()) {
1613                    return true;
1614                }
1615            }
1616        }
1617
1618        return super.onKeyDown(keyCode, event);
1619    }
1620
1621    @Override
1622    public boolean onKeyUp(int keyCode, KeyEvent event) {
1623        if (!mFilmstripVisible) {
1624            // If a module is in the middle of capture, it should
1625            // consume the key event.
1626            if (mCurrentModule.onKeyUp(keyCode, event)) {
1627                return true;
1628            } else if (keyCode == KeyEvent.KEYCODE_MENU
1629                    || keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
1630                // Let the mode list view consume the event.
1631                mModeListView.onMenuPressed();
1632                return true;
1633            } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
1634                mCameraAppUI.showFilmstrip();
1635                return true;
1636            }
1637        } else {
1638            if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
1639                mFilmstripController.goToNextItem();
1640                return true;
1641            } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
1642                boolean wentToPrevious = mFilmstripController.goToPreviousItem();
1643                if (!wentToPrevious) {
1644                  // at beginning of filmstrip, hide and go back to preview
1645                  mCameraAppUI.hideFilmstrip();
1646                }
1647                return true;
1648            }
1649        }
1650        return super.onKeyUp(keyCode, event);
1651    }
1652
1653    @Override
1654    public void onBackPressed() {
1655        if (!mCameraAppUI.onBackPressed()) {
1656            if (!mCurrentModule.onBackPressed()) {
1657                super.onBackPressed();
1658            }
1659        }
1660    }
1661
1662    @Override
1663    public boolean isAutoRotateScreen() {
1664        // TODO: Move to OrientationManager.
1665        return mAutoRotateScreen;
1666    }
1667
1668    @Override
1669    public boolean onCreateOptionsMenu(Menu menu) {
1670        MenuInflater inflater = getMenuInflater();
1671        inflater.inflate(R.menu.filmstrip_menu, menu);
1672        mActionBarMenu = menu;
1673        return super.onCreateOptionsMenu(menu);
1674    }
1675
1676    protected void updateStorageSpace() {
1677        mStorageSpaceBytes = Storage.getAvailableSpace();
1678    }
1679
1680    protected long getStorageSpaceBytes() {
1681        return mStorageSpaceBytes;
1682    }
1683
1684    protected void updateStorageSpaceAndHint() {
1685        updateStorageSpace();
1686        updateStorageHint(mStorageSpaceBytes);
1687    }
1688
1689    protected void updateStorageHint(long storageSpace) {
1690        String message = null;
1691        if (storageSpace == Storage.UNAVAILABLE) {
1692            message = getString(R.string.no_storage);
1693        } else if (storageSpace == Storage.PREPARING) {
1694            message = getString(R.string.preparing_sd);
1695        } else if (storageSpace == Storage.UNKNOWN_SIZE) {
1696            message = getString(R.string.access_sd_fail);
1697        } else if (storageSpace <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
1698            message = getString(R.string.spaceIsLow_content);
1699        }
1700
1701        if (message != null) {
1702            if (mStorageHint == null) {
1703                mStorageHint = OnScreenHint.makeText(mAppContext, message);
1704            } else {
1705                mStorageHint.setText(message);
1706            }
1707            mStorageHint.show();
1708        } else if (mStorageHint != null) {
1709            mStorageHint.cancel();
1710            mStorageHint = null;
1711        }
1712    }
1713
1714    protected void setResultEx(int resultCode) {
1715        mResultCodeForTesting = resultCode;
1716        setResult(resultCode);
1717    }
1718
1719    protected void setResultEx(int resultCode, Intent data) {
1720        mResultCodeForTesting = resultCode;
1721        mResultDataForTesting = data;
1722        setResult(resultCode, data);
1723    }
1724
1725    public int getResultCode() {
1726        return mResultCodeForTesting;
1727    }
1728
1729    public Intent getResultData() {
1730        return mResultDataForTesting;
1731    }
1732
1733    public boolean isSecureCamera() {
1734        return mSecureCamera;
1735    }
1736
1737    @Override
1738    public boolean isPaused() {
1739        return mPaused;
1740    }
1741
1742    @Override
1743    public int getPreferredChildModeIndex(int modeIndex) {
1744        if (modeIndex == getResources().getInteger(R.integer.camera_mode_photo)) {
1745            boolean hdrPlusOn = mSettingsManager.isHdrPlusOn();
1746            if (hdrPlusOn && GcamHelper.hasGcamCapture()) {
1747                modeIndex = getResources().getInteger(R.integer.camera_mode_gcam);
1748            }
1749        }
1750        return modeIndex;
1751    }
1752
1753    @Override
1754    public void onModeSelected(int modeIndex) {
1755        if (mCurrentModeIndex == modeIndex) {
1756            return;
1757        }
1758
1759        CameraPerformanceTracker.onEvent(CameraPerformanceTracker.MODE_SWITCH_START);
1760        // Record last used camera mode for quick switching
1761        if (modeIndex == getResources().getInteger(R.integer.camera_mode_photo)
1762                || modeIndex == getResources().getInteger(R.integer.camera_mode_gcam)) {
1763            mSettingsManager.setInt(SettingsManager.SETTING_KEY_CAMERA_MODULE_LAST_USED_INDEX,
1764                    modeIndex);
1765        }
1766
1767        closeModule(mCurrentModule);
1768        int oldModuleIndex = mCurrentModeIndex;
1769
1770        // Select the correct module index from the mode switcher index.
1771        modeIndex = getPreferredChildModeIndex(modeIndex);
1772        setModuleFromModeIndex(modeIndex);
1773
1774        mCameraAppUI.resetBottomControls(mCurrentModule, modeIndex);
1775        mCameraAppUI.addShutterListener(mCurrentModule);
1776        openModule(mCurrentModule);
1777        mCurrentModule.onOrientationChanged(mLastRawOrientation);
1778        // Store the module index so we can use it the next time the Camera
1779        // starts up.
1780        SharedPreferences prefs = PreferenceManager
1781                .getDefaultSharedPreferences(mAppContext);
1782        prefs.edit().putInt(CameraSettings.KEY_STARTUP_MODULE_INDEX, modeIndex).apply();
1783    }
1784
1785    /**
1786     * Shows the settings dialog.
1787     */
1788    @Override
1789    public void onSettingsSelected() {
1790        Intent intent = new Intent(this, CameraSettingsActivity.class);
1791        startActivity(intent);
1792    }
1793
1794    /**
1795     * Sets the mCurrentModuleIndex, creates a new module instance for the given
1796     * index an sets it as mCurrentModule.
1797     */
1798    private void setModuleFromModeIndex(int modeIndex) {
1799        ModuleManagerImpl.ModuleAgent agent = mModuleManager.getModuleAgent(modeIndex);
1800        if (agent == null) {
1801            return;
1802        }
1803        if (!agent.requestAppForCamera()) {
1804            mCameraController.closeCamera();
1805        }
1806        mCurrentModeIndex = agent.getModuleId();
1807        mCurrentModule = (CameraModule) agent.createModule(this);
1808    }
1809
1810    @Override
1811    public SettingsManager getSettingsManager() {
1812        return mSettingsManager;
1813    }
1814
1815    @Override
1816    public CameraServices getServices() {
1817        return (CameraServices) getApplication();
1818    }
1819
1820    public List<String> getSupportedModeNames() {
1821        List<Integer> indices = mModuleManager.getSupportedModeIndexList();
1822        List<String> supported = new ArrayList<String>();
1823
1824        for (Integer modeIndex : indices) {
1825            String name = CameraUtil.getCameraModeText(modeIndex, mAppContext);
1826            if (name != null && !name.equals("")) {
1827                supported.add(name);
1828            }
1829        }
1830        return supported;
1831    }
1832
1833    @Override
1834    public ButtonManager getButtonManager() {
1835        if (mButtonManager == null) {
1836            mButtonManager = new ButtonManager(this);
1837        }
1838        return mButtonManager;
1839    }
1840
1841    /**
1842     * Creates an AlertDialog appropriate for choosing whether to enable
1843     * location on the first run of the app.
1844     */
1845    public AlertDialog getFirstTimeLocationAlert() {
1846        AlertDialog.Builder builder = new AlertDialog.Builder(this);
1847        builder = SettingsUtil.getFirstTimeLocationAlertBuilder(builder, new Callback<Boolean>() {
1848            @Override
1849            public void onCallback(Boolean locationOn) {
1850                mSettingsManager.setLocation(locationOn, mLocationManager);
1851            }
1852        });
1853        if (builder != null) {
1854            return builder.create();
1855        } else {
1856            return null;
1857        }
1858    }
1859
1860    /**
1861     * Launches an ACTION_EDIT intent for the given local data item. If
1862     * 'withTinyPlanet' is set, this will show a disambig dialog first to let
1863     * the user start either the tiny planet editor or another photo edior.
1864     *
1865     * @param data The data item to edit.
1866     */
1867    public void launchEditor(LocalData data) {
1868        Intent intent = new Intent(Intent.ACTION_EDIT)
1869                .setDataAndType(data.getUri(), data.getMimeType())
1870                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1871        try {
1872            launchActivityByIntent(intent);
1873        } catch (ActivityNotFoundException e) {
1874            launchActivityByIntent(Intent.createChooser(intent, null));
1875        }
1876    }
1877
1878    @Override
1879    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1880        super.onCreateContextMenu(menu, v, menuInfo);
1881
1882        MenuInflater inflater = getMenuInflater();
1883        inflater.inflate(R.menu.filmstrip_context_menu, menu);
1884    }
1885
1886    @Override
1887    public boolean onContextItemSelected(MenuItem item) {
1888        switch (item.getItemId()) {
1889            case R.id.tiny_planet_editor:
1890                mMyFilmstripBottomControlListener.onTinyPlanet();
1891                return true;
1892            case R.id.photo_editor:
1893                mMyFilmstripBottomControlListener.onEdit();
1894                return true;
1895        }
1896        return false;
1897    }
1898
1899    /**
1900     * Launch the tiny planet editor.
1901     *
1902     * @param data The data must be a 360 degree stereographically mapped
1903     *            panoramic image. It will not be modified, instead a new item
1904     *            with the result will be added to the filmstrip.
1905     */
1906    public void launchTinyPlanetEditor(LocalData data) {
1907        TinyPlanetFragment fragment = new TinyPlanetFragment();
1908        Bundle bundle = new Bundle();
1909        bundle.putString(TinyPlanetFragment.ARGUMENT_URI, data.getUri().toString());
1910        bundle.putString(TinyPlanetFragment.ARGUMENT_TITLE, data.getTitle());
1911        fragment.setArguments(bundle);
1912        fragment.show(getFragmentManager(), "tiny_planet");
1913    }
1914
1915    private void openModule(CameraModule module) {
1916        module.init(this, isSecureCamera(), isCaptureIntent());
1917        module.resume();
1918        updatePreviewVisibility();
1919    }
1920
1921    private void closeModule(CameraModule module) {
1922        module.pause();
1923        mCameraAppUI.clearModuleUI();
1924    }
1925
1926    private void performDeletion() {
1927        if (!mPendingDeletion) {
1928            return;
1929        }
1930        hideUndoDeletionBar(false);
1931        mDataAdapter.executeDeletion();
1932    }
1933
1934    public void showUndoDeletionBar() {
1935        if (mPendingDeletion) {
1936            performDeletion();
1937        }
1938        Log.v(TAG, "showing undo bar");
1939        mPendingDeletion = true;
1940        if (mUndoDeletionBar == null) {
1941            ViewGroup v = (ViewGroup) getLayoutInflater().inflate(R.layout.undo_bar,
1942                    mAboveFilmstripControlLayout, true);
1943            mUndoDeletionBar = (ViewGroup) v.findViewById(R.id.camera_undo_deletion_bar);
1944            View button = mUndoDeletionBar.findViewById(R.id.camera_undo_deletion_button);
1945            button.setOnClickListener(new View.OnClickListener() {
1946                @Override
1947                public void onClick(View view) {
1948                    mDataAdapter.undoDataRemoval();
1949                    hideUndoDeletionBar(true);
1950                }
1951            });
1952            // Setting undo bar clickable to avoid touch events going through
1953            // the bar to the buttons (eg. edit button, etc) underneath the bar.
1954            mUndoDeletionBar.setClickable(true);
1955            // When there is user interaction going on with the undo button, we
1956            // do not want to hide the undo bar.
1957            button.setOnTouchListener(new View.OnTouchListener() {
1958                @Override
1959                public boolean onTouch(View v, MotionEvent event) {
1960                    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
1961                        mIsUndoingDeletion = true;
1962                    } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
1963                        mIsUndoingDeletion = false;
1964                    }
1965                    return false;
1966                }
1967            });
1968        }
1969        mUndoDeletionBar.setAlpha(0f);
1970        mUndoDeletionBar.setVisibility(View.VISIBLE);
1971        mUndoDeletionBar.animate().setDuration(200).alpha(1f).setListener(null).start();
1972    }
1973
1974    private void hideUndoDeletionBar(boolean withAnimation) {
1975        Log.v(TAG, "Hiding undo deletion bar");
1976        mPendingDeletion = false;
1977        if (mUndoDeletionBar != null) {
1978            if (withAnimation) {
1979                mUndoDeletionBar.animate().setDuration(200).alpha(0f)
1980                        .setListener(new Animator.AnimatorListener() {
1981                            @Override
1982                            public void onAnimationStart(Animator animation) {
1983                                // Do nothing.
1984                            }
1985
1986                            @Override
1987                            public void onAnimationEnd(Animator animation) {
1988                                mUndoDeletionBar.setVisibility(View.GONE);
1989                            }
1990
1991                            @Override
1992                            public void onAnimationCancel(Animator animation) {
1993                                // Do nothing.
1994                            }
1995
1996                            @Override
1997                            public void onAnimationRepeat(Animator animation) {
1998                                // Do nothing.
1999                            }
2000                        }).start();
2001            } else {
2002                mUndoDeletionBar.setVisibility(View.GONE);
2003            }
2004        }
2005    }
2006
2007    @Override
2008    public void onOrientationChanged(int orientation) {
2009        // We keep the last known orientation. So if the user first orient
2010        // the camera then point the camera to floor or sky, we still have
2011        // the correct orientation.
2012        if (orientation == OrientationManager.ORIENTATION_UNKNOWN) {
2013            return;
2014        }
2015        mLastRawOrientation = orientation;
2016        if (mCurrentModule != null) {
2017            mCurrentModule.onOrientationChanged(orientation);
2018        }
2019    }
2020
2021    /**
2022     * Enable/disable swipe-to-filmstrip. Will always disable swipe if in
2023     * capture intent.
2024     *
2025     * @param enable {@code true} to enable swipe.
2026     */
2027    public void setSwipingEnabled(boolean enable) {
2028        // TODO: Bring back the functionality.
2029        if (isCaptureIntent()) {
2030            // lockPreview(true);
2031        } else {
2032            // lockPreview(!enable);
2033        }
2034    }
2035
2036    // Accessor methods for getting latency times used in performance testing
2037    public long getFirstPreviewTime() {
2038        if (mCurrentModule instanceof PhotoModule) {
2039            long coverHiddenTime = getCameraAppUI().getCoverHiddenTime();
2040            if (coverHiddenTime != -1) {
2041                return coverHiddenTime - mOnCreateTime;
2042            }
2043        }
2044        return -1;
2045    }
2046
2047    public long getAutoFocusTime() {
2048        return (mCurrentModule instanceof PhotoModule) ?
2049                ((PhotoModule) mCurrentModule).mAutoFocusTime : -1;
2050    }
2051
2052    public long getShutterLag() {
2053        return (mCurrentModule instanceof PhotoModule) ?
2054                ((PhotoModule) mCurrentModule).mShutterLag : -1;
2055    }
2056
2057    public long getShutterToPictureDisplayedTime() {
2058        return (mCurrentModule instanceof PhotoModule) ?
2059                ((PhotoModule) mCurrentModule).mShutterToPictureDisplayedTime : -1;
2060    }
2061
2062    public long getPictureDisplayedToJpegCallbackTime() {
2063        return (mCurrentModule instanceof PhotoModule) ?
2064                ((PhotoModule) mCurrentModule).mPictureDisplayedToJpegCallbackTime : -1;
2065    }
2066
2067    public long getJpegCallbackFinishTime() {
2068        return (mCurrentModule instanceof PhotoModule) ?
2069                ((PhotoModule) mCurrentModule).mJpegCallbackFinishTime : -1;
2070    }
2071
2072    public long getCaptureStartTime() {
2073        return (mCurrentModule instanceof PhotoModule) ?
2074                ((PhotoModule) mCurrentModule).mCaptureStartTime : -1;
2075    }
2076
2077    public boolean isRecording() {
2078        return (mCurrentModule instanceof VideoModule) ?
2079                ((VideoModule) mCurrentModule).isRecording() : false;
2080    }
2081
2082    public CameraManager.CameraOpenCallback getCameraOpenErrorCallback() {
2083        return mCameraController;
2084    }
2085
2086    // For debugging purposes only.
2087    public CameraModule getCurrentModule() {
2088        return mCurrentModule;
2089    }
2090
2091    @Override
2092    public void showTutorial(AbstractTutorialOverlay tutorial) {
2093        mCameraAppUI.showTutorial(tutorial, getLayoutInflater());
2094    }
2095
2096    /**
2097     * Reads the current location recording settings and passes it on to the
2098     * location manager.
2099     */
2100    public void syncLocationManagerSetting() {
2101        mSettingsManager.syncLocationManager(mLocationManager);
2102    }
2103
2104    private void keepScreenOnForAWhile() {
2105        if (mKeepScreenOn) {
2106            return;
2107        }
2108        mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
2109        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2110        mMainHandler.sendEmptyMessageDelayed(MSG_CLEAR_SCREEN_ON_FLAG, SCREEN_DELAY_MS);
2111    }
2112
2113    private void resetScreenOn() {
2114        mKeepScreenOn = false;
2115        mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
2116        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2117    }
2118
2119    /**
2120     * @return {@code true} if the Gallery is launched successfully.
2121     */
2122    private boolean startGallery() {
2123        if (mGalleryIntent == null) {
2124            return false;
2125        }
2126        try {
2127            UsageStatistics.changeScreen(NavigationChange.Mode.GALLERY, InteractionCause.BUTTON);
2128            Intent startGalleryIntent = new Intent(mGalleryIntent);
2129            int currentDataId = mFilmstripController.getCurrentId();
2130            LocalData currentLocalData = mDataAdapter.getLocalData(currentDataId);
2131            if (currentLocalData != null) {
2132                GalleryHelper.setContentUri(startGalleryIntent, currentLocalData.getUri());
2133            }
2134            launchActivityByIntent(startGalleryIntent);
2135        } catch (ActivityNotFoundException e) {
2136            Log.w(TAG, "Failed to launch gallery activity, closing");
2137        }
2138        return false;
2139    }
2140
2141    private void setNfcBeamPushUriFromData(LocalData data) {
2142        final Uri uri = data.getUri();
2143        if (uri != Uri.EMPTY) {
2144            mNfcPushUris[0] = uri;
2145        } else {
2146            mNfcPushUris[0] = null;
2147        }
2148    }
2149
2150    /**
2151     * Updates the visibility of the filmstrip bottom controls and action bar.
2152     */
2153    private void updateUiByData(final int dataId) {
2154        final LocalData currentData = mDataAdapter.getLocalData(dataId);
2155        if (currentData == null) {
2156            Log.w(TAG, "Current data ID not found.");
2157            hideSessionProgress();
2158            return;
2159        }
2160        updateActionBarMenu(currentData);
2161
2162        /* Bottom controls. */
2163        updateBottomControlsByData(currentData);
2164
2165        if (isSecureCamera()) {
2166            // We cannot show buttons in secure camera since go to other
2167            // activities might create a security hole.
2168            mCameraAppUI.getFilmstripBottomControls().hideControls();
2169            return;
2170        }
2171
2172
2173        setNfcBeamPushUriFromData(currentData);
2174
2175        if (!mDataAdapter.isMetadataUpdated(dataId)) {
2176            mDataAdapter.updateMetadata(dataId);
2177        }
2178    }
2179
2180    /**
2181     * Updates the bottom controls based on the data.
2182     */
2183    private void updateBottomControlsByData(final LocalData currentData) {
2184
2185        final CameraAppUI.BottomPanel filmstripBottomPanel =
2186                mCameraAppUI.getFilmstripBottomControls();
2187        filmstripBottomPanel.showControls();
2188        filmstripBottomPanel.setEditButtonVisibility(
2189                currentData.isDataActionSupported(LocalData.DATA_ACTION_EDIT));
2190        filmstripBottomPanel.setShareButtonVisibility(
2191                currentData.isDataActionSupported(LocalData.DATA_ACTION_SHARE));
2192        filmstripBottomPanel.setDeleteButtonVisibility(
2193                currentData.isDataActionSupported(LocalData.DATA_ACTION_DELETE));
2194
2195        /* Progress bar */
2196
2197        Uri contentUri = currentData.getUri();
2198        CaptureSessionManager sessionManager = getServices()
2199                .getCaptureSessionManager();
2200
2201        if (sessionManager.hasErrorMessage(contentUri)) {
2202            showProcessError(sessionManager.getErrorMesage(contentUri));
2203        } else {
2204            filmstripBottomPanel.hideProgressError();
2205            CaptureSession session = sessionManager.getSession(contentUri);
2206
2207            if (session != null) {
2208                int sessionProgress = session.getProgress();
2209
2210                if (sessionProgress < 0) {
2211                    hideSessionProgress();
2212                } else {
2213                    CharSequence progressMessage = session.getProgressMessage();
2214                    showSessionProgress(progressMessage);
2215                    updateSessionProgress(sessionProgress);
2216                }
2217            } else {
2218                hideSessionProgress();
2219            }
2220        }
2221
2222        /* View button */
2223
2224        // We need to add this to a separate DB.
2225        final int viewButtonVisibility;
2226        if (PanoramaMetadataLoader.isPanoramaAndUseViewer(currentData)) {
2227            viewButtonVisibility = CameraAppUI.BottomPanel.VIEWER_PHOTO_SPHERE;
2228        } else if (RgbzMetadataLoader.hasRGBZData(currentData)) {
2229            viewButtonVisibility = CameraAppUI.BottomPanel.VIEWER_REFOCUS;
2230        } else {
2231            viewButtonVisibility = CameraAppUI.BottomPanel.VIEWER_NONE;
2232        }
2233
2234        filmstripBottomPanel.setTinyPlanetEnabled(
2235                PanoramaMetadataLoader.isPanorama360(currentData));
2236        filmstripBottomPanel.setViewerButtonVisibility(viewButtonVisibility);
2237    }
2238
2239    private class PeekAnimationHandler extends Handler {
2240        private class DataAndCallback {
2241            LocalData mData;
2242            com.android.camera.util.Callback<Bitmap> mCallback;
2243
2244            public DataAndCallback(LocalData data, com.android.camera.util.Callback<Bitmap>
2245                    callback) {
2246                mData = data;
2247                mCallback = callback;
2248            }
2249        }
2250
2251        public PeekAnimationHandler(Looper looper) {
2252            super(looper);
2253        }
2254
2255        /**
2256         * Starts the animation decoding job and posts a {@code Runnable} back
2257         * when when the decoding is done.
2258         *
2259         * @param data The data item to decode the thumbnail for.
2260         * @param callback {@link com.android.camera.util.Callback} after the
2261         *                 decoding is done.
2262         */
2263        public void startDecodingJob(final LocalData data,
2264                final com.android.camera.util.Callback<Bitmap> callback) {
2265            PeekAnimationHandler.this.obtainMessage(0 /** dummy integer **/,
2266                    new DataAndCallback(data, callback)).sendToTarget();
2267        }
2268
2269        @Override
2270        public void handleMessage(Message msg) {
2271            final LocalData data = ((DataAndCallback) msg.obj).mData;
2272            final com.android.camera.util.Callback<Bitmap> callback =
2273                    ((DataAndCallback) msg.obj).mCallback;
2274            if (data == null || callback == null) {
2275                return;
2276            }
2277
2278            final Bitmap bitmap;
2279            switch (data.getLocalDataType()) {
2280                case LocalData.LOCAL_IN_PROGRESS_DATA:
2281                    byte[] jpegData = Storage.getJpegForSession(data.getUri());
2282                    if (jpegData != null) {
2283                        bitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
2284                    } else {
2285                        bitmap = null;
2286                    }
2287                    break;
2288
2289                case LocalData.LOCAL_IMAGE:
2290                    FileInputStream stream;
2291                    try {
2292                        stream = new FileInputStream(data.getPath());
2293                    } catch (FileNotFoundException e) {
2294                        Log.e(TAG, "File not found:" + data.getPath());
2295                        return;
2296                    }
2297                    Point dim = CameraUtil.resizeToFill(data.getWidth(), data.getHeight(),
2298                            data.getRotation(), mAboveFilmstripControlLayout.getWidth(),
2299                            mAboveFilmstripControlLayout.getMeasuredHeight());
2300                    if (data.getRotation() % 180 != 0) {
2301                        int dummy = dim.x;
2302                        dim.x = dim.y;
2303                        dim.y = dummy;
2304                    }
2305                    bitmap = LocalDataUtil
2306                            .loadImageThumbnailFromStream(stream, data.getWidth(), data.getHeight(),
2307                                    (int) (dim.x * 0.7f), (int) (dim.y * 0.7),
2308                                    data.getRotation(), MAX_PEEK_BITMAP_PIXELS);
2309                    break;
2310
2311                case LocalData.LOCAL_VIDEO:
2312                    bitmap = LocalDataUtil.loadVideoThumbnail(data.getPath());
2313                    break;
2314
2315                default:
2316                    bitmap = null;
2317                    break;
2318            }
2319
2320            if (bitmap == null) {
2321                return;
2322            }
2323
2324            mMainHandler.post(new Runnable() {
2325                @Override
2326                public void run() {
2327                    callback.onCallback(bitmap);
2328                }
2329            });
2330        }
2331    }
2332
2333    private void showDetailsDialog(int dataId) {
2334        final LocalData data = mDataAdapter.getLocalData(dataId);
2335        if (data == null) {
2336            return;
2337        }
2338        MediaDetails details = data.getMediaDetails(getAndroidContext());
2339        if (details == null) {
2340            return;
2341        }
2342        Dialog detailDialog = DetailsDialog.create(CameraActivity.this, details);
2343        detailDialog.show();
2344
2345        UsageStatistics.photoInteraction(
2346                UsageStatistics.hashFileName(fileNameFromDataID(dataId)),
2347                eventprotos.CameraEvent.InteractionType.DETAILS,
2348                InteractionCause.BUTTON);
2349    }
2350
2351    /**
2352     * Show or hide action bar items depending on current data type.
2353     */
2354    private void updateActionBarMenu(LocalData data) {
2355        if (mActionBarMenu == null) {
2356            return;
2357        }
2358
2359        MenuItem detailsMenuItem = mActionBarMenu.findItem(R.id.action_details);
2360        if (detailsMenuItem == null) {
2361            return;
2362        }
2363
2364        int type = data.getLocalDataType();
2365        boolean showDetails = (type == LocalData.LOCAL_IMAGE) || (type == LocalData.LOCAL_VIDEO);
2366        detailsMenuItem.setVisible(showDetails);
2367    }
2368}
2369