--- android/AndroidUtils/src/dk/thoerup/androidutils/CheckUpdates.java 2011/03/30 17:26:50 1245 +++ android/AndroidUtils/src/dk/thoerup/androidutils/CheckUpdates.java 2011/03/30 19:25:10 1246 @@ -1,8 +1,10 @@ package dk.thoerup.androidutils; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.RandomAccessFile; import java.net.URL; import java.net.URLConnection; @@ -21,6 +23,7 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.os.AsyncTask; +import android.os.Environment; import android.util.Log; public class CheckUpdates { @@ -40,13 +43,17 @@ Context context; - public void checkForUpdates(Context context, String url, String title, String apkUrl) { + checkForUpdates(context, url, title, apkUrl, false); + } + + public void checkForUpdates(Context context, String url, String title, String apkUrl, boolean forced) { this.context = context; this.title = title; SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE); - long lastCheck = prefs.getLong(LASTCHECK, 0); + + long lastCheck = prefs.getLong(LASTCHECK, 0); long now = System.currentTimeMillis(); //should i use android.os.SystemClock.elapsedRealtime() instead ? Log.i(CHECKUPDATES, "Now=" + now + ", lastCheck=" + lastCheck + ", lastCheck+timespan=" + (lastCheck + TIMESPAN_DAY) ); @@ -66,7 +73,7 @@ androidVersion = android.os.Build.VERSION.RELEASE; - UpdateTask task = new UpdateTask(); + UpdateTask task = new UpdateTask(forced); task.execute(url, apkUrl); @@ -77,11 +84,18 @@ } } + class UpdateTask extends AsyncTask { boolean result = false; int newestVersion = 0; String apkUrl; + + boolean forced = false; + + public UpdateTask(boolean forced) { + this.forced = forced; + } @Override protected Void doInBackground(String... arg0) { @@ -138,20 +152,62 @@ long now = System.currentTimeMillis(); SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE); //when done write + Editor edit = prefs.edit(); - edit.putLong(LASTCHECK, now); + edit.putLong(LASTCHECK, now ); edit.commit(); + if (newestVersion > versionCode) { String launchUrl = (apkUrl == null) ? "market://search?q=pname:" + packageName : apkUrl; - //showUpdateDialog(launchUrl); - showNotification(launchUrl); + + if (forced == true) { + forcedUpdate(launchUrl); + } else { + //showUpdateDialog(launchUrl); + showNotification(launchUrl); + } } } } + private void forcedUpdate(String apkUrl) { + Log.e(CHECKUPDATES, "Forced update started"); + try { + byte[] apkData = HttpUtil.getContent(apkUrl, 5000); + File tempFile = new File( Environment.getExternalStorageDirectory() + "/temp.apk" ); + + if (tempFile.exists()) { + tempFile.delete(); + } + + RandomAccessFile raf = new RandomAccessFile(tempFile,"rw"); + raf.write(apkData); + + Log.e(CHECKUPDATES, "File:" + tempFile.getPath()); + + + + Intent i =new Intent(); + i.setAction(Intent.ACTION_VIEW); + i.setDataAndType( Uri.fromFile(tempFile), "application/vnd.android.package-archive"); + i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); + + + try { + context.startActivity(i); + } catch (Exception e) { + Log.e(CHECKUPDATES, "Activity launch failed", e); + } + context = null; + + } catch (Exception e) { + Log.e(CHECKUPDATES, "forcedUpdate failed", e); + } + } + private void showNotification(String launchUrl) { NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); //int icon = android.R.drawable.sym_action_email; //TODO: find a better icon