11package org .openimis .imisclaims ;
22
33import android .app .Activity ;
4+ import android .app .AlertDialog ;
5+ import android .app .DownloadManager ;
46import android .app .ProgressDialog ;
57import android .content .Context ;
68import android .content .Intent ;
79import android .net .Uri ;
810import android .os .Bundle ;
911import androidx .annotation .Nullable ;
12+
13+ import android .os .Environment ;
1014import android .view .MenuItem ;
1115import android .view .View ;
1216import android .widget .RelativeLayout ;
1317import android .widget .TextView ;
18+ import android .widget .Toast ;
1419
1520import org .json .JSONArray ;
1621import org .json .JSONException ;
22+ import org .json .JSONObject ;
23+ import org .openimis .imisclaims .network .exception .HttpException ;
1724import org .openimis .imisclaims .tools .Log ;
1825import org .openimis .imisclaims .tools .StorageManager ;
1926import org .openimis .imisclaims .util .StreamUtils ;
2027import org .openimis .imisclaims .util .UriUtils ;
2128
29+ import java .io .BufferedReader ;
2230import java .io .IOException ;
2331import java .io .InputStream ;
32+ import java .io .InputStreamReader ;
2433import java .io .OutputStream ;
34+ import java .net .HttpURLConnection ;
35+ import java .net .URL ;
2536import java .util .ArrayList ;
2637
2738public class SynchronizeActivity extends ImisActivity {
@@ -31,7 +42,7 @@ public class SynchronizeActivity extends ImisActivity {
3142 ArrayList <String > broadcastList ;
3243
3344 TextView tvUploadClaims , tvZipClaims ;
34- RelativeLayout uploadClaims , zipClaims , importMasterData , downloadMasterData ;
45+ RelativeLayout uploadClaims , zipClaims , importMasterData , downloadMasterData , checkUpdate ;
3546
3647 ProgressDialog pd ;
3748 Uri exportUri ;
@@ -64,6 +75,7 @@ protected void onCreate(Bundle savedInstanceState) {
6475 zipClaims = findViewById (R .id .zip_claims );
6576 importMasterData = findViewById (R .id .importMasterData );
6677 downloadMasterData = findViewById (R .id .downloadMasterData );
78+ checkUpdate = findViewById (R .id .checkUpdate );
6779
6880 uploadClaims .setOnClickListener (view -> doLoggedIn (this ::confirmUploadClaims ));
6981 zipClaims .setOnClickListener (view -> confirmXMLCreation ());
@@ -72,6 +84,7 @@ protected void onCreate(Bundle savedInstanceState) {
7284 downloadMasterData .setOnClickListener (view -> {
7385 }); //TODO Not yet implemented
7486 downloadMasterData .setVisibility (View .GONE );
87+ checkUpdate .setOnClickListener (view -> CheckUpdate ());
7588
7689 }
7790
@@ -205,4 +218,107 @@ public void exportClaims() {
205218 pd = ProgressDialog .show (this , "" , getResources ().getString (R .string .Processing ));
206219 SynchronizeService .exportClaims (this );
207220 }
221+
222+ public void CheckUpdate (){
223+ if (global .isNetworkAvailable ()) {
224+ String progress_message = getResources ().getString (R .string .Checking_For_Updates );
225+ pd = ProgressDialog .show (this , getResources ().getString (R .string .initializing ), progress_message );
226+
227+ Thread thread = new Thread (() -> {
228+ try {
229+ //get current release
230+ String currentVersion = BuildConfig .VERSION_NAME ; //
231+ boolean updateAvailable = false ;
232+
233+ //get all github releases
234+ URL url = new URL ("https://api.github.com/repos/openimis/claims_android_app_java/releases" );
235+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
236+ connection .setRequestProperty ("Accept" , "application/vnd.github.v3+json" );
237+ connection .setReadTimeout (60_000 );
238+ BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
239+ StringBuilder response = new StringBuilder ();
240+ String line ;
241+ while ((line = reader .readLine ()) != null ) {
242+ response .append (line );
243+ }
244+ reader .close ();
245+ connection .disconnect ();
246+
247+ //get lastest version
248+ JSONArray jsonarray = new JSONArray (response .toString ());
249+ String lastVersion = "" ;
250+ String tag_name = "" ;
251+ for (int i = 0 ; i < jsonarray .length (); i ++){
252+ JSONObject releaseObj = jsonarray .getJSONObject (i );
253+ if (releaseObj .getString ("tag_name" ).equals (getResources ().getString (R .string .release_tag ))){
254+ tag_name = releaseObj .getString ("tag_name" );
255+ String releaseName = releaseObj .getString ("name" );
256+ if (!releaseName .equals (currentVersion )){
257+ lastVersion = releaseName ;
258+ updateAvailable = true ;
259+ }
260+ }
261+ }
262+
263+ //print result
264+ boolean finalUpdateAvailable = updateAvailable ;
265+ String finalLastVersion = lastVersion ;
266+ String finalTagName = tag_name ;
267+ runOnUiThread (() -> {
268+ pd .dismiss ();
269+ if (finalUpdateAvailable ) {
270+ new AlertDialog .Builder (this )
271+ .setTitle (getResources ().getString (R .string .updateAvailable ))
272+ .setMessage (getResources ().getString (R .string .newVersion ) + " " + finalLastVersion )
273+ .setPositiveButton (getResources ().getString (R .string .download ), (dialog , which ) -> downloadUpdate (finalLastVersion , finalTagName ))
274+ .setNegativeButton (getResources ().getString (R .string .cancel ), null )
275+ .show ();
276+ } else {
277+ Toast .makeText (this ,
278+ getResources ().getString (R .string .haveLastVersion ) + " " + currentVersion ,
279+ Toast .LENGTH_LONG ).show ();
280+ }
281+ });
282+ } catch (HttpException e ){
283+ runOnUiThread (() -> {
284+ pd .dismiss ();
285+ Toast .makeText (this ,
286+ getResources ().getString (R .string .Error ),
287+ Toast .LENGTH_SHORT ).show ();
288+ });
289+ } catch (Exception e ) {
290+ runOnUiThread (() -> {
291+ pd .dismiss ();
292+ Toast .makeText (this ,
293+ e .getMessage (),
294+ Toast .LENGTH_SHORT ).show ();
295+ });
296+ }
297+ });
298+ thread .start ();
299+ } else {
300+ showDialog (getResources ().getString (R .string .CheckInternet ));
301+ }
302+ }
303+
304+ public void downloadUpdate (String lastVersion , String tagName ) {
305+ try {
306+ String apkUrl = "https://github.com/openimis/claims_android_app_java/releases/download/" + tagName + "/claimManagement-" + BuildConfig .FLAVOR + "-debug.apk" ;
307+
308+ DownloadManager .Request request = new DownloadManager .Request (Uri .parse (apkUrl ))
309+ .setTitle (getResources ().getString (R .string .claimUpdate ))
310+ .setDescription (getResources ().getString (R .string .getVersion ) + lastVersion )
311+ .setDestinationInExternalPublicDir (Environment .DIRECTORY_DOWNLOADS , "claimManagement-" + BuildConfig .FLAVOR + "-debug.apk" )
312+ .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE_NOTIFY_COMPLETED );
313+
314+ DownloadManager manager = (DownloadManager ) getSystemService (Context .DOWNLOAD_SERVICE );
315+ manager .enqueue (request );
316+
317+ Toast .makeText (this , getResources ().getString (R .string .downloading ), Toast .LENGTH_SHORT ).show ();
318+
319+ } catch (Exception e ) {
320+ Toast .makeText (this , getResources ().getString (R .string .downloadUpdateFail ), Toast .LENGTH_SHORT ).show ();
321+ Log .e ("DownloadUpdate" , "Erreur: " , e );
322+ }
323+ }
208324}
0 commit comments