当前位置:   article > 正文

Android12 禁用adb

Android12 禁用adb

这种做法没有删除任何adb相关的东西,只是设置persist.sys.usb.config中的adb相关属性时添加宏控制。
userdebug版本一样可以禁用

添加宏开关
  • device/mediatek/mt6761/BoardConfig.mk
# for adb
ADB_ENABLE:=false
  • 1
  • 2
添加ro.adb.enabled属性
  • build/make/core/main.mk
# for adb
ifeq (true,$(strip $(ADB_ENABLE)))
  ADDITIONAL_SYSTEM_PROPERTIES += ro.adb.enabled=1
else
  ADDITIONAL_SYSTEM_PROPERTIES += ro.adb.enabled=0
endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
根据ro.adb.enabled设置persist.sys.usb.config
  • system/core/init/property_service.cpp
static uint32_t PropertySet(const std::string& name, const std::string& value, std::string* error) {
	// for adb 
    if (name == "persist.sys.usb.config" /*|| name == "sys.usb.config"*/){
        LOG(INFO) << " name= " << name << " value= " << value;
        std::string adb_value = GetProperty("ro.adb.enabled", "0");
        if (adb_value == "0"){
            if (value.find("adb") != std::string::npos){
                LOG(ERROR) << " ro.adb.enabled is 0, adb is not allowed ";
                return PROP_SUCCESS;
            }
        }
    }
	// end
	
    size_t valuelen = value.size();

    if (!IsLegalPropertyName(name)) {
        *error = "Illegal property name";
        return PROP_ERROR_INVALID_NAME;
    }

    if (auto result = IsLegalPropertyValue(name, value); !result.ok()) {
        *error = result.error().message();
        return PROP_ERROR_INVALID_VALUE;
    }
#ifdef MTK_LOG
    SnapshotPropertyFlowTraceLog("_spf");
#endif
    prop_info* pi = (prop_info*) __system_property_find(name.c_str());
    if (pi != nullptr) {
        // ro.* properties are actually "write-once".
        if (StartsWith(name, "ro.")) {
            *error = "Read-only property was already set";
            return PROP_ERROR_READ_ONLY_PROPERTY;
        }

#ifdef MTK_LOG
        SnapshotPropertyFlowTraceLog("_spu");
#endif
        __system_property_update(pi, value.c_str(), valuelen);
    } else {
#ifdef MTK_LOG
        SnapshotPropertyFlowTraceLog("_spa");
#endif
        int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
        if (rc < 0) {
            *error = "__system_property_add failed";
            return PROP_ERROR_SET_FAILED;
        }
    }

    // Don't write properties to disk until after we have read all default
    // properties to prevent them from being overwritten by default values.
    if (persistent_properties_loaded && StartsWith(name, "persist.")) {
        WritePersistentProperty(name, value);
    }
    // If init hasn't started its main loop, then it won't be handling property changed messages
    // anyway, so there's no need to try to send them.
    auto lock = std::lock_guard{accept_messages_lock};
    if (accept_messages) {
#ifdef MTK_LOG
        SnapshotPropertyFlowTraceLog("SPC");
#endif
        PropertyChanged(name, value);
    }

#ifdef MTK_LOG
    if (GetMTKLOGDISABLERATELIMIT()) // MTK add log
        PropSetLog(name, value, error);
#endif

    return PROP_SUCCESS;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
相关UI及开发者模式控制
  • frameworks/base/core/java/android/os/Build.java
 /*
  * True if Adb is enabled
  */
  public static final boolean IS_ADB_ENABLED =
        SystemProperties.getInt("ro.adb.enabled", 0) == 1;  
        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/SettingsActivity.java
 // True if Adb is enabled
 final boolean showDev = Build.IS_ADB_ENABLED/*DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
              && !Utils.isMonkeyRunning()*/;
 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
                 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
              showDev, isAdmin)
              || somethingChanged;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
 @Override
 public boolean isAvailable() {
 	// for adb
    // True if Adb is enabled
    return Build.IS_ADB_ENABLED/*!Utils.isMonkeyRunning()*/;
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java
 @Override
 public boolean handlePreferenceTreeClick(Preference preference) {
 	 // for adb
     if (preference.getKey().equals("build_number")){
         if (!Build.IS_ADB_ENABLED){
             preference.setSelectable(false);
         }
     }
	 // end
     if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
         return false;
     }
     ...
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

修改完后,连接USB不会显示功能选择列表,无法连点调出开发者模式。

测试

编译完成后,build.prop中显示

ro.adb.enabled=0
... 
# Auto-added by post_process_props.py
persist.sys.usb.config=adb
# end of file
  • 1
  • 2
  • 3
  • 4
  • 5

命令窗口显示

C:\Users\dell>adb devices
List of devices attached


C:\Users\dell>
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/861347
推荐阅读
相关标签
  

闽ICP备14008679号