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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 908 - (hide annotations) (download)
Sat Jun 26 00:04:58 2010 UTC (13 years, 11 months ago) by torben
Original Path: android/CheckUpdates/src/dk/thoerup/checkupdates/CheckUpdates.java
File size: 4879 byte(s)
Re-import as an android project
1 torben 908 package dk.thoerup.checkupdates;
2    
3     import java.io.ByteArrayOutputStream;
4     import java.io.IOException;
5     import java.io.InputStream;
6     import java.net.URL;
7     import java.net.URLConnection;
8    
9     import android.app.AlertDialog;
10     import android.content.Context;
11     import android.content.DialogInterface;
12     import android.content.Intent;
13     import android.content.SharedPreferences;
14     import android.content.DialogInterface.OnClickListener;
15     import android.content.SharedPreferences.Editor;
16     import android.content.pm.PackageInfo;
17     import android.content.pm.PackageManager;
18     import android.content.pm.PackageManager.NameNotFoundException;
19     import android.net.Uri;
20     import android.os.AsyncTask;
21     import android.util.Log;
22    
23     public class CheckUpdates {
24     final static long TIMESPAN_DAY = 24*60*60*1000; // one day
25     final static String CHECKUPDATES = "CheckUpdates";
26     final static String LASTCHECK = "lastcheck";
27    
28     int versionCode;
29     String versionName;
30     String packageName;
31     String phone_model;
32     String androidVersion;
33    
34     String title;
35    
36    
37     Context context;
38    
39     public void checkForUpdates(Context context, String url, String title) {
40     this.context = context;
41     this.title = title;
42    
43     SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE);
44     long lastCheck = prefs.getLong(LASTCHECK, 0);
45    
46     long now = System.currentTimeMillis(); //should i use android.os.SystemClock.elapsedRealtime() instead ?
47     Log.i(CHECKUPDATES, "Now=" + now + ", lastCheck=" + lastCheck + ", lastCheck+timespan=" + (lastCheck + TIMESPAN_DAY) );
48    
49     if ( now > (lastCheck+TIMESPAN_DAY) ){
50    
51     PackageManager pm = context.getPackageManager();
52     try {
53     PackageInfo pi;
54    
55     pi = pm.getPackageInfo(context.getPackageName(), 0);
56    
57     versionCode = pi.versionCode;
58     versionName = pi.versionName;
59     packageName = pi.packageName;
60     phone_model = android.os.Build.MODEL;
61     androidVersion = android.os.Build.VERSION.RELEASE;
62    
63    
64     UpdateTask task = new UpdateTask();
65     task.execute(url);
66    
67    
68     } catch (NameNotFoundException e) {
69     Log.e(CHECKUPDATES, "NamingException", e);
70     }
71    
72     }
73     }
74    
75     class UpdateTask extends AsyncTask<String,Void,Void> {
76     boolean result = false;
77     int newestVersion = 0;
78    
79     @Override
80     protected Void doInBackground(String... arg0) {
81     String requestUrl = arg0[0] + "?version=" + encode(versionName) + "&phone=" + encode(phone_model) + "&android=" + encode(androidVersion);
82     try {
83     URL url = new URL(requestUrl);
84     URLConnection conn = url.openConnection();
85     conn.setConnectTimeout(2500);
86    
87     String resultStr = readIOStream( conn.getInputStream() );
88     resultStr = resultStr.trim();
89     newestVersion = Integer.parseInt(resultStr);
90    
91     result = true;
92    
93     } catch (Exception e) {
94     Log.e(CHECKUPDATES, "Check for " + packageName + " failed!", e);
95     }
96    
97     return null;
98     }
99    
100     private String encode(String data) {
101     try {
102     return java.net.URLEncoder.encode(data, "UTF-8");
103     } catch (Exception e) {
104     return data; //if encoding fails, return original and hope all goes well
105     }
106     }
107    
108     private String readIOStream(InputStream in) throws IOException {
109     ByteArrayOutputStream baos = new ByteArrayOutputStream();
110    
111     byte buf[] = new byte[1024];
112     int len;
113    
114     while ( (len=in.read(buf)) != -1) {
115     baos.write(buf, 0, len);
116     }
117    
118     return baos.toString();
119     }
120    
121     @Override
122     protected void onPostExecute(Void r) {
123     super.onPostExecute(r);
124    
125     Log.i(CHECKUPDATES, "result=" + result + ", newestVersion" + newestVersion + ", versionCode=" + versionCode);
126    
127     if (result == true) {
128    
129     long now = System.currentTimeMillis();
130     SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE);
131     //when done write
132     Editor edit = prefs.edit();
133     edit.putLong(LASTCHECK, now);
134     edit.commit();
135    
136     if (newestVersion > versionCode) {
137     AlertDialog.Builder builder = new AlertDialog.Builder(context);
138     builder.setTitle(title);
139     builder.setMessage("This application has an a newer version available. Update now?");
140    
141     builder.setPositiveButton("Yes", new OnClickListener() {
142     public void onClick(DialogInterface dialog, int which) {
143     Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + packageName) );
144     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
145     try {
146     context.startActivity(i);
147     } catch (Exception e) {
148     Log.e(CHECKUPDATES, "Activity launch failed", e);
149     }
150     context = null;
151    
152     }
153     });
154     builder.setNegativeButton("No", new OnClickListener() {
155     public void onClick(DialogInterface dialog, int which) {
156     context = null;
157     }
158     });
159    
160     try {
161     builder.show();
162     } catch (Exception e) {
163     Log.e(CHECKUPDATES, "Builder.show failed", e);
164     }
165    
166    
167     }
168    
169     }
170    
171     }
172    
173     }
174    
175    
176     }

  ViewVC Help
Powered by ViewVC 1.1.20