| OLD | NEW | 
|---|
| 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  Loading... | 
| 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 { | 
| 61 | 62   private static final String TAG = Utils.getTag(AlarmReceiver.class); | 
| 62   private static final String TAG = "AlarmReceiver"; |  | 
| 63   private static final 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") | 
| 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) contex
     t.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) contex
     t.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) | 
| 92       notification.flags |= Notification.DEFAULT_SOUND; | 93       notification.flags |= Notification.DEFAULT_SOUND; | 
| 93     final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new 
     Intent(), 0); | 94     final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new 
     Intent(), 0); | 
| 94 | 95 | 
| 95     // Get update info | 96     // Get update info | 
| 96     if (application.checkWriteExternalPermission() && connected) | 97     if (application.checkWriteExternalPermission() && connected) | 
| 97     { | 98     { | 
| 98       Thread thread = new Thread(new Runnable() { | 99       final Thread thread = new Thread(new Runnable() | 
|  | 100       { | 
| 99         @Override | 101         @Override | 
| 100         public void run() | 102         public void run() | 
| 101         { | 103         { | 
| 102           boolean success = false; | 104           boolean success = false; | 
| 103           try | 105           try | 
| 104           { | 106           { | 
| 105             // Read updates manifest | 107             // Read updates manifest | 
| 106             DefaultHttpClient httpClient = new DefaultHttpClient(); | 108             final DefaultHttpClient httpClient = new DefaultHttpClient(); | 
| 107 | 109 | 
| 108             String locale = Locale.getDefault().toString().toLowerCase(); | 110             final String locale = Locale.getDefault().toString().toLowerCase(); | 
| 109             String device = AdblockPlus.getDeviceName(); | 111             final String device = AdblockPlus.getDeviceName(); | 
| 110             boolean releaseBuild = context.getResources().getBoolean(R.bool.def_
     release); | 112             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); | 113             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)); | 114             final URL updateUrl = new URL(String.format(updateUrlTemplate, Build
     .VERSION.SDK_INT, AdblockPlus.getApplication().getBuildNumber(), locale, device)
     ); | 
| 113             // The following line correctly url-encodes query string parameters | 115             // 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()); | 116             final URI uri = new URI(updateUrl.getProtocol(), updateUrl.getUserIn
     fo(), updateUrl.getHost(), updateUrl.getPort(), updateUrl.getPath(), updateUrl.g
     etQuery(), updateUrl.getRef()); | 
| 115             HttpGet httpGet = new HttpGet(uri); | 117             final HttpGet httpGet = new HttpGet(uri); | 
| 116 | 118 | 
| 117             HttpResponse httpResponse = httpClient.execute(httpGet); | 119             final HttpResponse httpResponse = httpClient.execute(httpGet); | 
| 118             HttpEntity httpEntity = httpResponse.getEntity(); | 120             final HttpEntity httpEntity = httpResponse.getEntity(); | 
| 119             String xml = EntityUtils.toString(httpEntity); | 121             final String xml = EntityUtils.toString(httpEntity); | 
| 120 | 122 | 
| 121             // Parse XML | 123             // Parse XML | 
| 122             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | 124             final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstanc
     e(); | 
| 123             DocumentBuilder db = dbf.newDocumentBuilder(); | 125             final DocumentBuilder db = dbf.newDocumentBuilder(); | 
| 124 | 126 | 
| 125             InputSource is = new InputSource(); | 127             final InputSource is = new InputSource(); | 
| 126             is.setCharacterStream(new StringReader(xml)); | 128             is.setCharacterStream(new StringReader(xml)); | 
| 127             Document doc = db.parse(is); | 129             final Document doc = db.parse(is); | 
| 128 | 130 | 
| 129             // Find best match | 131             // Find best match | 
| 130             NodeList nl = doc.getElementsByTagName("updatecheck"); | 132             final NodeList nl = doc.getElementsByTagName("updatecheck"); | 
| 131             int newBuild = -1; | 133             int newBuild = -1; | 
| 132             int newApi = -1; | 134             int newApi = -1; | 
| 133             String newUrl = null; | 135             String newUrl = null; | 
| 134             for (int i = 0; i < nl.getLength(); i++) | 136             for (int i = 0; i < nl.getLength(); i++) | 
| 135             { | 137             { | 
| 136               Element e = (Element) nl.item(i); | 138               final Element e = (Element) nl.item(i); | 
| 137               String url = e.getAttribute("package"); | 139               final String url = e.getAttribute("package"); | 
| 138               int build = Integer.parseInt(e.getAttribute("build")); | 140               final int build = Integer.parseInt(e.getAttribute("build")); | 
| 139               int api = Integer.parseInt(e.getAttribute("api")); | 141               final int api = Integer.parseInt(e.getAttribute("api")); | 
| 140               if (api > android.os.Build.VERSION.SDK_INT) | 142               if (api > android.os.Build.VERSION.SDK_INT) | 
| 141                 continue; | 143                 continue; | 
| 142               if ((build > newBuild) || (build == newBuild && api > newApi)) | 144               if ((build > newBuild) || (build == newBuild && api > newApi)) | 
| 143               { | 145               { | 
| 144                 newBuild = build; | 146                 newBuild = build; | 
| 145                 newApi = api; | 147                 newApi = api; | 
| 146                 newUrl = url; | 148                 newUrl = url; | 
| 147               } | 149               } | 
| 148             } | 150             } | 
| 149 | 151 | 
| 150             int thisBuild = context.getPackageManager().getPackageInfo(context.g
     etPackageName(), 0).versionCode; | 152             final int thisBuild = context.getPackageManager().getPackageInfo(con
     text.getPackageName(), 0).versionCode; | 
| 151 | 153 | 
| 152             // Notify user if newer update was found | 154             // Notify user if newer update was found | 
| 153             if (thisBuild < newBuild) | 155             if (thisBuild < newBuild) | 
| 154             { | 156             { | 
| 155               notification.icon = R.drawable.ic_stat_download; | 157               notification.icon = R.drawable.ic_stat_download; | 
| 156               Intent intent = new Intent(context, UpdaterActivity.class).addFlag
     s(Intent.FLAG_ACTIVITY_NEW_TASK); | 158               final Intent intent = new Intent(context, UpdaterActivity.class).a
     ddFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 
| 157               intent.setAction("download"); | 159               intent.setAction("download"); | 
| 158               intent.putExtra("url", newUrl); | 160               intent.putExtra("url", newUrl); | 
| 159               intent.putExtra("build", newBuild); | 161               intent.putExtra("build", newBuild); | 
| 160               PendingIntent updateIntent = PendingIntent.getActivity(context, 0,
      intent, PendingIntent.FLAG_UPDATE_CURRENT); | 162               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); | 163               notification.setLatestEventInfo(context, context.getText(R.string.
     app_name), context.getString(R.string.msg_update_available), updateIntent); | 
| 162               notificationManager.notify(NOTIFICATION_ID, notification); | 164               notificationManager.notify(NOTIFICATION_ID, notification); | 
| 163             } | 165             } | 
| 164             // Notify user that no update was found | 166             // Notify user that no update was found | 
| 165             else if (notify) | 167             else if (notify) | 
| 166             { | 168             { | 
| 167               notification.setLatestEventInfo(context, context.getText(R.string.
     app_name), context.getString(R.string.msg_update_missing), emptyIntent); | 169               notification.setLatestEventInfo(context, context.getText(R.string.
     app_name), context.getString(R.string.msg_update_missing), emptyIntent); | 
| 168               notificationManager.notify(NOTIFICATION_ID, notification); | 170               notificationManager.notify(NOTIFICATION_ID, notification); | 
| 169             } | 171             } | 
| 170             success = true; | 172             success = true; | 
| 171           } | 173           } | 
| 172           catch (IOException e) | 174           catch (final IOException e) | 
| 173           { | 175           { | 
| 174           } | 176           } | 
| 175           catch (NumberFormatException e) | 177           catch (final NumberFormatException e) | 
| 176           { | 178           { | 
| 177           } | 179           } | 
| 178           catch (NameNotFoundException e) | 180           catch (final NameNotFoundException e) | 
| 179           { | 181           { | 
| 180           } | 182           } | 
| 181           catch (ParserConfigurationException e) | 183           catch (final ParserConfigurationException e) | 
| 182           { | 184           { | 
| 183           } | 185           } | 
| 184           catch (SAXException e) | 186           catch (final SAXException e) | 
| 185           { | 187           { | 
| 186             Log.e(TAG, "Error", e); | 188             Log.e(TAG, "Error", e); | 
| 187           } | 189           } | 
| 188           catch (URISyntaxException e) | 190           catch (final URISyntaxException e) | 
| 189           { | 191           { | 
| 190             Log.e(TAG, "Error", e); | 192             Log.e(TAG, "Error", e); | 
| 191           } | 193           } | 
| 192           finally | 194           finally | 
| 193           { | 195           { | 
| 194             if (!success) | 196             if (!success) | 
| 195             { | 197             { | 
| 196               // Notify user about failure | 198               // Notify user about failure | 
| 197               if (notify) | 199               if (notify) | 
| 198               { | 200               { | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
| 213       if (notify) | 215       if (notify) | 
| 214       { | 216       { | 
| 215         notification.setLatestEventInfo(context, context.getText(R.string.app_na
     me), context.getString(R.string.msg_update_fail), emptyIntent); | 217         notification.setLatestEventInfo(context, context.getText(R.string.app_na
     me), context.getString(R.string.msg_update_fail), emptyIntent); | 
| 216         notificationManager.notify(NOTIFICATION_ID, notification); | 218         notificationManager.notify(NOTIFICATION_ID, notification); | 
| 217       } | 219       } | 
| 218       // Schedule retry in 30 minutes - there is no connection available at this
      time | 220       // Schedule retry in 30 minutes - there is no connection available at this
      time | 
| 219       application.scheduleUpdater(30); | 221       application.scheduleUpdater(30); | 
| 220     } | 222     } | 
| 221   } | 223   } | 
| 222 } | 224 } | 
| OLD | NEW | 
|---|