/[projects]/android/AndroidUtils/src/dk/thoerup/androidutils/CheckUpdates.java
ViewVC logotype

Diff of /android/AndroidUtils/src/dk/thoerup/androidutils/CheckUpdates.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1245 by torben, Fri Sep 24 11:06:02 2010 UTC revision 1246 by torben, Wed Mar 30 19:25:10 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.androidutils;  package dk.thoerup.androidutils;
2    
3  import java.io.ByteArrayOutputStream;  import java.io.ByteArrayOutputStream;
4    import java.io.File;
5  import java.io.IOException;  import java.io.IOException;
6  import java.io.InputStream;  import java.io.InputStream;
7    import java.io.RandomAccessFile;
8  import java.net.URL;  import java.net.URL;
9  import java.net.URLConnection;  import java.net.URLConnection;
10    
# Line 21  import android.content.pm.PackageManager Line 23  import android.content.pm.PackageManager
23  import android.content.pm.PackageManager.NameNotFoundException;  import android.content.pm.PackageManager.NameNotFoundException;
24  import android.net.Uri;  import android.net.Uri;
25  import android.os.AsyncTask;  import android.os.AsyncTask;
26    import android.os.Environment;
27  import android.util.Log;  import android.util.Log;
28    
29  public class CheckUpdates {  public class CheckUpdates {
# Line 40  public class CheckUpdates { Line 43  public class CheckUpdates {
43                    
44          Context context;          Context context;
45    
   
46          public void checkForUpdates(Context context, String url, String title,  String apkUrl) {          public void checkForUpdates(Context context, String url, String title,  String apkUrl) {
47                    checkForUpdates(context, url, title,  apkUrl, false);
48            }
49    
50            public void checkForUpdates(Context context, String url, String title,  String apkUrl, boolean forced) {
51                  this.context = context;                  this.context = context;
52                  this.title = title;                  this.title = title;
53                                    
54                  SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE);                  SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE);
55                  long lastCheck = prefs.getLong(LASTCHECK, 0);                          
56                    long lastCheck = prefs.getLong(LASTCHECK, 0);          
57    
58                  long now = System.currentTimeMillis(); //should i use  android.os.SystemClock.elapsedRealtime() instead ?                  long now = System.currentTimeMillis(); //should i use  android.os.SystemClock.elapsedRealtime() instead ?
59                  Log.i(CHECKUPDATES, "Now=" + now + ",  lastCheck=" + lastCheck + ", lastCheck+timespan=" + (lastCheck + TIMESPAN_DAY) );                  Log.i(CHECKUPDATES, "Now=" + now + ",  lastCheck=" + lastCheck + ", lastCheck+timespan=" + (lastCheck + TIMESPAN_DAY) );
# Line 66  public class CheckUpdates { Line 73  public class CheckUpdates {
73                                  androidVersion = android.os.Build.VERSION.RELEASE;                                  androidVersion = android.os.Build.VERSION.RELEASE;
74                                                                    
75                    
76                                  UpdateTask task = new UpdateTask();                                  UpdateTask task = new UpdateTask(forced);
77                                  task.execute(url, apkUrl);                                  task.execute(url, apkUrl);
78                                                                    
79    
# Line 77  public class CheckUpdates { Line 84  public class CheckUpdates {
84                  }                  }
85          }          }
86                    
87            
88          class UpdateTask extends AsyncTask<String,Void,Void> {          class UpdateTask extends AsyncTask<String,Void,Void> {
89                  boolean result = false;                  boolean result = false;
90                  int newestVersion = 0;                  int newestVersion = 0;
91                                    
92                  String apkUrl;                  String apkUrl;
93                    
94                    boolean forced = false;
95                    
96                    public UpdateTask(boolean forced) {
97                            this.forced = forced;
98                    }
99    
100                  @Override                  @Override
101                  protected Void doInBackground(String... arg0) {                                  protected Void doInBackground(String... arg0) {                
# Line 138  public class CheckUpdates { Line 152  public class CheckUpdates {
152                                  long now = System.currentTimeMillis();                                  long now = System.currentTimeMillis();
153                                  SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE);                                  SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE);
154                                  //when done write                                  //when done write
155                                    
156                                  Editor edit = prefs.edit();                                  Editor edit = prefs.edit();
157                                  edit.putLong(LASTCHECK, now);                                  edit.putLong(LASTCHECK, now );
158                                  edit.commit();                                  edit.commit();
159                                                                    
160                                    
161                                  if (newestVersion > versionCode) {                                                        if (newestVersion > versionCode) {                      
162                                          String launchUrl = (apkUrl == null) ? "market://search?q=pname:" + packageName : apkUrl;                                          String launchUrl = (apkUrl == null) ? "market://search?q=pname:" + packageName : apkUrl;
163                                          //showUpdateDialog(launchUrl);                                          
164                                          showNotification(launchUrl);                                          if (forced == true) {
165                                                    forcedUpdate(launchUrl);
166                                            } else {
167                                                    //showUpdateDialog(launchUrl);
168                                                    showNotification(launchUrl);
169                                            }
170                                  }                                  }
171                                                                    
172                          }                          }
173                                                    
174                  }                  }
175                                    
176                    private void forcedUpdate(String apkUrl) {
177                            Log.e(CHECKUPDATES, "Forced update started");
178                            try {
179                                    byte[] apkData = HttpUtil.getContent(apkUrl, 5000);
180                                    File tempFile = new File( Environment.getExternalStorageDirectory() + "/temp.apk" );
181                                    
182                                    if (tempFile.exists()) {
183                                            tempFile.delete();
184                                    }
185                                    
186                                    RandomAccessFile raf = new RandomAccessFile(tempFile,"rw");
187                                    raf.write(apkData);
188                                    
189                                    Log.e(CHECKUPDATES, "File:" + tempFile.getPath());
190                                    
191                                    
192                                    
193                                    Intent i =new Intent();
194                                    i.setAction(Intent.ACTION_VIEW);
195                                    i.setDataAndType( Uri.fromFile(tempFile), "application/vnd.android.package-archive");
196                                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
197    
198    
199                                    try {
200                                            context.startActivity(i);
201                                    } catch (Exception e) {
202                                            Log.e(CHECKUPDATES, "Activity launch failed", e);
203                                    }
204                                    context = null;
205                                    
206                            } catch (Exception e) {
207                                    Log.e(CHECKUPDATES, "forcedUpdate failed", e);
208                            }
209                    }
210                    
211                  private void showNotification(String launchUrl) {                  private void showNotification(String launchUrl) {
212                          NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);                          NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
213                          //int icon = android.R.drawable.sym_action_email; //TODO: find a better icon                          //int icon = android.R.drawable.sym_action_email; //TODO: find a better icon

Legend:
Removed from v.1245  
changed lines
  Added in v.1246

  ViewVC Help
Powered by ViewVC 1.1.20