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


通过暗码去打开/关闭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组件时不会有该问题出现。






免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM