1 /* 2 * Copyright (C) 2019 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 17 package com.android.cts.usespermissiondiffcertapp; 18 19 import static com.android.cts.permissiondeclareapp.UtilsProvider.ACTION_CLEAR_PRIMARY_CLIP; 20 import static com.android.cts.permissiondeclareapp.UtilsProvider.ACTION_GRANT_URI; 21 import static com.android.cts.permissiondeclareapp.UtilsProvider.ACTION_REVOKE_URI; 22 import static com.android.cts.permissiondeclareapp.UtilsProvider.ACTION_SET_PRIMARY_CLIP; 23 import static com.android.cts.permissiondeclareapp.UtilsProvider.ACTION_START_ACTIVITIES; 24 import static com.android.cts.permissiondeclareapp.UtilsProvider.ACTION_START_ACTIVITY; 25 import static com.android.cts.permissiondeclareapp.UtilsProvider.ACTION_START_SERVICE; 26 import static com.android.cts.permissiondeclareapp.UtilsProvider.ACTION_VERIFY_OUTGOING_PERSISTED; 27 import static com.android.cts.permissiondeclareapp.UtilsProvider.EXTRA_INTENT; 28 import static com.android.cts.permissiondeclareapp.UtilsProvider.EXTRA_MODE; 29 import static com.android.cts.permissiondeclareapp.UtilsProvider.EXTRA_PACKAGE_NAME; 30 import static com.android.cts.permissiondeclareapp.UtilsProvider.EXTRA_URI; 31 32 import android.content.ClipData; 33 import android.content.Context; 34 import android.content.Intent; 35 import android.net.Uri; 36 import android.os.Bundle; 37 38 import androidx.test.InstrumentationRegistry; 39 40 import com.android.cts.permissiondeclareapp.UtilsProvider; 41 42 import java.util.Objects; 43 44 public class Utils { getContext()45 private static Context getContext() { 46 return InstrumentationRegistry.getTargetContext(); 47 } 48 call(Intent intent)49 private static void call(Intent intent) { 50 final Bundle extras = new Bundle(); 51 extras.putParcelable(Intent.EXTRA_INTENT, intent); 52 getContext().getContentResolver().call(UtilsProvider.URI, "", "", extras); 53 } 54 grantClipUriPermissionViaActivity(ClipData clip, int mode)55 static void grantClipUriPermissionViaActivity(ClipData clip, int mode) { 56 grantClipUriPermission(clip, mode, ACTION_START_ACTIVITY); 57 } 58 grantClipUriPermissionViaActivities(ClipData clip, int mode)59 static void grantClipUriPermissionViaActivities(ClipData clip, int mode) { 60 grantClipUriPermission(clip, mode, ACTION_START_ACTIVITIES); 61 } 62 grantClipUriPermissionViaService(ClipData clip, int mode)63 static void grantClipUriPermissionViaService(ClipData clip, int mode) { 64 grantClipUriPermission(clip, mode, ACTION_START_SERVICE); 65 } 66 grantClipUriPermission(ClipData clip, int mode, String action)67 private static void grantClipUriPermission(ClipData clip, int mode, String action) { 68 Intent grantIntent = new Intent(); 69 if (clip.getItemCount() == 1) { 70 grantIntent.setData(clip.getItemAt(0).getUri()); 71 } else { 72 grantIntent.setClipData(clip); 73 // Make this Intent unique from the one that started it. 74 for (int i=0; i<clip.getItemCount(); i++) { 75 Uri uri = clip.getItemAt(i).getUri(); 76 if (uri != null) { 77 grantIntent.addCategory(uri.toString()); 78 } 79 } 80 } 81 grantIntent.addFlags(mode); 82 grantIntent.setClass(getContext(), 83 Objects.equals(ACTION_START_SERVICE, action) ? ReceiveUriService.class 84 : ReceiveUriActivity.class); 85 Intent intent = new Intent(); 86 intent.setAction(action); 87 intent.putExtra(EXTRA_INTENT, grantIntent); 88 call(intent); 89 } 90 grantClipUriPermissionViaContext(Uri uri, int mode)91 static void grantClipUriPermissionViaContext(Uri uri, int mode) { 92 Intent intent = new Intent(); 93 intent.setAction(ACTION_GRANT_URI); 94 intent.putExtra(EXTRA_PACKAGE_NAME, getContext().getPackageName()); 95 intent.putExtra(EXTRA_URI, uri); 96 intent.putExtra(EXTRA_MODE, mode); 97 call(intent); 98 } 99 revokeClipUriPermissionViaContext(Uri uri, int mode)100 static void revokeClipUriPermissionViaContext(Uri uri, int mode) { 101 Intent intent = new Intent(); 102 intent.setAction(ACTION_REVOKE_URI); 103 intent.putExtra(EXTRA_URI, uri); 104 intent.putExtra(EXTRA_MODE, mode); 105 call(intent); 106 } 107 setPrimaryClip(ClipData clip)108 static void setPrimaryClip(ClipData clip) { 109 Intent intent = new Intent(); 110 intent.setAction(ACTION_SET_PRIMARY_CLIP); 111 intent.setClipData(clip); 112 call(intent); 113 } 114 clearPrimaryClip()115 static void clearPrimaryClip() { 116 Intent intent = new Intent(); 117 intent.setAction(ACTION_CLEAR_PRIMARY_CLIP); 118 call(intent); 119 } 120 verifyOutgoingPersisted(Uri uri)121 static void verifyOutgoingPersisted(Uri uri) { 122 Intent intent = new Intent(); 123 intent.setAction(ACTION_VERIFY_OUTGOING_PERSISTED); 124 intent.putExtra(EXTRA_URI, uri); 125 call(intent); 126 } 127 } 128