Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 package org.adblockplus.sbrowser.contentblocker.engine; | |
19 | |
20 import android.content.Context; | |
21 import android.os.AsyncTask; | |
22 | |
23 import java.io.IOException; | |
24 import java.lang.ref.WeakReference; | |
25 import java.util.ArrayList; | |
26 import java.util.Iterator; | |
27 import java.util.List; | |
28 | |
jens
2018/03/08 09:18:40
I didn't want to leave a lot of single comments, b
diegocarloslima
2018/03/08 11:54:34
Nope, the reason is just that I don't usually mark
jens
2018/03/08 12:33:54
Acknowledged.
| |
29 public class EngineManager | |
30 { | |
31 private static final EngineManager INSTANCE = new EngineManager(); | |
32 | |
33 private Engine engine; | |
34 private CreateEngineAsyncTask createEngineTask; | |
35 private final List<WeakReference<OnEngineCreatedCallback>> engineCreatedCallba cks = new ArrayList<>(); | |
36 | |
37 private EngineManager() | |
38 { | |
39 } | |
40 | |
41 public static EngineManager getInstance() | |
42 { | |
43 return INSTANCE; | |
44 } | |
45 | |
46 public void retrieveEngine(Context context, OnEngineCreatedCallback callback) | |
47 { | |
48 synchronized (engineCreatedCallbacks) | |
49 { | |
50 if (callback != null) | |
51 { | |
52 engineCreatedCallbacks.add(new WeakReference<>(callback)); | |
53 } | |
54 } | |
55 if (engine != null) | |
56 { | |
57 notifyEngineCreated(); | |
58 } | |
59 else if (createEngineTask == null || createEngineTask.isCancelled()) | |
60 { | |
61 this.createEngineTask = new CreateEngineAsyncTask(); | |
62 this.createEngineTask.execute(context); | |
63 } | |
64 } | |
65 | |
66 public void removeEngineCreatedCallback(OnEngineCreatedCallback callback) | |
67 { | |
68 if (callback != null) | |
69 { | |
70 synchronized (engineCreatedCallbacks) | |
71 { | |
72 final Iterator<WeakReference<OnEngineCreatedCallback>> iterator = engine CreatedCallbacks.iterator(); | |
73 while (iterator.hasNext()) | |
74 { | |
75 final OnEngineCreatedCallback cb = iterator.next().get(); | |
76 if (callback.equals(cb)) | |
77 { | |
78 iterator.remove(); | |
79 } | |
80 } | |
81 } | |
82 } | |
83 } | |
84 | |
85 private void setEngineAndNotify(Engine engine) | |
86 { | |
87 this.createEngineTask = null; | |
88 this.engine = engine; | |
89 notifyEngineCreated(); | |
90 } | |
91 | |
92 private void notifyEngineCreated() | |
93 { | |
94 synchronized (engineCreatedCallbacks) | |
95 { | |
96 final Iterator<WeakReference<OnEngineCreatedCallback>> iterator = engineCr eatedCallbacks.iterator(); | |
97 while (iterator.hasNext()) | |
98 { | |
99 final OnEngineCreatedCallback callback = iterator.next().get(); | |
100 if (callback != null) | |
101 { | |
102 Engine.runOnUiThread(new Runnable() | |
103 { | |
104 @Override | |
105 public void run() | |
106 { | |
107 callback.onEngineCreated(engine); | |
108 } | |
109 }); | |
110 } | |
111 iterator.remove(); | |
112 } | |
113 } | |
114 } | |
115 | |
116 public interface OnEngineCreatedCallback | |
117 { | |
118 void onEngineCreated(Engine engine); | |
119 } | |
120 | |
121 private static final class CreateEngineAsyncTask extends AsyncTask<Context, Vo id, Engine> | |
122 { | |
123 @Override | |
124 protected Engine doInBackground(Context... context) | |
125 { | |
126 try | |
127 { | |
128 return Engine.create(context[0].getApplicationContext()); | |
129 } | |
130 catch (IOException e) | |
131 { | |
132 } | |
133 return null; | |
134 } | |
135 | |
136 @Override | |
137 protected void onPostExecute(Engine engine) | |
138 { | |
139 EngineManager.getInstance().setEngineAndNotify(engine); | |
140 } | |
141 } | |
142 } | |
OLD | NEW |