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.os.cts; 18 19 import static android.os.PowerManager.PARTIAL_WAKE_LOCK; 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.Context; 26 import android.content.Intent; 27 import android.os.IBinder; 28 import android.os.PowerManager; 29 30 public class LowPowerStandbyForegroundService extends Service { 31 private static final int ID = 1; 32 private static final String EXTRA_FGS_TYPE = "EXTRA_FGS_TYPE"; 33 34 private PowerManager.WakeLock mTestWakeLock; 35 36 @Override onBind(Intent intent)37 public IBinder onBind(Intent intent) { 38 return null; 39 } 40 41 @Override onStartCommand(Intent intent, int flags, int startId)42 public int onStartCommand(Intent intent, int flags, int startId) { 43 NotificationManager notificationManager = getSystemService(NotificationManager.class); 44 NotificationChannel notificationChannel = new NotificationChannel("fgsNotifChannel", 45 "fgsNotifChannel", NotificationManager.IMPORTANCE_DEFAULT); 46 notificationManager.createNotificationChannel(notificationChannel); 47 48 Notification notification = new Notification.Builder(this, notificationChannel.getId()) 49 .setContentTitle("ForegroundService") 50 .setContentText("Running") 51 .setSmallIcon(android.R.drawable.ic_info) 52 .build(); 53 54 startForeground(ID, notification, intent.getIntExtra(EXTRA_FGS_TYPE, 0)); 55 56 PowerManager powerManager = getApplicationContext().getSystemService(PowerManager.class); 57 mTestWakeLock = powerManager.newWakeLock(PARTIAL_WAKE_LOCK, 58 LowPowerStandbyTest.TEST_WAKE_LOCK_TAG); 59 mTestWakeLock.acquire(10000); 60 61 return START_NOT_STICKY; 62 } 63 64 @Override onDestroy()65 public void onDestroy() { 66 super.onDestroy(); 67 if (mTestWakeLock != null && mTestWakeLock.isHeld()) { 68 mTestWakeLock.release(); 69 } 70 } 71 createIntentWithForegroundServiceType(Context context, int fgsType)72 public static Intent createIntentWithForegroundServiceType(Context context, int fgsType) { 73 Intent intent = new Intent(context, LowPowerStandbyForegroundService.class); 74 intent.putExtra(EXTRA_FGS_TYPE, fgsType); 75 return intent; 76 } 77 } 78