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; |
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 |
|
|
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 |
String launchUrl = (apkUrl == null) ? "market://search?q=pname:" + packageName : apkUrl; |
|
153 |
|
} |
154 |
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); |
|
155 |
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
private void showNotification(String launchUrl) { |
156 |
try { |
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
157 |
context.startActivity(i); |
int icon = android.R.drawable.sym_action_email; //TODO: find a better icon |
158 |
} catch (Exception e) { |
|
159 |
Log.e(CHECKUPDATES, "Activity launch failed", e); |
Notification notification = new Notification(icon, "Update available", System.currentTimeMillis() ); |
160 |
} |
notification.flags |= Notification.FLAG_AUTO_CANCEL ; |
161 |
context = null; |
|
162 |
|
CharSequence contentTitle = title; |
163 |
} |
CharSequence contentText = "New version available."; |
164 |
}); |
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); |
165 |
builder.setNegativeButton("No", new OnClickListener() { |
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); |
166 |
public void onClick(DialogInterface dialog, int which) { |
|
167 |
context = null; |
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); |
168 |
} |
|
169 |
}); |
nm.notify(1, notification); |
170 |
|
|
171 |
|
} |
172 |
|
|
173 |
|
private void showUpdateDialog(final String launchUrl) { |
174 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(context); |
175 |
|
builder.setTitle(title); |
176 |
|
builder.setMessage("This application has an a newer version available. Update now?"); |
177 |
|
|
178 |
|
builder.setPositiveButton("Yes", new OnClickListener() { |
179 |
|
public void onClick(DialogInterface dialog, int which) { |
180 |
|
|
181 |
|
|
182 |
|
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); |
183 |
|
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
184 |
try { |
try { |
185 |
builder.show(); |
context.startActivity(i); |
186 |
} catch (Exception e) { |
} catch (Exception e) { |
187 |
Log.e(CHECKUPDATES, "Builder.show failed", e); |
Log.e(CHECKUPDATES, "Activity launch failed", e); |
188 |
} |
} |
189 |
|
context = null; |
190 |
|
|
191 |
} |
} |
192 |
|
}); |
193 |
} |
builder.setNegativeButton("No", new OnClickListener() { |
194 |
|
public void onClick(DialogInterface dialog, int which) { |
195 |
|
context = null; |
196 |
|
} |
197 |
|
}); |
198 |
|
|
199 |
|
try { |
200 |
|
builder.show(); |
201 |
|
} catch (Exception e) { |
202 |
|
Log.e(CHECKUPDATES, "Builder.show failed", e); |
203 |
|
} |
204 |
} |
} |
205 |
|
|
206 |
} |
} |
207 |
|
|
208 |
|
|
209 |
} |
} |
210 |
|
|