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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/org/adblockplus/android/updater/UpdaterService.java
diff --git a/src/org/adblockplus/android/updater/UpdaterService.java b/src/org/adblockplus/android/updater/UpdaterService.java
index b462d2f925edea19406a580a96e934382834320b..d8c3f133fc4b0cf4a7d393834a1a65334fcc4c7a 100644
--- a/src/org/adblockplus/android/updater/UpdaterService.java
+++ b/src/org/adblockplus/android/updater/UpdaterService.java
@@ -26,6 +26,7 @@ import java.net.URL;
import java.net.URLConnection;
import org.adblockplus.android.R;
+import org.adblockplus.android.Utils;
import android.app.Notification;
import android.app.NotificationManager;
@@ -43,7 +44,7 @@ import android.util.Log;
*/
public class UpdaterService extends Service
{
- private final static String TAG = "UpdaterService";
+ private final static String TAG = Utils.getTag(UpdaterService.class);
private File updateDir;
@@ -52,74 +53,80 @@ public class UpdaterService extends Service
{
super.onCreate();
// Use common Android path for downloads
- updateDir = new File(Environment.getExternalStorageDirectory().getPath(), "downloads");
+ this.updateDir = new File(Environment.getExternalStorageDirectory().getPath(), "downloads");
}
+ @SuppressWarnings("deprecation")
@Override
- public void onStart(Intent intent, int startId)
+ public void onStart(final Intent intent, final int startId)
{
super.onStart(intent, startId);
// Stop if media not available
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
- stopSelf();
+ this.stopSelf();
return;
}
- updateDir.mkdirs();
+ this.updateDir.mkdirs();
// Start download
if (intent != null && intent.hasExtra("url"))
+ {
new DownloadTask(this).execute(intent.getStringExtra("url"));
+ }
}
@Override
- public IBinder onBind(Intent intent)
+ public IBinder onBind(final Intent intent)
{
return null;
}
private class DownloadTask extends AsyncTask<String, Integer, String>
{
- private Context context;
- private Notification notification;
- private PendingIntent contentIntent;
- private NotificationManager notificationManager;
- private int notificationId = R.string.app_name + 2;
+ private final Context context;
+ private final Notification notification;
+ private final PendingIntent contentIntent;
+ private final NotificationManager notificationManager;
+ private final int notificationId = R.string.app_name + 2;
- public DownloadTask(Context context)
+ public DownloadTask(final Context context)
{
this.context = context;
- notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- notification = new Notification();
- contentIntent = PendingIntent.getActivity(context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
+ this.notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
+ this.notification = new Notification();
+ this.contentIntent = PendingIntent.getActivity(context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
}
+ @SuppressWarnings("deprecation")
@Override
protected void onPreExecute()
{
- notification.flags |= Notification.FLAG_ONGOING_EVENT;
- notification.when = 0;
- notification.icon = R.drawable.ic_stat_download;
- notification.setLatestEventInfo(context, getString(R.string.app_name), String.format(getString(R.string.msg_update_downloading), 0), contentIntent);
- notificationManager.notify(notificationId, notification);
+ this.notification.flags |= Notification.FLAG_ONGOING_EVENT;
+ this.notification.when = 0;
+ this.notification.icon = R.drawable.ic_stat_download;
+ this.notification.setLatestEventInfo(this.context, UpdaterService.this.getString(R.string.app_name),
+ String.format(UpdaterService.this.getString(R.string.msg_update_downloading), 0),
+ this.contentIntent);
+ this.notificationManager.notify(this.notificationId, this.notification);
}
@Override
- protected String doInBackground(String... sUrl)
+ protected String doInBackground(final String... sUrl)
{
try
{
// Create connection
- URL url = new URL(sUrl[0]);
+ final URL url = new URL(sUrl[0]);
Log.e(TAG, "D: " + sUrl[0]);
- URLConnection connection = url.openConnection();
+ final URLConnection connection = url.openConnection();
connection.connect();
- int fileLength = connection.getContentLength();
+ final int fileLength = connection.getContentLength();
Log.e(TAG, "S: " + fileLength);
// Check if file already exists
- File updateFile = new File(updateDir, "AdblockPlus-update.apk");
+ final File updateFile = new File(UpdaterService.this.updateDir, "AdblockPlus-update.apk");
if (updateFile.exists())
{
// if (updateFile.length() == fileLength)
@@ -129,10 +136,10 @@ public class UpdaterService extends Service
}
// Download the file
- InputStream input = new BufferedInputStream(url.openStream());
- OutputStream output = new FileOutputStream(updateFile);
+ final InputStream input = new BufferedInputStream(url.openStream());
+ final OutputStream output = new FileOutputStream(updateFile);
- byte data[] = new byte[1024];
+ final byte data[] = new byte[1024];
long total = 0;
int count;
int progress = 0;
@@ -141,10 +148,10 @@ public class UpdaterService extends Service
total += count;
output.write(data, 0, count);
- int p = (int) (total * 100 / fileLength);
+ final int p = (int)(total * 100 / fileLength);
if (p != progress)
{
- publishProgress(p);
+ this.publishProgress(p);
progress = p;
}
}
@@ -154,36 +161,41 @@ public class UpdaterService extends Service
input.close();
return updateFile.getAbsolutePath();
}
- catch (Exception e)
+ catch (final Exception e)
{
Log.e(TAG, "Download error", e);
return null;
}
}
+ @SuppressWarnings("deprecation")
@Override
- protected void onProgressUpdate(Integer... progress)
+ protected void onProgressUpdate(final Integer... progress)
{
- notification.setLatestEventInfo(context, getString(R.string.app_name), String.format(getString(R.string.msg_update_downloading), progress[0]), contentIntent);
- notificationManager.notify(notificationId, notification);
+ this.notification.setLatestEventInfo(this.context, UpdaterService.this.getString(R.string.app_name),
+ String.format(UpdaterService.this.getString(R.string.msg_update_downloading), progress[0]),
+ this.contentIntent);
+ this.notificationManager.notify(this.notificationId, this.notification);
}
+ @SuppressWarnings("deprecation")
@Override
- protected void onPostExecute(String result)
+ protected void onPostExecute(final String result)
{
- notificationManager.cancel(notificationId);
+ this.notificationManager.cancel(this.notificationId);
if (result != null)
{
- Notification notification = new Notification();
+ final Notification notification = new Notification();
notification.icon = R.drawable.ic_stat_download;
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
- Intent intent = new Intent(context, UpdaterActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ final Intent intent = new Intent(this.context, UpdaterActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("update");
intent.putExtra("path", result);
- PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- notification.setLatestEventInfo(context, context.getText(R.string.app_name), context.getString(R.string.msg_update_ready), contentIntent);
- notificationManager.notify(R.string.app_name + 1, notification);
+ final PendingIntent contentIntent = PendingIntent.getActivity(this.context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+ notification.setLatestEventInfo(this.context, this.context.getText(R.string.app_name), this.context.getString(R.string.msg_update_ready),
+ contentIntent);
+ this.notificationManager.notify(R.string.app_name + 1, notification);
}
}
}

Powered by Google App Engine
This is Rietveld