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

Side by Side Diff: src/org/adblockplus/android/updater/AlarmReceiver.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 | « no previous file | src/org/adblockplus/android/updater/UpdaterActivity.java » ('j') | 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.IOException;
4 import java.io.StringReader;
5
6 import javax.xml.parsers.DocumentBuilder;
7 import javax.xml.parsers.DocumentBuilderFactory;
8 import javax.xml.parsers.ParserConfigurationException;
9
10 import org.adblockplus.android.AdblockPlus;
11 import org.adblockplus.android.R;
12 import org.apache.http.HttpEntity;
13 import org.apache.http.HttpResponse;
14 import org.apache.http.client.methods.HttpGet;
15 import org.apache.http.impl.client.DefaultHttpClient;
16 import org.apache.http.util.EntityUtils;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.NodeList;
20 import org.xml.sax.InputSource;
21 import org.xml.sax.SAXException;
22
23 import android.app.Notification;
24 import android.app.NotificationManager;
25 import android.app.PendingIntent;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.pm.PackageManager.NameNotFoundException;
30 import android.net.ConnectivityManager;
31 import android.net.NetworkInfo;
32 import android.util.Log;
33
34 /**
35 * Processes Alarm event to check for update availability.
36 */
37 public class AlarmReceiver extends BroadcastReceiver
38 {
39
40 private static final String TAG = "AlarmReceiver";
41 private static final int NOTIFICATION_ID = R.string.app_name + 1;
42
43 @Override
44 public void onReceive(final Context context, final Intent intent)
45 {
46 Log.i(TAG, "Alarm; requesting updater service");
47
48 final AdblockPlus application = AdblockPlus.getApplication();
49
50 // Indicates manual (immediate) update check which requires response to user .
51 final boolean notify = intent.getBooleanExtra("notifynoupdate", false);
52
53 // Check network availability
54 boolean connected = false;
55 ConnectivityManager connectivityManager = (ConnectivityManager) context.getS ystemService(Context.CONNECTIVITY_SERVICE);
56 NetworkInfo networkInfo = null;
57 if (connectivityManager != null)
58 {
59 networkInfo = connectivityManager.getActiveNetworkInfo();
60 connected = networkInfo == null ? false : networkInfo.isConnected();
61 }
62
63 // Prepare notification
64 final NotificationManager notificationManager = (NotificationManager) contex t.getSystemService(Context.NOTIFICATION_SERVICE);
65 final Notification notification = new Notification();
66 notification.icon = R.drawable.ic_stat_warning;
67 notification.when = System.currentTimeMillis();
68 notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY _ALERT_ONCE;
69 if (notify)
70 notification.flags |= Notification.DEFAULT_SOUND;
71 final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
72
73 // Get update info
74 if (application.checkWriteExternalPermission() && connected)
75 {
76 Thread thread = new Thread(new Runnable() {
77 @Override
78 public void run()
79 {
80 boolean success = false;
81 try
82 {
83 // Read updates manifest
84 DefaultHttpClient httpClient = new DefaultHttpClient();
85 HttpGet httpGet = new HttpGet(context.getString(R.string.update_url) );
86
87 HttpResponse httpResponse = httpClient.execute(httpGet);
88 HttpEntity httpEntity = httpResponse.getEntity();
89 String xml = EntityUtils.toString(httpEntity);
90
91 // Parse XML
92 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
93 DocumentBuilder db = dbf.newDocumentBuilder();
94
95 InputSource is = new InputSource();
96 is.setCharacterStream(new StringReader(xml));
97 Document doc = db.parse(is);
98
99 // Find best match
100 NodeList nl = doc.getElementsByTagName("updatecheck");
101 int newBuild = -1;
102 int newApi = -1;
103 String newUrl = null;
104 for (int i = 0; i < nl.getLength(); i++)
105 {
106 Element e = (Element) nl.item(i);
107 String url = e.getAttribute("package");
108 int build = Integer.parseInt(e.getAttribute("build"));
109 int api = Integer.parseInt(e.getAttribute("api"));
110 if (api > android.os.Build.VERSION.SDK_INT)
111 continue;
112 if ((build > newBuild) || (build == newBuild && api > newApi))
113 {
114 newBuild = build;
115 newApi = api;
116 newUrl = url;
117 }
118 }
119
120 int thisBuild = context.getPackageManager().getPackageInfo(context.g etPackageName(), 0).versionCode;
121
122 // Notify user if newer update was found
123 if (thisBuild < newBuild)
124 {
125 notification.icon = R.drawable.ic_stat_download;
126 Intent intent = new Intent(context, UpdaterActivity.class).addFlag s(Intent.FLAG_ACTIVITY_NEW_TASK);
127 intent.setAction("download");
128 intent.putExtra("url", newUrl);
129 intent.putExtra("build", newBuild);
130 PendingIntent updateIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
131 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_available), updateIntent);
132 notificationManager.notify(NOTIFICATION_ID, notification);
133 }
134 // Notify user that no update was found
135 else if (notify)
136 {
137 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_missing), emptyIntent);
138 notificationManager.notify(NOTIFICATION_ID, notification);
139 }
140 success = true;
141 // Schedule next check
142 application.scheduleUpdater(0);
143 }
144 catch (IOException e)
145 {
146 }
147 catch (NumberFormatException e)
148 {
149 }
150 catch (NameNotFoundException e)
151 {
152 }
153 catch (ParserConfigurationException e)
154 {
155 }
156 catch (SAXException e)
157 {
158 Log.e(TAG, "Error", e);
159 }
160 finally
161 {
162 if (!success)
163 {
164 // Notify user about failure
165 if (notify)
166 {
167 notification.setLatestEventInfo(context, context.getText(R.strin g.app_name), context.getString(R.string.msg_update_fail), emptyIntent);
168 notificationManager.notify(NOTIFICATION_ID, notification);
169 }
170 // Schedule retry in 1 hour - this is most probably problem on ser ver
171 application.scheduleUpdater(60);
172 }
173 }
174 }
175 });
176 thread.start();
177 }
178 else
179 {
180 // Notify user about failure
181 if (notify)
182 {
183 notification.setLatestEventInfo(context, context.getText(R.string.app_na me), context.getString(R.string.msg_update_fail), emptyIntent);
184 notificationManager.notify(NOTIFICATION_ID, notification);
185 }
186 // Schedule retry in 30 minutes - there is no connection available at this time
187 application.scheduleUpdater(30);
188 }
189 }
190 }
OLDNEW
« no previous file with comments | « no previous file | src/org/adblockplus/android/updater/UpdaterActivity.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld