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 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 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.registerReceiverAsUser(mBroadcastReceiver, UserHandle.CURRENT, userFilter, 71 null, null); 72 mRegisteredReceiver = true; 73 } 74 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 75 if (userManager.isUserUnlocked()) { 76 if (!mInitialized) { 77 if (Constants.DEBUG) { 78 Log.d(TAG, "Initialized"); 79 } 80 super.attachInfo(mContext, mProviderInfo); 81 mInitialized = true; 82 } 83 if (mRegisteredReceiver) { 84 mContext.unregisterReceiver(mBroadcastReceiver); 85 mRegisteredReceiver = false; 86 } 87 } 88 } 89 } 90 91 /** 92 * Return a content URI for a given {@link File}. Specific temporary 93 * permissions for the content URI can be set with 94 * {@link Context#grantUriPermission(String, Uri, int)}, or added 95 * to an {@link Intent} by calling {@link Intent#setData(Uri) setData()} and then 96 * {@link Intent#setFlags(int) setFlags()}; in both cases, the applicable flags are 97 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} and 98 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION}. A FileProvider can only return a 99 * <code>content</code> {@link Uri} for file paths defined in their <code><paths></code> 100 * meta-data element. See the Class Overview for more information. 101 * 102 * @param context A {@link Context} for the current component. 103 * @param authority The authority of a {@link FileProvider} defined in a 104 * {@code <provider>} element in your app's manifest. 105 * @param file A {@link File} pointing to the filename for which you want a 106 * <code>content</code> {@link Uri}. 107 * @return A content URI for the file. Null if the user hasn't unlock the phone 108 * @throws IllegalArgumentException When the given {@link File} is outside 109 * the paths supported by the provider. 110 */ 111 public static Uri getUriForFile(Context context, String authority, File file) { 112 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 113 if (!userManager.isUserUnlocked()) { 114 return null; 115 } 116 context = context.createCredentialProtectedStorageContext(); 117 return FileProvider.getUriForFile(context, authority, file); 118 } 119 } 120