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

Commit 541c9949 authored by Mike Lockwood's avatar Mike Lockwood
Browse files

Move adb related functionality from SystemServer and NotificationManagerService to UsbDeviceManager



Change-Id: I9e64b54bc2d027d88ae2d2cd893068e29b6fe237
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 4a97122e
Loading
Loading
Loading
Loading
+1 −75
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.StatusBarManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
@@ -37,7 +36,6 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.hardware.usb.UsbManager;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Binder;
@@ -47,7 +45,6 @@ import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.os.Vibrator;
import android.provider.Settings;
import android.telephony.TelephonyManager;
@@ -112,10 +109,6 @@ public class NotificationManagerService extends INotificationManager.Stub
    // This is reset to false when the screen is turned on.
    private boolean mPendingPulseNotification;

    // for adb connected notifications
    private boolean mAdbNotificationShown = false;
    private Notification mAdbNotification;

    private final ArrayList<NotificationRecord> mNotificationList =
            new ArrayList<NotificationRecord>();

@@ -330,12 +323,7 @@ public class NotificationManagerService extends INotificationManager.Stub

            boolean queryRestart = false;
            
            if (action.equals(UsbManager.ACTION_USB_STATE)) {
                Bundle extras = intent.getExtras();
                boolean usbConnected = extras.getBoolean(UsbManager.USB_CONNECTED);
                boolean adbEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_ADB);
                updateAdbNotification(usbConnected && adbEnabled);
            } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
            if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
                    || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
                    || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
                    || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
@@ -436,7 +424,6 @@ public class NotificationManagerService extends INotificationManager.Stub

        // register for various Intents
        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_STATE);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
