Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2017 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 com.googlecode.android_scripting.activity;
     18 
     19 import android.app.Notification;
     20 import android.app.NotificationManager;
     21 import android.app.Service;
     22 import android.content.Context;
     23 
     24 import com.googlecode.android_scripting.Log;
     25 
     26 import java.lang.reflect.InvocationTargetException;
     27 import java.lang.reflect.Method;
     28 
     29 /**
     30  * A utility class supplying helper methods for {@link Service} objects.
     31  *
     32  */
     33 public class ServiceUtils {
     34   private ServiceUtils() {
     35   }
     36 
     37   /**
     38    * Marks the service as a foreground service. This uses reflection to figure out whether the new
     39    * APIs for marking a service as a foreground service are available. If not, it falls back to the
     40    * old {@link #setForeground(boolean)} call.
     41    *
     42    * @param service
     43    *          the service to put in foreground mode
     44    * @param notificationId
     45    *          id of the notification to show
     46    * @param notification
     47    *          the notification to show
     48    */
     49   public static void setForeground(Service service, Integer notificationId,
     50       Notification notification) {
     51     final Class<?>[] startForegroundSignature = new Class[] { int.class, Notification.class };
     52     Method startForeground = null;
     53     try {
     54       startForeground = service.getClass().getMethod("startForeground", startForegroundSignature);
     55 
     56       try {
     57         startForeground.invoke(service, new Object[] { notificationId, notification });
     58       } catch (IllegalArgumentException e) {
     59         // Should not happen!
     60         Log.e("Could not set TriggerService to foreground mode.", e);
     61       } catch (IllegalAccessException e) {
     62         // Should not happen!
     63         Log.e("Could not set TriggerService to foreground mode.", e);
     64       } catch (InvocationTargetException e) {
     65         // Should not happen!
     66         Log.e("Could not set TriggerService to foreground mode.", e);
     67       }
     68 
     69     } catch (NoSuchMethodException e) {
     70       // Fall back on old API.
     71       // service.setForeground(true); //too old to be supported
     72 
     73       NotificationManager manager =
     74           (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
     75       manager.notify(notificationId, notification);
     76     }
     77   }
     78 }
     79