| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.app.AppOpsManager; |
| 21 |
import android.content.ComponentName; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.pm.PackageManager; |
| 24 |
import android.os.Binder; |
| 25 |
import android.os.Handler; |
| 26 |
import android.text.TextUtils; |
| 27 |
import android.util.Slog; |
| 28 |
|
| 29 |
import java.io.PrintWriter; |
| 30 |
import java.io.StringWriter; |
| 31 |
import java.util.Objects; |
| 32 |
import java.util.function.Predicate; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public final class DumpUtils { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static final ComponentName[] CRITICAL_SECTION_COMPONENTS = { |
| 47 |
new ComponentName("com.android.systemui", "com.android.systemui.SystemUIService") |
| 48 |
}; |
| 49 |
private static final String TAG = "DumpUtils"; |
| 50 |
private static final boolean DEBUG = false; |
| 51 |
|
| 52 |
private DumpUtils() { |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static void dumpAsync(Handler handler, final Dump dump, PrintWriter pw, |
| 63 |
final String prefix, long timeout) { |
| 64 |
final StringWriter sw = new StringWriter(); |
| 65 |
if (handler.runWithScissors(new Runnable() { |
| 66 |
@Override |
| 67 |
public void run() { |
| 68 |
PrintWriter lpw = new FastPrintWriter(sw); |
| 69 |
dump.dump(lpw, prefix); |
| 70 |
lpw.close(); |
| 71 |
} |
| 72 |
}, timeout)) { |
| 73 |
pw.print(sw.toString()); |
| 74 |
} else { |
| 75 |
pw.println("... timed out"); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public interface Dump { |
| 80 |
void dump(PrintWriter pw, String prefix); |
| 81 |
} |
| 82 |
|
| 83 |
private static void logMessage(PrintWriter pw, String msg) { |
| 84 |
if (DEBUG) Slog.v(TAG, msg); |
| 85 |
pw.println(msg); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static boolean checkDumpPermission(Context context, String tag, PrintWriter pw) { |
| 95 |
if (context.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 96 |
!= PackageManager.PERMISSION_GRANTED) { |
| 97 |
logMessage(pw, "Permission Denial: can't dump " + tag + " from from pid=" |
| 98 |
+ Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 99 |
+ " due to missing android.permission.DUMP permission"); |
| 100 |
return false; |
| 101 |
} else { |
| 102 |
return true; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static boolean checkUsageStatsPermission(Context context, String tag, PrintWriter pw) { |
| 115 |
|
| 116 |
final int uid = Binder.getCallingUid(); |
| 117 |
switch (uid) { |
| 118 |
case android.os.Process.ROOT_UID: |
| 119 |
case android.os.Process.SYSTEM_UID: |
| 120 |
case android.os.Process.SHELL_UID: |
| 121 |
case android.os.Process.INCIDENTD_UID: |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
if (context.checkCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) |
| 127 |
!= PackageManager.PERMISSION_GRANTED) { |
| 128 |
logMessage(pw, "Permission Denial: can't dump " + tag + " from from pid=" |
| 129 |
+ Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 130 |
+ " due to missing android.permission.PACKAGE_USAGE_STATS permission"); |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
final AppOpsManager appOps = context.getSystemService(AppOpsManager.class); |
| 138 |
final String[] pkgs = context.getPackageManager().getPackagesForUid(uid); |
| 139 |
if (pkgs != null) { |
| 140 |
for (String pkg : pkgs) { |
| 141 |
switch (appOps.noteOpNoThrow(AppOpsManager.OP_GET_USAGE_STATS, uid, pkg)) { |
| 142 |
case AppOpsManager.MODE_ALLOWED: |
| 143 |
if (DEBUG) Slog.v(TAG, "Found package " + pkg + " with " |
| 144 |
+ "android:get_usage_stats allowed"); |
| 145 |
return true; |
| 146 |
case AppOpsManager.MODE_DEFAULT: |
| 147 |
if (DEBUG) Slog.v(TAG, "Found package " + pkg + " with " |
| 148 |
+ "android:get_usage_stats default"); |
| 149 |
return true; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
logMessage(pw, "Permission Denial: can't dump " + tag + " from from pid=" |
| 155 |
+ Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 156 |
+ " due to android:get_usage_stats app-op not allowed"); |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public static boolean checkDumpAndUsageStatsPermission(Context context, String tag, |
| 169 |
PrintWriter pw) { |
| 170 |
return checkDumpPermission(context, tag, pw) && checkUsageStatsPermission(context, tag, pw); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public static boolean isPlatformPackage(@Nullable String packageName) { |
| 178 |
return (packageName != null) |
| 179 |
&& (packageName.equals("android") |
| 180 |
|| packageName.startsWith("android.") |
| 181 |
|| packageName.startsWith("com.android.")); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public static boolean isPlatformPackage(@Nullable ComponentName cname) { |
| 189 |
return (cname != null) && isPlatformPackage(cname.getPackageName()); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public static boolean isPlatformPackage(@Nullable ComponentName.WithComponentName wcn) { |
| 197 |
return (wcn != null) && isPlatformPackage(wcn.getComponentName()); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public static boolean isNonPlatformPackage(@Nullable String packageName) { |
| 205 |
return (packageName != null) && !isPlatformPackage(packageName); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public static boolean isNonPlatformPackage(@Nullable ComponentName cname) { |
| 213 |
return (cname != null) && isNonPlatformPackage(cname.getPackageName()); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public static boolean isNonPlatformPackage(@Nullable ComponentName.WithComponentName wcn) { |
| 221 |
return (wcn != null) && !isPlatformPackage(wcn.getComponentName()); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
private static boolean isCriticalPackage(@Nullable ComponentName cname) { |
| 228 |
if (cname == null) { |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
for (int i = 0; i < CRITICAL_SECTION_COMPONENTS.length; i++) { |
| 233 |
if (cname.equals(CRITICAL_SECTION_COMPONENTS[i])) { |
| 234 |
return true; |
| 235 |
} |
| 236 |
} |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public static boolean isPlatformCriticalPackage(@Nullable ComponentName.WithComponentName wcn) { |
| 247 |
return (wcn != null) && isPlatformPackage(wcn.getComponentName()) && |
| 248 |
isCriticalPackage(wcn.getComponentName()); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public static boolean isPlatformNonCriticalPackage( |
| 258 |
@Nullable ComponentName.WithComponentName wcn) { |
| 259 |
return (wcn != null) && isPlatformPackage(wcn.getComponentName()) && |
| 260 |
!isCriticalPackage(wcn.getComponentName()); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public static <TRec extends ComponentName.WithComponentName> Predicate<TRec> filterRecord( |
| 268 |
@Nullable String filterString) { |
| 269 |
|
| 270 |
if (TextUtils.isEmpty(filterString)) { |
| 271 |
return rec -> false; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
if ("all".equals(filterString)) { |
| 276 |
return Objects::nonNull; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
if ("all-platform".equals(filterString)) { |
| 281 |
return DumpUtils::isPlatformPackage; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
if ("all-non-platform".equals(filterString)) { |
| 286 |
return DumpUtils::isNonPlatformPackage; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
if ("all-platform-critical".equals(filterString)) { |
| 291 |
return DumpUtils::isPlatformCriticalPackage; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
if ("all-platform-non-critical".equals(filterString)) { |
| 296 |
return DumpUtils::isPlatformNonCriticalPackage; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
final ComponentName filterCname = ComponentName.unflattenFromString(filterString); |
| 301 |
if (filterCname != null) { |
| 302 |
|
| 303 |
return rec -> (rec != null) && filterCname.equals(rec.getComponentName()); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
final int id = ParseUtils.parseIntWithBase(filterString, 16, -1); |
| 309 |
return rec -> { |
| 310 |
final ComponentName cn = rec.getComponentName(); |
| 311 |
return ((id != -1) && (System.identityHashCode(rec) == id)) |
| 312 |
|| cn.flattenToString().toLowerCase().contains(filterString.toLowerCase()); |
| 313 |
}; |
| 314 |
} |
| 315 |
} |
| 316 |
|