OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 14 matching lines...) Expand all Loading... |
25 import android.os.IBinder; | 25 import android.os.IBinder; |
26 import android.util.Log; | 26 import android.util.Log; |
27 | 27 |
28 public final class EngineService extends Service | 28 public final class EngineService extends Service |
29 { | 29 { |
30 private static final String TAG = EngineService.class.getSimpleName(); | 30 private static final String TAG = EngineService.class.getSimpleName(); |
31 | 31 |
32 private volatile Engine engine = null; | 32 private volatile Engine engine = null; |
33 private volatile boolean isInitialized = false; | 33 private volatile boolean isInitialized = false; |
34 private Throwable failureCause = null; | 34 private Throwable failureCause = null; |
35 private static final LinkedBlockingQueue<OnEngineCreatedCallback> ON_CREATED_C
ALLBACKS = | 35 private static final LinkedBlockingQueue<EngineCreatedCallbackWrapper> ON_CREA
TED_CALLBACKS = |
36 new LinkedBlockingQueue<EngineService.OnEngineCreatedCallback>(); | 36 new LinkedBlockingQueue<EngineCreatedCallbackWrapper>(); |
37 | 37 |
38 @Override | 38 @Override |
39 public int onStartCommand(Intent intent, int flags, int startId) | 39 public int onStartCommand(Intent intent, int flags, int startId) |
40 { | 40 { |
41 return Service.START_STICKY; | 41 return Service.START_STICKY; |
42 } | 42 } |
43 | 43 |
44 /** | 44 /** |
45 * The callback gets executed on the UI thread. | 45 * The callback gets executed on the UI thread. |
46 * | 46 * |
47 * @param context | 47 * @param context |
48 * @param callback | 48 * @param callback |
49 */ | 49 */ |
50 public static void startService(final Context context, final OnEngineCreatedCa
llback callback) | 50 public static void startService(final Context context, final OnEngineCreatedCa
llback callback) |
51 { | 51 { |
| 52 startService(context, callback, true); |
| 53 } |
| 54 |
| 55 /** |
| 56 * |
| 57 * @param context |
| 58 * @param callback |
| 59 * @param runOnUiThread |
| 60 * {@code true} if the callback should be executed on the UI thread |
| 61 */ |
| 62 public static void startService(final Context context, final OnEngineCreatedCa
llback callback, |
| 63 final boolean runOnUiThread) |
| 64 { |
52 context.startService(new Intent(context, EngineService.class)); | 65 context.startService(new Intent(context, EngineService.class)); |
53 ON_CREATED_CALLBACKS.offer(callback); | 66 ON_CREATED_CALLBACKS.offer(new EngineCreatedCallbackWrapper(callback, runOnU
iThread)); |
54 } | 67 } |
55 | 68 |
56 @Override | 69 @Override |
57 public void onCreate() | 70 public void onCreate() |
58 { | 71 { |
59 super.onCreate(); | 72 super.onCreate(); |
60 startDaemonThread(new Initializer(this)); | 73 startDaemonThread(new Initializer(this)); |
61 startDaemonThread(new CreationNotifier(this)); | 74 startDaemonThread(new CreationNotifier(this)); |
62 } | 75 } |
63 | 76 |
64 @Override | 77 @Override |
65 public IBinder onBind(Intent intent) | 78 public IBinder onBind(Intent intent) |
66 { | 79 { |
67 return null; | 80 return null; |
68 } | 81 } |
69 | 82 |
70 public interface OnEngineCreatedCallback | |
71 { | |
72 public void onEngineCreated(Engine engine, boolean success); | |
73 } | |
74 | |
75 private static void startDaemonThread(final Runnable runnable) | 83 private static void startDaemonThread(final Runnable runnable) |
76 { | 84 { |
77 final Thread t = new Thread(runnable); | 85 final Thread t = new Thread(runnable); |
78 t.setDaemon(true); | 86 t.setDaemon(true); |
79 t.start(); | 87 t.start(); |
80 } | 88 } |
81 | 89 |
82 private static class Initializer implements Runnable | 90 private static class Initializer implements Runnable |
83 { | 91 { |
84 private final EngineService service; | 92 private final EngineService service; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 { | 130 { |
123 try | 131 try |
124 { | 132 { |
125 while (!this.service.isInitialized) | 133 while (!this.service.isInitialized) |
126 { | 134 { |
127 Thread.sleep(250); | 135 Thread.sleep(250); |
128 } | 136 } |
129 | 137 |
130 for (;;) | 138 for (;;) |
131 { | 139 { |
132 final OnEngineCreatedCallback callback = EngineService.ON_CREATED_CALL
BACKS.take(); | 140 final EngineCreatedCallbackWrapper wrapper = EngineService.ON_CREATED_
CALLBACKS.take(); |
133 if (callback != null) | 141 if (wrapper != null) |
134 { | 142 { |
135 Engine.runOnUiThread(new Runnable() | 143 if (wrapper.runOnUiThread) |
136 { | 144 { |
137 private final EngineService service = CreationNotifier.this.servic
e; | 145 Engine.runOnUiThread(new Runnable() |
| 146 { |
| 147 private final EngineService service = CreationNotifier.this.serv
ice; |
138 | 148 |
139 @Override | 149 @Override |
140 public void run() | 150 public void run() |
141 { | 151 { |
142 callback.onEngineCreated(this.service.engine, this.service.failu
reCause == null); | 152 wrapper.callback.onEngineCreated(this.service.engine, |
143 } | 153 this.service.failureCause == null); |
144 }); | 154 } |
| 155 }); |
| 156 } |
| 157 else |
| 158 { |
| 159 wrapper.callback.onEngineCreated(this.service.engine, |
| 160 this.service.failureCause == null); |
| 161 } |
145 } | 162 } |
146 } | 163 } |
147 } | 164 } |
148 catch (final Throwable t) | 165 catch (final Throwable t) |
149 { | 166 { |
150 Log.e(TAG, "Notifier died: " + t.getMessage(), t); | 167 Log.e(TAG, "Notifier died: " + t.getMessage(), t); |
151 } | 168 } |
152 } | 169 } |
153 } | 170 } |
| 171 |
| 172 public interface OnEngineCreatedCallback |
| 173 { |
| 174 public void onEngineCreated(Engine engine, boolean success); |
| 175 } |
| 176 |
| 177 private static class EngineCreatedCallbackWrapper |
| 178 { |
| 179 final OnEngineCreatedCallback callback; |
| 180 final boolean runOnUiThread; |
| 181 |
| 182 public EngineCreatedCallbackWrapper(final OnEngineCreatedCallback callback, |
| 183 final boolean runOnUiThread) |
| 184 { |
| 185 this.callback = callback; |
| 186 this.runOnUiThread = runOnUiThread; |
| 187 } |
| 188 } |
154 } | 189 } |
OLD | NEW |