Use internal volume controller and status bar.

Remove code that let these components be replaced by external
processes.

Bug: 33006669
Bug: 34106436
Test: manual. reboot device, play music, change volume,
view notifications
Change-Id: I458f11537ab9db6f23735487513815553004613f
diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java
index c820302..73d9ea7 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java
@@ -16,31 +16,13 @@
 
 package com.android.systemui.volume;
 
-import android.app.Notification;
-import android.app.NotificationManager;
-import android.app.PendingIntent;
-import android.content.BroadcastReceiver;
-import android.content.ComponentName;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.content.DialogInterface.OnClickListener;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.content.pm.ApplicationInfo;
 import android.content.res.Configuration;
-import android.media.AudioManager;
-import android.media.session.MediaSessionManager;
 import android.os.Handler;
-import android.provider.Settings;
-import android.text.TextUtils;
 import android.util.Log;
 
-import com.android.systemui.Prefs;
 import com.android.systemui.R;
 import com.android.systemui.SystemUI;
 import com.android.systemui.qs.tiles.DndTile;
-import com.android.systemui.statusbar.ServiceMonitor;
-import com.android.systemui.statusbar.phone.SystemUIDialog;
 import com.android.systemui.statusbar.policy.ZenModeController;
 import com.android.systemui.statusbar.policy.ZenModeControllerImpl;
 
@@ -52,34 +34,18 @@
     private static boolean LOGD = Log.isLoggable(TAG, Log.DEBUG);
 
     private final Handler mHandler = new Handler();
-    private final Receiver mReceiver = new Receiver();
-    private final RestorationNotification mRestorationNotification = new RestorationNotification();
 
     private boolean mEnabled;
-    private AudioManager mAudioManager;
-    private NotificationManager mNotificationManager;
-    private MediaSessionManager mMediaSessionManager;
-    private ServiceMonitor mVolumeControllerService;
-
     private VolumeDialogComponent mVolumeComponent;
 
     @Override
     public void start() {
         mEnabled = mContext.getResources().getBoolean(R.bool.enable_volume_ui);
         if (!mEnabled) return;
-        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
-        mNotificationManager =
-                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
-        mMediaSessionManager = (MediaSessionManager) mContext
-                .getSystemService(Context.MEDIA_SESSION_SERVICE);
         final ZenModeController zenController = new ZenModeControllerImpl(mContext, mHandler);
         mVolumeComponent = new VolumeDialogComponent(this, mContext, null, zenController);
         putComponent(VolumeComponent.class, getVolumeComponent());
-        mReceiver.start();
-        mVolumeControllerService = new ServiceMonitor(TAG, LOGD,
-                mContext, Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT,
-                new ServiceMonitorCallbacks());
-        mVolumeControllerService.start();
+        setDefaultVolumeController();
     }
 
     private VolumeComponent getVolumeComponent() {
@@ -97,154 +63,12 @@
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         pw.print("mEnabled="); pw.println(mEnabled);
         if (!mEnabled) return;
-        pw.print("mVolumeControllerService="); pw.println(mVolumeControllerService.getComponent());
         getVolumeComponent().dump(fd, pw, args);
     }
 
