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