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.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.UriPermission; 23 import android.net.Uri; 24 import android.util.Log; 25 26 import java.util.List; 27 28 public class GrantUriPermission extends BroadcastReceiver { 29 public static final String ACTION_GRANT_URI = "grantUri"; 30 public static final String ACTION_REVOKE_URI = "revokeUri"; 31 public static final String ACTION_START_ACTIVITY = "startActivity"; 32 public static final String ACTION_START_SERVICE = "startService"; 33 public static final String ACTION_VERIFY_OUTGOING_PERSISTED = "verifyOutgoingPersisted"; 34 35 public static final String EXTRA_PACKAGE_NAME = "packageName"; 36 public static final String EXTRA_INTENT = Intent.EXTRA_INTENT; 37 public static final String EXTRA_URI = "uri"; 38 public static final String EXTRA_MODE = "mode"; 39 40 public static final int SUCCESS = 101; 41 public static final int FAILURE = 100; 42 43 @Override onReceive(Context context, Intent intent)44 public void onReceive(Context context, Intent intent) { 45 try { 46 final String action = intent.getAction(); 47 if (ACTION_GRANT_URI.equals(action)) { 48 final Uri uri = intent.getParcelableExtra(EXTRA_URI); 49 context.grantUriPermission(intent.getStringExtra(EXTRA_PACKAGE_NAME), uri, 50 intent.getIntExtra(EXTRA_MODE, 0)); 51 52 } else if (ACTION_REVOKE_URI.equals(action)) { 53 final Uri uri = intent.getParcelableExtra(EXTRA_URI); 54 context.revokeUriPermission(uri, intent.getIntExtra(EXTRA_MODE, 0)); 55 56 } else if (ACTION_START_ACTIVITY.equals(action)) { 57 final Intent newIntent = intent.getParcelableExtra(EXTRA_INTENT); 58 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 59 context.startActivity(newIntent); 60 61 } else if (ACTION_START_SERVICE.equals(action)) { 62 final Intent newIntent = intent.getParcelableExtra(EXTRA_INTENT); 63 context.startService(newIntent); 64 65 } else if (ACTION_VERIFY_OUTGOING_PERSISTED.equals(action)) { 66 verifyOutgoingPersisted(context, intent); 67 } 68 69 if (isOrderedBroadcast()) { 70 setResultCode(SUCCESS); 71 } 72 } catch (SecurityException e) { 73 Log.i("GrantUriPermission", "Security exception", e); 74 if (isOrderedBroadcast()) { 75 setResultCode(FAILURE); 76 } 77 } 78 } 79 verifyOutgoingPersisted(Context context, Intent intent)80 private void verifyOutgoingPersisted(Context context, Intent intent) { 81 final Uri uri = intent.getParcelableExtra(EXTRA_URI); 82 final List<UriPermission> perms = context.getContentResolver() 83 .getOutgoingPersistedUriPermissions(); 84 if (uri != null) { 85 // Should have a single persisted perm 86 if (perms.size() != 1) { 87 throw new SecurityException("Missing grant"); 88 } 89 final UriPermission perm = perms.get(0); 90 if (!perm.getUri().equals(uri)) { 91 throw new SecurityException( 92 "Expected " + uri + " but found " + perm.getUri()); 93 } 94 } else { 95 // Should have zero persisted perms 96 if (perms.size() != 0) { 97 throw new SecurityException("Unexpected grant"); 98 } 99 } 100 } 101 } 102