Move AccessPointPreference to SettingsLib

Change-Id: Ib4a7c64e976aed5e75ec04fa72e2fdbd12923371
This commit is contained in:
Tony Mantler
2016-01-22 14:59:22 -08:00
parent 4cd2b33dec
commit 99b255c8a5
8 changed files with 87 additions and 292 deletions

View File

@@ -15,9 +15,6 @@
--> -->
<resources> <resources>
<declare-styleable name="WifiEncryptionState">
<attr name="state_encrypted" format="boolean" />
</declare-styleable>
<declare-styleable name="IconPreferenceScreen"> <declare-styleable name="IconPreferenceScreen">
<attr name="icon" /> <attr name="icon" />
</declare-styleable> </declare-styleable>

View File

@@ -207,7 +207,6 @@
<!-- WiFi Preferences --> <!-- WiFi Preferences -->
<dimen name="wifi_divider_height">1px</dimen> <dimen name="wifi_divider_height">1px</dimen>
<dimen name="wifi_preference_badge_padding">8dip</dimen>
<!-- Color picker --> <!-- Color picker -->
<dimen name="color_swatch_size">16dp</dimen> <dimen name="color_swatch_size">16dp</dimen>

View File

@@ -24,7 +24,6 @@
<attr name="setup_divider_color" format="reference" /> <attr name="setup_divider_color" format="reference" />
<attr name="side_margin" format="reference|dimension" /> <attr name="side_margin" format="reference|dimension" />
<attr name="wifi_signal_color" format="reference" /> <attr name="wifi_signal_color" format="reference" />
<attr name="wifi_signal" format="reference" />
<style name="SetupWizardDisableAppStartingTheme"> <style name="SetupWizardDisableAppStartingTheme">
<!-- Theme to disable the app starting window. The actual theme of the activity needs to <!-- Theme to disable the app starting window. The actual theme of the activity needs to

View File

@@ -21,7 +21,7 @@
<!-- Needed so PreferenceGroupAdapter allows AccessPointPreference to be <!-- Needed so PreferenceGroupAdapter allows AccessPointPreference to be
recycled. Removed in onResume --> recycled. Removed in onResume -->
<com.android.settings.wifi.AccessPointPreference <com.android.settings.wifi.LongPressAccessPointPreference
android:key="dummy" /> android:key="dummy" />
</PreferenceScreen> </PreferenceScreen>

View File

