1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.media.app.media_session_test_helper; 18 19 import android.app.Notification; 20 import android.app.NotificationChannel; 21 import android.app.NotificationManager; 22 import android.app.Service; 23 import android.content.Intent; 24 import android.media.MediaSession2; 25 import android.media.session.MediaSession; 26 import android.os.IBinder; 27 import android.text.TextUtils; 28 import android.util.Log; 29 30 import android.media.cts.MediaSessionTestHelperConstants; 31 32 /** 33 * Service of the media session's host-side CTS helper. 34 * <p>This is the foreground service to prevent this process from being killed by the OOM killer 35 * while the host-side tests are running. 36 */ 37 public class MediaSessionTestHelperService extends Service { 38 private static final String TAG = "MediaSessionTestHelperService"; 39 40 private static final int NOTIFICATION_ID = 100; 41 private static final String NOTIFICATION_CHANNEL = TAG; 42 43 private MediaSession mMediaSession; 44 private MediaSession2 mMediaSession2; 45 46 @Override onCreate()47 public void onCreate() { 48 super.onCreate(); 49 50 // Build notification UI to make this a foreground service. 51 NotificationManager manager = 52 (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 53 NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL, 54 getString(R.string.label), NotificationManager.IMPORTANCE_DEFAULT); 55 manager.createNotificationChannel(notificationChannel); 56 57 Notification notification = new Notification.Builder(this, NOTIFICATION_CHANNEL) 58 .setSmallIcon(android.R.drawable.sym_def_app_icon) 59 .setContentTitle(getString(R.string.label)).build(); 60 startForeground(NOTIFICATION_ID, notification); 61 } 62 63 @Override onStartCommand(Intent intent, int flags, int startId)64 public int onStartCommand(Intent intent, int flags, int startId) { 65 super.onStartCommand(intent, flags, startId); 66 if (!TextUtils.equals(intent.getAction(), MediaSessionTestHelperConstants.ACTION_CONTROL)) { 67 Log.e(TAG, "Invalid action " + intent.getAction() + ". Test may fail"); 68 return START_STICKY; 69 } 70 int flag = intent.getIntExtra(MediaSessionTestHelperConstants.EXTRA_CONTROL_COMMAND, 0); 71 if ((flag & MediaSessionTestHelperConstants.FLAG_CREATE_MEDIA_SESSION) != 0) { 72 if (mMediaSession == null) { 73 mMediaSession = new MediaSession(this, TAG); 74 } 75 } 76 if (mMediaSession != null) { 77 if ((flag & MediaSessionTestHelperConstants.FLAG_SET_MEDIA_SESSION_ACTIVE) != 0) { 78 mMediaSession.setActive(true); 79 } 80 if ((flag & MediaSessionTestHelperConstants.FLAG_SET_MEDIA_SESSION_INACTIVE) != 0) { 81 mMediaSession.setActive(false); 82 } 83 } 84 if ((flag & MediaSessionTestHelperConstants.FLAG_CREATE_MEDIA_SESSION2) != 0) { 85 if (mMediaSession2 == null) { 86 mMediaSession2 = new MediaSession2.Builder(this).setId(TAG).build(); 87 } 88 } 89 if ((flag & MediaSessionTestHelperConstants.FLAG_RELEASE_ALL_MEDIA_SESSION) != 0) { 90 releaseAllMediaSession(); 91 } 92 return START_STICKY; 93 } 94 95 @Override onBind(Intent intent)96 public IBinder onBind(Intent intent) { 97 // It's not a bind service. 98 return null; 99 } 100 101 @Override onDestroy()102 public void onDestroy() { 103 super.onDestroy(); 104 releaseAllMediaSession(); 105 } 106 releaseAllMediaSession()107 private void releaseAllMediaSession() { 108 if (mMediaSession != null) { 109 mMediaSession.release(); 110 mMediaSession = null; 111 } 112 } 113 } 114