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 |
torben |
1350 |
import dk.thoerup.genericjavautils.HttpUtil;; |
30 |
|
|
|
31 |
torben |
908 |
public class CheckUpdates { |
32 |
torben |
946 |
final static long TIMESPAN_DAY = 24*60*60*1000; // one day |
33 |
torben |
943 |
|
34 |
torben |
908 |
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 |
torben |
910 |
Context context; |
47 |
torben |
908 |
|
48 |
torben |
1246 |
public void checkForUpdates(Context context, String url, String title, String apkUrl) { |
49 |
|
|
checkForUpdates(context, url, title, apkUrl, false); |
50 |
|
|
} |
51 |
torben |
910 |
|
52 |
torben |
1246 |
public void checkForUpdates(Context context, String url, String title, String apkUrl, boolean forced) { |
53 |
torben |
908 |
this.context = context; |
54 |
|
|
this.title = title; |
55 |
|
|
|
56 |
|
|
SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE); |
57 |
torben |
1246 |
|
58 |
|
|
long lastCheck = prefs.getLong(LASTCHECK, 0); |
59 |
torben |
908 |
|
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 |
torben |
1246 |
UpdateTask task = new UpdateTask(forced); |
79 |
torben |
910 |
task.execute(url, apkUrl); |
80 |
torben |
908 |
|
81 |
|
|
|
82 |
|
|
} catch (NameNotFoundException e) { |
83 |
|
|
Log.e(CHECKUPDATES, "NamingException", e); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
} |
87 |
|
|
} |
88 |
|
|
|
89 |
torben |
1246 |
|
90 |
torben |
908 |
class UpdateTask extends AsyncTask<String,Void,Void> { |
91 |
|
|
boolean result = false; |
92 |
|
|
int newestVersion = 0; |
93 |
torben |
910 |
|
94 |
|
|
String apkUrl; |
95 |
torben |
1246 |
|
96 |
|
|
boolean forced = false; |
97 |
|
|
|
98 |
|
|
public UpdateTask(boolean forced) { |
99 |
|
|
this.forced = forced; |
100 |
|
|
} |
101 |
torben |
908 |
|
102 |
|
|
@Override |
103 |
|
|
protected Void doInBackground(String... arg0) { |
104 |
|
|
String requestUrl = arg0[0] + "?version=" + encode(versionName) + "&phone=" + encode(phone_model) + "&android=" + encode(androidVersion); |
105 |
torben |
910 |
apkUrl = arg0[1]; |
106 |
|
|
|
107 |
torben |
908 |
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 |
torben |
1246 |
|
158 |
torben |
908 |
Editor edit = prefs.edit(); |
159 |
torben |
1246 |
edit.putLong(LASTCHECK, now ); |
160 |
torben |
908 |
edit.commit(); |
161 |
|
|
|
162 |
torben |
1246 |
|
163 |
torben |
943 |
if (newestVersion > versionCode) { |
164 |
|
|
String launchUrl = (apkUrl == null) ? "market://search?q=pname:" + packageName : apkUrl; |
165 |
torben |
1246 |
|
166 |
|
|
if (forced == true) { |
167 |
|
|
forcedUpdate(launchUrl); |
168 |
|
|
} else { |
169 |
|
|
//showUpdateDialog(launchUrl); |
170 |
|
|
showNotification(launchUrl); |
171 |
|
|
} |
172 |
torben |
943 |
} |
173 |
|
|
|
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
} |
177 |
|
|
|
178 |
torben |
1246 |
private void forcedUpdate(String apkUrl) { |
179 |
|
|
Log.e(CHECKUPDATES, "Forced update started"); |
180 |
|
|
try { |
181 |
|
|
byte[] apkData = HttpUtil.getContent(apkUrl, 5000); |
182 |
torben |
1247 |
File tempFile = new File( Environment.getExternalStorageDirectory() + "/" + HttpUtil.getLastPart(apkUrl) ); |
183 |
torben |
1246 |
|
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 |
|
|
|
208 |
|
|
} catch (Exception e) { |
209 |
|
|
Log.e(CHECKUPDATES, "forcedUpdate failed", e); |
210 |
|
|
} |
211 |
|
|
} |
212 |
|
|
|
213 |
torben |
943 |
private void showNotification(String launchUrl) { |
214 |
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
215 |
torben |
944 |
//int icon = android.R.drawable.sym_action_email; //TODO: find a better icon |
216 |
|
|
int icon = R.drawable.searchicon; |
217 |
torben |
943 |
|
218 |
torben |
1129 |
|
219 |
torben |
943 |
Notification notification = new Notification(icon, "Update available", System.currentTimeMillis() ); |
220 |
|
|
notification.flags |= Notification.FLAG_AUTO_CANCEL ; |
221 |
|
|
|
222 |
|
|
CharSequence contentTitle = title; |
223 |
|
|
CharSequence contentText = "New version available."; |
224 |
|
|
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); |
225 |
torben |
944 |
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
226 |
torben |
943 |
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); |
227 |
|
|
|
228 |
|
|
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); |
229 |
|
|
|
230 |
|
|
nm.notify(1, notification); |
231 |
|
|
|
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
private void showUpdateDialog(final String launchUrl) { |
235 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context); |
236 |
|
|
builder.setTitle(title); |
237 |
|
|
builder.setMessage("This application has an a newer version available. Update now?"); |
238 |
|
|
|
239 |
|
|
builder.setPositiveButton("Yes", new OnClickListener() { |
240 |
|
|
public void onClick(DialogInterface dialog, int which) { |
241 |
|
|
|
242 |
torben |
908 |
|
243 |
torben |
943 |
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); |
244 |
|
|
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
245 |
torben |
908 |
try { |
246 |
torben |
943 |
context.startActivity(i); |
247 |
torben |
908 |
} catch (Exception e) { |
248 |
torben |
943 |
Log.e(CHECKUPDATES, "Activity launch failed", e); |
249 |
torben |
908 |
} |
250 |
torben |
943 |
context = null; |
251 |
torben |
908 |
|
252 |
|
|
} |
253 |
torben |
943 |
}); |
254 |
|
|
builder.setNegativeButton("No", new OnClickListener() { |
255 |
|
|
public void onClick(DialogInterface dialog, int which) { |
256 |
|
|
context = null; |
257 |
|
|
} |
258 |
|
|
}); |
259 |
|
|
|
260 |
|
|
try { |
261 |
|
|
builder.show(); |
262 |
|
|
} catch (Exception e) { |
263 |
|
|
Log.e(CHECKUPDATES, "Builder.show failed", e); |
264 |
torben |
908 |
} |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
|
270 |
|
|
} |