@@ -1,235 +0,0 @@
/*
* Copyright (C) 2015 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.app.Fragment;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.net.wifi.WifiConfiguration;
import android.os.Looper;
import android.os.UserHandle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settingslib.wifi.AccessPoint;
public class AccessPointPreference extends Preference {
private static final int[] STATE_SECURED = {
R.attr.state_encrypted
};
private static final int[] STATE_NONE = {};
private static int[] wifi_signal_attributes = { R.attr.wifi_signal };
private final StateListDrawable mWifiSld;
private final int mBadgePadding;
private final UserBadgeCache mBadgeCache;
private final Fragment mFragment;
private TextView mTitleView;
private boolean mForSavedNetworks = false;
private AccessPoint mAccessPoint;
private Drawable mBadge;
private int mLevel;
private CharSequence mContentDescription;
static final int[] WIFI_CONNECTION_STRENGTH = {
R.string.accessibility_wifi_one_bar,
R.string.accessibility_wifi_two_bars,
R.string.accessibility_wifi_three_bars,
R.string.accessibility_wifi_signal_full
};
// Used for dummy pref.
public AccessPointPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mWifiSld = null;
mBadgePadding = 0;
mBadgeCache = null;
mFragment = null;
}
public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
boolean forSavedNetworks, Fragment fragment) {
super(context);
mFragment = fragment;
mBadgeCache = cache;
mAccessPoint = accessPoint;
mForSavedNetworks = forSavedNetworks;
mAccessPoint.setTag(this);
mLevel = -1;
mWifiSld = (StateListDrawable) context.getTheme()
.obtainStyledAttributes(wifi_signal_attributes).getDrawable(0);
// 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);
refresh();
}
public AccessPoint getAccessPoint() {
return mAccessPoint;
}
@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);
}
if (mAccessPoint == null) {
// Used for dummy pref.
return;
}
Drawable drawable = getIcon();
if (drawable != null) {
drawable.setLevel(mLevel);
}
mTitleView = (TextView) view.findViewById(com.android.internal.R.id.title);
if (mTitleView != null) {
// Attach to the end of the title view
mTitleView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, mBadge, null);
mTitleView.setCompoundDrawablePadding(mBadgePadding);
}
view.itemView.setContentDescription(mContentDescription);
}
protected void updateIcon(int level, Context context) {
if (level == -1) {
setIcon(null);
} else {
if (getIcon() == null) {
// To avoid a drawing race condition, we first set the state (SECURE/NONE) and then
// set the icon (drawable) to that state's drawable.
// If sld is null then we are indexing and therefore do not have access to
// (nor need to display) the drawable.
if (mWifiSld != null) {
mWifiSld.setState((mAccessPoint.getSecurity() != AccessPoint.SECURITY_NONE)
? STATE_SECURED
: STATE_NONE);
Drawable drawable = mWifiSld.getCurrent();
if (!mForSavedNetworks) {
setIcon(drawable);
} 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.
mBadge = mBadgeCache.getUserBadge(config.creatorUid);
}
}
/**
* Updates the title and summary; may indirectly call notifyChanged().
*/
public void refresh() {
if (mForSavedNetworks) {
setTitle(mAccessPoint.getConfigName());
} else {
setTitle(mAccessPoint.getSsid());
}
final Context context = getContext();
int level = mAccessPoint.getLevel();
if (level != mLevel) {
mLevel = level;
updateIcon(mLevel, context);
notifyChanged();
}
updateBadge(context);
setSummary(mForSavedNetworks ? mAccessPoint.getSavedNetworkSummary()
: mAccessPoint.getSettingsSummary());
mContentDescription = getTitle();
if (getSummary() != null) {
mContentDescription = TextUtils.concat(mContentDescription, ",", getSummary());
}
if (level >= 0 && level < WIFI_CONNECTION_STRENGTH.length) {
mContentDescription = TextUtils.concat(mContentDescription, ",",
getContext().getString(WIFI_CONNECTION_STRENGTH[level]));
}
}
@Override
protected void notifyChanged() {
if (Looper.getMainLooper() != Looper.myLooper()) {
// Let our BG thread callbacks call setTitle/setSummary.
postNotifyChanged();
} else {
super.notifyChanged();
}
}
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;
UserBadgeCache(PackageManager pm) {
mPm = pm;
}
private Drawable getUserBadge(int userId) {
int index = mBadges.indexOfKey(userId);
if (index < 0) {
Drawable badge = mPm.getUserBadgeForDensity(new UserHandle(userId), 0 /* dpi */);
mBadges.put(userId, badge);
return badge;
}
return mBadges.valueAt(index);
}
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (C) 2015 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.app.Fragment;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.net.wifi.WifiConfiguration;
import android.os.Looper;
import android.os.UserHandle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settingslib.wifi.AccessPoint;
import com.android.settingslib.wifi.AccessPointPreference;
public class LongPressAccessPointPreference extends AccessPointPreference {
private final Fragment mFragment;
// Used for dummy pref.
public LongPressAccessPointPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mFragment = null;
}
public LongPressAccessPointPreference(AccessPoint accessPoint, Context context,
UserBadgeCache cache, boolean forSavedNetworks, Fragment fragment) {
super(accessPoint, context, cache, forSavedNetworks);
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);
}
}
}

View File

