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

Delta Between Two Patch Sets: src/org/adblockplus/android/updater/AlarmReceiver.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
« no previous file with change/comment | « src/org/adblockplus/android/Utils.java ('k') | src/org/adblockplus/android/updater/UpdaterActivity.java » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 import android.net.ConnectivityManager; 52 import android.net.ConnectivityManager;
53 import android.net.NetworkInfo; 53 import android.net.NetworkInfo;
54 import android.os.Build; 54 import android.os.Build;
55 import android.util.Log; 55 import android.util.Log;
56 56
57 /** 57 /**
58 * Processes Alarm event to check for update availability. 58 * Processes Alarm event to check for update availability.
59 */ 59 */
60 public class AlarmReceiver extends BroadcastReceiver 60 public class AlarmReceiver extends BroadcastReceiver
61 { 61 {
62 private final static String TAG = Utils.getTag(AlarmReceiver.class); 62 private static final String TAG = Utils.getTag(AlarmReceiver.class);
63 private final static int NOTIFICATION_ID = R.string.app_name + 1; 63 private static final int NOTIFICATION_ID = R.string.app_name + 1;
64 64
65 @SuppressWarnings("deprecation")
66 @Override 65 @Override
67 public void onReceive(final Context context, final Intent intent) 66 public void onReceive(final Context context, final Intent intent)
68 { 67 {
69 Log.i(TAG, "Alarm; requesting updater service"); 68 Log.i(TAG, "Alarm; requesting updater service");
70 69
71 final AdblockPlus application = AdblockPlus.getApplication(); 70 final AdblockPlus application = AdblockPlus.getApplication();
72 71
73 // Indicates manual (immediate) update check which requires response to user . 72 // Indicates manual (immediate) update check which requires response to user .
74 final boolean notify = intent.getBooleanExtra("notifynoupdate", false); 73 final boolean notify = intent.getBooleanExtra("notifynoupdate", false);
75 74
76 // Check network availability 75 // Check network availability
77 boolean connected = false; 76 boolean connected = false;
78 final ConnectivityManager connectivityManager = (ConnectivityManager)context .getSystemService(Context.CONNECTIVITY_SERVICE); 77 final ConnectivityManager connectivityManager = (ConnectivityManager) contex t.getSystemService(Context.CONNECTIVITY_SERVICE);
79 NetworkInfo networkInfo = null; 78 NetworkInfo networkInfo = null;
80 if (connectivityManager != null) 79 if (connectivityManager != null)
81 { 80 {
82 networkInfo = connectivityManager.getActiveNetworkInfo(); 81 networkInfo = connectivityManager.getActiveNetworkInfo();
83 connected = networkInfo == null ? false : networkInfo.isConnected(); 82 connected = networkInfo == null ? false : networkInfo.isConnected();
84 } 83 }
85 84
86 // Prepare notification 85 // Prepare notification
87 final NotificationManager notificationManager = (NotificationManager)context .getSystemService(Context.NOTIFICATION_SERVICE); 86 final NotificationManager notificationManager = (NotificationManager) contex t.getSystemService(Context.NOTIFICATION_SERVICE);
88 final Notification notification = new Notification(); 87 final Notification notification = new Notification();
89 notification.icon = R.drawable.ic_stat_warning; 88 notification.icon = R.drawable.ic_stat_warning;
90 notification.when = System.currentTimeMillis(); 89 notification.when = System.currentTimeMillis();
91 notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY _ALERT_ONCE; 90 notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY _ALERT_ONCE;
92 if (notify) 91 if (notify)
93 {
94 notification.flags |= Notification.DEFAULT_SOUND; 92 notification.flags |= Notification.DEFAULT_SOUND;
95 }
96 final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); 93 final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
97 94
98 // Get update info 95 // Get update info
99 if (application.checkWriteExternalPermission() && connected) 96 if (application.checkWriteExternalPermission() && connected)
100 { 97 {
101 final Thread thread = new Thread(new Runnable() 98 final Thread thread = new Thread(new Runnable()
102 { 99 {
103 @Override 100 @Override
104 public void run() 101 public void run()
105 { 102 {
106 boolean success = false; 103 boolean success = false;
107 try 104 try
108 { 105 {
109 // Read updates manifest 106 // Read updates manifest
110 final DefaultHttpClient httpClient = new DefaultHttpClient(); 107 final DefaultHttpClient httpClient = new DefaultHttpClient();
111 108
112 final String locale = Locale.getDefault().toString().toLowerCase(); 109 final String locale = Locale.getDefault().toString().toLowerCase();
113 final String device = AdblockPlus.getDeviceName(); 110 final String device = AdblockPlus.getDeviceName();
114 final boolean releaseBuild = context.getResources().getBoolean(R.boo l.def_release); 111 final boolean releaseBuild = context.getResources().getBoolean(R.boo l.def_release);
115 final String updateUrlTemplate = context.getString(releaseBuild ? R. string.update_url : R.string.devbuild_update_url); 112 final String updateUrlTemplate = context.getString(releaseBuild ? R. string.update_url : R.string.devbuild_update_url);
116 final URL updateUrl = new URL(String.format(updateUrlTemplate, Build .VERSION.SDK_INT, AdblockPlus.getApplication().getBuildNumber(), 113 final URL updateUrl = new URL(String.format(updateUrlTemplate, Build .VERSION.SDK_INT, AdblockPlus.getApplication().getBuildNumber(), locale, device) );
117 locale, device));
118 // The following line correctly url-encodes query string parameters 114 // The following line correctly url-encodes query string parameters
119 final URI uri = new URI(updateUrl.getProtocol(), updateUrl.getUserIn fo(), updateUrl.getHost(), updateUrl.getPort(), updateUrl.getPath(), 115 final URI uri = new URI(updateUrl.getProtocol(), updateUrl.getUserIn fo(), updateUrl.getHost(), updateUrl.getPort(), updateUrl.getPath(), updateUrl.g etQuery(), updateUrl.getRef());
120 updateUrl.getQuery(), updateUrl.getRef());
121 final HttpGet httpGet = new HttpGet(uri); 116 final HttpGet httpGet = new HttpGet(uri);
122 117
123 final HttpResponse httpResponse = httpClient.execute(httpGet); 118 final HttpResponse httpResponse = httpClient.execute(httpGet);
124 final HttpEntity httpEntity = httpResponse.getEntity(); 119 final HttpEntity httpEntity = httpResponse.getEntity();
125 final String xml = EntityUtils.toString(httpEntity); 120 final String xml = EntityUtils.toString(httpEntity);
126 121
127 // Parse XML 122 // Parse XML
128 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstanc e(); 123 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstanc e();
129 final DocumentBuilder db = dbf.newDocumentBuilder(); 124 final DocumentBuilder db = dbf.newDocumentBuilder();
130 125
131 final InputSource is = new InputSource(); 126 final InputSource is = new InputSource();
132 is.setCharacterStream(new StringReader(xml)); 127 is.setCharacterStream(new StringReader(xml));
133 final Document doc = db.parse(is); 128 final Document doc = db.parse(is);
134 129
135 // Find best match 130 // Find best match
136 final NodeList nl = doc.getElementsByTagName("updatecheck"); 131 final NodeList nl = doc.getElementsByTagName("updatecheck");
137 int newBuild = -1; 132 int newBuild = -1;
138 int newApi = -1; 133 int newApi = -1;
139 String newUrl = null; 134 String newUrl = null;
140 for (int i = 0; i < nl.getLength(); i++) 135 for (int i = 0; i < nl.getLength(); i++)
141 { 136 {
142 final Element e = (Element)nl.item(i); 137 final Element e = (Element) nl.item(i);
143 final String url = e.getAttribute("package"); 138 final String url = e.getAttribute("package");
144 final int build = Integer.parseInt(e.getAttribute("build")); 139 final int build = Integer.parseInt(e.getAttribute("build"));
145 final int api = Integer.parseInt(e.getAttribute("api")); 140 final int api = Integer.parseInt(e.getAttribute("api"));
146 if (api > android.os.Build.VERSION.SDK_INT) 141 if (api > android.os.Build.VERSION.SDK_INT)
147 {
148 continue; 142 continue;
149 }
150 if ((build > newBuild) || (build == newBuild && api > newApi)) 143 if ((build > newBuild) || (build == newBuild && api > newApi))
151 { 144 {
152 newBuild = build; 145 newBuild = build;
153 newApi = api; 146 newApi = api;
154 newUrl = url; 147 newUrl = url;
155 } 148 }
156 } 149 }
157 150
158 final int thisBuild = context.getPackageManager().getPackageInfo(con text.getPackageName(), 0).versionCode; 151 final int thisBuild = context.getPackageManager().getPackageInfo(con text.getPackageName(), 0).versionCode;
159 152
160 // Notify user if newer update was found 153 // Notify user if newer update was found
161 if (thisBuild < newBuild) 154 if (thisBuild < newBuild)
162 { 155 {
163 notification.icon = R.drawable.ic_stat_download; 156 notification.icon = R.drawable.ic_stat_download;
164 final Intent intent = new Intent(context, UpdaterActivity.class).a ddFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 157 final Intent intent = new Intent(context, UpdaterActivity.class).a ddFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
165 intent.setAction("download"); 158 intent.setAction("download");
166 intent.putExtra("url", newUrl); 159 intent.putExtra("url", newUrl);
167 intent.putExtra("build", newBuild); 160 intent.putExtra("build", newBuild);
168 final PendingIntent updateIntent = PendingIntent.getActivity(conte xt, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 161 final PendingIntent updateIntent = PendingIntent.getActivity(conte xt, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
169 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_available), 162 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_available), updateIntent);
170 updateIntent);
171 notificationManager.notify(NOTIFICATION_ID, notification); 163 notificationManager.notify(NOTIFICATION_ID, notification);
172 } 164 }
173 // Notify user that no update was found 165 // Notify user that no update was found
174 else if (notify) 166 else if (notify)
175 { 167 {
176 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_missing), 168 notification.setLatestEventInfo(context, context.getText(R.string. app_name), context.getString(R.string.msg_update_missing), emptyIntent);
177 emptyIntent);
178 notificationManager.notify(NOTIFICATION_ID, notification); 169 notificationManager.notify(NOTIFICATION_ID, notification);
179 } 170 }
180 success = true; 171 success = true;
181 } 172 }
182 catch (final IOException e) 173 catch (final IOException e)
183 { 174 {
184 } 175 }
185 catch (final NumberFormatException e) 176 catch (final NumberFormatException e)
186 { 177 {
187 } 178 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 if (notify) 214 if (notify)
224 { 215 {
225 notification.setLatestEventInfo(context, context.getText(R.string.app_na me), context.getString(R.string.msg_update_fail), emptyIntent); 216 notification.setLatestEventInfo(context, context.getText(R.string.app_na me), context.getString(R.string.msg_update_fail), emptyIntent);
226 notificationManager.notify(NOTIFICATION_ID, notification); 217 notificationManager.notify(NOTIFICATION_ID, notification);
227 } 218 }
228 // Schedule retry in 30 minutes - there is no connection available at this time 219 // Schedule retry in 30 minutes - there is no connection available at this time
229 application.scheduleUpdater(30); 220 application.scheduleUpdater(30);
230 } 221 }
231 } 222 }
232 } 223 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld