| 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 Utils | |
| 36 { | |
| 37 private Utils() | |
| 38 { | |
| 39 // | |
| 40 } | |
| 41 | |
| 42 public static String getTag(final Class<?> clazz) | |
| 43 { | |
| 44 return clazz.getSimpleName(); | |
| 45 } | |
| 46 | |
| 47 public static String capitalizeString(final String s) | |
| 48 { | |
| 49 if (s == null || s.length() == 0) | |
| 50 { | |
| 51 return ""; | |
| 52 } | |
| 53 | |
| 54 final char first = s.charAt(0); | |
| 55 | |
| 56 return Character.isUpperCase(first) ? s : Character.toUpperCase(first) + s.s
ubstring(1); | |
| 57 } | |
| 58 | |
| 59 protected static void showUpdateNotification(final Context context, final Stri
ng url, | |
| 60 final String error) | |
| 61 { | |
| 62 final NotificationCompat.Builder builder = new NotificationCompat.Builder(co
ntext); | |
| 63 builder.setContentTitle(context.getText(R.string.app_name)); | |
| 64 builder.setSmallIcon(R.drawable.ic_stat_warning); | |
| 65 builder.setAutoCancel(true); | |
| 66 builder.setOnlyAlertOnce(true); | |
| 67 final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new
Intent(), 0); | |
| 68 builder.setContentIntent(emptyIntent); | |
| 69 | |
| 70 if (StringUtils.isNotEmpty(error)) | |
| 71 { | |
| 72 builder.setContentText(context.getString(R.string.msg_update_fail)); | |
| 73 } | |
| 74 else if (StringUtils.isNotEmpty(url)) | |
| 75 { | |
| 76 builder.setSmallIcon(R.drawable.ic_stat_download); | |
| 77 final Intent intent = new Intent(context, UpdaterActivity.class) | |
| 78 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| 79 intent.setAction("download"); | |
| 80 intent.putExtra("url", url); | |
| 81 final PendingIntent updateIntent = | |
| 82 PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDAT
E_CURRENT); | |
| 83 builder.setContentIntent(updateIntent); | |
| 84 builder.setContentText(context.getString(R.string.msg_update_available)); | |
| 85 } | |
| 86 else | |
| 87 { | |
| 88 builder.setContentText(context.getString(R.string.msg_update_missing)); | |
| 89 } | |
| 90 | |
| 91 final Notification notification = builder.getNotification(); | |
| 92 final NotificationManager notificationManager = | |
| 93 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERV
ICE); | |
| 94 notificationManager.notify(AdblockPlus.UPDATE_NOTIFICATION_ID, notification)
; | |
| 95 } | |
| 96 | |
| 97 protected static void updateSubscriptionStatus(final Context context, final Su
bscription sub) | |
| 98 { | |
| 99 final JsValue jsDownloadStatus = sub.getProperty("downloadStatus"); | |
| 100 final String downloadStatus = jsDownloadStatus.isNull() ? "" : jsDownloadSta
tus.toString(); | |
| 101 final long lastDownload = sub.getProperty("lastDownload").asLong(); | |
| 102 | |
| 103 String status = "synchronize_never"; | |
| 104 long time = 0; | |
| 105 | |
| 106 if (sub.isUpdating()) | |
| 107 { | |
| 108 status = "synchronize_in_progress"; | |
| 109 } | |
| 110 else if (StringUtils.isNotEmpty(downloadStatus) && !downloadStatus.equals("s
ynchronize_ok")) | |
| 111 { | |
| 112 status = downloadStatus; | |
| 113 } | |
| 114 else if (lastDownload > 0) | |
| 115 { | |
| 116 time = lastDownload; | |
| 117 status = "synchronize_last_at"; | |
| 118 } | |
| 119 | |
| 120 context.sendBroadcast(new Intent(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS) | |
| 121 .putExtra("url", sub.getProperty("url").toString()) | |
| 122 .putExtra("status", status) | |
| 123 .putExtra("time", time * 1000L)); | |
| 124 } | |
| 125 | |
| 126 public static void appendRawTextFile(final Context context, final StringBuilde
r text, final int id) | |
| 127 { | |
| 128 try | |
| 129 { | |
| 130 final BufferedReader buf = new BufferedReader(new InputStreamReader(contex
t.getResources().openRawResource(id))); | |
| 131 | |
| 132 try | |
| 133 { | |
| 134 String line; | |
| 135 while ((line = buf.readLine()) != null) | |
| 136 { | |
| 137 text.append(line); | |
| 138 text.append('\n'); | |
| 139 } | |
| 140 } | |
| 141 finally | |
| 142 { | |
| 143 buf.close(); | |
| 144 } | |
| 145 | |
| 146 } | |
| 147 catch (final Exception e) | |
| 148 { | |
| 149 // Ignored for now | |
| 150 } | |
| 151 } | |
| 152 } | |
| OLD | NEW |