summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/OnScreenSettings.java
blob: 410790085d5d43560d503f01e498c6701d135b84 (plain)
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
package com.android.camera;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
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.CheckBox;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import java.util.ArrayList;

// Please reference to {@link android.widget.ZoomButtonsController} for detail
// information about adding window to WindowManager.
public class OnScreenSettings {
    private static final String TAG = "OnScreenSettings";
    private static final int MSG_POST_SET_HIDE = 1;
    private static final int MSG_POST_SET_VISIBLE = 2;

    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 View mMainPanel;
    private boolean mIsVisible = false;
    private OnVisibilityChangedListener mVisibilityListener;
    private MainMenuAdapter mMainAdapter;

    private final LayoutInflater mInflater;

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_POST_SET_HIDE:
                    setVisible(false);
                    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 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) {
            // Update main adapter before show up
            if (mMainAdapter != null) mMainAdapter.notifyDataSetChanged();
            if (mContainerLayoutParams.token == null) {
                mContainerLayoutParams.token = mOwnerView.getWindowToken();
            }
            mWindowManager.addView(mContainer, mContainerLayoutParams);
            refreshPositioningVariables();
        } else {
            // Reset the two menus
            mSubMenu.setAdapter(null);
            mSubMenu.setVisibility(View.INVISIBLE);
            mMainMenu.setVisibility(View.VISIBLE);

            mWindowManager.removeView(mContainer);
        }
        if (mVisibilityListener != null) {
            mVisibilityListener.onVisibilityChanged(mIsVisible);
        }
    }

    public void expandPanel() {
        setVisible(true);
        Util.slideIn(mMainPanel, Util.DIRECTION_LEFT);
    }

    public void collapsePanel() {
        Util.slideOut(mMainPanel, Util.DIRECTION_LEFT)
                .setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation animation) {
                // Cannot setVisible(false) here, GC will recycle something
                // still in use and result in SEGFAULT in skia
                mHandler.sendEmptyMessage(MSG_POST_SET_HIDE);
            }

            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    private void refreshPositioningVariables() {
        // if the mOwnerView is detached from window then skip.
        if (mOwnerView.getWindowToken() == null) return;

        // Position the zoom controls on the bottom of the owner view.
        int ownerHeight = mOwnerView.getHeight();
        int ownerWidth = mOwnerView.getWidth();
        // The gap between the top of the owner and the top of the container
        int containerOwnerYOffset = ownerHeight - mContainer.getHeight();

        // Calculate the owner view's bounds
        int[] mOwnerViewRawLocation = new int[2];
        mOwnerView.getLocationOnScreen(mOwnerViewRawLocation);

        // lp.x and lp.y should be relative to the owner's window top-left
        mContainerLayoutParams.x = mOwnerViewRawLocation[0];
        mContainerLayoutParams.y = mOwnerViewRawLocation[1];

        mContainerLayoutParams.width = ownerWidth * 2 / 3;
        mContainerLayoutParams.height = ownerHeight;

        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 = 0;
        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.TRANSPARENT;

        mContainerLayoutParams = lp;

        Container container = new Container(mContext);
        container.setLayoutParams(lp);

        mInflater.inflate(R.layout.on_screen_menu, container);

        mMainPanel = container.findViewById(R.id.main_panel);
        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:
                    collapsePanel();
                    return true;
            }
            return false;
        }
    }

    private boolean onContainerKey(KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_BACK:
                if (event.getAction() == KeyEvent.ACTION_UP) {
                    if (mSubMenu.getVisibility() == View.VISIBLE) {
                        closeSubMenu();
                    } else {
                        collapsePanel();
                    }
                    return true;
                }
            case KeyEvent.KEYCODE_MENU:
                if (event.getAction() == KeyEvent.ACTION_UP) {
                    collapsePanel();
                    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(
            Preference preference, ArrayList<Preference> list) {
        list.add(preference);
        if (preference instanceof PreferenceGroup) {
            PreferenceGroup group = (PreferenceGroup) preference;
            for (int i = 0, n = group.getPreferenceCount(); i < n; ++i) {
                Preference child = group.getPreference(i);
                addPreference(child, list);
            }
        }
    }

    public void setPreferenceScreen(PreferenceScreen screen) {
        ArrayList<Preference> list = new ArrayList<Preference>();

        // We don't want the screen add to the list, we add the first level
        // preference here.
        for (int  i = 0, n = screen.getPreferenceCount(); i < n; ++i) {
            addPreference(screen.getPreference(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<Preference> mPreferences;

        public MainMenuAdapter(
                Context context, ArrayList<Preference> preferences) {
            mPreferences = preferences;
        }

        public void onItemClick(
                AdapterView<?> parent, View view, int position, long id) {
            Preference preference = mPreferences.get(position);
            if (preference instanceof CheckBoxPreference) {
                CheckBoxPreference ckPref = (CheckBoxPreference) preference;
                ((CheckBox) view.findViewById(
                        R.id.check_box)).setChecked(!ckPref.isChecked());
                ckPref.setChecked(!ckPref.isChecked());
            } else if (preference instanceof ListPreference) {
                SubMenuAdapter adapter = new SubMenuAdapter(
                        mContext, (ListPreference) preference);
                mSubMenu.setAdapter(adapter);
                mSubMenu.setOnItemClickListener(adapter);
                showSubMenu();
            }
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            Preference 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 if (preference instanceof CheckBoxPreference) {
                convertView = inflateIfNeed(convertView,
                        R.layout.on_screen_menu_checkbox_item, parent, false);
                ((TextView) convertView.findViewById(
                        R.id.title)).setText(preference.getTitle());
                CheckBox checkBox = ((CheckBox)
                        convertView.findViewById(R.id.check_box));
                checkBox.setChecked(
                        ((CheckBoxPreference) preference).isChecked());
            } else if (preference instanceof ListPreference) {
                convertView = inflateIfNeed(convertView,
                        R.layout.on_screen_menu_list_item, parent, false);
                ((TextView) convertView.findViewById(
                        R.id.title)).setText(preference.getTitle());
                ((TextView) convertView.findViewById(R.id.summary))
                        .setText(((ListPreference) preference).getEntry());
            }
            return convertView;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEnabled(int position) {
            Preference preference = mPreferences.get(position);
            return !(preference instanceof PreferenceGroup);
        }

        public int getCount() {
            return mPreferences.size();
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemViewType(int position) {
            Preference pref = mPreferences.get(position);
            if (pref instanceof PreferenceGroup) return 0;
            if (pref instanceof ListPreference) return 1;
            if (pref instanceof CheckBoxPreference) return 2;
            throw new IllegalStateException();
        }

        @Override
        public int getViewTypeCount() {
            // we have three type, see getItemViewType()
            return 3;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isEmpty() {
            return mPreferences.isEmpty();
        }
    }

    private class SubMenuAdapter extends BaseAdapter
            implements OnItemClickListener {
        private final ListPreference mPreference;

        public SubMenuAdapter(Context context, ListPreference preference) {
            mPreference = preference;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            CharSequence entry[] = mPreference.getEntries();
            if (position == 0) {
                convertView = inflateIfNeed(convertView,
                        R.layout.on_screen_submenu_header, parent, false);
                ((TextView) convertView.findViewById(
                        R.id.title)).setText(mPreference.getDialogTitle());
            } else if (position == entry.length + 1) {
                convertView = inflateIfNeed(convertView,
                        R.layout.on_screen_submenu_cancel, parent, false);
            } else {
                convertView = inflateIfNeed(convertView,
                        R.layout.on_screen_submenu_item, parent, false);
                boolean checked = mPreference.getValue().equals(
                        mPreference.getEntryValues()[position - 1]);
                ((TextView) convertView.findViewById(
                        R.id.title)).setText(entry[position - 1]);
                RadioButton radio = ((RadioButton)
                        convertView.findViewById(R.id.radio_button));
                radio.setChecked(checked);
            }
            return convertView;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEnabled(int position) {
            return getItemViewType(position) != 0;
        }

        public int getCount() {
            // add one header and one cancel
            return mPreference.getEntries().length + 2;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0) return 0;
            if (position == getCount() - 1) return 1;
            return 2;
        }

        @Override
        public int getViewTypeCount() {
            return 3;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        public void onItemClick(
                AdapterView<?> parent, View view, int position, long id) {
            CharSequence values[] = mPreference.getEntryValues();
            if (position <= values.length) {
                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 on "back" or the original
            // option.
            closeSubMenu();
        }
    }

    private class Container extends FrameLayout {
        public Container(Context context) {
            super(context);
        }

        /*
         * 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);
        }
    }
}