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;
21a48cf192f37bc94740594a1731716403bb03ec39Mike Digmanimport static com.android.systemui.statusbar.phone.NavigationBarInflaterView.MENU_IME_ROTATE;
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.graphics.Color;
33ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.graphics.drawable.Drawable;
34ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.graphics.drawable.Icon;
35ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.os.Bundle;
36ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.os.Handler;
37ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v7.preference.ListPreference;
38ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v7.preference.Preference;
39ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.support.v7.preference.Preference.OnPreferenceChangeListener;
40ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.text.SpannableStringBuilder;
41ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.text.style.ImageSpan;
42ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.util.Log;
43ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.util.TypedValue;
44ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.view.KeyEvent;
45ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport android.widget.EditText;
463ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
47de850bbcaa61c1874b803f2086443febbafd81a4Jason Monkimport com.android.systemui.Dependency;
48ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport com.android.systemui.R;
49ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport com.android.systemui.tuner.TunerService.Tunable;
503ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
51ea05f87b253ce20a08158768951169b1cda0a623Jason Monkimport java.util.ArrayList;
52a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
53790442e4cd8106cf2c69d23ff185953b620b087aJason Monkpublic class NavBarTuner extends TunerPreferenceFragment {
543ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
55ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String LAYOUT = "layout";
56ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String LEFT = "left";
57ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String RIGHT = "right";
58a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
59ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String TYPE = "type";
60ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String KEYCODE = "keycode";
61ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private static final String ICON = "icon";
62a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
63790442e4cd8106cf2c69d23ff185953b620b087aJason Monk    private static final int[][] ICONS = new int[][]{
64790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_qs_circle, R.string.tuner_circle},
65790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_add, R.string.tuner_plus},
66790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_remove, R.string.tuner_minus},
67790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_left, R.string.tuner_left},
68790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_right, R.string.tuner_right},
69790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            {R.drawable.ic_menu, R.string.tuner_menu},
70ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    };
71a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
72ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private final ArrayList<Tunable> mTunables = new ArrayList<>();
73ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private Handler mHandler;
74a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
75ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    @Override
76ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    public void onCreate(@Nullable Bundle savedInstanceState) {
77ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        mHandler = new Handler();
78ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        super.onCreate(savedInstanceState);
79a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    }
80a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
81ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    @Override
82ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
83ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        super.onActivityCreated(savedInstanceState);
84ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
853ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
863ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
873ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    @Override
88ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
89ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        addPreferencesFromResource(R.xml.nav_bar_tuner);
90ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        bindLayout((ListPreference) findPreference(LAYOUT));
91790442e4cd8106cf2c69d23ff185953b620b087aJason Monk        bindButton(NAV_BAR_LEFT, NAVSPACE, LEFT);
92a48cf192f37bc94740594a1731716403bb03ec39Mike Digman        bindButton(NAV_BAR_RIGHT, MENU_IME_ROTATE, RIGHT);
933ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
943ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
953ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    @Override
96ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    public void onDestroy() {
97ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        super.onDestroy();
98de850bbcaa61c1874b803f2086443febbafd81a4Jason Monk        mTunables.forEach(t -> Dependency.get(TunerService.class).removeTunable(t));
993ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
1003ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
101ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private void addTunable(Tunable tunable, String... keys) {
102ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        mTunables.add(tunable);
103de850bbcaa61c1874b803f2086443febbafd81a4Jason Monk        Dependency.get(TunerService.class).addTunable(tunable, keys);
1043ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
1053ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
106ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private void bindLayout(ListPreference preference) {
107ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        addTunable((key, newValue) -> mHandler.post(() -> {
108ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String val = newValue;
109ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            if (val == null) {
110ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                val = "default";
111ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            }
112ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            preference.setValue(val);
113ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        }), NAV_BAR_VIEWS);
114ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        preference.setOnPreferenceChangeListener((preference1, newValue) -> {
115ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String val = (String) newValue;
116ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            if ("default".equals(val)) val = null;
117de850bbcaa61c1874b803f2086443febbafd81a4Jason Monk            Dependency.get(TunerService.class).setValue(NAV_BAR_VIEWS, val);
118ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            return true;
119ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        });
1203ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
1213ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
122790442e4cd8106cf2c69d23ff185953b620b087aJason Monk    private void bindButton(String setting, String def, String k) {
123790442e4cd8106cf2c69d23ff185953b620b087aJason Monk        ListPreference type = (ListPreference) findPreference(TYPE + "_" + k);
124ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        Preference keycode = findPreference(KEYCODE + "_" + k);
125ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        ListPreference icon = (ListPreference) findPreference(ICON + "_" + k);
126ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        setupIcons(icon);
127ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        addTunable((key, newValue) -> mHandler.post(() -> {
128ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String val = newValue;
129ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            if (val == null) {
130ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                val = def;
131ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            }
132ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String button = extractButton(val);
133ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            if (button.startsWith(KEY)) {
134ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                type.setValue(KEY);
135ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                String uri = extractImage(button);
136ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                int code = extractKeycode(button);
137ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                icon.setValue(uri);
138ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                updateSummary(icon);
139ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                keycode.setSummary(code + "");
140ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                keycode.setVisible(true);
141ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                icon.setVisible(true);
1423ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk            } else {
143ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                type.setValue(button);
144ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                keycode.setVisible(false);
145ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                icon.setVisible(false);
146ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            }
147ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        }), setting);
148ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        OnPreferenceChangeListener listener = (preference, newValue) -> {
149ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            mHandler.post(() -> {
150ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                setValue(setting, type, keycode, icon);
151ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                updateSummary(icon);
152ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            });
1533ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk            return true;
154ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        };
155ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        type.setOnPreferenceChangeListener(listener);
156ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        icon.setOnPreferenceChangeListener(listener);
157ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        keycode.setOnPreferenceClickListener(preference -> {
158ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            EditText editText = new EditText(getContext());
159ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            new AlertDialog.Builder(getContext())
160ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .setTitle(preference.getTitle())
161ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .setView(editText)
162ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .setNegativeButton(android.R.string.cancel, null)
163ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .setPositiveButton(android.R.string.ok, (dialog, which) -> {
164ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        int code = KeyEvent.KEYCODE_ENTER;
165ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        try {
166ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                            code = Integer.parseInt(editText.getText().toString());
167ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        } catch (Exception e) {
168ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        }
169ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        keycode.setSummary(code + "");
170ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                        setValue(setting, type, keycode, icon);
171ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    }).show();
1723ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk            return true;
173ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        });
1743ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
1753ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
176ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private void updateSummary(ListPreference icon) {
177ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        try {
178ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14,
179ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    getContext().getResources().getDisplayMetrics());
180ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String pkg = icon.getValue().split("/")[0];
181ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            int id = Integer.parseInt(icon.getValue().split("/")[1]);
182ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            SpannableStringBuilder builder = new SpannableStringBuilder();
183ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            Drawable d = Icon.createWithResource(pkg, id)
184ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .loadDrawable(getContext());
185ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            d.setTint(Color.BLACK);
186ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            d.setBounds(0, 0, size, size);
187790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
188ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            builder.append("  ", span, 0);
189790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            builder.append(" ");
190790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            for (int i = 0; i < ICONS.length; i++) {
191790442e4cd8106cf2c69d23ff185953b620b087aJason Monk                if (ICONS[i][0] == id) {
192790442e4cd8106cf2c69d23ff185953b620b087aJason Monk                    builder.append(getString(ICONS[i][1]));
193790442e4cd8106cf2c69d23ff185953b620b087aJason Monk                }
194790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            }
195ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            icon.setSummary(builder);
196ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        } catch (Exception e) {
197ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            Log.d("NavButton", "Problem with summary", e);
198ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            icon.setSummary(null);
1993ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk        }
2003ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
2013ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
202790442e4cd8106cf2c69d23ff185953b620b087aJason Monk    private void setValue(String setting, ListPreference type, Preference keycode,
203ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            ListPreference icon) {
204ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        String button = type.getValue();
205ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        if (KEY.equals(button)) {
206ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            String uri = icon.getValue();
207ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            int code = KeyEvent.KEYCODE_ENTER;
208ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            try {
209ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                code = Integer.parseInt(keycode.getSummary().toString());
210ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            } catch (Exception e) {
2113ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk            }
212ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            button = button + KEY_CODE_START + code + KEY_IMAGE_DELIM + uri + KEY_CODE_END;
2133ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk        }
214de850bbcaa61c1874b803f2086443febbafd81a4Jason Monk        Dependency.get(TunerService.class).setValue(setting, button);
2153ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
2163ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk
217ea05f87b253ce20a08158768951169b1cda0a623Jason Monk    private void setupIcons(ListPreference icon) {
218ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        CharSequence[] labels = new CharSequence[ICONS.length];
219ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        CharSequence[] values = new CharSequence[ICONS.length];
220ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14,
221ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                getContext().getResources().getDisplayMetrics());
222ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        for (int i = 0; i < ICONS.length; i++) {
223ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            SpannableStringBuilder builder = new SpannableStringBuilder();
224790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            Drawable d = Icon.createWithResource(getContext().getPackageName(), ICONS[i][0])
225ea05f87b253ce20a08158768951169b1cda0a623Jason Monk                    .loadDrawable(getContext());
226ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            d.setTint(Color.BLACK);
227ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            d.setBounds(0, 0, size, size);
228790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
229ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            builder.append("  ", span, 0);
230790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            builder.append(" ");
231790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            builder.append(getString(ICONS[i][1]));
232ea05f87b253ce20a08158768951169b1cda0a623Jason Monk            labels[i] = builder;
233790442e4cd8106cf2c69d23ff185953b620b087aJason Monk            values[i] = getContext().getPackageName() + "/" + ICONS[i][0];
234ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        }
235ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        icon.setEntries(labels);
236ea05f87b253ce20a08158768951169b1cda0a623Jason Monk        icon.setEntryValues(values);
2373ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk    }
2383ebd23943b87d5f9d9a59889b4316e631d0e7d15Jason Monk}
239