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