• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.settings.network;
17 
18 import android.content.Context;
19 import android.net.NetworkScoreManager;
20 import android.net.NetworkScorerAppData;
21 import android.support.v7.preference.Preference;
22 
23 import com.android.settings.R;
24 import com.android.settings.core.BasePreferenceController;
25 import com.android.settingslib.core.AbstractPreferenceController;
26 
27 import java.util.List;
28 
29 /**
30  * {@link AbstractPreferenceController} that shows the active network scorer and toggles the
31  * preference based on whether or not there are valid scorers installed.
32  */
33 public class NetworkScorerPickerPreferenceController extends BasePreferenceController {
34 
35     private final NetworkScoreManager mNetworkScoreManager;
36 
NetworkScorerPickerPreferenceController(Context context, String key)37     public NetworkScorerPickerPreferenceController(Context context, String key) {
38         super(context, key);
39         mNetworkScoreManager =
40                 (NetworkScoreManager) mContext.getSystemService(Context.NETWORK_SCORE_SERVICE);
41     }
42 
43     @Override
getAvailabilityStatus()44     public int getAvailabilityStatus() {
45         return AVAILABLE;
46     }
47 
48     @Override
updateState(Preference preference)49     public void updateState(Preference preference) {
50         final List<NetworkScorerAppData> allValidScorers =
51                 mNetworkScoreManager.getAllValidScorers();
52         boolean enabled = !allValidScorers.isEmpty();
53         preference.setEnabled(enabled);
54         if (!enabled) {
55             preference.setSummary(null);
56             return;
57         }
58 
59         NetworkScorerAppData scorer = mNetworkScoreManager.getActiveScorer();
60         if (scorer == null) {
61             preference.setSummary(mContext.getString(
62                     R.string.network_scorer_picker_none_preference));
63         } else {
64             preference.setSummary(scorer.getRecommendationServiceLabel());
65         }
66     }
67 }
68