通过暗码去打开/关闭usb debug开关

1. Description

通过在dialer输入暗码,打开/关闭usb debug开关。

其实这个功能本来在settings下面就有的,如果是正式版的设备需要连续点击几次版本号才能将usb debug开关显示出来,通过暗码来打开估计是为了更加方便后续的操作吧。

2. Analysis

  1. 首先在dialer处进行逻辑判断,如果接收到的是usb debug开关的暗码,则发送对应的广播。在mtk自带的dialer就有相关的逻辑了,如handleSecretCode方法就会接收*#*#<code>#*#*格式的暗码,然后发送广播,其相关代码如下所示:

    1. static boolean handleSecretCode(Context context, String input) {
    2. // Secret codes are accessed by dialing *#*#<code>#*#*
    3. /// M: add for handle reboot meta secret code @{
    4. if (FK_SUPPORTED.equals(SystemProperties.get(FK_REBOOT_META_SUPPORT))) {
    5. if (handleRebootMetaSecretCode(context, input)) {
    6. return true;
    7. }
    8. }
    9. /// @}
    10. int len = input.length();
    11. if (len <= 8 || !input.startsWith("*#*#") || !input.endsWith("#*#*")) {
    12. return false;
    13. }
    14. String secretCode = input.substring(4, len - 4);
    15. TelephonyManagerCompat.handleSecretCode(context, secretCode);
    16. return true;
    17. }
    18. /*handleSecretCode这个方法在TelephonyManagerCompat.java文件中,它会将输入的暗码以广播的形式发送出去*/
    19. public static void handleSecretCode(Context context, String secretCode) {
    20. // Must use system service on O+ to avoid using broadcasts, which are not allowed on O+.
    21. if (BuildCompat.isAtLeastO()) {
    22. if (!TelecomUtil.isDefaultDialer(context)) {
    23. LogUtil.e(
    24. "TelephonyManagerCompat.handleSecretCode",
    25. "not default dialer, cannot send special code");
    26. return;
    27. }
    28. context.getSystemService(TelephonyManager.class).sendDialerSpecialCode(secretCode);
    29. } else {
    30. // System service call is not supported pre-O, so must use a broadcast for N-.
    31. Intent intent =
    32. new Intent(SECRET_CODE_ACTION, Uri.parse("android_secret_code://" + secretCode));
    33. context.sendBroadcast(intent);
    34. }
    35. }
  2. 在广播接收器中进行对应的逻辑处理:

    usb debug的状态信息是存储在ContentProvider中的,对应的标识:

    1. /**
    2. * Whether ADB is enabled.
    3. */
    4. public static final String ADB_ENABLED = "adb_enabled";

    只要将存储在ContentProvider的状态值拿出来,然后进行判断,如果为0则表示当前usb debug是关闭的,如果为1则表示当前usb debug是打开的。只需要将状态值取反后再存入ContentProvider就可以改变usb debug状态。

