Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/org/adblockplus/android/updater/UpdaterService.java

Issue 5697499218051072: Usage of new API, cleanups (reduced) (Closed)
Patch Set: Created April 11, 2014, 1:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 final static 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 this.updateDir = new File(Environment.getExternalStorageDirectory().getPath( ), "downloads");
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 this.stopSelf();
67 return; 69 return;
68 } 70 }
69 updateDir.mkdirs(); 71 this.updateDir.mkdirs();
70 72
71 // Start download 73 // Start download
72 if (intent != null && intent.hasExtra("url")) 74 if (intent != null && intent.hasExtra("url"))
75 {
73 new DownloadTask(this).execute(intent.getStringExtra("url")); 76 new DownloadTask(this).execute(intent.getStringExtra("url"));
77 }
74 } 78 }
75 79
76 @Override 80 @Override
77 public IBinder onBind(Intent intent) 81 public IBinder onBind(final Intent intent)
78 { 82 {
79 return null; 83 return null;
80 } 84 }
81 85
82 private class DownloadTask extends AsyncTask<String, Integer, String> 86 private class DownloadTask extends AsyncTask<String, Integer, String>
83 { 87 {
84 private Context context; 88 private final Context context;
85 private Notification notification; 89 private final Notification notification;
86 private PendingIntent contentIntent; 90 private final PendingIntent contentIntent;
87 private NotificationManager notificationManager; 91 private final NotificationManager notificationManager;
88 private int notificationId = R.string.app_name + 2; 92 private final int notificationId = R.string.app_name + 2;
89 93
90 public DownloadTask(Context context) 94 public DownloadTask(final Context context)
91 { 95 {
92 this.context = context; 96 this.context = context;
93 notificationManager = (NotificationManager) context.getSystemService(Conte xt.NOTIFICATION_SERVICE); 97 this.notificationManager = (NotificationManager)context.getSystemService(C ontext.NOTIFICATION_SERVICE);
94 notification = new Notification(); 98 this.notification = new Notification();
95 contentIntent = PendingIntent.getActivity(context, 0, new Intent(), Pendin gIntent.FLAG_UPDATE_CURRENT); 99 this.contentIntent = PendingIntent.getActivity(context, 0, new Intent(), P endingIntent.FLAG_UPDATE_CURRENT);
100 }
101
102 @SuppressWarnings("deprecation")
103 @Override
104 protected void onPreExecute()
105 {
106 this.notification.flags |= Notification.FLAG_ONGOING_EVENT;
107 this.notification.when = 0;
108 this.notification.icon = R.drawable.ic_stat_download;
109 this.notification.setLatestEventInfo(this.context, UpdaterService.this.get String(R.string.app_name),
110 String.format(UpdaterService.this.getString(R.string.msg_update_downlo ading), 0),
111 this.contentIntent);
112 this.notificationManager.notify(this.notificationId, this.notification);
96 } 113 }
97 114
98 @Override 115 @Override
99 protected void onPreExecute() 116 protected String doInBackground(final String... sUrl)
100 {
101 notification.flags |= Notification.FLAG_ONGOING_EVENT;
102 notification.when = 0;
103 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);
105 notificationManager.notify(notificationId, notification);
106 }
107
108 @Override
109 protected String doInBackground(String... sUrl)
110 { 117 {
111 try 118 try
112 { 119 {
113 // Create connection 120 // Create connection
114 URL url = new URL(sUrl[0]); 121 final URL url = new URL(sUrl[0]);
115 Log.e(TAG, "D: " + sUrl[0]); 122 Log.e(TAG, "D: " + sUrl[0]);
116 URLConnection connection = url.openConnection(); 123 final URLConnection connection = url.openConnection();
117 connection.connect(); 124 connection.connect();
118 int fileLength = connection.getContentLength(); 125 final int fileLength = connection.getContentLength();
119 Log.e(TAG, "S: " + fileLength); 126 Log.e(TAG, "S: " + fileLength);
120 127
121 // Check if file already exists 128 // Check if file already exists
122 File updateFile = new File(updateDir, "AdblockPlus-update.apk"); 129 final File updateFile = new File(UpdaterService.this.updateDir, "Adblock Plus-update.apk");
123 if (updateFile.exists()) 130 if (updateFile.exists())
124 { 131 {
125 // if (updateFile.length() == fileLength) 132 // if (updateFile.length() == fileLength)
126 // return updateFile.getAbsolutePath(); 133 // return updateFile.getAbsolutePath();
127 // else 134 // else
128 updateFile.delete(); 135 updateFile.delete();
129 } 136 }
130 137
131 // Download the file 138 // Download the file
132 InputStream input = new BufferedInputStream(url.openStream()); 139 final InputStream input = new BufferedInputStream(url.openStream());
133 OutputStream output = new FileOutputStream(updateFile); 140 final OutputStream output = new FileOutputStream(updateFile);
134 141
135 byte data[] = new byte[1024]; 142 final byte data[] = new byte[1024];
136 long total = 0; 143 long total = 0;
137 int count; 144 int count;
138 int progress = 0; 145 int progress = 0;
139 while ((count = input.read(data)) != -1) 146 while ((count = input.read(data)) != -1)
140 { 147 {
141 total += count; 148 total += count;
142 output.write(data, 0, count); 149 output.write(data, 0, count);
143 150
144 int p = (int) (total * 100 / fileLength); 151 final int p = (int)(total * 100 / fileLength);
145 if (p != progress) 152 if (p != progress)
146 { 153 {
147 publishProgress(p); 154 this.publishProgress(p);
148 progress = p; 155 progress = p;
149 } 156 }
150 } 157 }
151 158
152 output.flush(); 159 output.flush();
153 output.close(); 160 output.close();
154 input.close(); 161 input.close();
155 return updateFile.getAbsolutePath(); 162 return updateFile.getAbsolutePath();
156 } 163 }
157 catch (Exception e) 164 catch (final Exception e)
158 { 165 {
159 Log.e(TAG, "Download error", e); 166 Log.e(TAG, "Download error", e);
160 return null; 167 return null;
161 } 168 }
162 } 169 }
163 170
171 @SuppressWarnings("deprecation")
164 @Override 172 @Override
165 protected void onProgressUpdate(Integer... progress) 173 protected void onProgressUpdate(final Integer... progress)
166 { 174 {
167 notification.setLatestEventInfo(context, getString(R.string.app_name), Str ing.format(getString(R.string.msg_update_downloading), progress[0]), contentInte nt); 175 this.notification.setLatestEventInfo(this.context, UpdaterService.this.get String(R.string.app_name),
168 notificationManager.notify(notificationId, notification); 176 String.format(UpdaterService.this.getString(R.string.msg_update_downlo ading), progress[0]),
177 this.contentIntent);
178 this.notificationManager.notify(this.notificationId, this.notification);
169 } 179 }
170 180
181 @SuppressWarnings("deprecation")
171 @Override 182 @Override
172 protected void onPostExecute(String result) 183 protected void onPostExecute(final String result)
173 { 184 {
174 notificationManager.cancel(notificationId); 185 this.notificationManager.cancel(this.notificationId);
175 if (result != null) 186 if (result != null)
176 { 187 {
177 Notification notification = new Notification(); 188 final Notification notification = new Notification();
178 notification.icon = R.drawable.ic_stat_download; 189 notification.icon = R.drawable.ic_stat_download;
179 notification.when = System.currentTimeMillis(); 190 notification.when = System.currentTimeMillis();
180 notification.flags |= Notification.FLAG_AUTO_CANCEL; 191 notification.flags |= Notification.FLAG_AUTO_CANCEL;
181 Intent intent = new Intent(context, UpdaterActivity.class).addFlags(Inte nt.FLAG_ACTIVITY_NEW_TASK); 192 final Intent intent = new Intent(this.context, UpdaterActivity.class).ad dFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
182 intent.setAction("update"); 193 intent.setAction("update");
183 intent.putExtra("path", result); 194 intent.putExtra("path", result);
184 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, inte nt, PendingIntent.FLAG_UPDATE_CURRENT); 195 final PendingIntent contentIntent = PendingIntent.getActivity(this.conte xt, 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); 196 notification.setLatestEventInfo(this.context, this.context.getText(R.str ing.app_name), this.context.getString(R.string.msg_update_ready),
186 notificationManager.notify(R.string.app_name + 1, notification); 197 contentIntent);
198 this.notificationManager.notify(R.string.app_name + 1, notification);
187 } 199 }
188 } 200 }
189 } 201 }
190 } 202 }
OLDNEW

Powered by Google App Engine
This is Rietveld