-    private void setDefaultVolumeController(boolean register) {
-        if (register) {
-            DndTile.setVisible(mContext, true);
-            if (LOGD) Log.d(TAG, "Registering default volume controller");
-            getVolumeComponent().register();
-        } else {
-            if (LOGD) Log.d(TAG, "Unregistering default volume controller");
-            mAudioManager.setVolumeController(null);
-            mMediaSessionManager.setRemoteVolumeController(null);
-        }
-    }
-
-    private String getAppLabel(ComponentName component) {
-        final String pkg = component.getPackageName();
-        try {
-            final ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(pkg, 0);
-            final String rt = mContext.getPackageManager().getApplicationLabel(ai).toString();
-            if (!TextUtils.isEmpty(rt)) {
-                return rt;
-            }
-        } catch (Exception e) {
-            Log.w(TAG, "Error loading app label", e);
-        }
-        return pkg;
-    }
-
-    private void showServiceActivationDialog(final ComponentName component) {
-        final SystemUIDialog d = new SystemUIDialog(mContext);
-        d.setMessage(mContext.getString(R.string.volumeui_prompt_message, getAppLabel(component)));
-        d.setPositiveButton(R.string.volumeui_prompt_allow, new OnClickListener() {
-            @Override
-            public void onClick(DialogInterface dialog, int which) {
-                mVolumeControllerService.setComponent(component);
-            }
-        });
-        d.setNegativeButton(R.string.volumeui_prompt_deny, null);
-        d.show();
-    }
-
-    private final class ServiceMonitorCallbacks implements ServiceMonitor.Callbacks {
-        @Override
-        public void onNoService() {
-            if (LOGD) Log.d(TAG, "onNoService");
-            setDefaultVolumeController(true);
-            mRestorationNotification.hide();
-            if (!mVolumeControllerService.isPackageAvailable()) {
-                mVolumeControllerService.setComponent(null);
-            }
-        }
-
-        @Override
-        public long onServiceStartAttempt() {
-            if (LOGD) Log.d(TAG, "onServiceStartAttempt");
-            // poke the setting to update the uid
-            mVolumeControllerService.setComponent(mVolumeControllerService.getComponent());
-            setDefaultVolumeController(false);
-            getVolumeComponent().dismissNow();
-            mRestorationNotification.show();
-            return 0;
-        }
-    }
-
-    private final class Receiver extends BroadcastReceiver {
-        private static final String ENABLE = "com.android.systemui.vui.ENABLE";
-        private static final String DISABLE = "com.android.systemui.vui.DISABLE";
-        private static final String EXTRA_COMPONENT = "component";
-
-        private static final String PREF = "com.android.systemui.PREF";
-        private static final String EXTRA_KEY = "key";
-        private static final String EXTRA_VALUE = "value";
-
-        public void start() {
-            final IntentFilter filter = new IntentFilter();
-            filter.addAction(ENABLE);
-            filter.addAction(DISABLE);
-            filter.addAction(PREF);
-            mContext.registerReceiver(this, filter, null, mHandler);
-        }
-
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            final String action = intent.getAction();
-            if (PREF.equals(action)) {
-                final String key = intent.getStringExtra(EXTRA_KEY);
-                if (key != null && intent.getExtras() != null) {
-                    final Object value = intent.getExtras().get(EXTRA_VALUE);
-                    if (value == null) {
-                        Prefs.remove(mContext, key);
-                    } else if (value instanceof Boolean) {
-                        Prefs.putBoolean(mContext, key, (Boolean) value);
-                    } else if (value instanceof Integer) {
-                        Prefs.putInt(mContext, key, (Integer) value);
-                    } else if (value instanceof Long) {
-                        Prefs.putLong(mContext, key, (Long) value);
-                    }
-                }
-                return;
-            }
-            final ComponentName component = intent.getParcelableExtra(EXTRA_COMPONENT);
-            final boolean current = component != null
-                    && component.equals(mVolumeControllerService.getComponent());
-            if (ENABLE.equals(action) && component != null) {
-                if (!current) {
-                    showServiceActivationDialog(component);
-                }
-            }
-            if (DISABLE.equals(action) && component != null) {
-                if (current) {
-                    mVolumeControllerService.setComponent(null);
-                }
-            }
-        }
-    }
-
-    private final class RestorationNotification {
-        public void hide() {
-            mNotificationManager.cancel(R.id.notification_volumeui);
-        }
-
-        public void show() {
-            final ComponentName component = mVolumeControllerService.getComponent();
-            if (component == null) {
-                Log.w(TAG, "Not showing restoration notification, component not active");
-                return;
-            }
-            final Intent intent =  new Intent(Receiver.DISABLE)
-                    .putExtra(Receiver.EXTRA_COMPONENT, component);
-            Notification.Builder builder = new Notification.Builder(mContext)
-                    .setSmallIcon(R.drawable.ic_volume_media)
-                    .setWhen(0)
-                    .setShowWhen(false)
-                    .setOngoing(true)
-                    .setContentTitle(mContext.getString(
-                            R.string.volumeui_notification_title, getAppLabel(component)))
-                    .setContentText(mContext.getString(R.string.volumeui_notification_text))
-                    .setContentIntent(PendingIntent.getBroadcast(mContext, 0, intent,
-                            PendingIntent.FLAG_UPDATE_CURRENT))
-                    .setPriority(Notification.PRIORITY_MIN)
-                    .setVisibility(Notification.VISIBILITY_PUBLIC)
-                    .setColor(mContext.getColor(
-                            com.android.internal.R.color.system_notification_accent_color));
-            overrideNotificationAppName(mContext, builder);
-            mNotificationManager.notify(R.id.notification_volumeui,
-                    builder.build());
-        }
+    private void setDefaultVolumeController() {
+        DndTile.setVisible(mContext, true);
+        if (LOGD) Log.d(TAG, "Registering default volume controller");
+        getVolumeComponent().register();
     }
 }