13ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk/*
2ea05f87b253ce20a08158768951169b1cda0a623Jason Monk * Copyright (C) 2017 The Android Open Source Project
33ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk *
43ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
53ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk * except in compliance with the License. You may obtain a copy of the License at
63ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk *
73ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
83ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk *
93ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk * Unless required by applicable law or agreed to in writing, software distributed under the
103ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
113ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk * KIND, either express or implied. See the License for the specific language governing
123ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk * permissions and limitations under the License.
133ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk */
143ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
153ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monkpackage com.android.systemui.tuner;
163ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
178457ad89a851323288561d85ac86c6c26666c904Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.KEY;
188457ad89a851323288561d85ac86c6c26666c904Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.KEY_CODE_END;
198457ad89a851323288561d85ac86c6c26666c904Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.KEY_CODE_START;
208457ad89a851323288561d85ac86c6c26666c904Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.KEY_IMAGE_DELIM;
213ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.MENU_IME;
223ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.NAVSPACE;
23ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.NAV_BAR_LEFT;
24ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.NAV_BAR_RIGHT;
253ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.NAV_BAR_VIEWS;
26c0d7058b14c24cd07912f5629c26b39b7b4673d5Winsonimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.extractButton;
27ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.extractImage;
28ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.extractKeycode;
293ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
30ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.annotation.Nullable;
31ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.app.AlertDialog;
32ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.content.DialogInterface;
33ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.content.DialogInterface.OnClickListener;
34ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.content.res.Resources;
35ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.graphics.Color;
36790442e4cd8106cf2c69d23ff185953b620b087aJason Monkimport android.graphics.Paint;
37790442e4cd8106cf2c69d23ff185953b620b087aJason Monkimport android.graphics.Paint.FontMetricsInt;
38790442e4cd8106cf2c69d23ff185953b620b087aJason Monkimport android.graphics.Rect;
39ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.graphics.drawable.Drawable;
40ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.graphics.drawable.Icon;
41ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.os.Bundle;
42ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.os.Handler;
43ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v14.preference.PreferenceFragment;
44ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v7.preference.DropDownPreference;
45ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v7.preference.ListPreference;
46ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v7.preference.Preference;
47ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v7.preference.Preference.OnPreferenceChangeListener;
48ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v7.preference.Preference.OnPreferenceClickListener;
49ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v7.preference.PreferenceCategory;
50ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.text.SpannableStringBuilder;
51ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.text.style.ImageSpan;
52ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.util.Log;
53ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.util.TypedValue;
54ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.view.KeyEvent;
55ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.widget.EditText;
563ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
57de850bbcaa61c1874b803f2086443febbafd81a4Jason Monkimport com.android.systemui.Dependency;
58ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport com.android.systemui.R;
59ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport com.android.systemui.statusbar.phone.NavigationBarInflaterView;
60ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport com.android.systemui.tuner.TunerService.Tunable;
613ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
62ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport java.util.ArrayList;
63a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
64790442e4cd8106cf2c69d23ff185953b620b087aJason Monkpublic class NavBarTuner extends TunerPreferenceFragment {
653ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
66ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String LAYOUT = "layout";
67ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String LEFT = "left";
68ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String RIGHT = "right";
69a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
70ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String TYPE = "type";
71ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String KEYCODE = "keycode";
72ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String ICON = "icon";
73a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
74790442e4cd8106cf2c69d23ff185953b620b087aJason Monk    private static final int[][] ICONS = new int[][]{
75790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_qs_circle, R.string.tuner_circle},
76790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_add, R.string.tuner_plus},
77790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_remove, R.string.tuner_minus},
78790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_left, R.string.tuner_left},
79790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_right, R.string.tuner_right},
80790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_menu, R.string.tuner_menu},
81ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    };
82a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
83ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private final ArrayList<Tunable> mTunables = new ArrayList<>();
84ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private Handler mHandler;
85a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
86ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    @Override
87ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    public void onCreate(@Nullable Bundle savedInstanceState) {
88ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        mHandler = new Handler();
89ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        super.onCreate(savedInstanceState);
90a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    }
91a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
92ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    @Override
93ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
94ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        super.onActivityCreated(savedInstanceState);
95ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
963ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
973ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
983ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    @Override
99ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
100ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        addPreferencesFromResource(R.xml.nav_bar_tuner);
101ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        bindLayout((ListPreference) findPreference(LAYOUT));
102790442e4cd8106cf2c69d23ff185953b620b087aJason Monk        bindButton(NAV_BAR_LEFT, NAVSPACE, LEFT);
103790442e4cd8106cf2c69d23ff185953b620b087aJason Monk        bindButton(NAV_BAR_RIGHT, MENU_IME, RIGHT);
1043ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
1053ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
1063ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    @Override
107ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    public void onDestroy() {
108ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        super.onDestroy();
109de850bbcaa61c1874b803f2086443febbafd81a4Jason Monk        mTunables.forEach(t -> Dependency.get(TunerService.class).removeTunable(t));
1103ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
1113ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
112ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private void addTunable(Tunable tunable, String... keys) {
113ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        mTunables.add(tunable);
114de850bbcaa61c1874b803f2086443febbafd81a4Jason Monk        Dependency.get(TunerService.class).addTunable(tunable, keys);
1153ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
1163ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
117ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private void bindLayout(ListPreference preference) {
118ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        addTunable((key, newValue) -> mHandler.post(() -> {
119ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String val = newValue;
120ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            if (val == null) {
121ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                val = "default";
122ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            }
123ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            preference.setValue(val);
124ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        }), NAV_BAR_VIEWS);
125ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        preference.setOnPreferenceChangeListener((preference1, newValue) -> {
126ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String val = (String) newValue;
127ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            if ("default".equals(val)) val = null;
128de850bbcaa61c1874b803f2086443febbafd81a4Jason Monk            Dependency.get(TunerService.class).setValue(NAV_BAR_VIEWS, val);
129ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            return true;
130ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        });
1313ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
1323ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
133790442e4cd8106cf2c69d23ff185953b620b087aJason Monk    private void bindButton(String setting, String def, String k) {
134790442e4cd8106cf2c69d23ff185953b620b087aJason Monk        ListPreference type = (ListPreference) findPreference(TYPE + "_" + k);
135ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        Preference keycode = findPreference(KEYCODE + "_" + k);
136ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        ListPreference icon = (ListPreference) findPreference(ICON + "_" + k);
137ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        setupIcons(icon);
138ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        addTunable((key, newValue) -> mHandler.post(() -> {
139ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String val = newValue;
140ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            if (val == null) {
141ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                val = def;
142ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            }
143ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String button = extractButton(val);
144ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            if (button.startsWith(KEY)) {
145ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                type.setValue(KEY);
146ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                String uri = extractImage(button);
147ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                int code = extractKeycode(button);
148ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                icon.setValue(uri);
149ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                updateSummary(icon);
150ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                keycode.setSummary(code + "");
151ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                keycode.setVisible(true);
152ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                icon.setVisible(true);
1533ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk            } else {
154ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                type.setValue(button);
155ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                keycode.setVisible(false);
156ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                icon.setVisible(false);
157ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            }
158ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        }), setting);
159ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        OnPreferenceChangeListener listener = (preference, newValue) -> {
160ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            mHandler.post(() -> {
161ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                setValue(setting, type, keycode, icon);
162ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                updateSummary(icon);
163ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            });
1643ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk            return true;
165ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        };
166ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        type.setOnPreferenceChangeListener(listener);
167ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        icon.setOnPreferenceChangeListener(listener);
168ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        keycode.setOnPreferenceClickListener(preference -> {
169ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            EditText editText = new EditText(getContext());
170ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            new AlertDialog.Builder(getContext())
171ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .setTitle(preference.getTitle())
172ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .setView(editText)
173ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .setNegativeButton(android.R.string.cancel, null)
174ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .setPositiveButton(android.R.string.ok, (dialog, which) -> {
175ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        int code = KeyEvent.KEYCODE_ENTER;
176ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        try {
177ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                            code = Integer.parseInt(editText.getText().toString());
178ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        } catch (Exception e) {
179ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        }
180ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        keycode.setSummary(code + "");
181ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        setValue(setting, type, keycode, icon);
182ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    }).show();
1833ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk            return true;
184ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        });
1853ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
1863ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
187ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private void updateSummary(ListPreference icon) {
188ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        try {
189ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14,
190ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    getContext().getResources().getDisplayMetrics());
191ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String pkg = icon.getValue().split("/")[0];
192ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            int id = Integer.parseInt(icon.getValue().split("/")[1]);
193ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            SpannableStringBuilder builder = new SpannableStringBuilder();
194ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            Drawable d = Icon.createWithResource(pkg, id)
195ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .loadDrawable(getContext());
196ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            d.setTint(Color.BLACK);
197ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            d.setBounds(0, 0, size, size);
198790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
199ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            builder.append("  ", span, 0);
200790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            builder.append(" ");
201790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            for (int i = 0; i < ICONS.length; i++) {
202790442e4cd8106cf2c69d23ff185953b620b087aJason Monk                if (ICONS[i][0] == id) {
203790442e4cd8106cf2c69d23ff185953b620b087aJason Monk                    builder.append(getString(ICONS[i][1]));
204790442e4cd8106cf2c69d23ff185953b620b087aJason Monk                }
205790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            }
206ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            icon.setSummary(builder);
207ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        } catch (Exception e) {
208ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            Log.d("NavButton", "Problem with summary", e);
209ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            icon.setSummary(null);
2103ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk        }
2113ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
2123ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
213790442e4cd8106cf2c69d23ff185953b620b087aJason Monk    private void setValue(String setting, ListPreference type, Preference keycode,
214ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            ListPreference icon) {
215ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        String button = type.getValue();
216ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        if (KEY.equals(button)) {
217ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String uri = icon.getValue();
218ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            int code = KeyEvent.KEYCODE_ENTER;
219ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            try {
220ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                code = Integer.parseInt(keycode.getSummary().toString());
221ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            } catch (Exception e) {
2223ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk            }
223ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            button = button + KEY_CODE_START + code + KEY_IMAGE_DELIM + uri + KEY_CODE_END;
2243ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk        }
225de850bbcaa61c1874b803f2086443febbafd81a4Jason Monk        Dependency.get(TunerService.class).setValue(setting, button);
2263ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
2273ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
228ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private void setupIcons(ListPreference icon) {
229ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        CharSequence[] labels = new CharSequence[ICONS.length];
230ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        CharSequence[] values = new CharSequence[ICONS.length];
231ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14,
232ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                getContext().getResources().getDisplayMetrics());
233ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        for (int i = 0; i < ICONS.length; i++) {
234ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            SpannableStringBuilder builder = new SpannableStringBuilder();
235790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            Drawable d = Icon.createWithResource(getContext().getPackageName(), ICONS[i][0])
236ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .loadDrawable(getContext());
237ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            d.setTint(Color.BLACK);
238ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            d.setBounds(0, 0, size, size);
239790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
240ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            builder.append("  ", span, 0);
241790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            builder.append(" ");
242790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            builder.append(getString(ICONS[i][1]));
243ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            labels[i] = builder;
244790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            values[i] = getContext().getPackageName() + "/" + ICONS[i][0];
245ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        }
246ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        icon.setEntries(labels);
247ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        icon.setEntryValues(values);
2483ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
2493ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk}
250