1 |
package dk.thoerup.checkupdates; |
package dk.thoerup.androidutils; |
2 |
|
|
3 |
import java.io.ByteArrayOutputStream; |
import java.io.ByteArrayOutputStream; |
4 |
import java.io.IOException; |
import java.io.IOException; |
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) { |
int icon = R.drawable.searchicon; |
159 |
Log.e(CHECKUPDATES, "Activity launch failed", e); |
|
160 |
} |
|
161 |
context = null; |
Notification notification = new Notification(icon, "Update available", System.currentTimeMillis() ); |
162 |
|
notification.flags |= Notification.FLAG_AUTO_CANCEL ; |
163 |
} |
|
164 |
}); |
CharSequence contentTitle = title; |
165 |
builder.setNegativeButton("No", new OnClickListener() { |
CharSequence contentText = "New version available."; |
166 |
public void onClick(DialogInterface dialog, int which) { |
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); |
167 |
context = null; |
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
168 |
} |
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); |
169 |
}); |
|
170 |
|
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); |
171 |
|
|
172 |
|
nm.notify(1, notification); |
173 |
|
|
174 |
|
} |
175 |
|
|
176 |
|
private void showUpdateDialog(final String launchUrl) { |
177 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(context); |
178 |
|
builder.setTitle(title); |
179 |
|
builder.setMessage("This application has an a newer version available. Update now?"); |
180 |
|
|
181 |
|
builder.setPositiveButton("Yes", new OnClickListener() { |
182 |
|
public void onClick(DialogInterface dialog, int which) { |
183 |
|
|
184 |
|
|
185 |
|
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); |
186 |
|
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
187 |
try { |
try { |
188 |
builder.show(); |
context.startActivity(i); |
189 |
} catch (Exception e) { |
} catch (Exception e) { |
190 |
Log.e(CHECKUPDATES, "Builder.show failed", e); |
Log.e(CHECKUPDATES, "Activity launch failed", e); |
191 |
} |
} |
192 |
|
context = null; |
193 |
|
|
194 |
} |
} |
195 |
|
}); |
196 |
} |
builder.setNegativeButton("No", new OnClickListener() { |
197 |
|
public void onClick(DialogInterface dialog, int which) { |
198 |
|
context = null; |
199 |
|
} |
200 |
|
}); |
201 |
|
|
202 |
|
try { |
203 |
|
builder.show(); |
204 |
|
} catch (Exception e) { |
205 |
|
Log.e(CHECKUPDATES, "Builder.show failed", e); |
206 |
|
} |
207 |
} |
} |
208 |
|
|
209 |
} |
} |
210 |
|
|
211 |
|
|
212 |
} |
} |
213 |
|
|