Create settings page for choosing default Network Scorer.

Bug: 35203304
Bug: 35852687
Test: make RunSettingsRoboTests -j40
Change-Id: Iaef671e0a9e8beb560e1d2c9864f44ef46993a6b
This commit is contained in:
Stephen Chen
2017-03-01 17:10:08 -08:00
parent 8e667cf2d4
commit 9347c95c39
7 changed files with 522 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
/*
* 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.
*/
package com.android.settings.network;
import android.annotation.Nullable;
import android.net.NetworkScoreManager;
import android.net.NetworkScorerAppData;
import java.util.List;
/**
* Wrapper around {@link NetworkScoreManager} to facilitate unit testing.
*
* TODO: delete this class once robolectric supports Android O
*/
public class NetworkScoreManagerWrapper {
private final NetworkScoreManager mNetworkScoreManager;
public NetworkScoreManagerWrapper(NetworkScoreManager networkScoreManager) {
mNetworkScoreManager = networkScoreManager;
}
/**
* Returns the list of available scorer apps. The list will be empty if there are
* no valid scorers.
*/
public List<NetworkScorerAppData> getAllValidScorers() {
return mNetworkScoreManager.getAllValidScorers();
}
/**
* Obtain the package name of the current active network scorer.
*
* <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
* broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to
* determine the current scorer and offer the user the ability to select a different scorer via
* the {@link #ACTION_CHANGE_ACTIVE} intent.
* @return the full package name of the current active scorer, or null if there is no active
* scorer.
*/
@Nullable
public String getActiveScorerPackage() {
return mNetworkScoreManager.getActiveScorerPackage();
}
/**
* Set the active scorer to a new package and clear existing scores.
*
* <p>Should never be called directly without obtaining user consent. This can be done by using
* the {@link #ACTION_CHANGE_ACTIVE} broadcast, or using a custom configuration activity.
*
* @return true if the operation succeeded, or false if the new package is not a valid scorer.
* @throws SecurityException if the caller is not a system process or does not hold the
* {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission
*/
public boolean setActiveScorer(String packageName) throws SecurityException {
return mNetworkScoreManager.setActiveScorer(packageName);
}
}

View File

@@ -0,0 +1,135 @@
/*
* 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.
*/
package com.android.settings.network;
import android.content.Context;
import android.net.NetworkScoreManager;
import android.net.NetworkScorerAppData;
import android.os.Bundle;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.settings.R;
import com.android.settings.core.InstrumentedPreferenceFragment;
import com.android.settings.core.instrumentation.Instrumentable;
import com.android.settings.widget.RadioButtonPreference;
import java.util.List;
/**
* Fragment for choosing default network scorer.
*/
public class NetworkScorerPicker extends InstrumentedPreferenceFragment implements
RadioButtonPreference.OnClickListener {
private NetworkScoreManagerWrapper mNetworkScoreManager;
@Override
public int getMetricsCategory() {
//TODO(35854268): Add logging.
return Instrumentable.METRICS_CATEGORY_UNKNOWN;
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
addPreferencesFromResource(R.xml.network_scorer_picker_prefs);
updateCandidates();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mNetworkScoreManager = createNetworkScorerManagerWrapper(context);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
// this is needed so the back button goes back to previous fragment
setHasOptionsMenu(true);
return view;
}
@VisibleForTesting
public void updateCandidates() {
final PreferenceScreen screen = getPreferenceScreen();
screen.removeAll();
final List<NetworkScorerAppData> scorers = mNetworkScoreManager.getAllValidScorers();
if (scorers.isEmpty()) {
final RadioButtonPreference nonePref = new RadioButtonPreference(getPrefContext());
nonePref.setTitle(R.string.network_scorer_picker_none_preference);
nonePref.setChecked(true);
screen.addPreference(nonePref);
return;
}
final String defaultAppKey = getActiveScorerPackage();
final int numScorers = scorers.size();
for (int i = 0; i < numScorers; i++) {
final RadioButtonPreference pref = new RadioButtonPreference(getPrefContext());
final NetworkScorerAppData appData = scorers.get(i);
final String appKey = appData.getRecommendationServicePackageName();
pref.setTitle(appData.getRecommendationServiceLabel());
pref.setKey(appKey);
pref.setChecked(TextUtils.equals(defaultAppKey, appKey));
pref.setOnClickListener(this);
screen.addPreference(pref);
}
}
private String getActiveScorerPackage() {
return mNetworkScoreManager.getActiveScorerPackage();
}
private boolean setActiveScorer(String key) {
if (!TextUtils.equals(key, getActiveScorerPackage())) {
return mNetworkScoreManager.setActiveScorer(key);
}
return false;
}
@Override
public void onRadioButtonClicked(RadioButtonPreference selected) {
final String selectedKey = selected.getKey();
final boolean success = setActiveScorer(selectedKey);
if (success) {
updateCheckedState(selectedKey);
}
}
private void updateCheckedState(String selectedKey) {
final PreferenceScreen screen = getPreferenceScreen();
final int count = screen.getPreferenceCount();
for (int i = 0; i < count; i++) {
final Preference pref = screen.getPreference(i);
if (pref instanceof RadioButtonPreference) {
final RadioButtonPreference radioPref = (RadioButtonPreference) pref;
radioPref.setChecked(TextUtils.equals(pref.getKey(), selectedKey));
}
}
}
@VisibleForTesting
NetworkScoreManagerWrapper createNetworkScorerManagerWrapper(Context context) {
return new NetworkScoreManagerWrapper(context.getSystemService(NetworkScoreManager.class));
}
}