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 
17 package android.jobscheduler.cts.jobtestapp;
18 
19 import static android.jobscheduler.cts.jobtestapp.TestJobSchedulerReceiver.PACKAGE_NAME;
20 
21 import android.app.Notification;
22 import android.app.NotificationChannel;
23 import android.app.NotificationManager;
24 import android.app.Service;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.content.pm.ServiceInfo;
30 import android.os.IBinder;
31 import android.util.Log;
32 
33 public class TestFgsService extends Service {
34     private static final String TAG = TestFgsService.class.getSimpleName();
35     public static final String ACTION_START_FGS = PACKAGE_NAME + ".action.START_FGS";
36     public static final String ACTION_FGS_STARTED = PACKAGE_NAME + ".action.FGS_STARTED";
37     public static final String ACTION_STOP_FOREGROUND = PACKAGE_NAME + ".action.STOP_FOREGROUND";
38 
39     private final BroadcastReceiver mStopReceiver = new BroadcastReceiver() {
40         @Override
41         public void onReceive(Context context, Intent intent) {
42             stopForeground(Service.STOP_FOREGROUND_REMOVE);
43             unregisterReceiver(mStopReceiver);
44             stopSelf();
45         }
46     };
47 
48     @Override
onStartCommand(Intent intent, int flags, int startId)49     public int onStartCommand(Intent intent, int flags, int startId) {
50         Log.i(TAG, "Test fgs onStartCommand");
51         final NotificationManager notificationManager = getSystemService(NotificationManager.class);
52         final NotificationChannel channel =
53                 new NotificationChannel(TAG, TAG, NotificationManager.IMPORTANCE_DEFAULT);
54         notificationManager.createNotificationChannel(channel);
55         final Notification notification = new Notification.Builder(this, TAG)
56                 .setContentTitle("Test")
57                 .setSmallIcon(android.R.mipmap.sym_def_app_icon)
58                 .setContentText(TAG)
59                 .build();
60         startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE);
61 
62         registerReceiver(mStopReceiver, new IntentFilter(ACTION_STOP_FOREGROUND),
63                 Context.RECEIVER_EXPORTED);
64 
65         final Intent reportFgsStartIntent = new Intent(ACTION_FGS_STARTED);
66         sendBroadcast(reportFgsStartIntent);
67         return START_STICKY;
68     }
69 
70     @Override
onBind(Intent intent)71     public IBinder onBind(Intent intent) {
72         return null;
73     }
74 }
75