80,471
社区成员




public class UsbReceiver extends BroadcastReceiver {
private static final String TAG = "UsbReceiver";
private static final String ASK_ON_PLUG = "ask_on_plug";
private static final String sInecm = SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE, "false");
private StorageManager mStorageManager = null;
private UsbManager mUsbManager;
private static boolean mMeidaMountStatus;
private SharedPreferences mSharedPre;
private SharedPreferences.Editor mEeditor;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
mUsbManager = (UsbManager)context.getSystemService(Context.USB_SERVICE);
mSharedPre = context.getSharedPreferences("com.android.settings_preferences", 0);
mEeditor = mSharedPre.edit();
if (UsbManager.ACTION_USB_STATE.equals(action) ){
onUsbStateReceive(context, intent);
} else if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
onMediaMountedReceive(context, intent);
}
}
private void onMediaMountedReceive(Context context, Intent intent) {
boolean usb_connected = mSharedPre.getBoolean("usbState", false);
if(usb_connected && context.getResources().getBoolean(R.bool.usb_mass_storage_mode_enable)
&& UsbManager.USB_FUNCTION_MASS_STORAGE.equals(mUsbManager.getDefaultFunction())) {
popUsbDialog(context);
} else {
return;//do nothing
}
}
private void popUsbDialog(Context context) {
boolean usb_connected = mSharedPre.getBoolean("usbState", false);
String sdcardState = Environment.getSDCardStorageState();
if (usb_connected
&& (sdcardState.equals(Environment.MEDIA_MOUNTED) || sdcardState.equals(Environment.MEDIA_MOUNTED_READ_ONLY))
&& UsbManager.USB_FUNCTION_MASS_STORAGE.equals(mUsbManager.getDefaultFunction())
&& !mStorageManager.isUsbMassStorageEnabled()
&& (Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 0) == 1)
&& context.getResources().getBoolean(R.bool.usb_mass_storage_mode_enable) ) {
Intent i = new Intent();
i.setClassName(context, "com.android.settings.MassStorageDialogActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
private void onUsbStateReceive(Context context, Intent intent) {
boolean usb_connected = intent.getBooleanExtra(UsbManager.USB_CONFIGURED, false);
final ContentResolver cr = context.getContentResolver();
mEeditor.putBoolean("usbState", usb_connected);
mEeditor.commit();
boolean changed = false;
boolean preState = mSharedPre.getBoolean("preState", false);
String progress = SystemProperties.get("vold.encrypt_progress");
if ((progress != null) && !progress.isEmpty()) {
return;
}
if (usb_connected != preState) {
changed = true;
}
KeyguardManager kgm =
(KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (sInecm.equals("false")
&& usb_connected && changed && !preState
&& (1 == Settings.System.getInt(cr, UsbReceiver.ASK_ON_PLUG, 1))
&& !kgm.inKeyguardRestrictedInputMode()) {
mEeditor.putBoolean("preState", true);
mEeditor.commit();
//注意这里,我这里处理的方式是当USB连接上了android设备,我就跳转到设置的UsbSettings界面,LZ可以参考我的代码自己来修改
context.startActivity(Intent.makeRestartActivityTask(
new ComponentName("com.android.settings","com.android.settings.UsbSettings")));
} else if (!usb_connected) {
mEeditor.putBoolean("preState", false);
mEeditor.commit();
}
String usbType = SystemProperties.get("persist.sys.usb.config", "");
if (usbType != null) {
UsbSettings.writeSuitestate(mUsbManager.getDefaultFunction());
}
if (UsbManager.USB_FUNCTION_MASS_STORAGE.equals(mUsbManager.getDefaultFunction())
&& context.getResources().getBoolean(R.bool.usb_mass_storage_mode_enable)) {
if (usb_connected){
popUsbDialog(context);
} else {
if (mStorageManager.isUsbMassStorageEnabled()) {
mStorageManager.disableUsbMassStorage();
}
}
}
}
}
if(BuildConfig.DEBUG)
难道是说这句?