1 /* 2 * Copyright (C) 2016 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.mtp; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.hardware.usb.UsbDevice; 22 import android.hardware.usb.UsbManager; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.provider.DocumentsContract; 26 import android.util.Log; 27 28 import java.io.IOException; 29 30 /** 31 * Invisible activity to receive intents. 32 * To show Files app for the UsbManager.ACTION_USB_DEVICE_ATTACHED intent, the intent should be 33 * received by activity. The activity has NoDisplay theme and immediately terminate after routing 34 * intent to DocumentsUI. 35 */ 36 public class ReceiverActivity extends Activity { 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(getIntent().getAction())) { 41 final UsbDevice device = getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE); 42 try { 43 final MtpDocumentsProvider provider = MtpDocumentsProvider.getInstance(); 44 provider.openDevice(device.getDeviceId()); 45 final String deviceRootId = provider.getDeviceDocumentId(device.getDeviceId()); 46 final Uri uri = DocumentsContract.buildRootUri( 47 MtpDocumentsProvider.AUTHORITY, deviceRootId); 48 49 final Intent intent = new Intent(DocumentsContract.ACTION_BROWSE); 50 intent.setData(uri); 51 intent.addCategory(Intent.CATEGORY_DEFAULT); 52 intent.putExtra(DocumentsContract.EXTRA_FANCY_FEATURES, true); 53 this.startActivity(intent); 54 } catch (IOException exception) { 55 Log.e(MtpDocumentsProvider.TAG, "Failed to open device", exception); 56 } 57 } 58 finish(); 59 } 60 } 61