1 /* 2 * Copyright (C) 2022 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.server.sdksandbox; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.os.Handler; 24 import android.os.Looper; 25 import android.util.Log; 26 27 import com.android.internal.annotations.Keep; 28 29 /** 30 * Broadcast Receiver for receiving new Sdk install requests and verifying Sdk code before running 31 * it in Sandbox. 32 * 33 * @hide 34 */ 35 @Keep // See b/255754931. Explicitly keep SdkSandboxVerifierReceiver for reflection. 36 public class SdkSandboxVerifierReceiver extends BroadcastReceiver { 37 38 private static final String TAG = "SdkSandboxManager"; 39 private static final Handler MAIN_HANDLER = new Handler(Looper.getMainLooper()); 40 41 42 @Override onReceive(Context context, Intent intent)43 public void onReceive(Context context, Intent intent) { 44 if (!Intent.ACTION_PACKAGE_NEEDS_VERIFICATION.equals(intent.getAction())) { 45 return; 46 } 47 48 Log.d(TAG, "Received sdk sandbox verification intent " + intent.toString()); 49 Log.d(TAG, "Extras " + intent.getExtras()); 50 51 MAIN_HANDLER.post(() -> verifySdkHandler(context, intent)); 52 } 53 verifySdkHandler(Context context, Intent intent)54 private void verifySdkHandler(Context context, Intent intent) { 55 int verificationId = intent.getIntExtra(PackageManager.EXTRA_VERIFICATION_ID, -1); 56 context.getPackageManager() 57 .verifyPendingInstall(verificationId, PackageManager.VERIFICATION_ALLOW); 58 } 59 } 60