1 /*
2  * Copyright (C) 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 
17 package android.app.stubs;
18 
19 import android.app.ForegroundServiceStartNotAllowedException;
20 import android.app.Notification;
21 import android.app.NotificationChannel;
22 import android.app.NotificationManager;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.ServiceInfo;
26 import android.util.Log;
27 
28 /**
29  * Foreground Service with location type.
30  */
31 public class LocalForegroundServiceLocation extends LocalForegroundService {
32 
33     private static final String TAG = "LocalForegroundServiceLocation";
34     private static final String NOTIFICATION_CHANNEL_ID = "cts/" + TAG;
35     public static final String EXTRA_FOREGROUND_SERVICE_TYPE = "ForegroundService.type";
36     public static final int COMMAND_START_FOREGROUND_WITH_TYPE = 1;
37     public static String ACTION_START_FGSL_RESULT =
38             "android.app.stubs.LocalForegroundServiceLocation.RESULT";
39     private int mNotificationId = 10;
40 
41     /** Returns the channel id for this service */
getNotificationChannelId()42     public static String getNotificationChannelId() {
43         return NOTIFICATION_CHANNEL_ID;
44     }
45 
46     @Override
onStartCommand(Intent intent, int flags, int startId)47     public int onStartCommand(Intent intent, int flags, int startId) {
48         String notificationChannelId = getNotificationChannelId();
49         NotificationManager notificationManager = getSystemService(NotificationManager.class);
50         notificationManager.createNotificationChannel(new NotificationChannel(
51             notificationChannelId, notificationChannelId,
52             NotificationManager.IMPORTANCE_DEFAULT));
53 
54         Context context = getApplicationContext();
55         int command = intent.getIntExtra(EXTRA_COMMAND, -1);
56         switch (command) {
57             case COMMAND_START_FOREGROUND_WITH_TYPE:
58                 final int type = intent.getIntExtra(EXTRA_FOREGROUND_SERVICE_TYPE,
59                         ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST);
60                 mNotificationId ++;
61                 final Notification notification =
62                     new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
63                         .setContentTitle(getNotificationTitle(mNotificationId))
64                         .setSmallIcon(R.drawable.black)
65                         .build();
66                 try {
67                     startForeground(mNotificationId, notification);
68                 } catch (ForegroundServiceStartNotAllowedException e) {
69                     Log.d(TAG, "startForeground gets an "
70                             + " ForegroundServiceStartNotAllowedException", e);
71                 }
72                 //assertEquals(type, getForegroundServiceType());
73                 break;
74             case COMMAND_STOP_FOREGROUND_REMOVE_NOTIFICATION:
75                 Log.d(TAG, "Stopping foreground removing notification");
76                 stopForeground(true);
77                 break;
78             case COMMAND_START_NO_FOREGROUND:
79                 Log.d(TAG, "Starting without calling startForeground()");
80                 break;
81             default:
82                 Log.e(TAG, "Unknown command: " + command);
83         }
84 
85         sendBroadcast(new Intent(ACTION_START_FGSL_RESULT)
86                 .setFlags(Intent.FLAG_RECEIVER_FOREGROUND));
87         return START_NOT_STICKY;
88     }
89 }
90