fdroidclient / F-Droid / src / org / fdroid / fdroid / receiver / DownloadManagerReceiver.java @ 69ecaf02
History | View | Annotate | Download (2.96 KB)
1 |
package org.fdroid.fdroid.receiver; |
---|---|
2 |
|
3 |
import android.app.DownloadManager; |
4 |
import android.app.Notification; |
5 |
import android.app.NotificationManager; |
6 |
import android.app.PendingIntent; |
7 |
import android.content.BroadcastReceiver; |
8 |
import android.content.Context; |
9 |
import android.content.Intent; |
10 |
import android.support.v4.app.NotificationCompat; |
11 |
|
12 |
import org.fdroid.fdroid.AppDetails; |
13 |
import org.fdroid.fdroid.R; |
14 |
import org.fdroid.fdroid.net.AsyncDownloaderFromAndroid; |
15 |
|
16 |
/**
|
17 |
* Receive notifications from the Android DownloadManager and pass them onto the
|
18 |
* AppDetails activity
|
19 |
*/
|
20 |
public class DownloadManagerReceiver extends BroadcastReceiver { |
21 |
@Override
|
22 |
public void onReceive(Context context, Intent intent) { |
23 |
// work out the app Id to send to the AppDetails Screen
|
24 |
long downloadId = AsyncDownloaderFromAndroid.getDownloadId(intent);
|
25 |
String appId = AsyncDownloaderFromAndroid.getAppId(context, downloadId);
|
26 |
|
27 |
if (appId == null) { |
28 |
// bogus broadcast (e.g. download cancelled, but system sent a DOWNLOAD_COMPLETE)
|
29 |
return;
|
30 |
} |
31 |
|
32 |
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
|
33 |
// show a notification the user can click to install the app
|
34 |
Intent appDetails = new Intent(context, AppDetails.class);
|
35 |
appDetails.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
36 |
appDetails.setAction(intent.getAction()); |
37 |
appDetails.putExtras(intent.getExtras()); |
38 |
appDetails.putExtra(AppDetails.EXTRA_APPID, appId); |
39 |
|
40 |
PendingIntent pi = PendingIntent.getActivity( |
41 |
context, 1, appDetails, PendingIntent.FLAG_ONE_SHOT);
|
42 |
|
43 |
// launch LocalRepoActivity if the user selects this notification
|
44 |
String downloadTitle = AsyncDownloaderFromAndroid.getDownloadTitle(context, downloadId);
|
45 |
Notification notif = new NotificationCompat.Builder(context) |
46 |
.setContentTitle(downloadTitle) |
47 |
.setContentText(context.getString(R.string.tap_to_install)) |
48 |
.setSmallIcon(R.drawable.ic_stat_notify) |
49 |
.setContentIntent(pi) |
50 |
.setAutoCancel(true)
|
51 |
.build(); |
52 |
|
53 |
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
54 |
nm.notify((int)downloadId, notif);
|
55 |
} else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())) { |
56 |
// pass the notification click onto the AppDetails screen and let it handle it
|
57 |
Intent appDetails = new Intent(context, AppDetails.class);
|
58 |
appDetails.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
59 |
appDetails.setAction(intent.getAction()); |
60 |
appDetails.putExtras(intent.getExtras()); |
61 |
appDetails.putExtra(AppDetails.EXTRA_APPID, appId); |
62 |
context.startActivity(appDetails); |
63 |
} |
64 |
} |
65 |
} |