1 /* 2 * Copyright (C) 2021 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 17 package com.android.wifitrackerlib; 18 19 import android.app.admin.DevicePolicyManager; 20 import android.content.Context; 21 import android.net.ConnectivityManager; 22 import android.net.wifi.WifiManager; 23 import android.os.Build; 24 import android.os.UserManager; 25 import android.provider.DeviceConfig; 26 import android.util.ArraySet; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 31 import java.util.Set; 32 33 /** 34 * Wrapper class for commonly referenced objects and static data. 35 */ 36 public class WifiTrackerInjector { 37 private static final String DEVICE_CONFIG_NAMESPACE = "wifi"; 38 39 @NonNull private final Context mContext; 40 private final boolean mIsDemoMode; 41 private final WifiManager mWifiManager; 42 @Nullable 43 private final ConnectivityManager mConnectivityManager; 44 private final UserManager mUserManager; 45 private final DevicePolicyManager mDevicePolicyManager; 46 @NonNull private final Set<String> mNoAttributionAnnotationPackages; 47 private boolean mIsUserDebugVerboseLoggingEnabled; 48 private boolean mVerboseLoggingDisabledOverride = false; 49 50 // TODO(b/201571677): Migrate the rest of the common objects to WifiTrackerInjector. WifiTrackerInjector(@onNull Context context)51 WifiTrackerInjector(@NonNull Context context) { 52 mContext = context; 53 mWifiManager = context.getSystemService(WifiManager.class); 54 mConnectivityManager = context.getSystemService(ConnectivityManager.class); 55 mIsDemoMode = NonSdkApiWrapper.isDemoMode(context); 56 mUserManager = context.getSystemService(UserManager.class); 57 mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class); 58 mNoAttributionAnnotationPackages = new ArraySet<>(); 59 String[] noAttributionAnnotationPackages = context.getString( 60 R.string.wifitrackerlib_no_attribution_annotation_packages).split(","); 61 for (int i = 0; i < noAttributionAnnotationPackages.length; i++) { 62 mNoAttributionAnnotationPackages.add(noAttributionAnnotationPackages[i]); 63 } 64 mIsUserDebugVerboseLoggingEnabled = context.getResources().getBoolean( 65 R.bool.wifitrackerlib_enable_verbose_logging_for_userdebug) 66 && Build.TYPE.equals("userdebug"); 67 } 68 getContext()69 @NonNull Context getContext() { 70 return mContext; 71 } 72 isDemoMode()73 boolean isDemoMode() { 74 return mIsDemoMode; 75 } 76 getUserManager()77 public UserManager getUserManager() { 78 return mUserManager; 79 } 80 getDevicePolicyManager()81 public DevicePolicyManager getDevicePolicyManager() { 82 return mDevicePolicyManager; 83 } 84 85 /** 86 * Returns the set of package names which we should not show attribution annotations for. 87 */ getNoAttributionAnnotationPackages()88 @NonNull Set<String> getNoAttributionAnnotationPackages() { 89 return mNoAttributionAnnotationPackages; 90 } 91 isSharedConnectivityFeatureEnabled()92 public boolean isSharedConnectivityFeatureEnabled() { 93 return DeviceConfig.getBoolean(DEVICE_CONFIG_NAMESPACE, 94 "shared_connectivity_enabled", false); 95 } 96 97 /** 98 * Whether verbose logging is enabled. 99 */ isVerboseLoggingEnabled()100 public boolean isVerboseLoggingEnabled() { 101 return !mVerboseLoggingDisabledOverride 102 && (mWifiManager.isVerboseLoggingEnabled() || mIsUserDebugVerboseLoggingEnabled); 103 } 104 105 /** 106 * Whether verbose summaries should be shown in WifiEntry. 107 */ isVerboseSummaryEnabled()108 public boolean isVerboseSummaryEnabled() { 109 return !mVerboseLoggingDisabledOverride && mWifiManager.isVerboseLoggingEnabled(); 110 } 111 112 /** 113 * Permanently disables verbose logging. 114 */ disableVerboseLogging()115 public void disableVerboseLogging() { 116 mVerboseLoggingDisabledOverride = true; 117 } 118 119 @Nullable getConnectivityManager()120 public ConnectivityManager getConnectivityManager() { 121 return mConnectivityManager; 122 } 123 124 /** 125 * Injection wrapper for NonSdkApiWrapper.isWifiStateChangedListenerEnabled(). 126 */ isWifiStateChangedListenerEnabled()127 public boolean isWifiStateChangedListenerEnabled() { 128 return NonSdkApiWrapper.isWifiStateChangedListenerEnabled(); 129 } 130 131 /** 132 * Injection wrapper for checking the SDK level is at least Baklava. 133 */ isAtLeastB()134 public boolean isAtLeastB() { 135 return Build.VERSION.SDK_INT > Build.VERSION_CODES.VANILLA_ICE_CREAM; 136 } 137 } 138