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.view.cts.surfacevalidator;
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.Binder;
29 import android.os.IBinder;
30 
31 public class LocalMediaProjectionService extends Service {
32 
33     private final IBinder mBinder = new LocalBinder();
34     private Bitmap mTestBitmap;
35 
36     private static final String NOTIFICATION_CHANNEL_ID = "Surfacevalidator";
37     private static final String CHANNEL_NAME = "ProjectionService";
38 
39     public class LocalBinder extends Binder {
getService()40         LocalMediaProjectionService getService() {
41             return LocalMediaProjectionService.this;
42         }
43     }
44 
45     @Override
onStartCommand(Intent intent, int flags, int startId)46     public int onStartCommand(Intent intent, int flags, int startId) {
47         startForeground();
48         return super.onStartCommand(intent, flags, startId);
49     }
50 
51     @Override
onBind(Intent intent)52     public IBinder onBind(Intent intent) {
53         return mBinder;
54     }
55 
56     @Override
onDestroy()57     public void onDestroy() {
58         if (mTestBitmap != null) {
59             mTestBitmap.recycle();
60             mTestBitmap = null;
61         }
62         super.onDestroy();
63     }
64 
createNotificationIcon()65     private Icon createNotificationIcon() {
66         mTestBitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
67         final Canvas canvas = new Canvas(mTestBitmap);
68         canvas.drawColor(Color.BLUE);
69         return Icon.createWithBitmap(mTestBitmap);
70     }
71 
startForeground()72     private void startForeground() {
73         final NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
74                 CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE);
75         channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
76 
77         final NotificationManager notificationManager =
78                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
79         notificationManager.createNotificationChannel(channel);
80 
81         final Notification.Builder notificationBuilder =
82                 new Notification.Builder(this, NOTIFICATION_CHANNEL_ID);
83 
84         final Notification notification = notificationBuilder.setOngoing(true)
85                 .setContentTitle("App is running")
86                 .setSmallIcon(createNotificationIcon())
87                 .setCategory(Notification.CATEGORY_SERVICE)
88                 .setContentText("Context")
89                 .build();
90 
91         startForeground(2, notification);
92     }
93 
94 }
95