From 7e3ecf479d3a8b1cd0202584b8917a606152d9eb Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 16 Aug 2014 09:51:37 +0200 Subject: [PATCH] Move update task to activity share Refs #1867 --- src/biz/bokhorst/xprivacy/ActivityMain.java | 130 +----------------- src/biz/bokhorst/xprivacy/ActivityShare.java | 135 +++++++++++++++++++ 2 files changed, 137 insertions(+), 128 deletions(-) diff --git a/src/biz/bokhorst/xprivacy/ActivityMain.java b/src/biz/bokhorst/xprivacy/ActivityMain.java index 5b9a02d94..7ab07f7da 100644 --- a/src/biz/bokhorst/xprivacy/ActivityMain.java +++ b/src/biz/bokhorst/xprivacy/ActivityMain.java @@ -822,134 +822,8 @@ private void optionPro() { private void optionUpdate() { if (Util.hasProLicense(this) == null) Util.viewUri(this, ActivityMain.cProUri); - else { - new AsyncTask() { - NotificationCompat.Builder builder = new NotificationCompat.Builder(ActivityMain.this); - Notification notification; - NotificationManager notificationManager = (NotificationManager) ActivityMain.this - .getSystemService(Context.NOTIFICATION_SERVICE); - - @Override - protected void onPreExecute() { - // Build notification - builder.setSmallIcon(R.drawable.ic_launcher); - builder.setContentTitle(ActivityMain.this.getString(R.string.app_name)); - builder.setAutoCancel(false); - builder.setOngoing(true); - } - - @Override - protected Object doInBackground(Object... args) { - try { - // Notify - builder.setContentText(ActivityMain.this.getString(R.string.title_update_checking)); - builder.setWhen(System.currentTimeMillis()); - notification = builder.build(); - notificationManager.notify(Util.NOTIFY_UPDATE, notification); - - // Encode package - String[] license = Util.getProLicenseUnchecked(); - int userId = Util.getUserId(Process.myUid()); - boolean test = PrivacyManager - .getSettingBool(userId, PrivacyManager.cSettingTestVersions, false); - JSONObject jRoot = new JSONObject(); - jRoot.put("test_versions", test); - jRoot.put("xprivacy_version", Util.getSelfVersionCode(ActivityMain.this)); - jRoot.put("xprivacy_version_name", Util.getSelfVersionName(ActivityMain.this)); - jRoot.put("email", license[1]); - jRoot.put("signature", license[2]); - - // Update - HttpParams httpParams = new BasicHttpParams(); - HttpConnectionParams.setConnectionTimeout(httpParams, ActivityShare.TIMEOUT_MILLISEC); - HttpConnectionParams.setSoTimeout(httpParams, ActivityShare.TIMEOUT_MILLISEC); - HttpClient httpclient = new DefaultHttpClient(httpParams); - - HttpPost httpost = new HttpPost(ActivityShare.getBaseURL() + "?format=json&action=update"); - httpost.setEntity(new ByteArrayEntity(jRoot.toString().getBytes("UTF-8"))); - httpost.setHeader("Accept", "application/json"); - httpost.setHeader("Content-type", "application/json"); - HttpResponse response = httpclient.execute(httpost); - StatusLine statusLine = response.getStatusLine(); - if (statusLine.getStatusCode() == HttpStatus.SC_OK) { - String contentType = response.getFirstHeader("Content-Type").getValue(); - if ("application/octet-stream".equals(contentType)) { - // Update notification - builder.setContentText(ActivityMain.this.getString(R.string.title_update_downloading)); - builder.setWhen(System.currentTimeMillis()); - notification = builder.build(); - notificationManager.notify(Util.NOTIFY_UPDATE, notification); - - // Download APK - File folder = Environment - .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); - folder.mkdirs(); - String fileName = response.getFirstHeader("Content-Disposition").getElements()[0] - .getParameterByName("filename").getValue(); - File download = new File(folder, fileName); - FileOutputStream fos = null; - try { - fos = new FileOutputStream(download); - response.getEntity().writeTo(fos); - } finally { - if (fos != null) - fos.close(); - } - - return download; - } else if ("application/json".equals(contentType)) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - response.getEntity().writeTo(out); - out.close(); - throw new IOException(out.toString("UTF-8")); - } else - throw new IOException(contentType); - } else - return statusLine; - } catch (Throwable ex) { - Util.bug(null, ex); - return ex; - } - } - - @Override - protected void onPostExecute(Object result) { - if (result instanceof StatusLine) { - notificationManager.cancel(Util.NOTIFY_UPDATE); - StatusLine status = (StatusLine) result; - if (status.getStatusCode() == 204) { // No Content - String none = ActivityMain.this.getString(R.string.title_update_none); - Toast.makeText(ActivityMain.this, none, Toast.LENGTH_LONG).show(); - } else - Toast.makeText(ActivityMain.this, status.getStatusCode() + " " + status.getReasonPhrase(), - Toast.LENGTH_LONG).show(); - - } else if (result instanceof Throwable) { - notificationManager.cancel(Util.NOTIFY_UPDATE); - Throwable ex = (Throwable) result; - Toast.makeText(ActivityMain.this, ex.toString(), Toast.LENGTH_LONG).show(); - - } else { - File download = (File) result; - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(Uri.fromFile(download), "application/vnd.android.package-archive"); - - PendingIntent pi = PendingIntent.getActivity(ActivityMain.this, 0, intent, - PendingIntent.FLAG_UPDATE_CURRENT); - - // Update notification - builder.setContentText(ActivityMain.this.getString(R.string.title_update_install)); - builder.setWhen(System.currentTimeMillis()); - builder.setAutoCancel(true); - builder.setOngoing(false); - builder.setContentIntent(pi); - notification = builder.build(); - notificationManager.notify(Util.NOTIFY_UPDATE, notification); - } - - } - }.executeOnExecutor(mExecutor); - } + else + new ActivityShare.UpdateTask(this).executeOnExecutor(mExecutor); } @SuppressLint("DefaultLocale") diff --git a/src/biz/bokhorst/xprivacy/ActivityShare.java b/src/biz/bokhorst/xprivacy/ActivityShare.java index 022b7d07a..c5ae7c17e 100644 --- a/src/biz/bokhorst/xprivacy/ActivityShare.java +++ b/src/biz/bokhorst/xprivacy/ActivityShare.java @@ -46,6 +46,9 @@ import android.accounts.Account; import android.accounts.AccountManager; import android.annotation.SuppressLint; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.app.ProgressDialog; import android.app.AlertDialog; import android.content.Context; @@ -64,6 +67,7 @@ import android.os.Process; import android.provider.ContactsContract; import android.provider.Settings.Secure; +import android.support.v4.app.NotificationCompat; import android.text.TextUtils; import android.util.Log; import android.util.Patterns; @@ -1821,6 +1825,137 @@ public void onClick(DialogInterface dialog, int which) { } } + public static class UpdateTask extends AsyncTask { + private ActivityBase mContext; + private NotificationCompat.Builder builder; + private Notification notification; + private NotificationManager notificationManager; + + public UpdateTask(ActivityBase context) { + mContext = context; + builder = new NotificationCompat.Builder(context); + notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + } + + @Override + protected void onPreExecute() { + // Build notification + builder.setSmallIcon(R.drawable.ic_launcher); + builder.setContentTitle(mContext.getString(R.string.app_name)); + builder.setAutoCancel(false); + builder.setOngoing(true); + } + + @Override + protected Object doInBackground(Object... args) { + try { + // Notify + builder.setContentText(mContext.getString(R.string.title_update_checking)); + builder.setWhen(System.currentTimeMillis()); + notification = builder.build(); + notificationManager.notify(Util.NOTIFY_UPDATE, notification); + + // Encode package + String[] license = Util.getProLicenseUnchecked(); + int userId = Util.getUserId(Process.myUid()); + boolean test = PrivacyManager.getSettingBool(userId, PrivacyManager.cSettingTestVersions, false); + JSONObject jRoot = new JSONObject(); + jRoot.put("test_versions", test); + jRoot.put("xprivacy_version", Util.getSelfVersionCode(mContext)); + jRoot.put("xprivacy_version_name", Util.getSelfVersionName(mContext)); + jRoot.put("email", license[1]); + jRoot.put("signature", license[2]); + + // Update + HttpParams httpParams = new BasicHttpParams(); + HttpConnectionParams.setConnectionTimeout(httpParams, ActivityShare.TIMEOUT_MILLISEC); + HttpConnectionParams.setSoTimeout(httpParams, ActivityShare.TIMEOUT_MILLISEC); + HttpClient httpclient = new DefaultHttpClient(httpParams); + + HttpPost httpost = new HttpPost(ActivityShare.getBaseURL() + "?format=json&action=update"); + httpost.setEntity(new ByteArrayEntity(jRoot.toString().getBytes("UTF-8"))); + httpost.setHeader("Accept", "application/json"); + httpost.setHeader("Content-type", "application/json"); + HttpResponse response = httpclient.execute(httpost); + StatusLine statusLine = response.getStatusLine(); + if (statusLine.getStatusCode() == HttpStatus.SC_OK) { + String contentType = response.getFirstHeader("Content-Type").getValue(); + if ("application/octet-stream".equals(contentType)) { + // Update notification + builder.setContentText(mContext.getString(R.string.title_update_downloading)); + builder.setWhen(System.currentTimeMillis()); + notification = builder.build(); + notificationManager.notify(Util.NOTIFY_UPDATE, notification); + + // Download APK + File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); + folder.mkdirs(); + String fileName = response.getFirstHeader("Content-Disposition").getElements()[0] + .getParameterByName("filename").getValue(); + File download = new File(folder, fileName); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(download); + response.getEntity().writeTo(fos); + } finally { + if (fos != null) + fos.close(); + } + + return download; + } else if ("application/json".equals(contentType)) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + response.getEntity().writeTo(out); + out.close(); + throw new IOException(out.toString("UTF-8")); + } else + throw new IOException(contentType); + } else + return statusLine; + } catch (Throwable ex) { + Util.bug(null, ex); + return ex; + } + } + + @Override + protected void onPostExecute(Object result) { + if (result instanceof StatusLine) { + notificationManager.cancel(Util.NOTIFY_UPDATE); + StatusLine status = (StatusLine) result; + if (status.getStatusCode() == 204) { // No Content + String none = mContext.getString(R.string.title_update_none); + Toast.makeText(mContext, none, Toast.LENGTH_LONG).show(); + } else + Toast.makeText(mContext, status.getStatusCode() + " " + status.getReasonPhrase(), Toast.LENGTH_LONG) + .show(); + + } else if (result instanceof Throwable) { + notificationManager.cancel(Util.NOTIFY_UPDATE); + Throwable ex = (Throwable) result; + Toast.makeText(mContext, ex.toString(), Toast.LENGTH_LONG).show(); + + } else { + File download = (File) result; + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(Uri.fromFile(download), "application/vnd.android.package-archive"); + + PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + + // Update notification + builder.setContentText(mContext.getString(R.string.title_update_install)); + builder.setWhen(System.currentTimeMillis()); + builder.setAutoCancel(true); + builder.setOngoing(false); + builder.setContentIntent(pi); + notification = builder.build(); + notificationManager.notify(Util.NOTIFY_UPDATE, notification); + } + + } + } + // Helper methods private void blueStreakOfProgress(Integer current, Integer max) {