1 /* 2 * Copyright (C) 2010 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 an 14 * limitations under the License. 15 */ 16 17 package com.android.server.usb; 18 19 import android.app.PendingIntent; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.hardware.usb.IUsbManager; 23 import android.hardware.usb.UsbAccessory; 24 import android.hardware.usb.UsbDevice; 25 import android.net.Uri; 26 import android.os.Binder; 27 import android.os.Bundle; 28 import android.os.ParcelFileDescriptor; 29 30 import java.io.File; 31 import java.io.FileDescriptor; 32 import java.io.PrintWriter; 33 34 /** 35 * UsbService manages all USB related state, including both host and device support. 36 * Host related events and calls are delegated to UsbHostManager, and device related 37 * support is delegated to UsbDeviceManager. 38 */ 39 public class UsbService extends IUsbManager.Stub { 40 private final Context mContext; 41 private UsbDeviceManager mDeviceManager; 42 private UsbHostManager mHostManager; 43 private final UsbSettingsManager mSettingsManager; 44 45 46 public UsbService(Context context) { 47 mContext = context; 48 mSettingsManager = new UsbSettingsManager(context); 49 PackageManager pm = mContext.getPackageManager(); 50 if (pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) { 51 mHostManager = new UsbHostManager(context, mSettingsManager); 52 } 53 if (new File("/sys/class/android_usb").exists()) { 54 mDeviceManager = new UsbDeviceManager(context, mSettingsManager); 55 } 56 } 57 58 public void systemReady() { 59 if (mDeviceManager != null) { 60 mDeviceManager.systemReady(); 61 } 62 if (mHostManager != null) { 63 mHostManager.systemReady(); 64 } 65 } 66 67 /* Returns a list of all currently attached USB devices (host mdoe) */ 68 public void getDeviceList(Bundle devices) { 69 if (mHostManager != null) { 70 mHostManager.getDeviceList(devices); 71 } 72 } 73 74 /* Opens the specified USB device (host mode) */ 75 public ParcelFileDescriptor openDevice(String deviceName) { 76 if (mHostManager != null) { 77 return mHostManager.openDevice(deviceName); 78 } else { 79 return null; 80 } 81 } 82 83 /* returns the currently attached USB accessory (device mode) */ 84 public UsbAccessory getCurrentAccessory() { 85 if (mDeviceManager != null) { 86 return mDeviceManager.getCurrentAccessory(); 87 } else { 88 return null; 89 } 90 } 91 92 /* opens the currently attached USB accessory (device mode) */ 93 public ParcelFileDescriptor openAccessory(UsbAccessory accessory) { 94 if (mDeviceManager != null) { 95 return mDeviceManager.openAccessory(accessory); 96 } else { 97 return null; 98 } 99 } 100 101 public void setDevicePackage(UsbDevice device, String packageName) { 102 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); 103 mSettingsManager.setDevicePackage(device, packageName); 104 } 105 106 public void setAccessoryPackage(UsbAccessory accessory, String packageName) { 107 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); 108 mSettingsManager.setAccessoryPackage(accessory, packageName); 109 } 110 111 public boolean hasDevicePermission(UsbDevice device) { 112 return mSettingsManager.hasPermission(device); 113 } 114 115 public boolean hasAccessoryPermission(UsbAccessory accessory) { 116 return mSettingsManager.hasPermission(accessory); 117 } 118 119 public void requestDevicePermission(UsbDevice device, String packageName, 120 PendingIntent pi) { 121 mSettingsManager.requestPermission(device, packageName, pi); 122 } 123 124 public void requestAccessoryPermission(UsbAccessory accessory, String packageName, 125 PendingIntent pi) { 126 mSettingsManager.requestPermission(accessory, packageName, pi); 127 } 128 129 public void grantDevicePermission(UsbDevice device, int uid) { 130 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); 131 mSettingsManager.grantDevicePermission(device, uid); 132 } 133 134 public void grantAccessoryPermission(UsbAccessory accessory, int uid) { 135 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); 136 mSettingsManager.grantAccessoryPermission(accessory, uid); 137 } 138 139 public boolean hasDefaults(String packageName) { 140 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); 141 return mSettingsManager.hasDefaults(packageName); 142 } 143 144 public void clearDefaults(String packageName) { 145 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); 146 mSettingsManager.clearDefaults(packageName); 147 } 148 149 public void setCurrentFunction(String function, boolean makeDefault) { 150 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); 151 if (mDeviceManager != null) { 152 mDeviceManager.setCurrentFunction(function, makeDefault); 153 } else { 154 throw new IllegalStateException("USB device mode not supported"); 155 } 156 } 157 158 public void setMassStorageBackingFile(String path) { 159 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); 160 if (mDeviceManager != null) { 161 mDeviceManager.setMassStorageBackingFile(path); 162 } else { 163 throw new IllegalStateException("USB device mode not supported"); 164 } 165 } 166 167 @Override 168 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 169 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 170 != PackageManager.PERMISSION_GRANTED) { 171 pw.println("Permission Denial: can't dump UsbManager from from pid=" 172 + Binder.getCallingPid() 173 + ", uid=" + Binder.getCallingUid()); 174 return; 175 } 176 177 pw.println("USB Manager State:"); 178 179 if (mDeviceManager != null) { 180 mDeviceManager.dump(fd, pw); 181 } 182 if (mHostManager != null) { 183 mHostManager.dump(fd, pw); 184 } 185 mSettingsManager.dump(fd, pw); 186 } 187 } 188