WifiAwareShellCommand.java revision dc929704e04d634eff04dfd55024b18578be9717
1/* 2 * Copyright (C) 2017 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 17package com.android.server.wifi.aware; 18 19import android.app.AppGlobals; 20import android.content.pm.IPackageManager; 21import android.os.Binder; 22import android.os.ShellCommand; 23 24import java.io.PrintWriter; 25 26/** 27 * Interprets and executes 'adb shell cmd wifiaware [args]'. 28 */ 29public class WifiAwareShellCommand extends ShellCommand { 30 private final WifiAwareStateManager mStateManager; 31 private final IPackageManager mPM; 32 33 WifiAwareShellCommand(WifiAwareStateManager stateManager) { 34 mStateManager = stateManager; 35 mPM = AppGlobals.getPackageManager(); 36 } 37 38 @Override 39 public int onCommand(String cmd) { 40 checkRootPermission(); 41 42 final PrintWriter pw = getOutPrintWriter(); 43 try { 44 switch (cmd != null ? cmd : "") { 45 default: 46 return handleDefaultCommands(cmd); 47 } 48 } catch (Exception e) { 49 pw.println("Exception: " + e); 50 } 51 return -1; 52 } 53 54 private void checkRootPermission() { 55 final int uid = Binder.getCallingUid(); 56 if (uid == 0) { 57 // Root can do anything. 58 return; 59 } 60 throw new SecurityException("Uid " + uid + " does not have access to wifiaware commands"); 61 } 62 63 @Override 64 public void onHelp() { 65 final PrintWriter pw = getOutPrintWriter(); 66 67 pw.println("Wi-Fi Aware (wifiaware) commands:"); 68 pw.println(" help"); 69 pw.println(" Print this help text."); 70 pw.println(); 71 } 72} 73