通过暗码去打开/关闭usb debug开关
1. Description
通过在dialer输入暗码,打开/关闭usb debug开关。
其实这个功能本来在settings下面就有的,如果是正式版的设备需要连续点击几次版本号才能将usb debug开关显示出来,通过暗码来打开估计是为了更加方便后续的操作吧。
2. Analysis
首先在dialer处进行逻辑判断,如果接收到的是usb debug开关的暗码,则发送对应的广播。在mtk自带的dialer就有相关的逻辑了,如handleSecretCode方法就会接收
*#*#<code>#*#*
格式的暗码,然后发送广播,其相关代码如下所示:static boolean handleSecretCode(Context context, String input) {
// Secret codes are accessed by dialing *#*#<code>#*#*
/// M: add for handle reboot meta secret code @{
if (FK_SUPPORTED.equals(SystemProperties.get(FK_REBOOT_META_SUPPORT))) {
if (handleRebootMetaSecretCode(context, input)) {
return true;
}
}
/// @}
int len = input.length();
if (len <= 8 || !input.startsWith("*#*#") || !input.endsWith("#*#*")) {
return false;
}
String secretCode = input.substring(4, len - 4);
TelephonyManagerCompat.handleSecretCode(context, secretCode);
return true;
}
/*handleSecretCode这个方法在TelephonyManagerCompat.java文件中,它会将输入的暗码以广播的形式发送出去*/
public static void handleSecretCode(Context context, String secretCode) {
// Must use system service on O+ to avoid using broadcasts, which are not allowed on O+.
if (BuildCompat.isAtLeastO()) {
if (!TelecomUtil.isDefaultDialer(context)) {
LogUtil.e(
"TelephonyManagerCompat.handleSecretCode",
"not default dialer, cannot send special code");
return;
}
context.getSystemService(TelephonyManager.class).sendDialerSpecialCode(secretCode);
} else {
// System service call is not supported pre-O, so must use a broadcast for N-.
Intent intent =
new Intent(SECRET_CODE_ACTION, Uri.parse("android_secret_code://" + secretCode));
context.sendBroadcast(intent);
}
}
在广播接收器中进行对应的逻辑处理:
usb debug的状态信息是存储在ContentProvider中的,对应的标识:/**
* Whether ADB is enabled.
*/
public static final String ADB_ENABLED = "adb_enabled";
只要将存储在ContentProvider的状态值拿出来,然后进行判断,如果为0则表示当前usb debug是关闭的,如果为1则表示当前usb debug是打开的。只需要将状态值取反后再存入ContentProvider就可以改变usb debug状态。
3. Solution
- 添加USB接收器USBDebugBroadcastReceiver,具体代码如下:
package com.android.settings;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import com.android.internal.telephony.TelephonyIntents;
public class USBDebugBroadcastReceiver extends BroadcastReceiver {
private static final String TAG="USBDebugBroadcastReceiver";
private Toast debugOpenToast;
@Override
public void onReceive(Context context, Intent intent) {
if(TelephonyIntents.SECRET_CODE_ACTION.equals(intent.getAction()) && "0000".equals(intent.getData().getHost())){
boolean mEnableAdb = false;
final ContentResolver mContentResolver = context.getContentResolver();
mEnableAdb = Settings.Global.getInt(mContentResolver,
Settings.Global.ADB_ENABLED, 0) > 0;
Resources resources=context.getResources();
if (debugOpenToast != null) {
debugOpenToast.cancel();
}
if(!mEnableAdb){
// make sure the ADB_ENABLED setting value matches the current state
try {
Settings.Global.putInt(mContentResolver,
Settings.Global.ADB_ENABLED, 1 );
debugOpenToast = Toast.makeText(context,resources.getString(R.string.enable_adb)+" "+resources.getString(R.string.gadget_state_on)
+" "+resources.getString(R.string.band_mode_succeeded),
Toast.LENGTH_SHORT);
debugOpenToast.show();
} catch (SecurityException e) {
// If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't be changed.
Log.d(TAG, "ADB_ENABLED is restricted.");
debugOpenToast = Toast.makeText(context,resources.getString(R.string.enable_adb)+" "+resources.getString(R.string.gadget_state_on)
+" "+resources.getString(R.string.band_mode_failed),
Toast.LENGTH_SHORT);
debugOpenToast.show();
}
}else{
try {
Settings.Global.putInt(mContentResolver,
Settings.Global.ADB_ENABLED, 0 );
debugOpenToast = Toast.makeText(context,resources.getString(R.string.enable_adb)+" "+resources.getString(R.string.gadget_state_off)
+" "+resources.getString(R.string.band_mode_succeeded),
Toast.LENGTH_SHORT);
debugOpenToast.show();
} catch (SecurityException e) {
// If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't be changed.
Log.d(TAG, "ADB_DISENABLED is restricted.");
debugOpenToast = Toast.makeText(context,resources.getString(R.string.enable_adb)+" "+resources.getString(R.string.gadget_state_off)
+" "+resources.getString(R.string.band_mode_failed),
Toast.LENGTH_SHORT);
debugOpenToast.show();
}
}
}
}
}
- 在对应的AndroidManifest.xml中为该接收器进行注册,具体如下:
<receiver android:name=".USBDebugBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE"/>
<data android:scheme="android_secret_code" android:host="33284"/>
</intent-filter>
</receiver>
4. Summary
这个问题相对简单,只要将期望的状态只存入对应的ContentProvider中就可与改变usb debug状态。之所以通过广播来处理,是因为与activity相比,通过intent启动指定activity组件时,如果没有找到合适的activity组件,会导致程序异常中止,但是通过intent启动BroadcastReceiver组件时不会有该问题出现。