1 /* 2 * Copyright (C) 2016 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.Notification; 20 import android.app.NotificationChannel; 21 import android.app.NotificationManager; 22 import android.app.Service; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.os.IBinder; 27 import android.util.Log; 28 import android.app.stubs.R; 29 30 import com.android.compatibility.common.util.IBinderParcelable; 31 32 public class LocalForegroundService extends LocalService { 33 34 private static final String TAG = "LocalForegroundService"; 35 private static final String EXTRA_COMMAND = "LocalForegroundService.command"; 36 private static final String NOTIFICATION_CHANNEL_ID = "cts/" + TAG; 37 38 public static final int COMMAND_START_FOREGROUND = 1; 39 public static final int COMMAND_STOP_FOREGROUND_REMOVE_NOTIFICATION = 2; 40 public static final int COMMAND_STOP_FOREGROUND_DONT_REMOVE_NOTIFICATION = 3; 41 public static final int COMMAND_STOP_FOREGROUND_DETACH_NOTIFICATION = 4; 42 public static final int COMMAND_STOP_FOREGROUND_REMOVE_NOTIFICATION_USING_FLAGS = 5; 43 public static final int COMMAND_START_NO_FOREGROUND = 6; 44 45 private int mNotificationId = 0; 46 47 @Override 48 public void onCreate() { 49 super.onCreate(); 50 Log.d(TAG, "service created: " + this + " in " + android.os.Process.myPid()); 51 } 52 53 @Override 54 public void onStart(Intent intent, int startId) { 55 NotificationManager notificationManager = getSystemService(NotificationManager.class); 56 notificationManager.createNotificationChannel(new NotificationChannel( 57 NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID, 58 NotificationManager.IMPORTANCE_DEFAULT)); 59 60 Context context = getApplicationContext(); 61 int command = intent.getIntExtra(EXTRA_COMMAND, -1); 62 63 Log.d(TAG, "service start cmd " + command + ", intent " + intent); 64 65 switch (command) { 66 case COMMAND_START_FOREGROUND: 67 mNotificationId ++; 68 Log.d(TAG, "Starting foreground using notification " + mNotificationId); 69 Notification notification = 70 new Notification.Builder(context, NOTIFICATION_CHANNEL_ID) 71 .setContentTitle(getNotificationTitle(mNotificationId)) 72 .setSmallIcon(R.drawable.black) 73 .build(); 74 startForeground(mNotificationId, notification); 75 break; 76 case COMMAND_STOP_FOREGROUND_REMOVE_NOTIFICATION: 77 Log.d(TAG, "Stopping foreground removing notification"); 78 stopForeground(true); 79 break; 80 case COMMAND_STOP_FOREGROUND_DONT_REMOVE_NOTIFICATION: 81 Log.d(TAG, "Stopping foreground without removing notification"); 82 stopForeground(false); 83 break; 84 case COMMAND_STOP_FOREGROUND_REMOVE_NOTIFICATION_USING_FLAGS: 85 Log.d(TAG, "Stopping foreground removing notification using flags"); 86 stopForeground(Service.STOP_FOREGROUND_REMOVE | Service.STOP_FOREGROUND_DETACH); 87 break; 88 case COMMAND_STOP_FOREGROUND_DETACH_NOTIFICATION: 89 Log.d(TAG, "Detaching foreground service notification"); 90 stopForeground(Service.STOP_FOREGROUND_DETACH); 91 break; 92 case COMMAND_START_NO_FOREGROUND: 93 Log.d(TAG, "Starting without calling startForeground()"); 94 break; 95 default: 96 Log.e(TAG, "Unknown command: " + command); 97 } 98 99 // Do parent's onStart at the end, so we don't race with the test code waiting for us to 100 // execute. 101 super.onStart(intent, startId); 102 } 103 104 @Override 105 public void onDestroy() { 106 Log.d(TAG, "service destroyed: " + this + " in " + android.os.Process.myPid()); 107 super.onDestroy(); 108 } 109 110 public static Bundle newCommand(IBinder stateReceiver, int command) { 111 Bundle bundle = new Bundle(); 112 bundle.putParcelable(LocalService.REPORT_OBJ_NAME, new IBinderParcelable(stateReceiver)); 113 bundle.putInt(EXTRA_COMMAND, command); 114 return bundle; 115 } 116 117 public static String getNotificationTitle(int id) { 118 return "I AM FOREGROOT #" + id; 119 } 120 } 121