blob: 19c2c14478f196f099f59fcd1af75776653df0ad [file] [log] [blame]
Daichi Hirono163e4b62016-01-18 18:05:17 +09001/*
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
17package com.android.mtp;
18
19import android.app.Activity;
Daichi Hirono163e4b62016-01-18 18:05:17 +090020import android.content.Intent;
Daichi Hirono1e374442016-02-11 10:08:21 -080021import android.hardware.usb.UsbDevice;
Daichi Hirono163e4b62016-01-18 18:05:17 +090022import android.hardware.usb.UsbManager;
Daichi Hirono1e374442016-02-11 10:08:21 -080023import android.net.Uri;
Daichi Hirono163e4b62016-01-18 18:05:17 +090024import android.os.Bundle;
Daichi Hirono1e374442016-02-11 10:08:21 -080025import android.provider.DocumentsContract;
26import android.util.Log;
27
28import java.io.IOException;
Daichi Hirono163e4b62016-01-18 18:05:17 +090029
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 */
36public 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 Hirono1e374442016-02-11 10:08:21 -080041 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 Tan5d3b37b2017-03-01 11:01:05 -080049 final Intent intent = new Intent(Intent.ACTION_VIEW);
50 intent.setDataAndType(uri, DocumentsContract.Root.MIME_TYPE_ITEM);
Daichi Hirono1e374442016-02-11 10:08:21 -080051 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 Hirono163e4b62016-01-18 18:05:17 +090056 }
57 finish();
58 }
59}