• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.server.wifi.util;
18 
19 import android.net.wifi.WifiManager;
20 import android.net.wifi.WifiManager.ApiType;
21 import android.util.SparseArray;
22 
23 import java.io.PrintWriter;
24 
25 /**
26  * Manage multiple last caller info
27  */
28 public class LastCallerInfoManager {
29     private final SparseArray<LastCallerInfo> mLastCallerInfoMap = new SparseArray<>();
30 
31     /**
32      * Store the last caller information for the API
33      */
put(@piType int apiName, int tid, int uid, int pid, String packageName, boolean toggleState)34     public void put(@ApiType int apiName, int tid, int uid, int pid, String packageName,
35             boolean toggleState) {
36         synchronized (mLastCallerInfoMap) {
37             LastCallerInfo callerInfo = new LastCallerInfo(tid, uid, pid, packageName, toggleState);
38             mLastCallerInfoMap.put(apiName, callerInfo);
39         }
40     }
41 
42     /**
43      * Get the LastCallerInfo for a particular api.
44      * @param apiName Name of the API
45      * @return The LastCallerInfo, or null if not available.
46      */
get(@piType int apiName)47     public LastCallerInfo get(@ApiType int apiName) {
48         synchronized (mLastCallerInfoMap) {
49             return mLastCallerInfoMap.get(apiName);
50         }
51     }
52 
53     /**
54      * Convert int constant into API String name
55      */
convertApiName(@piType int key)56     private String convertApiName(@ApiType int key) {
57         switch (key) {
58             case WifiManager.API_SCANNING_ENABLED:
59                 return "ScanningEnabled";
60             case WifiManager.API_WIFI_ENABLED:
61                 return "WifiEnabled";
62             case WifiManager.API_SOFT_AP:
63                 return "SoftAp";
64             case WifiManager.API_TETHERED_HOTSPOT:
65                 return "TetheredHotspot";
66             case WifiManager.API_AUTOJOIN_GLOBAL:
67                 return "AutojoinGlobal";
68             case WifiManager.API_SET_SCAN_SCHEDULE:
69                 return "SetScanScanSchedule";
70             default:
71                 return "Unknown";
72         }
73     }
74 
75     /**
76      * Print the last caller info for the APIs tracked
77      */
dump(PrintWriter pw)78     public void dump(PrintWriter pw) {
79         pw.println("Dump of LastCallerInfoManager");
80         for (int i = 0; i < mLastCallerInfoMap.size(); i++) {
81             String apiName = convertApiName(mLastCallerInfoMap.keyAt(i));
82             String callerInfo = mLastCallerInfoMap.valueAt(i).toString();
83             pw.println(apiName + ": " + callerInfo);
84         }
85     }
86 
87     /**
88      * Last caller info
89      */
90     public static class LastCallerInfo {
91         private int mTid;
92         private int mUid;
93         private int mPid;
94         private String mPackageName;
95         private boolean mToggleState;
96 
LastCallerInfo(int tid, int uid, int pid, String packageName, boolean toggleState)97         public LastCallerInfo(int tid, int uid, int pid, String packageName, boolean toggleState) {
98             mTid = tid;
99             mUid = uid;
100             mPid = pid;
101             mPackageName = packageName;
102             mToggleState = toggleState;
103         }
104 
105         /**
106          * Gets the packageName.
107          */
getPackageName()108         public String getPackageName() {
109             return mPackageName;
110         }
111 
112         /**
113          * Gets the toggleState.
114          */
getToggleState()115         public boolean getToggleState() {
116             return mToggleState;
117         }
118 
119         /**
120          * Convert the last caller info into String format
121          */
toString()122         public String toString() {
123             StringBuilder sb = new StringBuilder();
124             sb.append("tid=").append(mTid).append(" uid=").append(mUid)
125                     .append(" pid=").append(mPid).append(" packageName=").append(mPackageName)
126                     .append(" toggleState=").append(mToggleState);
127             return sb.toString();
128         }
129     }
130 }
131