1 /* 2 * Copyright (C) 2010 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.permissiondeclareapp; 18 19 import android.content.ClipboardManager; 20 import android.content.ContentProvider; 21 import android.content.ContentValues; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.UriPermission; 25 import android.database.Cursor; 26 import android.net.Uri; 27 import android.os.Binder; 28 import android.os.Bundle; 29 30 import java.util.List; 31 32 public class UtilsProvider extends ContentProvider { 33 public static final Uri URI = Uri.parse("content://com.android.cts.permissiondeclareapp/"); 34 35 public static final String ACTION_GRANT_URI = "grantUri"; 36 public static final String ACTION_REVOKE_URI = "revokeUri"; 37 public static final String ACTION_START_ACTIVITY = "startActivity"; 38 public static final String ACTION_START_SERVICE = "startService"; 39 public static final String ACTION_VERIFY_OUTGOING_PERSISTED = "verifyOutgoingPersisted"; 40 public static final String ACTION_SET_PRIMARY_CLIP = "setPrimaryClip"; 41 public static final String ACTION_CLEAR_PRIMARY_CLIP = "clearPrimaryClip"; 42 public static final String ACTION_SET_INSTALLER_PACKAGE_NAME = "setInstallerPackageName"; 43 44 public static final String EXTRA_PACKAGE_NAME = "packageName"; 45 public static final String EXTRA_INSTALLER_PACKAGE_NAME = "installerPackageName"; 46 public static final String EXTRA_INTENT = Intent.EXTRA_INTENT; 47 public static final String EXTRA_URI = "uri"; 48 public static final String EXTRA_MODE = "mode"; 49 50 @Override call(String method, String arg, Bundle extras)51 public Bundle call(String method, String arg, Bundle extras) { 52 final Context context = getContext(); 53 final Intent intent = extras.getParcelable(Intent.EXTRA_INTENT); 54 final String action = intent.getAction(); 55 56 final long token = Binder.clearCallingIdentity(); 57 try { 58 if (ACTION_GRANT_URI.equals(action)) { 59 final Uri uri = intent.getParcelableExtra(EXTRA_URI); 60 context.grantUriPermission(intent.getStringExtra(EXTRA_PACKAGE_NAME), uri, 61 intent.getIntExtra(EXTRA_MODE, 0)); 62 63 } else if (ACTION_REVOKE_URI.equals(action)) { 64 final Uri uri = intent.getParcelableExtra(EXTRA_URI); 65 context.revokeUriPermission(uri, intent.getIntExtra(EXTRA_MODE, 0)); 66 67 } else if (ACTION_START_ACTIVITY.equals(action)) { 68 final Intent newIntent = intent.getParcelableExtra(EXTRA_INTENT); 69 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 70 context.startActivity(newIntent); 71 72 } else if (ACTION_START_SERVICE.equals(action)) { 73 final Intent newIntent = intent.getParcelableExtra(EXTRA_INTENT); 74 context.startService(newIntent); 75 76 } else if (ACTION_VERIFY_OUTGOING_PERSISTED.equals(action)) { 77 verifyOutgoingPersisted(context, intent); 78 79 } else if (ACTION_SET_PRIMARY_CLIP.equals(action)) { 80 context.getSystemService(ClipboardManager.class) 81 .setPrimaryClip(intent.getClipData()); 82 83 } else if (ACTION_CLEAR_PRIMARY_CLIP.equals(action)) { 84 context.getSystemService(ClipboardManager.class).clearPrimaryClip(); 85 86 } else if (ACTION_SET_INSTALLER_PACKAGE_NAME.equals(action)) { 87 context.getPackageManager().setInstallerPackageName( 88 intent.getStringExtra(EXTRA_PACKAGE_NAME), 89 intent.getStringExtra(EXTRA_INSTALLER_PACKAGE_NAME)); 90 } 91 } finally { 92 Binder.restoreCallingIdentity(token); 93 } 94 return null; 95 } 96 verifyOutgoingPersisted(Context context, Intent intent)97 private void verifyOutgoingPersisted(Context context, Intent intent) { 98 final Uri uri = intent.getParcelableExtra(EXTRA_URI); 99 final List<UriPermission> perms = context.getContentResolver() 100 .getOutgoingPersistedUriPermissions(); 101 if (uri != null) { 102 // Should have a single persisted perm 103 if (perms.size() != 1) { 104 throw new SecurityException("Missing grant"); 105 } 106 final UriPermission perm = perms.get(0); 107 if (!perm.getUri().equals(uri)) { 108 throw new SecurityException( 109 "Expected " + uri + " but found " + perm.getUri()); 110 } 111 } else { 112 // Should have zero persisted perms 113 if (perms.size() != 0) { 114 throw new SecurityException("Unexpected grant"); 115 } 116 } 117 } 118 119 @Override onCreate()120 public boolean onCreate() { 121 return true; 122 } 123 124 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)125 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 126 String sortOrder) { 127 throw new UnsupportedOperationException(); 128 } 129 130 @Override getType(Uri uri)131 public String getType(Uri uri) { 132 throw new UnsupportedOperationException(); 133 } 134 135 @Override insert(Uri uri, ContentValues values)136 public Uri insert(Uri uri, ContentValues values) { 137 throw new UnsupportedOperationException(); 138 } 139 140 @Override delete(Uri uri, String selection, String[] selectionArgs)141 public int delete(Uri uri, String selection, String[] selectionArgs) { 142 throw new UnsupportedOperationException(); 143 } 144 145 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)146 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 147 throw new UnsupportedOperationException(); 148 } 149 } 150