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