@@ -31,8 +31,8 @@ import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable; import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw; import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.wifi.AccessPointPreference.UserBadgeCache;
import com.android.settingslib.wifi.AccessPoint; import com.android.settingslib.wifi.AccessPoint;
import com.android.settingslib.wifi.AccessPointPreference;
import com.android.settingslib.wifi.WifiTracker; import com.android.settingslib.wifi.WifiTracker;
import java.util.ArrayList; import java.util.ArrayList;
@@ -53,7 +53,7 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
private Bundle mAccessPointSavedState; private Bundle mAccessPointSavedState;
private AccessPoint mSelectedAccessPoint; private AccessPoint mSelectedAccessPoint;
private UserBadgeCache mUserBadgeCache; private AccessPointPreference.UserBadgeCache mUserBadgeCache;
// Instance state key // Instance state key
private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state"; private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state";
@@ -67,7 +67,7 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.wifi_display_saved_access_points); addPreferencesFromResource(R.xml.wifi_display_saved_access_points);
mUserBadgeCache = new UserBadgeCache(getPackageManager()); mUserBadgeCache = new AccessPointPreference.UserBadgeCache(getPackageManager());
} }
@Override @Override
@@ -108,8 +108,9 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
final int accessPointsSize = accessPoints.size(); final int accessPointsSize = accessPoints.size();
for (int i = 0; i < accessPointsSize; ++i){ for (int i = 0; i < accessPointsSize; ++i){
AccessPointPreference preference = new AccessPointPreference(accessPoints.get(i), LongPressAccessPointPreference preference =
context, mUserBadgeCache, true, this); new LongPressAccessPointPreference(accessPoints.get(i), context,
mUserBadgeCache, true, this);
preference.setIcon(null); preference.setIcon(null);
preferenceScreen.addPreference(preference); preferenceScreen.addPreference(preference);
} }
@@ -119,7 +120,7 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
} }
} }
private void showDialog(AccessPointPreference accessPoint, boolean edit) { private void showDialog(LongPressAccessPointPreference accessPoint, boolean edit) {
if (mDialog != null) { if (mDialog != null) {
removeDialog(WifiSettings.WIFI_DIALOG_ID); removeDialog(WifiSettings.WIFI_DIALOG_ID);
mDialog = null; mDialog = null;
@@ -184,8 +185,8 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
@Override @Override
public boolean onPreferenceTreeClick(Preference preference) { public boolean onPreferenceTreeClick(Preference preference) {
if (preference instanceof AccessPointPreference) { if (preference instanceof LongPressAccessPointPreference) {
showDialog((AccessPointPreference) preference, false); showDialog((LongPressAccessPointPreference) preference, false);
return true; return true;
} else{ } else{
return super.onPreferenceTreeClick(preference); return super.onPreferenceTreeClick(preference);

View File

@@ -63,6 +63,7 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.TextView.BufferType; import android.widget.TextView.BufferType;
import android.widget.Toast; import android.widget.Toast;
import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.MetricsLogger;
import com.android.settings.LinkifyUtils; import com.android.settings.LinkifyUtils;
import com.android.settings.R; import com.android.settings.R;
@@ -73,9 +74,9 @@ import com.android.settings.location.ScanningSettings;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable; import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw; import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.wifi.AccessPointPreference.UserBadgeCache;
import com.android.settingslib.wifi.AccessPoint; import com.android.settingslib.wifi.AccessPoint;
import com.android.settingslib.wifi.AccessPoint.AccessPointListener; import com.android.settingslib.wifi.AccessPoint.AccessPointListener;
import com.android.settingslib.wifi.AccessPointPreference;
import com.android.settingslib.wifi.WifiStatusTracker; import com.android.settingslib.wifi.WifiStatusTracker;
import com.android.settingslib.wifi.WifiTracker; import com.android.settingslib.wifi.WifiTracker;
@@ -155,7 +156,7 @@ public class WifiSettings extends RestrictedSettingsFragment
private HandlerThread mBgThread; private HandlerThread mBgThread;
private UserBadgeCache mUserBadgeCache; private AccessPointPreference.UserBadgeCache mUserBadgeCache;
private Preference mAddPreference; private Preference mAddPreference;
/* End of "used in Wifi Setup context" */ /* End of "used in Wifi Setup context" */
@@ -185,7 +186,7 @@ public class WifiSettings extends RestrictedSettingsFragment
mAddPreference.setIcon(ic_add); mAddPreference.setIcon(ic_add);
mAddPreference.setTitle(R.string.wifi_add_network); mAddPreference.setTitle(R.string.wifi_add_network);
mUserBadgeCache = new UserBadgeCache(getPackageManager()); mUserBadgeCache = new AccessPointPreference.UserBadgeCache(getPackageManager());
mBgThread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND); mBgThread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
mBgThread.start(); mBgThread.start();
@@ -448,8 +449,9 @@ public class WifiSettings extends RestrictedSettingsFragment
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) { public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) {
Preference preference = (Preference) view.getTag(); Preference preference = (Preference) view.getTag();
if (preference instanceof AccessPointPreference) { if (preference instanceof LongPressAccessPointPreference) {
mSelectedAccessPoint = ((AccessPointPreference) preference).getAccessPoint(); mSelectedAccessPoint =
((LongPressAccessPointPreference) preference).getAccessPoint();
menu.setHeaderTitle(mSelectedAccessPoint.getSsid()); menu.setHeaderTitle(mSelectedAccessPoint.getSsid());
if (mSelectedAccessPoint.isConnectable()) { if (mSelectedAccessPoint.isConnectable()) {
menu.add(Menu.NONE, MENU_ID_CONNECT, 0, R.string.wifi_menu_connect); menu.add(Menu.NONE, MENU_ID_CONNECT, 0, R.string.wifi_menu_connect);
@@ -515,8 +517,8 @@ public class WifiSettings extends RestrictedSettingsFragment
@Override @Override
public boolean onPreferenceTreeClick(Preference preference) { public boolean onPreferenceTreeClick(Preference preference) {
if (preference instanceof AccessPointPreference) { if (preference instanceof LongPressAccessPointPreference) {
mSelectedAccessPoint = ((AccessPointPreference) preference).getAccessPoint(); mSelectedAccessPoint = ((LongPressAccessPointPreference) preference).getAccessPoint();
if (mSelectedAccessPoint == null) { if (mSelectedAccessPoint == null) {
return false; return false;
} }
@@ -654,7 +656,8 @@ public class WifiSettings extends RestrictedSettingsFragment
getPreferenceScreen().addPreference(pref); getPreferenceScreen().addPreference(pref);
continue; continue;
} }
AccessPointPreference preference = new AccessPointPreference(accessPoint, LongPressAccessPointPreference
preference = new LongPressAccessPointPreference(accessPoint,
getPrefContext(), mUserBadgeCache, false, this); getPrefContext(), mUserBadgeCache, false, this);
preference.setOrder(index++); preference.setOrder(index++);
@@ -866,15 +869,6 @@ public class WifiSettings extends RestrictedSettingsFragment
mWifiManager.connect(networkId, mConnectListener); mWifiManager.connect(networkId, mConnectListener);
} }
/**
* Refreshes acccess points and ask Wifi module to scan networks again.
*/
/* package */ void refreshAccessPoints() {
mWifiTracker.resumeScanning();
getPreferenceScreen().removeAll();
}
/** /**
* Called when "add network" button is pressed. * Called when "add network" button is pressed.
*/ */
@@ -885,29 +879,6 @@ public class WifiSettings extends RestrictedSettingsFragment
showDialog(null, WifiConfigUiBase.MODE_CONNECT); showDialog(null, WifiConfigUiBase.MODE_CONNECT);
} }
/* package */ int getAccessPointsCount() {
final boolean wifiIsEnabled = mWifiTracker.isWifiEnabled();
if (wifiIsEnabled) {
return getPreferenceScreen().getPreferenceCount();
} else {
return 0;
}
}
/**
* Requests wifi module to pause wifi scan. May be ignored when the module is disabled.
*/
/* package */ void pauseWifiScan() {
mWifiTracker.pauseScanning();
}
/**
* Requests wifi module to resume wifi scan. May be ignored when the module is disabled.
*/
/* package */ void resumeWifiScan() {
mWifiTracker.resumeScanning();
}
@Override @Override
protected int getHelpResource() { protected int getHelpResource() {
return R.string.help_url_wifi; return R.string.help_url_wifi;
@@ -915,19 +886,19 @@ public class WifiSettings extends RestrictedSettingsFragment
@Override @Override
public void onAccessPointChanged(AccessPoint accessPoint) { public void onAccessPointChanged(AccessPoint accessPoint) {
((AccessPointPreference) accessPoint.getTag()).refresh(); ((LongPressAccessPointPreference) accessPoint.getTag()).refresh();
} }
@Override @Override
public void onLevelChanged(AccessPoint accessPoint) { public void onLevelChanged(AccessPoint accessPoint) {
((AccessPointPreference) accessPoint.getTag()).onLevelChanged(); ((LongPressAccessPointPreference) accessPoint.getTag()).onLevelChanged();
} }
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() { new BaseSearchIndexProvider() {
@Override @Override
public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); final List<SearchIndexableRaw> result = new ArrayList<>();
final Resources res = context.getResources(); final Resources res = context.getResources();
// Add fragment title // Add fragment title