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