Back to project page Go2-Rennes.
The source code is released under:
GNU General Public License
If you think the Android project Go2-Rennes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/******************************************************************************* * Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com * //from w w w .ja va 2s. co m * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ package fr.gotorennes.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.os.AsyncTask; import android.util.Log; import fr.gotorennes.R; /** * Utilitaire de contrle de mise jour. * Compare la version disponible sur le market avec celle installe. */ public class UpdateUtils { private static final String MARKET_URL = "https://play.google.com/store/apps/details?id="; private static final String VERSION_PATTERN = ".* itemprop=\"softwareVersion\">([^<]*)<.*"; /** * Contrle la mise jour dans une tche asynchrone * @param context Context */ public static void checkUpdate(final Context context) { AsyncTask<Void, Void, Boolean> checkTask = new AsyncTask<Void, Void, Boolean>() { @Override protected Boolean doInBackground(Void... params) { return needsUpdate(context); } @Override protected void onPostExecute(Boolean result) { if (result) { notifyUpdate(context); } } }; checkTask.execute(); } /** * Affichage d'une notification de mise jour * @param context Context */ public static void notifyUpdate(Context context) { NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, "Mise jour disponible", System.currentTimeMillis()); Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, "Mise jour disponible", "Une nouvelle version de " + getApplicationName(context) + " est disponible sur le market.", contentIntent); mNotificationManager.notify(1, notification); } /** * Contrle de la version installe * @param context Context * @return La version installe doit tre mise jour */ public static boolean needsUpdate(Context context) { String currentVersion = getCurrentVersion(context); if (currentVersion != null) { String marketVersion = getMarketVersion(context); return marketVersion != null && !currentVersion.equals(marketVersion); } return false; } /** * Nom de l'application * @param context Context * @return Nom de l'application */ public static String getApplicationName(Context context) { return context.getString(context.getApplicationInfo().labelRes); } /** * Nom de la version installe * @param context Context * @return Nom de la version installe */ public static String getCurrentVersion(Context context) { try { return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; } catch(NameNotFoundException ex) { Log.e("UpdateUtils", ex.getMessage()); } return null; } /** * Nom de la version disponible sur le market * @param context Context * @return Nom de la version disponible sur le market */ public static String getMarketVersion(Context context) { return getMarketVersion(context.getPackageName()); } /** * Nom de la version disponible sur le market * @param packageName Nom du package de l'application * @return Nom de la version disponible sur le market */ public static String getMarketVersion(String packageName) { String version = null; BufferedReader reader = null; try { URL marketURL = new URL(MARKET_URL + packageName); reader = new BufferedReader(new InputStreamReader(marketURL.openStream())); String line = reader.readLine(); while (line != null) { if (line.matches(VERSION_PATTERN)) { version = line.replaceFirst(VERSION_PATTERN, "$1").trim(); break; } line = reader.readLine(); } } catch (Exception ex) { Log.e("UpdateUtils", ex.getMessage()); } finally { closeReader(reader); } return version; } /** * Ferme la connexion du reader * @param reader Reader */ private static void closeReader(Reader reader) { if (reader != null) { try { reader.close(); } catch(IOException ex) { Log.e("UpdateUtils", ex.getMessage()); } } } }