Daichi Hirono | 163e4b6 | 2016-01-18 18:05:17 +0900 | [diff] [blame] | 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; |
Daichi Hirono | 163e4b6 | 2016-01-18 18:05:17 +0900 | [diff] [blame] | 20 | import android.content.Intent; |
Daichi Hirono | 1e37444 | 2016-02-11 10:08:21 -0800 | [diff] [blame] | 21 | import android.hardware.usb.UsbDevice; |
Daichi Hirono | 163e4b6 | 2016-01-18 18:05:17 +0900 | [diff] [blame] | 22 | import android.hardware.usb.UsbManager; |
Daichi Hirono | 1e37444 | 2016-02-11 10:08:21 -0800 | [diff] [blame] | 23 | import android.net.Uri; |
Daichi Hirono | 163e4b6 | 2016-01-18 18:05:17 +0900 | [diff] [blame] | 24 | import android.os.Bundle; |
Daichi Hirono | 1e37444 | 2016-02-11 10:08:21 -0800 | [diff] [blame] | 25 | import android.provider.DocumentsContract; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | import java.io.IOException; |
Daichi Hirono | 163e4b6 | 2016-01-18 18:05:17 +0900 | [diff] [blame] | 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())) { |
Daichi Hirono | 1e37444 | 2016-02-11 10:08:21 -0800 | [diff] [blame] | 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 | |
Garfield Tan | 5d3b37b | 2017-03-01 11:01:05 -0800 | [diff] [blame] | 49 | final Intent intent = new Intent(Intent.ACTION_VIEW); |
| 50 | intent.setDataAndType(uri, DocumentsContract.Root.MIME_TYPE_ITEM); |
Daichi Hirono | 1e37444 | 2016-02-11 10:08:21 -0800 | [diff] [blame] | 51 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 52 | this.startActivity(intent); |
| 53 | } catch (IOException exception) { |
| 54 | Log.e(MtpDocumentsProvider.TAG, "Failed to open device", exception); |
| 55 | } |
Daichi Hirono | 163e4b6 | 2016-01-18 18:05:17 +0900 | [diff] [blame] | 56 | } |
| 57 | finish(); |
| 58 | } |
| 59 | } |