CameraActivity.java revision b232fdf0a6e2b488e74c0ad505c4eddc3cfb78e0
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.Intent; 20import android.content.res.Configuration; 21import android.graphics.drawable.Drawable; 22import android.os.Bundle; 23import android.provider.MediaStore; 24import android.view.KeyEvent; 25import android.view.MotionEvent; 26import android.view.View; 27import android.view.View.OnClickListener; 28import android.view.ViewGroup.LayoutParams; 29import android.widget.FrameLayout; 30import android.widget.ImageView; 31 32import com.android.camera.ui.CameraSwitcher; 33import com.android.gallery3d.util.LightCycleHelper; 34 35import java.util.HashSet; 36 37public class CameraActivity extends ActivityBase 38 implements CameraSwitcher.CameraSwitchListener { 39 public static final int VIDEO_MODULE_INDEX = 0; 40 public static final int PHOTO_MODULE_INDEX = 1; 41 public static final int PANORAMA_MODULE_INDEX = 2; 42 public static final int LIGHTCYCLE_MODULE_INDEX = 3; 43 44 CameraModule mCurrentModule; 45 private FrameLayout mFrame; 46 private ShutterButton mShutter; 47 private CameraSwitcher mSwitcher; 48 private Drawable[] mDrawables; 49 private int mSelectedModule; 50 private View mEventGroup; 51 private HashSet<View> mDispatched; 52 53 private static final String TAG = "CAM_activity"; 54 55 private static final int[] DRAW_IDS = { 56 R.drawable.ic_switch_video_holo_light, 57 R.drawable.ic_switch_camera_holo_light, 58 R.drawable.ic_switch_pan_holo_light, 59 R.drawable.ic_switch_photo_pan_holo_light 60 }; 61 62 @Override 63 public void onCreate(Bundle state) { 64 super.onCreate(state); 65 mDispatched = new HashSet<View>(); 66 setContentView(R.layout.camera_main); 67 mFrame =(FrameLayout) findViewById(R.id.main_content); 68 mShutter = (ShutterButton) findViewById(R.id.shutter_button); 69 mSwitcher = (CameraSwitcher) findViewById(R.id.camera_switcher); 70 mEventGroup = findViewById(R.id.event_group); 71 mDrawables = new Drawable[DRAW_IDS.length]; 72 for (int i = 0; i < DRAW_IDS.length; i++) { 73 Drawable d = getResources().getDrawable(DRAW_IDS[i]); 74 mDrawables[i] = d; 75 } 76 for (int i = 0; i < mDrawables.length; i++) { 77 if (i == LIGHTCYCLE_MODULE_INDEX && !LightCycleHelper.hasLightCycleCapture(this)) { 78 continue; // not enabled, so don't add to UI 79 } 80 ImageView iv = new ImageView(this); 81 iv.setImageDrawable(mDrawables[i]); 82 mSwitcher.add(iv, new LayoutParams(LayoutParams.WRAP_CONTENT, 83 LayoutParams.WRAP_CONTENT)); 84 final int index = i; 85 iv.setOnClickListener(new OnClickListener() { 86 @Override 87 public void onClick(View arg0) { 88 mSwitcher.setCurrentModule(index); 89 onCameraSelected(index); 90 } 91 }); 92 } 93 94 mSwitcher.setSwitchListener(this); 95 if (MediaStore.INTENT_ACTION_VIDEO_CAMERA.equals(getIntent().getAction())) { 96 mCurrentModule = new VideoModule(); 97 mSelectedModule = VIDEO_MODULE_INDEX; 98 } else { 99 mCurrentModule = new PhotoModule(); 100 mSelectedModule = PHOTO_MODULE_INDEX; 101 } 102 mCurrentModule.init(this, mFrame, true); 103 mSwitcher.setCurrentModule(mSelectedModule); 104 } 105 106 @Override 107 public void onScroll() { 108 } 109 110 @Override 111 public void onCameraSelected(int i) { 112 if (i != mSelectedModule) { 113 mPaused = true; 114 boolean wasPanorama = isPanoramaActivity(); 115 closeModule(mCurrentModule); 116 mSelectedModule = i; 117 switch (i) { 118 case VIDEO_MODULE_INDEX: 119 mCurrentModule = new VideoModule(); 120 break; 121 case PHOTO_MODULE_INDEX: 122 mCurrentModule = new PhotoModule(); 123 break; 124 case PANORAMA_MODULE_INDEX: 125 mCurrentModule = new PanoramaModule(); 126 break; 127 case LIGHTCYCLE_MODULE_INDEX: 128 mCurrentModule = LightCycleHelper.createPanoramaModule(); 129 break; 130 } 131 openModule(mCurrentModule, wasPanorama); 132 } 133 } 134 135 private void openModule(CameraModule module, boolean wasPanorama) { 136 module.init(this, mFrame, !(wasPanorama || isPanoramaActivity())); 137 mPaused = false; 138 module.onResumeBeforeSuper(); 139 module.onResumeAfterSuper(); 140 } 141 142 private void closeModule(CameraModule module) { 143 module.onPauseBeforeSuper(); 144 module.onPauseAfterSuper(); 145 mFrame.removeAllViews(); 146 } 147 148 public ShutterButton getShutterButton() { 149 return mShutter; 150 } 151 152 public void hideUI() { 153 hideSwitcher(); 154 mShutter.setVisibility(View.GONE); 155 } 156 157 public void showUI() { 158 showSwitcher(); 159 mShutter.setVisibility(View.VISIBLE); 160 } 161 162 public void hideSwitcher() { 163 mSwitcher.setVisibility(View.GONE); 164 } 165 166 public void showSwitcher() { 167 if (mCurrentModule.needsSwitcher()) { 168 mSwitcher.setVisibility(View.VISIBLE); 169 } 170 } 171 172 public void switchToModule(int moduleIndex) { 173 mSwitcher.setCurrentModule(moduleIndex); 174 onCameraSelected(moduleIndex); 175 } 176 177 @Override 178 public void onConfigurationChanged(Configuration config) { 179 super.onConfigurationChanged(config); 180 mCurrentModule.onConfigurationChanged(config); 181 } 182 183 @Override 184 public void onPause() { 185 mPaused = true; 186 mCurrentModule.onPauseBeforeSuper(); 187 super.onPause(); 188 mCurrentModule.onPauseAfterSuper(); 189 } 190 191 @Override 192 public void onResume() { 193 mPaused = false; 194 mCurrentModule.onResumeBeforeSuper(); 195 super.onResume(); 196 mCurrentModule.onResumeAfterSuper(); 197 } 198 199 @Override 200 protected void onFullScreenChanged(boolean full) { 201 if (full) { 202 showUI(); 203 } else { 204 hideUI(); 205 } 206 super.onFullScreenChanged(full); 207 mCurrentModule.onFullScreenChanged(full); 208 } 209 210 @Override 211 protected void onStop() { 212 super.onStop(); 213 mCurrentModule.onStop(); 214 } 215 216 @Override 217 protected void onNewIntent(Intent intent) { 218 super.onNewIntent(intent); 219 getStateManager().clearActivityResult(); 220 } 221 222 @Override 223 protected void installIntentFilter() { 224 super.installIntentFilter(); 225 mCurrentModule.installIntentFilter(); 226 } 227 228 @Override 229 protected void onActivityResult( 230 int requestCode, int resultCode, Intent data) { 231 super.onActivityResult(requestCode, resultCode, data); 232 mCurrentModule.onActivityResult(requestCode, resultCode, data); 233 } 234 235 // Preview area is touched. Handle touch focus. 236 @Override 237 protected void onSingleTapUp(View view, int x, int y) { 238 mCurrentModule.onSingleTapUp(view, x, y); 239 } 240 241 @Override 242 public void onBackPressed() { 243 if (!mCurrentModule.onBackPressed()) { 244 super.onBackPressed(); 245 } 246 } 247 248 @Override 249 public boolean onKeyDown(int keyCode, KeyEvent event) { 250 return mCurrentModule.onKeyDown(keyCode, event) 251 || super.onKeyDown(keyCode, event); 252 } 253 254 @Override 255 public boolean onKeyUp(int keyCode, KeyEvent event) { 256 return mCurrentModule.onKeyUp(keyCode, event) 257 || super.onKeyUp(keyCode, event); 258 } 259 260 @Override 261 public boolean dispatchTouchEvent(MotionEvent m) { 262 // some custom logic to feed both switcher and shutter 263 boolean res = mCurrentModule.dispatchTouchEvent(m); 264 if (!res) { 265 // try switcher and shutter first 266 boolean front = tryDispatch(m, mShutter); 267 front |= tryDispatch(m, mSwitcher); 268 return front || mEventGroup.dispatchTouchEvent(m); 269 } 270 return res; 271 } 272 273 private boolean tryDispatch(MotionEvent m, View v) { 274 if ((m.getActionMasked() == MotionEvent.ACTION_DOWN) 275 && isInside(m, v)) { 276 mDispatched.add(v); 277 return v.dispatchTouchEvent(transformEvent(m, v)); 278 } else { 279 if (mDispatched.contains(v)) { 280 boolean res = v.dispatchTouchEvent(transformEvent(m, v)); 281 if (MotionEvent.ACTION_UP == m.getActionMasked() 282 || MotionEvent.ACTION_CANCEL == m.getActionMasked()) { 283 mDispatched.remove(v); 284 } 285 return res; 286 } 287 return false; 288 } 289 } 290 291 private boolean isInside(MotionEvent evt, View v) { 292 return (evt.getX() >= v.getLeft() && evt.getX() < v.getRight() 293 && evt.getY() >= v.getTop() && evt.getY() < v.getBottom()); 294 } 295 296 private MotionEvent transformEvent(MotionEvent m, View v) { 297 MotionEvent r = MotionEvent.obtain(m); 298 r.offsetLocation(- v.getLeft(), - v.getTop()); 299 return r; 300 } 301 302 303 // Preview texture has been copied. Now camera can be released and the 304 // animation can be started. 305 @Override 306 public void onPreviewTextureCopied() { 307 mCurrentModule.onPreviewTextureCopied(); 308 } 309 310 @Override 311 public void onUserInteraction() { 312 super.onUserInteraction(); 313 mCurrentModule.onUserInteraction(); 314 } 315 316 @Override 317 protected boolean updateStorageHintOnResume() { 318 return mCurrentModule.updateStorageHintOnResume(); 319 } 320 321 @Override 322 public void updateCameraAppView() { 323 super.updateCameraAppView(); 324 mCurrentModule.updateCameraAppView(); 325 } 326 327 @Override 328 public boolean isPanoramaActivity() { 329 return (mCurrentModule instanceof PanoramaModule); 330 } 331 332} 333