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