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.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.hardware.usb.UsbManager;
24import android.net.Uri;
25import android.os.Bundle;
26import android.util.Log;
27
28public class MtpReceiver extends BroadcastReceiver {
29    private static final String TAG = MtpReceiver.class.getSimpleName();
30    private static final boolean DEBUG = false;
31
32    @Override
33    public void onReceive(Context context, Intent intent) {
34        final String action = intent.getAction();
35        if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
36            final Intent usbState = context.registerReceiver(
37                    null, new IntentFilter(UsbManager.ACTION_USB_STATE));
38            if (usbState != null) {
39                handleUsbState(context, usbState);
40            }
41        } else if (UsbManager.ACTION_USB_STATE.equals(action)) {
42            handleUsbState(context, intent);
43        }
44    }
45
46    private void handleUsbState(Context context, Intent intent) {
47        Bundle extras = intent.getExtras();
48        boolean connected = extras.getBoolean(UsbManager.USB_CONFIGURED);
49        boolean mtpEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_MTP);
50        boolean ptpEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_PTP);
51        boolean unlocked = extras.getBoolean(UsbManager.USB_DATA_UNLOCKED);
52        // Start MTP service if USB is connected and either the MTP or PTP function is enabled
53        if (connected && (mtpEnabled || ptpEnabled)) {
54            intent = new Intent(context, MtpService.class);
55            intent.putExtra(UsbManager.USB_DATA_UNLOCKED, unlocked);
56            if (ptpEnabled) {
57                intent.putExtra(UsbManager.USB_FUNCTION_PTP, true);
58            }
59            if (DEBUG) { Log.d(TAG, "handleUsbState startService"); }
60            context.startService(intent);
61            // tell MediaProvider MTP is connected so it can bind to the service
62            context.getContentResolver().insert(Uri.parse(
63                    "content://media/none/mtp_connected"), null);
64        } else {
65            boolean status = context.stopService(new Intent(context, MtpService.class));
66            if (DEBUG) { Log.d(TAG, "handleUsbState stopService status=" + status); }
67            // tell MediaProvider MTP is disconnected so it can unbind from the service
68            context.getContentResolver().delete(Uri.parse(
69                    "content://media/none/mtp_connected"), null, null);
70        }
71    }
72}
73