v0.8.0: added an update notification service

This commit is contained in:
Evan Debenham
2019-11-22 15:05:47 -05:00
parent 2e533b74db
commit 2138ad24ec
24 changed files with 606 additions and 13 deletions

4
services/build.gradle Normal file
View File

@@ -0,0 +1,4 @@
apply plugin: 'java-library'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceCompatibility = targetCompatibility = appJavaCompatibility

View File

@@ -0,0 +1,33 @@
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2019 Evan Debenham
*
* 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, or
* (at your option) any later version.
*
* 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 com.shatteredpixel.shatteredpixeldungeon.services.updates;
public class AvailableUpdateData {
public String versionName;
public int versionCode;
public String desc;
public String URL;
}

View File

@@ -0,0 +1,36 @@
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2019 Evan Debenham
*
* 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, or
* (at your option) any later version.
*
* 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 com.shatteredpixel.shatteredpixeldungeon.services.updates;
public abstract class UpdateService {
public static abstract class UpdateResultCallback {
public abstract void onUpdateAvailable( AvailableUpdateData update );
public abstract void onNoUpdateFound();
public abstract void onConnectionFailed();
}
public abstract void checkForUpdate( UpdateResultCallback callback );
public abstract void initializeUpdate( AvailableUpdateData update );
}

View File

@@ -0,0 +1,9 @@
apply plugin: 'java-library'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceCompatibility = targetCompatibility = appJavaCompatibility
dependencies {
implementation project(':SPD-classes')
api project(':services')
}

View File

@@ -0,0 +1,55 @@
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2019 Evan Debenham
*
* 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, or
* (at your option) any later version.
*
* 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 com.shatteredpixel.shatteredpixeldungeon.services.updates;
import com.watabou.noosa.Game;
import com.watabou.utils.DeviceCompat;
public class DebugUpdates extends UpdateService {
private static AvailableUpdateData debugUpdateInfo;
@Override
public void checkForUpdate(UpdateResultCallback callback) {
//turn on to test update UI
if (false){
debugUpdateInfo = new AvailableUpdateData();
debugUpdateInfo.versionCode = Game.versionCode+1;
debugUpdateInfo.URL = "http://www.google.com";
callback.onUpdateAvailable(debugUpdateInfo);
} else {
debugUpdateInfo = null;
callback.onNoUpdateFound();
}
}
@Override
public void initializeUpdate(AvailableUpdateData update) {
DeviceCompat.openURI( update.URL );
}
}

View File

@@ -0,0 +1,36 @@
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2019 Evan Debenham
*
* 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, or
* (at your option) any later version.
*
* 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 com.shatteredpixel.shatteredpixeldungeon.services.updates;
public class UpdateImpl {
private static UpdateService updateChecker = new DebugUpdates();
public static UpdateService getUpdateService(){
return updateChecker;
}
public static boolean supportsUpdates(){
return true;
}
}

View File

@@ -0,0 +1,9 @@
apply plugin: 'java-library'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceCompatibility = targetCompatibility = appJavaCompatibility
dependencies {
implementation project(':SPD-classes')
api project(':services')
}

View File

@@ -0,0 +1,122 @@
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2019 Evan Debenham
*
* 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, or
* (at your option) any later version.
*
* 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 com.shatteredpixel.shatteredpixeldungeon.services.updates;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net;
import com.watabou.noosa.Game;
import com.watabou.utils.Bundle;
import com.watabou.utils.DeviceCompat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.SSLProtocolException;
public class GitHubUpdates extends UpdateService {
private static Pattern descPattern = Pattern.compile("(.*?)(\r\n|\n|\r)(\r\n|\n|\r)---", Pattern.DOTALL + Pattern.MULTILINE);
private static Pattern versionCodePattern = Pattern.compile("internal version number: ([0-9]*)", Pattern.CASE_INSENSITIVE);
@Override
public void checkForUpdate(UpdateResultCallback callback) {
if (!Game.platform.connectedToUnmeteredNetwork()){
callback.onConnectionFailed();
}
Net.HttpRequest httpGet = new Net.HttpRequest(Net.HttpMethods.GET);
httpGet.setUrl("https://api.github.com/repos/00-Evan/shattered-pixel-dungeon/releases");
httpGet.setHeader("Accept", "application/vnd.github.v3+json");
Gdx.net.sendHttpRequest(httpGet, new Net.HttpResponseListener() {
@Override
public void handleHttpResponse(Net.HttpResponse httpResponse) {
try {
Bundle latestRelease = null;
int latestVersionCode = Game.versionCode;
boolean includePrereleases = Game.version.toLowerCase().contains("beta");
for (Bundle b : Bundle.read( httpResponse.getResultAsStream() ).getBundleArray()){
Matcher m = versionCodePattern.matcher(b.getString("body"));
if (m.find()){
int releaseVersion = Integer.parseInt(m.group(1));
if (releaseVersion > latestVersionCode
&& (includePrereleases || !b.getBoolean("prerelease"))){
latestRelease = b;
latestVersionCode = releaseVersion;
}
}
}
if (latestRelease == null){
callback.onNoUpdateFound();
} else {
AvailableUpdateData update = new AvailableUpdateData();
update.versionName = latestRelease.getString("name");
update.versionCode = latestVersionCode;
Matcher m = descPattern.matcher(latestRelease.getString("body"));
m.find();
update.desc = m.group(1);
update.URL = latestRelease.getString("html_url");
callback.onUpdateAvailable(update);
}
} catch (Exception e) {
Game.reportException( e );
callback.onConnectionFailed();
}
}
@Override
public void failed(Throwable t) {
//Failure in SSL handshake, possibly because GitHub requires TLS 1.2+.
// Often happens for old OS versions with outdated security protocols.
// Future update attempts won't work anyway, so just pretend nothing was found.
if (t instanceof SSLProtocolException){
callback.onNoUpdateFound();
} else {
Game.reportException(t);
callback.onConnectionFailed();
}
}
@Override
public void cancelled() {
callback.onConnectionFailed();
}
});
}
@Override
public void initializeUpdate(AvailableUpdateData update) {
DeviceCompat.openURI( update.URL );
}
}

View File

@@ -0,0 +1,36 @@
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2019 Evan Debenham
*
* 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, or
* (at your option) any later version.
*
* 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 com.shatteredpixel.shatteredpixeldungeon.services.updates;
public class UpdateImpl {
private static UpdateService updateChecker = new GitHubUpdates();
public static UpdateService getUpdateService(){
return updateChecker;
}
public static boolean supportsUpdates(){
return true;
}
}