1/* 2 * Copyright (C) 2010 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.media; 18 19import android.content.Context; 20import android.content.ContentValues; 21import android.content.Intent; 22import android.content.BroadcastReceiver; 23import android.hardware.usb.UsbManager; 24import android.net.Uri; 25import android.os.Bundle; 26import android.util.Log; 27 28 29public class UsbReceiver extends BroadcastReceiver 30{ 31 private final static String TAG = "UsbReceiver"; 32 33 @Override 34 public void onReceive(Context context, Intent intent) { 35 Bundle extras = intent.getExtras(); 36 boolean connected = extras.getBoolean(UsbManager.USB_CONFIGURED); 37 boolean mtpEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_MTP); 38 boolean ptpEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_PTP); 39 // Start MTP service if USB is connected and either the MTP or PTP function is enabled 40 if (connected && (mtpEnabled || ptpEnabled)) { 41 intent = new Intent(context, MtpService.class); 42 if (ptpEnabled) { 43 intent.putExtra(UsbManager.USB_FUNCTION_PTP, true); 44 } 45 context.startService(intent); 46 // tell MediaProvider MTP is connected so it can bind to the service 47 context.getContentResolver().insert(Uri.parse( 48 "content://media/none/mtp_connected"), null); 49 } else { 50 context.stopService(new Intent(context, MtpService.class)); 51 // tell MediaProvider MTP is disconnected so it can unbind from the service 52 context.getContentResolver().delete(Uri.parse( 53 "content://media/none/mtp_connected"), null, null); 54 } 55 } 56} 57 58 59