repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.camera; |
| 18 | |
Chung-yih Wang | 5f6e354 | 2011-09-29 11:05:38 +0800 | [diff] [blame] | 19 | import com.android.camera.ui.PopupManager; |
Chih-yu Huang | 3ebe49d | 2011-10-07 18:44:53 +0800 | [diff] [blame] | 20 | import com.android.camera.ui.Rotatable; |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 21 | import com.android.camera.ui.RotateImageView; |
| 22 | |
| 23 | import android.content.Context; |
| 24 | import android.graphics.PorterDuff; |
| 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.util.AttributeSet; |
Chung-yih Wang | 46f4430 | 2011-09-28 11:07:28 +0800 | [diff] [blame] | 27 | import android.util.Log; |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 28 | import android.view.View; |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 29 | import android.view.animation.Animation; |
| 30 | import android.view.animation.Animation.AnimationListener; |
| 31 | import android.view.animation.AnimationUtils; |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 32 | import android.widget.ImageView; |
| 33 | import android.widget.RelativeLayout; |
| 34 | |
| 35 | /** |
| 36 | * A widget that includes three mode selections {@code RotateImageView}'s and |
| 37 | * a current mode indicator. |
| 38 | */ |
Chung-yih Wang | 5f6e354 | 2011-09-29 11:05:38 +0800 | [diff] [blame] | 39 | public class ModePicker extends RelativeLayout implements View.OnClickListener, |
Chih-yu Huang | 3ebe49d | 2011-10-07 18:44:53 +0800 | [diff] [blame] | 40 | PopupManager.OnOtherPopupShowedListener, Rotatable { |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 41 | public static final int MODE_CAMERA = 0; |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 42 | public static final int MODE_VIDEO = 1; |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 43 | public static final int MODE_PANORAMA = 2; |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 44 | |
Chung-yih Wang | 46f4430 | 2011-09-28 11:07:28 +0800 | [diff] [blame] | 45 | private static final String TAG = "ModePicker"; |
Chih-yu Huang | 3ea573b | 2011-08-22 18:12:07 +0800 | [diff] [blame] | 46 | // Total mode number |
| 47 | private static final int MODE_NUM = 3; |
| 48 | |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 49 | /** A callback to be called when the user wants to switch activity. */ |
| 50 | public interface OnModeChangeListener { |
| 51 | // Returns true if the listener agrees that the mode can be changed. |
| 52 | public boolean onModeChanged(int newMode); |
| 53 | } |
| 54 | |
| 55 | private final int DISABLED_COLOR; |
Chih-yu Huang | 3ea573b | 2011-08-22 18:12:07 +0800 | [diff] [blame] | 56 | private final int CURRENT_MODE_BACKGROUND; |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 57 | |
| 58 | private OnModeChangeListener mListener; |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 59 | private View mModeSelectionFrame; |
| 60 | private RotateImageView mModeSelectionIcon[]; |
| 61 | private View mCurrentModeFrame; |
| 62 | private RotateImageView mCurrentModeIcon[]; |
| 63 | private View mCurrentModeBar; |
Chung-yih Wang | 5f6e354 | 2011-09-29 11:05:38 +0800 | [diff] [blame] | 64 | private boolean mSelectionEnabled; |
| 65 | |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 66 | |
Wu-cheng Li | 3cd761b | 2011-09-20 15:48:37 +0800 | [diff] [blame] | 67 | private int mCurrentMode = 0; |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 68 | private Animation mFadeIn, mFadeOut; |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 69 | |
| 70 | public ModePicker(Context context, AttributeSet attrs) { |
| 71 | super(context, attrs); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 72 | DISABLED_COLOR = context.getResources().getColor(R.color.icon_disabled_color); |
Chih-yu Huang | 3ea573b | 2011-08-22 18:12:07 +0800 | [diff] [blame] | 73 | CURRENT_MODE_BACKGROUND = R.drawable.btn_mode_background; |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 74 | mFadeIn = AnimationUtils.loadAnimation( |
| 75 | context, R.anim.mode_selection_fade_in); |
| 76 | mFadeOut = AnimationUtils.loadAnimation( |
| 77 | context, R.anim.mode_selection_fade_out); |
| 78 | mFadeOut.setAnimationListener(mAnimationListener); |
Chung-yih Wang | 5f6e354 | 2011-09-29 11:05:38 +0800 | [diff] [blame] | 79 | PopupManager.getInstance(context).setOnOtherPopupShowedListener(this); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | protected void onFinishInflate() { |
| 83 | super.onFinishInflate(); |
| 84 | |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 85 | mModeSelectionFrame = findViewById(R.id.mode_selection); |
| 86 | mModeSelectionIcon = new RotateImageView[MODE_NUM]; |
| 87 | mModeSelectionIcon[MODE_PANORAMA] = |
| 88 | (RotateImageView) findViewById(R.id.mode_panorama); |
| 89 | mModeSelectionIcon[MODE_VIDEO] = |
| 90 | (RotateImageView) findViewById(R.id.mode_video); |
| 91 | mModeSelectionIcon[MODE_CAMERA] = |
| 92 | (RotateImageView) findViewById(R.id.mode_camera); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 93 | |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 94 | // The current mode frame is for Phone UI only. |
| 95 | mCurrentModeFrame = findViewById(R.id.current_mode); |
| 96 | if (mCurrentModeFrame != null) { |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 97 | mCurrentModeIcon = new RotateImageView[MODE_NUM]; |
| 98 | mCurrentModeIcon[0] = (RotateImageView) findViewById(R.id.mode_0); |
| 99 | mCurrentModeIcon[1] = (RotateImageView) findViewById(R.id.mode_1); |
| 100 | mCurrentModeIcon[2] = (RotateImageView) findViewById(R.id.mode_2); |
| 101 | } else { |
| 102 | // current_mode_bar is only for tablet. |
| 103 | mCurrentModeBar = findViewById(R.id.current_mode_bar); |
| 104 | enableModeSelection(true); |
| 105 | } |
Chung-yih Wang | 46f4430 | 2011-09-28 11:07:28 +0800 | [diff] [blame] | 106 | registerOnClickListener(); |
| 107 | } |
| 108 | |
| 109 | private void registerOnClickListener() { |
| 110 | if (mCurrentModeFrame != null) { |
| 111 | mCurrentModeFrame.setOnClickListener(this); |
| 112 | } |
| 113 | for (int i = 0; i < MODE_NUM; ++i) { |
| 114 | mModeSelectionIcon[i].setOnClickListener(this); |
| 115 | } |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Chung-yih Wang | 5f6e354 | 2011-09-29 11:05:38 +0800 | [diff] [blame] | 118 | @Override |
| 119 | public void onOtherPopupShowed() { |
| 120 | if (mSelectionEnabled) enableModeSelection(false); |
| 121 | } |
| 122 | |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 123 | private AnimationListener mAnimationListener = new AnimationListener() { |
| 124 | public void onAnimationEnd(Animation animation) { |
| 125 | changeToSelectedMode(); |
| 126 | mCurrentModeFrame.setVisibility(View.VISIBLE); |
| 127 | mModeSelectionFrame.setVisibility(View.GONE); |
| 128 | } |
| 129 | |
| 130 | public void onAnimationRepeat(Animation animation) { |
| 131 | } |
| 132 | |
| 133 | public void onAnimationStart(Animation animation) { |
| 134 | } |
| 135 | }; |
| 136 | |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 137 | private void enableModeSelection(boolean enabled) { |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 138 | if (mCurrentModeFrame != null) { |
Chung-yih Wang | 5f6e354 | 2011-09-29 11:05:38 +0800 | [diff] [blame] | 139 | mSelectionEnabled = enabled; |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 140 | // Animation Effect is applied on Phone UI only. |
| 141 | mModeSelectionFrame.startAnimation(enabled ? mFadeIn : mFadeOut); |
| 142 | if (enabled) { |
| 143 | mModeSelectionFrame.setVisibility(View.VISIBLE); |
| 144 | mCurrentModeFrame.setVisibility(View.GONE); |
| 145 | } |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 146 | } |
| 147 | updateModeState(); |
| 148 | } |
| 149 | |
| 150 | private void changeToSelectedMode() { |
Chung-yih Wang | 46f4430 | 2011-09-28 11:07:28 +0800 | [diff] [blame] | 151 | if (mListener != null) { |
| 152 | if (mListener.onModeChanged(mCurrentMode)) { |
| 153 | Log.e(TAG, "failed:onModeChanged:" + mCurrentMode); |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 154 | } |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | public void onClick(View view) { |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 159 | if (view == mCurrentModeFrame) { |
Chung-yih Wang | 5f6e354 | 2011-09-29 11:05:38 +0800 | [diff] [blame] | 160 | PopupManager.getInstance(getContext()).notifyShowPopup(this); |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 161 | enableModeSelection(true); |
| 162 | } else { |
Chung-yih Wang | 46f4430 | 2011-09-28 11:07:28 +0800 | [diff] [blame] | 163 | // Set the selected mode as the current one and switch to it. |
| 164 | for (int i = 0; i < MODE_NUM; ++i) { |
| 165 | if (view == mModeSelectionIcon[i] && (mCurrentMode != i)) { |
| 166 | setCurrentMode(i); |
| 167 | break; |
| 168 | } |
| 169 | } |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 170 | if (mCurrentModeBar == null) { |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 171 | enableModeSelection(false); |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 172 | } else { |
| 173 | changeToSelectedMode(); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 174 | } |
| 175 | } |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 176 | } |
| 177 | |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 178 | public void setOnModeChangeListener(OnModeChangeListener listener) { |
| 179 | mListener = listener; |
| 180 | } |
| 181 | |
| 182 | public void setCurrentMode(int mode) { |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 183 | mCurrentMode = mode; |
Chung-yih Wang | 46f4430 | 2011-09-28 11:07:28 +0800 | [diff] [blame] | 184 | updateModeState(); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | public boolean onModeChanged(int mode) { |
| 188 | setCurrentMode(mode); |
| 189 | return true; |
| 190 | } |
| 191 | |
Chih-yu Huang | 3ebe49d | 2011-10-07 18:44:53 +0800 | [diff] [blame] | 192 | public void setOrientation(int orientation) { |
Chih-yu Huang | 3ea573b | 2011-08-22 18:12:07 +0800 | [diff] [blame] | 193 | for (int i = 0; i < MODE_NUM; ++i) { |
Chih-yu Huang | 3ebe49d | 2011-10-07 18:44:53 +0800 | [diff] [blame] | 194 | mModeSelectionIcon[i].setOrientation(orientation); |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 195 | if (mCurrentModeFrame != null) { |
Chih-yu Huang | 3ebe49d | 2011-10-07 18:44:53 +0800 | [diff] [blame] | 196 | mCurrentModeIcon[i].setOrientation(orientation); |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 197 | } |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 198 | } |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void setEnabled(boolean enabled) { |
| 203 | super.setEnabled(enabled); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 204 | |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 205 | // Enable or disable the frames. |
| 206 | if (mCurrentModeFrame != null) mCurrentModeFrame.setEnabled(enabled); |
| 207 | mModeSelectionFrame.setEnabled(enabled); |
Chih-yu Huang | 3ea573b | 2011-08-22 18:12:07 +0800 | [diff] [blame] | 208 | |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 209 | // Enable or disable the icons. |
Chih-yu Huang | 3ea573b | 2011-08-22 18:12:07 +0800 | [diff] [blame] | 210 | for (int i = 0; i < MODE_NUM; ++i) { |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 211 | mModeSelectionIcon[i].setEnabled(enabled); |
| 212 | if (mCurrentModeFrame != null) mCurrentModeIcon[i].setEnabled(enabled); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 213 | } |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 214 | if (enabled) updateModeState(); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 215 | } |
| 216 | |
Chung-yih Wang | 46f4430 | 2011-09-28 11:07:28 +0800 | [diff] [blame] | 217 | private void highlightView(ImageView view, boolean enabled) { |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 218 | if (enabled) { |
Chung-yih Wang | 46f4430 | 2011-09-28 11:07:28 +0800 | [diff] [blame] | 219 | view.clearColorFilter(); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 220 | } else { |
Chung-yih Wang | 46f4430 | 2011-09-28 11:07:28 +0800 | [diff] [blame] | 221 | view.setColorFilter(DISABLED_COLOR, PorterDuff.Mode.SRC_ATOP); |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 222 | } |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | private void updateModeState() { |
Chih-Chung Chang | da705aa | 2011-10-31 18:50:52 +0800 | [diff] [blame] | 226 | // Grey-out the unselected icons for Phone UI. |
| 227 | if (mCurrentModeFrame != null) { |
| 228 | for (int i = 0; i < MODE_NUM; ++i) { |
| 229 | highlightView(mModeSelectionIcon[i], (i == mCurrentMode)); |
| 230 | } |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 231 | } |
Chih-Chung Chang | da705aa | 2011-10-31 18:50:52 +0800 | [diff] [blame] | 232 | |
Chung-yih Wang | 20ff6a1 | 2011-09-08 10:35:03 +0800 | [diff] [blame] | 233 | // Update the current mode icons on the Phone UI. The selected mode |
| 234 | // should be in the center of the current mode icon bar. |
| 235 | if (mCurrentModeFrame != null) { |
| 236 | for (int i = 0, j = 0; i < MODE_NUM; ++i) { |
| 237 | int target; |
| 238 | if (i == 1) { |
| 239 | // The second icon is always the selected mode. |
| 240 | target = mCurrentMode; |
| 241 | } else { |
| 242 | // Set the icons in order of camera, video and panorama. |
| 243 | if (j == mCurrentMode) j++; |
| 244 | target = j++; |
| 245 | } |
| 246 | mCurrentModeIcon[i].setImageDrawable( |
| 247 | mModeSelectionIcon[target].getDrawable()); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | protected void onLayout( |
| 254 | boolean changed, int left, int top, int right, int bottom) { |
| 255 | super.onLayout(changed, left, top, right, bottom); |
| 256 | // Layout the current mode indicator bar. |
| 257 | if (mCurrentModeBar != null) { |
| 258 | int viewWidth = mModeSelectionIcon[MODE_CAMERA].getWidth(); |
| 259 | int iconWidth = ((ImageView) mModeSelectionIcon[MODE_CAMERA]) |
| 260 | .getDrawable().getIntrinsicWidth(); |
| 261 | int padding = (viewWidth - iconWidth) / 2; |
| 262 | int l = mModeSelectionFrame.getLeft() + mCurrentMode * viewWidth; |
| 263 | mCurrentModeBar.layout((l + padding), |
| 264 | (bottom - top - mCurrentModeBar.getHeight()), |
| 265 | (l + padding + iconWidth), |
| 266 | (bottom - top)); |
| 267 | } |
repo sync | a72d73c | 2011-07-27 19:29:41 +0800 | [diff] [blame] | 268 | } |
| 269 | } |