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

Commit 35b7c339 authored by Fabien Sanglard's avatar Fabien Sanglard Committed by Android (Google) Code Review
Browse files

Merge "Run google-java-format on adb codebase" into main

parents d6247768 e8b4e63e
Loading
Loading
Loading
Loading
+5 −7
Original line number Diff line number Diff line
@@ -23,15 +23,13 @@ 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.
 * 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.
 * <p>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();
+317 −324

File changed.

Preview size limit exceeded, changes collapsed.

+90 −67
Original line number Diff line number Diff line
@@ -66,25 +66,18 @@ import java.util.Map;
 * of devices allowed to connect to ADB.
 */
public class AdbService extends IAdbManager.Stub {
    /**
     * Adb native daemon.
     */
    /** Adb native daemon. */
    static final String ADBD = "adbd";

    /**
     * Command to start native service.
     */
    /** Command to start native service. */
    static final String CTL_START = "ctl.start";

    /**
     * Command to start native service.
     */
    /** Command to start native service. */
    static final String CTL_STOP = "ctl.stop";

    private final RemoteCallbackList<IAdbCallback> mCallbacks = new RemoteCallbackList<>();
    /**
     * Manages the service lifecycle for {@code AdbService} in {@code SystemServer}.
     */

    /** Manages the service lifecycle for {@code AdbService} in {@code SystemServer}. */
    public static class Lifecycle extends SystemService {
        private AdbService mAdbService;

@@ -103,8 +96,8 @@ public class AdbService extends IAdbManager.Stub {
            if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
                mAdbService.systemReady();
            } else if (phase == SystemService.PHASE_BOOT_COMPLETED) {
                FgThread.getHandler().sendMessage(obtainMessage(
                        AdbService::bootCompleted, mAdbService));
                FgThread.getHandler()
                        .sendMessage(obtainMessage(AdbService::bootCompleted, mAdbService));
            }
        }
    }
@@ -132,7 +125,8 @@ public class AdbService extends IAdbManager.Stub {
                            "AdbTransportType.VSOCK is not yet supported here");
                default:
                    throw new IllegalArgumentException(
                        "isAdbEnabled called with unimplemented transport type=" + transportType);
                            "isAdbEnabled called with unimplemented transport type="
                                    + transportType);
            }
        }

@@ -157,11 +151,9 @@ public class AdbService extends IAdbManager.Stub {
            // register observer to listen for settings changes
            mObserver = new AdbSettingsObserver();
            mContentResolver.registerContentObserver(
                    Settings.Global.getUriFor(Settings.Global.ADB_ENABLED),
                    false, mObserver);
                    Settings.Global.getUriFor(Settings.Global.ADB_ENABLED), false, mObserver);
            mContentResolver.registerContentObserver(
                    Settings.Global.getUriFor(Settings.Global.ADB_WIFI_ENABLED),
                    false, mObserver);
                    Settings.Global.getUriFor(Settings.Global.ADB_WIFI_ENABLED), false, mObserver);
        } catch (Exception e) {
            Slog.e(TAG, "Error in registerContentObservers", e);
        }
