| 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.updater; | 18 package org.adblockplus.android.updater; |
| 19 | 19 |
| 20 import java.io.BufferedInputStream; | 20 import java.io.BufferedInputStream; |
| 21 import java.io.File; | 21 import java.io.File; |
| 22 import java.io.FileOutputStream; | 22 import java.io.FileOutputStream; |
| 23 import java.io.InputStream; | 23 import java.io.InputStream; |
| 24 import java.io.OutputStream; | 24 import java.io.OutputStream; |
| 25 import java.net.URL; | 25 import java.net.URL; |
| 26 import java.net.URLConnection; | 26 import java.net.URLConnection; |
| 27 | 27 |
| 28 import org.adblockplus.android.R; | 28 import org.adblockplus.android.R; |
| 29 import org.adblockplus.android.Utils; |
| 29 | 30 |
| 30 import android.app.Notification; | 31 import android.app.Notification; |
| 31 import android.app.NotificationManager; | 32 import android.app.NotificationManager; |
| 32 import android.app.PendingIntent; | 33 import android.app.PendingIntent; |
| 33 import android.app.Service; | 34 import android.app.Service; |
| 34 import android.content.Context; | 35 import android.content.Context; |
| 35 import android.content.Intent; | 36 import android.content.Intent; |
| 36 import android.os.AsyncTask; | 37 import android.os.AsyncTask; |
| 37 import android.os.Environment; | 38 import android.os.Environment; |
| 38 import android.os.IBinder; | 39 import android.os.IBinder; |
| 39 import android.util.Log; | 40 import android.util.Log; |
| 40 | 41 |
| 41 /** | 42 /** |
| 42 * Update downloader. | 43 * Update downloader. |
| 43 */ | 44 */ |
| 44 public class UpdaterService extends Service | 45 public class UpdaterService extends Service |
| 45 { | 46 { |
| 46 private final static String TAG = "UpdaterService"; | 47 private static final String TAG = Utils.getTag(UpdaterService.class); |
| 47 | 48 |
| 48 private File updateDir; | 49 private File updateDir; |
| 49 | 50 |
| 50 @Override | 51 @Override |
| 51 public void onCreate() | 52 public void onCreate() |
| 52 { | 53 { |
| 53 super.onCreate(); | 54 super.onCreate(); |
| 54 // Use common Android path for downloads | 55 // Use common Android path for downloads |
| 55 updateDir = new File(Environment.getExternalStorageDirectory().getPath(), "d
ownloads"); | 56 updateDir = new File(Environment.getExternalStorageDirectory().getPath(), "d
ownloads"); |
| 56 } | 57 } |
| 57 | 58 |
| 59 @SuppressWarnings("deprecation") |
| 58 @Override | 60 @Override |
| 59 public void onStart(Intent intent, int startId) | 61 public void onStart(final Intent intent, final int startId) |
| 60 { | 62 { |
| 61 super.onStart(intent, startId); | 63 super.onStart(intent, startId); |
| 62 | 64 |
| 63 // Stop if media not available | 65 // Stop if media not available |
| 64 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
) | 66 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
) |
| 65 { | 67 { |
| 66 stopSelf(); | 68 stopSelf(); |
| 67 return; | 69 return; |
| 68 } | 70 } |
| 69 updateDir.mkdirs(); | 71 updateDir.mkdirs(); |
| 70 | 72 |
| 71 // Start download | 73 // Start download |
| 72 if (intent != null && intent.hasExtra("url")) | 74 if (intent != null && intent.hasExtra("url")) |
| 73 new DownloadTask(this).execute(intent.getStringExtra("url")); | 75 new DownloadTask(this).execute(intent.getStringExtra("url")); |
| 74 } | 76 } |
| 75 | 77 |
| 76 @Override | 78 @Override |
| 77 public IBinder onBind(Intent intent) | 79 public IBinder onBind(final Intent intent) |
| 78 { | 80 { |
| 79 return null; | 81 return null; |
| 80 } | 82 } |
| 81 | 83 |
| 82 private class DownloadTask extends AsyncTask<String, Integer, String> | 84 private class DownloadTask extends AsyncTask<String, Integer, String> |
| 83 { | 85 { |
| 84 private Context context; | 86 private final Context context; |
| 85 private Notification notification; | 87 private final Notification notification; |
| 86 private PendingIntent contentIntent; | 88 private final PendingIntent contentIntent; |
| 87 private NotificationManager notificationManager; | 89 private final NotificationManager notificationManager; |
| 88 private int notificationId = R.string.app_name + 2; | 90 private final int notificationId = R.string.app_name + 2; |
| 89 | 91 |
| 90 public DownloadTask(Context context) | 92 public DownloadTask(final Context context) |
| 91 { | 93 { |
| 92 this.context = context; | 94 this.context = context; |
| 93 notificationManager = (NotificationManager) context.getSystemService(Conte
xt.NOTIFICATION_SERVICE); | 95 notificationManager = (NotificationManager) context.getSystemService(Conte
xt.NOTIFICATION_SERVICE); |
| 94 notification = new Notification(); | 96 notification = new Notification(); |
| 95 contentIntent = PendingIntent.getActivity(context, 0, new Intent(), Pendin
gIntent.FLAG_UPDATE_CURRENT); | 97 contentIntent = PendingIntent.getActivity(context, 0, new Intent(), Pendin
gIntent.FLAG_UPDATE_CURRENT); |
| 96 } | 98 } |
| 97 | 99 |
| 100 @SuppressWarnings("deprecation") |
| 98 @Override | 101 @Override |
| 99 protected void onPreExecute() | 102 protected void onPreExecute() |
| 100 { | 103 { |
| 101 notification.flags |= Notification.FLAG_ONGOING_EVENT; | 104 notification.flags |= Notification.FLAG_ONGOING_EVENT; |
| 102 notification.when = 0; | 105 notification.when = 0; |
| 103 notification.icon = R.drawable.ic_stat_download; | 106 notification.icon = R.drawable.ic_stat_download; |
| 104 notification.setLatestEventInfo(context, getString(R.string.app_name), Str
ing.format(getString(R.string.msg_update_downloading), 0), contentIntent); | 107 notification.setLatestEventInfo(context, getString(R.string.app_name), Str
ing.format(getString(R.string.msg_update_downloading), 0), contentIntent); |
| 105 notificationManager.notify(notificationId, notification); | 108 notificationManager.notify(notificationId, notification); |
| 106 } | 109 } |
| 107 | 110 |
| 108 @Override | 111 @Override |
| 109 protected String doInBackground(String... sUrl) | 112 protected String doInBackground(final String... sUrl) |
| 110 { | 113 { |
| 111 try | 114 try |
| 112 { | 115 { |
| 113 // Create connection | 116 // Create connection |
| 114 URL url = new URL(sUrl[0]); | 117 final URL url = new URL(sUrl[0]); |
| 115 Log.e(TAG, "D: " + sUrl[0]); | 118 Log.e(TAG, "D: " + sUrl[0]); |
| 116 URLConnection connection = url.openConnection(); | 119 final URLConnection connection = url.openConnection(); |
| 117 connection.connect(); | 120 connection.connect(); |
| 118 int fileLength = connection.getContentLength(); | 121 final int fileLength = connection.getContentLength(); |
| 119 Log.e(TAG, "S: " + fileLength); | 122 Log.e(TAG, "S: " + fileLength); |
| 120 | 123 |
| 121 // Check if file already exists | 124 // Check if file already exists |
| 122 File updateFile = new File(updateDir, "AdblockPlus-update.apk"); | 125 final File updateFile = new File(updateDir, "AdblockPlus-update.apk"); |
| 123 if (updateFile.exists()) | 126 if (updateFile.exists()) |
| 124 { | 127 { |
| 125 // if (updateFile.length() == fileLength) | 128 // if (updateFile.length() == fileLength) |
| 126 // return updateFile.getAbsolutePath(); | 129 // return updateFile.getAbsolutePath(); |
| 127 // else | 130 // else |
| 128 updateFile.delete(); | 131 updateFile.delete(); |
| 129 } | 132 } |
| 130 | 133 |
| 131 // Download the file | 134 // Download the file |
| 132 InputStream input = new BufferedInputStream(url.openStream()); | 135 final InputStream input = new BufferedInputStream(url.openStream()); |
| 133 OutputStream output = new FileOutputStream(updateFile); | 136 final OutputStream output = new FileOutputStream(updateFile); |
| 134 | 137 |
| 135 byte data[] = new byte[1024]; | 138 final byte data[] = new byte[1024]; |
| 136 long total = 0; | 139 long total = 0; |
| 137 int count; | 140 int count; |
| 138 int progress = 0; | 141 int progress = 0; |
| 139 while ((count = input.read(data)) != -1) | 142 while ((count = input.read(data)) != -1) |
| 140 { | 143 { |
| 141 total += count; | 144 total += count; |
| 142 output.write(data, 0, count); | 145 output.write(data, 0, count); |
| 143 | 146 |
| 144 int p = (int) (total * 100 / fileLength); | 147 final int p = (int) (total * 100 / fileLength); |
| 145 if (p != progress) | 148 if (p != progress) |
| 146 { | 149 { |
| 147 publishProgress(p); | 150 publishProgress(p); |
| 148 progress = p; | 151 progress = p; |
| 149 } | 152 } |
| 150 } | 153 } |
| 151 | 154 |
| 152 output.flush(); | 155 output.flush(); |
| 153 output.close(); | 156 output.close(); |
| 154 input.close(); | 157 input.close(); |
| 155 return updateFile.getAbsolutePath(); | 158 return updateFile.getAbsolutePath(); |
| 156 } | 159 } |
| 157 catch (Exception e) | 160 catch (final Exception e) |
| 158 { | 161 { |
| 159 Log.e(TAG, "Download error", e); | 162 Log.e(TAG, "Download error", e); |
| 160 return null; | 163 return null; |
| 161 } | 164 } |
| 162 } | 165 } |
| 163 | 166 |
| 167 @SuppressWarnings("deprecation") |
| 164 @Override | 168 @Override |
| 165 protected void onProgressUpdate(Integer... progress) | 169 protected void onProgressUpdate(final Integer... progress) |
| 166 { | 170 { |
| 167 notification.setLatestEventInfo(context, getString(R.string.app_name), Str
ing.format(getString(R.string.msg_update_downloading), progress[0]), contentInte
nt); | 171 notification.setLatestEventInfo(context, getString(R.string.app_name), Str
ing.format(getString(R.string.msg_update_downloading), progress[0]), contentInte
nt); |
| 168 notificationManager.notify(notificationId, notification); | 172 notificationManager.notify(notificationId, notification); |
| 169 } | 173 } |
| 170 | 174 |
| 175 @SuppressWarnings("deprecation") |
| 171 @Override | 176 @Override |
| 172 protected void onPostExecute(String result) | 177 protected void onPostExecute(final String result) |
| 173 { | 178 { |
| 174 notificationManager.cancel(notificationId); | 179 notificationManager.cancel(notificationId); |
| 175 if (result != null) | 180 if (result != null) |
| 176 { | 181 { |
| 177 Notification notification = new Notification(); | 182 final Notification notification = new Notification(); |
| 178 notification.icon = R.drawable.ic_stat_download; | 183 notification.icon = R.drawable.ic_stat_download; |
| 179 notification.when = System.currentTimeMillis(); | 184 notification.when = System.currentTimeMillis(); |
| 180 notification.flags |= Notification.FLAG_AUTO_CANCEL; | 185 notification.flags |= Notification.FLAG_AUTO_CANCEL; |
| 181 Intent intent = new Intent(context, UpdaterActivity.class).addFlags(Inte
nt.FLAG_ACTIVITY_NEW_TASK); | 186 final Intent intent = new Intent(context, UpdaterActivity.class).addFlag
s(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 182 intent.setAction("update"); | 187 intent.setAction("update"); |
| 183 intent.putExtra("path", result); | 188 intent.putExtra("path", result); |
| 184 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, inte
nt, PendingIntent.FLAG_UPDATE_CURRENT); | 189 final PendingIntent contentIntent = PendingIntent.getActivity(context, 0
, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
| 185 notification.setLatestEventInfo(context, context.getText(R.string.app_na
me), context.getString(R.string.msg_update_ready), contentIntent); | 190 notification.setLatestEventInfo(context, context.getText(R.string.app_na
me), context.getString(R.string.msg_update_ready), contentIntent); |
| 186 notificationManager.notify(R.string.app_name + 1, notification); | 191 notificationManager.notify(R.string.app_name + 1, notification); |
| 187 } | 192 } |
| 188 } | 193 } |
| 189 } | 194 } |
| 190 } | 195 } |
| OLD | NEW |