安卓源码环境屏蔽USB摄像头默认启动应用、摄像头应用权限弹出层

  • 问题分析:
    每次插入usb摄像头时,都会弹出两个提示框:
    要允许xxx应用访问xxx设备吗?
    要打开xxx应用来处理xxx设备吗?

根据系统UI提示信息判断是哪个源码文件的问题

#进入提示页面查看是哪两个提示框
cd /home/yihao/alps-release-p0.mp1-V5/frameworks/base/packages/SystemUI/res/values-zh-rCN/

最后找到是UsbPermissionActivity.java和UsbConfirmActivity.java文件,使用命令:

jgrep UsbPermissionActivity

稍等几分钟,搜索结果:(注意XY6765开发板需要改vendor目录下的源码)

yihao@yihao-PowerEdge-R720:~/alps-release-p0.mp1-V5$ jgrep UsbPermissionActivity
./frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java:55:public class UsbPermissionActivity extends AlertActivity
./frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java:58:    private static final String TAG = "UsbPermissionActivity";
./frameworks/base/services/usb/java/com/android/server/usb/UsbUserSettingsManager.java:220:                "com.android.systemui.usb.UsbPermissionActivity");
./frameworks/base/services/usb/java/com/android/server/usb/UsbUserSettingsManager.java:228:            Slog.e(TAG, "unable to start UsbPermissionActivity");
./frameworks/base/services/usb/java/com/android/server/usb/UsbUserSettingsManager.java:261:        // start UsbPermissionActivity so user can choose an activity
./vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java:55:public class UsbPermissionActivity extends AlertActivity
./vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java:58:    private static final String TAG = "UsbPermissionActivity";

更改UsbPermissionActivity.java代码:

#按包名放行
if(!mPackageName.contains("usbcameratest8")&&!mPackageName.contains("excelsior")){
            setupAlert();
        } else{
            mPermissionGranted=true;
            finish();
        }


#或者如下,全部放行
//setupAlert();//不使用窗口
 mPermissionGranted = true;
 finish();

更改UsbConfirmActivity.java代码:

@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);
        mResolveInfo = (ResolveInfo) intent.getParcelableExtra("rinfo");

        PackageManager packageManager = getPackageManager();
        String appName = mResolveInfo.loadLabel(packageManager).toString();

        final AlertController.AlertParams ap = mAlertParams;
        ap.mTitle = appName;
        if (mDevice == null) {
            ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName,
                    mAccessory.getDescription());
            mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
        } else {
            ap.mMessage = getString(R.string.usb_device_confirm_prompt, appName,
                    mDevice.getProductName());
            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(getString(R.string.always_use_accessory, appName,
                    mAccessory.getDescription()));
        } else {
            mAlwaysUse.setText(getString(R.string.always_use_device, appName,
                    mDevice.getProductName()));
        }
        mAlwaysUse.setOnCheckedChangeListener(this);
        mClearDefaultHint = (TextView)ap.mView.findViewById(
                com.android.internal.R.id.clearDefaultHint);
        mClearDefaultHint.setVisibility(View.GONE);


        /****@zxw 更改部分开始,屏蔽确认层******/
//        setupAlert();
        try {
            IBinder b = ServiceManager.getService(USB_SERVICE);
            IUsbManager service = IUsbManager.Stub.asInterface(b);
            final int uid = mResolveInfo.activityInfo.applicationInfo.uid;
            final int userId = UserHandle.myUserId();
//            boolean alwaysUse = mAlwaysUse.isChecked();
            boolean alwaysUse = true; //默认一直使用
//            Intent intent = null;

            if (mDevice != null) {
//                intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
//                intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);

                // grant permission for the device
                service.grantDevicePermission(mDevice, uid);
                // set or clear default setting
                if (alwaysUse) {
                    service.setDevicePackage(
                            mDevice, mResolveInfo.activityInfo.packageName, userId);
                } else {
                    service.setDevicePackage(mDevice, null, userId);
                }
            } else if (mAccessory != null) {
//                intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
//                intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);

                // grant permission for the accessory
                service.grantAccessoryPermission(mAccessory, uid);
                // set or clear default setting
                if (alwaysUse) {
                    service.setAccessoryPackage(
                            mAccessory, mResolveInfo.activityInfo.packageName, userId);
                } else {
                    service.setAccessoryPackage(mAccessory, null, userId);
                }
            }

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(
                    new ComponentName(mResolveInfo.activityInfo.packageName,
                            mResolveInfo.activityInfo.name));
            startActivityAsUser(intent, new UserHandle(userId));
        } catch (Exception e) {
            Log.e(TAG, "Unable to start activity", e);
        }

        finish();
        /****@zxw 更改部分结束,屏蔽确认层******/
    }

执行编译命令

make -j24 2>&1 | tee build.log 

更改完代码后,执行刷机命令:

$adb reboot bootloader
$fastboot flash system system.img #烧写system镜像到system分区
#烧写全部内容
$ fastboot flashall -w  //这个 -w 是为了wipes the /data partition擦除/data分区
$fastboot reboot

附录

中间修改代码后有报错,可以通过该提示找到编译后的jar包位置

[ 30% 32/104] Target Java: out/target/common/obj/APPS/MtkSystemUI_intermediates/classes-full-debug.jar

参考文章

https://blog.csdn.net/hubbybob1/article/details/50263925

你可能感兴趣的:(安卓源码环境屏蔽USB摄像头默认启动应用、摄像头应用权限弹出层)