通過暗碼去打開/關閉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