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