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 package com.android.tests.sdkprovider.crashtest; 17 18 import android.app.ActivityManager; 19 import android.app.ApplicationExitInfo; 20 import android.app.sdksandbox.SandboxedSdk; 21 import android.app.sdksandbox.SandboxedSdkProvider; 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.view.View; 25 26 import java.util.List; 27 import java.util.concurrent.Executors; 28 29 public class CrashTestSandboxedSdkProvider extends SandboxedSdkProvider { 30 31 private static final int MAX_HISTORICAL_PROCESS_EXIT_REASONS = 10; 32 33 static class CrashTestSdkImpl extends ICrashTestSdkApi.Stub { 34 private final Context mContext; 35 CrashTestSdkImpl(Context context)36 CrashTestSdkImpl(Context context) { 37 mContext = context; 38 } 39 40 @Override triggerCrash()41 public void triggerCrash() { 42 Executors.newSingleThreadExecutor() 43 .execute( 44 () -> { 45 throw new RuntimeException("This is a test exception"); 46 }); 47 } 48 49 @Override getSdkSandboxExitReasons()50 public List<ApplicationExitInfo> getSdkSandboxExitReasons() { 51 final ActivityManager am = mContext.getSystemService(ActivityManager.class); 52 if (am == null) { 53 throw new RuntimeException("No activity manager"); 54 } 55 return am.getHistoricalProcessExitReasons( 56 mContext.getPackageName(), 0, MAX_HISTORICAL_PROCESS_EXIT_REASONS); 57 } 58 } 59 60 @Override onLoadSdk(Bundle params)61 public SandboxedSdk onLoadSdk(Bundle params) { 62 return new SandboxedSdk(new CrashTestSdkImpl(getContext())); 63 } 64 65 @Override getView(Context windowContext, Bundle params, int width, int height)66 public View getView(Context windowContext, Bundle params, int width, int height) { 67 return new View(windowContext); 68 } 69 } 70