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