Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 47e1d1f5 authored by Ricardo Cerqueira's avatar Ricardo Cerqueira Committed by Steve Kondik
Browse files

Framework: Forward port ADB over network (Part 2 of 2)

Includes:
- ADB Over Network, integration of the adb-host mode (already present)
  The feature can be used/tested without the Settings part:

    setprop service.adb.tcp.port 5555

  Note: This ADB setting is not persistent (for security purpose) and
  require init.rc implementation event like this : http://bit.ly/AdbTcpIP

  Author: Tanguy Pruvot
  Id: I5c61a53948349c785356cb5aae165110d75e3074

  Author: sssemil <suleymanovemil8@gmail.com>
  Show notification on adb over network too

  Screenshots - http://goo.gl/TgsRI6
  Id: I9ddc0aa9a4f330a06ab5d97a8645d1b31bb6f299

Change-Id: I101216c5b8ddff5040d9eeaf35afefc5cd98bbf3
parent cce429f5
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -4712,6 +4712,12 @@ public final class Settings {
         */
        public static final String ADB_NOTIFY = "adb_notify";

        /**
         * The TCP/IP port to run ADB on, or -1 for USB
         * @hide
         */
        public static final String ADB_PORT = "adb_port";

        /**
         * The hostname for this device
         * @hide
+7 −0
Original line number Diff line number Diff line
@@ -86,4 +86,11 @@
    <!-- Long-press back kill application -->
    <string name="app_killed_message">Application killed</string>

    <!-- ADB over network notification -->
    <string name="adb_net_active_notification_title">ADB over network enabled</string>
    <!-- ADB over USB and network notification -->
    <string name="adb_both_active_notification_title">ADB over USB &amp; network enabled</string>
    <!-- ADB notification message-->
    <string name="adb_active_generic_notification_message">Touch to disable debugging.</string>

</resources>
+5 −0
Original line number Diff line number Diff line
@@ -2417,4 +2417,9 @@
  <!-- Config.xml entries -->
  <java-symbol type="integer" name="config_backKillTimeout" />

  <!-- ADB notification -->
  <java-symbol type="string" name="adb_net_active_notification_title" />
  <java-symbol type="string" name="adb_both_active_notification_title" />
  <java-symbol type="string" name="adb_active_generic_notification_message" />

</resources>
+25 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources.Theme;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Build;
import android.os.Environment;
import android.os.FactoryTest;
@@ -40,6 +42,7 @@ import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.storage.IMountService;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.EventLog;
import android.util.Slog;
@@ -175,6 +178,19 @@ public final class SystemServer {
        mFactoryTestMode = FactoryTest.getMode();
    }

    private class AdbPortObserver extends ContentObserver {
        public AdbPortObserver() {
            super(null);
        }
        @Override
        public void onChange(boolean selfChange) {
            int adbPort = Settings.Secure.getInt(mContentResolver,
                Settings.Secure.ADB_PORT, 0);
            // setting this will control whether ADB runs on TCP/IP or USB
            SystemProperties.set("service.adb.tcp.port", Integer.toString(adbPort));
        }
    }

    private void run() {
        // If a device's clock is before 1970 (before 0), a lot of
        // APIs crash dealing with negative numbers, notably
@@ -1012,6 +1028,15 @@ public final class SystemServer {
            mSystemServiceManager.startService(MediaProjectionManagerService.class);
        }

        // make sure the ADB_ENABLED setting value matches the secure property value
        Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_PORT,
                Integer.parseInt(SystemProperties.get("service.adb.tcp.port", "-1")));

        // register observer to listen for settings changes
        mContentResolver.registerContentObserver(
            Settings.Secure.getUriFor(Settings.Secure.ADB_PORT),
            false, new AdbPortObserver());

        // Before things start rolling, be sure we have decided whether
        // we are in safe mode.
        final boolean safeMode = wm.detectSafeMode();
+64 −41
Original line number Diff line number Diff line
@@ -305,7 +305,7 @@ public class UsbDeviceManager {
        private boolean mCurrentFunctionsApplied;
        private UsbAccessory mCurrentAccessory;
        private int mUsbNotificationId;
        private boolean mAdbNotificationShown;
        private int mAdbNotificationId;
        private int mCurrentUser = UserHandle.USER_NULL;

        public UsbHandler(Looper looper) {
@@ -330,14 +330,20 @@ public class UsbDeviceManager {
                mContentResolver.registerContentObserver(
                        Settings.Global.getUriFor(Settings.Global.ADB_ENABLED),
                                false, new AdbSettingsObserver());
                mContentResolver.registerContentObserver(
                        Settings.Secure.getUriFor(Settings.Secure.ADB_NOTIFY),
                                false, new ContentObserver(null) {

                ContentObserver adbNotificationObserver = new ContentObserver(null) {
                    @Override
                    public void onChange(boolean selfChange) {
                        updateAdbNotification();
                    }
                        }
                );
                };

                mContentResolver.registerContentObserver(
                        Settings.Secure.getUriFor(Settings.Secure.ADB_PORT),
                                false, adbNotificationObserver);
                mContentResolver.registerContentObserver(
                        Settings.Secure.getUriFor(Settings.Secure.ADB_NOTIFY),
                                false, adbNotificationObserver);

                // Watch for USB configuration changes
                mUEventObserver.startObserving(USB_STATE_MATCH);
@@ -775,16 +781,35 @@ public class UsbDeviceManager {

        private void updateAdbNotification() {
            if (mNotificationManager == null) return;
            final int id = com.android.internal.R.string.adb_active_notification_title;
            final int id;
            boolean usbAdbActive = mAdbEnabled && mConnected;
            boolean netAdbActive = mAdbEnabled &&
                    Settings.Secure.getInt(mContentResolver, Settings.Secure.ADB_PORT, -1) > 0;
            boolean hideNotification = "0".equals(SystemProperties.get("persist.adb.notify"))
                    || Settings.Secure.getInt(mContext.getContentResolver(),
                            Settings.Secure.ADB_NOTIFY, 1) == 0;

            if (mAdbEnabled && mConnected && !mAdbNotificationShown && !hideNotification) {
            if (hideNotification) {
                id = 0;
            } else if (usbAdbActive && netAdbActive) {
                id = com.android.internal.R.string.adb_both_active_notification_title;
            } else if (usbAdbActive) {
                id = com.android.internal.R.string.adb_active_notification_title;
            } else if (netAdbActive) {
                id = com.android.internal.R.string.adb_net_active_notification_title;
            } else {
                id = 0;
            }

            if (id != mAdbNotificationId) {
                if (mAdbNotificationId != 0) {
                    mNotificationManager.cancelAsUser(null, mAdbNotificationId, UserHandle.ALL);
                }
                if (id != 0) {
                    Resources r = mContext.getResources();
                    CharSequence title = r.getText(id);
                    CharSequence message = r.getText(
                        com.android.internal.R.string.adb_active_notification_message);
                            com.android.internal.R.string.adb_active_generic_notification_message);

                    Intent intent = Intent.makeRestartActivityTask(
                            new ComponentName("com.android.settings",
@@ -807,12 +832,10 @@ public class UsbDeviceManager {
                            .setVisibility(Notification.VISIBILITY_PUBLIC)
                            .build();

                mAdbNotificationShown = true;
                    mNotificationManager.notifyAsUser(null, id, notification,
                            UserHandle.ALL);
            } else if (mAdbNotificationShown) {
                mAdbNotificationShown = false;
                mNotificationManager.cancelAsUser(null, id, UserHandle.ALL);
                }
                mAdbNotificationId = id;
            }
        }