android4.0 hid插入提示

具体原理这里就不说了 我也没理顺 网上有很多文章都说的很清楚 这里我就直接上重点

主要修改文件

frameworks/base/service/java/com/android/server/usb/UsbService.java

frameworks/base/service/java/com/android/server/usb/UsbHostManager.java

首先将UsbService.java中的public UsbService(Context context)改为这样

    public UsbService(Context context) {
        mContext = context;
        mSettingsManager = new UsbSettingsManager(context);
        PackageManager pm = mContext.getPackageManager();
        //if (pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {//modify by hclydao
            mHostManager = new UsbHostManager(context, mSettingsManager);
        //}
        if (new File("/sys/class/android_usb").exists()) {
            mDeviceManager = new UsbDeviceManager(context, mSettingsManager);
        }
    }

不然usbhostmanager不会执行

接下来主要就是修改usbhostmanager了 当硬件usb host插入里会执行usbhostmanager里的usbDeviceAdded函数 拔掉时会执行usbDeviceRemoved

当设备插入时在usb jni里会回调usbDeviceAdded函数 此时会传回这个usb设备的相关信息 但是发现class都是0 还好 在int [] interfaceValues传回了整个usb相关信息interfaceValues[1] 对应的就是相应的class 3对应的是usb
hid 8对应的是mass storage 9对就的是usb hub 提示图片和信息直接使用系统自带的

最后修改为如下

/*
 * Copyright (C) 2011 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.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.IUsbManager;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.os.ParcelFileDescriptor;
import android.os.UEventObserver;
import android.provider.Settings;
import android.util.Slog;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.res.Resources;
import java.util.ArrayList;
/**
 * UsbHostManager manages USB state in host mode.
 */
public class UsbHostManager {
    private static final String TAG = UsbHostManager.class.getSimpleName();
    private static final boolean LOG = false;

    // contains all connected USB devices
    private final HashMap<String,UsbDevice> mDevices = new HashMap<String,UsbDevice>();

    // USB busses to exclude from USB host support
    private final String[] mHostBlacklist;

    private final Context mContext;
    private final Object mLock = new Object();
    private final UsbSettingsManager mSettingsManager;
	private Notification mUsbHostNotification;
	private ArrayList<String> dlist;
    public UsbHostManager(Context context, UsbSettingsManager settingsManager) {
        mContext = context;
        mSettingsManager = settingsManager;
        mHostBlacklist = context.getResources().getStringArray(
                com.android.internal.R.array.config_usbHostBlacklist);
    }

    private boolean isBlackListed(String deviceName) {
        int count = mHostBlacklist.length;
        for (int i = 0; i < count; i++) {
            if (deviceName.startsWith(mHostBlacklist[i])) {
                return true;
            }
        }
        return false;
    }

    /* returns true if the USB device should not be accessible by applications */
    private boolean isBlackListed(int clazz, int subClass, int protocol) {
        // blacklist hubs
        if (clazz == UsbConstants.USB_CLASS_HUB) return true;

        // blacklist HID boot devices (mouse and keyboard)
        if (clazz == UsbConstants.USB_CLASS_HID &&
                subClass == UsbConstants.USB_INTERFACE_SUBCLASS_BOOT) {
            return true;
        }

        return false;
    }

    /* Called from JNI in monitorUsbHostBus() to report new USB devices */
    private void usbDeviceAdded(String deviceName, int vendorID, int productID,
            int deviceClass, int deviceSubclass, int deviceProtocol,
            /* array of quintuples containing id, class, subclass, protocol
               and number of endpoints for each interface */
            int[] interfaceValues,
           /* array of quadruples containing address, attributes, max packet size
              and interval for each endpoint */
            int[] endpointValues) {
		if((interfaceValues != null) && (interfaceValues[1] == UsbConstants.USB_CLASS_HID)) {//add by hclydao
            setUsbHostNotification(
                    com.android.internal.R.string.usb_storage_notification_title,
                    com.android.internal.R.string.usb_storage_notification_title,
                    com.android.internal.R.drawable.stat_sys_tether_usb, true, true, null);
			if(dlist == null)
				dlist = new ArrayList<String>();
			dlist.add(deviceName);
		}
        if (isBlackListed(deviceName) ||
                isBlackListed(deviceClass, deviceSubclass, deviceProtocol)) {
            return;
        }

        synchronized (mLock) {
            if (mDevices.get(deviceName) != null) {
                Slog.w(TAG, "device already on mDevices list: " + deviceName);
                return;
            }

            int numInterfaces = interfaceValues.length / 5;
            Parcelable[] interfaces = new UsbInterface[numInterfaces];
            try {
                // repackage interfaceValues as an array of UsbInterface
                int intf, endp, ival = 0, eval = 0;
                for (intf = 0; intf < numInterfaces; intf++) {
                    int interfaceId = interfaceValues[ival++];
                    int interfaceClass = interfaceValues[ival++];
                    int interfaceSubclass = interfaceValues[ival++];
                    int interfaceProtocol = interfaceValues[ival++];
                    int numEndpoints = interfaceValues[ival++];

                    Parcelable[] endpoints = new UsbEndpoint[numEndpoints];
                    for (endp = 0; endp < numEndpoints; endp++) {
                        int address = endpointValues[eval++];
                        int attributes = endpointValues[eval++];
                        int maxPacketSize = endpointValues[eval++];
                        int interval = endpointValues[eval++];
                        endpoints[endp] = new UsbEndpoint(address, attributes,
                                maxPacketSize, interval);
                    }

                    // don't allow if any interfaces are blacklisted
                    if (isBlackListed(interfaceClass, interfaceSubclass, interfaceProtocol)) {
                        return;
                    }
                    interfaces[intf] = new UsbInterface(interfaceId, interfaceClass,
                            interfaceSubclass, interfaceProtocol, endpoints);
                }
            } catch (Exception e) {
                // beware of index out of bound exceptions, which might happen if
                // a device does not set bNumEndpoints correctly
                Slog.e(TAG, "error parsing USB descriptors", e);
                return;
            }

            UsbDevice device = new UsbDevice(deviceName, vendorID, productID,
                    deviceClass, deviceSubclass, deviceProtocol, interfaces);
            mDevices.put(deviceName, device);
            mSettingsManager.deviceAttached(device);
        }
    }

