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

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

Issue 5697499218051072: Usage of new API, cleanups (reduced) (Closed)
Patch Set: Created April 11, 2014, 1:31 p.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
(...skipping 12 matching lines...) Expand all
23 import java.net.URISyntaxException; 23 import java.net.URISyntaxException;
24 import java.net.URL; 24 import java.net.URL;
25 import java.util.Locale; 25 import java.util.Locale;
26 26
27 import javax.xml.parsers.DocumentBuilder; 27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory; 28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException; 29 import javax.xml.parsers.ParserConfigurationException;
30 30
31 import org.adblockplus.android.AdblockPlus; 31 import org.adblockplus.android.AdblockPlus;
32 import org.adblockplus.android.R; 32 import org.adblockplus.android.R;
33 import org.adblockplus.android.Utils;
33 import org.apache.http.HttpEntity; 34 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse; 35 import org.apache.http.HttpResponse;
35 import org.apache.http.client.methods.HttpGet; 36 import org.apache.http.client.methods.HttpGet;
36 import org.apache.http.impl.client.DefaultHttpClient; 37 import org.apache.http.impl.client.DefaultHttpClient;
37 import org.apache.http.util.EntityUtils; 38 import org.apache.http.util.EntityUtils;
38 import org.w3c.dom.Document; 39 import org.w3c.dom.Document;
39 import org.w3c.dom.Element; 40 import org.w3c.dom.Element;
40 import org.w3c.dom.NodeList; 41 import org.w3c.dom.NodeList;
41 import org.xml.sax.InputSource; 42 import org.xml.sax.InputSource;
42 import org.xml.sax.SAXException; 43 import org.xml.sax.SAXException;
43 44
44 import android.app.Notification; 45 import android.app.Notification;
45 import android.app.NotificationManager; 46 import android.app.NotificationManager;
46 import android.app.PendingIntent; 47 import android.app.PendingIntent;
47 import android.content.BroadcastReceiver; 48 import android.content.BroadcastReceiver;
48 import android.content.Context; 49 import android.content.Context;
49 import android.content.Intent; 50 import android.content.Intent;
50 import android.content.pm.PackageManager.NameNotFoundException; 51 import android.content.pm.PackageManager.NameNotFoundException;
51 import android.net.ConnectivityManager; 52 import android.net.ConnectivityManager;
52 import android.net.NetworkInfo; 53 import android.net.NetworkInfo;
53 import android.os.Build; 54 import android.os.Build;
54 import android.util.Log; 55 import android.util.Log;
55 56
56 /** 57 /**
57 * Processes Alarm event to check for update availability. 58 * Processes Alarm event to check for update availability.
58 */ 59 */
59 public class AlarmReceiver extends BroadcastReceiver 60 public class AlarmReceiver extends BroadcastReceiver
60 { 61 {
62 private final static String TAG = Utils.getTag(AlarmReceiver.class);
63 private final static int NOTIFICATION_ID = R.string.app_name + 1;
61 64
62 private static final String TAG = "AlarmReceiver"; 65 @SuppressWarnings("deprecation")
63 private static final int NOTIFICATION_ID = R.string.app_name + 1;
64
65 @Override 66 @Override
66 public void onReceive(final Context context, final Intent intent) 67 public void onReceive(final Context context, final Intent intent)
67 { 68 {
68 Log.i(TAG, "Alarm; requesting updater service"); 69 Log.i(TAG, "Alarm; requesting updater service");
69 70
70 final AdblockPlus application = AdblockPlus.getApplication(); 71 final AdblockPlus application = AdblockPlus.getApplication();
71 72
72 // Indicates manual (immediate) update check which requires response to user . 73 // Indicates manual (immediate) update check which requires response to user .
73 final boolean notify = intent.getBooleanExtra("notifynoupdate", false); 74 final boolean notify = intent.getBooleanExtra("notifynoupdate", false);
74 75
75 // Check network availability 76 // Check network availability
76 boolean connected = false; 77 boolean connected = false;
77 ConnectivityManager connectivityManager = (ConnectivityManager) context.getS ystemService(Context.CONNECTIVITY_SERVICE); 78 final ConnectivityManager connectivityManager = (ConnectivityManager)context .getSystemService(Context.CONNECTIVITY_SERVICE);
78 NetworkInfo networkInfo = null; 79 NetworkInfo networkInfo = null;
79 if (connectivityManager != null) 80 if (connectivityManager != null)
80 { 81 {
81 networkInfo = connectivityManager.getActiveNetworkInfo(); 82 networkInfo = connectivityManager.getActiveNetworkInfo();
82 connected = networkInfo == null ? false : networkInfo.isConnected(); 83 connected = networkInfo == null ? false : networkInfo.isConnected();
83 } 84 }
84 85
85 // Prepare notification 86 // Prepare notification
86 final NotificationManager notificationManager = (NotificationManager) contex t.getSystemService(Context.NOTIFICATION_SERVICE); 87 final NotificationManager notificationManager = (NotificationManager)context .getSystemService(Context.NOTIFICATION_SERVICE);
87 final Notification notification = new Notification(); 88 final Notification notification = new Notification();
88 notification.icon = R.drawable.ic_stat_warning; 89 notification.icon = R.drawable.ic_stat_warning;
89 notification.when = System.currentTimeMillis(); 90 notification.when = System.currentTimeMillis();
90 notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY _ALERT_ONCE; 91 notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY _ALERT_ONCE;
91 if (notify) 92 if (notify)
93 {
92 notification.flags |= Notification.DEFAULT_SOUND; 94 notification.flags |= Notification.DEFAULT_SOUND;
95 }
93 final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); 96 final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
94 97
95 // Get update info 98 // Get update info
96 if (application.checkWriteExternalPermission() && connected) 99 if (application.checkWriteExternalPermission() && connected)
97 { 100 {
98 Thread thread = new Thread(new Runnable() { 101 final Thread thread = new Thread(new Runnable()
102 {
99 @Override 103 @Override
100 public void run() 104 public void run()
101 { 105 {
102 boolean success = false; 106 boolean success = false;
103 try 107 try
104 { 108 {
105 // Read updates manifest 109 // Read updates manifest
106 DefaultHttpClient httpClient = new DefaultHttpClient(); 110 final DefaultHttpClient httpClient = new DefaultHttpClient();
107 111
108 String locale = Locale.getDefault().toString().toLowerCase(); 112 final String locale = Locale.getDefault().toString().toLowerCase();
109 String device = AdblockPlus.getDeviceName(); 113 final String device = AdblockPlus.getDeviceName();
110 boolean releaseBuild = context.getResources().getBoolean(R.bool.def_ release); 114 final boolean releaseBuild = context.getResources().getBoolean(R.boo l.def_release);
111 String updateUrlTemplate = context.getString(releaseBuild ? R.string .update_url : R.string.devbuild_update_url); 115 final String updateUrlTemplate = context.getString(releaseBuild ? R. string.update_url : R.string.devbuild_update_url);
112 URL updateUrl = new URL(String.format(updateUrlTemplate, Build.VERSI ON.SDK_INT, AdblockPlus.getApplication().getBuildNumber(), locale, device)); 116 final URL updateUrl = new URL(String.format(updateUrlTemplate, Build .VERSION.SDK_INT, AdblockPlus.getApplication().getBuildNumber(),
117 locale, device));
113 // The following line correctly url-encodes query string parameters 118 // The following line correctly url-encodes query string parameters
114 URI uri = new URI(updateUrl.getProtocol(), updateUrl.getUserInfo(), updateUrl.getHost(), updateUrl.getPort(), updateUrl.getPath(), updateUrl.getQuer y(), updateUrl.getRef()); 119 final URI uri = new URI(updateUrl.getProtocol(), updateUrl.getUserIn fo(), updateUrl.getHost(), updateUrl.getPort(), updateUrl.getPath(),
115 HttpGet httpGet = new HttpGet(uri); 120 updateUrl.getQuery(), updateUrl.getRef());
121 final HttpGet httpGet = new HttpGet(uri);
116 122
117 HttpResponse httpResponse = httpClient.execute(httpGet); 123 final HttpResponse httpResponse = httpClient.execute(httpGet);
118 HttpEntity httpEntity = httpResponse.getEntity(); 124 final HttpEntity httpEntity = httpResponse.getEntity();
119 String xml = EntityUtils.toString(httpEntity); 125 final String xml = EntityUtils.toString(httpEntity);
120 126
121 // Parse XML 127 // Parse XML
122 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 128 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstanc e();
123 DocumentBuilder db = dbf.newDocumentBuilder(); 129 final DocumentBuilder db = dbf.newDocumentBuilder();
124 130
125 InputSource is = new InputSource(); 131 final InputSource is = new InputSource();
126 is.setCharacterStream(new StringReader(xml)); 132 is.setCharacterStream(new StringReader(xml));
127 Document doc = db.parse(is); 133 final Document doc = db.parse(is);
128 134
129 // Find best match 135 // Find best match
130 NodeList nl = doc.getElementsByTagName("updatecheck"); 136 final NodeList nl = doc.getElementsByTagName("updatecheck");
131 int newBuild = -1; 137 int newBuild = -1;
132 int newApi = -1; 138 int newApi = -1;
133 String newUrl = null; 139 String newUrl = null;
134 for (int i = 0; i < nl.getLength(); i++) 140 for (int i = 0; i < nl.getLength(); i++)
135 { 141 {
136 Element e = (Element) nl.item(i); 142 final Element e = (Element)nl.item(i);
137 String url = e.getAttribute("package"); 143 final String url = e.getAttribute("package");
138 int build = Integer.parseInt(e.getAttribute("build")); 144 final int build = Integer.parseInt(e.getAttribute("build"));
139 int api = Integer.parseInt(e.getAttribute("api")); 145 final int api = Integer.parseInt(e.getAttribute("api"));
140 if (api > android.os.Build.VERSION.SDK_INT) 146 if (api > android.os.Build.VERSION.SDK_INT)
147 {
141 continue; 148 continue;
149 }
142 if ((build > newBuild) || (build == newBuild && api > newApi)) 150 if ((build > newBuild) || (build == newBuild && api > newApi))
143 { 151 {
144 newBuild = build; 152 newBuild = build;
145 newApi = api; 153 newApi = api;
146 newUrl = url; 154 newUrl = url;
147 } 155 }
148 } 156 }
149 157
150 int thisBuild = context.getPackageManager().getPackageInfo(context.g etPackageName(), 0).versionCode; 158 final int thisBuild = context.getPackageManager().getPackageInfo(con text.getPackageName(), 0).versionCode;
151 159
152 // Notify user if newer update was found 160 // Notify user if newer update was found
153 if (thisBuild < newBuild) 161 if (thisBuild < newBuild)
154 { 162 {
155 notification.icon = R.drawable.ic_stat_download; 163 notification.icon = R.drawable.ic_stat_download;
156 Intent intent = new Intent(context, UpdaterActivity.class).addFlag s(Intent.FLAG_ACTIVITY_NEW_TASK); 164 final Intent intent = new Intent(context, UpdaterActivity.class).a ddFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
157 intent.setAction("download"); 165 intent.setAction("download");
158 intent.putExtra("url", newUrl); 166 intent.putExtra("url", newUrl);
159 intent.putExtra("build", newBuild); 167 intent.putExtra("build", newBuild);
160 PendingIntent updateIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 168 final PendingIntent updateIntent = PendingIntent.getActivity(conte xt, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
161 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_available), updateIntent); 169 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_available),
170 updateIntent);
162 notificationManager.notify(NOTIFICATION_ID, notification); 171 notificationManager.notify(NOTIFICATION_ID, notification);
163 } 172 }
164 // Notify user that no update was found 173 // Notify user that no update was found
165 else if (notify) 174 else if (notify)
166 { 175 {
167 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_missing), emptyIntent); 176 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_missing),
177 emptyIntent);
168 notificationManager.notify(NOTIFICATION_ID, notification); 178 notificationManager.notify(NOTIFICATION_ID, notification);
169 } 179 }
170 success = true; 180 success = true;
171 } 181 }
172 catch (IOException e) 182 catch (final IOException e)
173 { 183 {
174 } 184 }
175 catch (NumberFormatException e) 185 catch (final NumberFormatException e)
176 { 186 {
177 } 187 }
178 catch (NameNotFoundException e) 188 catch (final NameNotFoundException e)
179 { 189 {
180 } 190 }
181 catch (ParserConfigurationException e) 191 catch (final ParserConfigurationException e)
182 { 192 {
183 } 193 }
184 catch (SAXException e) 194 catch (final SAXException e)
185 { 195 {
186 Log.e(TAG, "Error", e); 196 Log.e(TAG, "Error", e);
187 } 197 }
188 catch (URISyntaxException e) 198 catch (final URISyntaxException e)
189 { 199 {
190 Log.e(TAG, "Error", e); 200 Log.e(TAG, "Error", e);
191 } 201 }
192 finally 202 finally
193 { 203 {
194 if (!success) 204 if (!success)
195 { 205 {
196 // Notify user about failure 206 // Notify user about failure
197 if (notify) 207 if (notify)
198 { 208 {
(...skipping 14 matching lines...) Expand all
213 if (notify) 223 if (notify)
214 { 224 {
215 notification.setLatestEventInfo(context, context.getText(R.string.app_na me), context.getString(R.string.msg_update_fail), emptyIntent); 225 notification.setLatestEventInfo(context, context.getText(R.string.app_na me), context.getString(R.string.msg_update_fail), emptyIntent);
216 notificationManager.notify(NOTIFICATION_ID, notification); 226 notificationManager.notify(NOTIFICATION_ID, notification);
217 } 227 }
218 // Schedule retry in 30 minutes - there is no connection available at this time 228 // Schedule retry in 30 minutes - there is no connection available at this time
219 application.scheduleUpdater(30); 229 application.scheduleUpdater(30);
220 } 230 }
221 } 231 }
222 } 232 }
OLDNEW

Powered by Google App Engine
This is Rietveld