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

Delta Between Two Patch Sets: src/org/adblockplus/android/updater/UpdaterService.java

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

Powered by Google App Engine
This is Rietveld