/*******************************************************************************
* Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com
*
* 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.support.v4.app.NotificationCompat;
import android.util.Log;
import fr.gotorennes.R;
/**
* Utilitaire de contrôle de mise à jour.
* Compare la version disponible sur le market avec celle installée.
*/
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\">([^<]*)<.*";
/**
* Contrôle la mise à jour dans une tâche 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);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName()));
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Mise à jour disponible")
.setContentText("Une nouvelle version de " + getApplicationName(context) + " est disponible sur le market.")
.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());
}
/**
* Contrôle de la version installée
* @param context Context
* @return La version installée 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 installée
* @param context Context
* @return Nom de la version installée
*/
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());
}
}
}
}