Commit de236bd2 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Handle updates

Can check for updates (called in creation)
Launcher bug fixed
parent 5c8657b3
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:launchMode="singleTask" android:launchMode="singleInstance"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name="fr.utc.simde.jessy.MainActivity" <activity android:name="fr.utc.simde.jessy.MainActivity"
android:configChanges="orientation" android:configChanges="orientation"
......
package fr.utc.simde.jessy; package fr.utc.simde.jessy;
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log; import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.TextView; import android.widget.TextView;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.utc.simde.jessy.tools.CASConnexion; import fr.utc.simde.jessy.tools.CASConnexion;
import fr.utc.simde.jessy.tools.Config; import fr.utc.simde.jessy.tools.Config;
...@@ -28,6 +43,10 @@ import fr.utc.simde.jessy.tools.NemopaySession; ...@@ -28,6 +43,10 @@ import fr.utc.simde.jessy.tools.NemopaySession;
public abstract class BaseActivity extends NFCActivity { public abstract class BaseActivity extends NFCActivity {
private static final String LOG_TAG = "_BaseActivity"; private static final String LOG_TAG = "_BaseActivity";
private static final String gitUrl = "https://raw.githubusercontent.com/simde-utc/jessy/master/";
private static final String manifestUrl = "app/src/main/AndroidManifest.xml";
private static final String downloadLocation = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
protected static NemopaySession nemopaySession; protected static NemopaySession nemopaySession;
protected static Ginger ginger; protected static Ginger ginger;
protected static CASConnexion casConnexion; protected static CASConnexion casConnexion;
...@@ -539,4 +558,110 @@ public abstract class BaseActivity extends NFCActivity { ...@@ -539,4 +558,110 @@ public abstract class BaseActivity extends NFCActivity {
ginger.setKey(key); ginger.setKey(key);
} }
protected boolean haveStoragePermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else
return true;
}
protected void checkUpdate() { checkUpdate(true); }
protected void checkUpdate(final boolean popupIfNot) {
dialog.startLoading(BaseActivity.this, getString(R.string.information_collection), getString(R.string.check_update));
new Thread() {
@Override
public void run() {
HTTPRequest httpRequest = new HTTPRequest(gitUrl + manifestUrl);
httpRequest.get();
try {
final Matcher matcher = Pattern.compile("android:versionCode=\"([0-9]*)\".*android:versionName=\"(\\S*)\"").matcher(httpRequest.getResponse());
if (matcher.find()) {
if (BuildConfig.VERSION_CODE > Integer.parseInt(matcher.group(1))) {
runOnUiThread(new Runnable() {
@Override
public void run() {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(BaseActivity.this);
alertDialogBuilder
.setTitle(R.string.update)
.setMessage(getString(R.string.available_update) + "\n" + getString(R.string.actual_version) + ": " + BuildConfig.VERSION_NAME + "\n" + getString(R.string.available_version) + ": " + matcher.group(2))
.setCancelable(false)
.setPositiveButton(R.string.set_update, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
if (!update(matcher.group(2))) {
dialog.stopLoading();
dialog.errorDialog(BaseActivity.this, getString(R.string.update), getString(R.string.can_not_update));
}
}
})
.setNegativeButton(R.string.cancel, null);
dialog.createDialog(alertDialogBuilder);
}
});
}
else if (popupIfNot)
throw new Exception(getString(R.string.no_update));
}
else
throw new Exception(getString(R.string.can_not_detect_update));
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(BaseActivity.this, getString(R.string.update), e.getMessage() + "\n" + getString(R.string.actual_version) + ": " + BuildConfig.VERSION_NAME);
}
});
}
}
}.start();
}
protected boolean update(final String version) {
final String destination = this.downloadLocation + getString(R.string.app_name) + " " + version + ".apk";
final String url = this.gitUrl + getString(R.string.app_name) + " " + version + ".apk";
final Uri uri = Uri.parse("file://" + destination);
if (!haveStoragePermission())
return false;
File file = new File(destination);
if (file.exists())
file.delete();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(getString(R.string.update));
request.setTitle(getString(R.string.app_name));
request.setDestinationUri(uri);
final DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctx, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(install);
unregisterReceiver(this);
finish();
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
return true;
}
} }
...@@ -35,10 +35,13 @@ import java.io.File; ...@@ -35,10 +35,13 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.utc.simde.jessy.adapters.FoundationsAdapter; import fr.utc.simde.jessy.adapters.FoundationsAdapter;
import fr.utc.simde.jessy.adapters.OptionChoicesAdapter; import fr.utc.simde.jessy.adapters.OptionChoicesAdapter;
import fr.utc.simde.jessy.adapters.OptionsAdapter; import fr.utc.simde.jessy.adapters.OptionsAdapter;
import fr.utc.simde.jessy.tools.HTTPRequest;
/** /**
* Created by Samy on 26/10/2017. * Created by Samy on 26/10/2017.
...@@ -47,10 +50,6 @@ import fr.utc.simde.jessy.adapters.OptionsAdapter; ...@@ -47,10 +50,6 @@ import fr.utc.simde.jessy.adapters.OptionsAdapter;
public class FoundationsOptionsActivity extends BaseActivity { public class FoundationsOptionsActivity extends BaseActivity {
private static final String LOG_TAG = "_FoundationsOptionsActi"; private static final String LOG_TAG = "_FoundationsOptionsActi";
private static final String gitUrl = "https://raw.githubusercontent.com/simde-utc/jessy/master/";
private static final String manifestUrl = "app/src/main/AndroidManifest.xml";
private static final String downloadLocation = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
TabHost tabHost; TabHost tabHost;
ListView foundationList; ListView foundationList;
ListView optionList; ListView optionList;
...@@ -148,7 +147,7 @@ public class FoundationsOptionsActivity extends BaseActivity { ...@@ -148,7 +147,7 @@ public class FoundationsOptionsActivity extends BaseActivity {
else if (isOption(position,7)) else if (isOption(position,7))
keyGingerDialog(); keyGingerDialog();
else if (isOption(position,8)) else if (isOption(position,8))
update("0.8.4"); checkUpdate();
else else
configDialog(); configDialog();
} }
...@@ -157,55 +156,6 @@ public class FoundationsOptionsActivity extends BaseActivity { ...@@ -157,55 +156,6 @@ public class FoundationsOptionsActivity extends BaseActivity {
this.optionList.setAdapter(this.optionsAdapter); this.optionList.setAdapter(this.optionsAdapter);
} }
public boolean haveStoragePermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else
return true;
}
protected boolean update(final String version) {
final String destination = this.downloadLocation + getString(R.string.app_name) + " " + version + ".apk";
final String url = this.gitUrl + getString(R.string.app_name) + " " + version + ".apk";
final Uri uri = Uri.parse("file://" + destination);
if (!haveStoragePermission())
return false;
File file = new File(destination);
if (file.exists())
file.delete();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(getString(R.string.update));
request.setTitle(getString(R.string.app_name));
request.setDestinationUri(uri);
final DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctx, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(install);
unregisterReceiver(this);
finish();
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
return true;
}
protected void keyNemopayDialog() { protected void keyNemopayDialog() {
hasRights(getString(R.string.nemopay), new String[]{}, new Runnable(){ hasRights(getString(R.string.nemopay), new String[]{}, new Runnable(){
@Override @Override
......
...@@ -92,6 +92,7 @@ public class MainActivity extends BaseActivity { ...@@ -92,6 +92,7 @@ public class MainActivity extends BaseActivity {
}); });
setConfig(); setConfig();
checkUpdate(false);
} }
@Override @Override
......
...@@ -9,6 +9,6 @@ ...@@ -9,6 +9,6 @@
<item>Gestion des cartes/cotisations</item> <item>Gestion des cartes/cotisations</item>
<item>Modifier la clé Nemopay</item> <item>Modifier la clé Nemopay</item>
<item>Modifier la clé Ginger</item> <item>Modifier la clé Ginger</item>
<item>Vérifier les mises à jour</item> <item>Rechercher une mise à jour</item>
</string-array> </string-array>
</resources> </resources>
\ No newline at end of file
...@@ -57,6 +57,7 @@ ...@@ -57,6 +57,7 @@
<string name="short_tag">Numéro raccourci</string> <string name="short_tag">Numéro raccourci</string>
<string name="type">Type</string> <string name="type">Type</string>
<string name="day_s_">jour(s)</string> <string name="day_s_">jour(s)</string>
<string name="update">Mise à jour</string>
<string name="author">&lt;/&gt; avec ♥ par Samy NASTUZZI</string> <string name="author">&lt;/&gt; avec ♥ par Samy NASTUZZI</string>
<string name="error_unexpected">Une erreur improbable a eu lieu</string> <string name="error_unexpected">Une erreur improbable a eu lieu</string>
...@@ -149,6 +150,14 @@ ...@@ -149,6 +150,14 @@
<string name="contribute_ext">Extérieur (20€)</string> <string name="contribute_ext">Extérieur (20€)</string>
<string name="contribute_admin">Membre d\'honneur (0€)</string> <string name="contribute_admin">Membre d\'honneur (0€)</string>
<string name="contribute_temp">Temporaire</string> <string name="contribute_temp">Temporaire</string>
<string name="set_update">Mettre à jour</string>
<string name="check_update">Recherche d\'une mise à jour</string>
<string name="available_update">Mise à jour disponible</string>
<string name="can_not_detect_update">Impossible de détecter si une mise à jour disponible</string>
<string name="can_not_update">Impossible de réaliser la mise à jour</string>
<string name="no_update">Aucune mise à jour disponible</string>
<string name="actual_version">Version actuelle</string>
<string name="available_version">Version disponible</string>
<string name="print_mode">Mode d\'affichage</string> <string name="print_mode">Mode d\'affichage</string>
<string name="filters">Filtres d\'affichage</string> <string name="filters">Filtres d\'affichage</string>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment