check version Java -> Kotlin

This commit is contained in:
AdrianLxM 2019-04-10 00:36:02 +02:00
parent 6c0853d49a
commit bd5d31a4a3
3 changed files with 65 additions and 102 deletions

View file

@ -51,6 +51,7 @@ import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus;
import info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerUtilsKt;
import info.nightscout.androidaps.setupwizard.SetupWizardActivity;
import info.nightscout.androidaps.tabs.TabPageAdapter;
import info.nightscout.androidaps.utils.AndroidPermission;
@ -59,7 +60,6 @@ import info.nightscout.androidaps.utils.LocaleHelper;
import info.nightscout.androidaps.utils.OKDialog;
import info.nightscout.androidaps.utils.PasswordProtection;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.VersionChecker;
public class MainActivity extends AppCompatActivity {
private static Logger log = LoggerFactory.getLogger(L.CORE);
@ -115,7 +115,7 @@ public class MainActivity extends AppCompatActivity {
public void onPageScrollStateChanged(int state) {
}
});
VersionChecker.check();
VersionCheckerUtilsKt.checkVersion();
FabricPrivacy.setUserStats();
}

View file

@ -0,0 +1,63 @@
package info.nightscout.androidaps.plugins.general.versionChecker
import android.content.Context
import android.net.ConnectivityManager
import info.nightscout.androidaps.BuildConfig
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification
import org.apache.http.HttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
import org.slf4j.LoggerFactory
import java.io.IOException
import java.io.InputStream
// check network connection
fun isConnected(): Boolean {
val connMgr = MainApp.instance().applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return connMgr.activeNetworkInfo?.isConnected ?: false
}
// convert inputstream to String
@Throws(IOException::class)
fun InputStream.findVersion(): String? {
var version: String? = null
val regex = "(.*)version(.*)\"(((\\d+)\\.)+(\\d+))\"(.*)"
this.bufferedReader().forEachLine {
if (regex.toRegex().matches(it)) {
version = regex.toRegex().matchEntire(it)?.groupValues?.getOrNull(3)
return@forEachLine
}
}
return version
}
private val log = LoggerFactory.getLogger(L.CORE)
@Suppress("DEPRECATION")
fun checkVersion() = if (isConnected()) {
Thread {
try {
val request = HttpGet("https://raw.githubusercontent.com/MilosKozak/AndroidAPS/master/app/build.gradle")
val response: HttpResponse = DefaultHttpClient().execute(request)
val version: String? = response.entity.content?.findVersion()
val comparison = version?.compareTo(BuildConfig.VERSION_NAME.replace("\"", "")) ?: 0
if (comparison == 0) {
log.debug("Version equal to master of fetch failed")
} else if (comparison > 0) {
log.debug("Version outdated. Found $version")
val notification = Notification(Notification.NEWVERSIONDETECTED, String.format(MainApp.gs(R.string.versionavailable), version.toString()), Notification.LOW)
MainApp.bus().post(EventNewNotification(notification))
} else {
log.debug("Version newer than master. Are you developer?")
}
} catch (e: IOException) {
log.debug("Github master version check error: $e")
}
}.start()
} else
log.debug("Github master version no checked. No connectivity")

View file

@ -1,100 +0,0 @@
package info.nightscout.androidaps.utils;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
import static android.content.Context.CONNECTIVITY_SERVICE;
public class VersionChecker {
private static Logger log = LoggerFactory.getLogger(L.CORE);
public static void check() {
if (isConnected())
new Thread(() -> {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("https://raw.githubusercontent.com/MilosKozak/AndroidAPS/master/app/build.gradle");
HttpResponse response;
try {
response = client.execute(request);
InputStream inputStream = response.getEntity().getContent();
if (inputStream != null) {
String result = findLine(inputStream);
if (result != null) {
int compare = result.compareTo(BuildConfig.VERSION_NAME.replace("\"", ""));
if (compare == 0) {
log.debug("Version equal to master");
return;
} else if (compare > 0) {
log.debug("Version outdated. Found " + result);
Notification notification = new Notification(Notification.NEWVERSIONDETECTED, String.format(MainApp.gs(R.string.versionavailable), result), Notification.LOW);
MainApp.bus().post(new EventNewNotification(notification));
return;
} else {
log.debug("Version newer than master. Are you developer?");
return;
}
}
}
log.debug("Github master version not found");
} catch (IOException e) {
e.printStackTrace();
log.debug("Github master version check error");
}
}).start();
else
log.debug("Github master version no checked. No connectivity");
}
// convert inputstream to String
private static String findLine(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
String regex = "(.*)version(.*)\"(((\\d+)\\.)+(\\d+))\"(.*)";
Pattern p = Pattern.compile(regex);
while ((line = bufferedReader.readLine()) != null) {
Matcher m = p.matcher(line);
if (m.matches()) {
log.debug("+++ " + line);
return m.group(3);
} else {
log.debug("--- " + line);
}
}
inputStream.close();
return null;
}
// check network connection
public static boolean isConnected() {
ConnectivityManager connMgr = (ConnectivityManager) MainApp.instance().getApplicationContext().getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}