@@ -188,12 +180,15 @@ public class AdbService extends IAdbManager.Stub {
        public void onChange(boolean selfChange, @NonNull Uri uri, @UserIdInt int userId) {
            Slog.d("AdbSettingsObserver", "onChange " + uri.toString());
            if (mAdbUsbUri.equals(uri)) {
                boolean shouldEnable = (Settings.Global.getInt(mContentResolver,
                        Settings.Global.ADB_ENABLED, 0) > 0);
                boolean shouldEnable =
                        (Settings.Global.getInt(mContentResolver, Settings.Global.ADB_ENABLED, 0)
                                > 0);
                setAdbEnabled(shouldEnable, AdbTransportType.USB);
            } else if (mAdbWifiUri.equals(uri)) {
                boolean shouldEnable = (Settings.Global.getInt(mContentResolver,
                        Settings.Global.ADB_WIFI_ENABLED, 0) > 0);
                boolean shouldEnable =
                        (Settings.Global.getInt(
                                        mContentResolver, Settings.Global.ADB_WIFI_ENABLED, 0)
                                > 0);
                setAdbEnabled(shouldEnable, AdbTransportType.WIFI);
            }
        }
@@ -202,14 +197,14 @@ public class AdbService extends IAdbManager.Stub {
    private static final String TAG = AdbService.class.getSimpleName();

    /**
     * The persistent property which stores whether adb is enabled or not.
     * May also contain vendor-specific default functions for testing purposes.
     * The persistent property which stores whether adb is enabled or not. May also contain
     * vendor-specific default functions for testing purposes.
     */
    private static final String USB_PERSISTENT_CONFIG_PROPERTY = "persist.sys.usb.config";

    /**
     * The system property used by both framework and adbd to set if ADB Wifi should be enabled
     * when either system starts up.
     * The system property used by both framework and adbd to set if ADB Wifi should be enabled when
     * either system starts up.
     */
    private static final String WIFI_PERSISTENT_CONFIG_PROPERTY = "persist.adb.tls_server.enable";

@@ -243,21 +238,22 @@ public class AdbService extends IAdbManager.Stub {
         * Use the normal bootmode persistent prop to maintain state of adb across
         * all boot modes.
         */
        mIsAdbUsbEnabled = containsFunction(
        mIsAdbUsbEnabled =
                containsFunction(
                        SystemProperties.get(USB_PERSISTENT_CONFIG_PROPERTY, ""),
                        UsbManager.USB_FUNCTION_ADB);
        boolean shouldEnableAdbUsb = mIsAdbUsbEnabled
        boolean shouldEnableAdbUsb =
                mIsAdbUsbEnabled
                        || SystemProperties.getBoolean(
                                TestHarnessModeService.TEST_HARNESS_MODE_PROPERTY, false);
        mIsAdbWifiEnabled = "1".equals(
                SystemProperties.get(WIFI_PERSISTENT_CONFIG_PROPERTY, "0"));
        mIsAdbWifiEnabled = "1".equals(SystemProperties.get(WIFI_PERSISTENT_CONFIG_PROPERTY, "0"));

        // make sure the ADB_ENABLED setting value matches the current state
        try {
            Settings.Global.putInt(mContentResolver,
                    Settings.Global.ADB_ENABLED, shouldEnableAdbUsb ? 1 : 0);
            Settings.Global.putInt(mContentResolver,
                    Settings.Global.ADB_WIFI_ENABLED, mIsAdbWifiEnabled ? 1 : 0);
            Settings.Global.putInt(
                    mContentResolver, Settings.Global.ADB_ENABLED, shouldEnableAdbUsb ? 1 : 0);
            Settings.Global.putInt(
                    mContentResolver, Settings.Global.ADB_WIFI_ENABLED, mIsAdbWifiEnabled ? 1 : 0);
        } catch (SecurityException e) {
            // If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't be changed.
            Slog.d(TAG, "ADB_ENABLED is restricted.");
@@ -300,21 +296,20 @@ public class AdbService extends IAdbManager.Stub {
    public boolean isAdbWifiSupported() {
        mContext.enforceCallingPermission(
                android.Manifest.permission.MANAGE_DEBUGGING, "AdbService");
        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI) ||
                mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_ETHERNET);
        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)
                || mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_ETHERNET);
    }

    /**
     * @return true if the device supports secure ADB over Wi-Fi and device pairing by
     * QR code.
     * @return true if the device supports secure ADB over Wi-Fi and device pairing by QR code.
     * @hide
     */
    @Override
    public boolean isAdbWifiQrSupported() {
        mContext.enforceCallingPermission(
                android.Manifest.permission.MANAGE_DEBUGGING, "AdbService");
        return isAdbWifiSupported() && mContext.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA_ANY);
        return isAdbWifiSupported()
                && mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
    }

    @Override
@@ -404,7 +399,8 @@ public class AdbService extends IAdbManager.Stub {

    private void acquireMulticastLock() {
        if (mAdbMulticastLock == null) {
            WifiManager wifiManager = (WifiManager)
            WifiManager wifiManager =
                    (WifiManager)
                            mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            mAdbMulticastLock = wifiManager.createMulticastLock("AdbMulticastLock");
        }
@@ -423,8 +419,13 @@ public class AdbService extends IAdbManager.Stub {
    }

    private void setAdbEnabled(boolean enable, byte transportType) {
        FgThread.getHandler().sendMessage(obtainMessage(
                AdbService::setAdbEnabledDoNotCallDirectly, this, enable, transportType));
        FgThread.getHandler()
                .sendMessage(
                        obtainMessage(
                                AdbService::setAdbEnabledDoNotCallDirectly,
                                this,
                                enable,
                                transportType));
    }

    static void enableADBdWifi() {
@@ -438,8 +439,16 @@ public class AdbService extends IAdbManager.Stub {
    }

    private void setAdbEnabledDoNotCallDirectly(boolean enable, byte transportType) {
        Slog.d(TAG, "setAdbEnabled(" + enable + "), mIsAdbUsbEnabled=" + mIsAdbUsbEnabled
                 + ", mIsAdbWifiEnabled=" + mIsAdbWifiEnabled + ", transportType=" + transportType);
        Slog.d(
                TAG,
                "setAdbEnabled("
                        + enable
                        + "), mIsAdbUsbEnabled="
                        + mIsAdbUsbEnabled
                        + ", mIsAdbWifiEnabled="
                        + mIsAdbWifiEnabled
                        + ", transportType="
                        + transportType);

        switch (transportType) {
            case AdbTransportType.USB:
@@ -488,8 +497,15 @@ public class AdbService extends IAdbManager.Stub {
        mDebuggingManager.setAdbEnabled(enable, transportType);

        Slog.d(TAG, "Broadcasting enable = " + enable + ", type = " + transportType);
        mCallbacks.broadcast((callback) -> {
            Slog.d(TAG, "Sending enable = " + enable + ", type = " + transportType + " to "
        mCallbacks.broadcast(
                (callback) -> {
                    Slog.d(
                            TAG,
                            "Sending enable = "
                                    + enable
                                    + ", type = "
                                    + transportType
                                    + " to "
                                    + callback);
                    try {
                        callback.onDebuggingChanged(enable, transportType);
@@ -500,10 +516,17 @@ public class AdbService extends IAdbManager.Stub {
    }

    @Override
    public int handleShellCommand(ParcelFileDescriptor in, ParcelFileDescriptor out,
            ParcelFileDescriptor err, String[] args) {
        return new AdbShellCommand(this).exec(
                this, in.getFileDescriptor(), out.getFileDescriptor(), err.getFileDescriptor(),
    public int handleShellCommand(
            ParcelFileDescriptor in,
            ParcelFileDescriptor out,
            ParcelFileDescriptor err,
            String[] args) {
        return new AdbShellCommand(this)
                .exec(
                        this,
                        in.getFileDescriptor(),
                        out.getFileDescriptor(),
                        err.getFileDescriptor(),
                        args);
    }

@@ -532,8 +555,8 @@ public class AdbService extends IAdbManager.Stub {
                    dump = new DualDumpOutputStream(new IndentingPrintWriter(pw, "  "));
                }

                mDebuggingManager.dump(dump, "debugging_manager",
                        AdbServiceDumpProto.DEBUGGING_MANAGER);
                mDebuggingManager.dump(
                        dump, "debugging_manager", AdbServiceDumpProto.DEBUGGING_MANAGER);
                dump.flush();
            } else {
                pw.println("Dump current ADB state");
+5 −6
Original line number Diff line number Diff line
@@ -21,9 +21,7 @@ import com.android.modules.utils.BasicShellCommandHandler;
import java.io.PrintWriter;
import java.util.Objects;

/**
 * Interprets and executes 'adb shell cmd adb [args]'.
 */
/** Interprets and executes 'adb shell cmd adb [args]'. */
class AdbShellCommand extends BasicShellCommandHandler {

    private final AdbService mService;
@@ -40,18 +38,19 @@ class AdbShellCommand extends BasicShellCommandHandler {

        final PrintWriter pw = getOutPrintWriter();
        switch (cmd) {
            case "is-wifi-supported": {
            case "is-wifi-supported" -> {
                pw.println(Boolean.toString(mService.isAdbWifiSupported()));
                return 0;
            }
            case "is-wifi-qr-supported": {
            case "is-wifi-qr-supported" -> {
                pw.println(Boolean.toString(mService.isAdbWifiQrSupported()));
                return 0;
            }
            default:
            default -> {
                return handleDefaultCommands(cmd);
            }
        }
    }

    @Override
    public void onHelp() {