Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2c096596 authored by Stephen Chen's avatar Stephen Chen
Browse files

Add Wi-Fi friction icons to AccessPointPreference.

Friction icons are added to the end of the Wi-Fi picker list items to
provide visual information such as whether a network is secured, or saved.
The first two badges added are a lock icon to indicate a secure network, and
an open lock to indicate a saved network that is also secure.

Test: manual inspection
Bug: 34281330
Change-Id: I04ce9bc4a5bd245398d5f3b308ccb30b7cf58270
parent 699fd453
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2017 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/friction_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@null" />
+5 −0
Original line number Diff line number Diff line
@@ -36,7 +36,12 @@
    <declare-styleable name="WifiEncryptionState">
        <attr name="state_encrypted" format="boolean" />
    </declare-styleable>
    <declare-styleable name="WifiSavedState">
        <attr name="state_saved" format="boolean" />
    </declare-styleable>

    <attr name="wifi_signal" format="reference" />
    <attr name="wifi_friction" format="reference" />

    <declare-styleable name="UsageView">
        <attr name="android:colorAccent" />
+57 −1
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.settingslib.wifi;

import android.content.Context;
import android.content.pm.PackageManager;
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;
@@ -28,6 +30,7 @@ import android.support.v7.preference.PreferenceViewHolder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.settingslib.R;

@@ -36,11 +39,17 @@ public class AccessPointPreference extends Preference {
    private static final int[] STATE_SECURED = {
            R.attr.state_encrypted
    };
    private static final int[] STATE_SAVED = {
            R.attr.state_encrypted,
            R.attr.state_saved
    };
    private static final int[] STATE_NONE = {};

    private static int[] wifi_signal_attributes = { R.attr.wifi_signal };
    private static final int[] wifi_signal_attributes = { R.attr.wifi_signal };
    private static final int[] wifi_friction_attributes = { R.attr.wifi_friction };

    private final StateListDrawable mWifiSld;
    private final StateListDrawable mFrictionSld;
    private final int mBadgePadding;
    private final UserBadgeCache mBadgeCache;
    private TextView mTitleView;
@@ -63,6 +72,7 @@ public class AccessPointPreference extends Preference {
    public AccessPointPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mWifiSld = null;
        mFrictionSld = null;
        mBadgePadding = 0;
        mBadgeCache = null;
    }
@@ -70,6 +80,7 @@ public class AccessPointPreference extends Preference {
    public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
            boolean forSavedNetworks) {
        super(context);
        setWidgetLayoutResource(R.layout.access_point_friction_widget);
        mBadgeCache = cache;
        mAccessPoint = accessPoint;
        mForSavedNetworks = forSavedNetworks;
@@ -79,6 +90,15 @@ public class AccessPointPreference extends Preference {
        mWifiSld = (StateListDrawable) context.getTheme()
                .obtainStyledAttributes(wifi_signal_attributes).getDrawable(0);

        TypedArray frictionSld;
        try {
            frictionSld = context.getTheme().obtainStyledAttributes(wifi_friction_attributes);
        } catch (Resources.NotFoundException e) {
            // Fallback for platforms that do not need friction icon resources.
            frictionSld = null;
        }
        mFrictionSld = frictionSld != null ? (StateListDrawable) frictionSld.getDrawable(0) : null;

        // Distance from the end of the title at which this AP's user badge should sit.
        mBadgePadding = context.getResources()
                .getDimensionPixelSize(R.dimen.wifi_preference_badge_padding);
@@ -88,6 +108,7 @@ public class AccessPointPreference extends Preference {
    public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
            int iconResId, boolean forSavedNetworks) {
        super(context);
        setWidgetLayoutResource(R.layout.access_point_friction_widget);
        mBadgeCache = cache;
        mAccessPoint = accessPoint;
        mForSavedNetworks = forSavedNetworks;
@@ -98,6 +119,15 @@ public class AccessPointPreference extends Preference {
        mWifiSld = (StateListDrawable) context.getTheme()
                .obtainStyledAttributes(wifi_signal_attributes).getDrawable(0);

        TypedArray frictionSld;
        try {
            frictionSld = context.getTheme().obtainStyledAttributes(wifi_friction_attributes);
        } catch (Resources.NotFoundException e) {
            // Fallback for platforms that do not need friction icon resources.
            frictionSld = null;
        }
        mFrictionSld = frictionSld != null ? (StateListDrawable) frictionSld.getDrawable(0) : null;

        // Distance from the end of the title at which this AP's user badge should sit.
        mBadgePadding = context.getResources()
                .getDimensionPixelSize(R.dimen.wifi_preference_badge_padding);
@@ -126,6 +156,11 @@ public class AccessPointPreference extends Preference {
            mTitleView.setCompoundDrawablePadding(mBadgePadding);
        }
        view.itemView.setContentDescription(mContentDescription);

        if (!mForSavedNetworks) {
            ImageView frictionImageView = (ImageView) view.findViewById(R.id.friction_icon);
            bindFrictionImage(frictionImageView);
        }
    }

    protected void updateIcon(int level, Context context) {
@@ -152,6 +187,27 @@ public class AccessPointPreference extends Preference {
        }
    }

    /**
     * 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) {
            if (mAccessPoint.isSaved()) {
                mFrictionSld.setState(STATE_SAVED);
            } else {
                mFrictionSld.setState(STATE_SECURED);
            }
        }
        Drawable drawable = mFrictionSld.getCurrent();
        frictionImageView.setImageDrawable(drawable);
    }

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