欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android usb设备权限查询及自动获取详解流程

程序员文章站 2022-07-06 12:59:46
看到当上面的对话框弹出时,可以使用命令查看顶层的活动窗口adb shell dumpsys window | findstr mcurrentfocusmcurrentfocus=window{41a...

Android usb设备权限查询及自动获取详解流程

看到当上面的对话框弹出时,可以使用命令查看顶层的活动窗口

adb shell dumpsys window | findstr mcurrentfocus
mcurrentfocus=window{41ab0ee0 u0 com.android.systemui/com.android.systemui.usb.usbpermissionactivity}

Android usb设备权限查询及自动获取详解流程

这就是应用的位置,当然我们也可以是用grep命令来查找这个对话框的.xml文件,进入android源码然后输入命令:

grep '默认情况下用于' ./ -rn
./systemui/res/values-zh-rcn/strings.xml:51:    <string name="always_use_device" msgid="1450287437017315906">"默认情况下用于该 usb 设备"</string>
./systemui/res/values-zh-rcn/strings.xml:52:    <string name="always_use_accessory" msgid="1210954576979621596">"默认情况下用于该 usb 配件"</string>

那么这个对话框的路径在

/android/frameworks/base/packages/systemui/res/values-zh-rcn/strings.xml

其相关的内容如下:

<string name="always_use_device" msgid="1450287437017315906">"默认情况下用于该 usb 设备"</string>

<string name="usb_device_permission_prompt" msgid="834698001271562057">"允许应用“<xliff:g id="application">%1$s</xliff:g>”访问该 usb 设备吗?"

相关应用路径找到.java文件来修改,其位置在:

/android/frameworks/base/packages/systemui/src/com/android/systemui/usb/usbpermissionactivity.java

这就是那个讨厌的对话框对应的java程序,那么来看看这个程序如下:

/*
 * 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 and
 * limitations under the license.
 */

package com.android.systemui.usb;

import android.app.alertdialog;
import android.app.pendingintent;
import android.content.context;
import android.content.dialoginterface;
import android.content.intent;
import android.content.pm.applicationinfo;
import android.content.pm.packagemanager;
import android.hardware.usb.iusbmanager;
import android.hardware.usb.usbaccessory;
import android.hardware.usb.usbdevice;
import android.hardware.usb.usbmanager;
import android.os.bundle;
import android.os.ibinder;
import android.os.remoteexception;
import android.os.servicemanager;
import android.os.userhandle;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.widget.checkbox;
import android.widget.compoundbutton;
import android.widget.textview;

import com.android.internal.app.alertactivity;
import com.android.internal.app.alertcontroller;
import com.android.systemui.r;

public class usbpermissionactivity extends alertactivity
        implements dialoginterface.onclicklistener, checkbox.oncheckedchangelistener {

    private static final string tag = "usbpermissionactivity";

    private checkbox malwaysuse;
    private textview mcleardefaulthint;
    private usbdevice mdevice;
    private usbaccessory maccessory;
    private pendingintent mpendingintent;
    private string mpackagename;
    private int muid;
    private boolean mpermissiongranted;
    private usbdisconnectedreceiver mdisconnectedreceiver;

    @override
    public void oncreate(bundle icicle) {
        super.oncreate(icicle);

       intent intent = getintent();
        mdevice = (usbdevice)intent.getparcelableextra(usbmanager.extra_device);
        maccessory = (usbaccessory)intent.getparcelableextra(usbmanager.extra_accessory);
        mpendingintent = (pendingintent)intent.getparcelableextra(intent.extra_intent);
        muid = intent.getintextra(intent.extra_uid, -1);
        mpackagename = intent.getstringextra("package");
        log.e(tag, "mpackagename   "+ mpackagename);
		log.e(tag, "muid   "+ muid);
        packagemanager packagemanager = getpackagemanager();
        applicationinfo ainfo;
        try {
            ainfo = packagemanager.getapplicationinfo(mpackagename, 0);
        } catch (packagemanager.namenotfoundexception e) {
            log.e(tag, "unable to look up package name", e);
            finish();
            return;
        }
        string appname = ainfo.loadlabel(packagemanager).tostring();
        log.e(tag, "appname   "+ appname);
        final alertcontroller.alertparams ap = malertparams;
        ap.micon = ainfo.loadicon(packagemanager);
        ap.mtitle = appname;
        if (mdevice == null) {
            ap.mmessage = getstring(r.string.usb_accessory_permission_prompt, appname);
            mdisconnectedreceiver = new usbdisconnectedreceiver(this, maccessory);
        } else {
            ap.mmessage = getstring(r.string.usb_device_permission_prompt, appname);
            mdisconnectedreceiver = new usbdisconnectedreceiver(this, mdevice);
        }
        ap.mpositivebuttontext = getstring(android.r.string.ok);
        ap.mnegativebuttontext = getstring(android.r.string.cancel);
        ap.mpositivebuttonlistener = this;
        ap.mnegativebuttonlistener = this;

        // add "always use" checkbox
        layoutinflater inflater = (layoutinflater)getsystemservice(
                context.layout_inflater_service);
        ap.mview = inflater.inflate(com.android.internal.r.layout.always_use_checkbox, null);
        malwaysuse = (checkbox)ap.mview.findviewbyid(com.android.internal.r.id.alwaysuse);
        if (mdevice == null) {
            malwaysuse.settext(r.string.always_use_accessory);
        } else {
            malwaysuse.settext(r.string.always_use_device);
        }
        malwaysuse.setoncheckedchangelistener(this);
        mcleardefaulthint = (textview)ap.mview.findviewbyid(
                                                    com.android.internal.r.id.cleardefaulthint);
        mcleardefaulthint.setvisibility(view.gone);

        if(!mpackagename.equals("com.zqc.usbcamera"))
			setupalert();
		else 
		{
			mpermissiongranted = true;
			finish();
		}

    }

    @override
    public void ondestroy() {
        ibinder b = servicemanager.getservice(usb_service);
        iusbmanager service = iusbmanager.stub.asinterface(b);

        // send response via pending intent
        intent intent = new intent();
        try {
            if (mdevice != null) {
                intent.putextra(usbmanager.extra_device, mdevice);
                if (mpermissiongranted) {
                    service.grantdevicepermission(mdevice, muid);
                    if (malwaysuse.ischecked()) {
                        final int userid = userhandle.getuserid(muid);
                        service.setdevicepackage(mdevice, mpackagename, userid);
                    }
                }
            }
            if (maccessory != null) {
                intent.putextra(usbmanager.extra_accessory, maccessory);
                if (mpermissiongranted) {
                    service.grantaccessorypermission(maccessory, muid);
                    if (malwaysuse.ischecked()) {
                        final int userid = userhandle.getuserid(muid);
                        service.setaccessorypackage(maccessory, mpackagename, userid);
                    }
                }
            }
            intent.putextra(usbmanager.extra_permission_granted, mpermissiongranted);
            mpendingintent.send(this, 0, intent);
        } catch (pendingintent.canceledexception e) {
            log.w(tag, "pendingintent was cancelled");
        } catch (remoteexception e) {
            log.e(tag, "iusbservice connection failed", e);
        }

        if (mdisconnectedreceiver != null) {
            unregisterreceiver(mdisconnectedreceiver);
        }
        super.ondestroy();
    }

    public void onclick(dialoginterface dialog, int which) {
        if (which == alertdialog.button_positive) {
            mpermissiongranted = true;
        }
        finish();
    }

    public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
        if (mcleardefaulthint == null) return;

        if(ischecked) {
            mcleardefaulthint.setvisibility(view.visible);
        } else {
            mcleardefaulthint.setvisibility(view.gone);
        }
    }
}

