4444import androidx .preference .ListPreference ;
4545import androidx .preference .Preference ;
4646import androidx .preference .Preference .OnPreferenceChangeListener ;
47+ import androidx .preference .PreferenceFragment ;
4748
4849import com .android .systemui .Dependency ;
4950import com .android .systemui .R ;
5051import com .android .systemui .tuner .TunerService .Tunable ;
5152
5253import java .util .ArrayList ;
5354
54- public class NavBarTuner extends TunerPreferenceFragment {
55+ public class NavBarTuner extends PreferenceFragment {
5556
5657 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- };
7258
7359 private final ArrayList <Tunable > mTunables = new ArrayList <>();
7460 private Handler mHandler ;
@@ -89,8 +75,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
8975 public void onCreatePreferences (Bundle savedInstanceState , String rootKey ) {
9076 addPreferencesFromResource (R .xml .nav_bar_tuner );
9177 bindLayout ((ListPreference ) findPreference (LAYOUT ));
92- bindButton (NAV_BAR_LEFT , NAVSPACE , LEFT );
93- bindButton (NAV_BAR_RIGHT , MENU_IME_ROTATE , RIGHT );
9478 }
9579
9680 @ Override
@@ -114,126 +98,11 @@ private void bindLayout(ListPreference preference) {
11498 }), NAV_BAR_VIEWS );
11599 preference .setOnPreferenceChangeListener ((preference1 , newValue ) -> {
116100 String val = (String ) newValue ;
101+ int valueIndex = preference .findIndexOfValue (val );
102+ preference .setSummary (preference .getEntries ()[valueIndex ]);
117103 if ("default" .equals (val )) val = null ;
118104 Dependency .get (TunerService .class ).setValue (NAV_BAR_VIEWS , val );
119105 return true ;
120106 });
121107 }
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- }
239108}
0 commit comments