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: Even more review issues fixed. Created April 28, 2014, 10:18 a.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 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
58 @Override 59 @Override
59 public void onStart(Intent intent, int startId) 60 public void onStart(final Intent intent, final int startId)
60 { 61 {
61 super.onStart(intent, startId); 62 super.onStart(intent, startId);
62 63
63 // Stop if media not available 64 // Stop if media not available
64 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ) 65 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) )
65 { 66 {
66 stopSelf(); 67 stopSelf();
67 return; 68 return;
68 } 69 }
69 updateDir.mkdirs(); 70 updateDir.mkdirs();
70 71
71 // Start download 72 // Start download
72 if (intent != null && intent.hasExtra("url")) 73 if (intent != null && intent.hasExtra("url"))
73 new DownloadTask(this).execute(intent.getStringExtra("url")); 74 new DownloadTask(this).execute(intent.getStringExtra("url"));
74 } 75 }
75 76
76 @Override 77 @Override
77 public IBinder onBind(Intent intent) 78 public IBinder onBind(final Intent intent)
78 { 79 {
79 return null; 80 return null;
80 } 81 }
81 82
82 private class DownloadTask extends AsyncTask<String, Integer, String> 83 private class DownloadTask extends AsyncTask<String, Integer, String>
83 { 84 {
84 private Context context; 85 private final Context context;
85 private Notification notification; 86 private final Notification notification;
86 private PendingIntent contentIntent; 87 private final PendingIntent contentIntent;
87 private NotificationManager notificationManager; 88 private final NotificationManager notificationManager;
88 private int notificationId = R.string.app_name + 2; 89 private final int notificationId = R.string.app_name + 2;
89 90
90 public DownloadTask(Context context) 91 public DownloadTask(final Context context)
91 { 92 {
92 this.context = context; 93 this.context = context;
93 notificationManager = (NotificationManager) context.getSystemService(Conte xt.NOTIFICATION_SERVICE); 94 notificationManager = (NotificationManager) context.getSystemService(Conte xt.NOTIFICATION_SERVICE);
94 notification = new Notification(); 95 notification = new Notification();
95 contentIntent = PendingIntent.getActivity(context, 0, new Intent(), Pendin gIntent.FLAG_UPDATE_CURRENT); 96 contentIntent = PendingIntent.getActivity(context, 0, new Intent(), Pendin gIntent.FLAG_UPDATE_CURRENT);
96 } 97 }
97 98
98 @Override 99 @Override
99 protected void onPreExecute() 100 protected void onPreExecute()
100 { 101 {
101 notification.flags |= Notification.FLAG_ONGOING_EVENT; 102 notification.flags |= Notification.FLAG_ONGOING_EVENT;
102 notification.when = 0; 103 notification.when = 0;
103 notification.icon = R.drawable.ic_stat_download; 104 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 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 notificationManager.notify(notificationId, notification);
106 } 107 }
107 108
108 @Override 109 @Override
109 protected String doInBackground(String... sUrl) 110 protected String doInBackground(final String... sUrl)
110 { 111 {
111 try 112 try
112 { 113 {
113 // Create connection 114 // Create connection
114 URL url = new URL(sUrl[0]); 115 final URL url = new URL(sUrl[0]);
115 Log.e(TAG, "D: " + sUrl[0]); 116 Log.e(TAG, "D: " + sUrl[0]);
116 URLConnection connection = url.openConnection(); 117 final URLConnection connection = url.openConnection();
117 connection.connect(); 118 connection.connect();
118 int fileLength = connection.getContentLength(); 119 final int fileLength = connection.getContentLength();
119 Log.e(TAG, "S: " + fileLength); 120 Log.e(TAG, "S: " + fileLength);
120 121
121 // Check if file already exists 122 // Check if file already exists
122 File updateFile = new File(updateDir, "AdblockPlus-update.apk"); 123 final File updateFile = new File(updateDir, "AdblockPlus-update.apk");
123 if (updateFile.exists()) 124 if (updateFile.exists())
124 { 125 {
125 // if (updateFile.length() == fileLength) 126 // if (updateFile.length() == fileLength)
126 // return updateFile.getAbsolutePath(); 127 // return updateFile.getAbsolutePath();
127 // else 128 // else
128 updateFile.delete(); 129 updateFile.delete();
129 } 130 }
130 131
131 // Download the file 132 // Download the file
132 InputStream input = new BufferedInputStream(url.openStream()); 133 final InputStream input = new BufferedInputStream(url.openStream());
133 OutputStream output = new FileOutputStream(updateFile); 134 final OutputStream output = new FileOutputStream(updateFile);
134 135
135 byte data[] = new byte[1024]; 136 final byte data[] = new byte[1024];
136 long total = 0; 137 long total = 0;
137 int count; 138 int count;
138 int progress = 0; 139 int progress = 0;
139 while ((count = input.read(data)) != -1) 140 while ((count = input.read(data)) != -1)
140 { 141 {
141 total += count; 142 total += count;
142 output.write(data, 0, count); 143 output.write(data, 0, count);
143 144
144 int p = (int) (total * 100 / fileLength); 145 final int p = (int) (total * 100 / fileLength);
145 if (p != progress) 146 if (p != progress)
146 { 147 {
147 publishProgress(p); 148 publishProgress(p);
148 progress = p; 149 progress = p;
149 } 150 }
150 } 151 }
151 152
152 output.flush(); 153 output.flush();
153 output.close(); 154 output.close();
154 input.close(); 155 input.close();
155 return updateFile.getAbsolutePath(); 156 return updateFile.getAbsolutePath();
156 } 157 }
157 catch (Exception e) 158 catch (final Exception e)
158 { 159 {
159 Log.e(TAG, "Download error", e); 160 Log.e(TAG, "Download error", e);
160 return null; 161 return null;
161 } 162 }
162 } 163 }
163 164
164 @Override 165 @Override
165 protected void onProgressUpdate(Integer... progress) 166 protected void onProgressUpdate(final Integer... progress)
166 { 167 {
167 notification.setLatestEventInfo(context, getString(R.string.app_name), Str ing.format(getString(R.string.msg_update_downloading), progress[0]), contentInte nt); 168 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); 169 notificationManager.notify(notificationId, notification);
169 } 170 }
170 171
171 @Override 172 @Override
172 protected void onPostExecute(String result) 173 protected void onPostExecute(final String result)
173 { 174 {
174 notificationManager.cancel(notificationId); 175 notificationManager.cancel(notificationId);
175 if (result != null) 176 if (result != null)
176 { 177 {
177 Notification notification = new Notification(); 178 final Notification notification = new Notification();
178 notification.icon = R.drawable.ic_stat_download; 179 notification.icon = R.drawable.ic_stat_download;
179 notification.when = System.currentTimeMillis(); 180 notification.when = System.currentTimeMillis();
180 notification.flags |= Notification.FLAG_AUTO_CANCEL; 181 notification.flags |= Notification.FLAG_AUTO_CANCEL;
181 Intent intent = new Intent(context, UpdaterActivity.class).addFlags(Inte nt.FLAG_ACTIVITY_NEW_TASK); 182 final Intent intent = new Intent(context, UpdaterActivity.class).addFlag s(Intent.FLAG_ACTIVITY_NEW_TASK);
182 intent.setAction("update"); 183 intent.setAction("update");
183 intent.putExtra("path", result); 184 intent.putExtra("path", result);
184 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, inte nt, PendingIntent.FLAG_UPDATE_CURRENT); 185 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); 186 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); 187 notificationManager.notify(R.string.app_name + 1, notification);
187 } 188 }
188 } 189 }
189 } 190 }
190 } 191 }
OLDNEW
« no previous file with comments | « src/org/adblockplus/android/updater/UpdaterActivity.java ('k') | src/org/adblockplus/brazil/BaseRequestHandler.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld