Home | History | Annotate | Download | only in util
      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.networkrecommendation.util;
     17 
     18 import android.content.res.Resources.Theme;
     19 import android.graphics.drawable.Drawable;
     20 import android.net.NetworkBadging;
     21 import android.net.RssiCurve;
     22 import android.net.ScoredNetwork;
     23 import android.net.wifi.WifiConfiguration;
     24 import android.net.wifi.WifiManager;
     25 import android.os.Bundle;
     26 import android.os.UserManager;
     27 import android.support.annotation.VisibleForTesting;
     28 
     29 /**
     30  * This class provides access to @SystemApi methods that were added in Android O and not yet
     31  * available in using the --config robo_experimental configuration when testing.
     32  */
     33 public class RoboCompatUtil {
     34 
     35     /**
     36      * {@link UserManager#ACTION_USER_RESTRICTIONS_CHANGED}. TODO: remove when string is available
     37      * in experimental.
     38      */
     39     public static final String ACTION_USER_RESTRICTIONS_CHANGED =
     40             "android.os.action.USER_RESTRICTIONS_CHANGED";
     41 
     42     private static RoboCompatUtil mRoboCompatUtil;
     43 
     44     private RoboCompatUtil() {}
     45 
     46     /** Get a shared instance of this utility. */
     47     public static synchronized RoboCompatUtil getInstance() {
     48         if (mRoboCompatUtil == null) {
     49             mRoboCompatUtil = new RoboCompatUtil();
     50         }
     51         return mRoboCompatUtil;
     52     }
     53 
     54     @VisibleForTesting
     55     public static void setInstanceForTesting(RoboCompatUtil roboCompatUtil) {
     56         mRoboCompatUtil = roboCompatUtil;
     57     }
     58 
     59     /** Wraps WifiManager.connect. */
     60     public void connectToWifi(WifiManager wifiManager, WifiConfiguration wifiConfiguration) {
     61         wifiManager.connect(wifiConfiguration, null /* actionListener */);
     62     }
     63 
     64     /** Wraps WifiConfiguration.hasNoInternetAccess. */
     65     @SuppressWarnings("unchecked")
     66     public boolean hasNoInternetAccess(WifiConfiguration wifiConfiguration) {
     67         return wifiConfiguration.hasNoInternetAccess();
     68     }
     69 
     70     /** Wraps WifiConfiguration.isNoInternetAccessExpected. */
     71     public boolean isNoInternetAccessExpected(WifiConfiguration wifiConfiguration) {
     72         return wifiConfiguration.isNoInternetAccessExpected();
     73     }
     74 
     75     /** Wraps WifiConfiguration.useExternalScores. */
     76     public boolean useExternalScores(WifiConfiguration wifiConfiguration) {
     77         return wifiConfiguration.useExternalScores;
     78     }
     79 
     80     /** Wraps WifiConfiguration.isPasspoint. */
     81     public boolean isPasspoint(WifiConfiguration wifiConfiguration) {
     82         return wifiConfiguration.isPasspoint();
     83     }
     84 
     85     /** Wraps NetworkBadging.getWifiIcon. */
     86     public Drawable getWifiIcon(int signalLevel, int badging, Theme theme) {
     87         return NetworkBadging.getWifiIcon(signalLevel, badging, theme);
     88     }
     89 
     90     /** Wraps RssiCurve.activeNetworkRssiBoost. */
     91     public int activeNetworkRssiBoost(RssiCurve curve) {
     92         return curve.activeNetworkRssiBoost;
     93     }
     94 
     95     /** Wraps ScoredNetwork.attributes. */
     96     public Bundle attributes(ScoredNetwork scoredNetwork) {
     97         return scoredNetwork.attributes;
     98     }
     99 }
    100