Final UX for Wi-Fi assistant platform settings.

When one app is available, the toggle works as before. When multiple
apps are available, we allow the user to choose the app in a dialog
before turning on the setting.

Bug: 13780935
Change-Id: I1c4391bf97a53febe580fb2b896b4850372062e7
This commit is contained in:
Jeff Davidson
2014-11-12 18:36:41 -08:00
parent 26104298eb
commit ce32b21727
6 changed files with 111 additions and 40 deletions

View File

@@ -67,6 +67,11 @@ public class AppListPreference extends ListPreference {
}
}
public AppListPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public AppListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@@ -105,6 +110,8 @@ public class AppListPreference extends ListPreference {
setEntryValues(packageNames);
if (selectedIndex != -1) {
setValueIndex(selectedIndex);
} else {
setValue(null);
}
}

View File

@@ -0,0 +1,59 @@
package com.android.settings;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Checkable;
/**
* A hybrid of AppListPreference and SwitchPreference, representing a preference which can be on or
* off but must have a selected value when turned on.
*
* It is invalid to show this preference when zero valid apps are present.
*/
public class AppListSwitchPreference extends AppListPreference {
private static final String TAG = "AppListSwitchPref";
private Checkable mSwitch;
public AppListSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs, 0, R.style.AppListSwitchPreference);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
mSwitch = (Checkable) view.findViewById(com.android.internal.R.id.switchWidget);
mSwitch.setChecked(getValue() != null);
}
@Override
protected void showDialog(Bundle state) {
if (getValue() != null) {
// Turning off the current value.
if (callChangeListener(null)) {
setValue(null);
}
} else if (getEntryValues() == null || getEntryValues().length == 0) {
Log.e(TAG, "Attempting to show dialog with zero entries: " + getKey());
} else if (getEntryValues().length == 1) {
// Suppress the dialog and just toggle the preference with the only choice.
String value = getEntryValues()[0].toString();
if (callChangeListener(value)) {
setValue(value);
}
} else {
super.showDialog(state);
}
}
@Override
public void setValue(String value) {
super.setValue(value);
if (mSwitch != null) {
mSwitch.setChecked(value != null);
}
}
}

View File

@@ -42,6 +42,7 @@ import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.android.settings.AppListSwitchPreference;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
@@ -66,6 +67,7 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
private WifiManager mWifiManager;
private NetworkScoreManager mNetworkScoreManager;
private AppListSwitchPreference mWifiAssistantPreference;
private IntentFilter mFilter;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@@ -130,18 +132,14 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
pref.setIntent(intent);
final Context context = getActivity();
SwitchPreference wifiAssistant = (SwitchPreference)findPreference(KEY_WIFI_ASSISTANT);
NetworkScorerAppData scorer = getWifiAssistantApp(context);
if (UserHandle.myUserId() == UserHandle.USER_OWNER && scorer != null) {
final boolean checked = NetworkScorerAppManager.getActiveScorer(context) != null;
wifiAssistant.setSummary(getResources().getString(
R.string.wifi_automatically_manage_summary, scorer.mScorerName));
wifiAssistant.setOnPreferenceChangeListener(this);
wifiAssistant.setChecked(checked);
} else {
if (wifiAssistant != null) {
getPreferenceScreen().removePreference(wifiAssistant);
}
mWifiAssistantPreference = (AppListSwitchPreference) findPreference(KEY_WIFI_ASSISTANT);
Collection<NetworkScorerAppData> scorers =
NetworkScorerAppManager.getAllValidScorers(context);
if (UserHandle.myUserId() == UserHandle.USER_OWNER && !scorers.isEmpty()) {
mWifiAssistantPreference.setOnPreferenceChangeListener(this);
initWifiAssistantPreference(scorers);
} else if (mWifiAssistantPreference != null) {
getPreferenceScreen().removePreference(mWifiAssistantPreference);
}
Intent wifiDirectIntent = new Intent(context,
@@ -202,6 +200,18 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
}
}
private void initWifiAssistantPreference(Collection<NetworkScorerAppData> scorers) {
int count = scorers.size();
String[] packageNames = new String[count];
int i = 0;
for (NetworkScorerAppData scorer : scorers) {
packageNames[i] = scorer.mPackageName;
i++;
}
mWifiAssistantPreference.setPackageNames(packageNames,
mNetworkScoreManager.getActiveScorerPackage());
}
private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) {
if (value != null) {
String[] values = getResources().getStringArray(R.array.wifi_sleep_policy_values);
@@ -261,12 +271,13 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
return false;
}
} else if (KEY_WIFI_ASSISTANT.equals(key)) {
if (((Boolean)newValue).booleanValue() == false) {
NetworkScorerAppData wifiAssistant =
NetworkScorerAppManager.getScorer(context, (String) newValue);
if (wifiAssistant == null) {
mNetworkScoreManager.setActiveScorer(null);
return true;
}
NetworkScorerAppData wifiAssistant = getWifiAssistantApp(context);
Intent intent = new Intent();
if (wifiAssistant.mConfigurationActivityClassName != null) {
// App has a custom configuration activity; launch that.
@@ -282,6 +293,9 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
}
startActivity(intent);
// Don't update the preference widget state until the child activity returns.
// It will be updated in onResume after the activity finishes.
return false;
}
if (KEY_SLEEP_POLICY.equals(key)) {
@@ -317,22 +331,6 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
wifiIpAddressPref.setSelectable(false);
}
/**
* Returns the Network Scorer for the Wifi Assistant App.
*/
public static NetworkScorerAppData getWifiAssistantApp(Context context) {
Collection<NetworkScorerAppData> scorers =
NetworkScorerAppManager.getAllValidScorers(context);
if (scorers.isEmpty()) {
return null;
}
// TODO: b/13780935 - Implement proper scorer selection. Rather than pick the first
// scorer on the system, we should allow the user to select one.
return scorers.iterator().next();
}
/* Wrapper class for the WPS dialog to properly handle life cycle events like rotation. */
public static class WpsFragment extends DialogFragment {
private static int mWpsSetup;