Home | History | Annotate | Download | only in throttling
      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 android.content.pm.cts.shortcutmanager.throttling;
     17 
     18 import android.app.Notification;
     19 import android.app.NotificationChannel;
     20 import android.app.NotificationManager;
     21 import android.app.Service;
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.pm.cts.shortcutmanager.common.Constants;
     26 import android.content.pm.cts.shortcutmanager.common.ReplyUtil;
     27 import android.os.IBinder;
     28 import android.util.Log;
     29 
     30 /**
     31  * Make sure that when a fg service is running, shortcut manager calls are not throttled.
     32  */
     33 public class FgService extends Service {
     34     private static final String NOTIFICATION_CHANNEL_ID = "cts/shortcutmanager/FgService";
     35 
     36     public static void start(Context context, String replyAction) {
     37         final Intent i =
     38                 new Intent().setComponent(new ComponentName(context, FgService.class))
     39                         .putExtra(Constants.EXTRA_REPLY_ACTION, replyAction);
     40         context.startService(i);
     41     }
     42 
     43     @Override
     44     public int onStartCommand(Intent intent, int flags, int startId) {
     45 
     46         // Start as foreground.
     47         NotificationManager notificationManager = getSystemService(NotificationManager.class);
     48         notificationManager.createNotificationChannel(new NotificationChannel(
     49                 NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID,
     50                 NotificationManager.IMPORTANCE_DEFAULT));
     51         Notification notification =
     52                 new Notification.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID)
     53                         .setContentTitle("FgService")
     54                         .setSmallIcon(android.R.drawable.ic_popup_sync)
     55                         .build();
     56         startForeground(1, notification);
     57 
     58         final String replyAction = intent.getStringExtra(Constants.EXTRA_REPLY_ACTION);
     59 
     60         Log.i(ThrottledTests.TAG, Constants.TEST_FG_SERVICE_UNTHROTTLED);
     61 
     62         // Actual test.
     63         ReplyUtil.runTestAndReply(this, replyAction, () -> {
     64             ThrottledTests.assertCallNotThrottled(this);
     65         });
     66 
     67         // Stop self.
     68         stopForeground(true);
     69         stopSelf();
     70 
     71         return Service.START_NOT_STICKY;
     72     }
     73 
     74     @Override
     75     public IBinder onBind(Intent intent) {
     76         return null;
     77     }
     78 }
     79