1.1UsbDeviceManager.java/高
主要完成功能切换及状态的更新,需要向外广播状态,接收kernel发送过来的event信息。实质性的工作都在这里边完成。
重要等级:高
1.2 UsbManager.java /usbService.java/中
这两个主要是接口,因为usb的工作本身就应该在kernel里,framework只是简单的逻辑流程重组。Android
下面这个体现了manager、service的工作方式,用户会通过usbManager的调用来完成usb功能的设置。
public int setUsbTethering(boolean enable){
………………
1.3UsbSettings.java
packages/apps/Settings/src/com/android/settings/deviceinfo
这个是用户在setting中,选择不同的存储类型时的,进行的界面更新及状态切换的处理流程。
1.4UsbSettingsManager.java
frameworks/base/services/java/com/android/server/usb
没有明白具体功能是干什么的,在UsbDeviceManager.java有用到一些。
发送usb device attached
1.5StorageNotification.java/高
frameworks/base/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java
主要用来处理存储设备相关动作的状态更新、通知。主要涉及两个方面:一是插入usbcabel后的状态栏更新,主要针对ums功能。二是storage状态发生改变时,会调用这个activity,如mount、unmount等。
所以入口主要有两个:onUsbMassStorageConnecti
1.6UsbStorageActivity.java/高
1.7Usbstring.c
1.8UsbDevice.java
/frameworks/base/core/java/android/hardware/usb
1.9UsbDeviceConnection.java
/frameworks/base/core/java/android/hardware/usb
usb设备的打开,基本传输等动作,似乎是native层面的。
1.10UsbDisconnectedReceiver.java
/frameworks/base/core/java/android/hardware/usb
接收断开的信息,android.hardware.usb.action.USB_DEVICE_DETACHED
1.11UsbHostManager.java
UsbDeviceAdded函数:报告有USB设备插入,通过getCurrentSettings().deviceAttached发送intent消息:UsbManager.ACTION_USB_DEVICE_ATTACHED。
对应的JNI函数com_android_server_UsbHostManager.cpp
其中的monitorUsbHostBus会调用usb_host_run函数(system/core/libusbhost/usbhost.c)不停的读取bus总线,读取到以后,调用注册的回调函数usb_device_added--àUsbDeviceAdded(method_usbDeviceAdded)
1. 首先在system server 中有以下代码
boolean disableNonCoreServices = SystemProperties.getBoolean("config.disable_noncore", false);
if (!disableNonCoreServices) {
try {
Slog.i(TAG, "USB Service");
// Manage USB host and device support , usb 服务的启动
usb = new UsbService(context);
ServiceManager.addService(Context.USB_SERVICE, usb);
} catch (Throwable e) {
reportWtf("starting UsbService", e);
}
try {
Slog.i(TAG, "Serial Service");
// Serial port support 串口服务的启动
serial = new SerialService(context);
ServiceManager.addService(Context.SERIAL_SERVICE, serial);
} catch (Throwable e) {
Slog.e(TAG, "Failure starting SerialService", e);
}
}
2. 看 usbservice 的实现
public class UsbService extends IUsbManager.Stub
public UsbService(Context context) {
mContext = context;
final PackageManager pm = mContext.getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {
mHostManager = new UsbHostManager(context);
}
if (new File("/sys/class/android_usb").exists()) {
mDeviceManager = new UsbDeviceManager(context);
}
setCurrentUser(UserHandle.USER_OWNER);
final IntentFilter userFilter = new IntentFilter();
userFilter.addAction(Intent.ACTION_USER_SWITCHED);
userFilter.addAction(Intent.ACTION_USER_STOPPED);
mContext.registerReceiver(mUserReceiver, userFilter, null, null);
}
public UsbHostManager(Context context) {
mContext = context;
mHostBlacklist = context.getResources().getStringArray(
com.android.internal.R.array.config_usbHostBlacklist);
}
public UsbDeviceManager(Context context) {
mContext = context;
mContentResolver = context.getContentResolver();
PackageManager pm = mContext.getPackageManager();
mHasUsbAccessory = pm.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
initRndisAddress();
readOemUsbOverrideConfig();
mHandler = new UsbHandler(FgThread.get().getLooper());
if (nativeIsStartRequested()) {
if (DEBUG) Slog.d(TAG, "accessory attached at boot");
startAccessoryMode();
}
boolean secureAdbEnabled = SystemProperties.getBoolean("ro.adb.secure", false);
boolean dataEncrypted = "1".equals(SystemProperties.get("vold.decrypt"));
if (secureAdbEnabled && !dataEncrypted) {
mDebuggingManager = new UsbDebuggingManager(context);
}
}
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions an
* limitations under the License.
*/
package com.android.server.usb;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Handler;
import android.os.Environment;
import android.os.FileUtils;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Slog;
import android.util.Base64;
import com.android.server.FgThread;
import java.lang.Thread;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.util.Arrays;
public class UsbDebuggingManager implements Runnable {
private static final String TAG = "UsbDebuggingManager";
private static final boolean DEBUG = false;
private final String ADBD_SOCKET = "adbd";
private final String ADB_DIRECTORY = "misc/adb";
private final String ADB_KEYS_FILE = "adb_keys";
private final int BUFFER_SIZE = 4096;
private final Context mContext;
private final Handler mHandler;
private Thread mThread;
private boolean mAdbEnabled = false;
private String mFingerprints;
private LocalSocket mSocket = null;
private OutputStream mOutputStream = null;
public UsbDebuggingManager(Context context) {
mHandler = new UsbDebuggingHandler(FgThread.get().getLooper());
mContext = context;
}
private void listenToSocket() throws IOException {
try {
byte[] buffer = new byte[BUFFER_SIZE];
LocalSocketAddress address = new LocalSocketAddress(ADBD_SOCKET,
LocalSocketAddress.Namespace.RESERVED);
InputStream inputStream = null;
mSocket = new LocalSocket();
mSocket.connect(address);
mOutputStream = mSocket.getOutputStream();
inputStream = mSocket.getInputStream();
while (true) {
int count = inputStream.read(buffer);
if (count < 0) {
Slog.e(TAG, "got " + count + " reading");
break;
}
if (buffer[0] == 'P' && buffer[1] == 'K') {
String key = new String(Arrays.copyOfRange(buffer, 2, count));
Slog.d(TAG, "Received public key: " + key);
Message msg = mHandler.obtainMessage(UsbDebuggingHandler.MESSAGE_ADB_CONFIRM);
msg.obj = key;
mHandler.sendMessage(msg);
}
else {
Slog.e(TAG, "Wrong message: " + (new String(Arrays.copyOfRange(buffer, 0, 2))));
break;
}
}
} catch (IOException ex) {
Slog.e(TAG, "Communication error: ", ex);
throw ex;
} finally {
closeSocket();
}
}
@Override
public void run() {
while (mAdbEnabled) {
try {
listenToSocket();
} catch (Exception e) {
/* Don't loop too fast if adbd dies, before init restarts it */
SystemClock.sleep(1000);
}
}
}
private void closeSocket() {
if (mOutputStream != null) {
try {
mOutputStream.close();
} catch (IOException e) {
Slog.e(TAG, "Failed closing output stream: " + e);
}
}
if (mSocket != null) {
try {
mSocket.close();
} catch (IOException ex) {
Slog.e(TAG, "Failed closing socket: " + ex);
}
}
}
private void sendResponse(String msg) {
if (mOutputStream != null) {
try {
mOutputStream.write(msg.getBytes());
}
catch (IOException ex) {
Slog.e(TAG, "Failed to write response:", ex);
}
}
}
class UsbDebuggingHandler extends Handler {
private static final int MESSAGE_ADB_ENABLED = 1;
private static final int MESSAGE_ADB_DISABLED = 2;
private static final int MESSAGE_ADB_ALLOW = 3;
private static final int MESSAGE_ADB_DENY = 4;
private static final int MESSAGE_ADB_CONFIRM = 5;
private static final int MESSAGE_ADB_CLEAR = 6;
public UsbDebuggingHandler(Looper looper) {
super(looper);
}
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_ADB_ENABLED:
if (mAdbEnabled)
break;
mAdbEnabled = true;
mThread = new Thread(UsbDebuggingManager.this, TAG);
mThread.start();
break;
case MESSAGE_ADB_DISABLED:
if (!mAdbEnabled)
break;
mAdbEnabled = false;
closeSocket();
try {
mThread.join();
} catch (Exception ex) {
}
mThread = null;
mOutputStream = null;
mSocket = null;
break;
case MESSAGE_ADB_ALLOW: {
String key = (String)msg.obj;
String fingerprints = getFingerprints(key);
if (!fingerprints.equals(mFingerprints)) {
Slog.e(TAG, "Fingerprints do not match. Got "
+ fingerprints + ", expected " + mFingerprints);
break;
}
if (msg.arg1 == 1) {
writeKey(key);
}
sendResponse("OK");
break;
}
case MESSAGE_ADB_DENY:
sendResponse("NO");
break;
case MESSAGE_ADB_CONFIRM: {
String key = (String)msg.obj;
mFingerprints = getFingerprints(key);
showConfirmationDialog(key, mFingerprints);
break;
}
case MESSAGE_ADB_CLEAR:
deleteKeyFile();
break;
}
}
}
private String getFingerprints(String key) {
String hex = "0123456789ABCDEF";
StringBuilder sb = new StringBuilder();
MessageDigest digester;
try {
digester = MessageDigest.getInstance("MD5");
} catch (Exception ex) {
Slog.e(TAG, "Error getting digester: " + ex);
return "";
}
byte[] base64_data = key.split("\\s+")[0].getBytes();
byte[] digest = digester.digest(Base64.decode(base64_data, Base64.DEFAULT));
for (int i = 0; i < digest.length; i++) {
sb.append(hex.charAt((digest[i] >> 4) & 0xf));
sb.append(hex.charAt(digest[i] & 0xf));
if (i < digest.length - 1)
sb.append(":");
}
return sb.toString();
}
private void showConfirmationDialog(String key, String fingerprints) {
Intent dialogIntent = new Intent();
dialogIntent.setClassName("com.android.systemui",
"com.android.systemui.usb.UsbDebuggingActivity");
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.putExtra("key", key);
dialogIntent.putExtra("fingerprints", fingerprints);
try {
mContext.startActivity(dialogIntent);
} catch (ActivityNotFoundException e) {
Slog.e(TAG, "unable to start UsbDebuggingActivity");
}
}
private File getUserKeyFile() {
File dataDir = Environment.getDataDirectory();
File adbDir = new File(dataDir, ADB_DIRECTORY);
if (!adbDir.exists()) {
Slog.e(TAG, "ADB data directory does not exist");
return null;
}
return new File(adbDir, ADB_KEYS_FILE);
}
private void writeKey(String key) {
try {
File keyFile = getUserKeyFile();
if (keyFile == null) {
return;
}
if (!keyFile.exists()) {
keyFile.createNewFile();
FileUtils.setPermissions(keyFile.toString(),
FileUtils.S_IRUSR | FileUtils.S_IWUSR |
FileUtils.S_IRGRP, -1, -1);
}
FileOutputStream fo = new FileOutputStream(keyFile, true);
fo.write(key.getBytes());
fo.write('\n');
fo.close();
}
catch (IOException ex) {
Slog.e(TAG, "Error writing key:" + ex);
}
}
private void deleteKeyFile() {
File keyFile = getUserKeyFile();
if (keyFile != null) {
keyFile.delete();
}
}
public void setAdbEnabled(boolean enabled) {
mHandler.sendEmptyMessage(enabled ? UsbDebuggingHandler.MESSAGE_ADB_ENABLED
: UsbDebuggingHandler.MESSAGE_ADB_DISABLED);
}
public void allowUsbDebugging(boolean alwaysAllow, String publicKey) {
Message msg = mHandler.obtainMessage(UsbDebuggingHandler.MESSAGE_ADB_ALLOW);
msg.arg1 = alwaysAllow ? 1 : 0;
msg.obj = publicKey;
mHandler.sendMessage(msg);
}
public void denyUsbDebugging() {
mHandler.sendEmptyMessage(UsbDebuggingHandler.MESSAGE_ADB_DENY);
}
public void clearUsbDebuggingKeys() {
mHandler.sendEmptyMessage(UsbDebuggingHandler.MESSAGE_ADB_CLEAR);
}
public void dump(FileDescriptor fd, PrintWriter pw) {
pw.println(" USB Debugging State:");
pw.println(" Connected to adbd: " + (mOutputStream != null));
pw.println(" Last key received: " + mFingerprints);
pw.println(" User keys:");
try {
pw.println(FileUtils.readTextFile(new File("/data/misc/adb/adb_keys"), 0, null));
} catch (IOException e) {
pw.println("IOException: " + e);
}
pw.println(" System keys:");
try {
pw.println(FileUtils.readTextFile(new File("/adb_keys"), 0, null));
} catch (IOException e) {
pw.println("IOException: " + e);
}
}
}