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