recycleしすぎてた?
[ofnhwx.olib.git] / src / jp / gr / java_conf / ofnhwx / olib / base / BaseService.java
blob765bd9f6998629ef9b6f8ab55e20b7c2052c7ef9
1 package jp.gr.java_conf.ofnhwx.olib.base;
3 import java.lang.reflect.Method;
4 import java.util.HashMap;
5 import java.util.Map;
7 import android.app.Notification;
8 import android.app.Service;
10 /**
11 * サービスの互換性対策.
12 * @author yuta
14 public abstract class BaseService extends Service {
16 private static final Class<?>[] setForegroundSignature = new Class[] { boolean.class };
17 private static final Class<?>[] startForegroundSignature = new Class[] { int.class, Notification.class };
18 private static final Class<?>[] stopForegroundSignature = new Class[] { boolean.class };
20 private Method setForeground;
21 private Method startForeground;
22 private Method stopForeground;
24 private Object[] setForegroundArgs = new Object[1];
25 private Object[] startForegroundArgs = new Object[2];
26 private Object[] stopForegroundArgs = new Object[1];
28 private static Map<String, BaseService> services = new HashMap<String, BaseService>();
29 private boolean isForeground;
31 @Override
32 public void onCreate() {
33 super.onCreate();
34 try {
35 startForeground = getClass().getMethod("startForeground", startForegroundSignature);
36 stopForeground = getClass().getMethod("stopForeground" , stopForegroundSignature );
37 } catch (Exception e) {
38 startForeground = null;
39 stopForeground = null;
41 try {
42 setForeground = getClass().getMethod("setForeground", setForegroundSignature);
43 } catch (Exception e) {
44 setForeground = null;
46 services.put(getClass().getName(), this);
49 @Override
50 public void onDestroy() {
51 super.onDestroy();
52 services.put(getClass().getName(), this);
55 /**
56 * {@link #isForeground(Class)}を参照.
58 public boolean isForeground() {
59 return isForeground(getClass());
62 /**
63 * {@link #startForegroundCompat(Class, int, Notification)}を参照.
65 public void startForegroundCompat(int id, Notification notification) {
66 startForegroundCompat(getClass(), id, notification);
69 /**
70 * {@link #stopForegroundCompat(Class)}を参照.
72 public void stopForegroundCompat() {
73 stopForegroundCompat(getClass());
76 /**
77 * サービスのフォアグラウンド設定を取得.
78 * @param clazz {@link OService#getClass()}
79 * @return true:サビースはフォアグラウンドに設定されている
81 public static final boolean isForeground(Class<? extends BaseService> clazz) {
82 BaseService service = services.get(clazz.getName());
83 if (service == null) {
84 return false;
85 } else {
86 return service.isForeground;
90 /**
91 * サービスをフォアグラウンドにする.
92 * @param clazz {@link OService#getClass()}
93 * @param id 通知のID
94 * @param notification 通知オブジェクト
96 public static final void startForegroundCompat(Class<? extends BaseService> clazz, int id, Notification notification) {
97 BaseService service = services.get(clazz.getName());
98 if (service == null || service.isForeground) {
99 return;
101 try {
102 if (service.startForeground != null) {
103 service.startForegroundArgs[0] = Integer.valueOf(id);
104 service.startForegroundArgs[1] = notification;
105 service.startForeground.invoke(service, service.startForegroundArgs);
106 return;
108 service.setForegroundArgs[0] = Boolean.TRUE;
109 service.setForeground.invoke(service, service.setForegroundArgs);
110 } catch (Exception e) {
111 // do nothing
112 } finally {
113 service.isForeground = true;
118 * サービスのフォアグラウンド設定を解除する.
119 * @param clazz {@link OService#getClass()}
121 public static final void stopForegroundCompat(Class<? extends BaseService> clazz) {
122 BaseService service = services.get(clazz.getName());
123 if (service == null || !service.isForeground) {
124 return;
126 try {
127 if (service.stopForeground != null) {
128 service.stopForegroundArgs[0] = Boolean.TRUE;
129 service.stopForeground.invoke(service, service.stopForegroundArgs);
130 return;
132 service.setForegroundArgs[0] = Boolean.FALSE;
133 service.setForeground.invoke(service, service.setForegroundArgs);
134 } catch (Exception e) {
135 // do nothing
136 } finally {
137 service.isForeground = false;