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