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