1 /* 2 * Copyright (C) 2017 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 package com.android.bluetooth.opp; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.content.pm.ProviderInfo; 23 import android.net.Uri; 24 import android.os.UserHandle; 25 import android.os.UserManager; 26 import android.support.v4.content.FileProvider; 27 import android.util.Log; 28 import java.io.File; 29 30 /** 31 * A FileProvider for files received by Bluetooth share 32 */ 33 public class BluetoothOppFileProvider extends FileProvider { 34 private static final String TAG = "BluetoothOppFileProvider"; 35 36 private Context mContext = null; 37 private ProviderInfo mProviderInfo = null; 38 private boolean mRegisteredReceiver = false; 39 private boolean mInitialized = false; 40 41 /** Broadcast receiver that attach FileProvider info when user unlocks the phone for the 42 * first time after reboot and the credential-encrypted storage is available. 43 */ 44 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 45 @Override 46 public void onReceive(final Context context, Intent intent) { 47 if (Intent.ACTION_USER_UNLOCKED.equals(intent.getAction())) { 48 attachInfo(mContext, mProviderInfo); 49 } 50 } 51 }; 52 53 /** 54 * After the FileProvider is instantiated, this method is called to provide the system with 55 * information about the provider. The actual initialization is delayed until user unlock the 56 * device 57 * 58 * @param context A {@link Context} for the current component. 59 * @param info A {@link ProviderInfo} for the new provider. 60 */ 61 @Override 62 public void attachInfo(Context context, ProviderInfo info) { 63 synchronized (this) { 64 mContext = context; 65 mProviderInfo = info; 66 if (!mRegisteredReceiver) { 67 IntentFilter userFilter = new IntentFilter(); 68 userFilter.addAction(Intent.ACTION_USER_UNLOCKED); 69 mContext.registerReceiverAsUser( 70 mBroadcastReceiver, UserHandle.CURRENT, userFilter, null, null); 71 mRegisteredReceiver = true; 72 } 73 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 74 if (userManager.isUserUnlocked()) { 75 if (!mInitialized) { 76 if (Constants.DEBUG) Log.d(TAG, "Initialized"); 77 super.attachInfo(mContext, mProviderInfo); 78 mInitialized = true; 79 } 80 if (mRegisteredReceiver) { 81 mContext.unregisterReceiver(mBroadcastReceiver); 82 mRegisteredReceiver = false; 83 } 84 } 85 } 86 } 87 88 /** 89 * Return a content URI for a given {@link File}. Specific temporary 90 * permissions for the content URI can be set with 91 * {@link Context#grantUriPermission(String, Uri, int)}, or added 92 * to an {@link Intent} by calling {@link Intent#setData(Uri) setData()} and then 93 * {@link Intent#setFlags(int) setFlags()}; in both cases, the applicable flags are 94 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} and 95 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION}. A FileProvider can only return a 96 * <code>content</code> {@link Uri} for file paths defined in their <code><paths></code> 97 * meta-data element. See the Class Overview for more information. 98 * 99 * @param context A {@link Context} for the current component. 100 * @param authority The authority of a {@link FileProvider} defined in a 101 * {@code <provider>} element in your app's manifest. 102 * @param file A {@link File} pointing to the filename for which you want a 103 * <code>content</code> {@link Uri}. 104 * @return A content URI for the file. Null if the user hasn't unlock the phone 105 * @throws IllegalArgumentException When the given {@link File} is outside 106 * the paths supported by the provider. 107 */ 108 public static Uri getUriForFile(Context context, String authority, File file) { 109 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 110 if (!userManager.isUserUnlocked()) { 111 return null; 112 } 113 context = context.createCredentialProtectedStorageContext(); 114 return FileProvider.getUriForFile(context, authority, file); 115 } 116 } 117