SecondLevelIndicatorControlBar.java revision db3556078a75be0bd4a08195d90f060724034e88
1/* 2 * Copyright (C) 2010 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.PreferenceGroup; 20import com.android.camera.R; 21 22import android.content.Context; 23import android.util.AttributeSet; 24import android.view.MotionEvent; 25import android.view.View; 26import android.widget.ImageView; 27 28/** 29 * A view that contains camera setting indicators which are spread over a 30 * vertical bar in preview frame. 31 */ 32public class SecondLevelIndicatorControlBar extends IndicatorControl implements 33 View.OnClickListener { 34 private static final String TAG = "SecondLevelIndicatorControlBar"; 35 private ImageView mCloseIcon; 36 int mDegree = 0; 37 int mSelectedIndex = -1; 38 39 public SecondLevelIndicatorControlBar(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 } 42 43 public void initialize(Context context, PreferenceGroup group, 44 String[] keys, String[] otherSettingKeys) { 45 if (mCloseIcon == null) { 46 mCloseIcon = new ColorFilterImageView(context); 47 mCloseIcon.setImageResource(R.drawable.btn_close_settings); 48 mCloseIcon.setOnClickListener(this); 49 addView(mCloseIcon); 50 } 51 setPreferenceGroup(group); 52 addControls(keys, otherSettingKeys); 53 if (mDegree != 0) setDegree(mDegree); 54 } 55 56 public void onClick(View view) { 57 dismissSettingPopup(); 58 mOnIndicatorEventListener.onIndicatorEvent( 59 OnIndicatorEventListener.EVENT_LEAVE_SECOND_LEVEL_INDICATOR_BAR); 60 } 61 62 @Override 63 public boolean dispatchTouchEvent(MotionEvent event) { 64 if (!onFilterTouchEventForSecurity(event)) return false; 65 66 int action = event.getAction(); 67 if (!isEnabled()) return false; 68 69 double x = (double) event.getX(); 70 double y = (double) event.getY(); 71 int height = getHeight(); 72 if (height == 0) return false; // the event is sent before onMeasure() 73 if (x > getWidth()) x = getWidth(); 74 if (y >= height) y = height - 1; 75 76 int index = (int) (y * getChildCount()) / height; 77 View b = getChildAt(index); 78 b.dispatchTouchEvent(event); 79 if ((mSelectedIndex != -1) && (index != mSelectedIndex)) { 80 View v = getChildAt(mSelectedIndex); 81 if (v instanceof AbstractIndicatorButton) { 82 AbstractIndicatorButton c = (AbstractIndicatorButton) v; 83 event.setAction(MotionEvent.ACTION_CANCEL); 84 c.dispatchTouchEvent(event); 85 c.dismissPopup(); 86 } 87 88 if (action == MotionEvent.ACTION_MOVE) { 89 event.setAction(MotionEvent.ACTION_DOWN); 90 b.dispatchTouchEvent(event); 91 } 92 } 93 mSelectedIndex = index; 94 return true; 95 } 96 97 @Override 98 public void setDegree(int degree) { 99 mDegree = degree; 100 super.setDegree(degree); 101 } 102 103 @Override 104 protected void onLayout( 105 boolean changed, int left, int top, int right, int bottom) { 106 int count = getChildCount(); 107 if (count == 0) return; 108 int width = right - left; 109 int height = bottom - top; 110 int h = height / count; 111 112 // TODO: follow the redlines of UI design in next change. The icons are 113 // simply distributed on the bar equally with current implmentation. 114 for (int i = 0; i < count; i++) { 115 getChildAt(i).layout(0, i * h, width, (i + 1) * h); 116 } 117 } 118 119 @Override 120 public void setEnabled(boolean enabled) { 121 super.setEnabled(enabled); 122 if (mCurrentMode == MODE_VIDEO) { 123 mCloseIcon.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE); 124 } 125 mCloseIcon.setEnabled(enabled); 126 } 127} 128