Yuvipanda has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/159506

Change subject: Check for new releases and pop up a notification if there is one
......................................................................

Check for new releases and pop up a notification if there is one

Change-Id: Iac34556b3ef15e9ff2bf1ce626989f156b58ef74
---
M wikipedia/res/values-qq/strings.xml
M wikipedia/res/values/strings.xml
A wikipedia/src/main/java/org/wikipedia/alphaupdater/AlphaUpdateChecker.java
M wikipedia/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java
4 files changed, 85 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/06/159506/1

diff --git a/wikipedia/res/values-qq/strings.xml 
b/wikipedia/res/values-qq/strings.xml
index 5e6c80d..4f028a6 100644
--- a/wikipedia/res/values-qq/strings.xml
+++ b/wikipedia/res/values-qq/strings.xml
@@ -298,4 +298,6 @@
 {{Identical|Retry}}</string>
   <string name="search_redirect_title">Label that tells the user that the 
search result is actually a redirect from another page, specified by the \"%s\" 
symbol.</string>
   <string name="app_store_description">Description of the app as put up on the 
Google Play Store</string>
+  <string name="alpha_update_notification_title">Title for notification when 
new alpha update is available</string>
+  <string name="alpha_update_notification_text">Text for notification when new 
alpha update is available.</string>
 </resources>
diff --git a/wikipedia/res/values/strings.xml b/wikipedia/res/values/strings.xml
index bb5d671..02e60f5 100644
--- a/wikipedia/res/values/strings.xml
+++ b/wikipedia/res/values/strings.xml
@@ -234,4 +234,6 @@
    - Wikipedia Zero: Data charges waived for participating mobile operators.
 
 The code is fully open source. If you have experience with Java and the 
Android SDK, then come fork us! 
https://github.com/wikimedia/apps-android-wikipedia</string>
+    <string name="alpha_update_notification_title">New alpha update 
available</string>
+    <string name="alpha_update_notification_text">Tap to download</string>
 </resources>
diff --git 
a/wikipedia/src/main/java/org/wikipedia/alphaupdater/AlphaUpdateChecker.java 
b/wikipedia/src/main/java/org/wikipedia/alphaupdater/AlphaUpdateChecker.java
new file mode 100644
index 0000000..2ec8955
--- /dev/null
+++ b/wikipedia/src/main/java/org/wikipedia/alphaupdater/AlphaUpdateChecker.java
@@ -0,0 +1,73 @@
+package org.wikipedia.alphaupdater;
+
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.net.Uri;
+import android.preference.PreferenceManager;
+import android.support.v4.app.NotificationCompat;
+import com.github.kevinsawicki.http.HttpRequest;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.wikipedia.R;
+import org.wikipedia.recurring.RecurringTask;
+
+import java.util.Date;
+
+public class AlphaUpdateChecker extends RecurringTask {
+    // The 'l' suffix is needed because stupid Java overflows constants 
otherwise
+    private static final long RUN_INTERVAL_MILLI = 1L;//24L * 60L * 60L * 
1000L; // Once a day!
+
+    private static final String PREFERENCE_KEY_ALPHA_COMMIT = 
"alpha_last_checked_commit";
+    public AlphaUpdateChecker(Context context) {
+        super(context);
+    }
+
+    @Override
+    protected boolean shouldRun(Date lastRun) {
+        return System.currentTimeMillis() - lastRun.getTime() >= 
RUN_INTERVAL_MILLI;
+    }
+
+    @Override
+    protected void run(Date lastRun) {
+        // Check for updates!
+        HttpRequest request = 
HttpRequest.get("https://tools.wmflabs.org/wikipedia-android-builds/runs/latest/meta.json";);
+        JSONObject meta;
+        try {
+            meta = new JSONObject(request.body());
+        } catch (JSONException e) {
+            // This never happens
+            throw new RuntimeException(e);
+        }
+        SharedPreferences prefs = 
PreferenceManager.getDefaultSharedPreferences(getContext());
+        if (prefs.contains(PREFERENCE_KEY_ALPHA_COMMIT)) {
+            if (!prefs.getString(PREFERENCE_KEY_ALPHA_COMMIT, 
"").equals(meta.optString("commit_hash", ""))) {
+                showNotification();
+            }
+        }
+
+        prefs.edit().putString(PREFERENCE_KEY_ALPHA_COMMIT, 
meta.optString("commit_hash")).commit();
+    }
+
+    private void showNotification() {
+        Intent intent = new Intent(Intent.ACTION_VIEW, 
Uri.parse("https://tools.wmflabs.org/wikipedia-android-builds/runs/latest/wikipedia.apk";));
+        PendingIntent pintent = PendingIntent.getActivity(getContext(), 0, 
intent, 0);
+
+        Notification notification = new 
NotificationCompat.Builder(getContext()).setSmallIcon(R.drawable.launcher_alpha)
+                
.setContentTitle(getContext().getString(R.string.alpha_update_notification_title))
+                
.setContentText(getContext().getString(R.string.alpha_update_notification_text))
+                .setContentIntent(pintent)
+                .build();
+
+        NotificationManager manager = (NotificationManager) 
getContext().getSystemService(Context.NOTIFICATION_SERVICE);
+        manager.notify(1, notification);
+    }
+
+    @Override
+    protected String getName() {
+        return "alpha-update-checker";
+    }
+}
diff --git 
a/wikipedia/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java 
b/wikipedia/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java
index a50f65d..30b6c32 100644
--- 
a/wikipedia/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java
+++ 
b/wikipedia/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java
@@ -2,6 +2,8 @@
 
 import android.content.Context;
 import org.wikipedia.RemoteConfigRefreshTask;
+import org.wikipedia.WikipediaApp;
+import org.wikipedia.alphaupdater.AlphaUpdateChecker;
 import org.wikipedia.bridge.StyleFetcherTask;
 import org.wikipedia.concurrency.ExecutorService;
 import org.wikipedia.concurrency.SaneAsyncTask;
@@ -20,14 +22,17 @@
         SaneAsyncTask<Void> task = new SaneAsyncTask<Void>(executor) {
             @Override
             public Void performTask() throws Throwable {
-                RecurringTask[] tasks = new RecurringTask[] {
+                RecurringTask[] allTasks = new RecurringTask[] {
                         // Has list of all rotating tasks that need to be run
                         new RemoteConfigRefreshTask(context),
-                        new StyleFetcherTask(context)
+                        new StyleFetcherTask(context),
                 };
-                for (RecurringTask task: tasks) {
+                for (RecurringTask task: allTasks) {
                     task.runIfNecessary();
                 }
+                if (WikipediaApp.getInstance().getReleaseType() == 
WikipediaApp.RELEASE_ALPHA) {
+                    new AlphaUpdateChecker(context).runIfNecessary();
+                }
                 return null;
             }
         };

-- 
To view, visit https://gerrit.wikimedia.org/r/159506
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iac34556b3ef15e9ff2bf1ce626989f156b58ef74
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to