summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java
blob: fa0de7d272c8f9b6c8c4e665bec9518e77f7206f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (C) 2014 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.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.pm.ActivityInfo;
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;

import java.io.FileDescriptor;
import java.io.PrintWriter;

public class VolumeUI extends SystemUI {
    private static final String TAG = "VolumeUI";
    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;

    private Configuration mConfiguration;

    @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();
        mConfiguration = new Configuration(mContext.getResources().getConfiguration());
    }

    private VolumeComponent getVolumeComponent() {
        return mVolumeComponent;
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (!mEnabled) return;
        getVolumeComponent().onConfigurationChanged(newConfig);

        if (isThemeChange(newConfig)) {
            mContext.recreateTheme();
            mVolumeComponent.recreateDialog();
        }
        mConfiguration.setTo(newConfig);
    }

    private boolean isThemeChange(Configuration newConfig) {
        if (mConfiguration != null) {
            int changes = mConfiguration.updateFrom(newConfig);
            return (changes & ActivityInfo.CONFIG_THEME_RESOURCE) != 0;
        }
        return false;
    }

    @Override
    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);
            mNotificationManager.notify(R.id.notification_volumeui,
                    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))
                            .build());
        }
    }
}