1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.car.developeroptions.network; 17 18 import android.content.Context; 19 import android.net.NetworkScoreManager; 20 import android.net.NetworkScorerAppData; 21 22 import androidx.preference.Preference; 23 24 import com.android.car.developeroptions.R; 25 import com.android.car.developeroptions.core.BasePreferenceController; 26 import com.android.settingslib.core.AbstractPreferenceController; 27 28 import java.util.List; 29 30 /** 31 * {@link AbstractPreferenceController} that shows the active network scorer and toggles the 32 * preference based on whether or not there are valid scorers installed. 33 */ 34 public class NetworkScorerPickerPreferenceController extends BasePreferenceController { 35 36 private final NetworkScoreManager mNetworkScoreManager; 37 NetworkScorerPickerPreferenceController(Context context, String key)38 public NetworkScorerPickerPreferenceController(Context context, String key) { 39 super(context, key); 40 mNetworkScoreManager = 41 (NetworkScoreManager) mContext.getSystemService(Context.NETWORK_SCORE_SERVICE); 42 } 43 44 @Override getAvailabilityStatus()45 public int getAvailabilityStatus() { 46 return AVAILABLE; 47 } 48 49 @Override updateState(Preference preference)50 public void updateState(Preference preference) { 51 final List<NetworkScorerAppData> allValidScorers = 52 mNetworkScoreManager.getAllValidScorers(); 53 boolean enabled = !allValidScorers.isEmpty(); 54 preference.setEnabled(enabled); 55 if (!enabled) { 56 preference.setSummary(null); 57 return; 58 } 59 60 NetworkScorerAppData scorer = mNetworkScoreManager.getActiveScorer(); 61 if (scorer == null) { 62 preference.setSummary(mContext.getString( 63 R.string.network_scorer_picker_none_preference)); 64 } else { 65 preference.setSummary(scorer.getRecommendationServiceLabel()); 66 } 67 } 68 } 69