• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * 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 {
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    */
setForeground(Service service, Integer notificationId, Notification notification)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