• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.settings.system;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.text.BidiFormatter;
23 import android.text.SpannableString;
24 import android.text.SpannableStringBuilder;
25 import android.text.TextDirectionHeuristics;
26 import android.text.style.BulletSpan;
27 
28 import androidx.annotation.StringRes;
29 import androidx.preference.Preference;
30 
31 import com.android.car.settings.R;
32 import com.android.car.settings.common.FragmentController;
33 import com.android.car.settings.common.PreferenceController;
34 
35 /** Controller to determine which items appear as resetable within the reset network description. */
36 public class ResetNetworkItemsPreferenceController extends PreferenceController<Preference> {
37 
ResetNetworkItemsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)38     public ResetNetworkItemsPreferenceController(Context context, String preferenceKey,
39             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
40         super(context, preferenceKey, fragmentController, uxRestrictions);
41     }
42 
43     @Override
getPreferenceType()44     protected Class<Preference> getPreferenceType() {
45         return Preference.class;
46     }
47 
48     @Override
updateState(Preference preference)49     protected void updateState(Preference preference) {
50         SpannableStringBuilder sb = new SpannableStringBuilder();
51         sb.append(getContext().getString(R.string.reset_network_desc));
52         sb.append(System.lineSeparator());
53         if (hasFeature(PackageManager.FEATURE_WIFI)) {
54             addBulletedValue(sb, R.string.reset_network_item_wifi);
55         }
56         if (hasFeature(PackageManager.FEATURE_TELEPHONY)) {
57             addBulletedValue(sb, R.string.reset_network_item_mobile);
58         }
59         if (hasFeature(PackageManager.FEATURE_BLUETOOTH)) {
60             addBulletedValue(sb, R.string.reset_network_item_bluetooth);
61         }
62         preference.setTitle(sb);
63     }
64 
hasFeature(String feature)65     private boolean hasFeature(String feature) {
66         return getContext().getPackageManager().hasSystemFeature(feature);
67     }
68 
addBulletedValue(SpannableStringBuilder sb, @StringRes int resId)69     private void addBulletedValue(SpannableStringBuilder sb, @StringRes int resId) {
70         sb.append(System.lineSeparator());
71         SpannableString value = new SpannableString(
72                 BidiFormatter.getInstance().unicodeWrap(getContext().getString(resId),
73                         TextDirectionHeuristics.LOCALE));
74         // Match android.content.res.StringBlock which applies a 10 gapWidth BulletSpan as the <li>
75         // style. This is a workaround for translation specific behavior in StringBlock which led
76         // to multiple indents when building the list using getText(resId).
77         value.setSpan(new BulletSpan(/* gapWidth= */ 10), /* start= */ 0,
78                 value.length(), /* flags= */ 0);
79         sb.append(value);
80     }
81 }
82