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 package com.android.cts.net.hostside.app2; 17 18 import static com.android.cts.net.hostside.app2.Common.TAG; 19 import static com.android.cts.net.hostside.app2.Common.TEST_PKG; 20 21 import android.R; 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.os.AsyncTask; 28 import android.os.Bundle; 29 import android.os.IBinder; 30 import android.os.RemoteException; 31 import android.util.Log; 32 33 import com.android.cts.net.hostside.INetworkStateObserver; 34 35 /** 36 * Service used to change app state to FOREGROUND_SERVICE. 37 */ 38 public class MyForegroundService extends Service { 39 private static final String NOTIFICATION_CHANNEL_ID = "cts/MyForegroundService"; 40 private static final int FLAG_START_FOREGROUND = 1; 41 private static final int FLAG_STOP_FOREGROUND = 2; 42 43 @Override 44 public IBinder onBind(Intent intent) { 45 return null; 46 } 47 48 @Override 49 public int onStartCommand(Intent intent, int flags, int startId) { 50 Log.v(TAG, "MyForegroundService.onStartCommand(): " + intent); 51 NotificationManager notificationManager = getSystemService(NotificationManager.class); 52 notificationManager.createNotificationChannel(new NotificationChannel( 53 NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID, 54 NotificationManager.IMPORTANCE_DEFAULT)); 55 switch (intent.getFlags()) { 56 case FLAG_START_FOREGROUND: 57 Log.d(TAG, "Starting foreground"); 58 startForeground(42, new Notification.Builder(this, NOTIFICATION_CHANNEL_ID) 59 .setSmallIcon(R.drawable.ic_dialog_alert) // any icon is fine 60 .build()); 61 Common.notifyNetworkStateObserver(this, intent); 62 break; 63 case FLAG_STOP_FOREGROUND: 64 Log.d(TAG, "Stopping foreground"); 65 stopForeground(true); 66 break; 67 default: 68 Log.wtf(TAG, "Invalid flag on intent " + intent); 69 } 70 return START_STICKY; 71 } 72 } 73