03a371866d9c5c9e095a31f88a7775cbacbad8b0
[android/platform/packages/providers/DownloadProvider.git] / src / com / android / providers / downloads / DownloadReceiver.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.providers.downloads;
18
19 import android.app.NotificationManager;
20 import android.content.ActivityNotFoundException;
21 import android.content.BroadcastReceiver;
22 import android.content.ContentUris;
23 import android.content.ContentValues;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.pm.PackageManager;
27 import android.content.pm.ResolveInfo;
28 import android.database.Cursor;
29 import android.provider.Downloads;
30 import android.net.ConnectivityManager;
31 import android.net.NetworkInfo;
32 import android.net.Uri;
33 import android.util.Config;
34 import android.util.Log;
35
36 import java.io.File;
37 import java.util.List;
38
39 /**
40  * Receives system broadcasts (boot, network connectivity)
41  */
42 public class DownloadReceiver extends BroadcastReceiver {
43
44     public void onReceive(Context context, Intent intent) {
45         if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
46             if (Constants.LOGVV) {
47                 Log.v(Constants.TAG, "Receiver onBoot");
48             }
49             context.startService(new Intent(context, DownloadService.class));
50         } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
51             if (Constants.LOGVV) {
52                 Log.v(Constants.TAG, "Receiver onConnectivity");
53             }
54             NetworkInfo info = (NetworkInfo)
55                     intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
56             if (info != null && info.isConnected()) {
57                 context.startService(new Intent(context, DownloadService.class));
58             }
59         } else if (intent.getAction().equals(Constants.ACTION_RETRY)) {
60             if (Constants.LOGVV) {
61                 Log.v(Constants.TAG, "Receiver retry");
62             }
63             context.startService(new Intent(context, DownloadService.class));
64         } else if (intent.getAction().equals(Constants.ACTION_OPEN)
65                 || intent.getAction().equals(Constants.ACTION_LIST)) {
66             if (Constants.LOGVV) {
67                 if (intent.getAction().equals(Constants.ACTION_OPEN)) {
68                     Log.v(Constants.TAG, "Receiver open for " + intent.getData());
69                 } else {
70                     Log.v(Constants.TAG, "Receiver list for " + intent.getData());
71                 }
72             }
73             Cursor cursor = context.getContentResolver().query(
74                     intent.getData(), null, null, null, null);
75             if (cursor != null) {
76                 if (cursor.moveToFirst()) {
77                     int statusColumn = cursor.getColumnIndexOrThrow(Downloads.STATUS);
78                     int status = cursor.getInt(statusColumn);
79                     int visibilityColumn = cursor.getColumnIndexOrThrow(Downloads.VISIBILITY);
80                     int visibility = cursor.getInt(visibilityColumn);
81                     if (Downloads.isStatusCompleted(status)
82                             && visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
83                         ContentValues values = new ContentValues();
84                         values.put(Downloads.VISIBILITY, Downloads.VISIBILITY_VISIBLE);
85                         context.getContentResolver().update(intent.getData(), values, null, null);
86                     }
87
88                     if (intent.getAction().equals(Constants.ACTION_OPEN)) {
89                         int filenameColumn = cursor.getColumnIndexOrThrow(Downloads._DATA);
90                         int mimetypeColumn = cursor.getColumnIndexOrThrow(Downloads.MIMETYPE);
91                         String filename = cursor.getString(filenameColumn);
92                         String mimetype = cursor.getString(mimetypeColumn);
93                         Uri path = Uri.parse(filename);
94                         // If there is no scheme, then it must be a file
95                         if (path.getScheme() == null) {
96                             path = Uri.fromFile(new File(filename));
97                         }
98                         Intent activityIntent = new Intent(Intent.ACTION_VIEW);
99                         activityIntent.setDataAndType(path, mimetype);
100                         activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
101                         try {
102                             context.startActivity(activityIntent);
103                         } catch (ActivityNotFoundException ex) {
104                             if (Config.LOGD) {
105                                 Log.d(Constants.TAG, "no activity for " + mimetype, ex);
106                             }
107                             // nothing anyone can do about this, but we're in a clean state,
108                             //     swallow the exception entirely
109                         }
110                     } else {
111                         int packageColumn =
112                                 cursor.getColumnIndexOrThrow(Downloads.NOTIFICATION_PACKAGE);
113                         int classColumn =
114                                 cursor.getColumnIndexOrThrow(Downloads.NOTIFICATION_CLASS);
115                         String pckg = cursor.getString(packageColumn);
116                         String clazz = cursor.getString(classColumn);
117                         if (pckg != null && clazz != null) {
118                             Intent appIntent = new Intent(Downloads.NOTIFICATION_CLICKED_ACTION);
119                             appIntent.setClassName(pckg, clazz);
120                             if (intent.getBooleanExtra("multiple", true)) {
121                                 appIntent.setData(Downloads.CONTENT_URI);
122                             } else {
123                                 appIntent.setData(intent.getData());
124                             }
125                             context.sendBroadcast(appIntent);
126                         }
127                     }
128                 }
129                 cursor.close();
130             }
131             NotificationManager notMgr = (NotificationManager) context
132                     .getSystemService(Context.NOTIFICATION_SERVICE);
133             if (notMgr != null) {
134                 notMgr.cancel((int) ContentUris.parseId(intent.getData()));
135             }
136         } else if (intent.getAction().equals(Constants.ACTION_HIDE)) {
137             if (Constants.LOGVV) {
138                 Log.v(Constants.TAG, "Receiver hide for " + intent.getData());
139             }
140             Cursor cursor = context.getContentResolver().query(
141                     intent.getData(), null, null, null, null);
142             if (cursor != null) {
143                 if (cursor.moveToFirst()) {
144                     int statusColumn = cursor.getColumnIndexOrThrow(Downloads.STATUS);
145                     int status = cursor.getInt(statusColumn);
146                     int visibilityColumn = cursor.getColumnIndexOrThrow(Downloads.VISIBILITY);
147                     int visibility = cursor.getInt(visibilityColumn);
148                     if (Downloads.isStatusCompleted(status)
149                             && visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
150                         ContentValues values = new ContentValues();
151                         values.put(Downloads.VISIBILITY, Downloads.VISIBILITY_VISIBLE);
152                         context.getContentResolver().update(intent.getData(), values, null, null);
153                     }
154                 }
155                 cursor.close();
156             }
157         }
158     }
159 }