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

Annotation of /android/CheckUpdates/src/dk/thoerup/checkupdates/CheckUpdates.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20