OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 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.android; |
| 19 |
| 20 import java.io.BufferedReader; |
| 21 import java.io.InputStreamReader; |
| 22 |
| 23 import org.adblockplus.android.updater.UpdaterActivity; |
| 24 import org.adblockplus.libadblockplus.JsValue; |
| 25 import org.adblockplus.libadblockplus.Subscription; |
| 26 import org.apache.commons.lang.StringUtils; |
| 27 |
| 28 import android.app.Notification; |
| 29 import android.app.NotificationManager; |
| 30 import android.app.PendingIntent; |
| 31 import android.content.Context; |
| 32 import android.content.Intent; |
| 33 import android.support.v4.app.NotificationCompat; |
| 34 |
| 35 public final class AppUtils |
| 36 { |
| 37 private AppUtils() |
| 38 { |
| 39 // |
| 40 } |
| 41 |
| 42 protected static void showUpdateNotification(final Context context, final Stri
ng url, |
| 43 final String error) |
| 44 { |
| 45 final NotificationCompat.Builder builder = new NotificationCompat.Builder(co
ntext); |
| 46 builder.setContentTitle(context.getText(R.string.app_name)); |
| 47 builder.setSmallIcon(R.drawable.ic_stat_warning); |
| 48 builder.setAutoCancel(true); |
| 49 builder.setOnlyAlertOnce(true); |
| 50 final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new
Intent(), 0); |
| 51 builder.setContentIntent(emptyIntent); |
| 52 |
| 53 if (StringUtils.isNotEmpty(error)) |
| 54 { |
| 55 builder.setContentText(context.getString(R.string.msg_update_fail)); |
| 56 } |
| 57 else if (StringUtils.isNotEmpty(url)) |
| 58 { |
| 59 builder.setSmallIcon(R.drawable.ic_stat_download); |
| 60 final Intent intent = new Intent(context, UpdaterActivity.class) |
| 61 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 62 intent.setAction("download"); |
| 63 intent.putExtra("url", url); |
| 64 final PendingIntent updateIntent = |
| 65 PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDAT
E_CURRENT); |
| 66 builder.setContentIntent(updateIntent); |
| 67 builder.setContentText(context.getString(R.string.msg_update_available)); |
| 68 } |
| 69 else |
| 70 { |
| 71 builder.setContentText(context.getString(R.string.msg_update_missing)); |
| 72 } |
| 73 |
| 74 final Notification notification = builder.getNotification(); |
| 75 final NotificationManager notificationManager = |
| 76 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERV
ICE); |
| 77 notificationManager.notify(AdblockPlus.UPDATE_NOTIFICATION_ID, notification)
; |
| 78 } |
| 79 |
| 80 protected static void updateSubscriptionStatus(final Context context, final Su
bscription sub) |
| 81 { |
| 82 final JsValue jsDownloadStatus = sub.getProperty("downloadStatus"); |
| 83 final String downloadStatus = jsDownloadStatus.isNull() ? "" : jsDownloadSta
tus.toString(); |
| 84 final long lastDownload = sub.getProperty("lastDownload").asLong(); |
| 85 |
| 86 String status = "synchronize_never"; |
| 87 long time = 0; |
| 88 |
| 89 if (sub.isUpdating()) |
| 90 { |
| 91 status = "synchronize_in_progress"; |
| 92 } |
| 93 else if (StringUtils.isNotEmpty(downloadStatus) && !downloadStatus.equals("s
ynchronize_ok")) |
| 94 { |
| 95 status = downloadStatus; |
| 96 } |
| 97 else if (lastDownload > 0) |
| 98 { |
| 99 time = lastDownload; |
| 100 status = "synchronize_last_at"; |
| 101 } |
| 102 |
| 103 context.sendBroadcast(new Intent(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS) |
| 104 .putExtra("url", sub.getProperty("url").toString()) |
| 105 .putExtra("status", status) |
| 106 .putExtra("time", time * 1000L)); |
| 107 } |
| 108 |
| 109 public static void appendRawTextFile(final Context context, final StringBuilde
r text, final int id) |
| 110 { |
| 111 try |
| 112 { |
| 113 final BufferedReader buf = new BufferedReader(new InputStreamReader(contex
t.getResources().openRawResource(id))); |
| 114 |
| 115 try |
| 116 { |
| 117 String line; |
| 118 while ((line = buf.readLine()) != null) |
| 119 { |
| 120 text.append(line); |
| 121 text.append('\n'); |
| 122 } |
| 123 } |
| 124 finally |
| 125 { |
| 126 buf.close(); |
| 127 } |
| 128 |
| 129 } |
| 130 catch (final Exception e) |
| 131 { |
| 132 // Ignored for now |
| 133 } |
| 134 } |
| 135 } |
OLD | NEW |