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 java.io.BufferedOutputStream; |
| 21 import java.io.DataOutputStream; |
| 22 import java.io.File; |
| 23 import java.io.FileOutputStream; |
| 24 import java.io.IOException; |
| 25 import java.util.zip.GZIPOutputStream; |
| 26 |
| 27 import org.adblockplus.adblockplussbrowser.R; |
| 28 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils; |
| 29 |
| 30 import android.content.Context; |
| 31 import android.text.format.DateUtils; |
| 32 import android.util.Log; |
| 33 |
| 34 import javax.net.ssl.HttpsURLConnection; |
| 35 |
| 36 public class Notification |
| 37 { |
| 38 private static final String TAG = Notification.class.getSimpleName(); |
| 39 public static final String NOTIFICATION_URL = "https://notification.adblockplu
s.org/notification.json"; |
| 40 public static final String NOTIFICATION_DATA_FILE_NAME = "notification.abp"; |
| 41 public static final String KEY_EXTRA_ID = "_extra_id"; |
| 42 public static final String KEY_EXTRA_URL = "_extra_url"; |
| 43 |
| 44 private static final long NOTIFICATION_DOWNLOAD_INTERVAL = DateUtils.DAY_IN_MI
LLIS; |
| 45 private static final long DOWNLOAD_RETRY_INTERVAL = DateUtils.HOUR_IN_MILLIS; |
| 46 |
| 47 public static boolean shouldUpdate(final Context context) |
| 48 { |
| 49 final long now = System.currentTimeMillis(); |
| 50 final long lastUpdate = SharedPrefsUtils.getLong(context, R.string.key_last_
notification_update_timestamp, 0); |
| 51 final long lastTry = SharedPrefsUtils.getLong(context, R.string.key_last_tri
ed_notification_update_timestamp, 0); |
| 52 |
| 53 if (lastTry > lastUpdate) |
| 54 { |
| 55 return now - lastTry > DOWNLOAD_RETRY_INTERVAL; |
| 56 } |
| 57 else |
| 58 { |
| 59 return now - lastUpdate > NOTIFICATION_DOWNLOAD_INTERVAL; |
| 60 } |
| 61 } |
| 62 |
| 63 public static void update(final Context context, final int responseCode, final
String text, |
| 64 final File notificationDataFile) |
| 65 { |
| 66 if (responseCode != HttpsURLConnection.HTTP_OK || text == null) |
| 67 { |
| 68 SharedPrefsUtils.putLong(context, R.string.key_last_tried_notification_upd
ate_timestamp, System.currentTimeMillis()); |
| 69 } |
| 70 else |
| 71 { |
| 72 SharedPrefsUtils.putLong(context, R.string.key_last_notification_update_ti
mestamp, System.currentTimeMillis()); |
| 73 persistData(notificationDataFile, text); |
| 74 } |
| 75 } |
| 76 |
| 77 private static void persistData(final File filtersFile, final String text) |
| 78 { |
| 79 try (final DataOutputStream outputStream = new DataOutputStream(new Buffered
OutputStream( |
| 80 new GZIPOutputStream(new FileOutputStream(filtersFile))))) |
| 81 { |
| 82 outputStream.write(text.getBytes()); |
| 83 } |
| 84 catch (IOException e) |
| 85 { |
| 86 Log.d(TAG, "Failed to write notification data to internal storage.", e); |
| 87 } |
| 88 } |
| 89 } |
OLD | NEW |