summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/widget/AccessPointPreference.java
blob: 79dc27fdd44834abe7b60cb3bae1656b1b3524bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.android.tv.settings.widget;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.net.wifi.WifiConfiguration;
import android.os.Looper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

import com.android.settingslib.wifi.AccessPoint;
import com.android.settingslib.wifi.AccessPoint.Speed;
import com.android.tv.settings.R;

public class AccessPointPreference extends Preference {

  private static final int[] STATE_SECURED = {
      R.attr.state_encrypted
  };

  private static final int[] STATE_METERED = {
      R.attr.state_metered
  };

  private static final int[] FRICTION_ATTRS = {
      R.attr.wifi_friction
  };

  private static final int[] WIFI_CONNECTION_STRENGTH = {
      R.string.accessibility_no_wifi,
      R.string.accessibility_wifi_one_bar,
      R.string.accessibility_wifi_two_bars,
      R.string.accessibility_wifi_three_bars,
      R.string.accessibility_wifi_signal_full
  };

  @Nullable
  private final StateListDrawable mFrictionSld;
  private final int mBadgePadding;
  private final UserBadgeCache mBadgeCache;
  private final IconInjector mIconInjector;
  private TextView mTitleView;
  private boolean mShowDivider;

  private boolean mForSavedNetworks = false;
  private AccessPoint mAccessPoint;
  private int mLevel;
  private CharSequence mContentDescription;
  private int mDefaultIconResId;
  private int mWifiSpeed = Speed.NONE;

  @Nullable
  private static StateListDrawable getFrictionStateListDrawable(Context context) {
    TypedArray frictionSld;
    try {
      frictionSld = context.getTheme().obtainStyledAttributes(FRICTION_ATTRS);
    } catch (Resources.NotFoundException e) {
      // Fallback for platforms that do not need friction icon resources.
      frictionSld = null;
    }
    return frictionSld != null ? (StateListDrawable) frictionSld.getDrawable(0) : null;
  }

