check for new version on github
This commit is contained in:
parent
eee187a71f
commit
c0a3eb67ba
5 changed files with 103 additions and 0 deletions
|
@ -160,6 +160,8 @@ android {
|
|||
unitTests.returnDefaultValues = true
|
||||
unitTests.includeAndroidResources = true
|
||||
}
|
||||
|
||||
useLibrary "org.apache.http.legacy"
|
||||
}
|
||||
|
||||
allprojects {
|
||||
|
|
|
@ -63,6 +63,7 @@ import info.nightscout.utils.LogDialog;
|
|||
import info.nightscout.utils.OKDialog;
|
||||
import info.nightscout.utils.PasswordProtection;
|
||||
import info.nightscout.utils.SP;
|
||||
import info.nightscout.utils.VersionChecker;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private static Logger log = LoggerFactory.getLogger(L.CORE);
|
||||
|
@ -118,6 +119,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
public void onPageScrollStateChanged(int state) {
|
||||
}
|
||||
});
|
||||
VersionChecker.check();
|
||||
}
|
||||
|
||||
private void checkPluginPreferences(ViewPager viewPager) {
|
||||
|
|
|
@ -66,6 +66,7 @@ public class Notification {
|
|||
public static final int PERMISSION_SMS = 38;
|
||||
public static final int MAXIMUM_BASAL_VALUE_REPLACED = 39;
|
||||
public static final int NSMALFUNCTION = 40;
|
||||
public static final int NEWVERSIONDETECTED = 41;
|
||||
|
||||
|
||||
public int id;
|
||||
|
|
97
app/src/main/java/info/nightscout/utils/VersionChecker.java
Normal file
97
app/src/main/java/info/nightscout/utils/VersionChecker.java
Normal file
|
@ -0,0 +1,97 @@
|
|||
package info.nightscout.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.Overview.events.EventNewNotification;
|
||||
import info.nightscout.androidaps.plugins.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) {
|
||||
result = result.replace("version", "").replace("\"", "").replace("\\s+", "").trim();
|
||||
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())
|
||||
return 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -1171,6 +1171,7 @@
|
|||
<string name="nav_logsettings">Log settings</string>
|
||||
<string name="resettodefaults">Reset to defaults</string>
|
||||
<string name="nsmalfunction">NSClient malfunction. Consider NS and NSClient restart.</string>
|
||||
<string name="versionavailable">Version %1$s available</string>
|
||||
|
||||
<plurals name="objective_days">
|
||||
<item quantity="one">%1$d day</item>
|
||||
|
|
Loading…
Reference in a new issue