7 |
import java.net.URLConnection; |
import java.net.URLConnection; |
8 |
|
|
9 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
10 |
|
import android.app.Notification; |
11 |
|
import android.app.NotificationManager; |
12 |
|
import android.app.PendingIntent; |
13 |
import android.content.Context; |
import android.content.Context; |
14 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
15 |
import android.content.Intent; |
import android.content.Intent; |
24 |
import android.util.Log; |
import android.util.Log; |
25 |
|
|
26 |
public class CheckUpdates { |
public class CheckUpdates { |
27 |
final static long TIMESPAN_DAY = 24*60*60*1000; // one day |
final static long TIMESPAN_DAY = /*24*60*60* */1000; // one day |
28 |
|
|
29 |
final static String CHECKUPDATES = "CheckUpdates"; |
final static String CHECKUPDATES = "CheckUpdates"; |
30 |
final static String LASTCHECK = "lastcheck"; |
final static String LASTCHECK = "lastcheck"; |
31 |
|
|
38 |
String title; |
String title; |
39 |
|
|
40 |
|
|
41 |
Context context; |
Context context; |
42 |
|
|
43 |
public void checkForUpdates(Context context, String url, String title) { |
|
44 |
|
public void checkForUpdates(Context context, String url, String title, String apkUrl) { |
45 |
this.context = context; |
this.context = context; |
46 |
this.title = title; |
this.title = title; |
47 |
|
|
67 |
|
|
68 |
|
|
69 |
UpdateTask task = new UpdateTask(); |
UpdateTask task = new UpdateTask(); |
70 |
task.execute(url); |
task.execute(url, apkUrl); |
71 |
|
|
72 |
|
|
73 |
} catch (NameNotFoundException e) { |
} catch (NameNotFoundException e) { |
80 |
class UpdateTask extends AsyncTask<String,Void,Void> { |
class UpdateTask extends AsyncTask<String,Void,Void> { |
81 |
boolean result = false; |
boolean result = false; |
82 |
int newestVersion = 0; |
int newestVersion = 0; |
83 |
|
|
84 |
|
String apkUrl; |
85 |
|
|
86 |
@Override |
@Override |
87 |
protected Void doInBackground(String... arg0) { |
protected Void doInBackground(String... arg0) { |
88 |
String requestUrl = arg0[0] + "?version=" + encode(versionName) + "&phone=" + encode(phone_model) + "&android=" + encode(androidVersion); |
String requestUrl = arg0[0] + "?version=" + encode(versionName) + "&phone=" + encode(phone_model) + "&android=" + encode(androidVersion); |
89 |
|
apkUrl = arg0[1]; |
90 |
|
|
91 |
try { |
try { |
92 |
URL url = new URL(requestUrl); |
URL url = new URL(requestUrl); |
93 |
URLConnection conn = url.openConnection(); |
URLConnection conn = url.openConnection(); |
142 |
edit.putLong(LASTCHECK, now); |
edit.putLong(LASTCHECK, now); |
143 |
edit.commit(); |
edit.commit(); |
144 |
|
|
145 |
if (newestVersion > versionCode) { |
if (newestVersion > versionCode) { |
146 |
AlertDialog.Builder builder = new AlertDialog.Builder(context); |
String launchUrl = (apkUrl == null) ? "market://search?q=pname:" + packageName : apkUrl; |
147 |
builder.setTitle(title); |
//showUpdateDialog(launchUrl); |
148 |
builder.setMessage("This application has an a newer version available. Update now?"); |
showNotification(launchUrl); |
149 |
|
} |
150 |
builder.setPositiveButton("Yes", new OnClickListener() { |
|
151 |
public void onClick(DialogInterface dialog, int which) { |
} |
152 |
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + packageName) ); |
|
153 |
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
} |
154 |
try { |
|
155 |
context.startActivity(i); |
private void showNotification(String launchUrl) { |
156 |
} catch (Exception e) { |
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
157 |
Log.e(CHECKUPDATES, "Activity launch failed", e); |
//int icon = android.R.drawable.sym_action_email; //TODO: find a better icon |
158 |
} |
int icon = R.drawable.searchicon; |
159 |
context = null; |
|
160 |
|
Notification notification = new Notification(icon, "Update available", System.currentTimeMillis() ); |
161 |
} |
notification.flags |= Notification.FLAG_AUTO_CANCEL ; |
162 |
}); |
|
163 |
builder.setNegativeButton("No", new OnClickListener() { |
CharSequence contentTitle = title; |
164 |
public void onClick(DialogInterface dialog, int which) { |
CharSequence contentText = "New version available."; |
165 |
context = null; |
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 { |
try { |
187 |
builder.show(); |
context.startActivity(i); |
188 |
} catch (Exception e) { |
} catch (Exception e) { |
189 |
Log.e(CHECKUPDATES, "Builder.show failed", e); |
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 |
} |
} |
212 |
|
|