2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.providers.downloads;
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;
37 * Receives system broadcasts (boot, network connectivity)
39 public class DownloadReceiver extends BroadcastReceiver {
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");
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");
51 NetworkInfo info = (NetworkInfo)
52 intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
53 if (info != null && info.isConnected()) {
55 Log.i(Constants.TAG, "Broadcast: Network Up");
57 context.startService(new Intent(context, DownloadService.class));
60 Log.i(Constants.TAG, "Broadcast: Network Down");
63 } else if (intent.getAction().equals(Constants.ACTION_RETRY)) {
64 if (Constants.LOGVV) {
65 Log.v(Constants.TAG, "Receiver retry");
68 Log.i(Constants.TAG, "Broadcast: Timed Retry");
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());
77 Log.v(Constants.TAG, "Receiver list for " + intent.getData());
80 Cursor cursor = context.getContentResolver().query(
81 intent.getData(), null, null, null, 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);
96 if (intent.getAction().equals(Constants.ACTION_OPEN)) {
97 int filenameColumn = cursor.getColumnIndexOrThrow(Downloads._DATA);
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));
107 Intent activityIntent = new Intent(Intent.ACTION_VIEW);
108 activityIntent.setDataAndType(path, mimetype);
109 activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
111 context.startActivity(activityIntent);
112 } catch (ActivityNotFoundException ex) {
114 Log.d(Constants.TAG, "no activity for " + mimetype, ex);
116 // nothing anyone can do about this, but we're in a clean state,
117 // swallow the exception entirely
121 cursor.getColumnIndexOrThrow(Downloads.COLUMN_NOTIFICATION_PACKAGE);
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);
132 appIntent.setData(intent.getData());
134 context.sendBroadcast(appIntent);
140 NotificationManager notMgr = (NotificationManager) context
141 .getSystemService(Context.NOTIFICATION_SERVICE);
142 if (notMgr != null) {
143 notMgr.cancel((int) ContentUris.parseId(intent.getData()));
145 } else if (intent.getAction().equals(Constants.ACTION_HIDE)) {
146 if (Constants.LOGVV) {
147 Log.v(Constants.TAG, "Receiver hide for " + intent.getData());
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);