OSDN Git Service

74f29318cbef66be2c7662cdb4c533cf630394d7
[android-x86/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                     Log.i(Constants.TAG, "Broadcast: Network Up");
56                 }
57                 context.startService(new Intent(context, DownloadService.class));
58             } else {
59                 if (Constants.LOGX) {
60                     Log.i(Constants.TAG, "Broadcast: Network Down");
61                 }
62             }
63         } else if (intent.getAction().equals(Constants.ACTION_RETRY)) {
64             if (Constants.LOGVV) {
65                 Log.v(Constants.TAG, "Receiver retry");
66             }
67             if (Constants.LOGX) {
68                 Log.i(Constants.TAG, "Broadcast: Timed Retry");
69             }
70             context.startService(new Intent(context, DownloadService.class));
71         } else if (intent.getAction().equals(Constants.ACTION_OPEN)
72                 || intent.getAction().equals(Constants.ACTION_LIST)) {
73             if (Constants.LOGVV) {
74                 if (intent.getAction().equals(Constants.ACTION_OPEN)) {
75                     Log.v(Constants.TAG, "Receiver open for " + intent.getData());
76                 } else {
77                     Log.v(Constants.TAG, "Receiver list for " + intent.getData());
78                 }
79             }
80             Cursor cursor = context.getContentResolver().query(
81                     intent.getData(), null, null, null, null);
82             if (cursor != null) {
83                 if (cursor.moveToFirst()) {
84                     int statusColumn = cursor.getColumnIndexOrThrow(Downloads.COLUMN_STATUS);
85                     int status = cursor.getInt(statusColumn);
86                     int visibilityColumn =
87                             cursor.getColumnIndexOrThrow(Downloads.COLUMN_VISIBILITY);
88                     int visibility = cursor.getInt(visibilityColumn);
89                     if (Downloads.isStatusCompleted(status)
90                             && visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
91                         ContentValues values = new ContentValues();
92                         values.put(Downloads.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE);
93                         context.getContentResolver().update(intent.getData(), values, null, null);
94                     }
95
96                     if (intent.getAction().equals(Constants.ACTION_OPEN)) {
97                         int filenameColumn = cursor.getColumnIndexOrThrow(Downloads._DATA);
98                         int mimetypeColumn =
99                                 cursor.getColumnIndexOrThrow(Downloads.COLUMN_MIME_TYPE);
100                         String filename = cursor.getString(filenameColumn);
101                         String mimetype = cursor.getString(mimetypeColumn);
102                         Uri path = Uri.parse(filename);
103                         // If there is no scheme, then it must be a file
104                         if (path.getScheme() == null) {
105                             path = Uri.fromFile(new File(filename));
106                         }
107                         Intent activityIntent = new Intent(Intent.ACTION_VIEW);
108                         activityIntent.setDataAndType(path, mimetype);
109                         activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
110                         try {
111                             context.startActivity(activityIntent);
112                         } catch (ActivityNotFoundException ex) {
113                             if (Config.LOGD) {
114                                 Log.d(Constants.TAG, "no activity for " + mimetype, ex);
115                             }
116                             // nothing anyone can do about this, but we're in a clean state,
117                             //     swallow the exception entirely
118                         }
119                     } else {
120                         int packageColumn =
121                                 cursor.getColumnIndexOrThrow(Downloads.COLUMN_NOTIFICATION_PACKAGE);
122                         int classColumn =
123                                 cursor.getColumnIndexOrThrow(Downloads.COLUMN_NOTIFICATION_CLASS);
124                         String pckg = cursor.getString(packageColumn);
125                         String clazz = cursor.getString(classColumn);
126                         if (pckg != null && clazz != null) {
127                             Intent appIntent = new Intent(Downloads.ACTION_NOTIFICATION_CLICKED);
128                             appIntent.setClassName(pckg, clazz);
129                             if (intent.getBooleanExtra("multiple", true)) {
130                                 appIntent.setData(Downloads.CONTENT_URI);
131                             } else {
132                                 appIntent.setData(intent.getData());
133                             }
134                             context.sendBroadcast(appIntent);
135                         }
136                     }
137                 }
138                 cursor.close();
139             }
140             NotificationManager notMgr = (NotificationManager) context
141                     .getSystemService(Context.NOTIFICATION_SERVICE);
142             if (notMgr != null) {
143                 notMgr.cancel((int) ContentUris.parseId(intent.getData()));
144             }
145         } else if (intent.getAction().equals(Constants.ACTION_HIDE)) {
146             if (Constants.LOGVV) {
147                 Log.v(Constants.TAG, "Receiver hide for " + intent.getData());
148             }
149             Cursor cursor = context.getContentResolver().query(
150                     intent.getData(), null, null, null, null);
151             if (cursor != null) {
152                 if (cursor.moveToFirst()) {
153                     int statusColumn = cursor.getColumnIndexOrThrow(Downloads.COLUMN_STATUS);
154                     int status = cursor.getInt(statusColumn);
155                     int visibilityColumn =
156                             cursor.getColumnIndexOrThrow(Downloads.COLUMN_VISIBILITY);
157                     int visibility = cursor.getInt(visibilityColumn);
158                     if (Downloads.isStatusCompleted(status)
159                             && visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
160                         ContentValues values = new ContentValues();
161                         values.put(Downloads.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE);
162                         context.getContentResolver().update(intent.getData(), values, null, null);
163                     }
164                 }
165                 cursor.close();
166             }
167         }
168     }
169 }