package jp.example.tethering; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; /** * Android sends android.hardware.usb.action.USB_STATE with EXTRA_CONNECTED * when the USB cable is plugged/unplugged. Start tethering automatically. */ public class UsbReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { boolean connected = intent.getBooleanExtra("connected", false); if (!connected) { return; } SharedPreferences prefs = context.getSharedPreferences("tethering", Context.MODE_PRIVATE); if (!prefs.getBoolean("auto_usb", true)) { return; } if (prefs.getBoolean("manual_off", false)) { return; } final PendingResult pending = goAsync(); new Thread(new Runnable() { @Override public void run() { try { // Give the RNDIS gadget a moment to appear. Thread.sleep(2500); String apk = context.getPackageCodePath(); RootRelay.run(RootRelay.acceptMobileData() + "pm disable-user --user 0 jp.softbank.mb.tetherAuthNfp >/dev/null 2>&1\n" + "am force-stop jp.softbank.mb.tetherAuthNfp\n" + "CLASSPATH=" + apk + " /system/bin/app_process /system/bin " + "jp.example.tethering.TetherCli usb true\n"); } catch (InterruptedException ignored) { } finally { pending.finish(); } } }).start(); } }