这段代码是获取应用的包名,我们可以通过包名对比来决定是否弹出对话框。

final alertcontroller.alertparams ap = malertparams;
        ap.micon = ainfo.loadicon(packagemanager);
        ap.mtitle = appname;
        if (mdevice == null) {
            ap.mmessage = getstring(r.string.usb_accessory_permission_prompt, appname);
            mdisconnectedreceiver = new usbdisconnectedreceiver(this, maccessory);
        } else {
            ap.mmessage = getstring(r.string.usb_device_permission_prompt, appname);
            mdisconnectedreceiver = new usbdisconnectedreceiver(this, mdevice);
        }
        ap.mpositivebuttontext = getstring(android.r.string.ok);
        ap.mnegativebuttontext = getstring(android.r.string.cancel);
        ap.mpositivebuttonlistener = this;
        ap.mnegativebuttonlistener = this;

        // add "always use" checkbox
        layoutinflater inflater = (layoutinflater)getsystemservice(
                context.layout_inflater_service);
        ap.mview = inflater.inflate(com.android.internal.r.layout.always_use_checkbox, null);
        malwaysuse = (checkbox)ap.mview.findviewbyid(com.android.internal.r.id.alwaysuse);
        if (mdevice == null) {
            malwaysuse.settext(r.string.always_use_accessory);
        } else {
            malwaysuse.settext(r.string.always_use_device);
        }
        malwaysuse.setoncheckedchangelistener(this);
        mcleardefaulthint = (textview)ap.mview.findviewbyid(
                                                    com.android.internal.r.id.cleardefaulthint);
        mcleardefaulthint.setvisibility(view.gone);

