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