• 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.annotation.IntDef;
20 import android.util.SparseArray;
21 
22 import java.io.PrintWriter;
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 
26 /**
27  * Manage multiple last caller info
28  */
29 public class LastCallerInfoManager {
30     /** @hide */
31     @Retention(RetentionPolicy.SOURCE)
32     @IntDef(value = {
33             SCANNING_ENABLED,
34             WIFI_ENABLED,
35             SOFT_AP,
36             TETHERED_HOTSPOT,
37             AUTOJOIN_GLOBAL})
38     public @interface ApiType {}
39     public static final int SCANNING_ENABLED = 1;
40     public static final int WIFI_ENABLED = 2;
41     public static final int SOFT_AP = 3;
42     public static final int TETHERED_HOTSPOT = 4;
43     public static final int AUTOJOIN_GLOBAL = 5;
44 
45     private final SparseArray<LastCallerInfo> mLastCallerInfoMap = new SparseArray<>();
46 
47     /**
48      * Store the last caller information for the API
49      */
put(@piType int apiName, int tid, int uid, int pid, String packageName, boolean toggleState)50     public void put(@ApiType int apiName, int tid, int uid, int pid, String packageName,
51             boolean toggleState) {
52         synchronized (mLastCallerInfoMap) {
53             LastCallerInfo callerInfo = new LastCallerInfo(tid, uid, pid, packageName, toggleState);
54             mLastCallerInfoMap.put(apiName, callerInfo);
55         }
56     }
57 
58     /**
59      * Convert int constant into API String name
60      */
convertApiName(@piType int key)61     private String convertApiName(@ApiType int key) {
62         switch (key) {
63             case SCANNING_ENABLED:
64                 return "ScanningEnabled";
65             case WIFI_ENABLED:
66                 return "WifiEnabled";
67             case SOFT_AP:
68                 return "SoftAp";
69             case TETHERED_HOTSPOT:
70                 return "TetheredHotspot";
71             case AUTOJOIN_GLOBAL:
72                 return "AutojoinGlobal";
73             default:
74                 return "Unknown";
75         }
76     }
77 
78     /**
79      * Print the last caller info for the APIs tracked
80      */
dump(PrintWriter pw)81     public void dump(PrintWriter pw) {
82         pw.println("Dump of LastCallerInfoManager");
83         for (int i = 0; i < mLastCallerInfoMap.size(); i++) {
84             String apiName = convertApiName(mLastCallerInfoMap.keyAt(i));
85             String callerInfo = mLastCallerInfoMap.valueAt(i).toString();
86             pw.println(apiName + ": " + callerInfo);
87         }
88     }
89 
90     /**
91      * Last caller info
92      */
93     public static class LastCallerInfo {
94         private int mTid;
95         private int mUid;
96         private int mPid;
97         private String mPackageName;
98         private boolean mToggleState;
99 
LastCallerInfo(int tid, int uid, int pid, String packageName, boolean toggleState)100         public LastCallerInfo(int tid, int uid, int pid, String packageName, boolean toggleState) {
101             mTid = tid;
102             mUid = uid;
103             mPid = pid;
104             mPackageName = packageName;
105             mToggleState = toggleState;
106         }
107 
108         /**
109          * Convert the last caller info into String format
110          */
toString()111         public String toString() {
112             StringBuilder sb = new StringBuilder();
113             sb.append("tid=").append(mTid).append(" uid=").append(mUid)
114                     .append(" pid=").append(mPid).append(" packageName=").append(mPackageName)
115                     .append(" toggleState=").append(mToggleState);
116             return sb.toString();
117         }
118     }
119 }
120