Jason Monk | 7f01f3b | 2018-05-15 15:46:26 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.server.slice; |
| 16 | |
| 17 | import android.app.slice.SliceProvider; |
| 18 | import android.content.ContentResolver; |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.PackageInfo; |
| 21 | import android.net.Uri; |
| 22 | import android.os.Binder; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.Process; |
| 25 | import android.os.ShellCommand; |
| 26 | import android.util.ArraySet; |
| 27 | |
| 28 | import java.io.PrintWriter; |
| 29 | import java.util.List; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | public class SliceShellCommand extends ShellCommand { |
| 33 | |
| 34 | private final SliceManagerService mService; |
| 35 | |
| 36 | public SliceShellCommand(SliceManagerService service) { |
| 37 | mService = service; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public int onCommand(String cmd) { |
| 42 | if (cmd == null) { |
| 43 | return handleDefaultCommands(cmd); |
| 44 | } |
| 45 | switch (cmd) { |
| 46 | case "get-permissions": |
| 47 | return runGetPermissions(getNextArgRequired()); |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void onHelp() { |
| 54 | final PrintWriter pw = getOutPrintWriter(); |
| 55 | pw.println("Status bar commands:"); |
| 56 | pw.println(" help"); |
| 57 | pw.println(" Print this help text."); |
| 58 | pw.println(""); |
| 59 | pw.println(" get-permissions <authority>"); |
| 60 | pw.println(" List the pkgs that have permission to an authority."); |
| 61 | pw.println(""); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | private int runGetPermissions(String authority) { |
| 66 | if (Binder.getCallingUid() != Process.SHELL_UID |
| 67 | && Binder.getCallingUid() != Process.ROOT_UID) { |
| 68 | getOutPrintWriter().println("Only shell can get permissions"); |
| 69 | return -1; |
| 70 | } |
| 71 | Context context = mService.getContext(); |
| 72 | long ident = Binder.clearCallingIdentity(); |
| 73 | try { |
| 74 | Uri uri = new Uri.Builder() |
| 75 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 76 | .authority(authority) |
| 77 | .build(); |
| 78 | if (!SliceProvider.SLICE_TYPE.equals(context.getContentResolver().getType(uri))) { |
| 79 | getOutPrintWriter().println(authority + " is not a slice provider"); |
| 80 | return -1; |
| 81 | } |
| 82 | Bundle b = context.getContentResolver().call(uri, SliceProvider.METHOD_GET_PERMISSIONS, |
| 83 | null, null); |
| 84 | if (b == null) { |
| 85 | getOutPrintWriter().println("An error occurred getting permissions"); |
| 86 | return -1; |
| 87 | } |
| 88 | String[] permissions = b.getStringArray(SliceProvider.EXTRA_RESULT); |
| 89 | final PrintWriter pw = getOutPrintWriter(); |
| 90 | Set<String> listedPackages = new ArraySet<>(); |
| 91 | if (permissions != null && permissions.length != 0) { |
| 92 | List<PackageInfo> apps = |
| 93 | context.getPackageManager().getPackagesHoldingPermissions(permissions, 0); |
| 94 | for (PackageInfo app : apps) { |
| 95 | pw.println(app.packageName); |
| 96 | listedPackages.add(app.packageName); |
| 97 | } |
| 98 | } |
| 99 | for (String pkg : mService.getAllPackagesGranted(authority)) { |
| 100 | if (!listedPackages.contains(pkg)) { |
| 101 | pw.println(pkg); |
| 102 | listedPackages.add(pkg); |
| 103 | } |
| 104 | } |
| 105 | } finally { |
| 106 | Binder.restoreCallingIdentity(ident); |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | } |