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.ui;
18 
19 import com.android.camera.ListPreference;
20 import com.android.camera.R;
21 
22 import android.content.Context;
23 import android.os.Handler;
24 import android.util.AttributeSet;
25 import android.util.Log;
26 import android.view.MotionEvent;
27 import android.view.View;
28 import android.view.View.OnTouchListener;
29 import android.view.accessibility.AccessibilityEvent;
30 import android.widget.Button;
31 import android.widget.TextView;
32 
33 /* A knob setting control. */
34 public class InLineSettingKnob extends InLineSettingItem {
35     private final String TAG = "InLineSettingKnob";
36     private boolean mNext, mPrevious;
37     private Button mPrevButton, mNextButton;
38     private Handler mHandler;
39     // The view that shows the current selected setting. Ex: 5MP
40     private TextView mEntry;
41 
42     private final Runnable mRunnable = new Runnable() {
43         public void run() {
44             if (mNext) {
45                 if (changeIndex(mIndex - 1)) {
46                     mHandler.postDelayed(this, 100);
47                 }
48             } else if (mPrevious) {
49                 if (changeIndex(mIndex + 1)) {
50                     mHandler.postDelayed(this, 100);
51                 }
52             }
53         }
54     };
55 
InLineSettingKnob(Context context, AttributeSet attrs)56     public InLineSettingKnob(Context context, AttributeSet attrs) {
57         super(context, attrs);
58         mHandler = new Handler();
59     }
60 
61     OnTouchListener mNextTouchListener = new OnTouchListener() {
62         public boolean onTouch(View v, MotionEvent event) {
63             if (mOverrideValue != null) return true;
64             if (event.getAction() == MotionEvent.ACTION_DOWN) {
65                 if (!mNext && changeIndex(mIndex - 1)) {
66                     mNext = true;
67                     // Give bigger delay so users can change only one step.
68                     mHandler.postDelayed(mRunnable, 300);
69                 }
70             } else if (event.getAction() == MotionEvent.ACTION_UP
71                     || event.getAction() == MotionEvent.ACTION_CANCEL) {
72                 mNext = false;
73             }
74             return false;
75         }
76     };
77 
78     OnTouchListener mPreviousTouchListener = new OnTouchListener() {
79         public boolean onTouch(View v, MotionEvent event) {
80             if (mOverrideValue != null) return true;
81             if (event.getAction() == MotionEvent.ACTION_DOWN) {
82                 if (!mPrevious && changeIndex(mIndex + 1)) {
83                     mPrevious = true;
84                     // Give bigger delay so users can change only one step.
85                     mHandler.postDelayed(mRunnable, 300);
86                 }
87             } else if (event.getAction() == MotionEvent.ACTION_UP
88                     || event.getAction() == MotionEvent.ACTION_CANCEL) {
89                 mPrevious = false;
90             }
91             return false;
92         }
93     };
94 
95     @Override
onFinishInflate()96     protected void onFinishInflate() {
97         super.onFinishInflate();
98         mNextButton = (Button) findViewById(R.id.increment);
99         mNextButton.setOnTouchListener(mNextTouchListener);
100         mPrevButton = (Button) findViewById(R.id.decrement);
101         mPrevButton.setOnTouchListener(mPreviousTouchListener);
102         mEntry = (TextView) findViewById(R.id.current_setting);
103     }
104 
105     @Override
initialize(ListPreference preference)106     public void initialize(ListPreference preference) {
107         super.initialize(preference);
108         // Add content descriptions for the increment and decrement buttons.
109         mNextButton.setContentDescription(getResources().getString(
110                 R.string.accessibility_increment, mPreference.getTitle()));
111         mPrevButton.setContentDescription(getResources().getString(
112                 R.string.accessibility_decrement, mPreference.getTitle()));
113     }
114 
updateView()115     protected void updateView() {
116         if (mOverrideValue == null) {
117             mEntry.setText(mPreference.getEntry());
118             mNextButton.setVisibility(mIndex == 0 ? View.INVISIBLE : View.VISIBLE);
119             mPrevButton.setVisibility(mIndex == mPreference.getEntryValues().length - 1
120                     ? View.INVISIBLE : View.VISIBLE);
121         } else {
122             int index = mPreference.findIndexOfValue(mOverrideValue);
123             if (index != -1) {
124                 mEntry.setText(mPreference.getEntries()[index]);
125             } else {
126                 // Avoid the crash if camera driver has bugs.
127                 Log.e(TAG, "Fail to find override value=" + mOverrideValue);
128                 mPreference.print();
129             }
130             mNextButton.setVisibility(View.INVISIBLE);
131             mPrevButton.setVisibility(View.INVISIBLE);
132         }
133     }
134 
135     @Override
dispatchPopulateAccessibilityEvent(AccessibilityEvent event)136     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
137         onPopulateAccessibilityEvent(event);
138         return true;
139     }
140 
141     @Override
onPopulateAccessibilityEvent(AccessibilityEvent event)142     public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
143         super.onPopulateAccessibilityEvent(event);
144         event.getText().add(mPreference.getTitle() + mPreference.getEntry());
145     }
146 }
147