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

Unverified Commit 62306ce0 authored by Ricardo Cerqueira's avatar Ricardo Cerqueira Committed by Michael Bestas
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 a351017a
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -49,4 +49,11 @@
    <!-- Reboot Progress Dialog. This is shown if the user chooses to reboot the phone. -->
    <string name="reboot_progress">Rebooting\u2026</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
@@ -2882,4 +2882,9 @@
  <java-symbol type="drawable" name="ic_lock_settings" />
  <java-symbol type="drawable" name="ic_lock_user" />

  <!-- 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>
+24 −0
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources.Theme;
import android.os.BaseBundle;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Build;
import android.os.Environment;
import android.os.FactoryTest;
@@ -237,6 +239,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 = CMSettings.Secure.getInt(mContentResolver,
                CMSettings.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() {
        try {
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "InitBeforeStartServices");
@@ -1250,6 +1265,15 @@ public final class SystemServer {
          }
        }

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

        // register observer to listen for settings changes
        mContentResolver.registerContentObserver(
            CMSettings.Secure.getUriFor(CMSettings.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();
+55 −35
Original line number Diff line number Diff line
@@ -329,7 +329,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) {
@@ -372,6 +372,9 @@ public class UsbDeviceManager {
                mContentResolver.registerContentObserver(
                        CMSettings.Secure.getUriFor(CMSettings.Secure.ADB_NOTIFY),
                                false, adbNotificationObserver);
                mContentResolver.registerContentObserver(
                        CMSettings.Secure.getUriFor(CMSettings.Secure.ADB_PORT),
                                false, adbNotificationObserver);

                // Watch for USB configuration changes
                mUEventObserver.startObserving(USB_STATE_MATCH);
@@ -890,16 +893,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 &&
                    CMSettings.Secure.getInt(mContentResolver, CMSettings.Secure.ADB_PORT, -1) > 0;
            boolean hideNotification = "0".equals(SystemProperties.get("persist.adb.notify"))
                    || CMSettings.Secure.getInt(mContext.getContentResolver(),
                            CMSettings.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",
@@ -922,12 +944,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;
            }
        }