1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.camera;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.google.android.camera.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
* The on-screen setting menu.
*
* <p>Please reference to {@code android.widget.ZoomButtonsController} for
* detail information about adding window to WindowManager.
*/
public class OnScreenSettings {
@SuppressWarnings("unused")
private static final String TAG = "OnScreenSettings";
private static final int MSG_POST_SET_VISIBLE = 1;
private static final Pattern TITLE_PATTERN =
Pattern.compile("(.*)\\s*\\((.+)\\)");
/**
* A callback to be invoked when the on-screen menu's visibility changes.
*/
public interface OnVisibilityChangedListener {
public void onVisibilityChanged(boolean visibility);
}
private LayoutParams mContainerLayoutParams;
private final Context mContext;
private final Container mContainer;
private final WindowManager mWindowManager;
private final View mOwnerView;
private ListView mMainMenu;
private ListView mSubMenu;
private boolean mIsVisible = false;
private OnVisibilityChangedListener mVisibilityListener;
private MainMenuAdapter mMainAdapter;
private final LayoutInflater mInflater;
private Runnable mRestoreRunner;
private PreferenceGroup mPreferenceGroup;
// We store the override values here. For a given preference,
// if the mapping value of the preference key is not null, we will
// use the value in this map instead of the value read from the preference
//
// This is design for the scene mode, for example, in the scene mode
// "Action", the focus mode will become "infinite" no matter what in the
// preference settings. So, we need to put a {pref_camera_focusmode_key,
// "infinite"} entry in this map.
private HashMap<String, String> mOverride = new HashMap<String, String>();
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_POST_SET_VISIBLE:
setVisible(true);
break;
}
}
};
public OnScreenSettings(View ownerView) {
mContext = ownerView.getContext();
mInflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mWindowManager = (WindowManager)
mContext.getSystemService(Context.WINDOW_SERVICE);
mOwnerView = ownerView;
mContainer = createContainer();
}
public void clearSettings() {
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(mContext).edit();
editor.clear();
editor.commit();
mPreferenceGroup.reloadValue();
if (mMainAdapter != null) {
mMainAdapter.notifyDataSetChanged();
}
mOverride.clear();
}
public void setRestoreRunner(Runnable restorer) {
mRestoreRunner = restorer;
}
public boolean isVisible() {
return mIsVisible;
}
public void setOnVisibilityChangedListener(
OnVisibilityChangedListener listener) {
mVisibilityListener = listener;
}
public void setVisible(boolean visible) {
mHandler.removeMessages(MSG_POST_SET_VISIBLE);
if (visible) {
if (mOwnerView.getWindowToken() == null) {
/*
* We need a window token to show ourselves, maybe the owner's
* window hasn't been created yet but it will have been by the
* time the looper is idle, so post the setVisible(true) call.
*/
mHandler.sendEmptyMessage(MSG_POST_SET_VISIBLE);
return;
}
}
if (mIsVisible == visible) {
return;
}
mIsVisible = visible;
if (visible) {
// Invalid the main adapter before show up, so it would be like
// a new created list
if (mMainAdapter != null) mMainAdapter.notifyDataSetInvalidated();
if (mContainerLayoutParams.token == null) {
mContainerLayoutParams.token = mOwnerView.getWindowToken();
}
mSubMenu.setVisibility(View.INVISIBLE);
mMainMenu.setVisibility(View.VISIBLE);
mWindowManager.addView(mContainer, mContainerLayoutParams);
updateLayout();
} else {
// Reset the two menus
mWindowManager.removeView(mContainer);
}
if (mVisibilityListener != null) {
mVisibilityListener.onVisibilityChanged(mIsVisible);
}
}
// Override the preference settings, if value == null, then disable the
// override.
public void overrideSettings(String key, String value) {
if (value == null) {
if (mOverride.remove(key) != null && mMainAdapter != null) {
mMainAdapter.notifyDataSetChanged();
}
} else {
if (mOverride.put(key, value) == null && mMainAdapter != null) {
mMainAdapter.notifyDataSetChanged();
}
}
}
public void updateLayout() {
// if the mOwnerView is detached from window then skip.
if (mOwnerView.getWindowToken() == null) return;
Display display = mWindowManager.getDefaultDisplay();
mContainerLayoutParams.x = 0;
mContainerLayoutParams.y = 0;
mContainerLayoutParams.width = display.getWidth() / 2;
mContainerLayoutParams.height = display.getHeight();
if (mIsVisible) {
mWindowManager.updateViewLayout(mContainer, mContainerLayoutParams);
}
}
private void showSubMenu() {
Util.slideOut(mMainMenu, Util.DIRECTION_LEFT);
Util.slideIn(mSubMenu, Util.DIRECTION_RIGHT);
mSubMenu.requestFocus();
}
private void closeSubMenu() {
Util.slideOut(mSubMenu, Util.DIRECTION_RIGHT);
Util.slideIn(mMainMenu, Util.DIRECTION_LEFT);
}
private Container createContainer() {
LayoutParams lp = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.flags = LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
lp.gravity = Gravity.TOP | Gravity.LEFT;
lp.height = LayoutParams.WRAP_CONTENT;
lp.width = LayoutParams.WRAP_CONTENT;
lp.type = LayoutParams.TYPE_APPLICATION_PANEL;
lp.format = PixelFormat.OPAQUE;
lp.windowAnimations = R.style.Animation_OnScreenMenu;
mContainerLayoutParams = lp;
Container container = new Container(mContext);
container.setLayoutParams(lp);
mInflater.inflate(R.layout.on_screen_menu, container);
mMainMenu = (ListView) container.findViewById(R.id.menu_view);
mSubMenu = (ListView) container.findViewById(R.id.sub_menu);
container.findViewById(R.id.btn_gripper)
.setOnTouchListener(new GripperTouchListener());
return container;
}
private class GripperTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return true;
case MotionEvent.ACTION_UP:
setVisible(false);
return true;
}
return false;
}
}
private boolean onContainerKey(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_CAMERA:
case KeyEvent.KEYCODE_FOCUS:
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_MENU:
if (event.getAction() == KeyEvent.ACTION_UP) {
setVisible(false);
return true;
}
}
return false;
}
// Add the preference and it's children recursively to the given list. So
// that we can show the preference (and it's children) in the list view.
private static void addPreference(
CameraPreference preference, ArrayList<CameraPreference> list) {
list.add(preference);
if (preference instanceof PreferenceGroup) {
PreferenceGroup group = (PreferenceGroup) preference;
for (int i = 0, n = group.size(); i < n; ++i) {
CameraPreference child = group.get(i);
addPreference(child, list);
}
}
}
public void setPreferenceGroup(PreferenceGroup group) {
this.mPreferenceGroup = group;
ArrayList<CameraPreference> list = new ArrayList<CameraPreference>();
// We don't want the root group added to the list. So, we add the
// children of the root here
for (int i = 0, n = group.size(); i < n; ++i) {
addPreference(group.get(i), list);
}
mMainAdapter = new MainMenuAdapter(mContext, list);
mMainMenu.setAdapter(mMainAdapter);
mMainMenu.setOnItemClickListener(mMainAdapter);
}
private View inflateIfNeed(
View view, int resource, ViewGroup root, boolean attachToRoot) {
if (view != null) return view;
return mInflater.inflate(resource, root, attachToRoot);
}
private class MainMenuAdapter extends BaseAdapter
implements OnItemClickListener {
private final ArrayList<CameraPreference> mPreferences;
public MainMenuAdapter(
Context context, ArrayList<CameraPreference> preferences) {
mPreferences = preferences;
}
public void onItemClick(
AdapterView<?> parent, View view, int position, long id) {
// If not the last item (restore settings)
if (position < mPreferences.size()) {
CameraPreference preference = mPreferences.get(position);
SubMenuAdapter adapter = new SubMenuAdapter(
mContext, (ListPreference) preference);
mSubMenu.setAdapter(adapter);
mSubMenu.setOnItemClickListener(adapter);
showSubMenu();
} else {
MenuHelper.confirmAction(mContext,
mContext.getString(R.string.confirm_restore_title),
mContext.getString(R.string.confirm_restore_message),
mRestoreRunner);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
// The last item in the view (i.e., restore settings)
if (position == mPreferences.size()) {
convertView = inflateIfNeed(convertView,
R.layout.on_screen_menu_list_item, parent, false);
((TextView) convertView.findViewById(R.id.title)).setText(
mContext.getString(R.string.pref_restore_title));
((TextView) convertView.findViewById(R.id.summary)).setText(
mContext.getString(R.string.pref_restore_detail));
// Make sure we didn't set focusable. Otherwise, it will
// consume all the events. See more detail in the comments
// below.
convertView.setFocusable(false);
return convertView;
}
CameraPreference preference = mPreferences.get(position);
if (preference instanceof PreferenceGroup) {
convertView = inflateIfNeed(convertView,
R.layout.on_screen_menu_header, parent, false);
PreferenceGroup group = (PreferenceGroup) preference;
((TextView) convertView.findViewById(
R.id.title)).setText(group.getTitle());
} else {
convertView = inflateIfNeed(convertView,
R.layout.on_screen_menu_list_item, parent, false);
String override = mOverride.get(
((ListPreference) preference).getKey());
TextView title = (TextView)
convertView.findViewById(R.id.title);
title.setText(preference.getTitle());
title.setEnabled(override == null);
TextView summary = (TextView)
convertView.findViewById(R.id.summary);
summary.setText(override == null
? ((ListPreference) preference).getEntry()
: override);
summary.setEnabled(override == null);
// A little trick here, making the view focusable will eat
// both touch/key events on the view and thus make it looks
// like disabled.
convertView.setFocusable(override != null);
}
return convertView;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
// The last item (restore default)
if (position == mPreferences.size()) return true;
CameraPreference preference = mPreferences.get(position);
return !(preference instanceof PreferenceGroup);
}
public int getCount() {
// The last one is "restore default"
return mPreferences.size() + 1;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
if (position == mPreferences.size()) return 1;
CameraPreference pref = mPreferences.get(position);
if (pref instanceof PreferenceGroup) return 0;
if (pref instanceof ListPreference) return 1;
throw new IllegalStateException();
}
@Override
public int getViewTypeCount() {
// we have two types, see getItemViewType()
return 2;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isEmpty() {
return mPreferences.isEmpty();
}
}
private class SubMenuAdapter extends BaseAdapter
implements OnItemClickListener {
private final ListPreference mPreference;
private final IconListPreference mIconPreference;
public SubMenuAdapter(Context context, ListPreference preference) {
mPreference = preference;
mIconPreference = (preference instanceof IconListPreference)
? (IconListPreference) preference
: null;
}
public View getView(int position, View convertView, ViewGroup parent) {
CharSequence entry[] = mPreference.getEntries();
if (position == 0) {
convertView = inflateIfNeed(convertView,
R.layout.on_screen_menu_header, parent, false);
((TextView) convertView.findViewById(
R.id.title)).setText(mPreference.getTitle());
} else {
int index = position - 1;
convertView = inflateIfNeed(convertView,
R.layout.on_screen_submenu_item, parent, false);
boolean checked = mPreference.getValue().equals(
mPreference.getEntryValues()[index]);
String title = entry[index].toString();
String detail = null;
// Handle the title of format "Title (details)". We extract the
// details from the title message for better UI layout. The
// detail will be shown in second line with a smaller font.
Matcher matcher = TITLE_PATTERN.matcher(title);
if (matcher.matches()) {
title = matcher.group(1);
detail = matcher.group(2);
}
((TextView) convertView
.findViewById(R.id.title)).setText(title);
TextView detailView = (TextView)
convertView.findViewById(R.id.summary);
if (detail == null) {
detailView.setVisibility(View.GONE);
} else {
detailView.setVisibility(View.VISIBLE);
detailView.setText(detail);
}
((RadioButton) convertView.findViewById(
R.id.radio_button)).setChecked(checked);
ImageView icon = (ImageView)
convertView.findViewById(R.id.icon);
if (mIconPreference != null) {
icon.setVisibility(View.VISIBLE);
icon.setImageDrawable(
mIconPreference.getIcons()[position - 1]);
} else {
icon.setVisibility(View.GONE);
}
}
return convertView;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return getItemViewType(position) != 0;
}
public int getCount() {
// add one for the header
return mPreference.getEntries().length + 1;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position == 0 ? 0 : 1;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public boolean hasStableIds() {
return true;
}
public void onItemClick(
AdapterView<?> parent, View view, int position, long id) {
CharSequence values[] = mPreference.getEntryValues();
int idx = mPreference.findIndexOfValue(mPreference.getValue());
if (idx != position - 1) {
mPreference.setValueIndex(position - 1);
notifyDataSetChanged();
mMainAdapter.notifyDataSetChanged();
return;
}
// Close the sub menu when user presses the original option.
closeSubMenu();
}
}
private class Container extends FrameLayout {
public Container(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (super.onTouchEvent(event)) return true;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setVisible(false);
return true;
}
return false;
}
/*
* Need to override this to intercept the key events. Otherwise, we
* would attach a key listener to the container but its superclass
* ViewGroup gives it to the focused View instead of calling the key
* listener, and so we wouldn't get the events.
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return onContainerKey(event)
? true
: super.dispatchKeyEvent(event);
}
}
}
|