国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【干貨】Android系統(tǒng)定制基礎(chǔ)篇:第十五部分(Android支持鼠標(biāo)右鍵返回、GPIO 控制方案、屬性標(biāo)識(shí)USB攝像頭的VID與PID)

這篇具有很好參考價(jià)值的文章主要介紹了【干貨】Android系統(tǒng)定制基礎(chǔ)篇:第十五部分(Android支持鼠標(biāo)右鍵返回、GPIO 控制方案、屬性標(biāo)識(shí)USB攝像頭的VID與PID)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

1、修改 frameworks/native/services/inputflinger/InputReader.cpp 如下:

diff --git a/frameworks/native/services/inputflinger/InputReader.cpp b/frameworks/native/services/inputflinger/Inp
index 7207a83..2721800 100755
--- a/frameworks/native/services/inputflinger/InputReader.cpp
+++ b/frameworks/native/services/inputflinger/InputReader.cpp
@@ -1422,6 +1422,7 @@ uint32_t CursorButtonAccumulator::getButtonState() const {
         result |= AMOTION_EVENT_BUTTON_PRIMARY;
     }
     if (mBtnRight) {
+        /*
         char targetProduct[PROPERTY_VALUE_MAX] = {0};
         property_get("ro.target.product", targetProduct, "");
         if (strcmp(targetProduct, "box") == 0) {
@@ -1429,6 +1430,9 @@ uint32_t CursorButtonAccumulator::getButtonState() const {
         } else {
             result |= AMOTION_EVENT_BUTTON_SECONDARY;
         }
+        */
+        
+        result |= AMOTION_EVENT_BUTTON_BACK;
     }
     if (mBtnMiddle) {
         result |= AMOTION_EVENT_BUTTON_TERTIARY;

二、Android GPIO 控制方案
GPIO 功能在 Android Framework 中增加 GPIO 相關(guān) API,讓 APP 可以直接通過(guò) JAVA API 操控 GPIO。支持 輸入、輸出、模擬按鍵 三種模式。做為輸入時(shí)可以用于app獲取外部設(shè)備的電平狀態(tài)。做為輸出時(shí)可以輸出高低電平,用于控制外設(shè)。當(dāng)做為模擬按鍵時(shí),此 GPIO 低電平時(shí) APP 會(huì)收到對(duì)應(yīng)的鍵值。

移植
參考項(xiàng)目 Android-GPIOControlDriver,移植驅(qū)動(dòng)和 Framework 代碼。
使用
1.在 APP 源碼 aidl/android/os/ 目錄下新建 IGpioService.aidl,如下:

package android.os;
 
/** {@hide} */
interface IGpioService
{
	int gpioWrite(int gpio, int value);
	int gpioRead(int gpio);
	int gpioDirection(int gpio, int direction, int value);
	int gpioRegKeyEvent(int gpio);
	int gpioUnregKeyEvent(int gpio);
	int gpioGetNumber();
}

2.參考下面源碼調(diào)用 GPIO 相關(guān) API:

package com.ayst.sample.items.gpio;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.IBinder;
import android.os.IGpioService;
import android.os.RemoteException;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/11/6.
 */
public class Gpio {
    private IGpioService mGpioService;

    @SuppressLint("WrongConstant")
    public Gpio(Context context) {
        Method method = null;
        try {
            method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
            IBinder binder = (IBinder) method.invoke(null, new Object[]{"gpio"});
            mGpioService = IGpioService.Stub.asInterface(binder);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * GPIO write
     *
     * @param gpio  0~Number
     * @param value 0: Low 1: High
     */
    public void gpioWrite(int gpio, int value) {
        if (null != mGpioService) {
            try {
                mGpioService.gpioWrite(gpio, value);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * GPIO read
     *
     * @param gpio 0~Number
     * @return 0: Low 1: High other:error
     */
    public int gpioRead(int gpio) {
        if (null != mGpioService) {
            try {
                return mGpioService.gpioRead(gpio);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    /**
     * GPIO direction
     *
     * @param gpio      0~Number
     * @param direction 0: input 1: output
     * @param value     0: Low 1: High
     */
    public void gpioDirection(int gpio, int direction, int value) {
        if (null != mGpioService) {
            try {
                mGpioService.gpioDirection(gpio, direction, value);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * GPIO register key event
     *
     * @param gpio 0~Number
     */
    public void gpioRegKeyEvent(int gpio) {
        if (null != mGpioService) {
            try {
                mGpioService.gpioRegKeyEvent(gpio);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * GPIO unregister key event
     *
     * @param gpio 0~Number
     */
    public void gpioUnregKeyEvent(int gpio) {
        if (null != mGpioService) {
            try {
                mGpioService.gpioUnregKeyEvent(gpio);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Get GPIO number
     *
     * @return <0: error other: GPIO number
     */
    public int gpioGetNumber() {
        if (null != mGpioService) {
            try {
                return mGpioService.gpioGetNumber();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }
}

輸入

// 將GPIO_0設(shè)置為輸入
gpio.gpioDirection(0, 0, 0);
// 讀GPIO_0電平
int level = gpio.gpioRead(0);

輸出

// 將GPIO_0設(shè)置為輸出,并默認(rèn)輸出低電平
gpio.gpioDirection(0, 1, 0);
// GPIO_0輸出高電平
gpio.gpioWrite(0, 1);

按鍵模式

/* 將GPIO_0設(shè)置按鍵模式
當(dāng)GPIO_0為低電平時(shí)將收到KeyEvent.KEYCODE_GPIO_0 KeyEvent.ACTION_DOWN,
當(dāng)GPIO_0為高電平時(shí)收到KeyEvent.KEYCODE_GPIO_0 KeyEvent.ACTION_UP
*/
gpio.gpioRegKeyEvent(0);

【干貨】Android系統(tǒng)定制基礎(chǔ)篇:第十五部分(Android支持鼠標(biāo)右鍵返回、GPIO 控制方案、屬性標(biāo)識(shí)USB攝像頭的VID與PID)完整 DEMO 源碼請(qǐng)參考:https://github.com/aystshen/topband_sample

三、屬性標(biāo)識(shí)USB攝像頭的VID與PID

Android 在使用多個(gè) USB 攝像頭時(shí),根據(jù)加載順序不同他們的設(shè)備文件順序不同,比如:“video0, video1, video2”,每次啟動(dòng)它們的順序都可能不同,這樣 APP 就無(wú)法知道哪個(gè)設(shè)備文件對(duì)應(yīng)的是哪個(gè)攝像頭,因此下面方案增加屬性來(lái)標(biāo)識(shí)設(shè)備文件與攝像頭 VID、PID 的關(guān)系,這樣就解決了上面的問(wèn)題。
實(shí)現(xiàn)

diff --git a/frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java
index 26d5ac9..cfd8479 100755
--- a/frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java
+++ b/frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@@ -51,6 +51,7 @@ import android.os.UserManager;
 import android.os.storage.StorageManager;
 import android.os.storage.StorageVolume;
 import android.provider.Settings;
+import android.text.TextUtils;
 import android.util.Pair;
 import android.util.Slog;
 
@@ -61,8 +62,10 @@ import com.android.internal.os.SomeArgs;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.server.FgThread;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -95,6 +98,10 @@ public class UsbDeviceManager {
      * The non-persistent property which stores the current USB actual state.
      */
     private static final String USB_STATE_PROPERTY = "sys.usb.state";
+    
+    private static final String USB_CAMERA_PROPERTY_PRE = "topband.dev.";
+
+    private static final String USB_CAMERA_CLASS_PATH = "/sys/class/video4linux";
 
     /**
      * ro.bootmode value when phone boots into usual Android.
@@ -215,15 +222,29 @@ public class UsbDeviceManager {
             if (devPath != null && devPath.contains("/devices/platform")) {
                 if ("video4linux".equals(subSystem)) {
                     Intent intent = new Intent(Intent.ACTION_USB_CAMERA);
+
                     String action = event.get("ACTION");
+                    String name = event.get("DEVNAME");
+                    String idProduct = searchAndReadFileBackward("/sys" + devPath, "idProduct");
+                    String idVendor = searchAndReadFileBackward("/sys" + devPath, "idVendor");
+
+                    if (DEBUG) Slog.d(TAG, action + " usb camera: " + name + " [" + idVendor + ":" + idProduct + "]");
                     if ("remove".equals(action)){
                         Slog.d(TAG,"usb camera removed");
                         intent.setFlags(Intent.FLAG_USB_CAMERA_REMOVE);
                         SystemProperties.set("persist.sys.usbcamera.status","remove");
+
+                        if (!name.isEmpty()) {
+                            SystemProperties.set(USB_CAMERA_PROPERTY_PRE + name, "");
+                        }
                     } else if ("add".equals(action)) {
                         Slog.d(TAG,"usb camera added");
                         intent.setFlags(Intent.FLAG_USB_CAMERA_ADD);
                         SystemProperties.set("persist.sys.usbcamera.status","add");
+
+                        if (!name.isEmpty() && !idProduct.isEmpty() && !idVendor.isEmpty()) {
+                            SystemProperties.set(USB_CAMERA_PROPERTY_PRE + name, idVendor + ":" + idProduct);
+                        }
                     }
 
                     int num = android.hardware.Camera.getNumberOfCameras();
@@ -243,6 +264,99 @@ public class UsbDeviceManager {
             }
         }
     };
+    
+    private void initUsbCameraProperty() {
+        File filePath = new File(USB_CAMERA_CLASS_PATH);
+        File[] files = filePath.listFiles();
+        if (null != files) {
+            for (File file : files) {
+                if (file.isDirectory()) {
+                    String idProduct = searchAndReadFileForward(file.getPath() + "/device/input", "product");
+                    String idVendor = searchAndReadFileForward(file.getPath() + "/device/input", "vendor");
+                    if (!TextUtils.isEmpty(idProduct) || !idVendor.isEmpty()) {
+                        if (DEBUG) Slog.v(TAG, "initUsbCameraProperty, add camera property: " + idVendor + ":" + idProduct);
+                        SystemProperties.set(USB_CAMERA_PROPERTY_PRE + file.getName(), idVendor + ":" + idProduct);
+                    }
+                }
+            }
+        }
+    }
+
+    private String searchAndReadFileForward(String filePath, String fileName) {
+        if (TextUtils.isEmpty(filePath) || TextUtils.isEmpty(fileName)) {
+            return "";
+        }
+
+        return searchAndReadFileForward(new File(filePath), fileName);
+    }
+
+    private String searchAndReadFileForward(File filePath, String fileName) {
+        if (null != filePath && !TextUtils.isEmpty(fileName)) {
+            Slog.v(TAG, "searchAndReadFileForward, path: " + filePath.getPath());
+
+            File file = new File(filePath.getPath() + "/" + fileName);
+            if (file.exists()) {
+                return readFileByLines(file.getPath());
+            }
+
+            File[] files = filePath.listFiles();
+            if (null != files) {
+                for (File subfile : files) {
+                    if (subfile.isDirectory()) {
+                        return searchAndReadFileForward(subfile, fileName);
+                    }
+                }
+            }
+        }
+
+        return "";
+    }
+
+    private String searchAndReadFileBackward(String filePath, String fileName) {
+        if (TextUtils.isEmpty(filePath) || TextUtils.isEmpty(fileName)) {
+            return "";
+        }
+
+        return searchAndReadFileBackward(new File(filePath), fileName);
+    }
+
+    private String searchAndReadFileBackward(File filePath, String fileName) {
+        if (null != filePath && !TextUtils.isEmpty(fileName)) {
+            File file = new File(filePath.getPath() + "/" + fileName);
+            if (file.exists()) {
+                return readFileByLines(file.getPath());
+            }
+
+            searchAndReadFileBackward(filePath.getParentFile(), fileName);
+        }
+
+        return "";
+    }
+
+    private static String readFileByLines(String fileName) {
+        File file = new File(fileName);
+        BufferedReader reader = null;
+        StringBuilder builder = new StringBuilder();
+        try {
+            reader = new BufferedReader(new FileReader(file));
+            String tempString;
+            while ((tempString = reader.readLine()) != null) {
+                builder.append(tempString);
+            }
+            reader.close();
+            return builder.toString();
+        } catch (IOException e) {
+            Slog.e(TAG, "readFileByLines, " + e.getMessage());
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException ignored) {
+                }
+            }
+        }
+        return "";
+    }
 
     public UsbDeviceManager(Context context, UsbAlsaManager alsaManager,
             UsbSettingsManager settingsManager) {
@@ -494,7 +608,7 @@ public class UsbDeviceManager {
                             		UsbManager.removeFunction(UsbManager.removeFunction(persisted,
                                     	UsbManager.USB_FUNCTION_MTP), UsbManager.USB_FUNCTION_PTP));
                 	}
		}	
 
                 String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim();
                 updateState(state);
@@ -548,7 +662,7 @@ public class UsbDeviceManager {
             } else if ("CONFIGURED".equals(state)) {
                 connected = 1;
                 configured = 1;
		if ("true".equals(SystemProperties.get("ro.usb.default_mtp")) &&
 		UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MTP))
                 	mUsbDataUnlocked = true;
             } else {
@@ -1030,6 +1144,7 @@ public class UsbDeviceManager {
                     updateUsbNotification(false);
                     updateAdbNotification(false);
                     updateUsbFunctions();
+                    initUsbCameraProperty();
                     break;
                 case MSG_LOCALE_CHANGED:
                     updateAdbNotification(true);

驗(yàn)證
編譯運(yùn)行后,讀取屬性如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-502015.html

[topband.dev.video0]: [0bda:2714]
[topband.dev.video1]: [0bda:b321]

到了這里,關(guān)于【干貨】Android系統(tǒng)定制基礎(chǔ)篇:第十五部分(Android支持鼠標(biāo)右鍵返回、GPIO 控制方案、屬性標(biāo)識(shí)USB攝像頭的VID與PID)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包