1abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood/*
2abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood * Copyright (C) 2010 The Android Open Source Project
3abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood *
4abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood * Licensed under the Apache License, Version 2.0 (the "License");
5abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood * you may not use this file except in compliance with the License.
6abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood * You may obtain a copy of the License at
7abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood *
8abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood *      http://www.apache.org/licenses/LICENSE-2.0
9abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood *
10abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood * Unless required by applicable law or agreed to in writing, software
11abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood * distributed under the License is distributed on an "AS IS" BASIS,
12abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood * See the License for the specific language governing permissions and
14abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood * limitations under the License.
15abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood */
16abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood
17abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwoodpackage com.android.providers.media;
18abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood
198efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkeyimport android.content.BroadcastReceiver;
20abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwoodimport android.content.Context;
21abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwoodimport android.content.Intent;
228efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkeyimport android.content.IntentFilter;
234ad1d2a8859c8b2177e683c22a3696a9ff83a0b6Mike Lockwoodimport android.hardware.usb.UsbManager;
24abf5daf6a8c2f9441af98b489398a476e7c6564cMike Lockwoodimport android.net.Uri;
25abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwoodimport android.os.Bundle;
261469c78b58e40d0400487e61078b1297899d8956Nick Kralevichimport android.util.Log;
27abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood
288efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkeypublic class MtpReceiver extends BroadcastReceiver {
291469c78b58e40d0400487e61078b1297899d8956Nick Kralevich    private static final String TAG = MtpReceiver.class.getSimpleName();
301469c78b58e40d0400487e61078b1297899d8956Nick Kralevich    private static final boolean DEBUG = false;
31abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood
32abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood    @Override
33abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood    public void onReceive(Context context, Intent intent) {
348efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey        final String action = intent.getAction();
358efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey        if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
368efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey            final Intent usbState = context.registerReceiver(
378efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey                    null, new IntentFilter(UsbManager.ACTION_USB_STATE));
388efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey            if (usbState != null) {
398efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey                handleUsbState(context, usbState);
408efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey            }
418efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey        } else if (UsbManager.ACTION_USB_STATE.equals(action)) {
428efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey            handleUsbState(context, intent);
438efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey        }
448efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey    }
458efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey
468efd65fe64c7978534bb549b2329068a2f8c5075Jeff Sharkey    private void handleUsbState(Context context, Intent intent) {
47abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood        Bundle extras = intent.getExtras();
484373275edca8a940073cf0f2fd57757a0fc0d2d8Mike Lockwood        boolean connected = extras.getBoolean(UsbManager.USB_CONFIGURED);
496fcef4f5098c35a94cf0f17b6dd2b99149d2296cMike Lockwood        boolean mtpEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_MTP);
506fcef4f5098c35a94cf0f17b6dd2b99149d2296cMike Lockwood        boolean ptpEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_PTP);
511469c78b58e40d0400487e61078b1297899d8956Nick Kralevich        boolean unlocked = extras.getBoolean(UsbManager.USB_DATA_UNLOCKED);
526fcef4f5098c35a94cf0f17b6dd2b99149d2296cMike Lockwood        // Start MTP service if USB is connected and either the MTP or PTP function is enabled
536fcef4f5098c35a94cf0f17b6dd2b99149d2296cMike Lockwood        if (connected && (mtpEnabled || ptpEnabled)) {
546fcef4f5098c35a94cf0f17b6dd2b99149d2296cMike Lockwood            intent = new Intent(context, MtpService.class);
551469c78b58e40d0400487e61078b1297899d8956Nick Kralevich            intent.putExtra(UsbManager.USB_DATA_UNLOCKED, unlocked);
566fcef4f5098c35a94cf0f17b6dd2b99149d2296cMike Lockwood            if (ptpEnabled) {
576fcef4f5098c35a94cf0f17b6dd2b99149d2296cMike Lockwood                intent.putExtra(UsbManager.USB_FUNCTION_PTP, true);
586fcef4f5098c35a94cf0f17b6dd2b99149d2296cMike Lockwood            }
591469c78b58e40d0400487e61078b1297899d8956Nick Kralevich            if (DEBUG) { Log.d(TAG, "handleUsbState startService"); }
606fcef4f5098c35a94cf0f17b6dd2b99149d2296cMike Lockwood            context.startService(intent);
61819cafdb3d4c3ce8a74d3b572b8ca0a0b639e8b2Mike Lockwood            // tell MediaProvider MTP is connected so it can bind to the service
62819cafdb3d4c3ce8a74d3b572b8ca0a0b639e8b2Mike Lockwood            context.getContentResolver().insert(Uri.parse(
63819cafdb3d4c3ce8a74d3b572b8ca0a0b639e8b2Mike Lockwood                    "content://media/none/mtp_connected"), null);
64819cafdb3d4c3ce8a74d3b572b8ca0a0b639e8b2Mike Lockwood        } else {
651469c78b58e40d0400487e61078b1297899d8956Nick Kralevich            boolean status = context.stopService(new Intent(context, MtpService.class));
661469c78b58e40d0400487e61078b1297899d8956Nick Kralevich            if (DEBUG) { Log.d(TAG, "handleUsbState stopService status=" + status); }
67819cafdb3d4c3ce8a74d3b572b8ca0a0b639e8b2Mike Lockwood            // tell MediaProvider MTP is disconnected so it can unbind from the service
68819cafdb3d4c3ce8a74d3b572b8ca0a0b639e8b2Mike Lockwood            context.getContentResolver().delete(Uri.parse(
69819cafdb3d4c3ce8a74d3b572b8ca0a0b639e8b2Mike Lockwood                    "content://media/none/mtp_connected"), null, null);
70819cafdb3d4c3ce8a74d3b572b8ca0a0b639e8b2Mike Lockwood        }
71819cafdb3d4c3ce8a74d3b572b8ca0a0b639e8b2Mike Lockwood    }
72abf8d09064cdc380d4996f5022d3a20eb4448ed1Mike Lockwood}
73