Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Move update task to activity share
Browse files Browse the repository at this point in the history
Refs #1867
  • Loading branch information
M66B committed Aug 16, 2014
1 parent 3e2dda3 commit 7e3ecf4
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 128 deletions.
130 changes: 2 additions & 128 deletions src/biz/bokhorst/xprivacy/ActivityMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -822,134 +822,8 @@ private void optionPro() {
private void optionUpdate() {
if (Util.hasProLicense(this) == null)
Util.viewUri(this, ActivityMain.cProUri);
else {
new AsyncTask<Object, Object, Object>() {
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")
Expand Down
135 changes: 135 additions & 0 deletions src/biz/bokhorst/xprivacy/ActivityShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1821,6 +1825,137 @@ public void onClick(DialogInterface dialog, int which) {
}
}

public static class UpdateTask extends AsyncTask<Object, Object, Object> {
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) {
Expand Down

0 comments on commit 7e3ecf4

Please sign in to comment.