1 |
package dk.thoerup.androidutils; |
2 |
|
3 |
import java.io.ByteArrayOutputStream; |
4 |
import java.io.File; |
5 |
import java.io.IOException; |
6 |
import java.io.InputStream; |
7 |
import java.io.RandomAccessFile; |
8 |
import java.net.URL; |
9 |
import java.net.URLConnection; |
10 |
|
11 |
import android.app.AlertDialog; |
12 |
import android.app.Notification; |
13 |
import android.app.NotificationManager; |
14 |
import android.app.PendingIntent; |
15 |
import android.content.Context; |
16 |
import android.content.DialogInterface; |
17 |
import android.content.Intent; |
18 |
import android.content.SharedPreferences; |
19 |
import android.content.DialogInterface.OnClickListener; |
20 |
import android.content.SharedPreferences.Editor; |
21 |
import android.content.pm.PackageInfo; |
22 |
import android.content.pm.PackageManager; |
23 |
import android.content.pm.PackageManager.NameNotFoundException; |
24 |
import android.net.Uri; |
25 |
import android.os.AsyncTask; |
26 |
import android.os.Environment; |
27 |
import android.util.Log; |
28 |
|
29 |
import dk.thoerup.genericjavautils.HttpUtil;; |
30 |
|
31 |
public class CheckUpdates { |
32 |
final static long TIMESPAN_DAY = 24*60*60*1000; // one day |
33 |
|
34 |
final static String CHECKUPDATES = "CheckUpdates"; |
35 |
final static String LASTCHECK = "lastcheck"; |
36 |
|
37 |
int versionCode; |
38 |
String versionName; |
39 |
String packageName; |
40 |
String phone_model; |
41 |
String androidVersion; |
42 |
|
43 |
String title; |
44 |
|
45 |
|
46 |
Context context; |
47 |
|
48 |
public void checkForUpdates(Context context, String url, String title, String apkUrl) { |
49 |
checkForUpdates(context, url, title, apkUrl, false); |
50 |
} |
51 |
|
52 |
public void checkForUpdates(Context context, String url, String title, String apkUrl, boolean forced) { |
53 |
this.context = context; |
54 |
this.title = title; |
55 |
|
56 |
SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE); |
57 |
|
58 |
long lastCheck = prefs.getLong(LASTCHECK, 0); |
59 |
|
60 |
long now = System.currentTimeMillis(); //should i use android.os.SystemClock.elapsedRealtime() instead ? |
61 |
Log.i(CHECKUPDATES, "Now=" + now + ", lastCheck=" + lastCheck + ", lastCheck+timespan=" + (lastCheck + TIMESPAN_DAY) ); |
62 |
|
63 |
if ( now > (lastCheck+TIMESPAN_DAY) ){ |
64 |
|
65 |
PackageManager pm = context.getPackageManager(); |
66 |
try { |
67 |
PackageInfo pi; |
68 |
|
69 |
pi = pm.getPackageInfo(context.getPackageName(), 0); |
70 |
|
71 |
versionCode = pi.versionCode; |
72 |
versionName = pi.versionName; |
73 |
packageName = pi.packageName; |
74 |
phone_model = android.os.Build.MODEL; |
75 |
androidVersion = android.os.Build.VERSION.RELEASE; |
76 |
|
77 |
|
78 |
UpdateTask task = new UpdateTask(forced); |
79 |
task.execute(url, apkUrl); |
80 |
|
81 |
|
82 |
} catch (NameNotFoundException e) { |
83 |
Log.e(CHECKUPDATES, "NamingException", e); |
84 |
} |
85 |
|
86 |
} |
87 |
} |
88 |
|
89 |
|
90 |
class UpdateTask extends AsyncTask<String,Void,Void> { |
91 |
boolean result = false; |
92 |
int newestVersion = 0; |
93 |
|
94 |
String apkUrl; |
95 |
|
96 |
boolean forced = false; |
97 |
|
98 |
public UpdateTask(boolean forced) { |
99 |
this.forced = forced; |
100 |
} |
101 |
|
102 |
@Override |
103 |
protected Void doInBackground(String... arg0) { |
104 |
String requestUrl = arg0[0] + "?version=" + encode(versionName) + "&phone=" + encode(phone_model) + "&android=" + encode(androidVersion); |
105 |
apkUrl = arg0[1]; |
106 |
|
107 |
try { |
108 |
URL url = new URL(requestUrl); |
109 |
URLConnection conn = url.openConnection(); |
110 |
conn.setConnectTimeout(2500); |
111 |
|
112 |
String resultStr = readIOStream( conn.getInputStream() ); |
113 |
resultStr = resultStr.trim(); |
114 |
newestVersion = Integer.parseInt(resultStr); |
115 |
|
116 |
result = true; |
117 |
|
118 |
} catch (Exception e) { |
119 |
Log.e(CHECKUPDATES, "Check for " + packageName + " failed!", e); |
120 |
} |
121 |
|
122 |
return null; |
123 |
} |
124 |
|
125 |
private String encode(String data) { |
126 |
try { |
127 |
return java.net.URLEncoder.encode(data, "UTF-8"); |
128 |
} catch (Exception e) { |
129 |
return data; //if encoding fails, return original and hope all goes well |
130 |
} |
131 |
} |
132 |
|
133 |
private String readIOStream(InputStream in) throws IOException { |
134 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
135 |
|
136 |
byte buf[] = new byte[1024]; |
137 |
int len; |
138 |
|
139 |
while ( (len=in.read(buf)) != -1) { |
140 |
baos.write(buf, 0, len); |
141 |
} |
142 |
|
143 |
return baos.toString(); |
144 |
} |
145 |
|
146 |
@Override |
147 |
protected void onPostExecute(Void r) { |
148 |
super.onPostExecute(r); |
149 |
|
150 |
Log.i(CHECKUPDATES, "result=" + result + ", newestVersion" + newestVersion + ", versionCode=" + versionCode); |
151 |
|
152 |
if (result == true) { |
153 |
|
154 |
long now = System.currentTimeMillis(); |
155 |
SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE); |
156 |
//when done write |
157 |
|
158 |
Editor edit = prefs.edit(); |
159 |
edit.putLong(LASTCHECK, now ); |
160 |
edit.commit(); |
161 |
|
162 |
|
163 |
if (newestVersion > versionCode) { |
164 |
String launchUrl = (apkUrl == null) ? "market://search?q=pname:" + packageName : apkUrl; |
165 |
|
166 |
if (forced == true) { |
167 |
forcedUpdate(launchUrl); |
168 |
} else { |
169 |
//showUpdateDialog(launchUrl); |
170 |
showNotification(launchUrl); |
171 |
} |
172 |
} |
173 |
|
174 |
} |
175 |
|
176 |
} |
177 |
|
178 |
private void forcedUpdate(String apkUrl) { |
179 |
Log.e(CHECKUPDATES, "Forced update started"); |
180 |
try { |
181 |
byte[] apkData = HttpUtil.getContent(apkUrl, 5000); |
182 |
File tempFile = new File( Environment.getExternalStorageDirectory() + "/" + HttpUtil.getLastPart(apkUrl) ); |
183 |
|
184 |
if (tempFile.exists()) { |
185 |
tempFile.delete(); |
186 |
} |
187 |
|
188 |
RandomAccessFile raf = new RandomAccessFile(tempFile,"rw"); |
189 |
raf.write(apkData); |
190 |
|
191 |
Log.e(CHECKUPDATES, "File:" + tempFile.getPath()); |
192 |
|
193 |
|
194 |
|
195 |
Intent i =new Intent(); |
196 |
i.setAction(Intent.ACTION_VIEW); |
197 |
i.setDataAndType( Uri.fromFile(tempFile), "application/vnd.android.package-archive"); |
198 |
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
199 |
|
200 |
|
201 |
try { |
202 |
context.startActivity(i); |
203 |
} catch (Exception e) { |
204 |
Log.e(CHECKUPDATES, "Activity launch failed", e); |
205 |
} |
206 |
context = null; |
207 |
raf.close(); |
208 |
|
209 |
} catch (Exception e) { |
210 |
Log.e(CHECKUPDATES, "forcedUpdate failed", e); |
211 |
} |
212 |
} |
213 |
|
214 |
private void showNotification(String launchUrl) { |
215 |
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
216 |
//int icon = android.R.drawable.sym_action_email; //TODO: find a better icon |
217 |
int icon = R.drawable.searchicon; |
218 |
|
219 |
|
220 |
Notification notification = new Notification(icon, "Update available", System.currentTimeMillis() ); |
221 |
notification.flags |= Notification.FLAG_AUTO_CANCEL ; |
222 |
|
223 |
CharSequence contentTitle = title; |
224 |
CharSequence contentText = "New version available."; |
225 |
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); |
226 |
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
227 |
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); |
228 |
|
229 |
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); |
230 |
|
231 |
nm.notify(1, notification); |
232 |
|
233 |
} |
234 |
|
235 |
private void showUpdateDialog(final String launchUrl) { |
236 |
AlertDialog.Builder builder = new AlertDialog.Builder(context); |
237 |
builder.setTitle(title); |
238 |
builder.setMessage("This application has an a newer version available. Update now?"); |
239 |
|
240 |
builder.setPositiveButton("Yes", new OnClickListener() { |
241 |
public void onClick(DialogInterface dialog, int which) { |
242 |
|
243 |
|
244 |
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); |
245 |
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
246 |
try { |
247 |
context.startActivity(i); |
248 |
} catch (Exception e) { |
249 |
Log.e(CHECKUPDATES, "Activity launch failed", e); |
250 |
} |
251 |
context = null; |
252 |
|
253 |
} |
254 |
}); |
255 |
builder.setNegativeButton("No", new OnClickListener() { |
256 |
public void onClick(DialogInterface dialog, int which) { |
257 |
context = null; |
258 |
} |
259 |
}); |
260 |
|
261 |
try { |
262 |
builder.show(); |
263 |
} catch (Exception e) { |
264 |
Log.e(CHECKUPDATES, "Builder.show failed", e); |
265 |
} |
266 |
} |
267 |
|
268 |
} |
269 |
|
270 |
|
271 |
} |