Initial Contribution
[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.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.database.Cursor;
28 import android.provider.Downloads;
29 import android.net.ConnectivityManager;
30 import android.net.NetworkInfo;
31 import android.net.Uri;
32 import android.util.Config;
33 import android.util.Log;
34
35 import java.io.File;
36 import java.util.List;
37
38 /**
39  * Receives system broadcasts (boot, network connectivity)
40  */
41 public class DownloadReceiver extends BroadcastReceiver {
42
43     /** Tag used for debugging/logging */
44     public static final String TAG = Constants.TAG;
45
46     public void onReceive(Context context, Intent intent) {
47         if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
48             if (Constants.LOGVV) {
49                 Log.v(TAG, "Receiver onBoot");
50             }
51             context.startService(new Intent(context, DownloadService.class));
52         } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
53             if (Constants.LOGVV) {
54                 Log.v(TAG, "Receiver onConnectivity");
55             }
56             NetworkInfo info = (NetworkInfo)
57                     intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
58             if (info != null && info.isConnected()) {
59                 context.startService(new Intent(context, DownloadService.class));
60             }
61         } else if (intent.getAction().equals(Constants.ACTION_RETRY)) {
62             if (Constants.LOGVV) {
63                 Log.v(TAG, "Receiver retry");
64             }
65             context.startService(new Intent(context, DownloadService.class));
66         } else if (intent.getAction().equals(Constants.ACTION_OPEN)
67                 || intent.getAction().equals(Constants.ACTION_LIST)) {
68             if (Constants.LOGVV) {
69                 if (intent.getAction().equals(Constants.ACTION_OPEN)) {
70                     Log.v(Constants.TAG, "Receiver open for " + intent.getData());
71                 } else {
72                     Log.v(Constants.TAG, "Receiver list for " + intent.getData());
73                 }
74             }
75             Cursor cursor = context.getContentResolver().query(
76                     intent.getData(), null, null, null, null);
77             if (cursor != null) {
78                 boolean mustCommit = false;
79                 if (cursor.moveToFirst()) {
80                     int statusColumn = cursor.getColumnIndexOrThrow(Downloads.STATUS);
81                     int status = cursor.getInt(statusColumn);
82                     int visibilityColumn = cursor.getColumnIndexOrThrow(Downloads.VISIBILITY);
83                     int visibility = cursor.getInt(visibilityColumn);
84                     if (Downloads.isStatusCompleted(status)
85                             && visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
86                         cursor.updateInt(visibilityColumn, Downloads.VISIBILITY_VISIBLE);
87                         mustCommit = true;
88                     }
89
90                     if (intent.getAction().equals(Constants.ACTION_OPEN)) {
91                         int filenameColumn = cursor.getColumnIndexOrThrow(Downloads.FILENAME);
92                         int mimetypeColumn = cursor.getColumnIndexOrThrow(Downloads.MIMETYPE);
93                         String filename = cursor.getString(filenameColumn);
94                         String mimetype = cursor.getString(mimetypeColumn);
95                         Uri path = Uri.parse(filename);
96                         // If there is no scheme, then it must be a file
97                         if (path.getScheme() == null) {
98                             path = Uri.fromFile(new File(filename));
99                         }
100                         Intent activityIntent = new Intent(Intent.ACTION_VIEW);
101                         activityIntent.setDataAndType(path, mimetype);
102                         activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
103                         try {
104                             context.startActivity(activityIntent);
105                         } catch (ActivityNotFoundException ex) {
106                             if (Config.LOGD) {
107                                 Log.d(Constants.TAG, "no activity for " + mimetype, ex);
108                             }
109                             // nothing anyone can do about this, but we're in a clean state,
110                             //     swallow the exception entirely
111                         }
112                     } else {
113                         int packageColumn =
114                                 cursor.getColumnIndexOrThrow(Downloads.NOTIFICATION_PACKAGE);
115                         int classColumn =
116                                 cursor.getColumnIndexOrThrow(Downloads.NOTIFICATION_CLASS);
117                         String pckg = cursor.getString(packageColumn);
118                         String clazz = cursor.getString(classColumn);
119                         if (pckg != null && clazz != null) {
120                             Intent appIntent = new Intent(Downloads.NOTIFICATION_CLICKED_ACTION);
121                             appIntent.setClassName(pckg, clazz);
122                             if (intent.getBooleanExtra("multiple", true)) {
123                                 appIntent.setData(Downloads.CONTENT_URI);
124                             } else {
125                                 appIntent.setData(intent.getData());
126                             }
127                             context.sendBroadcast(appIntent);
128                         }
129                     }
130                 }
131                 if (mustCommit) {
132                     if (!cursor.commitUpdates()) {
133                         Log.e(Constants.TAG, "commitUpdate failed in onReceive/OPEN-LIST");
134                     }
135                 }
136                 cursor.close();
137             }
138             NotificationManager notMgr = (NotificationManager) context
139                     .getSystemService(Context.NOTIFICATION_SERVICE);
140             if (notMgr != null) {
141                 notMgr.cancel((int) ContentUris.parseId(intent.getData()));
142             }
143         } else if (intent.getAction().equals(Constants.ACTION_HIDE)) {
144             if (Constants.LOGVV) {
145                 Log.v(Constants.TAG, "Receiver hide for " + intent.getData());
146             }
147             Cursor cursor = context.getContentResolver().query(
148                     intent.getData(), null, null, null, null);
149             if (cursor != null) {
150                 if (cursor.moveToFirst()) {
151                     int statusColumn = cursor.getColumnIndexOrThrow(Downloads.STATUS);
152                     int status = cursor.getInt(statusColumn);
153                     int visibilityColumn = cursor.getColumnIndexOrThrow(Downloads.VISIBILITY);
154                     int visibility = cursor.getInt(visibilityColumn);
155                     if (Downloads.isStatusCompleted(status)
156                             && visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
157                         cursor.updateInt(visibilityColumn, Downloads.VISIBILITY_VISIBLE);
158                         if (!cursor.commitUpdates()) {
159                             Log.e(Constants.TAG, "commitUpdate failed in onReceive/HIDE");
160                         }
161                     }
162                 }
163                 cursor.close();
164             }
165         }
166     }
167 }