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

  ViewVC Help
Powered by ViewVC 1.1.20