3. Solution

  • 添加USB接收器USBDebugBroadcastReceiver,具体代码如下:
  1. package com.android.settings;
  2. import android.content.BroadcastReceiver;
  3. import android.content.ContentResolver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.res.Resources;
  7. import android.provider.Settings;
  8. import android.util.Log;
  9. import android.widget.Toast;
  10. import com.android.internal.telephony.TelephonyIntents;
  11. public class USBDebugBroadcastReceiver extends BroadcastReceiver {
  12. private static final String TAG="USBDebugBroadcastReceiver";
  13. private Toast debugOpenToast;
  14. @Override
  15. public void onReceive(Context context, Intent intent) {
  16. if(TelephonyIntents.SECRET_CODE_ACTION.equals(intent.getAction()) && "0000".equals(intent.getData().getHost())){
  17. boolean mEnableAdb = false;
  18. final ContentResolver mContentResolver = context.getContentResolver();
  19. mEnableAdb = Settings.Global.getInt(mContentResolver,
  20. Settings.Global.ADB_ENABLED, 0) > 0;
  21. Resources resources=context.getResources();
  22. if (debugOpenToast != null) {
  23. debugOpenToast.cancel();
  24. }
  25. if(!mEnableAdb){
  26. // make sure the ADB_ENABLED setting value matches the current state
  27. try {
  28. Settings.Global.putInt(mContentResolver,
  29. Settings.Global.ADB_ENABLED, 1 );
  30. debugOpenToast = Toast.makeText(context,resources.getString(R.string.enable_adb)+" "+resources.getString(R.string.gadget_state_on)
  31. +" "+resources.getString(R.string.band_mode_succeeded),
  32. Toast.LENGTH_SHORT);
  33. debugOpenToast.show();
  34. } catch (SecurityException e) {
  35. // If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't be changed.
  36. Log.d(TAG, "ADB_ENABLED is restricted.");
  37. debugOpenToast = Toast.makeText(context,resources.getString(R.string.enable_adb)+" "+resources.getString(R.string.gadget_state_on)
  38. +" "+resources.getString(R.string.band_mode_failed),
  39. Toast.LENGTH_SHORT);
  40. debugOpenToast.show();
  41. }
  42. }else{
  43. try {
  44. Settings.Global.putInt(mContentResolver,
  45. Settings.Global.ADB_ENABLED, 0 );
  46. debugOpenToast = Toast.makeText(context,resources.getString(R.string.enable_adb)+" "+resources.getString(R.string.gadget_state_off)
  47. +" "+resources.getString(R.string.band_mode_succeeded),
  48. Toast.LENGTH_SHORT);
  49. debugOpenToast.show();
  50. } catch (SecurityException e) {
  51. // If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't be changed.
  52. Log.d(TAG, "ADB_DISENABLED is restricted.");
  53. debugOpenToast = Toast.makeText(context,resources.getString(R.string.enable_adb)+" "+resources.getString(R.string.gadget_state_off)
  54. +" "+resources.getString(R.string.band_mode_failed),
  55. Toast.LENGTH_SHORT);
  56. debugOpenToast.show();
  57. }
  58. }
  59. }
  60. }
  61. }
  • 在对应的AndroidManifest.xml中为该接收器进行注册,具体如下:
  1. <receiver android:name=".USBDebugBroadcastReceiver">
  2. <intent-filter>
  3. <action android:name="android.provider.Telephony.SECRET_CODE"/>
  4. <data android:scheme="android_secret_code" android:host="33284"/>
  5. </intent-filter>
  6. </receiver>

4. Summary

这个问题相对简单,只要将期望的状态只存入对应的ContentProvider中就可与改变usb debug状态。之所以通过广播来处理,是因为与activity相比,通过intent启动指定activity组件时,如果没有找到合适的activity组件,会导致程序异常中止,但是通过intent启动BroadcastReceiver组件时不会有该问题出现。

最新文章

  1. EXCEL处理大量数据的潜在风险
  2. vue.common.js?e881:433 TypeError: Cannot read property &#39;nodeName&#39; of undefined
  3. Eclipse_luna_J2EE_For_JS+tomcat8.0环境搭建、配置、开发入门
  4. .net MVC 中枚举类型Enum 转化成 下拉列表的数据源
  5. crontab执行脚本中文乱码,手动执行没有问题
  6. oracle中清空表数据的两种方法
  7. 解决requestAnimationFrame的兼容问题
  8. 《用户和组的管理》Redhat6.3
  9. Exceptionin thread &quot;main&quot; java.lang.UnsatisfiedLinkError:org.apache.hadoop.util.NativeCrc32.nativeComputeChunkedSumsByteArray(II[BI[BIILjav
  10. 编译android5.0源码的
  11. javascript 动态创建表格
  12. 播放视频的框架Vitamio的使用问题
  13. hibernate---一级缓存, 二级缓存, 查询缓存
  14. Python中的浅拷贝与深拷贝
  15. 解决ubuntu中arm-linux-gcc not found
  16. Nginx入门到精通
  17. 【LeetCode OJ】Add Two Numbers
  18. Flutter TabBar
  19. MFC常用操作
  20. 【翻译】HTML5开发——轻量级Web Database存储库html5sql.js

热门文章

  1. Wireshark基本使用(1)
  2. &lt;转&gt;网络爬虫原理
  3. Git差异并列显示
  4. 谷歌protobuf(protocol-buffers)各种开发语言数据类型转换说明
  5. cmake之if
  6. num_duilib之TabBox用法(21)
  7. c++之升序和降序排序
  8. 第二十四个知识点:描述一个二进制m组的滑动窗口指数算法
  9. InfoGAN
  10. 文件挂载(一)- Linux挂载Linux文件夹