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

Unified Diff: src/org/adblockplus/android/updater/AlarmReceiver.java

Issue 5153282527854592: Issue 98 - Use the libadblockplus update mechanism (Closed)
Patch Set: Created Sept. 25, 2014, 3:57 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/org/adblockplus/android/updater/AlarmReceiver.java
===================================================================
deleted file mode 100644
--- a/src/org/adblockplus/android/updater/AlarmReceiver.java
+++ /dev/null
@@ -1,223 +0,0 @@
-/*
- * This file is part of Adblock Plus <http://adblockplus.org/>,
- * Copyright (C) 2006-2014 Eyeo GmbH
- *
- * Adblock Plus is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 3 as
- * published by the Free Software Foundation.
- *
- * Adblock Plus is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package org.adblockplus.android.updater;
-
-import java.io.IOException;
-import java.io.StringReader;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.util.Locale;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.adblockplus.android.AdblockPlus;
-import org.adblockplus.android.R;
-import org.adblockplus.android.Utils;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.util.EntityUtils;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-
-import android.app.Notification;
-import android.app.NotificationManager;
-import android.app.PendingIntent;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.pm.PackageManager.NameNotFoundException;
-import android.net.ConnectivityManager;
-import android.net.NetworkInfo;
-import android.os.Build;
-import android.util.Log;
-
-/**
- * Processes Alarm event to check for update availability.
- */
-public class AlarmReceiver extends BroadcastReceiver
-{
- private static final String TAG = Utils.getTag(AlarmReceiver.class);
- private static final int NOTIFICATION_ID = R.string.app_name + 1;
-
- @Override
- public void onReceive(final Context context, final Intent intent)
- {
- Log.i(TAG, "Alarm; requesting updater service");
-
- final AdblockPlus application = AdblockPlus.getApplication();
-
- // Indicates manual (immediate) update check which requires response to user.
- final boolean notify = intent.getBooleanExtra("notifynoupdate", false);
-
- // Check network availability
- boolean connected = false;
- final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = null;
- if (connectivityManager != null)
- {
- networkInfo = connectivityManager.getActiveNetworkInfo();
- connected = networkInfo == null ? false : networkInfo.isConnected();
- }
-
- // Prepare notification
- final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- final Notification notification = new Notification();
- notification.icon = R.drawable.ic_stat_warning;
- notification.when = System.currentTimeMillis();
- notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
- if (notify)
- notification.flags |= Notification.DEFAULT_SOUND;
- final PendingIntent emptyIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
-
- // Get update info
- if (application.checkWriteExternalPermission() && connected)
- {
- final Thread thread = new Thread(new Runnable()
- {
- @Override
- public void run()
- {
- boolean success = false;
- try
- {
- // Read updates manifest
- final DefaultHttpClient httpClient = new DefaultHttpClient();
-
- final String locale = Locale.getDefault().toString().toLowerCase();
- final String device = AdblockPlus.getDeviceName();
- final boolean releaseBuild = context.getResources().getBoolean(R.bool.def_release);
- final String updateUrlTemplate = context.getString(releaseBuild ? R.string.update_url : R.string.devbuild_update_url);
- final URL updateUrl = new URL(String.format(updateUrlTemplate, Build.VERSION.SDK_INT, AdblockPlus.getApplication().getBuildNumber(), locale, device));
- // The following line correctly url-encodes query string parameters
- final URI uri = new URI(updateUrl.getProtocol(), updateUrl.getUserInfo(), updateUrl.getHost(), updateUrl.getPort(), updateUrl.getPath(), updateUrl.getQuery(), updateUrl.getRef());
- final HttpGet httpGet = new HttpGet(uri);
-
- final HttpResponse httpResponse = httpClient.execute(httpGet);
- final HttpEntity httpEntity = httpResponse.getEntity();
- final String xml = EntityUtils.toString(httpEntity);
-
- // Parse XML
- final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- final DocumentBuilder db = dbf.newDocumentBuilder();
-
- final InputSource is = new InputSource();
- is.setCharacterStream(new StringReader(xml));
- final Document doc = db.parse(is);
-
- // Find best match
- final NodeList nl = doc.getElementsByTagName("updatecheck");
- int newBuild = -1;
- int newApi = -1;
- String newUrl = null;
- for (int i = 0; i < nl.getLength(); i++)
- {
- final Element e = (Element) nl.item(i);
- final String url = e.getAttribute("package");
- final int build = Integer.parseInt(e.getAttribute("build"));
- final int api = Integer.parseInt(e.getAttribute("api"));
- if (api > android.os.Build.VERSION.SDK_INT)
- continue;
- if ((build > newBuild) || (build == newBuild && api > newApi))
- {
- newBuild = build;
- newApi = api;
- newUrl = url;
- }
- }
-
- final int thisBuild = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
-
- // Notify user if newer update was found
- if (thisBuild < newBuild)
- {
- notification.icon = R.drawable.ic_stat_download;
- final Intent intent = new Intent(context, UpdaterActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setAction("download");
- intent.putExtra("url", newUrl);
- intent.putExtra("build", newBuild);
- final PendingIntent updateIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- notification.setLatestEventInfo(context, context.getText(R.string.app_name), context.getString(R.string.msg_update_available), updateIntent);
- notificationManager.notify(NOTIFICATION_ID, notification);
- }
- // Notify user that no update was found
- else if (notify)
- {
- notification.setLatestEventInfo(context, context.getText(R.string.app_name), context.getString(R.string.msg_update_missing), emptyIntent);
- notificationManager.notify(NOTIFICATION_ID, notification);
- }
- success = true;
- }
- catch (final IOException e)
- {
- }
- catch (final NumberFormatException e)
- {
- }
- catch (final NameNotFoundException e)
- {
- }
- catch (final ParserConfigurationException e)
- {
- }
- catch (final SAXException e)
- {
- Log.e(TAG, "Error", e);
- }
- catch (final URISyntaxException e)
- {
- Log.e(TAG, "Error", e);
- }
- finally
- {
- if (!success)
- {
- // Notify user about failure
- if (notify)
- {
- notification.setLatestEventInfo(context, context.getText(R.string.app_name), context.getString(R.string.msg_update_fail), emptyIntent);
- notificationManager.notify(NOTIFICATION_ID, notification);
- }
- }
- // Schedule next check
- application.scheduleUpdater(0);
- }
- }
- });
- thread.start();
- }
- else
- {
- // Notify user about failure
- if (notify)
- {
- notification.setLatestEventInfo(context, context.getText(R.string.app_name), context.getString(R.string.msg_update_fail), emptyIntent);
- notificationManager.notify(NOTIFICATION_ID, notification);
- }
- // Schedule retry in 30 minutes - there is no connection available at this time
- application.scheduleUpdater(30);
- }
- }
-}

Powered by Google App Engine
This is Rietveld