  // Used for fake pref.
  public AccessPointPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    mFrictionSld = null;
    mBadgePadding = 0;
    mBadgeCache = null;
    mIconInjector = new IconInjector(context);
  }

  public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
      boolean forSavedNetworks) {
    this(accessPoint, context, cache, 0 /* iconResId */, forSavedNetworks);
    refresh();
  }

  public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
      int iconResId, boolean forSavedNetworks) {
    this(accessPoint, context, cache, iconResId, forSavedNetworks,
        getFrictionStateListDrawable(context), -1 /* level */, new IconInjector(context));
  }

  @VisibleForTesting
  AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
      int iconResId, boolean forSavedNetworks, StateListDrawable frictionSld,
      int level, IconInjector iconInjector) {
    super(context);
    setLayoutResource(R.layout.preference_access_point_tv);
    setWidgetLayoutResource(getWidgetLayoutResourceId());
    mBadgeCache = cache;
    mAccessPoint = accessPoint;
    mForSavedNetworks = forSavedNetworks;
    mAccessPoint.setTag(this);
    mLevel = level;
    mDefaultIconResId = iconResId;
    mFrictionSld = frictionSld;
    mIconInjector = iconInjector;
    mBadgePadding = context.getResources()
        .getDimensionPixelSize(R.dimen.wifi_preference_badge_padding);
  }

  protected int getWidgetLayoutResourceId() {
    return R.layout.access_point_friction_widget;
  }

  public AccessPoint getAccessPoint() {
    return mAccessPoint;
  }

  @Override
  public void onBindViewHolder(final PreferenceViewHolder view) {
    super.onBindViewHolder(view);
    if (mAccessPoint == null) {
      // Used for fake pref.
      return;
    }
    Drawable drawable = getIcon();
    if (drawable != null) {
      drawable.setLevel(mLevel);
    }

    mTitleView = (TextView) view.findViewById(android.R.id.title);
    if (mTitleView != null) {
      // Attach to the end of the title view
      mTitleView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, null, null);
      mTitleView.setCompoundDrawablePadding(mBadgePadding);
    }
    view.itemView.setContentDescription(mContentDescription);

    ImageView frictionImageView = (ImageView) view.findViewById(R.id.friction_icon);
    bindFrictionImage(frictionImageView);

    final View divider = view.findViewById(R.id.two_target_divider);
    divider.setVisibility(shouldShowDivider() ? View.VISIBLE : View.INVISIBLE);
  }

  public boolean shouldShowDivider() {
    return mShowDivider;
  }

  public void setShowDivider(boolean showDivider) {
    mShowDivider = showDivider;
    notifyChanged();
  }

  protected void updateIcon(int level, Context context) {
    if (level == -1) {
      safeSetDefaultIcon();
      return;
    }

    Drawable drawable = mIconInjector.getIcon(level);
    if (!mForSavedNetworks && drawable != null) {
      drawable.setTintList(getColorAttr(context, android.R.attr.colorControlNormal));
      setIcon(drawable);
    } else {
      safeSetDefaultIcon();
    }
  }

  /**
   * Binds the friction icon drawable using a StateListDrawable.
   *
   * <p>Friction icons will be rebound when notifyChange() is called, and therefore
   * do not need to be managed in refresh()</p>.
   */
  private void bindFrictionImage(ImageView frictionImageView) {
    if (frictionImageView == null || mFrictionSld == null) {
      return;
    }
    if ((mAccessPoint.getSecurity() != AccessPoint.SECURITY_NONE)
        && (mAccessPoint.getSecurity() != AccessPoint.SECURITY_OWE)) {
      mFrictionSld.setState(STATE_SECURED);
    } else if (mAccessPoint.isMetered()) {
      mFrictionSld.setState(STATE_METERED);
    }
    Drawable drawable = mFrictionSld.getCurrent();
    frictionImageView.setImageDrawable(drawable);
  }

  private void safeSetDefaultIcon() {
    if (mDefaultIconResId != 0) {
      setIcon(mDefaultIconResId);
    } else {
      setIcon(null);
    }
  }

  protected void updateBadge(Context context) {
    WifiConfiguration config = mAccessPoint.getConfig();
    if (config != null) {
      // Fetch badge (may be null)
      // Get the badge using a cache since the PM will ask the UserManager for the list
      // of profiles every time otherwise.
    }
  }

  /**
   * Updates the title and summary; may indirectly call notifyChanged().
   */
  public void refresh() {
    setTitle(this, mAccessPoint);
    final Context context = getContext();
    int level = mAccessPoint.getLevel();
    int wifiSpeed = Speed.NONE;
    if (level != mLevel || wifiSpeed != mWifiSpeed) {
      mLevel = level;
      mWifiSpeed = wifiSpeed;
      updateIcon(mLevel, context);
      notifyChanged();
    }

    updateBadge(context);

    setSummary(mAccessPoint.getSettingsSummary());

    mContentDescription = buildContentDescription(getContext(), this /* pref */, mAccessPoint);
  }

  @Override
  protected void notifyChanged() {
    if (Looper.getMainLooper() != Looper.myLooper()) {
      // Let our BG thread callbacks call setTitle/setSummary.
      postNotifyChanged();
    } else {
      super.notifyChanged();
    }
  }

  @VisibleForTesting
  static void setTitle(AccessPointPreference preference, AccessPoint ap) {
    preference.setTitle(ap.getTitle());
  }

  /**
   * Helper method to generate content description string.
   */
  @VisibleForTesting
  static CharSequence buildContentDescription(Context context, Preference pref, AccessPoint ap) {
    CharSequence contentDescription = pref.getTitle();
    final CharSequence summary = pref.getSummary();
    if (!TextUtils.isEmpty(summary)) {
      contentDescription = TextUtils.concat(contentDescription, ",", summary);
    }
    int level = ap.getLevel();
    if (level >= 0 && level < WIFI_CONNECTION_STRENGTH.length) {
      contentDescription = TextUtils.concat(contentDescription, ",",
          context.getString(WIFI_CONNECTION_STRENGTH[level]));
    }
    return TextUtils.concat(contentDescription, ",",
        ap.getSecurity() == AccessPoint.SECURITY_NONE
            ? context.getString(R.string.accessibility_wifi_security_type_none)
            : context.getString(R.string.accessibility_wifi_security_type_secured));
  }

  public void onLevelChanged() {
    postNotifyChanged();
  }

  private void postNotifyChanged() {
    if (mTitleView != null) {
      mTitleView.post(mNotifyChanged);
    } // Otherwise we haven't been bound yet, and don't need to update.
  }

  private final Runnable mNotifyChanged = new Runnable() {
    @Override
    public void run() {
      notifyChanged();
    }
  };

  public static class UserBadgeCache {
    private final SparseArray<Drawable> mBadges = new SparseArray<>();
    private final PackageManager mPm;

    public UserBadgeCache(PackageManager pm) {
      mPm = pm;
    }
  }

  static class IconInjector {
    private final Context mContext;

    public IconInjector(Context context) {
      mContext = context;
    }

    public Drawable getIcon(int level) {
      return mContext.getDrawable(getWifiIconResource(level));
    }
  }

  static final int[] WIFI_PIE = {
      R.drawable.ic_wifi_signal_0,
      R.drawable.ic_wifi_signal_1,
      R.drawable.ic_wifi_signal_2,
      R.drawable.ic_wifi_signal_3,
      R.drawable.ic_wifi_signal_4
  };

  static final int[] SHOW_X_WIFI_PIE = {
      R.drawable.ic_show_x_wifi_signal_0,
      R.drawable.ic_show_x_wifi_signal_1,
      R.drawable.ic_show_x_wifi_signal_2,
      R.drawable.ic_show_x_wifi_signal_3,
      R.drawable.ic_show_x_wifi_signal_4
  };

  /**
   * Returns the Wifi icon resource for a given RSSI level.
   *
   * @param level The number of bars to show (0-4)
   * @throws IllegalArgumentException if an invalid RSSI level is given.
   */
  public static int getWifiIconResource(int level) {
    return getWifiIconResource(false /* showX */, level);
  }

  /**
   * Returns the Wifi icon resource for a given RSSI level.
   *
   * @param showX True if a connected Wi-Fi network has the problem which should show Pie+x
   *              signal icon to users.
   * @param level The number of bars to show (0-4)
   * @throws IllegalArgumentException if an invalid RSSI level is given.
   */
  public static int getWifiIconResource(boolean showX, int level) {
    if (level < 0 || level >= WIFI_PIE.length) {
      throw new IllegalArgumentException("No Wifi icon found for level: " + level);
    }
    return showX ? SHOW_X_WIFI_PIE[level] : WIFI_PIE[level];
  }

  public static ColorStateList getColorAttr(Context context, int attr) {
    TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
    ColorStateList stateList = null;
    try {
      stateList = ta.getColorStateList(0);
    } finally {
      ta.recycle();
    }
    return stateList;
  }
}