1 /* 2 * Copyright 2018 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 androidx.media; 18 19 import android.app.Notification; 20 import android.app.NotificationChannel; 21 import android.app.NotificationManager; 22 import android.content.Context; 23 24 import androidx.media.MediaSession2.SessionCallback; 25 import androidx.media.TestUtils.SyncHandler; 26 27 import java.util.concurrent.Executor; 28 29 /** 30 * Mock implementation of {@link MediaSessionService2} for testing. 31 */ 32 public class MockMediaSessionService2 extends MediaSessionService2 { 33 // Keep in sync with the AndroidManifest.xml 34 public static final String ID = "TestSession"; 35 36 private static final String DEFAULT_MEDIA_NOTIFICATION_CHANNEL_ID = "media_session_service"; 37 private static final int DEFAULT_MEDIA_NOTIFICATION_ID = 1001; 38 39 private NotificationChannel mDefaultNotificationChannel; 40 private MediaSession2 mSession; 41 private NotificationManager mNotificationManager; 42 43 @Override 44 public void onCreate() { 45 TestServiceRegistry.getInstance().setServiceInstance(this); 46 super.onCreate(); 47 mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 48 } 49 50 @Override 51 public MediaSession2 onCreateSession(String sessionId) { 52 final MockPlayer player = new MockPlayer(1); 53 final SyncHandler handler = (SyncHandler) TestServiceRegistry.getInstance().getHandler(); 54 final Executor executor = new Executor() { 55 @Override 56 public void execute(Runnable runnable) { 57 handler.post(runnable); 58 } 59 }; 60 SessionCallback sessionCallback = TestServiceRegistry.getInstance().getSessionCallback(); 61 if (sessionCallback == null) { 62 // Ensures non-null 63 sessionCallback = new SessionCallback() {}; 64 } 65 mSession = new MediaSession2.Builder(this) 66 .setPlayer(player) 67 .setSessionCallback(executor, sessionCallback) 68 .setId(sessionId).build(); 69 return mSession; 70 } 71 72 @Override 73 public void onDestroy() { 74 super.onDestroy(); 75 TestServiceRegistry.getInstance().cleanUp(); 76 } 77 78 @Override 79 public MediaNotification onUpdateNotification() { 80 if (mDefaultNotificationChannel == null) { 81 mDefaultNotificationChannel = new NotificationChannel( 82 DEFAULT_MEDIA_NOTIFICATION_CHANNEL_ID, 83 DEFAULT_MEDIA_NOTIFICATION_CHANNEL_ID, 84 NotificationManager.IMPORTANCE_DEFAULT); 85 mNotificationManager.createNotificationChannel(mDefaultNotificationChannel); 86 } 87 Notification notification = new Notification.Builder( 88 this, DEFAULT_MEDIA_NOTIFICATION_CHANNEL_ID) 89 .setContentTitle(getPackageName()) 90 .setContentText("Dummt test notification") 91 .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); 92 return new MediaNotification(DEFAULT_MEDIA_NOTIFICATION_ID, notification); 93 } 94 } 95