IndicatorControlWheelContainer.java revision ab2ffa88872149978823c3184d0af162d3cdca13
1/* 2 * Copyright (C) 2011 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.ui; 18 19import com.android.camera.CameraPreference.OnPreferenceChangedListener; 20import com.android.camera.PreferenceGroup; 21import com.android.camera.R; 22import com.android.camera.Util; 23 24import android.content.Context; 25import android.util.AttributeSet; 26import android.view.MotionEvent; 27import android.view.View; 28 29/** 30 * On the tablet UI, we have IndicatorControlWheelContainer which contains a 31 * ShutterButton, a IndicatorControlWheel (which combines first-level and 32 * second-level indicators) and a ZoomControlWheel. 33 */ 34public class IndicatorControlWheelContainer extends IndicatorControlContainer 35 implements OnIndicatorEventListener { 36 public static final int STROKE_WIDTH = 87; 37 public static final int SHUTTER_BUTTON_RADIUS = 74; 38 public static final int FULL_WHEEL_RADIUS = 93; 39 40 private static final String TAG = "IndicatorControlWheelContainer"; 41 42 private View mShutterButton; 43 private double mShutterButtonRadius; 44 private IndicatorControlWheel mIndicatorControlWheel; 45 private ZoomControlWheel mZoomControlWheel; 46 private int mCenterX, mCenterY; 47 48 public IndicatorControlWheelContainer(Context context, AttributeSet attrs) { 49 super(context, attrs); 50 } 51 52 @Override 53 protected void onFinishInflate() { 54 mShutterButton = findViewById(R.id.shutter_button); 55 mShutterButtonRadius = Util.dpToPixel(SHUTTER_BUTTON_RADIUS); 56 57 mZoomControlWheel = (ZoomControlWheel) findViewById(R.id.zoom_control); 58 mZoomControlWheel.setOnIndicatorEventListener(this); 59 60 mIndicatorControlWheel = (IndicatorControlWheel) findViewById( 61 R.id.indicator_control_wheel); 62 } 63 64 public void initialize(Context context, PreferenceGroup group, 65 String flashSetting, boolean isZoomSupported, 66 String[] keys, String[] otherSettingKeys) { 67 mIndicatorControlWheel.initialize(context, group, flashSetting, 68 isZoomSupported, keys, otherSettingKeys); 69 mIndicatorControlWheel.setOnIndicatorEventListener(this); 70 } 71 72 public void onIndicatorEvent(int event) { 73 switch (event) { 74 case OnIndicatorEventListener.EVENT_ENTER_ZOOM_CONTROL: 75 mIndicatorControlWheel.setVisibility(View.GONE); 76 mZoomControlWheel.setVisibility(View.VISIBLE); 77 mZoomControlWheel.startZoomControl(); 78 break; 79 80 case OnIndicatorEventListener.EVENT_LEAVE_ZOOM_CONTROL: 81 mZoomControlWheel.setVisibility(View.GONE); 82 mIndicatorControlWheel.setVisibility(View.VISIBLE); 83 break; 84 } 85 } 86 87 @Override 88 public boolean dispatchTouchEvent(MotionEvent event) { 89 if (!onFilterTouchEventForSecurity(event)) return false; 90 91 int action = event.getAction(); 92 93 double dx = event.getX() - mCenterX; 94 double dy = mCenterY - event.getY(); 95 double radius = Math.sqrt(dx * dx + dy * dy); 96 97 // Check if the event should be dispatched to the shutter button. 98 if (radius <= mShutterButtonRadius) { 99 if (mIndicatorControlWheel.getVisibility() == View.VISIBLE) { 100 mIndicatorControlWheel.onTouchOutBound(); 101 } else { 102 return mZoomControlWheel.dispatchTouchEvent(event); 103 } 104 if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) { 105 return mShutterButton.dispatchTouchEvent(event); 106 } 107 return false; 108 } 109 110 if (mShutterButton.isPressed()) { 111 // Send cancel to the shutter button if it was pressed. 112 event.setAction(MotionEvent.ACTION_CANCEL); 113 mShutterButton.dispatchTouchEvent(event); 114 return true; 115 } 116 117 if (mIndicatorControlWheel.getVisibility() == View.VISIBLE) { 118 return mIndicatorControlWheel.dispatchTouchEvent(event); 119 } else { 120 return mZoomControlWheel.dispatchTouchEvent(event); 121 } 122 } 123 124 @Override 125 protected void onLayout( 126 boolean changed, int left, int top, int right, int bottom) { 127 128 // Layout the shutter button. 129 int shutterButtonWidth = mShutterButton.getMeasuredWidth(); 130 int shutterButtonHeight = mShutterButton.getMeasuredHeight(); 131 mCenterX = right - left - Util.dpToPixel(FULL_WHEEL_RADIUS); 132 mCenterY = (bottom - top) / 2; 133 mShutterButton.layout(right - left - shutterButtonWidth, 134 mCenterY - shutterButtonHeight / 2, 135 right - left, 136 mCenterY + shutterButtonHeight - shutterButtonHeight / 2); 137 // Layout the control wheel. 138 mIndicatorControlWheel.layout(0, 0, right - left, bottom - top); 139 mZoomControlWheel.layout(0, 0, right - left, bottom - top); 140 } 141 142 @Override 143 protected void onMeasure(int widthSpec, int heightSpec) { 144 // Measure all children. 145 int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 146 mShutterButton.measure(freeSpec, freeSpec); 147 mIndicatorControlWheel.measure(freeSpec, freeSpec); 148 mZoomControlWheel.measure(freeSpec, freeSpec); 149 150 // Measure myself. Add some buffer for highlight arc. 151 int desiredWidth = mShutterButton.getMeasuredWidth() 152 + IndicatorControlWheel.HIGHLIGHT_WIDTH * 4; 153 int desiredHeight = mShutterButton.getMeasuredHeight() 154 + IndicatorControlWheel.HIGHLIGHT_WIDTH * 4; 155 int widthMode = MeasureSpec.getMode(widthSpec); 156 int heightMode = MeasureSpec.getMode(heightSpec); 157 int measuredWidth, measuredHeight; 158 if (widthMode == MeasureSpec.UNSPECIFIED) { 159 measuredWidth = desiredWidth; 160 } else if (widthMode == MeasureSpec.AT_MOST) { 161 measuredWidth = Math.min(desiredWidth, MeasureSpec.getSize(widthSpec)); 162 } else { // MeasureSpec.EXACTLY 163 measuredWidth = MeasureSpec.getSize(widthSpec); 164 } 165 if (heightMode == MeasureSpec.UNSPECIFIED) { 166 measuredHeight = desiredHeight; 167 } else if (heightMode == MeasureSpec.AT_MOST) { 168 measuredHeight = Math.min(desiredHeight, MeasureSpec.getSize(heightSpec)); 169 } else { // MeasureSpec.EXACTLY 170 measuredHeight = MeasureSpec.getSize(heightSpec); 171 } 172 setMeasuredDimension(measuredWidth, measuredHeight); 173 } 174 175 @Override 176 public void setListener(OnPreferenceChangedListener listener) { 177 mIndicatorControlWheel.setListener(listener); 178 } 179 180 @Override 181 public void reloadPreferences() { 182 mIndicatorControlWheel.reloadPreferences(); 183 } 184 185 @Override 186 public View getActiveSettingPopup() { 187 return mIndicatorControlWheel.getActiveSettingPopup(); 188 } 189 190 @Override 191 public boolean dismissSettingPopup() { 192 return mIndicatorControlWheel.dismissSettingPopup(); 193 } 194 195 @Override 196 public void setDegree(int degree) { 197 mIndicatorControlWheel.setDegree(degree); 198 mZoomControlWheel.setDegree(degree); 199 } 200 201 public void startTimeLapseAnimation(int timeLapseInterval, long startTime) { 202 mIndicatorControlWheel.startTimeLapseAnimation( 203 timeLapseInterval, startTime); 204 } 205 206 public void stopTimeLapseAnimation() { 207 mIndicatorControlWheel.stopTimeLapseAnimation(); 208 } 209 210 @Override 211 public void setEnabled(boolean enabled) { 212 mIndicatorControlWheel.setEnabled(enabled); 213 mZoomControlWheel.setEnabled(enabled); 214 } 215 216 @Override 217 public void overrideSettings(final String ... keyvalues) { 218 mIndicatorControlWheel.overrideSettings(keyvalues); 219 } 220} 221