@@ -1116,67 +1103,6 @@ public class NotificationManagerService extends INotificationManager.Stub
        return -1;
    }

    // This is here instead of StatusBarPolicy because it is an important
    // security feature that we don't want people customizing the platform
    // to accidentally lose.
    private void updateAdbNotification(boolean adbEnabled) {
        if (adbEnabled) {
            if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
                return;
            }
            if (!mAdbNotificationShown) {
                NotificationManager notificationManager = (NotificationManager) mContext
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                if (notificationManager != null) {
                    Resources r = mContext.getResources();
                    CharSequence title = r.getText(
                            com.android.internal.R.string.adb_active_notification_title);
                    CharSequence message = r.getText(
                            com.android.internal.R.string.adb_active_notification_message);

                    if (mAdbNotification == null) {
                        mAdbNotification = new Notification();
                        mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
                        mAdbNotification.when = 0;
                        mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
                        mAdbNotification.tickerText = title;
                        mAdbNotification.defaults = 0; // please be quiet
                        mAdbNotification.sound = null;
                        mAdbNotification.vibrate = null;
                    }

                    Intent intent = new Intent(
                            Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                            Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    // Note: we are hard-coding the component because this is
                    // an important security UI that we don't want anyone
                    // intercepting.
                    intent.setComponent(new ComponentName("com.android.settings",
                            "com.android.settings.DevelopmentSettings"));
                    PendingIntent pi = PendingIntent.getActivity(mContext, 0,
                            intent, 0);

                    mAdbNotification.setLatestEventInfo(mContext, title, message, pi);

                    mAdbNotificationShown = true;
                    notificationManager.notify(
                            com.android.internal.R.string.adb_active_notification_title,
                            mAdbNotification);
                }
            }

        } else if (mAdbNotificationShown) {
            NotificationManager notificationManager = (NotificationManager) mContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                mAdbNotificationShown = false;
                notificationManager.cancel(
                        com.android.internal.R.string.adb_active_notification_title);
            }
        }
    }

    private void updateNotificationPulse() {
        synchronized (mNotificationList) {
            updateLightsLocked();
+0 −22
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.IPackageManager;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.media.AudioService;
import android.os.Looper;
import android.os.RemoteException;
@@ -66,19 +65,6 @@ class ServerThread extends Thread {

    ContentResolver mContentResolver;

    private class AdbSettingsObserver extends ContentObserver {
        public AdbSettingsObserver() {
            super(null);
        }
        @Override
        public void onChange(boolean selfChange) {
            boolean enableAdb = (Settings.Secure.getInt(mContentResolver,
                Settings.Secure.ADB_ENABLED, 0) > 0);
            // setting this secure property will start or stop adbd
           SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0");
        }
    }

    @Override
    public void run() {
        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
@@ -485,14 +471,6 @@ class ServerThread extends Thread {
            }
        }

        // make sure the ADB_ENABLED setting value matches the secure property value
        Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
                "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);

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

        // Before things start rolling, be sure we have decided whether
        // we are in safe mode.
        final boolean safeMode = wm.detectSafeMode();
+92 −0
Original line number Diff line number Diff line
@@ -17,12 +17,17 @@
package com.android.server.usb;

import android.app.PendingIntent;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.net.Uri;
@@ -32,6 +37,7 @@ import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.os.ParcelFileDescriptor;
import android.os.SystemProperties;
import android.os.UEventObserver;
import android.provider.Settings;
import android.util.Log;
@@ -92,10 +98,81 @@ public class UsbDeviceManager {
    private final ArrayList<String> mDefaultFunctions = new ArrayList<String>();

    private final Context mContext;
    ContentResolver mContentResolver;
    private final Object mLock = new Object();
    private final UsbSettingsManager mSettingsManager;
    private NotificationManager mNotificationManager;
    private final boolean mHasUsbAccessory;

    // for adb connected notifications
    private boolean mAdbNotificationShown = false;
    private Notification mAdbNotification;
    private boolean mAdbEnabled;

    private class AdbSettingsObserver extends ContentObserver {
        public AdbSettingsObserver() {
            super(null);
        }
        @Override
        public void onChange(boolean selfChange) {
            mAdbEnabled = (Settings.Secure.getInt(mContentResolver,
                Settings.Secure.ADB_ENABLED, 0) > 0);
            // setting this secure property will start or stop adbd
           SystemProperties.set("persist.service.adb.enable", mAdbEnabled ? "1" : "0");
           updateAdbNotification();
        }
    }

    private void updateAdbNotification() {
        if (mNotificationManager == null) return;
        boolean adbEnabled = mAdbEnabled && (mConnected == 1);
        if (adbEnabled) {
            if ("0".equals(SystemProperties.get("persist.adb.notify"))) return;

            if (!mAdbNotificationShown) {
                Resources r = mContext.getResources();
                CharSequence title = r.getText(
                        com.android.internal.R.string.adb_active_notification_title);
                CharSequence message = r.getText(
                        com.android.internal.R.string.adb_active_notification_message);

                if (mAdbNotification == null) {
                    mAdbNotification = new Notification();
                    mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
                    mAdbNotification.when = 0;
                    mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
                    mAdbNotification.tickerText = title;
                    mAdbNotification.defaults = 0; // please be quiet
                    mAdbNotification.sound = null;
                    mAdbNotification.vibrate = null;
                }

                Intent intent = new Intent(
                        Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                        Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                // Note: we are hard-coding the component because this is
                // an important security UI that we don't want anyone
                // intercepting.
                intent.setComponent(new ComponentName("com.android.settings",
                        "com.android.settings.DevelopmentSettings"));
                PendingIntent pi = PendingIntent.getActivity(mContext, 0,
                        intent, 0);

                mAdbNotification.setLatestEventInfo(mContext, title, message, pi);

                mAdbNotificationShown = true;
                mNotificationManager.notify(
                        com.android.internal.R.string.adb_active_notification_title,
                        mAdbNotification);
            }
        } else if (mAdbNotificationShown) {
            mAdbNotificationShown = false;
            mNotificationManager.cancel(
                    com.android.internal.R.string.adb_active_notification_title);
        }
    }

    private final void readCurrentAccessoryLocked() {
        if (mHasUsbAccessory) {
            String[] strings = nativeGetAccessoryStrings();
@@ -191,6 +268,7 @@ public class UsbDeviceManager {

    public UsbDeviceManager(Context context, UsbSettingsManager settingsManager) {
        mContext = context;
        mContentResolver = context.getContentResolver();
        mSettingsManager = settingsManager;
        PackageManager pm = mContext.getPackageManager();
        mHasUsbAccessory = pm.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
@@ -198,6 +276,16 @@ public class UsbDeviceManager {
        synchronized (mLock) {
            init();  // set initial status

            // make sure the ADB_ENABLED setting value matches the secure property value
            mAdbEnabled = "1".equals(SystemProperties.get("persist.service.adb.enable"));
            Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
                    mAdbEnabled ? 1 : 0);

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

            // Watch for USB configuration changes
            if (mConfiguration >= 0) {
                mUEventObserver.startObserving(USB_CONNECTED_MATCH);
@@ -281,6 +369,9 @@ public class UsbDeviceManager {

    public void systemReady() {
        synchronized (mLock) {
                mNotificationManager = (NotificationManager)
                        mContext.getSystemService(Context.NOTIFICATION_SERVICE);

            update(false);
            if (mCurrentAccessory != null) {
                Log.d(TAG, "accessoryAttached at systemReady");
@@ -335,6 +426,7 @@ public class UsbDeviceManager {
                switch (msg.what) {
                    case MSG_UPDATE_STATE:
                        if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) {
                            updateAdbNotification();
                            if (mConnected == 0) {
                                if (UsbManager.isFunctionEnabled(
                                            UsbManager.USB_FUNCTION_ACCESSORY)) {