Merge "Add WifiTrackerLib as separate dependency from SettingsLib"
This commit is contained in:
@@ -73,6 +73,7 @@ android_library {
|
|||||||
"android.hardware.dumpstate-V1.0-java",
|
"android.hardware.dumpstate-V1.0-java",
|
||||||
"android.hardware.dumpstate-V1.1-java",
|
"android.hardware.dumpstate-V1.1-java",
|
||||||
"lottie",
|
"lottie",
|
||||||
|
"WifiTrackerLib",
|
||||||
],
|
],
|
||||||
|
|
||||||
libs: [
|
libs: [
|
||||||
|
@@ -68,6 +68,7 @@ import com.android.settings.wifi.AddNetworkFragment;
|
|||||||
import com.android.settings.wifi.AddWifiNetworkPreference;
|
import com.android.settings.wifi.AddWifiNetworkPreference;
|
||||||
import com.android.settings.wifi.ConfigureWifiEntryFragment;
|
import com.android.settings.wifi.ConfigureWifiEntryFragment;
|
||||||
import com.android.settings.wifi.ConnectedWifiEntryPreference;
|
import com.android.settings.wifi.ConnectedWifiEntryPreference;
|
||||||
|
import com.android.settings.wifi.LongPressWifiEntryPreference;
|
||||||
import com.android.settings.wifi.WifiConfigUiBase2;
|
import com.android.settings.wifi.WifiConfigUiBase2;
|
||||||
import com.android.settings.wifi.WifiConnectListener;
|
import com.android.settings.wifi.WifiConnectListener;
|
||||||
import com.android.settings.wifi.WifiDialog2;
|
import com.android.settings.wifi.WifiDialog2;
|
||||||
@@ -83,7 +84,6 @@ import com.android.settingslib.search.Indexable;
|
|||||||
import com.android.settingslib.search.SearchIndexable;
|
import com.android.settingslib.search.SearchIndexable;
|
||||||
import com.android.settingslib.utils.ThreadUtils;
|
import com.android.settingslib.utils.ThreadUtils;
|
||||||
import com.android.settingslib.widget.LayoutPreference;
|
import com.android.settingslib.widget.LayoutPreference;
|
||||||
import com.android.settingslib.wifi.LongPressWifiEntryPreference;
|
|
||||||
import com.android.settingslib.wifi.WifiSavedConfigUtils;
|
import com.android.settingslib.wifi.WifiSavedConfigUtils;
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
import com.android.wifitrackerlib.WifiEntry.ConnectCallback;
|
import com.android.wifitrackerlib.WifiEntry.ConnectCallback;
|
||||||
|
@@ -22,7 +22,6 @@ import androidx.fragment.app.Fragment;
|
|||||||
import androidx.preference.PreferenceViewHolder;
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
|
||||||
import com.android.settingslib.R;
|
import com.android.settingslib.R;
|
||||||
import com.android.settingslib.wifi.LongPressWifiEntryPreference;
|
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 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.
|
||||||
|
*/
|
||||||
|
package com.android.settings.wifi;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import androidx.fragment.app.Fragment;
|
||||||
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
|
||||||
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WifiEntryPreference that can be long pressed.
|
||||||
|
*/
|
||||||
|
public class LongPressWifiEntryPreference extends WifiEntryPreference {
|
||||||
|
|
||||||
|
private final Fragment mFragment;
|
||||||
|
|
||||||
|
public LongPressWifiEntryPreference(Context context, WifiEntry wifiEntry, Fragment fragment) {
|
||||||
|
super(context, wifiEntry);
|
||||||
|
mFragment = fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(final PreferenceViewHolder view) {
|
||||||
|
super.onBindViewHolder(view);
|
||||||
|
if (mFragment != null) {
|
||||||
|
view.itemView.setOnCreateContextMenuListener(mFragment);
|
||||||
|
view.itemView.setTag(this);
|
||||||
|
view.itemView.setLongClickable(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -37,7 +37,6 @@ import com.android.settings.overlay.FeatureFactory;
|
|||||||
import com.android.settings.wifi.details2.WifiNetworkDetailsFragment2;
|
import com.android.settings.wifi.details2.WifiNetworkDetailsFragment2;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
import com.android.settingslib.wifi.WifiEntryPreference;
|
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
import com.android.wifitrackerlib.WifiPickerTracker;
|
import com.android.wifitrackerlib.WifiPickerTracker;
|
||||||
|
|
||||||
|
315
src/com/android/settings/wifi/WifiEntryPreference.java
Normal file
315
src/com/android/settings/wifi/WifiEntryPreference.java
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 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.
|
||||||
|
*/
|
||||||
|
package com.android.settings.wifi;
|
||||||
|
|
||||||
|
import android.annotation.Nullable;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.content.res.TypedArray;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.graphics.drawable.StateListDrawable;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ImageButton;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
|
||||||
|
import androidx.annotation.DrawableRes;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
import androidx.preference.Preference;
|
||||||
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
|
||||||
|
import com.android.settingslib.R;
|
||||||
|
import com.android.settingslib.Utils;
|
||||||
|
import com.android.settingslib.wifi.WifiUtils;
|
||||||
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Preference to display a WifiEntry in a wifi picker.
|
||||||
|
*/
|
||||||
|
public class WifiEntryPreference extends Preference implements WifiEntry.WifiEntryCallback,
|
||||||
|
View.OnClickListener {
|
||||||
|
|
||||||
|
private static final int[] STATE_SECURED = {
|
||||||
|
R.attr.state_encrypted
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final int[] FRICTION_ATTRS = {
|
||||||
|
R.attr.wifi_friction
|
||||||
|
};
|
||||||
|
|
||||||
|
// These values must be kept within [WifiEntry.WIFI_LEVEL_MIN, WifiEntry.WIFI_LEVEL_MAX]
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
// StateListDrawable to display secured lock / metered "$" icon
|
||||||
|
@Nullable private final StateListDrawable mFrictionSld;
|
||||||
|
private final IconInjector mIconInjector;
|
||||||
|
private WifiEntry mWifiEntry;
|
||||||
|
private int mLevel = -1;
|
||||||
|
private boolean mShowX; // Shows the Wi-Fi signl icon of Pie+x when it's true.
|
||||||
|
private CharSequence mContentDescription;
|
||||||
|
private OnButtonClickListener mOnButtonClickListener;
|
||||||
|
|
||||||
|
public WifiEntryPreference(@NonNull Context context, @NonNull WifiEntry wifiEntry) {
|
||||||
|
this(context, wifiEntry, new IconInjector(context));
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
WifiEntryPreference(@NonNull Context context, @NonNull WifiEntry wifiEntry,
|
||||||
|
@NonNull IconInjector iconInjector) {
|
||||||
|
super(context);
|
||||||
|
|
||||||
|
setLayoutResource(R.layout.preference_access_point);
|
||||||
|
setWidgetLayoutResource(R.layout.access_point_friction_widget);
|
||||||
|
mFrictionSld = getFrictionStateListDrawable();
|
||||||
|
mWifiEntry = wifiEntry;
|
||||||
|
mWifiEntry.setListener(this);
|
||||||
|
mIconInjector = iconInjector;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public WifiEntry getWifiEntry() {
|
||||||
|
return mWifiEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(final PreferenceViewHolder view) {
|
||||||
|
super.onBindViewHolder(view);
|
||||||
|
final Drawable drawable = getIcon();
|
||||||
|
if (drawable != null) {
|
||||||
|
drawable.setLevel(mLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
view.itemView.setContentDescription(mContentDescription);
|
||||||
|
|
||||||
|
// Turn off divider
|
||||||
|
view.findViewById(R.id.two_target_divider).setVisibility(View.INVISIBLE);
|
||||||
|
|
||||||
|
// Enable the icon button when the help string in this WifiEntry is not null.
|
||||||
|
final ImageButton imageButton = (ImageButton) view.findViewById(R.id.icon_button);
|
||||||
|
final ImageView frictionImageView = (ImageView) view.findViewById(
|
||||||
|
R.id.friction_icon);
|
||||||
|
if (mWifiEntry.getHelpUriString() != null
|
||||||
|
&& mWifiEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_DISCONNECTED) {
|
||||||
|
final Drawable drawablehelp = getDrawable(R.drawable.ic_help);
|
||||||
|
drawablehelp.setTintList(
|
||||||
|
Utils.getColorAttr(getContext(), android.R.attr.colorControlNormal));
|
||||||
|
((ImageView) imageButton).setImageDrawable(drawablehelp);
|
||||||
|
imageButton.setVisibility(View.VISIBLE);
|
||||||
|
imageButton.setOnClickListener(this);
|
||||||
|
imageButton.setContentDescription(
|
||||||
|
getContext().getText(R.string.help_label));
|
||||||
|
|
||||||
|
if (frictionImageView != null) {
|
||||||
|
frictionImageView.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
imageButton.setVisibility(View.GONE);
|
||||||
|
|
||||||
|
if (frictionImageView != null) {
|
||||||
|
frictionImageView.setVisibility(View.VISIBLE);
|
||||||
|
bindFrictionImage(frictionImageView);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the title and summary; may indirectly call notifyChanged().
|
||||||
|
*/
|
||||||
|
public void refresh() {
|
||||||
|
setTitle(mWifiEntry.getTitle());
|
||||||
|
final int level = mWifiEntry.getLevel();
|
||||||
|
final boolean showX = mWifiEntry.shouldShowXLevelIcon();
|
||||||
|
if (level != mLevel || showX != mShowX) {
|
||||||
|
mLevel = level;
|
||||||
|
mShowX = showX;
|
||||||
|
updateIcon(mShowX, mLevel);
|
||||||
|
notifyChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
setSummary(mWifiEntry.getSummary(false /* concise */));
|
||||||
|
mContentDescription = buildContentDescription();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates the state of the WifiEntry has changed and clients may retrieve updates through
|
||||||
|
* the WifiEntry getter methods.
|
||||||
|
*/
|
||||||
|
public void onUpdated() {
|
||||||
|
// TODO(b/70983952): Fill this method in
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Result of the connect request indicated by the WifiEntry.CONNECT_STATUS constants.
|
||||||
|
*/
|
||||||
|
public void onConnectResult(int status) {
|
||||||
|
// TODO(b/70983952): Fill this method in
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Result of the disconnect request indicated by the WifiEntry.DISCONNECT_STATUS constants.
|
||||||
|
*/
|
||||||
|
public void onDisconnectResult(int status) {
|
||||||
|
// TODO(b/70983952): Fill this method in
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Result of the forget request indicated by the WifiEntry.FORGET_STATUS constants.
|
||||||
|
*/
|
||||||
|
public void onForgetResult(int status) {
|
||||||
|
// TODO(b/70983952): Fill this method in
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Result of the sign-in request indecated by the WifiEntry.SIGNIN_STATUS constants
|
||||||
|
*/
|
||||||
|
public void onSignInResult(int status) {
|
||||||
|
// TODO(b/70983952): Fill this method in
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int getIconColorAttr() {
|
||||||
|
final boolean accent = (mWifiEntry.hasInternetAccess()
|
||||||
|
&& mWifiEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTED);
|
||||||
|
return accent ? android.R.attr.colorAccent : android.R.attr.colorControlNormal;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateIcon(boolean showX, int level) {
|
||||||
|
if (level == -1) {
|
||||||
|
setIcon(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Drawable drawable = mIconInjector.getIcon(showX, level);
|
||||||
|
if (drawable != null) {
|
||||||
|
drawable.setTint(Utils.getColorAttrDefaultColor(getContext(), getIconColorAttr()));
|
||||||
|
setIcon(drawable);
|
||||||
|
} else {
|
||||||
|
setIcon(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private StateListDrawable getFrictionStateListDrawable() {
|
||||||
|
TypedArray frictionSld;
|
||||||
|
try {
|
||||||
|
frictionSld = getContext().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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 ((mWifiEntry.getSecurity() != WifiEntry.SECURITY_NONE)
|
||||||
|
&& (mWifiEntry.getSecurity() != WifiEntry.SECURITY_OWE)) {
|
||||||
|
mFrictionSld.setState(STATE_SECURED);
|
||||||
|
}
|
||||||
|
frictionImageView.setImageDrawable(mFrictionSld.getCurrent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to generate content description string.
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
CharSequence buildContentDescription() {
|
||||||
|
final Context context = getContext();
|
||||||
|
|
||||||
|
CharSequence contentDescription = getTitle();
|
||||||
|
final CharSequence summary = getSummary();
|
||||||
|
if (!TextUtils.isEmpty(summary)) {
|
||||||
|
contentDescription = TextUtils.concat(contentDescription, ",", summary);
|
||||||
|
}
|
||||||
|
int level = mWifiEntry.getLevel();
|
||||||
|
if (level >= 0 && level < WIFI_CONNECTION_STRENGTH.length) {
|
||||||
|
contentDescription = TextUtils.concat(contentDescription, ",",
|
||||||
|
context.getString(WIFI_CONNECTION_STRENGTH[level]));
|
||||||
|
}
|
||||||
|
return TextUtils.concat(contentDescription, ",",
|
||||||
|
mWifiEntry.getSecurity() == WifiEntry.SECURITY_NONE
|
||||||
|
? context.getString(R.string.accessibility_wifi_security_type_none)
|
||||||
|
: context.getString(R.string.accessibility_wifi_security_type_secured));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static class IconInjector {
|
||||||
|
private final Context mContext;
|
||||||
|
|
||||||
|
IconInjector(Context context) {
|
||||||
|
mContext = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Drawable getIcon(boolean showX, int level) {
|
||||||
|
return mContext.getDrawable(WifiUtils.getInternetIconResource(level, showX));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set listeners, who want to listen the button client event.
|
||||||
|
*/
|
||||||
|
public void setOnButtonClickListener(OnButtonClickListener listener) {
|
||||||
|
mOnButtonClickListener = listener;
|
||||||
|
notifyChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
if (view.getId() == R.id.icon_button) {
|
||||||
|
if (mOnButtonClickListener != null) {
|
||||||
|
mOnButtonClickListener.onButtonClick(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback to inform the caller that the icon button is clicked.
|
||||||
|
*/
|
||||||
|
public interface OnButtonClickListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register to listen the button click event.
|
||||||
|
*/
|
||||||
|
void onButtonClick(WifiEntryPreference preference);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Drawable getDrawable(@DrawableRes int iconResId) {
|
||||||
|
Drawable buttonIcon = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
buttonIcon = getContext().getDrawable(iconResId);
|
||||||
|
} catch (Resources.NotFoundException exception) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
return buttonIcon;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -74,7 +74,6 @@ import com.android.settingslib.RestrictedLockUtils;
|
|||||||
import com.android.settingslib.RestrictedLockUtilsInternal;
|
import com.android.settingslib.RestrictedLockUtilsInternal;
|
||||||
import com.android.settingslib.search.Indexable;
|
import com.android.settingslib.search.Indexable;
|
||||||
import com.android.settingslib.search.SearchIndexable;
|
import com.android.settingslib.search.SearchIndexable;
|
||||||
import com.android.settingslib.wifi.LongPressWifiEntryPreference;
|
|
||||||
import com.android.settingslib.wifi.WifiSavedConfigUtils;
|
import com.android.settingslib.wifi.WifiSavedConfigUtils;
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
import com.android.wifitrackerlib.WifiEntry.ConnectCallback;
|
import com.android.wifitrackerlib.WifiEntry.ConnectCallback;
|
||||||
|
@@ -41,7 +41,7 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.SettingsPreferenceFragment;
|
import com.android.settings.SettingsPreferenceFragment;
|
||||||
import com.android.settings.core.SubSettingLauncher;
|
import com.android.settings.core.SubSettingLauncher;
|
||||||
import com.android.settings.wifi.AddNetworkFragment;
|
import com.android.settings.wifi.AddNetworkFragment;
|
||||||
import com.android.settingslib.wifi.WifiEntryPreference;
|
import com.android.settings.wifi.WifiEntryPreference;
|
||||||
import com.android.wifitrackerlib.SavedNetworkTracker;
|
import com.android.wifitrackerlib.SavedNetworkTracker;
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
|
@@ -25,7 +25,7 @@ import androidx.preference.PreferenceGroup;
|
|||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settingslib.wifi.WifiEntryPreference;
|
import com.android.settings.wifi.WifiEntryPreference;
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@@ -64,11 +64,11 @@ import com.android.settings.testutils.shadow.ShadowDataUsageUtils;
|
|||||||
import com.android.settings.testutils.shadow.ShadowFragment;
|
import com.android.settings.testutils.shadow.ShadowFragment;
|
||||||
import com.android.settings.wifi.AddWifiNetworkPreference;
|
import com.android.settings.wifi.AddWifiNetworkPreference;
|
||||||
import com.android.settings.wifi.ConnectedWifiEntryPreference;
|
import com.android.settings.wifi.ConnectedWifiEntryPreference;
|
||||||
|
import com.android.settings.wifi.LongPressWifiEntryPreference;
|
||||||
import com.android.settings.wifi.WifiConfigController2;
|
import com.android.settings.wifi.WifiConfigController2;
|
||||||
import com.android.settings.wifi.WifiDialog2;
|
import com.android.settings.wifi.WifiDialog2;
|
||||||
import com.android.settingslib.connectivity.ConnectivitySubsystemsRecoveryManager;
|
import com.android.settingslib.connectivity.ConnectivitySubsystemsRecoveryManager;
|
||||||
import com.android.settingslib.widget.LayoutPreference;
|
import com.android.settingslib.widget.LayoutPreference;
|
||||||
import com.android.settingslib.wifi.LongPressWifiEntryPreference;
|
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
import com.android.wifitrackerlib.WifiPickerTracker;
|
import com.android.wifitrackerlib.WifiPickerTracker;
|
||||||
|
|
||||||
|
@@ -34,8 +34,8 @@ import androidx.preference.PreferenceCategory;
|
|||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settings.wifi.WifiConnectionPreferenceController;
|
import com.android.settings.wifi.WifiConnectionPreferenceController;
|
||||||
|
import com.android.settings.wifi.WifiEntryPreference;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
import com.android.settingslib.wifi.WifiEntryPreference;
|
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
import com.android.wifitrackerlib.WifiPickerTracker;
|
import com.android.wifitrackerlib.WifiPickerTracker;
|
||||||
|
|
||||||
|
@@ -0,0 +1,255 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 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.
|
||||||
|
*/
|
||||||
|
package com.android.settings.wifi;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.LinearLayout;
|
||||||
|
|
||||||
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
|
||||||
|
import com.android.settingslib.R;
|
||||||
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
public class WifiEntryPreferenceTest {
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private WifiEntry mMockWifiEntry;
|
||||||
|
@Mock
|
||||||
|
private WifiEntryPreference.IconInjector mMockIconInjector;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockDrawable0;
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockDrawable1;
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockDrawable2;
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockDrawable3;
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockDrawable4;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockShowXDrawable0;
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockShowXDrawable1;
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockShowXDrawable2;
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockShowXDrawable3;
|
||||||
|
@Mock
|
||||||
|
private Drawable mMockShowXDrawable4;
|
||||||
|
|
||||||
|
private static final String MOCK_TITLE = "title";
|
||||||
|
private static final String MOCK_SUMMARY = "summary";
|
||||||
|
private static final String FAKE_URI_STRING = "fakeuri";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
when(mMockWifiEntry.getTitle()).thenReturn(MOCK_TITLE);
|
||||||
|
when(mMockWifiEntry.getSummary(false /* concise */)).thenReturn(MOCK_SUMMARY);
|
||||||
|
|
||||||
|
when(mMockIconInjector.getIcon(false /* showX */, 0)).thenReturn(mMockDrawable0);
|
||||||
|
when(mMockIconInjector.getIcon(false /* showX */, 1)).thenReturn(mMockDrawable1);
|
||||||
|
when(mMockIconInjector.getIcon(false /* showX */, 2)).thenReturn(mMockDrawable2);
|
||||||
|
when(mMockIconInjector.getIcon(false /* showX */, 3)).thenReturn(mMockDrawable3);
|
||||||
|
when(mMockIconInjector.getIcon(false /* showX */, 4)).thenReturn(mMockDrawable4);
|
||||||
|
|
||||||
|
when(mMockIconInjector.getIcon(true /* showX */, 0))
|
||||||
|
.thenReturn(mMockShowXDrawable0);
|
||||||
|
when(mMockIconInjector.getIcon(true /* showX */, 1))
|
||||||
|
.thenReturn(mMockShowXDrawable1);
|
||||||
|
when(mMockIconInjector.getIcon(true /* showX */, 2))
|
||||||
|
.thenReturn(mMockShowXDrawable2);
|
||||||
|
when(mMockIconInjector.getIcon(true /* showX */, 3))
|
||||||
|
.thenReturn(mMockShowXDrawable3);
|
||||||
|
when(mMockIconInjector.getIcon(true /* showX */, 4))
|
||||||
|
.thenReturn(mMockShowXDrawable4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructor_shouldSetWifiEntryTitleAndSummary() {
|
||||||
|
final WifiEntryPreference pref =
|
||||||
|
new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector);
|
||||||
|
|
||||||
|
assertThat(pref.getTitle()).isEqualTo(MOCK_TITLE);
|
||||||
|
assertThat(pref.getSummary()).isEqualTo(MOCK_SUMMARY);
|
||||||
|
assertThat(false).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructor_shouldSetIcon() {
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(0);
|
||||||
|
|
||||||
|
final WifiEntryPreference pref =
|
||||||
|
new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector);
|
||||||
|
|
||||||
|
assertThat(pref.getIcon()).isEqualTo(mMockDrawable0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void titleChanged_refresh_shouldUpdateTitle() {
|
||||||
|
final WifiEntryPreference pref =
|
||||||
|
new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector);
|
||||||
|
final String updatedTitle = "updated title";
|
||||||
|
when(mMockWifiEntry.getTitle()).thenReturn(updatedTitle);
|
||||||
|
|
||||||
|
pref.refresh();
|
||||||
|
|
||||||
|
assertThat(pref.getTitle()).isEqualTo(updatedTitle);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void summaryChanged_refresh_shouldUpdateSummary() {
|
||||||
|
final WifiEntryPreference pref =
|
||||||
|
new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector);
|
||||||
|
final String updatedSummary = "updated summary";
|
||||||
|
when(mMockWifiEntry.getSummary(false /* concise */)).thenReturn(updatedSummary);
|
||||||
|
|
||||||
|
pref.refresh();
|
||||||
|
|
||||||
|
assertThat(pref.getSummary()).isEqualTo(updatedSummary);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void levelChanged_refresh_shouldUpdateLevelIcon() {
|
||||||
|
final List<Drawable> iconList = new ArrayList<>();
|
||||||
|
final WifiEntryPreference pref =
|
||||||
|
new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector);
|
||||||
|
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(0);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(1);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(2);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(3);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(4);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(-1);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
|
||||||
|
assertThat(iconList).containsExactly(mMockDrawable0, mMockDrawable1,
|
||||||
|
mMockDrawable2, mMockDrawable3, mMockDrawable4, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void levelChanged_showXWifiRefresh_shouldUpdateLevelIcon() {
|
||||||
|
final List<Drawable> iconList = new ArrayList<>();
|
||||||
|
when(mMockWifiEntry.shouldShowXLevelIcon()).thenReturn(true);
|
||||||
|
final WifiEntryPreference pref =
|
||||||
|
new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector);
|
||||||
|
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(0);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(1);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(2);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(3);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(4);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
when(mMockWifiEntry.getLevel()).thenReturn(-1);
|
||||||
|
pref.refresh();
|
||||||
|
iconList.add(pref.getIcon());
|
||||||
|
|
||||||
|
assertThat(iconList).containsExactly(mMockShowXDrawable0, mMockShowXDrawable1,
|
||||||
|
mMockShowXDrawable2, mMockShowXDrawable3, mMockShowXDrawable4, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void notNull_whenGetHelpUriString_shouldSetImageButtonVisible() {
|
||||||
|
when(mMockWifiEntry.getHelpUriString()).thenReturn(FAKE_URI_STRING);
|
||||||
|
final WifiEntryPreference pref =
|
||||||
|
new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector);
|
||||||
|
final LayoutInflater inflater = LayoutInflater.from(mContext);
|
||||||
|
final View view = inflater.inflate(pref.getLayoutResource(), new LinearLayout(mContext),
|
||||||
|
false);
|
||||||
|
final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(view);
|
||||||
|
|
||||||
|
pref.onBindViewHolder(holder);
|
||||||
|
|
||||||
|
assertThat(view.findViewById(R.id.icon_button).getVisibility()).isEqualTo(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void helpButton_whenGetHelpUriStringNotNull_shouldSetCorrectContentDescription() {
|
||||||
|
when(mMockWifiEntry.getHelpUriString()).thenReturn(FAKE_URI_STRING);
|
||||||
|
final WifiEntryPreference pref =
|
||||||
|
new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector);
|
||||||
|
final LayoutInflater inflater = LayoutInflater.from(mContext);
|
||||||
|
final View view = inflater.inflate(pref.getLayoutResource(), new LinearLayout(mContext),
|
||||||
|
false);
|
||||||
|
final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(view);
|
||||||
|
|
||||||
|
pref.onBindViewHolder(holder);
|
||||||
|
|
||||||
|
assertThat(view.findViewById(R.id.icon_button).getContentDescription()).isEqualTo(
|
||||||
|
mContext.getString(R.string.help_label));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void subscriptionEntry_shouldSetImageButtonGone() {
|
||||||
|
when(mMockWifiEntry.isSubscription()).thenReturn(true);
|
||||||
|
final WifiEntryPreference pref =
|
||||||
|
new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector);
|
||||||
|
final LayoutInflater inflater = LayoutInflater.from(mContext);
|
||||||
|
final View view = inflater.inflate(pref.getLayoutResource(), new LinearLayout(mContext),
|
||||||
|
false);
|
||||||
|
final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(view);
|
||||||
|
|
||||||
|
pref.onBindViewHolder(holder);
|
||||||
|
|
||||||
|
assertThat(view.findViewById(R.id.icon_button).getVisibility()).isEqualTo(View.GONE);
|
||||||
|
}
|
||||||
|
}
|
@@ -57,7 +57,6 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.datausage.DataUsagePreference;
|
import com.android.settings.datausage.DataUsagePreference;
|
||||||
import com.android.settings.testutils.shadow.ShadowDataUsageUtils;
|
import com.android.settings.testutils.shadow.ShadowDataUsageUtils;
|
||||||
import com.android.settings.testutils.shadow.ShadowFragment;
|
import com.android.settings.testutils.shadow.ShadowFragment;
|
||||||
import com.android.settingslib.wifi.LongPressWifiEntryPreference;
|
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
import com.android.wifitrackerlib.WifiPickerTracker;
|
import com.android.wifitrackerlib.WifiPickerTracker;
|
||||||
|
|
||||||
|
@@ -36,7 +36,7 @@ import androidx.preference.Preference;
|
|||||||
import androidx.preference.PreferenceCategory;
|
import androidx.preference.PreferenceCategory;
|
||||||
import androidx.test.InstrumentationRegistry;
|
import androidx.test.InstrumentationRegistry;
|
||||||
|
|
||||||
import com.android.settingslib.wifi.WifiEntryPreference;
|
import com.android.settings.wifi.WifiEntryPreference;
|
||||||
import com.android.wifitrackerlib.SavedNetworkTracker;
|
import com.android.wifitrackerlib.SavedNetworkTracker;
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
|
@@ -35,7 +35,7 @@ import android.content.Context;
|
|||||||
import androidx.preference.PreferenceCategory;
|
import androidx.preference.PreferenceCategory;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settingslib.wifi.WifiEntryPreference;
|
import com.android.settings.wifi.WifiEntryPreference;
|
||||||
import com.android.wifitrackerlib.WifiEntry;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
Reference in New Issue
Block a user