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