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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1127 - (show annotations) (download)
Fri Sep 24 10:57:28 2010 UTC (13 years, 7 months ago) by torben
File size: 6114 byte(s)
move CheckUpdates.java to AndroidUtils 
(target is to merge checkutils into androidutils pacakge)


1 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.app.Notification;
11 import android.app.NotificationManager;
12 import android.app.PendingIntent;
13 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
29 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 Context context;
42
43
44 public void checkForUpdates(Context context, String url, String title, String apkUrl) {
45 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 task.execute(url, apkUrl);
71
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
84 String apkUrl;
85
86 @Override
87 protected Void doInBackground(String... arg0) {
88 String requestUrl = arg0[0] + "?version=" + encode(versionName) + "&phone=" + encode(phone_model) + "&android=" + encode(androidVersion);
89 apkUrl = arg0[1];
90
91 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 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 int icon = R.drawable.searchicon;
159
160 Notification notification = new Notification(icon, "Update available", System.currentTimeMillis() );
161 notification.flags |= Notification.FLAG_AUTO_CANCEL ;
162
163 CharSequence contentTitle = title;
164 CharSequence contentText = "New version available.";
165 Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) );
166 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
167 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
168
169 notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
170
171 nm.notify(1, notification);
172
173 }
174
175 private void showUpdateDialog(final String launchUrl) {
176 AlertDialog.Builder builder = new AlertDialog.Builder(context);
177 builder.setTitle(title);
178 builder.setMessage("This application has an a newer version available. Update now?");
179
180 builder.setPositiveButton("Yes", new OnClickListener() {
181 public void onClick(DialogInterface dialog, int which) {
182
183
184 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) );
185 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
186 try {
187 context.startActivity(i);
188 } catch (Exception e) {
189 Log.e(CHECKUPDATES, "Activity launch failed", e);
190 }
191 context = null;
192
193 }
194 });
195 builder.setNegativeButton("No", new OnClickListener() {
196 public void onClick(DialogInterface dialog, int which) {
197 context = null;
198 }
199 });
200
201 try {
202 builder.show();
203 } catch (Exception e) {
204 Log.e(CHECKUPDATES, "Builder.show failed", e);
205 }
206 }
207
208 }
209
210
211 }

  ViewVC Help
Powered by ViewVC 1.1.20