Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2018 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 android.os.cts.batterysaving.app;
     17 
     18 import static android.os.cts.batterysaving.common.Values.KEY_REQUEST_FOREGROUND;
     19 
     20 import android.app.Notification;
     21 import android.app.NotificationChannel;
     22 import android.app.NotificationManager;
     23 import android.app.Service;
     24 import android.content.ComponentName;
     25 import android.content.Intent;
     26 import android.os.IBinder;
     27 import android.util.Log;
     28 
     29 import java.util.concurrent.atomic.AtomicReference;
     30 
     31 public class TestService extends Service {
     32     private static final String TAG = "TestService";
     33 
     34     public static final AtomicReference<Intent> LastStartIntent = new AtomicReference();
     35 
     36     private final String getNotificationChannelId() {
     37         return new ComponentName(this, TestService.class).toShortString();
     38     }
     39 
     40     @Override
     41     public int onStartCommand(Intent intent, int flags, int startId) {
     42 
     43         Log.d(TAG, "TestService.TestService: intent=" + intent);
     44 
     45         if (intent.getBooleanExtra(KEY_REQUEST_FOREGROUND, false)) {
     46             NotificationManager notificationManager = getSystemService(NotificationManager.class);
     47             notificationManager.createNotificationChannel(new NotificationChannel(
     48                     getNotificationChannelId(), getNotificationChannelId(),
     49                     NotificationManager.IMPORTANCE_DEFAULT));
     50             Notification notification =
     51                     new Notification.Builder(getApplicationContext(), getNotificationChannelId())
     52                             .setContentTitle("FgService")
     53                             .setSmallIcon(android.R.drawable.ic_popup_sync)
     54                             .build();
     55 
     56             startForeground(1, notification);
     57         }
     58 
     59         LastStartIntent.set(intent);
     60 
     61         stopSelf();
     62 
     63         return START_NOT_STICKY;
     64     }
     65 
     66     @Override
     67     public IBinder onBind(Intent intent) {
     68         return null;
     69     }
     70 }
     71