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

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

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

Powered by Google App Engine
This is Rietveld