Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6690bb0f authored by Jack He's avatar Jack He Committed by android-build-merger
Browse files

Merge "OPP: Initialize OPP FileProvider after user unlock" am: 02944530 am:...

Merge "OPP: Initialize OPP FileProvider after user unlock" am: 02944530 am: 3fa35788 am: b68beb1f
am: d4569dfe

Change-Id: I2a2a049fbeee85aed68491dba966761363c073ac
parents cf97ab18 d4569dfe
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -87,8 +87,8 @@
                    android:pathPrefix="/btopp"
                    android:pathPrefix="/btopp"
                    android:permission="android.permission.ACCESS_BLUETOOTH_SHARE" />
                    android:permission="android.permission.ACCESS_BLUETOOTH_SHARE" />
        </provider>
        </provider>
        <provider android:name="android.support.v4.content.FileProvider"
        <provider android:name=".opp.BluetoothOppFileProvider"
            android:authorities="com.google.android.bluetooth.fileprovider"
            android:authorities="com.android.bluetooth.opp.fileprovider"
            android:grantUriPermissions="true"
            android:grantUriPermissions="true"
            android:exported="false">
            android:exported="false">
            <meta-data
            <meta-data
+116 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.bluetooth.opp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ProviderInfo;
import android.net.Uri;
import android.os.UserHandle;
import android.os.UserManager;
import android.support.v4.content.FileProvider;
import android.util.Log;
import java.io.File;

/**
 * A FileProvider for files received by Bluetooth share
 */
public class BluetoothOppFileProvider extends FileProvider {
    private static final String TAG = "BluetoothOppFileProvider";

    private Context mContext = null;
    private ProviderInfo mProviderInfo = null;
    private boolean mRegisteredReceiver = false;
    private boolean mInitialized = false;

    /** Broadcast receiver that attach FileProvider info when user unlocks the phone for the
     *  first time after reboot and the credential-encrypted storage is available.
     */
    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, Intent intent) {
            if (Intent.ACTION_USER_UNLOCKED.equals(intent.getAction())) {
                attachInfo(mContext, mProviderInfo);
            }
        }
    };

    /**
     * After the FileProvider is instantiated, this method is called to provide the system with
     * information about the provider. The actual initialization is delayed until user unlock the
     * device
     *
     * @param context A {@link Context} for the current component.
     * @param info A {@link ProviderInfo} for the new provider.
     */
    @Override
    public void attachInfo(Context context, ProviderInfo info) {
        synchronized (this) {
            mContext = context;
            mProviderInfo = info;
            if (!mRegisteredReceiver) {
                IntentFilter userFilter = new IntentFilter();
                userFilter.addAction(Intent.ACTION_USER_UNLOCKED);
                mContext.registerReceiverAsUser(
                        mBroadcastReceiver, UserHandle.CURRENT, userFilter, null, null);
                mRegisteredReceiver = true;
            }
            UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
            if (userManager.isUserUnlocked()) {
                if (!mInitialized) {
                    if (Constants.DEBUG) Log.d(TAG, "Initialized");
                    super.attachInfo(mContext, mProviderInfo);
                    mInitialized = true;
                }
                if (mRegisteredReceiver) {
                    mContext.unregisterReceiver(mBroadcastReceiver);
                    mRegisteredReceiver = false;
                }
            }
        }
    }

    /**
     * Return a content URI for a given {@link File}. Specific temporary
     * permissions for the content URI can be set with
     * {@link Context#grantUriPermission(String, Uri, int)}, or added
     * to an {@link Intent} by calling {@link Intent#setData(Uri) setData()} and then
     * {@link Intent#setFlags(int) setFlags()}; in both cases, the applicable flags are
     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} and
     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION}. A FileProvider can only return a
     * <code>content</code> {@link Uri} for file paths defined in their <code>&lt;paths&gt;</code>
     * meta-data element. See the Class Overview for more information.
     *
     * @param context A {@link Context} for the current component.
     * @param authority The authority of a {@link FileProvider} defined in a
     *            {@code <provider>} element in your app's manifest.
     * @param file A {@link File} pointing to the filename for which you want a
     * <code>content</code> {@link Uri}.
     * @return A content URI for the file. Null if the user hasn't unlock the phone
     * @throws IllegalArgumentException When the given {@link File} is outside
     * the paths supported by the provider.
     */
    public static Uri getUriForFile(Context context, String authority, File file) {
        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        if (!userManager.isUserUnlocked()) {
            return null;
        }
        context = context.createCredentialProtectedStorageContext();
        return FileProvider.getUriForFile(context, authority, file);
    }
}
+6 −3
Original line number Original line Diff line number Diff line
@@ -55,7 +55,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentHashMap;


import android.support.v4.content.FileProvider;
/**
/**
 * This class has some utilities for Opp application;
 * This class has some utilities for Opp application;
 */
 */
@@ -201,8 +200,12 @@ public class BluetoothOppUtility {
            return;
            return;
        }
        }


        Uri path = FileProvider.getUriForFile(context,
        Uri path = BluetoothOppFileProvider.getUriForFile(
                       "com.google.android.bluetooth.fileprovider", f);
                context, "com.android.bluetooth.opp.fileprovider", f);
        if (path == null) {
            Log.w(TAG, "Cannot get content URI for the shared file");
            return;
        }
        // If there is no scheme, then it must be a file
        // If there is no scheme, then it must be a file
        if (path.getScheme() == null) {
        if (path.getScheme() == null) {
            path = Uri.fromFile(new File(fileName));
            path = Uri.fromFile(new File(fileName));