Home | History | Annotate | Download | only in network
      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.annotation.Nullable;
     19 import android.net.NetworkScoreManager;
     20 import android.net.NetworkScorerAppData;
     21 
     22 import java.util.List;
     23 
     24 /**
     25  * Wrapper around {@link NetworkScoreManager} to facilitate unit testing.
     26  *
     27  * TODO: delete this class once robolectric supports Android O
     28  */
     29 public class NetworkScoreManagerWrapper {
     30     private final NetworkScoreManager mNetworkScoreManager;
     31 
     32     public NetworkScoreManagerWrapper(NetworkScoreManager networkScoreManager) {
     33         mNetworkScoreManager = networkScoreManager;
     34     }
     35 
     36     /**
     37      * Returns the list of available scorer apps. The list will be empty if there are
     38      * no valid scorers.
     39      */
     40     public List<NetworkScorerAppData> getAllValidScorers() {
     41         return mNetworkScoreManager.getAllValidScorers();
     42     }
     43 
     44     /**
     45      * Obtain the package name of the current active network scorer.
     46      *
     47      * <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
     48      * broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to
     49      * determine the current scorer and offer the user the ability to select a different scorer via
     50      * the {@link #ACTION_CHANGE_ACTIVE} intent.
     51      * @return the full package name of the current active scorer, or null if there is no active
     52      *         scorer.
     53      */
     54     @Nullable
     55     public String getActiveScorerPackage() {
     56         return mNetworkScoreManager.getActiveScorerPackage();
     57     }
     58 
     59     /**
     60      * Returns metadata about the active scorer or <code>null</code> if there is no active scorer.
     61      */
     62     @Nullable
     63     public NetworkScorerAppData getActiveScorer() {
     64         return mNetworkScoreManager.getActiveScorer();
     65     }
     66 
     67     /**
     68      * Set the active scorer to a new package and clear existing scores.
     69      *
     70      * <p>Should never be called directly without obtaining user consent. This can be done by using
     71      * the {@link #ACTION_CHANGE_ACTIVE} broadcast, or using a custom configuration activity.
     72      *
     73      * @return true if the operation succeeded, or false if the new package is not a valid scorer.
     74      * @throws SecurityException if the caller is not a system process or does not hold the
     75      *         {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission
     76      */
     77     public boolean setActiveScorer(String packageName) throws SecurityException {
     78         return mNetworkScoreManager.setActiveScorer(packageName);
     79     }
     80 }