    /* Called from JNI in monitorUsbHostBus to report USB device removal */
    private void usbDeviceRemoved(String deviceName) {
		if(dlist != null) {
			for(int i = 0;i<dlist.size();i++) {
				if(dlist.get(i).equals(deviceName)) {
					dlist.remove(i);
/*
				    setUsbHostNotification(
				            com.android.internal.R.string.ext_media_nomedia_notification_title,
				            com.android.internal.R.string.ext_media_nomedia_notification_message,
				            com.android.internal.R.drawable.stat_sys_tether_usb,
				            false, false, null);
*/
					setUsbHostNotification(0, 0, 0, false, false, null);
					break;
				}
			}
		}
        synchronized (mLock) {
            UsbDevice device = mDevices.remove(deviceName);
            if (device != null) {
                mSettingsManager.deviceDetached(device);
            }
        }
    }

    public void systemReady() {
        synchronized (mLock) {
            // Create a thread to call into native code to wait for USB host events.
            // This thread will call us back on usbDeviceAdded and usbDeviceRemoved.
            Runnable runnable = new Runnable() {
                public void run() {
                    monitorUsbHostBus();
                }
            };
            new Thread(null, runnable, "UsbService host thread").start();
        }
    }

    /* Returns a list of all currently attached USB devices */
    public void getDeviceList(Bundle devices) {
        synchronized (mLock) {
            for (String name : mDevices.keySet()) {
                devices.putParcelable(name, mDevices.get(name));
            }
        }
    }

    /* Opens the specified USB device */
    public ParcelFileDescriptor openDevice(String deviceName) {
        synchronized (mLock) {
            if (isBlackListed(deviceName)) {
                throw new SecurityException("USB device is on a restricted bus");
            }
            UsbDevice device = mDevices.get(deviceName);
            if (device == null) {
                // if it is not in mDevices, it either does not exist or is blacklisted
                throw new IllegalArgumentException(
                        "device " + deviceName + " does not exist or is restricted");
            }
            mSettingsManager.checkPermission(device);
            return nativeOpenDevice(deviceName);
        }
    }

    public void dump(FileDescriptor fd, PrintWriter pw) {
        synchronized (mLock) {
            pw.println("  USB Host State:");
            for (String name : mDevices.keySet()) {
                pw.println("    " + name + ": " + mDevices.get(name));
            }
        }
    }

    private synchronized void setUsbHostNotification(int titleId, int messageId, int icon, boolean visible,
                                                          boolean dismissable, PendingIntent pi) {

        if (!visible && mUsbHostNotification == null) {
            return;
        }

        NotificationManager notificationManager = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);

        if (notificationManager == null) {
            return;
        }

        if (mUsbHostNotification != null && visible) {
            /*
             * Dismiss the previous notification - we're about to
             * re-use it.
             */
            final int notificationId = mUsbHostNotification.icon;
            notificationManager.cancel(notificationId);
        }

        if (visible) {
            Resources r = Resources.getSystem();
            CharSequence title = r.getText(titleId);
            CharSequence message = r.getText(messageId);

            if (mUsbHostNotification == null) {
                mUsbHostNotification = new Notification();
                mUsbHostNotification.when = 0;
            }

            mUsbHostNotification.defaults &= ~Notification.DEFAULT_SOUND;

            if (dismissable) {
                mUsbHostNotification.flags = Notification.FLAG_AUTO_CANCEL;
            } else {
                mUsbHostNotification.flags = Notification.FLAG_ONGOING_EVENT;
            }

            mUsbHostNotification.tickerText = title;
            if (pi == null) {
                Intent intent = new Intent();
                pi = PendingIntent.getBroadcast(mContext, 0, intent, 0);
            }

            mUsbHostNotification.icon = icon;
            mUsbHostNotification.setLatestEventInfo(mContext, title, message, pi);
        }

