• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 =
78                 new NotificationChannel(
79                         getNotificationChannelId(),
80                         getNotificationChannelName(),
81                         NotificationManager.IMPORTANCE_NONE);
82         channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
83 
84         final NotificationManager notificationManager = getSystemService(NotificationManager.class);
85         notificationManager.createNotificationChannel(channel);
86 
87         final Notification.Builder notificationBuilder =
88                 new Notification.Builder(this, getNotificationChannelId());
89 
90         final Notification notification =
91                 notificationBuilder
92                         .setOngoing(true)
93                         .setContentTitle("App is running")
94                         .setSmallIcon(createNotificationIcon())
95                         .setCategory(Notification.CATEGORY_SERVICE)
96                         .setContentText("Context")
97                         .build();
98 
99         startForeground(getNotificationId(), notification);
100 
101         sendMessage(MSG_START_FOREGROUND_DONE);
102     }
103 
sendMessage(int what)104     void sendMessage(int what) {
105         final Message msg = Message.obtain();
106         msg.what = what;
107         try {
108             mMessenger.send(msg);
109         } catch (RemoteException e) {
110         }
111     }
112 
getNotificationId()113     int getNotificationId() {
114         return NOTIFICATION_ID;
115     }
116 
getNotificationChannelId()117     String getNotificationChannelId() {
118         return NOTIFICATION_CHANNEL_ID;
119     }
120 
getNotificationChannelName()121     String getNotificationChannelName() {
122         return CHANNEL_NAME;
123     }
124 }
125