Make use of WifiTracker's WorkerThread support

Let the AccessPoint callbacks come in on a BG thread and do more
of the handling there.

Bug: 21486080
Change-Id: I529152d9f5c429b790a669b75b6d955ce4336caf
This commit is contained in:
Jason Monk
2015-05-28 11:42:28 -04:00
parent ecf4339bc7
commit 7a0b412ed2
2 changed files with 78 additions and 44 deletions

View File

@@ -19,6 +19,7 @@ import android.content.Context;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable; import android.graphics.drawable.StateListDrawable;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.os.Looper;
import android.os.UserHandle; import android.os.UserHandle;
import android.preference.Preference; import android.preference.Preference;
import android.view.View; import android.view.View;
@@ -38,9 +39,12 @@ public class AccessPointPreference extends Preference {
private TextView mTitleView; private TextView mTitleView;
private TextView mSummaryView; private TextView mSummaryView;
private boolean showSummary = true; private boolean mShowSummary = true;
private boolean mForSavedNetworks = false; private boolean mForSavedNetworks = false;
private AccessPoint mAccessPoint; private AccessPoint mAccessPoint;
private Drawable mBadge;
private int mBadgePadding;
private int mLevel;
public AccessPointPreference(AccessPoint accessPoint, Context context, public AccessPointPreference(AccessPoint accessPoint, Context context,
boolean forSavedNetworks) { boolean forSavedNetworks) {
@@ -48,6 +52,7 @@ public class AccessPointPreference extends Preference {
mAccessPoint = accessPoint; mAccessPoint = accessPoint;
mForSavedNetworks = forSavedNetworks; mForSavedNetworks = forSavedNetworks;
mAccessPoint.setTag(this); mAccessPoint.setTag(this);
refresh(); refresh();
} }
@@ -58,24 +63,29 @@ public class AccessPointPreference extends Preference {
@Override @Override
protected void onBindView(View view) { protected void onBindView(View view) {
super.onBindView(view); super.onBindView(view);
updateIcon(mAccessPoint.getLevel(), getContext()); Drawable drawable = getIcon();
if (drawable != null) {
drawable.setLevel(mLevel);
}
mTitleView = (TextView) view.findViewById(com.android.internal.R.id.title); 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);
}
mSummaryView = (TextView) view.findViewById(com.android.internal.R.id.summary); mSummaryView = (TextView) view.findViewById(com.android.internal.R.id.summary);
mSummaryView.setVisibility(showSummary ? View.VISIBLE : View.GONE); mSummaryView.setVisibility(mShowSummary ? View.VISIBLE : View.GONE);
updateBadge(getContext()); updateBadge(getContext());
notifyChanged();
} }
protected void updateIcon(int level, Context context) { protected void updateIcon(int level, Context context) {
if (level == -1) { if (level == -1) {
setIcon(null); setIcon(null);
} else { } else {
Drawable drawable = getIcon(); if (getIcon() == null) {
if (drawable == null) {
// To avoid a drawing race condition, we first set the state (SECURE/NONE) and then // To avoid a drawing race condition, we first set the state (SECURE/NONE) and then
// set the icon (drawable) to that state's drawable. // set the icon (drawable) to that state's drawable.
StateListDrawable sld = (StateListDrawable) context.getTheme() StateListDrawable sld = (StateListDrawable) context.getTheme()
@@ -86,7 +96,7 @@ public class AccessPointPreference extends Preference {
sld.setState((mAccessPoint.getSecurity() != AccessPoint.SECURITY_NONE) sld.setState((mAccessPoint.getSecurity() != AccessPoint.SECURITY_NONE)
? STATE_SECURED ? STATE_SECURED
: STATE_NONE); : STATE_NONE);
drawable = sld.getCurrent(); Drawable drawable = sld.getCurrent();
if (!mForSavedNetworks) { if (!mForSavedNetworks) {
setIcon(drawable); setIcon(drawable);
} else { } else {
@@ -94,57 +104,39 @@ public class AccessPointPreference extends Preference {
} }
} }
} }
if (drawable != null) {
drawable.setLevel(level);
}
} }
} }
protected void updateBadge(Context context) { protected void updateBadge(Context context) {
if (mTitleView != null) {
WifiConfiguration config = mAccessPoint.getConfig(); WifiConfiguration config = mAccessPoint.getConfig();
if (config == null) { if (config != null) {
return;
}
// Fetch badge (may be null) // Fetch badge (may be null)
UserHandle creatorUser = new UserHandle(UserHandle.getUserId(config.creatorUid)); UserHandle creatorUser = new UserHandle(UserHandle.getUserId(config.creatorUid));
Drawable badge = mBadge = context.getPackageManager().getUserBadgeForDensity(creatorUser, 0 /* dpi */);
context.getPackageManager().getUserBadgeForDensity(creatorUser, 0 /* dpi */);
// Distance from the end of the title at which this AP's user badge should sit. // Distance from the end of the title at which this AP's user badge should sit.
final int badgePadding = context.getResources() mBadgePadding = context.getResources()
.getDimensionPixelSize(R.dimen.wifi_preference_badge_padding); .getDimensionPixelSize(R.dimen.wifi_preference_badge_padding);
// Attach to the end of the title view
mTitleView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, badge, null);
mTitleView.setCompoundDrawablePadding(badgePadding);
} }
} }
/**
* Shows or Hides the Summary of an AccessPoint.
*
* @param showSummary true will show the summary, false will hide the summary
*/
public void setShowSummary(boolean showSummary) {
this.showSummary = showSummary;
if (mSummaryView != null) {
mSummaryView.setVisibility(showSummary ? View.VISIBLE : View.GONE);
} // otherwise, will be handled in onBindView.
}
/** /**
* Updates the title and summary; may indirectly call notifyChanged(). * Updates the title and summary; may indirectly call notifyChanged().
*/ */
public void refresh() { public void refresh() {
if (mForSavedNetworks) if (mForSavedNetworks) {
setTitle(mAccessPoint.getConfigName()); setTitle(mAccessPoint.getConfigName());
else } else {
setTitle(mAccessPoint.getSsid()); setTitle(mAccessPoint.getSsid());
}
final Context context = getContext(); final Context context = getContext();
updateIcon(mAccessPoint.getLevel(), context); int level = mAccessPoint.getLevel();
if (level != mLevel) {
updateIcon(mLevel, context);
mLevel = level;
notifyChanged();
}
updateBadge(context); updateBadge(context);
// Force new summary // Force new summary
@@ -153,16 +145,40 @@ public class AccessPointPreference extends Preference {
String summary = mForSavedNetworks ? mAccessPoint.getSavedNetworkSummary() String summary = mForSavedNetworks ? mAccessPoint.getSavedNetworkSummary()
: mAccessPoint.getSettingsSummary(); : mAccessPoint.getSettingsSummary();
if (summary.length() > 0) { boolean showSummary = summary.length() > 0;
if (showSummary) {
setSummary(summary); setSummary(summary);
setShowSummary(true); }
if (showSummary != mShowSummary) {
mShowSummary = showSummary;
notifyChanged();
}
}
@Override
protected void notifyChanged() {
if (Looper.getMainLooper() != Looper.myLooper()) {
// Let our BG thread callbacks call setTitle/setSummary.
postNotifyChanged();
} else { } else {
setShowSummary(false); super.notifyChanged();
} }
} }
public void onLevelChanged() { public void onLevelChanged() {
notifyChanged(); 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();
}
};
} }

View File

@@ -41,6 +41,8 @@ import android.net.wifi.WifiManager;
import android.net.wifi.WpsInfo; import android.net.wifi.WpsInfo;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
import android.os.Bundle; import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Process;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
@@ -150,6 +152,8 @@ public class WifiSettings extends RestrictedSettingsFragment
private WifiTracker mWifiTracker; private WifiTracker mWifiTracker;
private String mOpenSsid; private String mOpenSsid;
private HandlerThread mBgThread;
/* End of "used in Wifi Setup context" */ /* End of "used in Wifi Setup context" */
public WifiSettings() { public WifiSettings() {
@@ -165,11 +169,25 @@ public class WifiSettings extends RestrictedSettingsFragment
} }
} }
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mBgThread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
mBgThread.start();
}
@Override
public void onDestroy() {
mBgThread.quit();
super.onDestroy();
}
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mWifiTracker = new WifiTracker(getActivity(), this, true, true, false); mWifiTracker =
new WifiTracker(getActivity(), this, mBgThread.getLooper(), true, true, false);
mWifiManager = mWifiTracker.getManager(); mWifiManager = mWifiTracker.getManager();
mConnectListener = new WifiManager.ActionListener() { mConnectListener = new WifiManager.ActionListener() {