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

Commit a316500a authored by Fabien Sanglard's avatar Fabien Sanglard
Browse files

Don't use new API until flag can guard it

This CL is a partial revert of ag/33318019 and aosp3542309 which started
to comm about adb WIFI via the next adbd_auth API.

There is currenly no native API guard. Testing with  __builtin_available
is not enough since Framework cannot check if these events will be sent.
As a result, the only way to fix this problem in trunk-staging and bp3a
is to disable usage of the new API.

Bug: 420187719
Flag: EXEMPT (Revert)
Test: Manual
Change-Id: I3e37dd81822d48f990ceb011c2947d29b358a7de
parent b9dd01e2
Loading
Loading
Loading
Loading
+89 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source 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.
 */
package com.android.server.adb;

import android.annotation.NonNull;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.util.Slog;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * This class will poll for a period of time for adbd to write the port
 * it connected to.
 *
 * TODO(joshuaduong): The port is being sent via system property because the adbd socket
 * (AdbDebuggingManager) is not created when ro.adb.secure=0. Thus, we must communicate the
 * port through different means. A better fix would be to always start AdbDebuggingManager, but
 * it needs to adjust accordingly on whether ro.adb.secure is set.
 */

class AdbConnectionPortPoller extends Thread {

    private static final String TAG = AdbConnectionPortPoller.class.getSimpleName();
    private final String mAdbPortProp = "service.adb.tls.port";
    private final int mDurationSecs = 10;
    private AtomicBoolean mCanceled = new AtomicBoolean(false);

    interface PortChangedCallback {

        void onPortChanged(int port);
    }

    private PortChangedCallback mCallback;

    AdbConnectionPortPoller(@NonNull PortChangedCallback callback) {
        this.mCallback = callback;
    }

    @Override
    public void run() {
        Slog.d(TAG, "Starting TLS port property poller");
        // Once adbwifi is enabled, we poll the service.adb.tls.port
        // system property until we get the port, or -1 on failure.
        // Let's also limit the polling to 10 seconds, just in case
        // something went wrong.
        for (int i = 0; i < mDurationSecs; ++i) {
            if (mCanceled.get()) {
                return;
            }

            // If the property is set to -1, then that means adbd has failed
            // to start the server. Otherwise we should have a valid port.
            int port = SystemProperties.getInt(mAdbPortProp, Integer.MAX_VALUE);
            if (port == -1 || (port > 0 && port <= 65535)) {
                Slog.d(TAG, "System property received TLS port " + port);
                mCallback.onPortChanged(port);
                return;
            }
            SystemClock.sleep(1000);
        }
        Slog.w(TAG, "Failed to receive adb connection port from system property");
        mCallback.onPortChanged(-1);
    }

