summaryrefslogtreecommitdiff
path: root/src/jp/example/tethering/MainActivity.java
diff options
context:
space:
mode:
authorroot <root@dangofactory.local>2026-11-23 10:20:49 +0900
committerroot <root@dangofactory.local>2026-11-23 10:20:49 +0900
commita67bdcc65a165d1da57ae6d6fead84a1d2e5b248 (patch)
treed0075dacac62d509a09974c1cd2f18ca72cf0e39 /src/jp/example/tethering/MainActivity.java
downloadtethering-app-main.tar.gz
tethering-app-main.zip
Import tethering-appHEADmain
Diffstat (limited to 'src/jp/example/tethering/MainActivity.java')
-rw-r--r--src/jp/example/tethering/MainActivity.java253
1 files changed, 253 insertions, 0 deletions
diff --git a/src/jp/example/tethering/MainActivity.java b/src/jp/example/tethering/MainActivity.java
new file mode 100644
index 0000000..4bf9a18
--- /dev/null
+++ b/src/jp/example/tethering/MainActivity.java
@@ -0,0 +1,253 @@
+package jp.example.tethering;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.SharedPreferences;
+import android.graphics.Color;
+import java.net.NetworkInterface;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
+import android.view.Gravity;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.Button;
+import android.widget.CheckBox;
+import android.widget.CompoundButton;
+import android.widget.LinearLayout;
+import android.widget.ScrollView;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.InterfaceAddress;
+
+public class MainActivity extends Activity {
+ private static final String PREFS = "tethering";
+ private static final String KEY_AUTO = "auto_usb";
+ private static final String KEY_MANUAL_OFF = "manual_off";
+
+ private TextView status;
+ private Button toggle;
+ private CheckBox auto;
+ private SharedPreferences prefs;
+ private final Handler handler = new Handler(Looper.getMainLooper());
+ private boolean lastOn = false;
+ private boolean busy = false;
+
+ private final Runnable refresh = new Runnable() {
+ @Override
+ public void run() {
+ refreshStatus();
+ handler.postDelayed(this, 3000);
+ }
+ };
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
+
+ ScrollView scroll = new ScrollView(this);
+ LinearLayout root = new LinearLayout(this);
+ root.setOrientation(LinearLayout.VERTICAL);
+ int p = dp(20);
+ root.setPadding(p, p, p, p);
+ scroll.addView(root);
+
+ TextView title = new TextView(this);
+ title.setText("テザリング");
+ title.setTextSize(28);
+ title.setGravity(Gravity.CENTER_HORIZONTAL);
+ root.addView(title);
+
+ status = new TextView(this);
+ status.setTextSize(18);
+ status.setPadding(0, dp(16), 0, dp(16));
+ status.setGravity(Gravity.CENTER_HORIZONTAL);
+ root.addView(status);
+
+ toggle = new Button(this);
+ toggle.setTextSize(22);
+ toggle.setFocusable(true);
+ toggle.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ setUsb(!lastOn);
+ }
+ });
+ root.addView(toggle, new LinearLayout.LayoutParams(
+ ViewGroup.LayoutParams.MATCH_PARENT, dp(72)));
+
+ auto = new CheckBox(this);
+ auto.setText("USB接続時に自動でON");
+ auto.setTextSize(16);
+ auto.setFocusable(true);
+ auto.setChecked(prefs.getBoolean(KEY_AUTO, true));
+ auto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ prefs.edit().putBoolean(KEY_AUTO, isChecked).apply();
+ if (isChecked) {
+ prefs.edit().putBoolean(KEY_MANUAL_OFF, false).apply();
+ }
+ }
+ });
+ root.addView(auto);
+
+ Button export = new Button(this);
+ export.setText("PC用スクリプトを書き出す");
+ export.setTextSize(16);
+ export.setFocusable(true);
+ export.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ exportScripts();
+ }
+ });
+ root.addView(export);
+
+ TextView info = new TextView(this);
+ info.setTextSize(14);
+ info.setPadding(0, dp(20), 0, 0);
+ info.setText("PC側は初回だけ同梱スクリプトを管理者で実行してください。\n"
+ + "以後はUSBを挿してアプリがONになれば、PCはDHCPで自動接続します。\n"
+ + "PowerShell / Linux / FreeBSD 用は\n"
+ + "「PC用スクリプトを書き出す」で /data/local/tmp に取り出せます。\n\n"
+ + "rootサービス: " + (RootRelay.available() ? "OK" : "見つかりません"));
+ root.addView(info);
+
+ setContentView(scroll);
+ toggle.requestFocus();
+ RootRelay.run(RootRelay.acceptMobileData());
+ refreshStatus();
+
+ if (prefs.getBoolean(KEY_AUTO, true) && !prefs.getBoolean(KEY_MANUAL_OFF, false)) {
+ handler.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ setUsb(true);
+ }
+ }, 1200);
+ }
+ }
+
+ private int dp(int v) {
+ return (int) (v * getResources().getDisplayMetrics().density + 0.5f);
+ }
+
+ private void exportScripts() {
+ try {
+ String dir = Ps1Exporter.exportAll(this);
+ new AlertDialog.Builder(this)
+ .setTitle("PC用スクリプトを書き出しました")
+ .setMessage("スマホ内: " + dir + "\n"
+ + "pc-autoconnect.ps1\n"
+ + "pc-autoconnect.sh\n"
+ + "pc-autoconnect-linux.sh\n"
+ + "pc-autoconnect-freebsd.sh\n\n"
+ + "PC側で:\n"
+ + "adb pull " + dir + "pc-autoconnect.ps1 R:\\Projects\\Tethering-pc\\\n"
+ + "adb pull " + dir + "pc-autoconnect.sh R:\\Projects\\Tethering-pc\\\n"
+ + "adb pull " + dir + "pc-autoconnect-linux.sh R:\\Projects\\Tethering-pc\\\n"
+ + "adb pull " + dir + "pc-autoconnect-freebsd.sh R:\\Projects\\Tethering-pc\\")
+ .setPositiveButton("OK", null)
+ .show();
+ } catch (Exception e) {
+ Toast.makeText(this, "書き出し失敗: " + e.getMessage(), Toast.LENGTH_LONG).show();
+ }
+ }
+
+ private void setUsb(boolean enable) {
+ if (busy) {
+ return;
+ }
+ busy = true;
+ prefs.edit().putBoolean(KEY_MANUAL_OFF, !enable).apply();
+ status.setText(enable ? "ON にしています..." : "OFF にしています...");
+
+ StringBuilder cmd = new StringBuilder();
+ if (enable) {
+ cmd.append(RootRelay.acceptMobileData());
+ // SoftBank's tetherAuthAPLNfp stops tethering again unless disabled.
+ cmd.append("pm disable-user --user 0 jp.softbank.mb.tetherAuthNfp >/dev/null 2>&1\n");
+ cmd.append("am force-stop jp.softbank.mb.tetherAuthNfp\n");
+ }
+ cmd.append("CLASSPATH=").append(getPackageCodePath())
+ .append(" /system/bin/app_process /system/bin ")
+ .append("jp.example.tethering.TetherCli usb ")
+ .append(enable).append("\n");
+
+ if (!RootRelay.run(cmd.toString())) {
+ status.setText("rootサービスに書き込めません");
+ busy = false;
+ return;
+ }
+
+ handler.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ busy = false;
+ refreshStatus();
+ }
+ }, 2500);
+ }
+
+ private void refreshStatus() {
+ boolean on = isUsbTethered();
+ lastOn = on;
+ String ip = usbIp();
+ android.util.Log.d("TetheringApp", "refreshStatus on=" + on + " ip=" + ip);
+ toggle.setText(on ? "USBテザリングをOFF" : "USBテザリングをON");
+ status.setText(on
+ ? "USBテザリング: ON" + (ip != null ? "\n" + ip : "")
+ : "USBテザリング: OFF");
+ status.setTextColor(on ? Color.rgb(0, 128, 0) : Color.rgb(180, 0, 0));
+ }
+
+ private boolean isUsbTethered() {
+ try {
+ NetworkInterface ni = NetworkInterface.getByName("usb0");
+ if (ni == null || !ni.isUp()) {
+ return false;
+ }
+ for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
+ if (ia.getAddress() instanceof Inet4Address) {
+ return true;
+ }
+ }
+ } catch (Exception ignored) {
+ }
+ return false;
+ }
+
+ private String usbIp() {
+ try {
+ NetworkInterface ni = NetworkInterface.getByName("usb0");
+ if (ni == null) {
+ return null;
+ }
+ for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
+ InetAddress a = ia.getAddress();
+ if (a instanceof Inet4Address) {
+ return a.getHostAddress() + "/" + ia.getNetworkPrefixLength();
+ }
+ }
+ } catch (Exception ignored) {
+ }
+ return null;
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ handler.post(refresh);
+ }
+
+ @Override
+ protected void onPause() {
+ handler.removeCallbacks(refresh);
+ super.onPause();
+ }
+}