        final int notificationId = mUsbHostNotification.icon;
        if (visible) {
            notificationManager.notify(notificationId, mUsbHostNotification);
        } else {
            notificationManager.cancel(notificationId);
        }
    }

    private native void monitorUsbHostBus();
    private native ParcelFileDescriptor nativeOpenDevice(String deviceName);
}
时间: 2024-08-25 13:56:48

android4.0 hid插入提示的相关文章

QT210 android2.3 和android4.0 烧写编译日记

QT210下载烧录编译android2.3过程 工作环境:ubuntu12.04.5 | QT210开发板光盘 | QT210开发板 android2.3编译环境:gcc version 4.4.7  | java version 6 | java version 5 | git version 1.7.9.5 tips by chsry:浅灰色是终端窗口运行保存的部分命令和信息,ubuntu14.04无法编译QT210 android2.3(无法安装java6) 安装好ubuntu12.04.

Android4.0 声卡配置-高通msm8916移植

一个正常的UAC设备插入Android 7.0是默认打开UAC配置的,打印的log如下: [ 2367.490491] usb 3-3.2: new full-speed USB device number 9 using xhci_hcd [ 2367.580010] usb 3-3.2: New USB device found, idVendor=0d8c, idProduct=0132 [ 2367.580018] usb 3-3.2: New USB device strings: M

android4.0蓝牙使能的详细解析

此博客是转载过来的哦... 给自己博客定几个部分: (1)写在前面的话:一些写博客时的废话. (2)内容简介:把文章的主要内容或者核心部分作一个框架性的概括,以方便大家阅读. (3)正文:这个不需要解释了.   写在前面的话:这是csdn上的第一篇博客,希望自己能够坚持写下去,也希望能够得到大家的支持.本文可能会涉及大量的源码注释,在文字方面可能不够尽如人意,希望真正想理解该过程的同学们能够耐心看下去. 内容简介:本文详细分析了android4.0中蓝牙使能的过程,相比较android2.3,4

android4.0 A10开发板,如何实现分屏(多屏幕显示)不同的内容

============问题描述============ 设想是如上图所示 目前的情况是,我有A10的开发板,android4.0.4 如果有同行的话,希望能指点一下 目前能单独显示vga,hdmi,lvds三种屏幕 vga  和  hdmi如果同时插入的话总有一个是不能正常显示的,因为我配置的 sys_config1.fex文件里面有单独针对的参数, 只要能让我实现分屏显示不同的内容(不同的屏,显示的不同的内容连接的是同一块板子)就行,连接显示是vga和tv     vga和hdmi    h

Ubuntu12.04下载Android4.0.1源码全过程,附若干问题解决[转]

学校里一直在做应用层开发,考虑到日后就业问题,这次决定研究源码和驱动,并进行编译.没想到就下载源码这一步折腾了我整整两天,期间遇到很多问题,哎,记录于此,希望日后再下源码的人不要再走无谓的弯路了.事实上可以在这里http://zhu.im/Android/下载源码,但是google推荐用repo来下载,为了多学东西就学下repo吧,毕竟下现成的也太么含量了.最初我参考的是老罗的博客http://blog.csdn.net/luoshengyang/article/details/6559955

Ubuntu12.04编译Android4.0.1源码全过程-----附wubi安装ubuntu编译android源码硬盘空间不够的问题解决

本文转至  http://blog.csdn.net/yanzi1225627/article/details/9263097 昨晚在编译源码,make一段时间之后报错如下: [html] view plaincopyprint? # A fatal error has been detected by the Java Runtime Environment: # #  SIGSEGV (0xb) at pc=0x40362d33, pid=12195, tid=2835454784 # # 

[bug修复方案分享]Android4.0以上弹出Notification时图标显示不正常

1. 现象 在Android4.0以上系统中,弹出通知栏消息时图标显示不全,如下图: v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} Normal 0 10 pt 0 2 false false false EN-US ZH-CN X-NONE $([{£¥·'"

android4.0蓝牙使能的详细解析 (转载)

此博客是转载过来的哦... 给自己博客定几个部分: (1)写在前面的话:一些写博客时的废话. (2)内容简介:把文章的主要内容或者核心部分作一个框架性的概括,以方便大家阅读. (3)正文:这个不需要解释了.   写在前面的话:这是csdn上的第一篇博客,希望自己能够坚持写下去,也希望能够得到大家的支持.本文可能会涉及大量的源码注释,在文字方面可能不够尽如人意,希望真正想理解该过程的同学们能够耐心看下去. 内容简介:本文详细分析了android4.0中蓝牙使能的过程,相比较android2.3,4

【转】如何下载并编译Android4.0内核源码goldfish(图文)

原文网址:http://blog.csdn.net/flydream0/article/details/7070392 关于如何下载Android4.0源码,请查看我的博客内另一篇文章(同样是图文教程): http://blog.csdn.net/flydream0/article/details/7036156 如何编译Android4.0源码请看: http://blog.csdn.net/flydream0/article/details/7046612 下面进入正题: 第一步:下载gol