CameraActivity.java revision ae4ca300c875a5ac67c24c48733a88e138a31b48
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.camera; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.res.Configuration; 22import android.graphics.drawable.Drawable; 23import android.os.Bundle; 24import android.provider.MediaStore; 25import android.view.KeyEvent; 26import android.view.LayoutInflater; 27import android.view.MotionEvent; 28import android.view.OrientationEventListener; 29import android.view.View; 30import android.view.View.OnClickListener; 31import android.view.ViewGroup; 32import android.widget.FrameLayout; 33 34import com.android.camera.ui.CameraSwitcher; 35import com.android.camera.ui.RotateImageView; 36import com.android.gallery3d.util.LightCycleHelper; 37 38public class CameraActivity extends ActivityBase 39 implements CameraSwitcher.CameraSwitchListener { 40 public static final int PHOTO_MODULE_INDEX = 0; 41 public static final int VIDEO_MODULE_INDEX = 1; 42 public static final int PANORAMA_MODULE_INDEX = 2; 43 public static final int LIGHTCYCLE_MODULE_INDEX = 3; 44 45 CameraModule mCurrentModule; 46 private FrameLayout mFrame; 47 private ShutterButton mShutter; 48 private FrameLayout mMenu; 49 private CameraSwitcher mSwitcher; 50 private View mShutterSwitcher; 51 private View mBlocker; 52 private Drawable[] mDrawables; 53 private int mCurrentModuleIndex; 54 private MenuListener mMenuListener; 55 private MotionEvent mDown; 56 57 private MyOrientationEventListener mOrientationListener; 58 // The degrees of the device rotated clockwise from its natural orientation. 59 private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; 60 // The orientation compensation for icons. Eg: if the value 61 // is 90, the UI components should be rotated 90 degrees counter-clockwise. 62 private int mOrientationCompensation = 0; 63 64 private static final String TAG = "CAM_activity"; 65 66 private static final int[] DRAW_IDS = { 67 R.drawable.ic_switch_camera, 68 R.drawable.ic_switch_video, 69 R.drawable.ic_switch_pan, 70 com.android.gallery3d.R.drawable.ic_menu_photosphere 71 }; 72 73 public interface MenuListener { 74 public void onMenuClicked(); 75 } 76 77 @Override 78 public void onCreate(Bundle state) { 79 super.onCreate(state); 80 setContentView(R.layout.camera_main); 81 mFrame =(FrameLayout) findViewById(R.id.main_content); 82 mDrawables = new Drawable[DRAW_IDS.length]; 83 for (int i = 0; i < DRAW_IDS.length; i++) { 84 mDrawables[i] = getResources().getDrawable(DRAW_IDS[i]); 85 } 86 init(); 87 mSwitcher.setSwitchListener(this); 88 if (MediaStore.INTENT_ACTION_VIDEO_CAMERA.equals(getIntent().getAction()) 89 || MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent().getAction())) { 90 mCurrentModule = new VideoModule(); 91 mCurrentModuleIndex = VIDEO_MODULE_INDEX; 92 } else { 93 mCurrentModule = new PhotoModule(); 94 mCurrentModuleIndex = PHOTO_MODULE_INDEX; 95 } 96 mCurrentModule.init(this, mFrame, true); 97 mSwitcher.setCurrentIndex(mCurrentModuleIndex); 98 mOrientationListener = new MyOrientationEventListener(this); 99 } 100 101 public void init() { 102 mBlocker = findViewById(R.id.blocker); 103 mShutterSwitcher = findViewById(R.id.camera_shutter_switcher); 104 mShutter = (ShutterButton) findViewById(R.id.shutter_button); 105 mMenu = (FrameLayout) findViewById(R.id.menu); 106 mSwitcher = (CameraSwitcher) findViewById(R.id.camera_switcher); 107 mSwitcher.setDrawIds(DRAW_IDS); 108 mMenu.setOnClickListener(new OnClickListener() { 109 @Override 110 public void onClick(View v) { 111 if (mMenuListener != null) { 112 mMenuListener.onMenuClicked(); 113 } 114 } 115 }); 116 int[] drawids = new int[LightCycleHelper.hasLightCycleCapture(this) 117 ? DRAW_IDS.length : DRAW_IDS.length - 1]; 118 int ix = 0; 119 for (int i = 0; i < mDrawables.length; i++) { 120 if (i == LIGHTCYCLE_MODULE_INDEX && !LightCycleHelper.hasLightCycleCapture(this)) { 121 continue; // not enabled, so don't add to UI 122 } 123 drawids[ix++] = DRAW_IDS[i]; 124 } 125 mSwitcher.setDrawIds(drawids); 126 } 127 128 public void setMenuListener(MenuListener listener) { 129 mMenuListener = listener; 130 if (mMenu != null) { 131 mMenu.setVisibility((listener != null) ? View.VISIBLE : View.GONE); 132 } 133 } 134 135 private class MyOrientationEventListener 136 extends OrientationEventListener { 137 public MyOrientationEventListener(Context context) { 138 super(context); 139 } 140 141 @Override 142 public void onOrientationChanged(int orientation) { 143 // We keep the last known orientation. So if the user first orient 144 // the camera then point the camera to floor or sky, we still have 145 // the correct orientation. 146 if (orientation == ORIENTATION_UNKNOWN) return; 147 mOrientation = Util.roundOrientation(orientation, mOrientation); 148 // When the screen is unlocked, display rotation may change. Always 149 // calculate the up-to-date orientationCompensation. 150 int orientationCompensation = 151 (mOrientation + Util.getDisplayRotation(CameraActivity.this)) % 360; 152 // Rotate camera mode icons in the switcher 153 if (mOrientationCompensation != orientationCompensation) { 154 mOrientationCompensation = orientationCompensation; 155 if (mSwitcher != null) { 156 mSwitcher.setOrientation(mOrientationCompensation, true); 157 } 158 } 159 mCurrentModule.onOrientationChanged(orientation); 160 } 161 } 162 163 @Override 164 public void onCameraSelected(int i) { 165 if (mPaused) return; 166 if (i != mCurrentModuleIndex) { 167 mPaused = true; 168 boolean wasPanorama = isPanoramaActivity(); 169 closeModule(mCurrentModule); 170 mCurrentModuleIndex = i; 171 mMenuListener = null; 172 switch (i) { 173 case VIDEO_MODULE_INDEX: 174 mCurrentModule = new VideoModule(); 175 break; 176 case PHOTO_MODULE_INDEX: 177 mCurrentModule = new PhotoModule(); 178 break; 179 case PANORAMA_MODULE_INDEX: 180 mCurrentModule = new PanoramaModule(); 181 break; 182 case LIGHTCYCLE_MODULE_INDEX: 183 mCurrentModule = LightCycleHelper.createPanoramaModule(); 184 break; 185 } 186 openModule(mCurrentModule, wasPanorama); 187 mCurrentModule.onOrientationChanged(mOrientation); 188 } 189 } 190 191 private void openModule(CameraModule module, boolean wasPanorama) { 192 module.init(this, mFrame, !(wasPanorama || isPanoramaActivity())); 193 mPaused = false; 194 module.onResumeBeforeSuper(); 195 module.onResumeAfterSuper(); 196 } 197 198 private void closeModule(CameraModule module) { 199 module.onPauseBeforeSuper(); 200 module.onPauseAfterSuper(); 201 mFrame.removeAllViews(); 202 } 203 204 public ShutterButton getShutterButton() { 205 return mShutter; 206 } 207 208 public void hideUI() { 209 hideSwitcher(); 210 mShutter.setVisibility(View.GONE); 211 } 212 213 public void showUI() { 214 showSwitcher(); 215 mShutter.setVisibility(View.VISIBLE); 216 } 217 218 public void hideSwitcher() { 219 mSwitcher.setVisibility(View.GONE); 220 mMenu.setVisibility(View.GONE); 221 mBlocker.setVisibility(View.GONE); 222 } 223 224 public void showSwitcher() { 225 if (mCurrentModule.needsSwitcher()) { 226 mSwitcher.setVisibility(View.VISIBLE); 227 mBlocker.setVisibility(View.VISIBLE); 228 if (mMenuListener != null) { 229 mMenu.setVisibility(View.VISIBLE); 230 } 231 } 232 } 233 234 @Override 235 public void onConfigurationChanged(Configuration config) { 236 super.onConfigurationChanged(config); 237 238 ViewGroup appRoot = (ViewGroup) findViewById(R.id.content); 239 // remove old switcher, shutter and shutter icon 240 View cameraControlsView = findViewById(R.id.camera_shutter_switcher); 241 appRoot.removeView(cameraControlsView); 242 243 // create new layout with the current orientation 244 LayoutInflater inflater = getLayoutInflater(); 245 inflater.inflate(R.layout.camera_shutter_switcher, appRoot); 246 init(); 247 248 mSwitcher.setImageResource(DRAW_IDS[mCurrentModuleIndex]); 249 mSwitcher.setSwitchListener(this); 250 if (mShowCameraAppView) { 251 showUI(); 252 } else { 253 hideUI(); 254 } 255 mCurrentModule.onConfigurationChanged(config); 256 } 257 258 @Override 259 public void onPause() { 260 mPaused = true; 261 mOrientationListener.disable(); 262 mCurrentModule.onPauseBeforeSuper(); 263 super.onPause(); 264 mCurrentModule.onPauseAfterSuper(); 265 } 266 267 @Override 268 public void onResume() { 269 mPaused = false; 270 mOrientationListener.enable(); 271 mCurrentModule.onResumeBeforeSuper(); 272 super.onResume(); 273 mCurrentModule.onResumeAfterSuper(); 274 } 275 276 @Override 277 protected void onFullScreenChanged(boolean full) { 278 if (full) { 279 showUI(); 280 } else { 281 hideUI(); 282 } 283 super.onFullScreenChanged(full); 284 mCurrentModule.onFullScreenChanged(full); 285 } 286 287 @Override 288 protected void onStop() { 289 super.onStop(); 290 mCurrentModule.onStop(); 291 } 292 293 @Override 294 protected void onNewIntent(Intent intent) { 295 super.onNewIntent(intent); 296 getStateManager().clearActivityResult(); 297 } 298 299 @Override 300 protected void installIntentFilter() { 301 super.installIntentFilter(); 302 mCurrentModule.installIntentFilter(); 303 } 304 305 @Override 306 protected void onActivityResult( 307 int requestCode, int resultCode, Intent data) { 308 super.onActivityResult(requestCode, resultCode, data); 309 mCurrentModule.onActivityResult(requestCode, resultCode, data); 310 } 311 312 // Preview area is touched. Handle touch focus. 313 @Override 314 protected void onSingleTapUp(View view, int x, int y) { 315 mCurrentModule.onSingleTapUp(view, x, y); 316 } 317 318 @Override 319 public void onBackPressed() { 320 if (!mCurrentModule.onBackPressed()) { 321 super.onBackPressed(); 322 } 323 } 324 325 @Override 326 public boolean onKeyDown(int keyCode, KeyEvent event) { 327 return mCurrentModule.onKeyDown(keyCode, event) 328 || super.onKeyDown(keyCode, event); 329 } 330 331 @Override 332 public boolean onKeyUp(int keyCode, KeyEvent event) { 333 return mCurrentModule.onKeyUp(keyCode, event) 334 || super.onKeyUp(keyCode, event); 335 } 336 337 public void cancelActivityTouchHandling() { 338 if (mDown != null) { 339 MotionEvent cancel = MotionEvent.obtain(mDown); 340 cancel.setAction(MotionEvent.ACTION_CANCEL); 341 super.dispatchTouchEvent(cancel); 342 } 343 } 344 345 @Override 346 public boolean dispatchTouchEvent(MotionEvent m) { 347 if (m.getActionMasked() == MotionEvent.ACTION_DOWN) { 348 mDown = m; 349 } 350 if ((mSwitcher != null) && mSwitcher.showsPopup() && !mSwitcher.isInsidePopup(m)) { 351 return mSwitcher.onTouch(null, m); 352 } else { 353 return mShutterSwitcher.dispatchTouchEvent(m) 354 || mCurrentModule.dispatchTouchEvent(m); 355 } 356 } 357 358 public boolean superDispatchTouchEvent(MotionEvent m) { 359 return super.dispatchTouchEvent(m); 360 } 361 362 // Preview texture has been copied. Now camera can be released and the 363 // animation can be started. 364 @Override 365 public void onPreviewTextureCopied() { 366 mCurrentModule.onPreviewTextureCopied(); 367 } 368 369 @Override 370 public void onCaptureTextureCopied() { 371 mCurrentModule.onCaptureTextureCopied(); 372 } 373 374 @Override 375 public void onUserInteraction() { 376 super.onUserInteraction(); 377 mCurrentModule.onUserInteraction(); 378 } 379 380 @Override 381 protected boolean updateStorageHintOnResume() { 382 return mCurrentModule.updateStorageHintOnResume(); 383 } 384 385 @Override 386 public void updateCameraAppView() { 387 super.updateCameraAppView(); 388 mCurrentModule.updateCameraAppView(); 389 } 390 391 @Override 392 public boolean isPanoramaActivity() { 393 return (mCurrentModule instanceof PanoramaModule); 394 } 395 396 // Accessor methods for getting latency times used in performance testing 397 public long getAutoFocusTime() { 398 return (mCurrentModule instanceof PhotoModule) ? 399 ((PhotoModule)mCurrentModule).mAutoFocusTime : -1; 400 } 401 402 public long getShutterLag() { 403 return (mCurrentModule instanceof PhotoModule) ? 404 ((PhotoModule)mCurrentModule).mShutterLag : -1; 405 } 406 407 public long getShutterToPictureDisplayedTime() { 408 return (mCurrentModule instanceof PhotoModule) ? 409 ((PhotoModule)mCurrentModule).mShutterToPictureDisplayedTime : -1; 410 } 411 412 public long getPictureDisplayedToJpegCallbackTime() { 413 return (mCurrentModule instanceof PhotoModule) ? 414 ((PhotoModule)mCurrentModule).mPictureDisplayedToJpegCallbackTime : -1; 415 } 416 417 public long getJpegCallbackFinishTime() { 418 return (mCurrentModule instanceof PhotoModule) ? 419 ((PhotoModule)mCurrentModule).mJpegCallbackFinishTime : -1; 420 } 421 422 public long getCaptureStartTime() { 423 return (mCurrentModule instanceof PhotoModule) ? 424 ((PhotoModule)mCurrentModule).mCaptureStartTime : -1; 425 } 426} 427