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.wifi.WifiManager;
22 import android.os.Build;
23 import android.os.UserManager;
24 import android.provider.DeviceConfig;
25 import android.util.ArraySet;
26 
27 import androidx.annotation.NonNull;
28 
29 import java.util.Set;
30 
31 /**
32  * Wrapper class for commonly referenced objects and static data.
33  */
34 public class WifiTrackerInjector {
35     private static final String DEVICE_CONFIG_NAMESPACE = "wifi";
36 
37     @NonNull private final Context mContext;
38     private final boolean mIsDemoMode;
39     private final WifiManager mWifiManager;
40     private final UserManager mUserManager;
41     private final DevicePolicyManager mDevicePolicyManager;
42     @NonNull private final Set<String> mNoAttributionAnnotationPackages;
43     private boolean mIsUserDebugVerboseLoggingEnabled;
44     private boolean mVerboseLoggingDisabledOverride = false;
45 
46     // TODO(b/201571677): Migrate the rest of the common objects to WifiTrackerInjector.
WifiTrackerInjector(@onNull Context context)47     WifiTrackerInjector(@NonNull Context context) {
48         mContext = context;
49         mWifiManager = context.getSystemService(WifiManager.class);
50         mIsDemoMode = NonSdkApiWrapper.isDemoMode(context);
51         mUserManager = context.getSystemService(UserManager.class);
52         mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
53         mNoAttributionAnnotationPackages = new ArraySet<>();
54         String[] noAttributionAnnotationPackages = context.getString(
55                 R.string.wifitrackerlib_no_attribution_annotation_packages).split(",");
56         for (int i = 0; i < noAttributionAnnotationPackages.length; i++) {
57             mNoAttributionAnnotationPackages.add(noAttributionAnnotationPackages[i]);
58         }
59         mIsUserDebugVerboseLoggingEnabled = context.getResources().getBoolean(
60                 R.bool.wifitrackerlib_enable_verbose_logging_for_userdebug)
61                 && Build.TYPE.equals("userdebug");
62     }
63 
getContext()64     @NonNull Context getContext() {
65         return mContext;
66     }
67 
isDemoMode()68     boolean isDemoMode() {
69         return mIsDemoMode;
70     }
71 
getUserManager()72     public UserManager getUserManager() {
73         return mUserManager;
74     }
75 
getDevicePolicyManager()76     public DevicePolicyManager getDevicePolicyManager() {
77         return mDevicePolicyManager;
78     }
79 
80     /**
81      * Returns the set of package names which we should not show attribution annotations for.
82      */
getNoAttributionAnnotationPackages()83     @NonNull Set<String> getNoAttributionAnnotationPackages() {
84         return mNoAttributionAnnotationPackages;
85     }
86 
isSharedConnectivityFeatureEnabled()87     public boolean isSharedConnectivityFeatureEnabled() {
88         return DeviceConfig.getBoolean(DEVICE_CONFIG_NAMESPACE,
89                 "shared_connectivity_enabled", false);
90     }
91 
92     /**
93      * Whether verbose logging is enabled.
94      */
isVerboseLoggingEnabled()95     public boolean isVerboseLoggingEnabled() {
96         return !mVerboseLoggingDisabledOverride
97                 && (mWifiManager.isVerboseLoggingEnabled() || mIsUserDebugVerboseLoggingEnabled);
98     }
99 
100     /**
101      * Whether verbose summaries should be shown in WifiEntry.
102      */
isVerboseSummaryEnabled()103     public boolean isVerboseSummaryEnabled() {
104         return !mVerboseLoggingDisabledOverride && mWifiManager.isVerboseLoggingEnabled();
105     }
106 
107     /**
108      * Permanently disables verbose logging.
109      */
disableVerboseLogging()110     public void disableVerboseLogging() {
111         mVerboseLoggingDisabledOverride = true;
112     }
113 }
114