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

Commit dafc4668 authored by Diksha Gohlyan's avatar Diksha Gohlyan
Browse files

Forward_result flag to getcontent from another app

Test: atest DocumentsUIGoogleTests

Bug: 144286721

Change-Id: I963f4a931c3c550da793eab1c46d767da6ddff66
parent ef2aba6b
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -91,14 +91,6 @@
            android:theme="@style/DocumentsTheme">
        </activity>

        <activity
            android:name=".ForResultForwarderActivity"
            android:theme="@android:style/Theme.NoDisplay"
            android:excludeFromRecents="true"
            android:finishOnCloseSystemDialogs="true"
            android:exported="false">
        </activity>

        <!--  Preserve original launcher activity from Nougat. -->
        <activity-alias
            android:name=".LauncherActivity"
+0 −2
Original line number Diff line number Diff line
@@ -87,9 +87,7 @@ public abstract class AbstractActionHandler<T extends FragmentActivity & CommonA
        implements ActionHandler {

    @VisibleForTesting
    public static final int CODE_FORWARD = 42;
    public static final int CODE_AUTHENTICATION = 43;
    public static final int CODE_FORWARD_CROSS_PROFILE = 44;

    @VisibleForTesting
    static final int LOADER_ID = 42;
+0 −69
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.documentsui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.android.documentsui.base.UserId;

/**
 * Forwards a cross-profile startActivityForResult intent.
 */
public class ForResultForwarderActivity extends Activity {

    private static final String TAG = "ForResultForwarderActiv";
    private static final String EXTRA_INTENT = "EXTRA_INTENT";
    private static final String EXTRA_USER = "EXTRA_USER";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Intent intent = getIntent();

        if (!intent.hasExtra(EXTRA_INTENT) || !intent.hasExtra(EXTRA_USER)) {
            Log.e(TAG, "Missing intent or user");
            setResult(Activity.RESULT_CANCELED);
            finish();
            return;
        }

        Intent targetIntent = intent.getParcelableExtra(EXTRA_INTENT);
        // We should never have the default value because of the above check
        UserId targetUserId = UserId.of(intent.getIntExtra(EXTRA_USER, /* defaultValue= */ -1));

        targetIntent.addFlags(
                Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
        try {
            targetUserId.startActivityAsUser(this, targetIntent);
        } catch (RuntimeException e) {
            Log.e(TAG, "Failed to forward activity");
            setResult(Activity.RESULT_CANCELED);
        }
        finish();
    }

    public static Intent getIntent(Context context, Intent intent, UserId targetUserId) {
        Intent forwarder = new Intent(context, ForResultForwarderActivity.class);
        forwarder.putExtra(EXTRA_INTENT, intent);
        forwarder.putExtra(EXTRA_USER, targetUserId.getIdentifier());
        return forwarder;
    }
}
+10 −48
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ import androidx.recyclerview.selection.ItemDetailsLookup.ItemDetails;
import com.android.documentsui.AbstractActionHandler;
import com.android.documentsui.ActivityConfig;
import com.android.documentsui.DocumentsAccess;
import com.android.documentsui.ForResultForwarderActivity;
import com.android.documentsui.Injector;
import com.android.documentsui.MetricConsts;
import com.android.documentsui.Metrics;
@@ -242,46 +241,6 @@ class ActionHandler<T extends FragmentActivity & Addons> extends AbstractActionH
        userId.startActivityAsUser(mActivity, intent);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (DEBUG) {
            Log.d(TAG, "onActivityResult() code=" + resultCode);
        }

        // Only relay back results when not canceled; otherwise stick around to
        // let the user pick another app/backend.
        switch (requestCode) {
            case CODE_FORWARD:
            case CODE_FORWARD_CROSS_PROFILE:
                onExternalAppResult(requestCode, resultCode, data);
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void onExternalAppResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != FragmentActivity.RESULT_CANCELED) {
            if (requestCode == CODE_FORWARD_CROSS_PROFILE) {
                UserId otherUser = UserId.CURRENT_USER.equals(mUserIdManager.getSystemUser())
                        ? mUserIdManager.getManagedUser()
                        : mUserIdManager.getSystemUser();
                if (!mState.canInteractWith(otherUser)) {
                    mDialogs.showActionNotAllowed();
                    return;
                }
            }
            // Remember that we last picked via external app
            mLastAccessed.setLastAccessedToExternalApp(mActivity);

            updatePickResult(data, false, MetricConsts.ROOT_THIRD_PARTY_APP);

            // Pass back result to original caller
            mActivity.setResult(resultCode, data, 0);
            mActivity.finish();
        }
    }

    @Override
    public void openInNewWindow(DocumentStack path) {
        // Open new window support only depends on vanilla Activity, so it is
@@ -311,27 +270,30 @@ class ActionHandler<T extends FragmentActivity & Addons> extends AbstractActionH
        }

        Intent intent = new Intent(mActivity.getIntent());
        final int flagsRemoved = Intent.FLAG_ACTIVITY_FORWARD_RESULT
                | Intent.FLAG_GRANT_READ_URI_PERMISSION
        final int flagsRemoved = Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION;
        intent.setFlags(intent.getFlags() & ~flagsRemoved);
        intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
        intent.setComponent(new ComponentName(
                info.activityInfo.applicationInfo.packageName, info.activityInfo.name));
        if (!UserId.CURRENT_USER.equals(userId)) {
            intent = ForResultForwarderActivity.getIntent(mActivity, intent, userId);
        }
        try {
            boolean isCurrentUser = UserId.CURRENT_USER.equals(userId);
            mActivity.startActivityForResult(intent,
                    isCurrentUser ? CODE_FORWARD : CODE_FORWARD_CROSS_PROFILE);
            if (isCurrentUser) {
                mActivity.startActivity(intent);
            } else {
                userId.startActivityAsUser(mActivity, intent);
            }
            mActivity.finish();
        } catch (SecurityException | ActivityNotFoundException e) {
            Log.e(TAG, "Caught error: " + e.getLocalizedMessage());
            mInjector.dialogs.showNoApplicationFound();
        }
    }


    @Override
    public void springOpenDirectory(DocumentInfo doc) {
    }
+0 −7
Original line number Diff line number Diff line
@@ -37,13 +37,6 @@
            </intent-filter>
        </activity>

        <activity android:name="com.android.documentsui.PickActivityTest$ReturnResultActivity">
            <intent-filter>
                <action android:name="com.android.documentsui.test.action.RETURN_RESULT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


        <provider
            android:name="com.android.documentsui.StubProvider"
Loading