Home | History | Annotate | Download | only in tuner
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 
     15 package com.android.systemui.tuner;
     16 
     17 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.KEY;
     18 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.KEY_CODE_END;
     19 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.KEY_CODE_START;
     20 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.KEY_IMAGE_DELIM;
     21 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.MENU_IME_ROTATE;
     22 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.NAVSPACE;
     23 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.NAV_BAR_LEFT;
     24 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.NAV_BAR_RIGHT;
     25 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.NAV_BAR_VIEWS;
     26 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.extractButton;
     27 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.extractImage;
     28 import static com.android.systemui.statusbar.phone.NavigationBarInflaterView.extractKeycode;
     29 
     30 import android.annotation.Nullable;
     31 import android.app.AlertDialog;
     32 import android.graphics.Color;
     33 import android.graphics.drawable.Drawable;
     34 import android.graphics.drawable.Icon;
     35 import android.os.Bundle;
     36 import android.os.Handler;
     37 import android.text.SpannableStringBuilder;
     38 import android.text.style.ImageSpan;
     39 import android.util.Log;
     40 import android.util.TypedValue;
     41 import android.view.KeyEvent;
     42 import android.widget.EditText;
     43 
     44 import androidx.preference.ListPreference;
     45 import androidx.preference.Preference;
     46 import androidx.preference.Preference.OnPreferenceChangeListener;
     47 
     48 import com.android.systemui.Dependency;
     49 import com.android.systemui.R;
     50 import com.android.systemui.tuner.TunerService.Tunable;
     51 
     52 import java.util.ArrayList;
     53 
     54 public class NavBarTuner extends TunerPreferenceFragment {
     55 
     56     private static final String LAYOUT = "layout";
     57     private static final String LEFT = "left";
     58     private static final String RIGHT = "right";
     59 
     60     private static final String TYPE = "type";
     61     private static final String KEYCODE = "keycode";
     62     private static final String ICON = "icon";
     63 
     64     private static final int[][] ICONS = new int[][]{
     65             {R.drawable.ic_qs_circle, R.string.tuner_circle},
     66             {R.drawable.ic_add, R.string.tuner_plus},
     67             {R.drawable.ic_remove, R.string.tuner_minus},
     68             {R.drawable.ic_left, R.string.tuner_left},
     69             {R.drawable.ic_right, R.string.tuner_right},
     70             {R.drawable.ic_menu, R.string.tuner_menu},
     71     };
     72 
     73     private final ArrayList<Tunable> mTunables = new ArrayList<>();
     74     private Handler mHandler;
     75 
     76     @Override
     77     public void onCreate(@Nullable Bundle savedInstanceState) {
     78         mHandler = new Handler();
     79         super.onCreate(savedInstanceState);
     80     }
     81 
     82     @Override
     83     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
     84         super.onActivityCreated(savedInstanceState);
     85         getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
     86     }
     87 
     88     @Override
     89     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
     90         addPreferencesFromResource(R.xml.nav_bar_tuner);
     91         bindLayout((ListPreference) findPreference(LAYOUT));
     92         bindButton(NAV_BAR_LEFT, NAVSPACE, LEFT);
     93         bindButton(NAV_BAR_RIGHT, MENU_IME_ROTATE, RIGHT);
     94     }
     95 
     96     @Override
     97     public void onDestroy() {
     98         super.onDestroy();
     99         mTunables.forEach(t -> Dependency.get(TunerService.class).removeTunable(t));
    100     }
    101 
    102     private void addTunable(Tunable tunable, String... keys) {
    103         mTunables.add(tunable);
    104         Dependency.get(TunerService.class).addTunable(tunable, keys);
    105     }
    106 
    107     private void bindLayout(ListPreference preference) {
    108         addTunable((key, newValue) -> mHandler.post(() -> {
    109             String val = newValue;
    110             if (val == null) {
    111                 val = "default";
    112             }
    113             preference.setValue(val);
    114         }), NAV_BAR_VIEWS);
    115         preference.setOnPreferenceChangeListener((preference1, newValue) -> {
    116             String val = (String) newValue;
    117             if ("default".equals(val)) val = null;
    118             Dependency.get(TunerService.class).setValue(NAV_BAR_VIEWS, val);
    119             return true;
    120         });
    121     }
    122 
    123     private void bindButton(String setting, String def, String k) {
    124         ListPreference type = (ListPreference) findPreference(TYPE + "_" + k);
    125         Preference keycode = findPreference(KEYCODE + "_" + k);
    126         ListPreference icon = (ListPreference) findPreference(ICON + "_" + k);
    127         setupIcons(icon);
    128         addTunable((key, newValue) -> mHandler.post(() -> {
    129             String val = newValue;
    130             if (val == null) {
    131                 val = def;
    132             }
    133             String button = extractButton(val);
    134             if (button.startsWith(KEY)) {
    135                 type.setValue(KEY);
    136                 String uri = extractImage(button);
    137                 int code = extractKeycode(button);
    138                 icon.setValue(uri);
    139                 updateSummary(icon);
    140                 keycode.setSummary(code + "");
    141                 keycode.setVisible(true);
    142                 icon.setVisible(true);
    143             } else {
    144                 type.setValue(button);
    145                 keycode.setVisible(false);
    146                 icon.setVisible(false);
    147             }
    148         }), setting);
    149         OnPreferenceChangeListener listener = (preference, newValue) -> {
    150             mHandler.post(() -> {
    151                 setValue(setting, type, keycode, icon);
    152                 updateSummary(icon);
    153             });
    154             return true;
    155         };
    156         type.setOnPreferenceChangeListener(listener);
    157         icon.setOnPreferenceChangeListener(listener);
    158         keycode.setOnPreferenceClickListener(preference -> {
    159             EditText editText = new EditText(getContext());
    160             new AlertDialog.Builder(getContext())
    161                     .setTitle(preference.getTitle())
    162                     .setView(editText)
    163                     .setNegativeButton(android.R.string.cancel, null)
    164                     .setPositiveButton(android.R.string.ok, (dialog, which) -> {
    165                         int code = KeyEvent.KEYCODE_ENTER;
    166                         try {
    167                             code = Integer.parseInt(editText.getText().toString());
    168                         } catch (Exception e) {
    169                         }
    170                         keycode.setSummary(code + "");
    171                         setValue(setting, type, keycode, icon);
    172                     }).show();
    173             return true;
    174         });
    175     }
    176 
    177     private void updateSummary(ListPreference icon) {
    178         try {
    179             int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14,
    180                     getContext().getResources().getDisplayMetrics());
    181             String pkg = icon.getValue().split("/")[0];
    182             int id = Integer.parseInt(icon.getValue().split("/")[1]);
    183             SpannableStringBuilder builder = new SpannableStringBuilder();
    184             Drawable d = Icon.createWithResource(pkg, id)
    185                     .loadDrawable(getContext());
    186             d.setTint(Color.BLACK);
    187             d.setBounds(0, 0, size, size);
    188             ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    189             builder.append("  ", span, 0);
    190             builder.append(" ");
    191             for (int i = 0; i < ICONS.length; i++) {
    192                 if (ICONS[i][0] == id) {
    193                     builder.append(getString(ICONS[i][1]));
    194                 }
    195             }
    196             icon.setSummary(builder);
    197         } catch (Exception e) {
    198             Log.d("NavButton", "Problem with summary", e);
    199             icon.setSummary(null);
    200         }
    201     }
    202 
    203     private void setValue(String setting, ListPreference type, Preference keycode,
    204             ListPreference icon) {
    205         String button = type.getValue();
    206         if (KEY.equals(button)) {
    207             String uri = icon.getValue();
    208             int code = KeyEvent.KEYCODE_ENTER;
    209             try {
    210                 code = Integer.parseInt(keycode.getSummary().toString());
    211             } catch (Exception e) {
    212             }
    213             button = button + KEY_CODE_START + code + KEY_IMAGE_DELIM + uri + KEY_CODE_END;
    214         }
    215         Dependency.get(TunerService.class).setValue(setting, button);
    216     }
    217 
    218     private void setupIcons(ListPreference icon) {
    219         CharSequence[] labels = new CharSequence[ICONS.length];
    220         CharSequence[] values = new CharSequence[ICONS.length];
    221         int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14,
    222                 getContext().getResources().getDisplayMetrics());
    223         for (int i = 0; i < ICONS.length; i++) {
    224             SpannableStringBuilder builder = new SpannableStringBuilder();
    225             Drawable d = Icon.createWithResource(getContext().getPackageName(), ICONS[i][0])
    226                     .loadDrawable(getContext());
    227             d.setTint(Color.BLACK);
    228             d.setBounds(0, 0, size, size);
    229             ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    230             builder.append("  ", span, 0);
    231             builder.append(" ");
    232             builder.append(getString(ICONS[i][1]));
    233             labels[i] = builder;
    234             values[i] = getContext().getPackageName() + "/" + ICONS[i][0];
    235         }
    236         icon.setEntries(labels);
    237         icon.setEntryValues(values);
    238     }
    239 }
    240