Android8.1 出厂前默认开启USB调试且自动授权,恢复出厂关闭USB调试方案

前言

设备出厂前需要进行全检,需要用到 USB 调试,为了提高效率需修改系统为插入 USB 默认可直接调试,

不弹出授权框。发到客户手里则需要手动连续点击版本号才可开启调试且需要授权框。

修改方案

1、预装 UsbNova app,恢复出厂可卸载,用于区分是否出厂标志

2、在 Provision 中根据出厂标志是否开启 USB 调试

3、在 SystemUI 中根据出厂标志是否默认 USB 授权

修改文件清单

	modified:   build/make/target/product/core.mk
	modified:   frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
	modified:   packages/apps/Provision/src/com/android/provision/DefaultActivity.java
	new file:   packages/apps/UsbNova/Android.mk
	new file:   packages/apps/UsbNova/UsbNova.apk
	modified:   vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java

1、预装 UsbNova app,恢复出厂可卸载,用于区分是否出厂标志

新增 packages/apps/UsbNova,放置一个包名为 com.testusb.launcher 的空 app,在 core.mk 中增加配置

去除 PackageManagerService.java 中最新的 patch,导致 data/app 目录下的 apk 不预装

注释 else 语句块的抛出异常
frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

@@ -11668,11 +11668,12 @@ public class PackageManagerService extends IPackageManager.Stub
                                     + " but expected at " + known.codePathString
                                     + "; ignoring.");
                         }
-                    } else {
+                    }
+                    /* else {
                         throw new PackageManagerException(INSTALL_FAILED_INVALID_INSTALL_LOCATION,
                                 "Application package " + pkg.packageName
                                 + " not found; ignoring.");
-                    }
+                    }*/
                 }
             }

Android.mk 指定输出路径为 TARGET_OUT_DATA_APPS

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Module name should match apk name to be installed
LOCAL_MODULE := UsbNova
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := PRESIGNED
LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
include $(BUILD_PREBUILT)

2、在 Provision 中根据出厂标志是否开启 USB 调试

Provision 在烧写完和恢复出厂时仅执行一次,所以选在这里开启 USB 调试,而且优先于其它 app 启动

通过判断我们刚刚添加的 UsbNova 包名,app 存在则为出厂前,app 不存在则已出厂,

因为出厂前会执行恢复出厂操作,会自动卸载 UsbNova

packages/apps/Provision/src/com/android/provision/DefaultActivity.java

package com.android.provision;

import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;
import android.content.Context;

/**
 * Application that sets the provisioned bit, like SetupWizard does.
 */
public class DefaultActivity extends Activity {

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        // Add a persistent setting to allow other apps to know the device has been provisioned.
        Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1);
        Settings.Secure.putInt(getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, 1);
        
		//20190823 cczheng add default open adb model
        if(checkPackage("com.testusb.launcher", this)){
            Settings.Global.putInt(getContentResolver(), Settings.Global.ADB_ENABLED, 1);
            Settings.Global.putInt(getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
        }

        // remove this activity from the package manager.
        PackageManager pm = getPackageManager();
        ComponentName name = new ComponentName(this, DefaultActivity.class);
        pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

       
        try{
            Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 156);  
        }catch (Exception localException){
            localException.printStackTrace();
        }

        // terminate the activity.
        finish();
    }
	
	//20190823 cczheng add default open adb model
    public boolean checkPackage(String packageName,Context context){ 
        if (packageName == null || "".equals(packageName)) 
            return false; 
        try { 
            context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES); 
            return true; 
        }catch (Exception e){ 
             e.printStackTrace();
             return false; 
        } 
    }
}


3、在 SystemUI 中根据出厂标志是否默认 USB 授权

同样通过判断 UsbNova 包名

vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java


private class UsbDisconnectedReceiver extends BroadcastReceiver {
        private final Activity mActivity;
        public UsbDisconnectedReceiver(Activity activity) {
            mActivity = activity;
        }

        @Override
        public void onReceive(Context content, Intent intent) {
            String action = intent.getAction();
            if (!UsbManager.ACTION_USB_STATE.equals(action)) {
                return;
            }
            /*boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
            if (!connected) {
                mActivity.finish();
            }*/
            //20190823 cczheng add for default grant usb debug when usbnova app exist S
            boolean connected =  checkPackage("com.testusb.launcher", content);
            Log.e(TAG,"UsbDisconnectedReceiver connected="+connected);
            if (connected) {
                mActivity.finish();
                try {
                    IBinder b = ServiceManager.getService(USB_SERVICE);
                    IUsbManager service = IUsbManager.Stub.asInterface(b);
                    service.allowUsbDebugging(true, mKey);
                } catch (Exception e) {
                    Log.e(TAG, "Unable to notify Usb service", e);
                }
            }
            //E
        }
    }

    public boolean checkPackage(String packageName,Context context){ 
        if (packageName == null || "".equals(packageName)) 
            return false; 
        try { 
            context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES); 
            return true; 
        }catch (Exception e){ 
             e.printStackTrace();
             return false; 
        } 
    }

	....

你可能感兴趣的:(Android8.1,源码修改)