android4.0 hid插入提示

來源:互聯網
上載者:User

標籤:

具體原理這裡就不說了 我也沒理順 網上有很多文章都說的很清楚 這裡我就直接上重點

主要修改檔案

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);}




android4.0 hid插入提示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.