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

Commit deed70c1 authored by Tanguy Pruvot's avatar Tanguy Pruvot Committed by Michael Bestas
Browse files

Port ADB over network and ability to hide the notification

Port "Option to hide Adb notification icon (2/2)"

From: Ricardo Cerqueira <cyanogenmod@cerqueira.org>
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

From: Ethan Chen <intervigil@gmail.com>
Use custom ADB over wifi property

* Use a custom system property to trigger the real one, so we avoid
  running afoul of any SELinux CTS requirements.

Change-Id: If23636cb5b51eb8fbdc76e9fa49ac474fbd6bf33
parent c3420b9b
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2017 The LineageOS Project
     Copyright (C) 2012-2014 The CyanogenMod Project
     Copyright (c) 2013, The Linux Foundation. All rights reserved.

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

           http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

    <!-- 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>
+22 −0
Original line number Diff line number Diff line
<!--
     Copyright (C) 2012-2015 The CyanogenMod Project
     Copyright (C) 2017 The LineageOS Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources>
    <!-- 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>
+26 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources.Theme;
import android.database.ContentObserver;
import android.database.sqlite.SQLiteCompatibilityWalFlags;
import android.database.sqlite.SQLiteGlobal;
import android.hardware.display.DisplayManagerInternal;
@@ -170,6 +171,8 @@ import java.util.Timer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

import lineageos.providers.LineageSettings;

public final class SystemServer {

    private static final String TAG = "SystemServer";
@@ -371,6 +374,20 @@ public final class SystemServer {
        mRuntimeRestart = "1".equals(SystemProperties.get("sys.boot_completed"));
    }

    private class AdbPortObserver extends ContentObserver {
        public AdbPortObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            int adbPort = LineageSettings.Secure.getInt(mContentResolver,
                    LineageSettings.Secure.ADB_PORT, 0);
            // Setting this will control whether ADB runs on TCP/IP or USB
            SystemProperties.set("adb.network.port", Integer.toString(adbPort));
        }
    }

    private void run() {
        try {
            traceBeginAndSlog("InitBeforeStartServices");
@@ -1899,6 +1916,15 @@ public final class SystemServer {
        mSystemServiceManager.startService(IncidentCompanionService.class);
        traceEnd();

        // Make sure the ADB_ENABLED setting value matches the secure property value
        LineageSettings.Secure.putInt(mContentResolver, LineageSettings.Secure.ADB_PORT,
                SystemProperties.getInt("service.adb.tcp.port", -1));

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

        if (safeMode) {
            mActivityManagerService.enterSafeMode();
        }
+1 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ java_library_static {
    libs: [
        "services.core",
        "android.hidl.manager-V1.0-java",
        "org.lineageos.platform.internal",
    ],

    static_libs: [
+43 −12
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.debug.AdbManagerInternal;
import android.debug.IAdbTransport;
import android.hardware.usb.ParcelableUsbPort;
@@ -103,6 +104,8 @@ import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Set;

import lineageos.providers.LineageSettings;

/**
 * UsbDeviceManager manages USB state in device mode.
 */
@@ -350,6 +353,20 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
        mContext.registerReceiver(languageChangedReceiver,
                new IntentFilter(Intent.ACTION_LOCALE_CHANGED));

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

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

        // Watch for USB configuration changes
        mUEventObserver = new UsbUEventObserver();
        mUEventObserver.startObserving(USB_STATE_MATCH);
@@ -453,7 +470,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser

        private UsbAccessory mCurrentAccessory;
        private int mUsbNotificationId;
        private boolean mAdbNotificationShown;
        private int mAdbNotificationId;
        private boolean mUsbCharging;
        private boolean mHideUsbNotification;
        private boolean mSupportsAllCombinations;
@@ -1173,17 +1190,33 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
        protected void updateAdbNotification(boolean force) {
            if (mNotificationManager == null) return;
            final int id = SystemMessage.NOTE_ADB_ACTIVE;
            final int titleRes = com.android.internal.R.string.adb_active_notification_title;

            if (isAdbEnabled() && mConnected) {
                if ("0".equals(getSystemProperty("persist.adb.notify", ""))) return;
            final int titleRes;
            boolean usbAdbActive = isAdbEnabled() && mConnected;
            boolean netAdbActive = isAdbEnabled() &&
                    LineageSettings.Secure.getInt(mContentResolver,
                            LineageSettings.Secure.ADB_PORT, -1) > 0;
            boolean hideNotification = SystemProperties.getInt("persist.adb.notify", -1) == 0
                    || LineageSettings.Secure.getInt(mContext.getContentResolver(),
                            LineageSettings.Secure.ADB_NOTIFY, 1) == 0;

            if (hideNotification) {
                titleRes = 0;
            } else if (usbAdbActive && netAdbActive) {
                titleRes = com.android.internal.R.string.adb_both_active_notification_title;
            } else if (usbAdbActive) {
                titleRes = com.android.internal.R.string.adb_active_notification_title;
            } else if (netAdbActive) {
                titleRes = com.android.internal.R.string.adb_net_active_notification_title;
            } else {
                titleRes = 0;
            }

                if (force && mAdbNotificationShown) {
                    mAdbNotificationShown = false;
            if (titleRes != mAdbNotificationId) {
                if (mAdbNotificationId != 0) {
                    mNotificationManager.cancelAsUser(null, id, UserHandle.ALL);
                }

                if (!mAdbNotificationShown) {
                if (titleRes != 0) {
                    Resources r = mContext.getResources();
                    CharSequence title = r.getText(titleRes);
                    CharSequence message = r.getText(
@@ -1212,13 +1245,11 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
                                    .extend(new Notification.TvExtender()
                                            .setChannelId(ADB_NOTIFICATION_CHANNEL_ID_TV))
                                    .build();
                    mAdbNotificationShown = true;
                    mNotificationManager.notifyAsUser(null, id, notification,
                            UserHandle.ALL);
                }
            } else if (mAdbNotificationShown) {
                mAdbNotificationShown = false;
                mNotificationManager.cancelAsUser(null, id, UserHandle.ALL);

                mAdbNotificationId = titleRes;
            }
        }