CameraActivity.java revision f63967a175cabd57fcbc20275cc7918bc34b2baf
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.content.ActivityNotFoundException;
25import android.content.BroadcastReceiver;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.SharedPreferences;
31import android.content.pm.ActivityInfo;
32import android.content.res.Configuration;
33import android.graphics.Bitmap;
34import android.graphics.Color;
35import android.graphics.Matrix;
36import android.graphics.SurfaceTexture;
37import android.graphics.drawable.ColorDrawable;
38import android.net.Uri;
39import android.nfc.NfcAdapter;
40import android.nfc.NfcAdapter.CreateBeamUrisCallback;
41import android.nfc.NfcEvent;
42import android.os.Build;
43import android.os.Bundle;
44import android.os.Handler;
45import android.os.Looper;
46import android.os.Message;
47import android.preference.PreferenceManager;
48import android.provider.MediaStore;
49import android.provider.Settings;
50import android.util.Log;
51import android.view.Gravity;
52import android.view.KeyEvent;
53import android.view.LayoutInflater;
54import android.view.MenuItem;
55import android.view.MotionEvent;
56import android.view.View;
57import android.view.ViewGroup;
58import android.view.Window;
59import android.view.WindowManager;
60import android.widget.FrameLayout;
61import android.widget.FrameLayout.LayoutParams;
62import android.widget.ImageView;
63import android.widget.PopupWindow;
64import android.widget.ProgressBar;
65import android.widget.ShareActionProvider;
66import android.widget.TextView;
67
68import com.android.camera.app.AppController;
69import com.android.camera.app.CameraAppUI;
70import com.android.camera.app.CameraController;
71import com.android.camera.app.CameraManager;
72import com.android.camera.app.CameraManagerFactory;
73import com.android.camera.app.CameraProvider;
74import com.android.camera.app.CameraServices;
75import com.android.camera.app.LocationManager;
76import com.android.camera.app.ModuleManagerImpl;
77import com.android.camera.app.OrientationManager;
78import com.android.camera.app.OrientationManagerImpl;
79import com.android.camera.data.CameraDataAdapter;
80import com.android.camera.data.FixedLastDataAdapter;
81import com.android.camera.data.InProgressDataWrapper;
82import com.android.camera.data.LocalData;
83import com.android.camera.data.LocalDataAdapter;
84import com.android.camera.data.LocalMediaObserver;
85import com.android.camera.data.PanoramaMetadataLoader;
86import com.android.camera.data.RgbzMetadataLoader;
87import com.android.camera.data.SimpleViewData;
88import com.android.camera.filmstrip.FilmstripContentPanel;
89import com.android.camera.filmstrip.FilmstripController;
90import com.android.camera.module.ModulesInfo;
91import com.android.camera.session.CaptureSessionManager;
92import com.android.camera.session.CaptureSessionManager.SessionListener;
93import com.android.camera.session.PlaceholderManager;
94import com.android.camera.settings.SettingsManager;
95import com.android.camera.settings.SettingsManager.SettingsCapabilities;
96import com.android.camera.tinyplanet.TinyPlanetFragment;
97import com.android.camera.ui.MainActivityLayout;
98import com.android.camera.ui.ModeListView;
99import com.android.camera.ui.PreviewStatusListener;
100import com.android.camera.ui.SettingsView;
101import com.android.camera.util.ApiHelper;
102import com.android.camera.util.CameraUtil;
103import com.android.camera.util.FeedbackHelper;
104import com.android.camera.util.GcamHelper;
105import com.android.camera.util.IntentHelper;
106import com.android.camera.util.PhotoSphereHelper.PanoramaViewHelper;
107import com.android.camera.util.UsageStatistics;
108import com.android.camera.widget.FilmstripView;
109import com.android.camera2.R;
110
111import java.io.File;
112import java.util.List;
113import java.util.ArrayList;
114
115public class CameraActivity extends Activity
116        implements AppController, CameraManager.CameraOpenCallback,
117        ActionBar.OnMenuVisibilityListener, ShareActionProvider.OnShareTargetSelectedListener,
118        OrientationManager.OnOrientationChangeListener {
119
120    private static final String TAG = "CameraActivity";
121
122    private static final String INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE =
123            "android.media.action.STILL_IMAGE_CAMERA_SECURE";
124    public static final String ACTION_IMAGE_CAPTURE_SECURE =
125            "android.media.action.IMAGE_CAPTURE_SECURE";
126
127    // The intent extra for camera from secure lock screen. True if the gallery
128    // should only show newly captured pictures. sSecureAlbumId does not
129    // increment. This is used when switching between camera, camcorder, and
130    // panorama. If the extra is not set, it is in the normal camera mode.
131    public static final String SECURE_CAMERA_EXTRA = "secure_camera";
132
133    /**
134     * Request code from an activity we started that indicated that we do not want
135     * to reset the view to the preview in onResume.
136     */
137    public static final int REQ_CODE_DONT_SWITCH_TO_PREVIEW = 142;
138
139    public static final int REQ_CODE_GCAM_DEBUG_POSTCAPTURE = 999;
140
141    private static final int MSG_HIDE_ACTION_BAR = 1;
142    private static final int MSG_CLEAR_SCREEN_ON_FLAG = 2;
143    private static final long SCREEN_DELAY_MS = 2 * 60 * 1000;  // 2 mins.
144
145    /**
146     * Whether onResume should reset the view to the preview.
147     */
148    private boolean mResetToPreviewOnResume = true;
149
150    /**
151     * This data adapter is used by FilmStripView.
152     */
153    private LocalDataAdapter mDataAdapter;
154
155    /**
156     * TODO: This should be moved to the app level.
157     */
158    private SettingsManager mSettingsManager;
159
160    /**
161     * TODO: This should be moved to the app level.
162     */
163    private SettingsController mSettingsController;
164    private ModeListView mModeListView;
165    private int mCurrentModeIndex;
166    private CameraModule mCurrentModule;
167    private ModuleManagerImpl mModuleManager;
168    private FrameLayout mAboveFilmstripControlLayout;
169    private FilmstripController mFilmstripController;
170    private boolean mFilmstripVisible;
171    private TextView mBottomProgressText;
172    private ProgressBar mBottomProgressBar;
173    private View mSessionProgressPanel;
174    private int mResultCodeForTesting;
175    private Intent mResultDataForTesting;
176    private OnScreenHint mStorageHint;
177    private long mStorageSpaceBytes = Storage.LOW_STORAGE_THRESHOLD_BYTES;
178    private boolean mAutoRotateScreen;
179    private boolean mSecureCamera;
180    private int mLastRawOrientation;
181    private OrientationManagerImpl mOrientationManager;
182    private LocationManager mLocationManager;
183    private ButtonManager mButtonManager;
184    private Handler mMainHandler;
185    private PanoramaViewHelper mPanoramaViewHelper;
186    private ActionBar mActionBar;
187    private ViewGroup mUndoDeletionBar;
188    private boolean mIsUndoingDeletion = false;
189
190    private final Uri[] mNfcPushUris = new Uri[1];
191
192    private LocalMediaObserver mLocalImagesObserver;
193    private LocalMediaObserver mLocalVideosObserver;
194
195    private boolean mPendingDeletion = false;
196
197    private CameraController mCameraController;
198    private boolean mPaused;
199    private CameraAppUI mCameraAppUI;
200
201    private FeedbackHelper mFeedbackHelper;
202
203    @Override
204    public CameraAppUI getCameraAppUI() {
205        return mCameraAppUI;
206    }
207
208    // close activity when screen turns off
209    private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
210        @Override
211        public void onReceive(Context context, Intent intent) {
212            finish();
213        }
214    };
215
216    /**
217     * Whether the screen is kept turned on.
218     */
219    private boolean mKeepScreenOn;
220    private int mLastLayoutOrientation;
221    private final CameraAppUI.BottomControls.Listener mMyFilmstripBottomControlListener =
222            new CameraAppUI.BottomControls.Listener() {
223
224                /**
225                 * If the current photo is a photo sphere, this will launch the Photo Sphere
226                 * panorama viewer.
227                 */
228                @Override
229                public void onExternalViewer() {
230                    if (mPanoramaViewHelper == null) {
231                        return;
232                    }
233                    final LocalData data = getCurrentLocalData();
234                    if (data == null) {
235                        return;
236                    }
237                    final Uri contentUri = data.getContentUri();
238                    if (contentUri == Uri.EMPTY) {
239                        return;
240                    }
241
242                    if (PanoramaMetadataLoader.isPanorama(data)) {
243                        mPanoramaViewHelper.showPanorama(contentUri);
244                    } else if (RgbzMetadataLoader.hasRGBZData(data)) {
245                        mPanoramaViewHelper.showRgbz(contentUri);
246                    }
247                }
248
249                @Override
250                public void onEdit() {
251                    LocalData data = getCurrentLocalData();
252                    if (data == null) {
253                        return;
254                    }
255                    launchEditor(data);
256                }
257
258                @Override
259                public void onTinyPlanet() {
260                    LocalData data = getCurrentLocalData();
261                    if (data == null) {
262                        return;
263                    }
264                    final int currentDataId = getCurrentDataId();
265                    UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
266                            UsageStatistics.ACTION_EDIT, null, 0,
267                            UsageStatistics.hashFileName(fileNameFromDataID(currentDataId)));
268                    launchTinyPlanetEditor(data);
269                }
270
271                @Override
272                public void onDelete() {
273                    final int currentDataId = getCurrentDataId();
274                    UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
275                            UsageStatistics.ACTION_DELETE, null, 0,
276                            UsageStatistics.hashFileName(fileNameFromDataID(currentDataId)));
277                    removeData(currentDataId);
278                }
279
280                @Override
281                public void onShare() {
282                    final LocalData data = getCurrentLocalData();
283                    Intent shareIntent = getShareIntentByData(data);
284                    if (shareIntent != null) {
285                        try {
286                            launchActivityByIntent(shareIntent);
287                            mCameraAppUI.getFilmstripBottomControls().setShareEnabled(false);
288                        } catch (ActivityNotFoundException ex) {
289                            // Nothing.
290                        }
291                    }
292                }
293
294                private int getCurrentDataId() {
295                    return mFilmstripController.getCurrentId();
296                }
297
298                private LocalData getCurrentLocalData() {
299                    return mDataAdapter.getLocalData(getCurrentDataId());
300                }
301
302                /**
303                 * Sets up the share intent and NFC properly according to the data.
304                 *
305                 * @param data The data to be shared.
306                 */
307                private Intent getShareIntentByData(final LocalData data) {
308                    Intent intent = null;
309                    final Uri contentUri = data.getContentUri();
310                    if (PanoramaMetadataLoader.isPanorama360(data) &&
311                            data.getContentUri() != Uri.EMPTY) {
312                        intent = new Intent(Intent.ACTION_SEND);
313                        intent.setType("application/vnd.google.panorama360+jpg");
314                        intent.putExtra(Intent.EXTRA_STREAM, contentUri);
315                    } else if (data.isDataActionSupported(LocalData.DATA_ACTION_SHARE)) {
316                        final String mimeType = data.getMimeType();
317                        intent = getShareIntentFromType(mimeType);
318                        if (intent != null) {
319                            intent.putExtra(Intent.EXTRA_STREAM, contentUri);
320                            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
321                        }
322                    }
323                    return intent;
324                }
325
326                /**
327                 * Get the share intent according to the mimeType
328                 *
329                 * @param mimeType The mimeType of current data.
330                 * @return the video/image's ShareIntent or null if mimeType is invalid.
331                 */
332                private Intent getShareIntentFromType(String mimeType) {
333                    // Lazily create the intent object.
334                    Intent intent = new Intent(Intent.ACTION_SEND);
335                    if (mimeType.startsWith("video/")) {
336                        intent.setType("video/*");
337                    } else {
338                        if (mimeType.startsWith("image/")) {
339                            intent.setType("image/*");
340                        } else {
341                            Log.w(TAG, "unsupported mimeType " + mimeType);
342                        }
343                    }
344                    return intent;
345                }
346            };
347
348    private ComboPreferences mPreferences;
349    private ContentResolver mContentResolver;
350
351    @Override
352    public void onCameraOpened(CameraManager.CameraProxy camera) {
353        if (!mModuleManager.getModuleAgent(mCurrentModeIndex).requestAppForCamera()) {
354            // We shouldn't be here. Just close the camera and leave.
355            camera.release(false);
356            throw new IllegalStateException("Camera opened but the module shouldn't be " +
357                    "requesting");
358        }
359        if (mCurrentModule != null) {
360            SettingsCapabilities capabilities =
361                SettingsController.getSettingsCapabilities(camera);
362            mSettingsManager.changeCamera(camera.getCameraId(), capabilities);
363            mCurrentModule.onCameraAvailable(camera);
364        }
365        mCameraAppUI.onChangeCamera();
366    }
367
368    @Override
369    public void onCameraDisabled(int cameraId) {
370        UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA, UsageStatistics.ACTION_OPEN_FAIL,
371                "security");
372
373        CameraUtil.showErrorAndFinish(this, R.string.camera_disabled);
374    }
375
376    @Override
377    public void onDeviceOpenFailure(int cameraId) {
378        UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
379                UsageStatistics.ACTION_OPEN_FAIL, "open");
380
381        CameraUtil.showErrorAndFinish(this, R.string.cannot_connect_camera);
382    }
383
384    @Override
385    public void onReconnectionFailure(CameraManager mgr) {
386        UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
387                UsageStatistics.ACTION_OPEN_FAIL, "reconnect");
388
389        CameraUtil.showErrorAndFinish(this, R.string.cannot_connect_camera);
390    }
391
392    private class MainHandler extends Handler {
393        public MainHandler(Looper looper) {
394            super(looper);
395        }
396
397        @Override
398        public void handleMessage(Message msg) {
399            switch (msg.what) {
400                case MSG_HIDE_ACTION_BAR: {
401                    removeMessages(MSG_HIDE_ACTION_BAR);
402                    CameraActivity.this.setFilmstripUiVisibility(false);
403                    break;
404                }
405
406                case MSG_CLEAR_SCREEN_ON_FLAG:  {
407                    if (!mPaused) {
408                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
409                    }
410                    break;
411                }
412
413                default:
414            }
415        }
416    }
417
418    private String fileNameFromDataID(int dataID) {
419        final LocalData localData = mDataAdapter.getLocalData(dataID);
420
421        File localFile = new File(localData.getPath());
422        return localFile.getName();
423    }
424
425    private final FilmstripContentPanel.Listener mFilmstripListener =
426            new FilmstripContentPanel.Listener() {
427
428                @Override
429                public void onFilmstripHidden() {
430                    mFilmstripVisible = false;
431                    CameraActivity.this.setFilmstripUiVisibility(false);
432                    // When the user hide the filmstrip (either swipe out or
433                    // tap on back key) we move to the first item so next time
434                    // when the user swipe in the filmstrip, the most recent
435                    // one is shown.
436                    mFilmstripController.goToFirstItem();
437                    if (mCurrentModule != null) {
438                        mCurrentModule.onPreviewVisibilityChanged(true);
439                    }
440                }
441
442                @Override
443                public void onFilmstripShown() {
444                    mFilmstripVisible = true;
445                    updateUiByData(mFilmstripController.getCurrentId());
446                    if (mCurrentModule != null) {
447                        mCurrentModule.onPreviewVisibilityChanged(false);
448                    }
449                }
450
451                @Override
452                public void onDataPromoted(int dataID) {
453                    UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
454                            UsageStatistics.ACTION_DELETE, "promoted", 0,
455                            UsageStatistics.hashFileName(fileNameFromDataID(dataID)));
456
457                    removeData(dataID);
458                }
459
460                @Override
461                public void onDataDemoted(int dataID) {
462                    UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
463                            UsageStatistics.ACTION_DELETE, "demoted", 0,
464                            UsageStatistics.hashFileName(fileNameFromDataID(dataID)));
465
466                    removeData(dataID);
467                }
468
469                @Override
470                public void onEnterFullScreen(int dataId) {
471                    if (mFilmstripVisible) {
472                        CameraActivity.this.setFilmstripUiVisibility(false);
473                    }
474                }
475
476                @Override
477                public void onLeaveFullScreen(int dataId) {
478                    // Do nothing.
479                }
480
481                @Override
482                public void onEnterFilmstrip(int dataId) {
483                    if (mFilmstripVisible) {
484                        CameraActivity.this.setFilmstripUiVisibility(true);
485                    }
486                }
487
488                @Override
489                public void onLeaveFilmstrip(int dataId) {
490                    // Do nothing.
491                }
492
493                @Override
494                public void onDataReloaded() {
495                    if (!mFilmstripVisible) {
496                        return;
497                    }
498                    updateUiByData(mFilmstripController.getCurrentId());
499                }
500
501                @Override
502                public void onDataUpdated(int dataId) {
503                    if (!mFilmstripVisible) {
504                        return;
505                    }
506                    updateUiByData(mFilmstripController.getCurrentId());
507                }
508
509                @Override
510                public void onEnterZoomView(int dataID) {
511                    if (mFilmstripVisible) {
512                        CameraActivity.this.setFilmstripUiVisibility(false);
513                    }
514                }
515
516                @Override
517                public void onDataFocusChanged(final int prevDataId, final int newDataId) {
518                    if (!mFilmstripVisible) {
519                        return;
520                    }
521                    // TODO: This callback is UI event callback, should always
522                    // happen on UI thread. Find the reason for this
523                    // runOnUiThread() and fix it.
524                    runOnUiThread(new Runnable() {
525                        @Override
526                        public void run() {
527                            updateUiByData(newDataId);
528                        }
529                    });
530                }
531            };
532
533    private final LocalDataAdapter.LocalDataListener mLocalDataListener =
534            new LocalDataAdapter.LocalDataListener() {
535                @Override
536                public void onMetadataUpdated(List<Integer> updatedData) {
537                    int currentDataId = mFilmstripController.getCurrentId();
538                    for (Integer dataId : updatedData) {
539                        if (dataId == currentDataId) {
540                            updateBottomControlsByData(mDataAdapter.getLocalData(dataId));
541                        }
542                    }
543                }
544            };
545
546    public void gotoGallery() {
547        UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA, UsageStatistics.ACTION_FILMSTRIP,
548                "thumbnailTap");
549
550        mFilmstripController.goToNextItem();
551    }
552
553    /**
554     * If {@param visible} is false, this hides the action bar and switches the
555     * filmstrip UI to lights-out mode.
556     */
557    // TODO: This should not be called outside of the activity.
558    public void setFilmstripUiVisibility(boolean visible) {
559        mMainHandler.removeMessages(MSG_HIDE_ACTION_BAR);
560
561        int currentSystemUIVisibility = mAboveFilmstripControlLayout.getSystemUiVisibility();
562        int newSystemUIVisibility = (visible ? View.SYSTEM_UI_FLAG_VISIBLE : View.SYSTEM_UI_FLAG_FULLSCREEN);
563        if (newSystemUIVisibility != currentSystemUIVisibility) {
564            mAboveFilmstripControlLayout.setSystemUiVisibility(newSystemUIVisibility);
565        }
566
567        boolean currentActionBarVisibility = mActionBar.isShowing();
568        mCameraAppUI.getFilmstripBottomControls().setVisible(visible);
569        if (visible != currentActionBarVisibility) {
570            if (visible) {
571                mActionBar.show();
572            } else {
573                mActionBar.hide();
574            }
575        }
576    }
577
578    private void hideSessionProgress() {
579        mSessionProgressPanel.setVisibility(View.GONE);
580    }
581
582    private void showSessionProgress(CharSequence message) {
583        mBottomProgressText.setText(message);
584        mSessionProgressPanel.setVisibility(View.VISIBLE);
585    }
586
587    private void updateSessionProgress(int progress) {
588        mBottomProgressBar.setProgress(progress);
589    }
590
591    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
592    private void setupNfcBeamPush() {
593        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(CameraActivity.this);
594        if (adapter == null) {
595            return;
596        }
597
598        if (!ApiHelper.HAS_SET_BEAM_PUSH_URIS) {
599            // Disable beaming
600            adapter.setNdefPushMessage(null, CameraActivity.this);
601            return;
602        }
603
604        adapter.setBeamPushUris(null, CameraActivity.this);
605        adapter.setBeamPushUrisCallback(new CreateBeamUrisCallback() {
606            @Override
607            public Uri[] createBeamUris(NfcEvent event) {
608                return mNfcPushUris;
609            }
610        }, CameraActivity.this);
611    }
612
613    @Override
614    public void onMenuVisibilityChanged(boolean isVisible) {
615        // TODO: Remove this or bring back the original implementation: cancel
616        // auto-hide actionbar.
617    }
618
619    @Override
620    public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent) {
621        int currentDataId = mFilmstripController.getCurrentId();
622        if (currentDataId < 0) {
623            return false;
624        }
625        UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA, UsageStatistics.ACTION_SHARE,
626                intent.getComponent().getPackageName(), 0,
627                UsageStatistics.hashFileName(fileNameFromDataID(currentDataId)));
628        return true;
629    }
630
631    // Note: All callbacks come back on the main thread.
632    private final SessionListener mSessionListener =
633            new SessionListener() {
634                @Override
635                public void onSessionQueued(final Uri uri) {
636                    notifyNewMedia(uri);
637                    int dataID = mDataAdapter.findDataByContentUri(uri);
638                    if (dataID != -1) {
639                        // Don't allow special UI actions (swipe to
640                        // delete, for example) on in-progress data.
641                        LocalData d = mDataAdapter.getLocalData(dataID);
642                        InProgressDataWrapper newData = new InProgressDataWrapper(d);
643                        mDataAdapter.updateData(dataID, newData);
644                    }
645                }
646
647                @Override
648                public void onSessionDone(final Uri uri) {
649                    Log.v(TAG, "onSessionDone:" + uri);
650                    int doneID = mDataAdapter.findDataByContentUri(uri);
651                    int currentDataId = mFilmstripController.getCurrentId();
652
653                    if (currentDataId == doneID) {
654                        hideSessionProgress();
655                        updateSessionProgress(0);
656                    }
657                    mDataAdapter.refresh(uri, /* isInProgress */ false);
658                }
659
660                @Override
661                public void onSessionProgress(final Uri uri, final int progress) {
662                    if (progress < 0) {
663                        // Do nothing, there is no task for this URI.
664                        return;
665                    }
666                    int currentDataId = mFilmstripController.getCurrentId();
667                    if (currentDataId == -1) {
668                        return;
669                    }
670                    if (uri.equals(
671                            mDataAdapter.getLocalData(currentDataId).getContentUri())) {
672                        updateSessionProgress(progress);
673                    }
674                }
675
676                @Override
677                public void onSessionUpdated(Uri uri) {
678                    mDataAdapter.refresh(uri, /* isInProgress */ true);
679                }
680            };
681
682    @Override
683    public Context getAndroidContext() {
684        return this;
685    }
686
687    @Override
688    public void launchActivityByIntent(Intent intent) {
689        startActivityForResult(intent, REQ_CODE_DONT_SWITCH_TO_PREVIEW);
690    }
691
692    @Override
693    public int getCurrentModuleIndex() {
694        return mCurrentModeIndex;
695    }
696
697    @Override
698    public int getQuickSwitchToModuleId(int currentModuleIndex) {
699        return mModuleManager.getQuickSwitchToModuleId(currentModuleIndex, mSettingsManager, this);
700    }
701
702    @Override
703    public SurfaceTexture getPreviewBuffer() {
704        // TODO: implement this
705        return null;
706    }
707
708    @Override
709    public void onPreviewStarted() {
710        mCameraAppUI.onPreviewStarted();
711    }
712
713    @Override
714    public void addPreviewAreaSizeChangedListener(
715            PreviewStatusListener.PreviewAreaSizeChangedListener listener) {
716        mCameraAppUI.addPreviewAreaSizeChangedListener(listener);
717    }
718
719    @Override
720    public void removePreviewAreaSizeChangedListener(
721            PreviewStatusListener.PreviewAreaSizeChangedListener listener) {
722        mCameraAppUI.removePreviewAreaSizeChangedListener(listener);
723    }
724
725    @Override
726    public void setupOneShotPreviewListener() {
727        mCameraController.setOneShotPreviewCallback(mMainHandler,
728                new CameraManager.CameraPreviewDataCallback() {
729                    @Override
730                    public void onPreviewFrame(byte[] data, CameraManager.CameraProxy camera) {
731                        mCameraAppUI.onNewPreviewFrame();
732                    }
733                });
734    }
735
736    @Override
737    public void updatePreviewAspectRatio(float aspectRatio) {
738        mCameraAppUI.updatePreviewAspectRatio(aspectRatio);
739    }
740
741    @Override
742    public boolean shouldShowShimmy() {
743        int remainingTimes = mSettingsManager.getInt(
744                SettingsManager.SETTING_SHIMMY_REMAINING_PLAY_TIMES_INDEX);
745        return remainingTimes > 0;
746    }
747
748    @Override
749    public void decrementShimmyPlayTimes() {
750        int remainingTimes = mSettingsManager.getInt(
751                SettingsManager.SETTING_SHIMMY_REMAINING_PLAY_TIMES_INDEX) - 1;
752        if (remainingTimes >= 0) {
753            mSettingsManager.setInt(SettingsManager.SETTING_SHIMMY_REMAINING_PLAY_TIMES_INDEX,
754                    remainingTimes);
755        }
756     }
757
758    @Override
759    public void updatePreviewTransform(Matrix matrix) {
760        mCameraAppUI.updatePreviewTransform(matrix);
761    }
762
763    @Override
764    public void setPreviewStatusListener(PreviewStatusListener previewStatusListener) {
765        mCameraAppUI.setPreviewStatusListener(previewStatusListener);
766    }
767
768    @Override
769    public FrameLayout getModuleLayoutRoot() {
770        return mCameraAppUI.getModuleRootView();
771    }
772
773    @Override
774    public void setShutterEventsListener(ShutterEventsListener listener) {
775        // TODO: implement this
776    }
777
778    @Override
779    public void setShutterEnabled(boolean enabled) {
780        // TODO: implement this
781    }
782
783    @Override
784    public boolean isShutterEnabled() {
785        // TODO: implement this
786        return false;
787    }
788
789    @Override
790    public void startPreCaptureAnimation() {
791        mCameraAppUI.startPreCaptureAnimation();
792    }
793
794    @Override
795    public void cancelPreCaptureAnimation() {
796        // TODO: implement this
797    }
798
799    @Override
800    public void startPostCaptureAnimation() {
801        // TODO: implement this
802    }
803
804    @Override
805    public void startPostCaptureAnimation(Bitmap thumbnail) {
806        // TODO: implement this
807    }
808
809    @Override
810    public void cancelPostCaptureAnimation() {
811        // TODO: implement this
812    }
813
814    @Override
815    public OrientationManager getOrientationManager() {
816        return mOrientationManager;
817    }
818
819    @Override
820    public LocationManager getLocationManager() {
821        return mLocationManager;
822    }
823
824    @Override
825    public void lockOrientation() {
826        mOrientationManager.lockOrientation();
827    }
828
829    @Override
830    public void unlockOrientation() {
831        mOrientationManager.unlockOrientation();
832    }
833
834    @Override
835    public void notifyNewMedia(Uri uri) {
836        ContentResolver cr = getContentResolver();
837        String mimeType = cr.getType(uri);
838        if (mimeType.startsWith("video/")) {
839            sendBroadcast(new Intent(CameraUtil.ACTION_NEW_VIDEO, uri));
840            mDataAdapter.addNewVideo(uri);
841        } else if (mimeType.startsWith("image/")) {
842            CameraUtil.broadcastNewPicture(this, uri);
843            mDataAdapter.addNewPhoto(uri);
844        } else if (mimeType.startsWith(PlaceholderManager.PLACEHOLDER_MIME_TYPE)) {
845            mDataAdapter.addNewPhoto(uri);
846        } else {
847            android.util.Log.w(TAG, "Unknown new media with MIME type:"
848                    + mimeType + ", uri:" + uri);
849        }
850    }
851
852    @Override
853    public void enableKeepScreenOn(boolean enabled) {
854        if (mPaused) {
855            return;
856        }
857
858        mKeepScreenOn = enabled;
859        if (mKeepScreenOn) {
860            mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
861            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
862        } else {
863            keepScreenOnForAWhile();
864        }
865    }
866
867    @Override
868    public CameraProvider getCameraProvider() {
869        return mCameraController;
870    }
871
872    private void removeData(int dataID) {
873        mDataAdapter.removeData(dataID);
874        if (mDataAdapter.getTotalNumber() > 1) {
875            showUndoDeletionBar();
876        } else {
877            // If camera preview is the only view left in filmstrip,
878            // no need to show undo bar.
879            mPendingDeletion = true;
880            performDeletion();
881            if (mFilmstripVisible) {
882                mCameraAppUI.getFilmstripContentPanel().animateHide();
883            }
884        }
885    }
886
887    @Override
888    public boolean onOptionsItemSelected(MenuItem item) {
889        // Handle presses on the action bar items
890        switch (item.getItemId()) {
891            case android.R.id.home:
892                if (mFilmstripVisible && IntentHelper.shouldLaunchGalleryOnUpAction()) {
893                    startGallery();
894                } else {
895                    onBackPressed();
896                }
897                return true;
898            default:
899                return super.onOptionsItemSelected(item);
900        }
901    }
902
903    private boolean isCaptureIntent() {
904        if (MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent().getAction())
905                || MediaStore.ACTION_IMAGE_CAPTURE.equals(getIntent().getAction())
906                || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(getIntent().getAction())) {
907            return true;
908        } else {
909            return false;
910        }
911    }
912
913    @Override
914    public void onCreate(Bundle state) {
915        super.onCreate(state);
916        GcamHelper.init(getContentResolver());
917
918        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
919        setContentView(R.layout.activity_main);
920        mActionBar = getActionBar();
921        mActionBar.addOnMenuVisibilityListener(this);
922        mMainHandler = new MainHandler(getMainLooper());
923        mCameraController =
924                new CameraController(this, this, mMainHandler,
925                        CameraManagerFactory.getAndroidCameraManager());
926        mPreferences = new ComboPreferences(getAndroidContext());
927        mContentResolver = this.getContentResolver();
928
929        mSettingsManager = new SettingsManager(this, mCameraController.getNumberOfCameras());
930
931        // Remove this after we get rid of ComboPreferences.
932        int cameraId = Integer.parseInt(mSettingsManager.get(SettingsManager.SETTING_CAMERA_ID));
933        mPreferences.setLocalId(this, cameraId);
934        CameraSettings.upgradeGlobalPreferences(mPreferences,
935                mCameraController.getNumberOfCameras());
936        // TODO: Try to move all the resources allocation to happen as soon as
937        // possible so we can call module.init() at the earliest time.
938        mModuleManager = new ModuleManagerImpl();
939        ModulesInfo.setupModules(this, mModuleManager);
940
941        mModeListView = (ModeListView) findViewById(R.id.mode_list_layout);
942        mModeListView.init(mModuleManager.getSupportedModeIndexList());
943        if (ApiHelper.HAS_ROTATION_ANIMATION) {
944            setRotationAnimation();
945        }
946
947        // Check if this is in the secure camera mode.
948        Intent intent = getIntent();
949        String action = intent.getAction();
950        if (INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action)
951                || ACTION_IMAGE_CAPTURE_SECURE.equals(action)) {
952            mSecureCamera = true;
953        } else {
954            mSecureCamera = intent.getBooleanExtra(SECURE_CAMERA_EXTRA, false);
955        }
956
957        if (mSecureCamera) {
958            // Change the window flags so that secure camera can show when locked
959            Window win = getWindow();
960            WindowManager.LayoutParams params = win.getAttributes();
961            params.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
962            win.setAttributes(params);
963
964            // Filter for screen off so that we can finish secure camera activity
965            // when screen is off.
966            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
967            registerReceiver(mScreenOffReceiver, filter);
968        }
969        mCameraAppUI = new CameraAppUI(this,
970                (MainActivityLayout) findViewById(R.id.activity_root_view),
971                isSecureCamera(), isCaptureIntent());
972
973        mCameraAppUI.setFilmstripBottomControlsListener(mMyFilmstripBottomControlListener);
974
975        mAboveFilmstripControlLayout =
976                (FrameLayout) findViewById(R.id.camera_filmstrip_content_layout);
977
978        // Add the session listener so we can track the session progress updates.
979        getServices().getCaptureSessionManager().addSessionListener(mSessionListener);
980        mSessionProgressPanel = findViewById(R.id.pano_session_progress_panel);
981        mBottomProgressBar = (ProgressBar) findViewById(R.id.pano_session_progress_bar);
982        mBottomProgressText = (TextView) findViewById(R.id.pano_session_progress_text);
983        mFilmstripController = ((FilmstripView) findViewById(R.id.filmstrip_view)).getController();
984        mFilmstripController.setImageGap(
985                getResources().getDimensionPixelSize(R.dimen.camera_film_strip_gap));
986        mPanoramaViewHelper = new PanoramaViewHelper(this);
987        mPanoramaViewHelper.onCreate();
988        // Set up the camera preview first so the preview shows up ASAP.
989        mDataAdapter = new CameraDataAdapter(getApplicationContext(),
990                new ColorDrawable(getResources().getColor(R.color.photo_placeholder)));
991        mDataAdapter.setLocalDataListener(mLocalDataListener);
992
993        mCameraAppUI.getFilmstripContentPanel().setFilmstripListener(mFilmstripListener);
994
995        mLocationManager = new LocationManager(this);
996        mSettingsController = new SettingsController(this);
997
998        int modeIndex = -1;
999        int photoIndex = getResources().getInteger(R.integer.camera_mode_photo);
1000        int gcamIndex = getResources().getInteger(R.integer.camera_mode_gcam);
1001        if (MediaStore.INTENT_ACTION_VIDEO_CAMERA.equals(getIntent().getAction())
1002                || MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent().getAction())) {
1003            modeIndex = photoIndex;
1004        } else if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(getIntent().getAction())
1005                || MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(getIntent()
1006                        .getAction())) {
1007            modeIndex = photoIndex;
1008            if (mSettingsManager.getInt(SettingsManager.SETTING_STARTUP_MODULE_INDEX)
1009                        == gcamIndex && GcamHelper.hasGcamCapture()) {
1010                modeIndex = gcamIndex;
1011            }
1012        } else if (MediaStore.ACTION_IMAGE_CAPTURE.equals(getIntent().getAction())
1013                || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(getIntent().getAction())) {
1014            modeIndex = photoIndex;
1015        } else {
1016            // If the activity has not been started using an explicit intent,
1017            // read the module index from the last time the user changed modes
1018            modeIndex = mSettingsManager.getInt(SettingsManager.SETTING_STARTUP_MODULE_INDEX);
1019            if ((modeIndex == gcamIndex &&
1020                    !GcamHelper.hasGcamCapture()) || modeIndex < 0) {
1021                modeIndex = photoIndex;
1022            }
1023        }
1024
1025        mOrientationManager = new OrientationManagerImpl(this);
1026        mOrientationManager.addOnOrientationChangeListener(mMainHandler, this);
1027
1028        setModuleFromModeIndex(modeIndex);
1029        // TODO: Remove this when refactor is done.
1030        if (modeIndex == getResources().getInteger(R.integer.camera_mode_photo)
1031                || modeIndex == getResources().getInteger(R.integer.camera_mode_video)
1032                || modeIndex == getResources().getInteger(R.integer.camera_mode_gcam)
1033                || modeIndex == getResources().getInteger(R.integer.camera_mode_craft)
1034                || modeIndex == getResources().getInteger(R.integer.camera_mode_refocus)) {
1035            mCameraAppUI.prepareModuleUI();
1036        }
1037
1038        syncBottomBarColor();
1039        mCurrentModule.init(this, isSecureCamera(), isCaptureIntent());
1040
1041        if (!mSecureCamera) {
1042            mFilmstripController.setDataAdapter(mDataAdapter);
1043            if (!isCaptureIntent()) {
1044                mDataAdapter.requestLoad();
1045            }
1046        } else {
1047            // Put a lock placeholder as the last image by setting its date to
1048            // 0.
1049            ImageView v = (ImageView) getLayoutInflater().inflate(
1050                    R.layout.secure_album_placeholder, null);
1051            v.setOnClickListener(new View.OnClickListener() {
1052                @Override
1053                public void onClick(View view) {
1054                    startGallery();
1055                    finish();
1056                }
1057            });
1058            mDataAdapter = new FixedLastDataAdapter(
1059                    getApplicationContext(),
1060                    mDataAdapter,
1061                    new SimpleViewData(
1062                            v,
1063                            v.getDrawable().getIntrinsicWidth(),
1064                            v.getDrawable().getIntrinsicHeight(),
1065                            0, 0));
1066            // Flush out all the original data.
1067            mDataAdapter.flush();
1068            mFilmstripController.setDataAdapter(mDataAdapter);
1069        }
1070
1071        setupNfcBeamPush();
1072
1073        mLocalImagesObserver = new LocalMediaObserver();
1074        mLocalVideosObserver = new LocalMediaObserver();
1075
1076        getContentResolver().registerContentObserver(
1077                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true,
1078                mLocalImagesObserver);
1079        getContentResolver().registerContentObserver(
1080                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, true,
1081                mLocalVideosObserver);
1082        if (FeedbackHelper.feedbackAvailable()) {
1083            mFeedbackHelper = new FeedbackHelper(this);
1084        }
1085    }
1086
1087    private void setRotationAnimation() {
1088        int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE;
1089        rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
1090        Window win = getWindow();
1091        WindowManager.LayoutParams winParams = win.getAttributes();
1092        winParams.rotationAnimation = rotationAnimation;
1093        win.setAttributes(winParams);
1094    }
1095
1096    @Override
1097    public void onUserInteraction() {
1098        super.onUserInteraction();
1099        if (!isFinishing()) {
1100            keepScreenOnForAWhile();
1101        }
1102    }
1103
1104    @Override
1105    public boolean dispatchTouchEvent(MotionEvent ev) {
1106        boolean result = super.dispatchTouchEvent(ev);
1107        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
1108            // Real deletion is postponed until the next user interaction after
1109            // the gesture that triggers deletion. Until real deletion is performed,
1110            // users can click the undo button to bring back the image that they
1111            // chose to delete.
1112            if (mPendingDeletion && !mIsUndoingDeletion) {
1113                performDeletion();
1114            }
1115        }
1116        return result;
1117    }
1118
1119    @Override
1120    public void onPause() {
1121        mPaused = true;
1122
1123        // Delete photos that are pending deletion
1124        performDeletion();
1125        mCurrentModule.pause();
1126        mOrientationManager.pause();
1127        // Close the camera and wait for the operation done.
1128        mCameraController.closeCamera();
1129
1130        mLocalImagesObserver.setActivityPaused(true);
1131        mLocalVideosObserver.setActivityPaused(true);
1132        resetScreenOn();
1133        super.onPause();
1134    }
1135
1136    @Override
1137    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1138        if (requestCode == REQ_CODE_DONT_SWITCH_TO_PREVIEW) {
1139            mResetToPreviewOnResume = false;
1140        } else {
1141            super.onActivityResult(requestCode, resultCode, data);
1142        }
1143    }
1144
1145    @Override
1146    public void onResume() {
1147        mPaused = false;
1148
1149        mLastLayoutOrientation = getResources().getConfiguration().orientation;
1150
1151        // TODO: Handle this in OrientationManager.
1152        // Auto-rotate off
1153        if (Settings.System.getInt(getContentResolver(),
1154                Settings.System.ACCELEROMETER_ROTATION, 0) == 0) {
1155            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
1156            mAutoRotateScreen = false;
1157        } else {
1158            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
1159            mAutoRotateScreen = true;
1160        }
1161
1162        UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
1163                UsageStatistics.ACTION_FOREGROUNDED, this.getClass().getSimpleName());
1164
1165        mOrientationManager.resume();
1166        super.onResume();
1167        mCurrentModule.resume();
1168        setSwipingEnabled(true);
1169
1170        if (mResetToPreviewOnResume) {
1171            mCameraAppUI.resume();
1172        }
1173        // The share button might be disabled to avoid double tapping.
1174        mCameraAppUI.getFilmstripBottomControls().setShareEnabled(true);
1175        // Default is showing the preview, unless disabled by explicitly
1176        // starting an activity we want to return from to the filmstrip rather
1177        // than the preview.
1178        mResetToPreviewOnResume = true;
1179
1180        if (mLocalVideosObserver.isMediaDataChangedDuringPause()
1181                || mLocalImagesObserver.isMediaDataChangedDuringPause()) {
1182            if (!mSecureCamera) {
1183                // If it's secure camera, requestLoad() should not be called
1184                // as it will load all the data.
1185                if (!mFilmstripVisible) {
1186                    mDataAdapter.requestLoad();
1187                }
1188            }
1189        }
1190        mLocalImagesObserver.setActivityPaused(false);
1191        mLocalVideosObserver.setActivityPaused(false);
1192
1193        keepScreenOnForAWhile();
1194
1195        // Lights-out mode at all times.
1196        findViewById(R.id.activity_root_view).setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
1197    }
1198
1199    @Override
1200    public void onStart() {
1201        super.onStart();
1202        mPanoramaViewHelper.onStart();
1203        boolean recordLocation = RecordLocationPreference.get(
1204                mPreferences, mContentResolver);
1205        mLocationManager.recordLocation(recordLocation);
1206    }
1207
1208    @Override
1209    protected void onStop() {
1210        mPanoramaViewHelper.onStop();
1211        if (mFeedbackHelper != null) {
1212            mFeedbackHelper.stopFeedback();
1213        }
1214
1215        mLocationManager.disconnect();
1216        CameraManagerFactory.recycle();
1217        super.onStop();
1218    }
1219
1220    @Override
1221    public void onDestroy() {
1222        if (mSecureCamera) {
1223            unregisterReceiver(mScreenOffReceiver);
1224        }
1225        mSettingsManager.removeAllListeners();
1226        getContentResolver().unregisterContentObserver(mLocalImagesObserver);
1227        getContentResolver().unregisterContentObserver(mLocalVideosObserver);
1228        super.onDestroy();
1229    }
1230
1231    @Override
1232    public void onConfigurationChanged(Configuration config) {
1233        super.onConfigurationChanged(config);
1234        Log.v(TAG, "onConfigurationChanged");
1235        if (config.orientation == Configuration.ORIENTATION_UNDEFINED) {
1236            return;
1237        }
1238
1239        if (mLastLayoutOrientation != config.orientation) {
1240            mLastLayoutOrientation = config.orientation;
1241            mCurrentModule.onLayoutOrientationChanged(
1242                    mLastLayoutOrientation == Configuration.ORIENTATION_LANDSCAPE);
1243        }
1244    }
1245
1246    @Override
1247    public boolean onKeyDown(int keyCode, KeyEvent event) {
1248        if (!mFilmstripVisible) {
1249            if (mCurrentModule.onKeyDown(keyCode, event)) {
1250                return true;
1251            }
1252            // Prevent software keyboard or voice search from showing up.
1253            if (keyCode == KeyEvent.KEYCODE_SEARCH
1254                    || keyCode == KeyEvent.KEYCODE_MENU) {
1255                if (event.isLongPress()) {
1256                    return true;
1257                }
1258            }
1259        }
1260
1261        return super.onKeyDown(keyCode, event);
1262    }
1263
1264    @Override
1265    public boolean onKeyUp(int keyCode, KeyEvent event) {
1266        if (!mFilmstripVisible && mCurrentModule.onKeyUp(keyCode, event)) {
1267            return true;
1268        }
1269        return super.onKeyUp(keyCode, event);
1270    }
1271
1272    @Override
1273    public void onBackPressed() {
1274        if (!mCameraAppUI.onBackPressed()) {
1275            if (!mCurrentModule.onBackPressed()) {
1276                super.onBackPressed();
1277            }
1278        }
1279    }
1280
1281    public boolean isAutoRotateScreen() {
1282        return mAutoRotateScreen;
1283    }
1284
1285    protected void updateStorageSpace() {
1286        mStorageSpaceBytes = Storage.getAvailableSpace();
1287    }
1288
1289    protected long getStorageSpaceBytes() {
1290        return mStorageSpaceBytes;
1291    }
1292
1293    protected void updateStorageSpaceAndHint() {
1294        updateStorageSpace();
1295        updateStorageHint(mStorageSpaceBytes);
1296    }
1297
1298    protected void updateStorageHint(long storageSpace) {
1299        String message = null;
1300        if (storageSpace == Storage.UNAVAILABLE) {
1301            message = getString(R.string.no_storage);
1302        } else if (storageSpace == Storage.PREPARING) {
1303            message = getString(R.string.preparing_sd);
1304        } else if (storageSpace == Storage.UNKNOWN_SIZE) {
1305            message = getString(R.string.access_sd_fail);
1306        } else if (storageSpace <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
1307            message = getString(R.string.spaceIsLow_content);
1308        }
1309
1310        if (message != null) {
1311            if (mStorageHint == null) {
1312                mStorageHint = OnScreenHint.makeText(this, message);
1313            } else {
1314                mStorageHint.setText(message);
1315            }
1316            mStorageHint.show();
1317        } else if (mStorageHint != null) {
1318            mStorageHint.cancel();
1319            mStorageHint = null;
1320        }
1321    }
1322
1323    protected void setResultEx(int resultCode) {
1324        mResultCodeForTesting = resultCode;
1325        setResult(resultCode);
1326    }
1327
1328    protected void setResultEx(int resultCode, Intent data) {
1329        mResultCodeForTesting = resultCode;
1330        mResultDataForTesting = data;
1331        setResult(resultCode, data);
1332    }
1333
1334    public int getResultCode() {
1335        return mResultCodeForTesting;
1336    }
1337
1338    public Intent getResultData() {
1339        return mResultDataForTesting;
1340    }
1341
1342    public boolean isSecureCamera() {
1343        return mSecureCamera;
1344    }
1345
1346    @Override
1347    public boolean isPaused() {
1348        return mPaused;
1349    }
1350
1351    @Override
1352    public void onModeSelected(int modeIndex) {
1353        if (mCurrentModeIndex == modeIndex) {
1354            return;
1355        }
1356
1357        int settingsIndex = getResources().getInteger(R.integer.camera_mode_setting);
1358        if (modeIndex == settingsIndex) {
1359            onSettingsSelected();
1360            return;
1361        }
1362
1363        closeModule(mCurrentModule);
1364        int oldModuleIndex = mCurrentModeIndex;
1365
1366        // Refocus and Gcam are modes that cannot be selected
1367        // from the mode list view, because they are not list items.
1368        // Check whether we should interpret MODULE_CRAFT as either.
1369        if (modeIndex == getResources().getInteger(R.integer.camera_mode_craft)) {
1370            boolean refocusOn = mSettingsManager.isRefocusOn();
1371            boolean hdrPlusOn = mSettingsManager.isHdrPlusOn();
1372            if (refocusOn && hdrPlusOn) {
1373                throw new IllegalStateException("Refocus and hdr plus cannot be on together.");
1374            }
1375            if (refocusOn) {
1376                modeIndex = getResources().getInteger(R.integer.camera_mode_refocus);
1377            } else if (hdrPlusOn) {
1378                modeIndex = getResources().getInteger(R.integer.camera_mode_gcam);
1379            } else {
1380                // Do nothing, keep MODULE_CRAFT.
1381            }
1382        }
1383
1384        setModuleFromModeIndex(modeIndex);
1385
1386        // TODO: The following check is temporary for modules attached to the
1387        // generic_module layout. When the refactor is done, similar logic will
1388        // be applied to all modules.
1389        if (mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_photo)
1390                || mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_video)
1391                || mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_gcam)
1392                || mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_craft)
1393                || mCurrentModeIndex == getResources().getInteger(R.integer.camera_mode_refocus)) {
1394            if (oldModuleIndex != getResources().getInteger(R.integer.camera_mode_photo)
1395                    && oldModuleIndex != getResources().getInteger(R.integer.camera_mode_video)
1396                    && oldModuleIndex != getResources().getInteger(R.integer.camera_mode_gcam)
1397                    && oldModuleIndex != getResources().getInteger(R.integer.camera_mode_craft)
1398                    && oldModuleIndex != getResources().getInteger(R.integer.camera_mode_refocus)) {
1399                mCameraAppUI.prepareModuleUI();
1400            } else {
1401                mCameraAppUI.clearModuleUI();
1402            }
1403        } else {
1404            // This is the old way of removing all views in CameraRootView. Will
1405            // be deprecated soon. It is here to make sure modules that haven't
1406            // been refactored can still function.
1407            mCameraAppUI.clearCameraUI();
1408        }
1409
1410        openModule(mCurrentModule);
1411        mCurrentModule.onOrientationChanged(mLastRawOrientation);
1412        // Store the module index so we can use it the next time the Camera
1413        // starts up.
1414        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
1415        prefs.edit().putInt(CameraSettings.KEY_STARTUP_MODULE_INDEX, modeIndex).apply();
1416    }
1417
1418    public void onSettingsSelected() {
1419        // Temporary until we finalize the touch flow.
1420        LayoutInflater inflater = getLayoutInflater();
1421        SettingsView settingsView = (SettingsView) inflater.inflate(R.layout.settings_list_layout,
1422            null, false);
1423        if (mSettingsController != null) {
1424            settingsView.setController(mSettingsController);
1425        }
1426        if (mFeedbackHelper != null) {
1427            settingsView.setFeedbackHelper(mFeedbackHelper);
1428        }
1429        PopupWindow popup = new PopupWindow(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
1430        popup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
1431        popup.setOutsideTouchable(true);
1432        popup.setFocusable(true);
1433        popup.setContentView(settingsView);
1434        popup.showAtLocation(mModeListView.getRootView(), Gravity.CENTER, 0, 0);
1435    }
1436
1437    /**
1438     * Sets the mCurrentModuleIndex, creates a new module instance for the given
1439     * index an sets it as mCurrentModule.
1440     */
1441    private void setModuleFromModeIndex(int modeIndex) {
1442        ModuleManagerImpl.ModuleAgent agent = mModuleManager.getModuleAgent(modeIndex);
1443        if (agent == null) {
1444            return;
1445        }
1446        if (!agent.requestAppForCamera()) {
1447            mCameraController.closeCamera();
1448        }
1449        mCurrentModeIndex = agent.getModuleId();
1450        mCurrentModule = (CameraModule)  agent.createModule(this);
1451    }
1452
1453    @Override
1454    public SettingsManager getSettingsManager() {
1455        return mSettingsManager;
1456    }
1457
1458    @Override
1459    public CameraServices getServices() {
1460        return (CameraServices) getApplication();
1461    }
1462
1463    @Override
1464    public SettingsController getSettingsController() {
1465        return mSettingsController;
1466    }
1467
1468    public List<String> getSupportedModeNames() {
1469        List<Integer> indices = mModuleManager.getSupportedModeIndexList();
1470        List<String> supported = new ArrayList<String>();
1471
1472        for (Integer modeIndex : indices) {
1473            String name = CameraUtil.getCameraModeText(modeIndex, this);
1474            if (name != null && !name.equals("")) {
1475                supported.add(name);
1476            }
1477        }
1478        return supported;
1479    }
1480
1481    @Override
1482    public ButtonManager getButtonManager() {
1483        if (mButtonManager == null) {
1484            mButtonManager = new ButtonManager(this);
1485        }
1486        return mButtonManager;
1487    }
1488
1489    /**
1490     * Creates an AlertDialog appropriate for choosing whether to enable location
1491     * on the first run of the app.
1492     */
1493    public AlertDialog getFirstTimeLocationAlert() {
1494        AlertDialog.Builder builder = new AlertDialog.Builder(this);
1495        builder = SettingsView.getFirstTimeLocationAlertBuilder(builder, mSettingsController);
1496        if (builder != null) {
1497            return builder.create();
1498        } else {
1499            return null;
1500        }
1501    }
1502
1503    /**
1504     * Launches an ACTION_EDIT intent for the given local data item.
1505     */
1506    public void launchEditor(LocalData data) {
1507        Intent intent = new Intent(Intent.ACTION_EDIT)
1508                .setDataAndType(data.getContentUri(), data.getMimeType())
1509                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1510        try {
1511            launchActivityByIntent(intent);
1512        } catch (ActivityNotFoundException e) {
1513            launchActivityByIntent(Intent.createChooser(intent, null));
1514        }
1515    }
1516
1517    /**
1518     * Launch the tiny planet editor.
1519     *
1520     * @param data The data must be a 360 degree stereographically mapped
1521     *             panoramic image. It will not be modified, instead a new item
1522     *             with the result will be added to the filmstrip.
1523     */
1524    public void launchTinyPlanetEditor(LocalData data) {
1525        TinyPlanetFragment fragment = new TinyPlanetFragment();
1526        Bundle bundle = new Bundle();
1527        bundle.putString(TinyPlanetFragment.ARGUMENT_URI, data.getContentUri().toString());
1528        bundle.putString(TinyPlanetFragment.ARGUMENT_TITLE, data.getTitle());
1529        fragment.setArguments(bundle);
1530        fragment.show(getFragmentManager(), "tiny_planet");
1531    }
1532
1533    private void syncBottomBarColor() {
1534        // Currently not all modules use the generic_module UI.
1535        // TODO: once all modules have a bottom bar, remove
1536        // isUsingBottomBar check.
1537        if (mCurrentModule.isUsingBottomBar()) {
1538            int color = getResources().getColor(
1539                    CameraUtil.getCameraThemeColorId(mCurrentModeIndex, this));
1540            mCameraAppUI.setBottomBarColor(color);
1541        }
1542    }
1543
1544    private void openModule(CameraModule module) {
1545        syncBottomBarColor();
1546        module.init(this, isSecureCamera(), isCaptureIntent());
1547        module.resume();
1548        module.onPreviewVisibilityChanged(!mFilmstripVisible);
1549    }
1550
1551    private void closeModule(CameraModule module) {
1552        module.pause();
1553    }
1554
1555    private void performDeletion() {
1556        if (!mPendingDeletion) {
1557            return;
1558        }
1559        hideUndoDeletionBar(false);
1560        mDataAdapter.executeDeletion();
1561    }
1562
1563    public void showUndoDeletionBar() {
1564        if (mPendingDeletion) {
1565            performDeletion();
1566        }
1567        Log.v(TAG, "showing undo bar");
1568        mPendingDeletion = true;
1569        if (mUndoDeletionBar == null) {
1570            ViewGroup v = (ViewGroup) getLayoutInflater().inflate(R.layout.undo_bar,
1571                    mAboveFilmstripControlLayout, true);
1572            mUndoDeletionBar = (ViewGroup) v.findViewById(R.id.camera_undo_deletion_bar);
1573            View button = mUndoDeletionBar.findViewById(R.id.camera_undo_deletion_button);
1574            button.setOnClickListener(new View.OnClickListener() {
1575                @Override
1576                public void onClick(View view) {
1577                    mDataAdapter.undoDataRemoval();
1578                    hideUndoDeletionBar(true);
1579                }
1580            });
1581            // Setting undo bar clickable to avoid touch events going through
1582            // the bar to the buttons (eg. edit button, etc) underneath the bar.
1583            mUndoDeletionBar.setClickable(true);
1584            // When there is user interaction going on with the undo button, we
1585            // do not want to hide the undo bar.
1586            button.setOnTouchListener(new View.OnTouchListener() {
1587                @Override
1588                public boolean onTouch(View v, MotionEvent event) {
1589                    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
1590                        mIsUndoingDeletion = true;
1591                    } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
1592                        mIsUndoingDeletion = false;
1593                    }
1594                    return false;
1595                }
1596            });
1597        }
1598        mUndoDeletionBar.setAlpha(0f);
1599        mUndoDeletionBar.setVisibility(View.VISIBLE);
1600        mUndoDeletionBar.animate().setDuration(200).alpha(1f).setListener(null).start();
1601    }
1602
1603    private void hideUndoDeletionBar(boolean withAnimation) {
1604        Log.v(TAG, "Hiding undo deletion bar");
1605        mPendingDeletion = false;
1606        if (mUndoDeletionBar != null) {
1607            if (withAnimation) {
1608                mUndoDeletionBar.animate().setDuration(200).alpha(0f)
1609                        .setListener(new Animator.AnimatorListener() {
1610                            @Override
1611                            public void onAnimationStart(Animator animation) {
1612                                // Do nothing.
1613                            }
1614
1615                            @Override
1616                            public void onAnimationEnd(Animator animation) {
1617                                mUndoDeletionBar.setVisibility(View.GONE);
1618                            }
1619
1620                            @Override
1621                            public void onAnimationCancel(Animator animation) {
1622                                // Do nothing.
1623                            }
1624
1625                            @Override
1626                            public void onAnimationRepeat(Animator animation) {
1627                                // Do nothing.
1628                            }
1629                        }).start();
1630            } else {
1631                mUndoDeletionBar.setVisibility(View.GONE);
1632            }
1633        }
1634    }
1635
1636    @Override
1637    public void onOrientationChanged(int orientation) {
1638        // We keep the last known orientation. So if the user first orient
1639        // the camera then point the camera to floor or sky, we still have
1640        // the correct orientation.
1641        if (orientation == OrientationManager.ORIENTATION_UNKNOWN) {
1642            return;
1643        }
1644        mLastRawOrientation = orientation;
1645        if (mCurrentModule != null) {
1646            mCurrentModule.onOrientationChanged(orientation);
1647        }
1648    }
1649
1650    /**
1651     * Enable/disable swipe-to-filmstrip. Will always disable swipe if in
1652     * capture intent.
1653     *
1654     * @param enable {@code true} to enable swipe.
1655     */
1656    public void setSwipingEnabled(boolean enable) {
1657        // TODO: Bring back the functionality.
1658        if (isCaptureIntent()) {
1659            //lockPreview(true);
1660        } else {
1661            //lockPreview(!enable);
1662        }
1663    }
1664
1665    // Accessor methods for getting latency times used in performance testing
1666    public long getAutoFocusTime() {
1667        return (mCurrentModule instanceof PhotoModule) ?
1668                ((PhotoModule) mCurrentModule).mAutoFocusTime : -1;
1669    }
1670
1671    public long getShutterLag() {
1672        return (mCurrentModule instanceof PhotoModule) ?
1673                ((PhotoModule) mCurrentModule).mShutterLag : -1;
1674    }
1675
1676    public long getShutterToPictureDisplayedTime() {
1677        return (mCurrentModule instanceof PhotoModule) ?
1678                ((PhotoModule) mCurrentModule).mShutterToPictureDisplayedTime : -1;
1679    }
1680
1681    public long getPictureDisplayedToJpegCallbackTime() {
1682        return (mCurrentModule instanceof PhotoModule) ?
1683                ((PhotoModule) mCurrentModule).mPictureDisplayedToJpegCallbackTime : -1;
1684    }
1685
1686    public long getJpegCallbackFinishTime() {
1687        return (mCurrentModule instanceof PhotoModule) ?
1688                ((PhotoModule) mCurrentModule).mJpegCallbackFinishTime : -1;
1689    }
1690
1691    public long getCaptureStartTime() {
1692        return (mCurrentModule instanceof PhotoModule) ?
1693                ((PhotoModule) mCurrentModule).mCaptureStartTime : -1;
1694    }
1695
1696    public boolean isRecording() {
1697        return (mCurrentModule instanceof VideoModule) ?
1698                ((VideoModule) mCurrentModule).isRecording() : false;
1699    }
1700
1701    public CameraManager.CameraOpenCallback getCameraOpenErrorCallback() {
1702        return mCameraController;
1703    }
1704
1705    // For debugging purposes only.
1706    public CameraModule getCurrentModule() {
1707        return mCurrentModule;
1708    }
1709
1710    private void keepScreenOnForAWhile() {
1711        if (mKeepScreenOn) {
1712            return;
1713        }
1714        mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
1715        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1716        mMainHandler.sendEmptyMessageDelayed(MSG_CLEAR_SCREEN_ON_FLAG, SCREEN_DELAY_MS);
1717    }
1718
1719    private void resetScreenOn() {
1720        mKeepScreenOn = false;
1721        mMainHandler.removeMessages(MSG_CLEAR_SCREEN_ON_FLAG);
1722        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1723    }
1724
1725    private void startGallery() {
1726        try {
1727            UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
1728                    UsageStatistics.ACTION_GALLERY, null);
1729            launchActivityByIntent(IntentHelper.getGalleryIntent(CameraActivity.this));
1730        } catch (ActivityNotFoundException e) {
1731            Log.w(TAG, "Failed to launch gallery activity, closing");
1732        }
1733    }
1734
1735    private void setNfcBeamPushUriFromData(LocalData data) {
1736        final Uri uri = data.getContentUri();
1737        if (uri != Uri.EMPTY) {
1738            mNfcPushUris[0] = uri;
1739        } else {
1740            mNfcPushUris[0] = null;
1741        }
1742    }
1743
1744    /**
1745     * Updates the visibility of the filmstrip bottom controls.
1746     */
1747    private void updateUiByData(final int dataId) {
1748        if (isSecureCamera()) {
1749            // We cannot show buttons in secure camera since go to other
1750            // activities might create a security hole.
1751            return;
1752        }
1753
1754        final LocalData currentData = mDataAdapter.getLocalData(dataId);
1755        if (currentData == null) {
1756            Log.w(TAG, "Current data ID not found.");
1757            hideSessionProgress();
1758            return;
1759        }
1760
1761        setNfcBeamPushUriFromData(currentData);
1762
1763        /* Bottom controls. */
1764
1765        updateBottomControlsByData(currentData);
1766        if (!mDataAdapter.isMetadataUpdated(dataId)) {
1767            mDataAdapter.updateMetadata(dataId);
1768        }
1769    }
1770
1771    /**
1772     * Updates the bottom controls based on the data.
1773     */
1774    private void updateBottomControlsByData(final LocalData currentData) {
1775
1776        final CameraAppUI.BottomControls filmstripBottomControls =
1777                mCameraAppUI.getFilmstripBottomControls();
1778        filmstripBottomControls.setEditButtonVisibility(
1779                currentData.isDataActionSupported(LocalData.DATA_ACTION_EDIT));
1780        filmstripBottomControls.setShareButtonVisibility(
1781                currentData.isDataActionSupported(LocalData.DATA_ACTION_SHARE));
1782        filmstripBottomControls.setDeleteButtonVisibility(
1783                currentData.isDataActionSupported(LocalData.DATA_ACTION_DELETE));
1784
1785        /* Progress bar */
1786
1787        Uri contentUri = currentData.getContentUri();
1788        CaptureSessionManager sessionManager = getServices()
1789                .getCaptureSessionManager();
1790        int sessionProgress = sessionManager.getSessionProgress(contentUri);
1791
1792        if (sessionProgress < 0) {
1793            hideSessionProgress();
1794        } else {
1795            CharSequence progressMessage = sessionManager
1796                    .getSessionProgressMessage(contentUri);
1797            showSessionProgress(progressMessage);
1798            updateSessionProgress(sessionProgress);
1799        }
1800
1801        /* View button */
1802
1803        // We need to add this to a separate DB.
1804        final int viewButtonVisibility;
1805        if (PanoramaMetadataLoader.isPanorama(currentData)) {
1806            viewButtonVisibility = CameraAppUI.BottomControls.VIEW_PHOTO_SPHERE;
1807        } else if (RgbzMetadataLoader.hasRGBZData(currentData)) {
1808            viewButtonVisibility = CameraAppUI.BottomControls.VIEW_RGBZ;
1809        } else {
1810            viewButtonVisibility = CameraAppUI.BottomControls.VIEW_NONE;
1811        }
1812
1813        filmstripBottomControls.setTinyPlanetButtonVisibility(
1814                PanoramaMetadataLoader.isPanorama360(currentData));
1815        filmstripBottomControls.setViewButtonVisibility(viewButtonVisibility);
1816    }
1817}
1818