Home | History | Annotate | Download | only in stubs
      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 static org.junit.Assert.assertEquals;
     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.content.pm.ServiceInfo;
     28 import android.os.Bundle;
     29 import android.os.IBinder;
     30 import android.util.Log;
     31 import android.app.stubs.R;
     32 
     33 import com.android.compatibility.common.util.IBinderParcelable;
     34 
     35 /**
     36  * Foreground Service with location type.
     37  */
     38 public class LocalForegroundServiceLocation extends LocalForegroundService {
     39 
     40     private static final String TAG = "LocalForegroundServiceLocation";
     41     private static final String NOTIFICATION_CHANNEL_ID = "cts/" + TAG;
     42     public static final String EXTRA_FOREGROUND_SERVICE_TYPE = "ForegroundService.type";
     43     public static final int COMMAND_START_FOREGROUND_WITH_TYPE = 1;
     44     private int mNotificationId = 10;
     45 
     46     /** Returns the channel id for this service */
     47     @Override
     48     protected String getNotificationChannelId() {
     49         return NOTIFICATION_CHANNEL_ID;
     50     }
     51 
     52     @Override
     53     public void onStart(Intent intent, int startId) {
     54         String notificationChannelId = getNotificationChannelId();
     55         NotificationManager notificationManager = getSystemService(NotificationManager.class);
     56         notificationManager.createNotificationChannel(new NotificationChannel(
     57             notificationChannelId, notificationChannelId,
     58             NotificationManager.IMPORTANCE_DEFAULT));
     59 
     60         Context context = getApplicationContext();
     61         int command = intent.getIntExtra(EXTRA_COMMAND, -1);
     62 
     63         switch (command) {
     64             case COMMAND_START_FOREGROUND_WITH_TYPE:
     65                 final int type = intent.getIntExtra(EXTRA_FOREGROUND_SERVICE_TYPE,
     66                         ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST);
     67                 mNotificationId ++;
     68                 final Notification notification =
     69                     new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
     70                         .setContentTitle(getNotificationTitle(mNotificationId))
     71                         .setSmallIcon(R.drawable.black)
     72                         .build();
     73                 startForeground(mNotificationId, notification, type);
     74                 assertEquals(type, getForegroundServiceType());
     75                 break;
     76             default:
     77                 Log.e(TAG, "Unknown command: " + command);
     78         }
     79     }
     80 }
     81