DownloadReceiver.java revision 59910f4a9ce953ea74c8db759448f227c96796b3
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 17package com.android.providers.downloads; 18 19import android.app.NotificationManager; 20import android.content.ActivityNotFoundException; 21import android.content.BroadcastReceiver; 22import android.content.ContentUris; 23import android.content.ContentValues; 24import android.content.Context; 25import android.content.Intent; 26import android.database.Cursor; 27import android.net.ConnectivityManager; 28import android.net.NetworkInfo; 29import android.net.Uri; 30import android.provider.Downloads; 31import android.util.Config; 32import android.util.Log; 33 34import java.io.File; 35 36/** 37 * Receives system broadcasts (boot, network connectivity) 38 */ 39public 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 context.startService(new Intent(context, DownloadService.class)); 55 } 56 } else if (intent.getAction().equals(Constants.ACTION_RETRY)) { 57 if (Constants.LOGVV) { 58 Log.v(Constants.TAG, "Receiver retry"); 59 } 60 context.startService(new Intent(context, DownloadService.class)); 61 } else if (intent.getAction().equals(Constants.ACTION_OPEN) 62 || intent.getAction().equals(Constants.ACTION_LIST)) { 63 if (Constants.LOGVV) { 64 if (intent.getAction().equals(Constants.ACTION_OPEN)) { 65 Log.v(Constants.TAG, "Receiver open for " + intent.getData()); 66 } else { 67 Log.v(Constants.TAG, "Receiver list for " + intent.getData()); 68 } 69 } 70 Cursor cursor = context.getContentResolver().query( 71 intent.getData(), null, null, null, null); 72 if (cursor != null) { 73 if (cursor.moveToFirst()) { 74 int statusColumn = cursor.getColumnIndexOrThrow(Downloads.COLUMN_STATUS); 75 int status = cursor.getInt(statusColumn); 76 int visibilityColumn = 77 cursor.getColumnIndexOrThrow(Downloads.COLUMN_VISIBILITY); 78 int visibility = cursor.getInt(visibilityColumn); 79 if (Downloads.isStatusCompleted(status) 80 && visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) { 81 ContentValues values = new ContentValues(); 82 values.put(Downloads.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE); 83 context.getContentResolver().update(intent.getData(), values, null, null); 84 } 85 86 if (intent.getAction().equals(Constants.ACTION_OPEN)) { 87 int filenameColumn = cursor.getColumnIndexOrThrow(Downloads._DATA); 88 int mimetypeColumn = 89 cursor.getColumnIndexOrThrow(Downloads.COLUMN_MIME_TYPE); 90 String filename = cursor.getString(filenameColumn); 91 String mimetype = cursor.getString(mimetypeColumn); 92 Uri path = Uri.parse(filename); 93 // If there is no scheme, then it must be a file 94 if (path.getScheme() == null) { 95 path = Uri.fromFile(new File(filename)); 96 } 97 Intent activityIntent = new Intent(Intent.ACTION_VIEW); 98 activityIntent.setDataAndType(path, mimetype); 99 activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 100 try { 101 context.startActivity(activityIntent); 102 } catch (ActivityNotFoundException ex) { 103 if (Config.LOGD) { 104 Log.d(Constants.TAG, "no activity for " + mimetype, ex); 105 } 106 // nothing anyone can do about this, but we're in a clean state, 107 // swallow the exception entirely 108 } 109 } else { 110 int packageColumn = 111 cursor.getColumnIndexOrThrow(Downloads.COLUMN_NOTIFICATION_PACKAGE); 112 int classColumn = 113 cursor.getColumnIndexOrThrow(Downloads.COLUMN_NOTIFICATION_CLASS); 114 String pckg = cursor.getString(packageColumn); 115 String clazz = cursor.getString(classColumn); 116 if (pckg != null && clazz != null) { 117 Intent appIntent = new Intent(Downloads.ACTION_NOTIFICATION_CLICKED); 118 appIntent.setClassName(pckg, clazz); 119 if (intent.getBooleanExtra("multiple", true)) { 120 appIntent.setData(Downloads.CONTENT_URI); 121 } else { 122 appIntent.setData(intent.getData()); 123 } 124 context.sendBroadcast(appIntent); 125 } 126 } 127 } 128 cursor.close(); 129 } 130 NotificationManager notMgr = (NotificationManager) context 131 .getSystemService(Context.NOTIFICATION_SERVICE); 132 if (notMgr != null) { 133 notMgr.cancel((int) ContentUris.parseId(intent.getData())); 134 } 135 } else if (intent.getAction().equals(Constants.ACTION_HIDE)) { 136 if (Constants.LOGVV) { 137 Log.v(Constants.TAG, "Receiver hide for " + intent.getData()); 138 } 139 Cursor cursor = context.getContentResolver().query( 140 intent.getData(), null, null, null, null); 141 if (cursor != null) { 142 if (cursor.moveToFirst()) { 143 int statusColumn = cursor.getColumnIndexOrThrow(Downloads.COLUMN_STATUS); 144 int status = cursor.getInt(statusColumn); 145 int visibilityColumn = 146 cursor.getColumnIndexOrThrow(Downloads.COLUMN_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.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE); 152 context.getContentResolver().update(intent.getData(), values, null, null); 153 } 154 } 155 cursor.close(); 156 } 157 } 158 } 159} 160