Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 package org.adblockplus.android; | 18 package org.adblockplus.android; |
19 | 19 |
20 import org.adblockplus.android.updater.UpdaterActivity; | 20 import java.util.List; |
21 | 21 |
22 import android.app.Notification; | 22 import org.adblockplus.libadblockplus.AppInfo; |
23 import android.app.NotificationManager; | 23 import org.adblockplus.libadblockplus.EventCallback; |
24 import android.app.PendingIntent; | 24 import org.adblockplus.libadblockplus.Filter; |
25 import org.adblockplus.libadblockplus.FilterChangeCallback; | |
26 import org.adblockplus.libadblockplus.FilterEngine; | |
27 import org.adblockplus.libadblockplus.JsEngine; | |
28 import org.adblockplus.libadblockplus.LogSystem; | |
29 import org.adblockplus.libadblockplus.Subscription; | |
30 import org.adblockplus.libadblockplus.UpdaterCallback; | |
31 import org.adblockplus.libadblockplus.WebRequest; | |
32 | |
25 import android.content.Context; | 33 import android.content.Context; |
26 import android.content.Intent; | |
27 import android.content.pm.PackageInfo; | 34 import android.content.pm.PackageInfo; |
28 import android.content.pm.PackageManager.NameNotFoundException; | 35 import android.content.pm.PackageManager.NameNotFoundException; |
29 import android.os.Build.VERSION; | 36 import android.os.Build.VERSION; |
30 import android.support.v4.app.NotificationCompat; | |
31 import android.util.Log; | 37 import android.util.Log; |
32 | 38 |
33 public class ABPEngine | 39 public final class ABPEngine |
34 { | 40 { |
35 private final static String TAG = "ABPEngine"; | 41 private static final String TAG = Utils.getTag(ABPEngine.class); |
36 private static final int NOTIFICATION_ID = R.string.app_name + 1; | 42 |
37 | 43 private final Context context; |
38 private Context context; | 44 |
39 | 45 private volatile JsEngine jsEngine; |
Felix Dahlke
2014/04/28 07:29:01
I know we discussed this in the last path set, but
René Jeschke
2014/04/28 08:34:32
Done.
| |
40 public ABPEngine(Context context, String basePath) | 46 private volatile FilterEngine filterEngine; |
47 private volatile LogSystem logSystem; | |
48 private volatile WebRequest webRequest; | |
49 private volatile EventCallback updateCallback; | |
50 private volatile UpdaterCallback updaterCallback; | |
51 private volatile FilterChangeCallback filterChangeCallback; | |
52 | |
53 private ABPEngine(final Context context) | |
41 { | 54 { |
42 this.context = context; | 55 this.context = context; |
43 String version; | 56 } |
57 | |
58 public static AppInfo generateAppInfo(final Context context) | |
59 { | |
60 String version = "0"; | |
44 try | 61 try |
45 { | 62 { |
46 final PackageInfo info = context.getPackageManager().getPackageInfo(contex t.getPackageName(), 0); | 63 final PackageInfo info = context.getPackageManager().getPackageInfo(contex t.getPackageName(), 0); |
47 version = info.versionName + "." + info.versionCode; | 64 version = info.versionName + "." + info.versionCode; |
48 } catch (NameNotFoundException e) | 65 } |
66 catch (final NameNotFoundException e) | |
49 { | 67 { |
50 Log.e(TAG, "Failed to get the application version number", e); | 68 Log.e(TAG, "Failed to get the application version number", e); |
51 version = "0"; | |
52 } | 69 } |
53 final String sdkVersion = String.valueOf(VERSION.SDK_INT); | 70 final String sdkVersion = String.valueOf(VERSION.SDK_INT); |
54 final String locale = context.getResources().getConfiguration().locale.toStr ing(); | 71 final String locale = context.getResources().getConfiguration().locale.toStr ing(); |
55 final boolean developmentBuild = !context.getResources().getBoolean(R.bool.d ef_release); | 72 final boolean developmentBuild = !context.getResources().getBoolean(R.bool.d ef_release); |
56 initialize(basePath, version, sdkVersion, locale, developmentBuild); | 73 |
57 } | 74 return AppInfo.builder() |
58 | 75 .setVersion(version) |
59 public void onFilterChanged(String url, String status, long time) | 76 .setApplicationVersion(sdkVersion) |
60 { | 77 .setLocale(locale) |
61 context.sendBroadcast(new Intent(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS). putExtra("url", url).putExtra("status", status).putExtra("time", time)); | 78 .setDevelopmentBuild(developmentBuild) |
Felix Dahlke
2014/04/28 07:29:01
Is this trailing whitespace? I can't really tell f
René Jeschke
2014/04/28 08:34:32
Nope, Editor->Save actions->Remove trailing whites
Felix Dahlke
2014/04/28 10:09:41
Ah, so I guess the diff is telling me that there W
René Jeschke
2014/04/28 10:18:34
Done.
| |
62 } | 79 .build(); |
63 | 80 } |
64 /** | 81 |
65 * Called when update event occurred. | 82 public static ABPEngine create(final Context context, final AppInfo appInfo, f inal String basePath) |
66 * @param url Update download address | 83 { |
67 */ | 84 final ABPEngine engine = new ABPEngine(context); |
68 public void onUpdateEvent(String url, String error) | 85 |
69 { | 86 engine.jsEngine = new JsEngine(appInfo); |
70 Notification notification = getNotification(url, error); | 87 engine.jsEngine.setDefaultFileSystem(basePath); |
71 NotificationManager notificationManager = (NotificationManager) context.getS ystemService(Context.NOTIFICATION_SERVICE); | 88 |
72 notificationManager.notify(NOTIFICATION_ID, notification); | 89 engine.logSystem = new AndroidLogSystem(); |
73 } | 90 engine.jsEngine.setLogSystem(engine.logSystem); |
74 | 91 |
75 private native void initialize(String basePath, String version, String sdkVers ion, String locale, boolean developmentBuild); | 92 engine.webRequest = new AndroidWebRequest(); |
76 | 93 engine.jsEngine.setWebRequest(engine.webRequest); |
77 public native void release(); | 94 |
78 | 95 engine.updateCallback = new AndroidUpdateAvailableCallback(context); |
79 public native boolean isFirstRun(); | 96 engine.jsEngine.setEventCallback("updateAvailable", engine.updateCallback); |
80 | 97 |
81 public native Subscription[] getListedSubscriptions(); | 98 engine.filterEngine = new FilterEngine(engine.jsEngine); |
82 | 99 engine.filterChangeCallback = new AndroidFilterChangeCallback(context); |
83 public native Subscription[] getRecommendedSubscriptions(); | 100 engine.filterEngine.setFilterChangeCallback(engine.filterChangeCallback); |
84 | 101 |
85 public native void addSubscription(String url); | 102 engine.updaterCallback = new AndroidUpdaterCallback(context); |
86 | 103 |
87 public native void removeSubscription(String url); | 104 return engine; |
88 | 105 } |
89 public native void refreshSubscription(String url); | 106 |
90 | 107 public void dispose() |
91 public native void actualizeSubscriptionStatus(String url); | 108 { |
92 | 109 // Safe disposing (just in case) |
93 public native void setAcceptableAdsEnabled(boolean enabled); | 110 if (this.filterEngine != null) |
94 | 111 { |
95 public native String getDocumentationLink(); | 112 this.filterEngine.dispose(); |
96 | 113 this.filterEngine = null; |
97 public native boolean matches(String url, String contentType, String[] documen tUrls); | 114 } |
98 | 115 |
99 public native String[] getSelectorsForDomain(String domain); | 116 if (this.jsEngine != null) |
100 | 117 { |
101 public native void checkUpdates(); | 118 this.jsEngine.dispose(); |
102 | 119 this.jsEngine = null; |
103 private Notification getNotification(String url, String error) | 120 } |
104 { | 121 |
105 final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); | 122 if (this.logSystem != null) |
106 | 123 { |
107 NotificationCompat.Builder builder = new NotificationCompat.Builder(context) ; | 124 this.logSystem.dispose(); |
108 builder.setContentTitle(context.getText(R.string.app_name)); | 125 this.logSystem = null; |
109 builder.setSmallIcon(R.drawable.ic_stat_warning); | 126 } |
110 builder.setWhen(System.currentTimeMillis()); | 127 |
111 builder.setAutoCancel(true); | 128 if (this.webRequest != null) |
112 builder.setOnlyAlertOnce(true); | 129 { |
113 builder.setContentIntent(emptyIntent); | 130 this.webRequest.dispose(); |
114 | 131 this.webRequest = null; |
115 if (url != null) | 132 } |
116 { | 133 |
117 builder.setSmallIcon(R.drawable.ic_stat_download); | 134 if (this.updateCallback != null) |
118 | 135 { |
119 | 136 this.updateCallback.dispose(); |
120 Intent intent = new Intent(context, UpdaterActivity.class).addFlags(Intent .FLAG_ACTIVITY_NEW_TASK); | 137 this.updateCallback = null; |
121 intent.setAction("download"); | 138 } |
122 intent.putExtra("url", url); | 139 |
123 PendingIntent updateIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | 140 if (this.updaterCallback != null) |
124 builder.setContentIntent(updateIntent); | 141 { |
125 builder.setContentText(context.getString(R.string.msg_update_available)); | 142 this.updaterCallback.dispose(); |
126 } | 143 this.updaterCallback = null; |
127 else if (error != null) | 144 } |
128 { | 145 |
129 //TODO Should we show error message to the user? | 146 if (this.filterChangeCallback != null) |
130 builder.setContentText(context.getString(R.string.msg_update_fail)); | 147 { |
131 } | 148 this.filterChangeCallback.dispose(); |
132 else | 149 this.filterChangeCallback = null; |
133 { | 150 } |
134 builder.setContentText(context.getString(R.string.msg_update_missing)); | 151 } |
135 } | 152 |
136 | 153 public boolean isFirstRun() |
137 Notification notification = builder.getNotification(); | 154 { |
138 return notification; | 155 return this.filterEngine.isFirstRun(); |
139 } | 156 } |
140 | 157 |
141 static | 158 private static org.adblockplus.android.Subscription convertJsSubscription(fina l Subscription jsSubscription) |
142 { | 159 { |
143 System.loadLibrary("adblockplus-jni"); | 160 final org.adblockplus.android.Subscription subscription = new org.adblockplu s.android.Subscription(); |
161 | |
162 subscription.title = jsSubscription.getProperty("title").toString(); | |
163 subscription.url = jsSubscription.getProperty("url").toString(); | |
164 | |
165 return subscription; | |
166 } | |
167 | |
168 private static org.adblockplus.android.Subscription[] convertJsSubscriptions(f inal List<Subscription> subs) | |
Felix Dahlke
2014/04/28 07:29:01
How about renaming "subs" to "jsSubscription" and
René Jeschke
2014/04/28 08:34:32
Done.
| |
169 { | |
170 final org.adblockplus.android.Subscription[] ret = new org.adblockplus.andro id.Subscription[subs.size()]; | |
171 | |
172 for (int i = 0; i < ret.length; i++) | |
173 { | |
174 ret[i] = convertJsSubscription(subs.get(i)); | |
175 } | |
176 | |
177 return ret; | |
178 } | |
179 | |
180 public org.adblockplus.android.Subscription[] getRecommendedSubscriptions() | |
181 { | |
182 return convertJsSubscriptions(this.filterEngine.fetchAvailableSubscriptions( )); | |
183 } | |
184 | |
185 public org.adblockplus.android.Subscription[] getListedSubscriptions() | |
186 { | |
187 return convertJsSubscriptions(this.filterEngine.getListedSubscriptions()); | |
188 } | |
189 | |
190 public void setSubscription(final String url) | |
191 { | |
192 Subscription sub = null; | |
193 for (final Subscription s : this.filterEngine.getListedSubscriptions()) | |
194 { | |
195 if (url.equals(s.getProperty("url").toString())) | |
196 { | |
197 sub = s; | |
198 } | |
199 s.removeFromList(); | |
200 } | |
201 if (sub != null) | |
202 { | |
203 sub.addToList(); | |
204 } | |
205 } | |
206 | |
207 public void refreshSubscriptions() | |
208 { | |
209 for (final Subscription s : this.filterEngine.getListedSubscriptions()) | |
210 { | |
211 s.updateFilters(); | |
212 } | |
213 } | |
214 | |
215 public void setAcceptableAdsEnabled(final boolean enabled) | |
216 { | |
217 final String url = this.filterEngine.getPref("subscriptions_exceptionsurl"). toString(); | |
218 final Subscription sub = this.filterEngine.getSubscription(url); | |
219 if (sub != null) | |
220 { | |
221 if (enabled) | |
222 { | |
223 sub.addToList(); | |
224 } | |
225 else | |
226 { | |
227 sub.removeFromList(); | |
228 } | |
229 } | |
230 } | |
231 | |
232 public String getDocumentationLink() | |
233 { | |
234 return this.filterEngine.getPref("documentation_link").toString(); | |
235 } | |
236 | |
237 public boolean matches(final String fullUrl, final String contentType, final S tring[] referrerChainArray) | |
238 { | |
239 final Filter filter = this.filterEngine.matches(fullUrl, contentType, referr erChainArray); | |
240 | |
241 if (filter == null) | |
242 { | |
243 return false; | |
244 } | |
245 | |
246 // hack: if there is no referrer, block only if filter is domain-specific | |
247 // (to re-enable in-app ads blocking, proposed on 12.11.2012 Monday meeting) | |
248 // (documentUrls contains the referrers on Android) | |
249 if (referrerChainArray.length == 0 && (filter.getProperty("text").toString() ).contains("||")) | |
250 { | |
251 return false; | |
252 } | |
253 | |
254 return filter.getType() != Filter.Type.EXCEPTION; | |
255 } | |
256 | |
257 public void checkForUpdates() | |
258 { | |
259 this.filterEngine.forceUpdateCheck(this.updaterCallback); | |
260 } | |
261 | |
262 public void updateSubscriptionStatus(final String url) | |
263 { | |
264 final Subscription sub = this.filterEngine.getSubscription(url); | |
265 if (sub != null) | |
266 { | |
267 Utils.updateSubscriptionStatus(this.context, sub); | |
268 } | |
144 } | 269 } |
145 } | 270 } |
OLD | NEW |