diff options
author | Christian Schneppe <christian@pix-art.de> | 2017-09-25 21:10:49 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2017-10-10 20:34:41 +0200 |
commit | 4076947705508da75a2609a4a7893c4348956adf (patch) | |
tree | 31cf7ab7a3b15397f15f8538424f4c34dc6494b4 /src/main/java/de/pixart/messenger/services | |
parent | c18f61522e0e1afe892daffc3176a9922c115110 (diff) |
reworked updater
Diffstat (limited to 'src/main/java/de/pixart/messenger/services')
4 files changed, 198 insertions, 141 deletions
diff --git a/src/main/java/de/pixart/messenger/services/CheckAppVersionService.java b/src/main/java/de/pixart/messenger/services/CheckAppVersionService.java deleted file mode 100644 index e3c4353ed..000000000 --- a/src/main/java/de/pixart/messenger/services/CheckAppVersionService.java +++ /dev/null @@ -1,42 +0,0 @@ -package de.pixart.messenger.services; - -import com.google.gson.JsonObject; - -import java.io.IOException; -import java.io.PrintWriter; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -public class CheckAppVersionService extends HttpServlet { - private static final long serialVersionUID = 1L; - - public CheckAppVersionService() { - super(); - } - - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - doPost(request, response); - } - - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - PrintWriter out = response.getWriter(); - response.setContentType("text/html"); - - //send a JSON response with the app Version and file URI - JsonObject myObj = new JsonObject(); - myObj.addProperty("success", false); - myObj.addProperty("latestVersionCode", 2); - myObj.addProperty("latestVersion", "1.0.0"); - myObj.addProperty("changelog", ""); - myObj.addProperty("filesize", ""); - myObj.addProperty("appURI", ""); - out.println(myObj.toString()); - out.close(); - } -} diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index daeba94e3..c1ec963f1 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -62,6 +62,7 @@ public class NotificationService { public static final int NOTIFICATION_ID = 2 * NOTIFICATION_ID_MULTIPLIER; public static final int FOREGROUND_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 4; public static final int ERROR_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 6; + public static final int UPDATE_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 8; private Conversation mOpenConversation; private boolean mIsInForeground; private long mLastNotification; @@ -779,4 +780,5 @@ public class NotificationService { notificationManager.notify(FOREGROUND_NOTIFICATION_ID, notification); return notification; } + } diff --git a/src/main/java/de/pixart/messenger/services/UpdateService.java b/src/main/java/de/pixart/messenger/services/UpdateService.java new file mode 100644 index 000000000..bb186063b --- /dev/null +++ b/src/main/java/de/pixart/messenger/services/UpdateService.java @@ -0,0 +1,196 @@ +package de.pixart.messenger.services; + +import android.app.Notification; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; +import android.os.Handler; +import android.os.Looper; +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationManagerCompat; +import android.util.Log; +import android.widget.Toast; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; + +import javax.net.ssl.HttpsURLConnection; + +import de.pixart.messenger.BuildConfig; +import de.pixart.messenger.Config; +import de.pixart.messenger.R; +import de.pixart.messenger.ui.UpdaterActivity; + +import static de.pixart.messenger.services.NotificationService.UPDATE_NOTIFICATION_ID; + +public class UpdateService extends AsyncTask<String, Object, UpdateService.Wrapper> { + + private Context context; + + public UpdateService(Context context) { + this.context = context; + } + + public class Wrapper + { + public boolean UpdateAvailable; + public boolean showNoUpdateToast; + } + + @Override + protected Wrapper doInBackground(String... params) { + + boolean showNoUpdateToast = false; + if (params[0].equals("true")) { + showNoUpdateToast = true; + } + + + HttpsURLConnection connection = null; + String jsonString = ""; + boolean UpdateAvailable = false; + + try { + URL url = new URL(Config.UPDATE_URL); + connection = (HttpsURLConnection)url.openConnection(); + connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000); + connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + jsonString += line; + } + + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + if (connection != null) { + connection.disconnect(); + } + } + + try { + JSONObject json = new JSONObject(jsonString); + if (json.getBoolean("success") && json.has("latestVersion") && json.has("appURI") && json.has("filesize")) { + String version = json.getString("latestVersion"); + String ownVersion = BuildConfig.VERSION_NAME; + String url = json.getString("appURI"); + String filesize = json.getString("filesize"); + String changelog = ""; + if (json.has("changelog")) { + changelog = json.getString("changelog"); + } + if (checkVersion(version, ownVersion) >= 1) { + Log.d(Config.LOGTAG, "AppUpdater: Version " + ownVersion + " should be updated to " + version); + UpdateAvailable = true; + showNotification(url, changelog, version, filesize); + } else { + Log.d(Config.LOGTAG, "AppUpdater: Version " + ownVersion + " is up to date"); + UpdateAvailable = false; + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + Wrapper w = new Wrapper(); + w.UpdateAvailable = UpdateAvailable; + w.showNoUpdateToast = showNoUpdateToast; + return w; + } + + @Override + protected void onPostExecute(Wrapper w) { + super.onPostExecute(w); + if (!w.UpdateAvailable) { + noUpdateMessage(w.showNoUpdateToast); + } + } + + private void noUpdateMessage(boolean show) { + if (!show) { + return; + } + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(new Runnable() { + + @Override + public void run() { + Toast.makeText(context, context.getString(R.string.no_update_available), Toast.LENGTH_LONG).show(); + } + }); + } + + private void showNotification(String url, String changelog, String version, String filesize) { + Intent intent = new Intent(context, UpdaterActivity.class); + intent.putExtra("update", "PixArtMessenger_UpdateService"); + intent.putExtra("url", url); + intent.putExtra("changelog", changelog); + PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); + mBuilder.setContentTitle(context.getString(R.string.update_service)); + mBuilder.setContentText(String.format(context.getString(R.string.update_available), version, filesize)); + mBuilder.setSmallIcon(R.drawable.ic_update_notification); + mBuilder.setContentIntent(pi); + Notification notification = mBuilder.build(); + notificationManager.notify(UPDATE_NOTIFICATION_ID, notification); + } + + private int checkVersion(String remoteVersion, String installedVersion) { + // Use this instead of String.compareTo() for a non-lexicographical + // comparison that works for version strings. e.g. "1.10".compareTo("1.6"). + // + // @param str1 a string of ordinal numbers separated by decimal points. + // @param str2 a string of ordinal numbers separated by decimal points. + // @return The result is a negative integer if str1 is _numerically_ less than str2. + // The result is a positive integer if str1 is _numerically_ greater than str2. + // The result is zero if the strings are _numerically_ equal. + // @note It does not work if "1.10" is supposed to be equal to "1.10.0". + + String[] remote = null; + String[] installed = null; + String[] remoteV = null; + String[] installedV = null; + try { + installedV = installedVersion.split(" "); + Log.d(Config.LOGTAG, "AppUpdater: Version installed: " + installedV[0]); + installed = installedV[0].split("\\."); + } catch (Exception e) { + e.printStackTrace(); + } + try { + remoteV = remoteVersion.split(" "); + if (installedV != null && installedV.length > 1) { + if (installedV[1] != null) { + remoteV[0] = remoteV[0] + ".1"; + } + } + Log.d(Config.LOGTAG, "AppUpdater: Version on server: " + remoteV[0]); + remote = remoteV[0].split("\\."); + } catch (Exception e) { + e.printStackTrace(); + } + int i = 0; + // set index to first non-equal ordinal or length of shortest localVersion string + if (remote != null && installed != null) { + while (i < remote.length && i < installed.length && remote[i].equals(installed[i])) { + i++; + } + // compare first non-equal ordinal number + if (i < remote.length && i < installed.length) { + int diff = Integer.valueOf(remote[i]).compareTo(Integer.valueOf(installed[i])); + return Integer.signum(diff); + } + // the strings are equal or one string is a substring of the other + // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4" + return Integer.signum(remote.length - installed.length); + } + return 0; + } +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/services/UpdaterWebService.java b/src/main/java/de/pixart/messenger/services/UpdaterWebService.java deleted file mode 100644 index 4e4a599ad..000000000 --- a/src/main/java/de/pixart/messenger/services/UpdaterWebService.java +++ /dev/null @@ -1,99 +0,0 @@ -package de.pixart.messenger.services; - -import android.app.IntentService; -import android.content.Intent; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.util.Log; - -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.StatusLine; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.conn.params.ConnManagerParams; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import de.pixart.messenger.Config; -import de.pixart.messenger.R; -import de.pixart.messenger.ui.UpdaterActivity.UpdateReceiver; - -public class UpdaterWebService extends IntentService { - public static final String REQUEST_STRING = ""; - public static final String RESPONSE_MESSAGE = ""; - - private String URL = null; - public static final int REGISTRATION_TIMEOUT = Config.SOCKET_TIMEOUT * 1000; - public static final int WAIT_TIMEOUT = Config.CONNECT_TIMEOUT * 1000; - - public UpdaterWebService() { - super("UpdaterWebService"); - } - - @Override - protected void onHandleIntent(Intent intent) { - - String requestString = intent.getStringExtra(REQUEST_STRING); - Log.d(Config.LOGTAG, "AppUpdater: " + requestString); - String responseMessage; - PackageInfo pInfo = null; - try { - pInfo = getPackageManager().getPackageInfo(getPackageName(), 0); - } catch (PackageManager.NameNotFoundException e) { - e.printStackTrace(); - } - //get the app version Name for display - final String versionName = pInfo.versionName; - - try { - - URL = requestString; - HttpClient httpclient = new DefaultHttpClient(); - HttpParams params = httpclient.getParams(); - - HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT); - HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT); - ConnManagerParams.setTimeout(params, WAIT_TIMEOUT); - - HttpGet httpGet = new HttpGet(URL); - httpGet.setHeader("User-Agent", getString(R.string.app_name) + " " + versionName); - HttpResponse response = httpclient.execute(httpGet); - - StatusLine statusLine = response.getStatusLine(); - Log.d(Config.LOGTAG, "AppUpdater: HTTP Status Code: " + statusLine.getStatusCode()); - if (statusLine.getStatusCode() == HttpStatus.SC_OK) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - response.getEntity().writeTo(out); - out.close(); - responseMessage = out.toString(); - } else { - Log.e(Config.LOGTAG, "AppUpdater: HTTP1:" + statusLine.getReasonPhrase()); - response.getEntity().getContent().close(); - throw new IOException(statusLine.getReasonPhrase()); - } - - } catch (ClientProtocolException e) { - Log.e(Config.LOGTAG, "AppUpdater: HTTP2:" + e); - responseMessage = ""; - } catch (IOException e) { - Log.e(Config.LOGTAG, "AppUpdater: HTTP3:" + e); - responseMessage = ""; - } catch (Exception e) { - Log.e(Config.LOGTAG, "AppUpdater: HTTP4:" + e); - responseMessage = ""; - } - - Intent broadcastIntent = new Intent(); - broadcastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - broadcastIntent.setAction(UpdateReceiver.PROCESS_RESPONSE); - broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); - broadcastIntent.putExtra(RESPONSE_MESSAGE, responseMessage); - sendBroadcast(broadcastIntent); - } -}
\ No newline at end of file |