    public void cancelAndWait() {
        Slog.d(TAG, "Stopping TLS port property poller");
        mCanceled.set(true);
        if (this.isAlive()) {
            try {
                this.join();
            } catch (InterruptedException e) {
            }
        }
    }
}
+34 −6
Original line number Diff line number Diff line
@@ -167,7 +167,10 @@ public class AdbDebuggingManager {
    private final Set<String> mWifiConnectedKeys = new HashSet<>();
    // The current info of the adbwifi connection.
    private AdbConnectionInfo mAdbConnectionInfo = new AdbConnectionInfo();

    // Polls for a tls port property when adb wifi is enabled
    private AdbConnectionPortPoller mConnectionPortPoller;

    private final Ticker mTicker;

    public AdbDebuggingManager(Context context) {
@@ -208,6 +211,27 @@ public class AdbDebuggingManager {
                android.Manifest.permission.MANAGE_DEBUGGING);
    }

    private void startTLSPortPoller() {
        mConnectionPortPoller = new AdbConnectionPortPoller(port -> {
            Slog.d(TAG, "Received tls port from poller =" + port);
            Message msg = mHandler.obtainMessage(port > 0
                    ? AdbDebuggingHandler.MSG_SERVER_CONNECTED
                    : AdbDebuggingHandler.MSG_SERVER_DISCONNECTED);
            msg.obj = port;
            mHandler.sendMessage(msg);
        });
        mConnectionPortPoller.start();
    }

    private void stopTLSPortPoller() {
        if (mConnectionPortPoller == null) {
            return;
        }

        mConnectionPortPoller.cancelAndWait();
        mConnectionPortPoller = null;
    }

    class PairingThread extends Thread implements NsdManager.RegistrationListener {
        private NsdManager mNsdManager;
        @Keep private String mPublicKey;
@@ -824,15 +848,11 @@ public class AdbDebuggingManager {
        }

        private void startAdbdWifi() {
            if (mThread != null) {
                mThread.sendResponse(MSG_START_ADB_WIFI);
            }
            AdbService.enableADBdWifi();
        }

        private void stopAdbdWifi() {
            if (mThread != null) {
                mThread.sendResponse(MSG_STOP_ADB_WIFI);
            }
            AdbService.disableADBdWifi();
        }

        private void startAdbDebuggingThread() {
@@ -1073,6 +1093,7 @@ public class AdbDebuggingManager {


                    startAdbDebuggingThread();
                    startTLSPortPoller();
                    startAdbdWifi();
                    mAdbWifiEnabled = true;

@@ -1118,6 +1139,7 @@ public class AdbDebuggingManager {
                    mContext.registerReceiver(mBroadcastReceiver, intentFilter);

                    startAdbDebuggingThread();
                    startTLSPortPoller();
                    startAdbdWifi();
                    mAdbWifiEnabled = true;

@@ -1218,14 +1240,20 @@ public class AdbDebuggingManager {
                    int port = (int) msg.obj;
                    onAdbdWifiServerDisconnected(port);
                    stopAdbDebuggingThread();
                    stopTLSPortPoller();
                    break;
                }
                case MSG_ADBD_SOCKET_CONNECTED: {
                    Slog.d(TAG, "adbd socket connected");
                    if (mAdbWifiEnabled) {
                        // In scenarios where adbd is restarted, the tls port may change.
                        startTLSPortPoller();
                    }
                    break;
                }
                case MSG_ADBD_SOCKET_DISCONNECTED: {
                    Slog.d(TAG, "adbd socket disconnected");
                    stopTLSPortPoller();
                    if (mAdbWifiEnabled) {
                        // In scenarios where adbd is restarted, the tls port may change.
                        onAdbdWifiServerDisconnected(-1);
+12 −2
Original line number Diff line number Diff line
@@ -433,6 +433,16 @@ public class AdbService extends IAdbManager.Stub {
                AdbService::setAdbEnabledDoNotCallDirectly, this, enable, transportType));
    }

    static void enableADBdWifi() {
        Slog.d(TAG, "Enabling ADBd Wifi property");
        SystemProperties.set(WIFI_PERSISTENT_CONFIG_PROPERTY, "1");
    }

    static void disableADBdWifi() {
        Slog.d(TAG, "Disabling ADBd Wifi property");
        SystemProperties.set(WIFI_PERSISTENT_CONFIG_PROPERTY, "0");
    }

    private void setAdbEnabledDoNotCallDirectly(boolean enable, byte transportType) {
        Slog.d(TAG, "setAdbEnabled(" + enable + "), mIsAdbUsbEnabled=" + mIsAdbUsbEnabled
                 + ", mIsAdbWifiEnabled=" + mIsAdbWifiEnabled + ", transportType=" + transportType);
@@ -451,11 +461,11 @@ public class AdbService extends IAdbManager.Stub {
                mIsAdbWifiEnabled = enable;
                if (mIsAdbWifiEnabled) {
                    // Start adb over WiFi.
                    SystemProperties.set(WIFI_PERSISTENT_CONFIG_PROPERTY, "1");
                    enableADBdWifi();
                    acquireMulticastLock();
                } else {
                    // Stop adb over WiFi.
                    SystemProperties.set(WIFI_PERSISTENT_CONFIG_PROPERTY, "0");
                    disableADBdWifi();
                    releaseMulticastLock();
                }
                break;