在上面的代码就是对话框了,接下来就是要修该的东西了

setupalert();

如果不需要显示对话框就可以这样修改,根据包名来判断该应用是否弹出

if(!mpackagename.equals("包名"))
	setupalert();
else {
	mpermissiongranted = true;
	finish();
}

最终给该应用授权usb设备权限的代码,是通过调用grantdevicepermission()和grantaccessorypermission()方法给该应用授权usb设备权限

 @override
    public void ondestroy() {
        ibinder b = servicemanager.getservice(usb_service);
        iusbmanager service = iusbmanager.stub.asinterface(b);

        // send response via pending intent
        intent intent = new intent();
        try {
            if (mdevice != null) {
                intent.putextra(usbmanager.extra_device, mdevice);
                if (mpermissiongranted) {
                    service.grantdevicepermission(mdevice, muid);
                    if (malwaysuse.ischecked()) {
                        final int userid = userhandle.getuserid(muid);
                        service.setdevicepackage(mdevice, mpackagename, userid);
                    }
                }
            }
            if (maccessory != null) {
                intent.putextra(usbmanager.extra_accessory, maccessory);
                if (mpermissiongranted) {
                    service.grantaccessorypermission(maccessory, muid);
                    if (malwaysuse.ischecked()) {
                        final int userid = userhandle.getuserid(muid);
                        service.setaccessorypackage(maccessory, mpackagename, userid);
                    }
                }
            }
            intent.putextra(usbmanager.extra_permission_granted, mpermissiongranted);
            mpendingintent.send(this, 0, intent);
        } catch (pendingintent.canceledexception e) {
            log.w(tag, "pendingintent was cancelled");
        } catch (remoteexception e) {
            log.e(tag, "iusbservice connection failed", e);
        }

        if (mdisconnectedreceiver != null) {
            unregisterreceiver(mdisconnectedreceiver);
        }
        super.ondestroy();
    }

日志打印

c:\users\android1>adb shell logcat -s usbpermissionactivity
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
e/usbpermissionactivity( 708): mpackagename com.test.usbcamera
e/usbpermissionactivity( 708): muid 10018
e/usbpermissionactivity( 708): appname usbcamera
e/usbpermissionactivity( 708): ondestroy() grantdevicepermission false
e/usbpermissionactivity( 708): mpackagename com.zqc.usbcamera
e/usbpermissionactivity( 708): muid 10019
e/usbpermissionactivity( 708): appname usbcamera
e/usbpermissionactivity( 708): ondestroy() grantdevicepermission false

到此这篇关于android usb设备权限查询及自动获取详解流程的文章就介绍到这了,更多相关android usb设备权限 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!