1 /* 2 * Copyright 2019 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 package android.media.cts; 17 18 import static android.media.cts.ForegroundServiceUtil.EXTRA_MESSENGER; 19 import static android.media.cts.ForegroundServiceUtil.MSG_SERVICE_DESTROYED; 20 import static android.media.cts.ForegroundServiceUtil.MSG_START_FOREGROUND_DONE; 21 22 import android.app.Notification; 23 import android.app.NotificationChannel; 24 import android.app.NotificationManager; 25 import android.app.Service; 26 import android.content.Intent; 27 import android.graphics.Bitmap; 28 import android.graphics.Canvas; 29 import android.graphics.Color; 30 import android.graphics.drawable.Icon; 31 import android.os.IBinder; 32 import android.os.Message; 33 import android.os.Messenger; 34 import android.os.RemoteException; 35 36 public class LocalMediaProjectionService extends Service { 37 private static final String TAG = "LocalMediaProjectionService"; 38 39 private Bitmap mTestBitmap; 40 41 private static final String NOTIFICATION_CHANNEL_ID = "AudioPlaybackCaptureTest"; 42 private static final String CHANNEL_NAME = "ProjectionService"; 43 private static final int NOTIFICATION_ID = 1; 44 45 Messenger mMessenger; 46 47 @Override onStartCommand(Intent intent, int flags, int startId)48 public int onStartCommand(Intent intent, int flags, int startId) { 49 mMessenger = intent.getParcelableExtra(EXTRA_MESSENGER); 50 startForeground(); 51 return super.onStartCommand(intent, flags, startId); 52 } 53 54 @Override onBind(Intent intent)55 public IBinder onBind(Intent intent) { 56 return null; 57 } 58 59 @Override onDestroy()60 public void onDestroy() { 61 if (mTestBitmap != null) { 62 mTestBitmap.recycle(); 63 mTestBitmap = null; 64 } 65 sendMessage(MSG_SERVICE_DESTROYED); 66 super.onDestroy(); 67 } 68 createNotificationIcon()69 private Icon createNotificationIcon() { 70 mTestBitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); 71 final Canvas canvas = new Canvas(mTestBitmap); 72 canvas.drawColor(Color.BLUE); 73 return Icon.createWithBitmap(mTestBitmap); 74 } 75 startForeground()76 private void startForeground() { 77 final NotificationChannel channel = new NotificationChannel(getNotificationChannelId(), 78 getNotificationChannelName(), NotificationManager.IMPORTANCE_NONE); 79 channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); 80 81 final NotificationManager notificationManager = 82 getSystemService(NotificationManager.class); 83 notificationManager.createNotificationChannel(channel); 84 85 final Notification.Builder notificationBuilder = 86 new Notification.Builder(this, getNotificationChannelId()); 87 88 final Notification notification = notificationBuilder.setOngoing(true) 89 .setContentTitle("App is running") 90 .setSmallIcon(createNotificationIcon()) 91 .setCategory(Notification.CATEGORY_SERVICE) 92 .setContentText("Context") 93 .build(); 94 95 startForeground(getNotificationId(), notification); 96 97 sendMessage(MSG_START_FOREGROUND_DONE); 98 } 99 sendMessage(int what)100 void sendMessage(int what) { 101 final Message msg = Message.obtain(); 102 msg.what = what; 103 try { 104 mMessenger.send(msg); 105 } catch (RemoteException e) { 106 } 107 } 108 getNotificationId()109 int getNotificationId() { 110 return NOTIFICATION_ID; 111 } 112 getNotificationChannelId()113 String getNotificationChannelId() { 114 return NOTIFICATION_CHANNEL_ID; 115 } 116 getNotificationChannelName()117 String getNotificationChannelName() { 118 return CHANNEL_NAME; 119 } 120 } 121