CameraActivity.java revision 97cb1ea3dbcf03b4e1fc6b149bf475ef3f3920fc
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.content.ActivityNotFoundException;
24import android.content.BroadcastReceiver;
25import android.content.ComponentName;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.ServiceConnection;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.res.Configuration;
34import android.graphics.drawable.ColorDrawable;
35import android.net.Uri;
36import android.nfc.NfcAdapter;
37import android.nfc.NfcAdapter.CreateBeamUrisCallback;
38import android.nfc.NfcEvent;
39import android.os.AsyncTask;
40import android.os.Build;
41import android.os.Bundle;
42import android.os.Handler;
43import android.os.IBinder;
44import android.os.Looper;
45import android.os.Message;
46import android.preference.PreferenceManager;
47import android.provider.MediaStore;
48import android.provider.Settings;
49import android.util.Log;
50import android.view.KeyEvent;
51import android.view.LayoutInflater;
52import android.view.Menu;
53import android.view.MenuInflater;
54import android.view.MenuItem;
55import android.view.MotionEvent;
56import android.view.OrientationEventListener;
57import android.view.View;
58import android.view.ViewGroup;
59import android.view.Window;
60import android.view.WindowManager;
61import android.widget.FrameLayout;
62import android.widget.ImageView;
63import android.widget.ProgressBar;
64import android.widget.ShareActionProvider;
65
66import com.android.camera.app.AppManagerFactory;
67import com.android.camera.app.PanoramaStitchingManager;
68import com.android.camera.crop.CropActivity;
69import com.android.camera.data.CameraDataAdapter;
70import com.android.camera.data.CameraPreviewData;
71import com.android.camera.data.FixedFirstDataAdapter;
72import com.android.camera.data.FixedLastDataAdapter;
73import com.android.camera.data.InProgressDataWrapper;
74import com.android.camera.data.LocalData;
75import com.android.camera.data.LocalDataAdapter;
76import com.android.camera.data.LocalMediaObserver;
77import com.android.camera.data.MediaDetails;
78import com.android.camera.data.SimpleViewData;
79import com.android.camera.tinyplanet.TinyPlanetFragment;
80import com.android.camera.ui.ModuleSwitcher;
81import com.android.camera.ui.DetailsDialog;
82import com.android.camera.ui.FilmStripView;
83import com.android.camera.util.ApiHelper;
84import com.android.camera.util.CameraUtil;
85import com.android.camera.util.GcamHelper;
86import com.android.camera.util.PhotoSphereHelper;
87import com.android.camera.util.PhotoSphereHelper.PanoramaViewHelper;
88import com.android.camera2.R;
89
90import static com.android.camera.CameraManager.CameraOpenErrorCallback;
91
92public class CameraActivity extends Activity
93        implements ModuleSwitcher.ModuleSwitchListener,
94        ActionBar.OnMenuVisibilityListener {
95
96    private static final String TAG = "CAM_Activity";
97
98    private static final String INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE =
99            "android.media.action.STILL_IMAGE_CAMERA_SECURE";
100    public static final String ACTION_IMAGE_CAPTURE_SECURE =
101            "android.media.action.IMAGE_CAPTURE_SECURE";
102    public static final String ACTION_TRIM_VIDEO =
103            "com.android.camera.action.TRIM";
104    public static final String MEDIA_ITEM_PATH = "media-item-path";
105
106    // The intent extra for camera from secure lock screen. True if the gallery
107    // should only show newly captured pictures. sSecureAlbumId does not
108    // increment. This is used when switching between camera, camcorder, and
109    // panorama. If the extra is not set, it is in the normal camera mode.
110    public static final String SECURE_CAMERA_EXTRA = "secure_camera";
111
112    /**
113     * Request code from an activity we started that indicated that we do not
114     * want to reset the view to the preview in onResume.
115     */
116    public static final int REQ_CODE_DONT_SWITCH_TO_PREVIEW = 142;
117
118    public static final int REQ_CODE_GCAM_DEBUG_POSTCAPTURE = 999;
119
120    private static final int HIDE_ACTION_BAR = 1;
121    private static final long SHOW_ACTION_BAR_TIMEOUT_MS = 3000;
122
123    /** Whether onResume should reset the view to the preview. */
124    private boolean mResetToPreviewOnResume = true;
125
126    // Supported operations at FilmStripView. Different data has different
127    // set of supported operations.
128    private static final int SUPPORT_DELETE = 1 << 0;
129    private static final int SUPPORT_ROTATE = 1 << 1;
130    private static final int SUPPORT_INFO = 1 << 2;
131    private static final int SUPPORT_CROP = 1 << 3;
132    private static final int SUPPORT_SETAS = 1 << 4;
133    private static final int SUPPORT_EDIT = 1 << 5;
134    private static final int SUPPORT_TRIM = 1 << 6;
135    private static final int SUPPORT_SHARE = 1 << 7;
136    private static final int SUPPORT_SHARE_PANORAMA360 = 1 << 8;
137    private static final int SUPPORT_SHOW_ON_MAP = 1 << 9;
138    private static final int SUPPORT_ALL = 0xffffffff;
139
140    /** This data adapter is used by FilmStripView. */
141    private LocalDataAdapter mDataAdapter;
142    /** This data adapter represents the real local camera data. */
143    private LocalDataAdapter mWrappedDataAdapter;
144
145    private PanoramaStitchingManager mPanoramaManager;
146    private int mCurrentModuleIndex;
147    private CameraModule mCurrentModule;
148    private FrameLayout mAboveFilmstripControlLayout;
149    private View mCameraModuleRootView;
150    private FilmStripView mFilmStripView;
151    private ProgressBar mBottomProgress;
152    private View mPanoStitchingPanel;
153    private int mResultCodeForTesting;
154    private Intent mResultDataForTesting;
155    private OnScreenHint mStorageHint;
156    private long mStorageSpaceBytes = Storage.LOW_STORAGE_THRESHOLD_BYTES;
157    private boolean mAutoRotateScreen;
158    private boolean mSecureCamera;
159    // This is a hack to speed up the start of SecureCamera.
160    private static boolean sFirstStartAfterScreenOn = true;
161    private int mLastRawOrientation;
162    private MyOrientationEventListener mOrientationListener;
163    private Handler mMainHandler;
164    private PanoramaViewHelper mPanoramaViewHelper;
165    private CameraPreviewData mCameraPreviewData;
166    private ActionBar mActionBar;
167    private OnActionBarVisibilityListener mOnActionBarVisibilityListener = null;
168    private Menu mActionBarMenu;
169    private ViewGroup mUndoDeletionBar;
170    private boolean mIsUndoingDeletion = false;
171
172    private Uri[] mNfcPushUris = new Uri[1];
173
174    private ShareActionProvider mStandardShareActionProvider;
175    private Intent mStandardShareIntent;
176    private ShareActionProvider mPanoramaShareActionProvider;
177    private Intent mPanoramaShareIntent;
178    private LocalMediaObserver mLocalImagesObserver;
179    private LocalMediaObserver mLocalVideosObserver;
180
181    private final int DEFAULT_SYSTEM_UI_VISIBILITY = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
182            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
183    private boolean mPendingDeletion = false;
184
185    private Intent mVideoShareIntent;
186    private Intent mImageShareIntent;
187
188    private class MyOrientationEventListener
189            extends OrientationEventListener {
190        public MyOrientationEventListener(Context context) {
191            super(context);
192        }
193
194        @Override
195        public void onOrientationChanged(int orientation) {
196            // We keep the last known orientation. So if the user first orient
197            // the camera then point the camera to floor or sky, we still have
198            // the correct orientation.
199            if (orientation == ORIENTATION_UNKNOWN) {
200                return;
201            }
202            mLastRawOrientation = orientation;
203            mCurrentModule.onOrientationChanged(orientation);
204        }
205    }
206
207    private MediaSaveService mMediaSaveService;
208    private ServiceConnection mConnection = new ServiceConnection() {
209        @Override
210        public void onServiceConnected(ComponentName className, IBinder b) {
211            mMediaSaveService = ((MediaSaveService.LocalBinder) b).getService();
212            mCurrentModule.onMediaSaveServiceConnected(mMediaSaveService);
213        }
214
215        @Override
216        public void onServiceDisconnected(ComponentName className) {
217            if (mMediaSaveService != null) {
218                mMediaSaveService.setListener(null);
219                mMediaSaveService = null;
220            }
221        }
222    };
223
224    private CameraOpenErrorCallback mCameraOpenErrorCallback =
225            new CameraOpenErrorCallback() {
226                @Override
227                public void onCameraDisabled(int cameraId) {
228                    CameraUtil.showErrorAndFinish(CameraActivity.this,
229                            R.string.camera_disabled);
230                }
231
232                @Override
233                public void onDeviceOpenFailure(int cameraId) {
234                    CameraUtil.showErrorAndFinish(CameraActivity.this,
235                            R.string.cannot_connect_camera);
236                }
237
238                @Override
239                public void onReconnectionFailure(CameraManager mgr) {
240                    CameraUtil.showErrorAndFinish(CameraActivity.this,
241                            R.string.cannot_connect_camera);
242                }
243            };
244
245    // close activity when screen turns off
246    private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
247        @Override
248        public void onReceive(Context context, Intent intent) {
249            finish();
250        }
251    };
252
253    private static BroadcastReceiver sScreenOffReceiver;
254
255    private static class ScreenOffReceiver extends BroadcastReceiver {
256        @Override
257        public void onReceive(Context context, Intent intent) {
258            sFirstStartAfterScreenOn = true;
259        }
260    }
261
262    private class MainHandler extends Handler {
263        public MainHandler(Looper looper) {
264            super(looper);
265        }
266
267        @Override
268        public void handleMessage(Message msg) {
269            if (msg.what == HIDE_ACTION_BAR) {
270                removeMessages(HIDE_ACTION_BAR);
271                CameraActivity.this.setSystemBarsVisibility(false);
272            }
273        }
274    }
275
276    public interface OnActionBarVisibilityListener {
277        public void onActionBarVisibilityChanged(boolean isVisible);
278    }
279
280    public void setOnActionBarVisibilityListener(OnActionBarVisibilityListener listener) {
281        mOnActionBarVisibilityListener = listener;
282    }
283
284    public static boolean isFirstStartAfterScreenOn() {
285        return sFirstStartAfterScreenOn;
286    }
287
288    public static void resetFirstStartAfterScreenOn() {
289        sFirstStartAfterScreenOn = false;
290    }
291
292    private FilmStripView.Listener mFilmStripListener =
293            new FilmStripView.Listener() {
294                @Override
295                public void onDataPromoted(int dataID) {
296                    removeData(dataID);
297                }
298
299                @Override
300                public void onDataDemoted(int dataID) {
301                    removeData(dataID);
302                }
303
304                @Override
305                public void onDataFullScreenChange(int dataID, boolean full) {
306                    boolean isCameraID = isCameraPreview(dataID);
307                    if (!isCameraID) {
308                        if (!full) {
309                            // Always show action bar in filmstrip mode
310                            CameraActivity.this.setSystemBarsVisibility(true, false);
311                        } else if (mActionBar.isShowing()) {
312                            // Hide action bar after time out in full screen mode
313                            mMainHandler.sendEmptyMessageDelayed(HIDE_ACTION_BAR,
314                                    SHOW_ACTION_BAR_TIMEOUT_MS);
315                        }
316                    }
317                }
318
319                /**
320                 * Check if the local data corresponding to dataID is the camera
321                 * preview.
322                 *
323                 * @param dataID the ID of the local data
324                 * @return true if the local data is not null and it is the
325                 *         camera preview.
326                 */
327                private boolean isCameraPreview(int dataID) {
328                    LocalData localData = mDataAdapter.getLocalData(dataID);
329                    if (localData == null) {
330                        Log.w(TAG, "Current data ID not found.");
331                        return false;
332                    }
333                    return localData.getLocalDataType() == LocalData.LOCAL_CAMERA_PREVIEW;
334                }
335
336                @Override
337                public void onCurrentDataChanged(final int dataID, final boolean current) {
338                    // Delay hiding action bar if there is any user interaction
339                    if (mMainHandler.hasMessages(HIDE_ACTION_BAR)) {
340                        mMainHandler.removeMessages(HIDE_ACTION_BAR);
341                        mMainHandler.sendEmptyMessageDelayed(HIDE_ACTION_BAR,
342                                SHOW_ACTION_BAR_TIMEOUT_MS);
343                    }
344                    runOnUiThread(new Runnable() {
345                        @Override
346                        public void run() {
347                            LocalData currentData = mDataAdapter.getLocalData(dataID);
348                            if (currentData == null) {
349                                Log.w(TAG, "Current data ID not found.");
350                                hidePanoStitchingProgress();
351                                return;
352                            }
353                            boolean isCameraID = currentData.getLocalDataType() ==
354                                    LocalData.LOCAL_CAMERA_PREVIEW;
355                            if (!current) {
356                                if (isCameraID) {
357                                    mCurrentModule.onPreviewFocusChanged(false);
358                                    CameraActivity.this.setSystemBarsVisibility(true);
359                                }
360                                hidePanoStitchingProgress();
361                            } else {
362                                if (isCameraID) {
363                                    mCurrentModule.onPreviewFocusChanged(true);
364                                    // Don't show the action bar in Camera
365                                    // preview.
366                                    CameraActivity.this.setSystemBarsVisibility(false);
367                                    if (mPendingDeletion) {
368                                        performDeletion();
369                                    }
370                                } else {
371                                    updateActionBarMenu(dataID);
372                                }
373
374                                Uri contentUri = currentData.getContentUri();
375                                if (contentUri == null) {
376                                    hidePanoStitchingProgress();
377                                    return;
378                                }
379                                int panoStitchingProgress = mPanoramaManager.getTaskProgress(
380                                        contentUri);
381                                if (panoStitchingProgress < 0) {
382                                    hidePanoStitchingProgress();
383                                    return;
384                                }
385                                showPanoStitchingProgress();
386                                updateStitchingProgress(panoStitchingProgress);
387                            }
388                        }
389                    });
390                }
391
392                @Override
393                public void onToggleSystemDecorsVisibility(int dataID) {
394                    // If action bar is showing, hide it immediately, otherwise
395                    // show action bar and hide it later
396                    if (mActionBar.isShowing()) {
397                        CameraActivity.this.setSystemBarsVisibility(false);
398                    } else {
399                        // Don't show the action bar if that is the camera preview.
400                        boolean isCameraID = isCameraPreview(dataID);
401                        if (!isCameraID) {
402                            CameraActivity.this.setSystemBarsVisibility(true, true);
403                        }
404                    }
405                }
406
407                @Override
408                public void setSystemDecorsVisibility(boolean visible) {
409                    CameraActivity.this.setSystemBarsVisibility(visible);
410                }
411            };
412
413    public void gotoGallery() {
414        mFilmStripView.getController().goToNextItem();
415    }
416
417    /**
418     * If {@param visible} is false, this hides the action bar and switches the system UI
419     * to lights-out mode.
420     */
421    // TODO: This should not be called outside of the activity.
422    public void setSystemBarsVisibility(boolean visible) {
423        setSystemBarsVisibility(visible, false);
424    }
425
426    /**
427     * If {@param visible} is false, this hides the action bar and switches the
428     * system UI to lights-out mode. If {@param hideLater} is true, a delayed message
429     * will be sent after a timeout to hide the action bar.
430     */
431    private void setSystemBarsVisibility(boolean visible, boolean hideLater) {
432        mMainHandler.removeMessages(HIDE_ACTION_BAR);
433        boolean currentlyVisible = mActionBar.isShowing();
434
435        if (visible != currentlyVisible) {
436            int visibility = DEFAULT_SYSTEM_UI_VISIBILITY | (visible ? View.SYSTEM_UI_FLAG_VISIBLE
437                    : View.SYSTEM_UI_FLAG_LOW_PROFILE | View.SYSTEM_UI_FLAG_FULLSCREEN);
438            mAboveFilmstripControlLayout.setSystemUiVisibility(visibility);
439
440            if (visible) {
441                mActionBar.show();
442            } else {
443                mActionBar.hide();
444            }
445            if (mOnActionBarVisibilityListener != null) {
446                mOnActionBarVisibilityListener.onActionBarVisibilityChanged(visible);
447            }
448        }
449
450        // Now delay hiding the bars
451        if (visible && hideLater) {
452            mMainHandler.sendEmptyMessageDelayed(HIDE_ACTION_BAR, SHOW_ACTION_BAR_TIMEOUT_MS);
453        }
454    }
455
456    private void hidePanoStitchingProgress() {
457        mPanoStitchingPanel.setVisibility(View.GONE);
458    }
459
460    private void showPanoStitchingProgress() {
461        mPanoStitchingPanel.setVisibility(View.VISIBLE);
462    }
463
464    private void updateStitchingProgress(int progress) {
465        mBottomProgress.setProgress(progress);
466    }
467
468    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
469    private void setupNfcBeamPush() {
470        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(CameraActivity.this);
471        if (adapter == null) {
472            return;
473        }
474
475        if (!ApiHelper.HAS_SET_BEAM_PUSH_URIS) {
476            // Disable beaming
477            adapter.setNdefPushMessage(null, CameraActivity.this);
478            return;
479        }
480
481        adapter.setBeamPushUris(null, CameraActivity.this);
482        adapter.setBeamPushUrisCallback(new CreateBeamUrisCallback() {
483            @Override
484            public Uri[] createBeamUris(NfcEvent event) {
485                return mNfcPushUris;
486            }
487        }, CameraActivity.this);
488    }
489
490    private void setNfcBeamPushUri(Uri uri) {
491        mNfcPushUris[0] = uri;
492    }
493
494    private void setStandardShareIntent(Uri contentUri, String mimeType) {
495        mStandardShareIntent = getShareIntentFromType(mimeType);
496        if (mStandardShareIntent != null) {
497            mStandardShareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
498            mStandardShareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
499            if (mStandardShareActionProvider != null) {
500                mStandardShareActionProvider.setShareIntent(mStandardShareIntent);
501            }
502        }
503    }
504
505    /**
506     * Get the share intent according to the mimeType
507     *
508     * @param mimeType The mimeType of current data.
509     * @return the video/image's ShareIntent or null if mimeType is invalid.
510     */
511    private Intent getShareIntentFromType(String mimeType) {
512        // Lazily create the intent object.
513        if (mimeType.startsWith("video/")) {
514            if (mVideoShareIntent == null) {
515                mVideoShareIntent = new Intent(Intent.ACTION_SEND);
516                mVideoShareIntent.setType("video/*");
517            }
518            return mVideoShareIntent;
519        } else if (mimeType.startsWith("image/")) {
520            if (mImageShareIntent == null) {
521                mImageShareIntent = new Intent(Intent.ACTION_SEND);
522                mImageShareIntent.setType("image/*");
523            }
524            return mImageShareIntent;
525        }
526        Log.w(TAG, "unsupported mimeType " + mimeType);
527        return null;
528    }
529
530    private void setPanoramaShareIntent(Uri contentUri) {
531        if (mPanoramaShareIntent == null) {
532            mPanoramaShareIntent = new Intent(Intent.ACTION_SEND);
533        }
534        mPanoramaShareIntent.setType("application/vnd.google.panorama360+jpg");
535        mPanoramaShareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
536        if (mPanoramaShareActionProvider != null) {
537            mPanoramaShareActionProvider.setShareIntent(mPanoramaShareIntent);
538        }
539    }
540
541    @Override
542    public void onMenuVisibilityChanged(boolean isVisible) {
543        // If menu is showing, we need to make sure action bar does not go away.
544        mMainHandler.removeMessages(HIDE_ACTION_BAR);
545        if (!isVisible) {
546            mMainHandler.sendEmptyMessageDelayed(HIDE_ACTION_BAR, SHOW_ACTION_BAR_TIMEOUT_MS);
547        }
548    }
549
550    /**
551     * According to the data type, make the menu items for supported operations
552     * visible.
553     *
554     * @param dataID the data ID of the current item.
555     */
556    private void updateActionBarMenu(int dataID) {
557        LocalData currentData = mDataAdapter.getLocalData(dataID);
558        if (currentData == null) {
559            return;
560        }
561        int type = currentData.getLocalDataType();
562
563        if (mActionBarMenu == null) {
564            return;
565        }
566
567        int supported = 0;
568
569        switch (type) {
570            case LocalData.LOCAL_IMAGE:
571                supported |= SUPPORT_DELETE | SUPPORT_ROTATE | SUPPORT_INFO
572                        | SUPPORT_CROP | SUPPORT_SETAS | SUPPORT_EDIT
573                        | SUPPORT_SHARE | SUPPORT_SHOW_ON_MAP;
574                break;
575            case LocalData.LOCAL_VIDEO:
576                supported |= SUPPORT_DELETE | SUPPORT_INFO | SUPPORT_TRIM
577                        | SUPPORT_SHARE;
578                break;
579            case LocalData.LOCAL_PHOTO_SPHERE:
580                supported |= SUPPORT_DELETE | SUPPORT_ROTATE | SUPPORT_INFO
581                        | SUPPORT_CROP | SUPPORT_SETAS | SUPPORT_EDIT
582                        | SUPPORT_SHARE | SUPPORT_SHOW_ON_MAP;
583                break;
584            case LocalData.LOCAL_360_PHOTO_SPHERE:
585                supported |= SUPPORT_DELETE | SUPPORT_ROTATE | SUPPORT_INFO
586                        | SUPPORT_CROP | SUPPORT_SETAS | SUPPORT_EDIT
587                        | SUPPORT_SHARE | SUPPORT_SHARE_PANORAMA360
588                        | SUPPORT_SHOW_ON_MAP;
589                break;
590            default:
591                break;
592        }
593
594        // In secure camera mode, we only support delete operation.
595        if (isSecureCamera()) {
596            supported &= SUPPORT_DELETE;
597        }
598
599        setMenuItemVisible(mActionBarMenu, R.id.action_delete,
600                (supported & SUPPORT_DELETE) != 0);
601        setMenuItemVisible(mActionBarMenu, R.id.action_rotate_ccw,
602                (supported & SUPPORT_ROTATE) != 0);
603        setMenuItemVisible(mActionBarMenu, R.id.action_rotate_cw,
604                (supported & SUPPORT_ROTATE) != 0);
605        setMenuItemVisible(mActionBarMenu, R.id.action_details,
606                (supported & SUPPORT_INFO) != 0);
607        setMenuItemVisible(mActionBarMenu, R.id.action_crop,
608                (supported & SUPPORT_CROP) != 0);
609        setMenuItemVisible(mActionBarMenu, R.id.action_setas,
610                (supported & SUPPORT_SETAS) != 0);
611        setMenuItemVisible(mActionBarMenu, R.id.action_edit,
612                (supported & SUPPORT_EDIT) != 0);
613        setMenuItemVisible(mActionBarMenu, R.id.action_trim,
614                (supported & SUPPORT_TRIM) != 0);
615
616        boolean standardShare = (supported & SUPPORT_SHARE) != 0;
617        boolean panoramaShare = (supported & SUPPORT_SHARE_PANORAMA360) != 0;
618        setMenuItemVisible(mActionBarMenu, R.id.action_share, standardShare);
619        setMenuItemVisible(mActionBarMenu, R.id.action_share_panorama, panoramaShare);
620
621        if (panoramaShare) {
622            // For 360 PhotoSphere, relegate standard share to the overflow menu
623            MenuItem item = mActionBarMenu.findItem(R.id.action_share);
624            if (item != null) {
625                item.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
626                item.setTitle(getResources().getString(R.string.share_as_photo));
627            }
628            // And, promote "share as panorama" to action bar
629            item = mActionBarMenu.findItem(R.id.action_share_panorama);
630            if (item != null) {
631                item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
632            }
633            setPanoramaShareIntent(currentData.getContentUri());
634        }
635        if (standardShare) {
636            if (!panoramaShare) {
637                MenuItem item = mActionBarMenu.findItem(R.id.action_share);
638                if (item != null) {
639                    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
640                    item.setTitle(getResources().getString(R.string.share));
641                }
642            }
643            setStandardShareIntent(currentData.getContentUri(), currentData.getMimeType());
644            setNfcBeamPushUri(currentData.getContentUri());
645        }
646
647        boolean itemHasLocation = currentData.getLatLong() != null;
648        setMenuItemVisible(mActionBarMenu, R.id.action_show_on_map,
649                itemHasLocation && (supported & SUPPORT_SHOW_ON_MAP) != 0);
650    }
651
652    private void setMenuItemVisible(Menu menu, int itemId, boolean visible) {
653        MenuItem item = menu.findItem(itemId);
654        if (item != null)
655            item.setVisible(visible);
656    }
657
658    private ImageTaskManager.TaskListener mStitchingListener =
659            new ImageTaskManager.TaskListener() {
660                @Override
661                public void onTaskQueued(String filePath, final Uri imageUri) {
662                    mMainHandler.post(new Runnable() {
663                        @Override
664                        public void run() {
665                            notifyNewMedia(imageUri);
666                            int dataID = mDataAdapter.findDataByContentUri(imageUri);
667                            if (dataID != -1) {
668                                // Don't allow special UI actions (swipe to
669                                // delete, for example) on in-progress data.
670                                LocalData d = mDataAdapter.getLocalData(dataID);
671                                InProgressDataWrapper newData = new InProgressDataWrapper(d);
672                                mDataAdapter.updateData(dataID, newData);
673                            }
674                        }
675                    });
676                }
677
678                @Override
679                public void onTaskDone(String filePath, final Uri imageUri) {
680                    Log.v(TAG, "onTaskDone:" + filePath);
681                    mMainHandler.post(new Runnable() {
682                        @Override
683                        public void run() {
684                            int doneID = mDataAdapter.findDataByContentUri(imageUri);
685                            int currentDataId = mFilmStripView.getCurrentId();
686
687                            if (currentDataId == doneID) {
688                                hidePanoStitchingProgress();
689                                updateStitchingProgress(0);
690                            }
691
692                            mDataAdapter.refresh(getContentResolver(), imageUri);
693                        }
694                    });
695                }
696
697                @Override
698                public void onTaskProgress(
699                        String filePath, final Uri imageUri, final int progress) {
700                    mMainHandler.post(new Runnable() {
701                        @Override
702                        public void run() {
703                            int currentDataId = mFilmStripView.getCurrentId();
704                            if (currentDataId == -1) {
705                                return;
706                            }
707                            if (imageUri.equals(
708                                    mDataAdapter.getLocalData(currentDataId).getContentUri())) {
709                                updateStitchingProgress(progress);
710                            }
711                        }
712                    });
713                }
714            };
715
716    public MediaSaveService getMediaSaveService() {
717        return mMediaSaveService;
718    }
719
720    public void notifyNewMedia(Uri uri) {
721        ContentResolver cr = getContentResolver();
722        String mimeType = cr.getType(uri);
723        if (mimeType.startsWith("video/")) {
724            sendBroadcast(new Intent(CameraUtil.ACTION_NEW_VIDEO, uri));
725            mDataAdapter.addNewVideo(cr, uri);
726        } else if (mimeType.startsWith("image/")) {
727            CameraUtil.broadcastNewPicture(this, uri);
728            mDataAdapter.addNewPhoto(cr, uri);
729        } else if (mimeType.startsWith("application/stitching-preview")) {
730            mDataAdapter.addNewPhoto(cr, uri);
731        } else {
732            android.util.Log.w(TAG, "Unknown new media with MIME type:"
733                    + mimeType + ", uri:" + uri);
734        }
735    }
736
737    private void removeData(int dataID) {
738        mDataAdapter.removeData(CameraActivity.this, dataID);
739        if (mDataAdapter.getTotalNumber() > 1) {
740            showUndoDeletionBar();
741        } else {
742            // If camera preview is the only view left in filmstrip,
743            // no need to show undo bar.
744            mPendingDeletion = true;
745            performDeletion();
746        }
747    }
748
749    private void bindMediaSaveService() {
750        Intent intent = new Intent(this, MediaSaveService.class);
751        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
752    }
753
754    private void unbindMediaSaveService() {
755        if (mConnection != null) {
756            unbindService(mConnection);
757        }
758    }
759
760    @Override
761    public boolean onCreateOptionsMenu(Menu menu) {
762        // Inflate the menu items for use in the action bar
763        MenuInflater inflater = getMenuInflater();
764        inflater.inflate(R.menu.operations, menu);
765        mActionBarMenu = menu;
766
767        // Configure the standard share action provider
768        MenuItem item = menu.findItem(R.id.action_share);
769        mStandardShareActionProvider = (ShareActionProvider) item.getActionProvider();
770        mStandardShareActionProvider.setShareHistoryFileName("standard_share_history.xml");
771        if (mStandardShareIntent != null) {
772            mStandardShareActionProvider.setShareIntent(mStandardShareIntent);
773        }
774
775        // Configure the panorama share action provider
776        item = menu.findItem(R.id.action_share_panorama);
777        mPanoramaShareActionProvider = (ShareActionProvider) item.getActionProvider();
778        mPanoramaShareActionProvider.setShareHistoryFileName("panorama_share_history.xml");
779        if (mPanoramaShareIntent != null) {
780            mPanoramaShareActionProvider.setShareIntent(mPanoramaShareIntent);
781        }
782
783        return super.onCreateOptionsMenu(menu);
784    }
785
786    @Override
787    public boolean onOptionsItemSelected(MenuItem item) {
788        int currentDataId = mFilmStripView.getCurrentId();
789        if (currentDataId < 0) {
790            return false;
791        }
792        final LocalData localData = mDataAdapter.getLocalData(currentDataId);
793
794        // Handle presses on the action bar items
795        switch (item.getItemId()) {
796            case android.R.id.home:
797                // ActionBar's Up/Home button was clicked
798                try {
799                    if (!CameraUtil.launchGallery(CameraActivity.this)) {
800                        mFilmStripView.getController().goToFirstItem();
801                    }
802                    return true;
803                } catch (ActivityNotFoundException e) {
804                    Log.w(TAG, "No activity found to handle APP_GALLERY category!");
805                    finish();
806                }
807            case R.id.action_delete:
808                removeData(currentDataId);
809                return true;
810            case R.id.action_edit:
811                launchEditor(localData);
812                return true;
813            case R.id.action_trim: {
814                // This is going to be handled by the Gallery app.
815                Intent intent = new Intent(ACTION_TRIM_VIDEO);
816                LocalData currentData = mDataAdapter.getLocalData(
817                        mFilmStripView.getCurrentId());
818                intent.setData(currentData.getContentUri());
819                // We need the file path to wrap this into a RandomAccessFile.
820                intent.putExtra(MEDIA_ITEM_PATH, currentData.getPath());
821                startActivityForResult(intent, REQ_CODE_DONT_SWITCH_TO_PREVIEW);
822                return true;
823            }
824            case R.id.action_rotate_ccw:
825                localData.rotate90Degrees(this, mDataAdapter, currentDataId, false);
826                return true;
827            case R.id.action_rotate_cw:
828                localData.rotate90Degrees(this, mDataAdapter, currentDataId, true);
829                return true;
830            case R.id.action_crop: {
831                Intent intent = new Intent(CropActivity.CROP_ACTION);
832                intent.setClass(this, CropActivity.class);
833                intent.setDataAndType(localData.getContentUri(), localData.getMimeType())
834                        .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
835                startActivityForResult(intent, REQ_CODE_DONT_SWITCH_TO_PREVIEW);
836                return true;
837            }
838            case R.id.action_setas: {
839                Intent intent = new Intent(Intent.ACTION_ATTACH_DATA)
840                        .setDataAndType(localData.getContentUri(),
841                                localData.getMimeType())
842                        .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
843                intent.putExtra("mimeType", intent.getType());
844                startActivityForResult(Intent.createChooser(
845                        intent, getString(R.string.set_as)), REQ_CODE_DONT_SWITCH_TO_PREVIEW);
846                return true;
847            }
848            case R.id.action_details:
849                (new AsyncTask<Void, Void, MediaDetails>() {
850                    @Override
851                    protected MediaDetails doInBackground(Void... params) {
852                        return localData.getMediaDetails(CameraActivity.this);
853                    }
854
855                    @Override
856                    protected void onPostExecute(MediaDetails mediaDetails) {
857                        if (mediaDetails != null) {
858                            DetailsDialog.create(CameraActivity.this, mediaDetails).show();
859                        }
860                    }
861                }).execute();
862                return true;
863            case R.id.action_show_on_map:
864                double[] latLong = localData.getLatLong();
865                if (latLong != null) {
866                    CameraUtil.showOnMap(this, latLong);
867                }
868                return true;
869            default:
870                return super.onOptionsItemSelected(item);
871        }
872    }
873
874    private boolean isCaptureIntent() {
875        if (MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent().getAction())
876                || MediaStore.ACTION_IMAGE_CAPTURE.equals(getIntent().getAction())
877                || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(getIntent().getAction())) {
878            return true;
879        } else {
880            return false;
881        }
882    }
883
884    @Override
885    public void onCreate(Bundle state) {
886        super.onCreate(state);
887        GcamHelper.init(getContentResolver());
888
889        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
890        setContentView(R.layout.camera_filmstrip);
891        mActionBar = getActionBar();
892        mActionBar.addOnMenuVisibilityListener(this);
893
894        if (ApiHelper.HAS_ROTATION_ANIMATION) {
895            setRotationAnimation();
896        }
897
898        mMainHandler = new MainHandler(getMainLooper());
899        // Check if this is in the secure camera mode.
900        Intent intent = getIntent();
901        String action = intent.getAction();
902        if (INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action)
903                || ACTION_IMAGE_CAPTURE_SECURE.equals(action)) {
904            mSecureCamera = true;
905        } else {
906            mSecureCamera = intent.getBooleanExtra(SECURE_CAMERA_EXTRA, false);
907        }
908
909        if (mSecureCamera) {
910            // Change the window flags so that secure camera can show when locked
911            Window win = getWindow();
912            WindowManager.LayoutParams params = win.getAttributes();
913            params.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
914            win.setAttributes(params);
915
916            // Filter for screen off so that we can finish secure camera activity
917            // when screen is off.
918            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
919            registerReceiver(mScreenOffReceiver, filter);
920            // TODO: This static screen off event receiver is a workaround to the
921            // double onResume() invocation (onResume->onPause->onResume). We should
922            // find a better solution to this.
923            if (sScreenOffReceiver == null) {
924                sScreenOffReceiver = new ScreenOffReceiver();
925                registerReceiver(sScreenOffReceiver, filter);
926            }
927        }
928        mAboveFilmstripControlLayout =
929                (FrameLayout) findViewById(R.id.camera_above_filmstrip_layout);
930        mAboveFilmstripControlLayout.setFitsSystemWindows(true);
931        // Hide action bar first since we are in full screen mode first, and
932        // switch the system UI to lights-out mode.
933        this.setSystemBarsVisibility(false);
934        mPanoramaManager = AppManagerFactory.getInstance(this)
935                .getPanoramaStitchingManager();
936        mPanoramaManager.addTaskListener(mStitchingListener);
937        LayoutInflater inflater = getLayoutInflater();
938        View rootLayout = inflater.inflate(R.layout.camera, null, false);
939        mCameraModuleRootView = rootLayout.findViewById(R.id.camera_app_root);
940        mPanoStitchingPanel = findViewById(R.id.pano_stitching_progress_panel);
941        mBottomProgress = (ProgressBar) findViewById(R.id.pano_stitching_progress_bar);
942        mCameraPreviewData = new CameraPreviewData(rootLayout,
943                FilmStripView.ImageData.SIZE_FULL,
944                FilmStripView.ImageData.SIZE_FULL);
945        // Put a CameraPreviewData at the first position.
946        mWrappedDataAdapter = new FixedFirstDataAdapter(
947                new CameraDataAdapter(new ColorDrawable(
948                        getResources().getColor(R.color.photo_placeholder))),
949                mCameraPreviewData);
950        mFilmStripView = (FilmStripView) findViewById(R.id.filmstrip_view);
951        mFilmStripView.setViewGap(
952                getResources().getDimensionPixelSize(R.dimen.camera_film_strip_gap));
953        mPanoramaViewHelper = new PanoramaViewHelper(this);
954        mPanoramaViewHelper.onCreate();
955        mFilmStripView.setPanoramaViewHelper(mPanoramaViewHelper);
956        // Set up the camera preview first so the preview shows up ASAP.
957        mFilmStripView.setListener(mFilmStripListener);
958
959        int moduleIndex = -1;
960        if (MediaStore.INTENT_ACTION_VIDEO_CAMERA.equals(getIntent().getAction())
961                || MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent().getAction())) {
962            moduleIndex = ModuleSwitcher.VIDEO_MODULE_INDEX;
963        } else if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(getIntent().getAction())
964                || MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(getIntent()
965                        .getAction())
966                || MediaStore.ACTION_IMAGE_CAPTURE.equals(getIntent().getAction())
967                || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(getIntent().getAction())) {
968            moduleIndex = ModuleSwitcher.PHOTO_MODULE_INDEX;
969        } else {
970            // If the activity has not been started using an explicit intent,
971            // read the module index from the last time the user changed modes
972            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
973            moduleIndex = prefs.getInt(CameraSettings.KEY_STARTUP_MODULE_INDEX, -1);
974            if ((moduleIndex == ModuleSwitcher.GCAM_MODULE_INDEX &&
975                    !GcamHelper.hasGcamCapture()) || moduleIndex < 0) {
976                moduleIndex = ModuleSwitcher.PHOTO_MODULE_INDEX;
977            }
978        }
979
980        mOrientationListener = new MyOrientationEventListener(this);
981        setModuleFromIndex(moduleIndex);
982        mCurrentModule.init(this, mCameraModuleRootView);
983
984        if (!mSecureCamera) {
985            mDataAdapter = mWrappedDataAdapter;
986            mFilmStripView.setDataAdapter(mDataAdapter);
987            if (!isCaptureIntent()) {
988                mDataAdapter.requestLoad(getContentResolver());
989            }
990        } else {
991            // Put a lock placeholder as the last image by setting its date to
992            // 0.
993            ImageView v = (ImageView) getLayoutInflater().inflate(
994                    R.layout.secure_album_placeholder, null);
995            v.setOnClickListener(new View.OnClickListener() {
996                @Override
997                public void onClick(View view) {
998                    CameraUtil.launchGallery(CameraActivity.this);
999                    finish();
1000                }
1001            });
1002            mDataAdapter = new FixedLastDataAdapter(
1003                    mWrappedDataAdapter,
1004                    new SimpleViewData(
1005                            v,
1006                            v.getDrawable().getIntrinsicWidth(),
1007                            v.getDrawable().getIntrinsicHeight(),
1008                            0, 0));
1009            // Flush out all the original data.
1010            mDataAdapter.flush();
1011            mFilmStripView.setDataAdapter(mDataAdapter);
1012        }
1013
1014        setupNfcBeamPush();
1015
1016        mLocalImagesObserver = new LocalMediaObserver();
1017        mLocalVideosObserver = new LocalMediaObserver();
1018
1019        getContentResolver().registerContentObserver(
1020                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true,
1021                mLocalImagesObserver);
1022        getContentResolver().registerContentObserver(
1023                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, true,
1024                mLocalVideosObserver);
1025    }
1026
1027    private void setRotationAnimation() {
1028        int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE;
1029        rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
1030        Window win = getWindow();
1031        WindowManager.LayoutParams winParams = win.getAttributes();
1032        winParams.rotationAnimation = rotationAnimation;
1033        win.setAttributes(winParams);
1034    }
1035
1036    @Override
1037    public void onUserInteraction() {
1038        super.onUserInteraction();
1039        mCurrentModule.onUserInteraction();
1040    }
1041
1042    @Override
1043    public boolean dispatchTouchEvent(MotionEvent ev) {
1044        boolean result = super.dispatchTouchEvent(ev);
1045        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
1046            // Real deletion is postponed until the next user interaction after
1047            // the gesture that triggers deletion. Until real deletion is performed,
1048            // users can click the undo button to bring back the image that they
1049            // chose to delete.
1050            if (mPendingDeletion && !mIsUndoingDeletion) {
1051                 performDeletion();
1052            }
1053        }
1054        return result;
1055    }
1056
1057    @Override
1058    public void onPause() {
1059        // Delete photos that are pending deletion
1060        performDeletion();
1061        mOrientationListener.disable();
1062        mCurrentModule.onPauseBeforeSuper();
1063        super.onPause();
1064        mCurrentModule.onPauseAfterSuper();
1065
1066        mLocalImagesObserver.setActivityPaused(true);
1067        mLocalVideosObserver.setActivityPaused(true);
1068    }
1069
1070    @Override
1071    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1072        if (requestCode == REQ_CODE_DONT_SWITCH_TO_PREVIEW) {
1073            mResetToPreviewOnResume = false;
1074        } else {
1075            super.onActivityResult(requestCode, resultCode, data);
1076        }
1077    }
1078
1079    @Override
1080    public void onResume() {
1081        // TODO: Handle this in OrientationManager.
1082        // Auto-rotate off
1083        if (Settings.System.getInt(getContentResolver(),
1084                Settings.System.ACCELEROMETER_ROTATION, 0) == 0) {
1085            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
1086            mAutoRotateScreen = false;
1087        } else {
1088            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
1089            mAutoRotateScreen = true;
1090        }
1091        mOrientationListener.enable();
1092        mCurrentModule.onResumeBeforeSuper();
1093        super.onResume();
1094        mCurrentModule.onResumeAfterSuper();
1095
1096        setSwipingEnabled(true);
1097
1098        if (mResetToPreviewOnResume) {
1099            // Go to the preview on resume.
1100            mFilmStripView.getController().goToFirstItem();
1101        }
1102        // Default is showing the preview, unless disabled by explicitly
1103        // starting an activity we want to return from to the filmstrip rather
1104        // than the preview.
1105        mResetToPreviewOnResume = true;
1106
1107        if (mLocalVideosObserver.isMediaDataChangedDuringPause()
1108                || mLocalImagesObserver.isMediaDataChangedDuringPause()) {
1109            mDataAdapter.requestLoad(getContentResolver());
1110        }
1111        mLocalImagesObserver.setActivityPaused(false);
1112        mLocalVideosObserver.setActivityPaused(false);
1113    }
1114
1115    @Override
1116    public void onStart() {
1117        super.onStart();
1118        bindMediaSaveService();
1119        mPanoramaViewHelper.onStart();
1120    }
1121
1122    @Override
1123    protected void onStop() {
1124        super.onStop();
1125        mPanoramaViewHelper.onStop();
1126        unbindMediaSaveService();
1127    }
1128
1129    @Override
1130    public void onDestroy() {
1131        if (mSecureCamera) {
1132            unregisterReceiver(mScreenOffReceiver);
1133        }
1134        getContentResolver().unregisterContentObserver(mLocalImagesObserver);
1135        getContentResolver().unregisterContentObserver(mLocalVideosObserver);
1136
1137        super.onDestroy();
1138    }
1139
1140    @Override
1141    public void onConfigurationChanged(Configuration config) {
1142        super.onConfigurationChanged(config);
1143        mCurrentModule.onConfigurationChanged(config);
1144    }
1145
1146    @Override
1147    public boolean onKeyDown(int keyCode, KeyEvent event) {
1148        if (mFilmStripView.inCameraFullscreen()) {
1149            if (mCurrentModule.onKeyDown(keyCode, event)) {
1150                return true;
1151            }
1152            // Prevent software keyboard or voice search from showing up.
1153            if (keyCode == KeyEvent.KEYCODE_SEARCH
1154                    || keyCode == KeyEvent.KEYCODE_MENU) {
1155                if (event.isLongPress()) {
1156                    return true;
1157                }
1158            }
1159        }
1160
1161        return super.onKeyDown(keyCode, event);
1162    }
1163
1164    @Override
1165    public boolean onKeyUp(int keyCode, KeyEvent event) {
1166        if (mFilmStripView.inCameraFullscreen() && mCurrentModule.onKeyUp(keyCode, event)) {
1167            return true;
1168        }
1169        return super.onKeyUp(keyCode, event);
1170    }
1171
1172    @Override
1173    public void onBackPressed() {
1174        if (!mFilmStripView.inCameraFullscreen()) {
1175            mFilmStripView.getController().goToFirstItem();
1176        } else if (!mCurrentModule.onBackPressed()) {
1177            super.onBackPressed();
1178        }
1179    }
1180
1181    public boolean isAutoRotateScreen() {
1182        return mAutoRotateScreen;
1183    }
1184
1185    protected void updateStorageSpace() {
1186        mStorageSpaceBytes = Storage.getAvailableSpace();
1187    }
1188
1189    protected long getStorageSpaceBytes() {
1190        return mStorageSpaceBytes;
1191    }
1192
1193    protected void updateStorageSpaceAndHint() {
1194        updateStorageSpace();
1195        updateStorageHint(mStorageSpaceBytes);
1196    }
1197
1198    protected void updateStorageHint(long storageSpace) {
1199        String message = null;
1200        if (storageSpace == Storage.UNAVAILABLE) {
1201            message = getString(R.string.no_storage);
1202        } else if (storageSpace == Storage.PREPARING) {
1203            message = getString(R.string.preparing_sd);
1204        } else if (storageSpace == Storage.UNKNOWN_SIZE) {
1205            message = getString(R.string.access_sd_fail);
1206        } else if (storageSpace <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
1207            message = getString(R.string.spaceIsLow_content);
1208        }
1209
1210        if (message != null) {
1211            if (mStorageHint == null) {
1212                mStorageHint = OnScreenHint.makeText(this, message);
1213            } else {
1214                mStorageHint.setText(message);
1215            }
1216            mStorageHint.show();
1217        } else if (mStorageHint != null) {
1218            mStorageHint.cancel();
1219            mStorageHint = null;
1220        }
1221    }
1222
1223    protected void setResultEx(int resultCode) {
1224        mResultCodeForTesting = resultCode;
1225        setResult(resultCode);
1226    }
1227
1228    protected void setResultEx(int resultCode, Intent data) {
1229        mResultCodeForTesting = resultCode;
1230        mResultDataForTesting = data;
1231        setResult(resultCode, data);
1232    }
1233
1234    public int getResultCode() {
1235        return mResultCodeForTesting;
1236    }
1237
1238    public Intent getResultData() {
1239        return mResultDataForTesting;
1240    }
1241
1242    public boolean isSecureCamera() {
1243        return mSecureCamera;
1244    }
1245
1246    @Override
1247    public void onModuleSelected(int moduleIndex) {
1248        if (mCurrentModuleIndex == moduleIndex) {
1249            return;
1250        }
1251
1252        CameraHolder.instance().keep();
1253        closeModule(mCurrentModule);
1254        setModuleFromIndex(moduleIndex);
1255
1256        openModule(mCurrentModule);
1257        mCurrentModule.onOrientationChanged(mLastRawOrientation);
1258        if (mMediaSaveService != null) {
1259            mCurrentModule.onMediaSaveServiceConnected(mMediaSaveService);
1260        }
1261
1262        // Store the module index so we can use it the next time the Camera
1263        // starts up.
1264        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
1265        prefs.edit().putInt(CameraSettings.KEY_STARTUP_MODULE_INDEX, moduleIndex).apply();
1266    }
1267
1268    /**
1269     * Sets the mCurrentModuleIndex, creates a new module instance for the given
1270     * index an sets it as mCurrentModule.
1271     */
1272    private void setModuleFromIndex(int moduleIndex) {
1273        mCurrentModuleIndex = moduleIndex;
1274        switch (moduleIndex) {
1275            case ModuleSwitcher.VIDEO_MODULE_INDEX:
1276                mCurrentModule = new VideoModule();
1277                break;
1278
1279            case ModuleSwitcher.PHOTO_MODULE_INDEX:
1280                mCurrentModule = new PhotoModule();
1281                break;
1282
1283            case ModuleSwitcher.WIDE_ANGLE_PANO_MODULE_INDEX:
1284                mCurrentModule = new WideAnglePanoramaModule();
1285                break;
1286
1287            case ModuleSwitcher.LIGHTCYCLE_MODULE_INDEX:
1288                mCurrentModule = PhotoSphereHelper.createPanoramaModule();
1289                break;
1290            case ModuleSwitcher.GCAM_MODULE_INDEX:
1291                // Force immediate release of Camera instance
1292                CameraHolder.instance().strongRelease();
1293                mCurrentModule = GcamHelper.createGcamModule();
1294                break;
1295            default:
1296                // Fall back to photo mode.
1297                mCurrentModule = new PhotoModule();
1298                mCurrentModuleIndex = ModuleSwitcher.PHOTO_MODULE_INDEX;
1299                break;
1300        }
1301    }
1302
1303    /**
1304     * Launches an ACTION_EDIT intent for the given local data item.
1305     */
1306    public void launchEditor(LocalData data) {
1307        Intent intent = new Intent(Intent.ACTION_EDIT)
1308                .setDataAndType(data.getContentUri(), data.getMimeType())
1309                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1310        startActivityForResult(Intent.createChooser(intent, null),
1311                REQ_CODE_DONT_SWITCH_TO_PREVIEW);
1312    }
1313
1314    /**
1315     * Launch the tiny planet editor.
1316     *
1317     * @param data the data must be a 360 degree stereographically mapped
1318     *            panoramic image. It will not be modified, instead a new item
1319     *            with the result will be added to the filmstrip.
1320     */
1321    public void launchTinyPlanetEditor(LocalData data) {
1322        TinyPlanetFragment fragment = new TinyPlanetFragment();
1323        Bundle bundle = new Bundle();
1324        bundle.putString(TinyPlanetFragment.ARGUMENT_URI, data.getContentUri().toString());
1325        bundle.putString(TinyPlanetFragment.ARGUMENT_TITLE, data.getTitle());
1326        fragment.setArguments(bundle);
1327        fragment.show(getFragmentManager(), "tiny_planet");
1328    }
1329
1330    private void openModule(CameraModule module) {
1331        module.init(this, mCameraModuleRootView);
1332        module.onResumeBeforeSuper();
1333        module.onResumeAfterSuper();
1334    }
1335
1336    private void closeModule(CameraModule module) {
1337        module.onPauseBeforeSuper();
1338        module.onPauseAfterSuper();
1339        ((ViewGroup) mCameraModuleRootView).removeAllViews();
1340    }
1341
1342    private void performDeletion() {
1343        if (!mPendingDeletion) {
1344            return;
1345        }
1346        hideUndoDeletionBar(false);
1347        mDataAdapter.executeDeletion(CameraActivity.this);
1348        updateActionBarMenu(mFilmStripView.getCurrentId());
1349    }
1350
1351    public void showUndoDeletionBar() {
1352        if (mPendingDeletion) {
1353            performDeletion();
1354        }
1355        Log.v(TAG, "showing undo bar");
1356        mPendingDeletion = true;
1357        if (mUndoDeletionBar == null) {
1358            ViewGroup v = (ViewGroup) getLayoutInflater().inflate(
1359                    R.layout.undo_bar, mAboveFilmstripControlLayout, true);
1360            mUndoDeletionBar = (ViewGroup) v.findViewById(R.id.camera_undo_deletion_bar);
1361            View button = mUndoDeletionBar.findViewById(R.id.camera_undo_deletion_button);
1362            button.setOnClickListener(new View.OnClickListener() {
1363                @Override
1364                public void onClick(View view) {
1365                    mDataAdapter.undoDataRemoval();
1366                    hideUndoDeletionBar(true);
1367                }
1368            });
1369            // Setting undo bar clickable to avoid touch events going through
1370            // the bar to the buttons (eg. edit button, etc) underneath the bar.
1371            mUndoDeletionBar.setClickable(true);
1372            // When there is user interaction going on with the undo button, we
1373            // do not want to hide the undo bar.
1374            button.setOnTouchListener(new View.OnTouchListener() {
1375                @Override
1376                public boolean onTouch(View v, MotionEvent event) {
1377                    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
1378                        mIsUndoingDeletion = true;
1379                    } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
1380                        mIsUndoingDeletion =false;
1381                    }
1382                    return false;
1383                }
1384            });
1385        }
1386        mUndoDeletionBar.setAlpha(0f);
1387        mUndoDeletionBar.setVisibility(View.VISIBLE);
1388        mUndoDeletionBar.animate().setDuration(200).alpha(1f).setListener(null).start();
1389    }
1390
1391    private void hideUndoDeletionBar(boolean withAnimation) {
1392        Log.v(TAG, "Hiding undo deletion bar");
1393        mPendingDeletion = false;
1394        if (mUndoDeletionBar != null) {
1395            if (withAnimation) {
1396                mUndoDeletionBar.animate()
1397                        .setDuration(200)
1398                        .alpha(0f)
1399                        .setListener(new Animator.AnimatorListener() {
1400                            @Override
1401                            public void onAnimationStart(Animator animation) {
1402                                // Do nothing.
1403                            }
1404
1405                            @Override
1406                            public void onAnimationEnd(Animator animation) {
1407                                mUndoDeletionBar.setVisibility(View.GONE);
1408                            }
1409
1410                            @Override
1411                            public void onAnimationCancel(Animator animation) {
1412                                // Do nothing.
1413                            }
1414
1415                            @Override
1416                            public void onAnimationRepeat(Animator animation) {
1417                                // Do nothing.
1418                            }
1419                        })
1420                        .start();
1421            } else {
1422                mUndoDeletionBar.setVisibility(View.GONE);
1423            }
1424        }
1425    }
1426
1427    @Override
1428    public void onShowSwitcherPopup() {
1429    }
1430
1431    /**
1432     * Enable/disable swipe-to-filmstrip. Will always disable swipe if in
1433     * capture intent.
1434     *
1435     * @param enable {@code true} to enable swipe.
1436     */
1437    public void setSwipingEnabled(boolean enable) {
1438        if (isCaptureIntent()) {
1439            mCameraPreviewData.lockPreview(true);
1440        } else {
1441            mCameraPreviewData.lockPreview(!enable);
1442        }
1443    }
1444
1445    // Accessor methods for getting latency times used in performance testing
1446    public long getAutoFocusTime() {
1447        return (mCurrentModule instanceof PhotoModule) ?
1448                ((PhotoModule) mCurrentModule).mAutoFocusTime : -1;
1449    }
1450
1451    public long getShutterLag() {
1452        return (mCurrentModule instanceof PhotoModule) ?
1453                ((PhotoModule) mCurrentModule).mShutterLag : -1;
1454    }
1455
1456    public long getShutterToPictureDisplayedTime() {
1457        return (mCurrentModule instanceof PhotoModule) ?
1458                ((PhotoModule) mCurrentModule).mShutterToPictureDisplayedTime : -1;
1459    }
1460
1461    public long getPictureDisplayedToJpegCallbackTime() {
1462        return (mCurrentModule instanceof PhotoModule) ?
1463                ((PhotoModule) mCurrentModule).mPictureDisplayedToJpegCallbackTime : -1;
1464    }
1465
1466    public long getJpegCallbackFinishTime() {
1467        return (mCurrentModule instanceof PhotoModule) ?
1468                ((PhotoModule) mCurrentModule).mJpegCallbackFinishTime : -1;
1469    }
1470
1471    public long getCaptureStartTime() {
1472        return (mCurrentModule instanceof PhotoModule) ?
1473                ((PhotoModule) mCurrentModule).mCaptureStartTime : -1;
1474    }
1475
1476    public boolean isRecording() {
1477        return (mCurrentModule instanceof VideoModule) ?
1478                ((VideoModule) mCurrentModule).isRecording() : false;
1479    }
1480
1481    public CameraOpenErrorCallback getCameraOpenErrorCallback() {
1482        return mCameraOpenErrorCallback;
1483    }
1484
1485    // For debugging purposes only.
1486    public CameraModule getCurrentModule() {
1487        return mCurrentModule;
1488    }
1489}
1490