diff options
Diffstat (limited to 'java')
| -rw-r--r-- | java/res/values/strings.xml | 22 | ||||
| -rw-r--r-- | java/src/com/android/intentresolver/ChooserActivity.java | 35 | ||||
| -rw-r--r-- | java/src/com/android/intentresolver/ChooserHelper.java | 59 | ||||
| -rw-r--r-- | java/tests/Android.bp | 33 | ||||
| -rw-r--r-- | java/tests/AndroidManifest.xml | 33 | ||||
| -rw-r--r-- | java/tests/AndroidTest.xml | 28 | ||||
| -rw-r--r-- | java/tests/src/com/android/intentresolver/ChooserHelperTest.java | 58 |
7 files changed, 268 insertions, 0 deletions
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml new file mode 100644 index 00000000..2e570d25 --- /dev/null +++ b/java/res/values/strings.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2021 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. +--> + +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> + + <!-- Title of the IntentResolver application. [CHAR LIMIT=50] --> + <string name="app_label">IntentResolver</string> + +</resources> diff --git a/java/src/com/android/intentresolver/ChooserActivity.java b/java/src/com/android/intentresolver/ChooserActivity.java new file mode 100644 index 00000000..3bd05b8e --- /dev/null +++ b/java/src/com/android/intentresolver/ChooserActivity.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2021 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.intentresolver; + +import android.app.Activity; +import android.os.Bundle; + +/** + * Activity for selecting which application ought to handle an ACTION_SEND intent. + */ +public final class ChooserActivity extends Activity { + + private static final String TAG = "ChooserActivity"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + ChooserHelper.onChoose(this); + finish(); + } +} diff --git a/java/src/com/android/intentresolver/ChooserHelper.java b/java/src/com/android/intentresolver/ChooserHelper.java new file mode 100644 index 00000000..5e3be849 --- /dev/null +++ b/java/src/com/android/intentresolver/ChooserHelper.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2021 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.intentresolver; + +import android.app.Activity; +import android.app.ActivityTaskManager; +import android.content.Intent; +import android.os.Bundle; +import android.os.IBinder; +import android.os.StrictMode; + +import androidx.annotation.VisibleForTesting; + +/** + * When a target is chosen from the SystemUI Chooser activity, unpack its arguments and + * startActivityAsCaller to handle the now-chosen intent. + */ +public class ChooserHelper { + + private static final String TAG = "ChooserHelper"; + + /** Dispatch the selected target indicated in the intent that started the provided Activity. */ + @VisibleForTesting + public static void onChoose(Activity activity) { + final Intent thisIntent = activity.getIntent(); + final Bundle thisExtras = thisIntent.getExtras(); + final Intent chosenIntent = thisIntent.getParcelableExtra(Intent.EXTRA_INTENT); + final Bundle options = thisIntent.getParcelableExtra(ActivityTaskManager.EXTRA_OPTIONS); + final IBinder permissionToken = + thisExtras.getBinder(ActivityTaskManager.EXTRA_PERMISSION_TOKEN); + final boolean ignoreTargetSecurity = + thisIntent.getBooleanExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY, false); + final int userId = thisIntent.getIntExtra(Intent.EXTRA_USER_ID, -1); + + // We're dispatching intents that might be coming from legacy apps, so + // (as in com.android.internal.app.ResolverActivity) exempt ourselves from death. + StrictMode.disableDeathOnFileUriExposure(); + try { + activity.startActivityAsCaller( + chosenIntent, options, permissionToken, ignoreTargetSecurity, userId); + } finally { + StrictMode.enableDeathOnFileUriExposure(); + } + } +} diff --git a/java/tests/Android.bp b/java/tests/Android.bp new file mode 100644 index 00000000..fc78a604 --- /dev/null +++ b/java/tests/Android.bp @@ -0,0 +1,33 @@ +package { + // See: http://go/android-license-faq + default_applicable_licenses: ["packages_modules_IntentResolver_license"], +} + +android_test { + name: "IntentResolverUnitTests", + + // Include all test java files. + srcs: ["src/**/*.java"], + + libs: [ + "android.test.runner", + "android.test.base", + "android.test.mock", + ], + + static_libs: [ + "IntentResolver-core", + "androidx.test.rules", + "mockito-target-minus-junit4", + "androidx.test.espresso.core", + "truth-prebuilt", + "testables", + "testng", + ], + jni_libs: [ + ], + test_suites: ["general-tests"], + min_sdk_version: "30", + platform_apis: true, + compile_multilib: "both", +} diff --git a/java/tests/AndroidManifest.xml b/java/tests/AndroidManifest.xml new file mode 100644 index 00000000..faf55ecf --- /dev/null +++ b/java/tests/AndroidManifest.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2021 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. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.intentresolver.tests"> + + <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" /> + + <uses-permission android:name="android.permission.START_ACTIVITY_AS_CALLER"/> + + <application> + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation android:name="android.testing.TestableInstrumentation" + android:targetPackage="com.android.intentresolver.tests" + android:label="Tests for IntentResolver"> + </instrumentation> + +</manifest> diff --git a/java/tests/AndroidTest.xml b/java/tests/AndroidTest.xml new file mode 100644 index 00000000..d1d77c10 --- /dev/null +++ b/java/tests/AndroidTest.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2021 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. +--> +<configuration description="Run IntentResolver Tests."> + <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup"> + <option name="test-file-name" value="IntentResolverUnitTests.apk" /> + </target_preparer> + + <option name="test-suite-tag" value="apct" /> + <option name="test-tag" value="IntentResolverUnitTests" /> + <test class="com.android.tradefed.testtype.AndroidJUnitTest" > + <option name="package" value="com.android.intentresolver.tests" /> + <option name="runner" value="android.testing.TestableInstrumentation" /> + <option name="hidden-api-checks" value="false"/> + </test> +</configuration> diff --git a/java/tests/src/com/android/intentresolver/ChooserHelperTest.java b/java/tests/src/com/android/intentresolver/ChooserHelperTest.java new file mode 100644 index 00000000..d791cd4b --- /dev/null +++ b/java/tests/src/com/android/intentresolver/ChooserHelperTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2021 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.intentresolver.tests; + +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyBoolean; +import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.app.Activity; +import android.app.ActivityTaskManager; +import android.content.Intent; +import android.os.Binder; +import android.test.suitebuilder.annotation.SmallTest; + +import androidx.test.runner.AndroidJUnit4; + +import com.android.intentresolver.ChooserHelper; + +import org.junit.Test; +import org.junit.runner.RunWith; + +@SmallTest +@RunWith(AndroidJUnit4.class) +public class ChooserHelperTest { + + @Test + public void testOnChoose_CallsStartActivityAsCallerWithToken() { + final Intent intent = new Intent(); + final Binder token = new Binder(); + intent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, token); + + final Activity mockActivity = mock(Activity.class); + when(mockActivity.getIntent()).thenReturn(intent); + + ChooserHelper.onChoose(mockActivity); + verify(mockActivity, times(1)).startActivityAsCaller( + any(), any(), eq(token), anyBoolean(), anyInt()); + } +} |