1 /*
2  * Copyright (C) 2021 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.sdkext.extensions.apps;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Build;
23 import android.os.ext.SdkExtensions;
24 import android.os.ext.testing.Test;
25 import android.util.Log;
26 
27 public class Receiver extends BroadcastReceiver {
28 
29     private static final String ACTION_GET_SDK_VERSION =
30             "com.android.sdkext.extensions.apps.GET_SDK_VERSION";
31     private static final String ACTION_MAKE_CALLS_DEFAULT =
32             "com.android.sdkext.extensions.apps.MAKE_CALLS_DEFAULT";
33     private static final String ACTION_MAKE_CALLS_45 =
34             "com.android.sdkext.extensions.apps.MAKE_CALLS_45";
35 
dessertLetterToInt(char letter)36     private static int dessertLetterToInt(char letter) {
37         switch (letter) {
38             case 'r':
39                 return Build.VERSION_CODES.R;
40             case 's':
41                 return Build.VERSION_CODES.S;
42             case 't':
43                 return Build.VERSION_CODES.TIRAMISU;
44         }
45         throw new IllegalArgumentException(String.valueOf(letter));
46     }
47 
48     @Override
onReceive(Context context, Intent intent)49     public void onReceive(Context context, Intent intent) {
50         try {
51             switch (intent.getAction()) {
52                 case ACTION_GET_SDK_VERSION:
53                     int extension = dessertLetterToInt(intent.getStringExtra("extra").charAt(0));
54                     int sdkVersion = SdkExtensions.getExtensionVersion(extension);
55                     setResultData(String.valueOf(sdkVersion));
56                     break;
57                 case ACTION_MAKE_CALLS_DEFAULT:
58                     makeCallsDefault();
59                     setResultData("true");
60                     break;
61                 case ACTION_MAKE_CALLS_45:
62                     makeCallsVersion45();
63                     setResultData("true");
64                     break;
65             }
66         } catch (Throwable e) {
67             Log.e("SdkExtensionsE2E", "Unexpected error/exception", e);
68             setResultData(e.toString());
69         }
70     }
71 
makeCallsDefault()72     private static void makeCallsDefault() {
73         expectException(NoClassDefFoundError.class, "test class", () -> new Test());
74         expectException(NoClassDefFoundError.class, "test class", () -> Test.staticPublicMethod());
75         expectException(
76                 NoClassDefFoundError.class, "test class", () -> Test.staticSystemApiMethod());
77         expectException(
78                 NoClassDefFoundError.class, "test class", () -> Test.staticModuleLibsApiMethod());
79         expectException(NoClassDefFoundError.class, "test class", () -> Test.staticHiddenMethod());
80     }
81 
makeCallsVersion45()82     private static void makeCallsVersion45() {
83         Test test = new Test();
84 
85         test.publicMethod();
86         test.systemApiMethod();
87         expectException(NoSuchMethodError.class, "module method", () -> test.moduleLibsApiMethod());
88         expectException(NoSuchMethodError.class, "testapi method", () -> test.testApiMethod());
89         expectException(NoSuchMethodError.class, "hidden method", () -> test.hiddenMethod());
90 
91         Test.staticPublicMethod();
92         Test.staticSystemApiMethod();
93         expectException(
94                 NoSuchMethodError.class,
95                 "static module-libs method",
96                 () -> Test.staticModuleLibsApiMethod());
97         expectException(
98                 NoSuchMethodError.class, "static testapi method", () -> Test.staticTestApiMethod());
99         expectException(
100                 NoSuchMethodError.class, "static hidden method", () -> Test.staticHiddenMethod());
101     }
102 
expectException(Class exceptionClass, String type, Runnable runnable)103     private static void expectException(Class exceptionClass, String type, Runnable runnable) {
104         boolean success = false;
105         try {
106             runnable.run();
107             success = true;
108         } catch (Throwable e) {
109             if (!e.getClass().equals(exceptionClass)) {
110                 throw new IllegalStateException("Saw unexpected exception", e);
111             }
112         }
113         if (success) {
114             throw new IllegalStateException("Accessed " + type + ", but shouldn't be able to");
115         }
116     }
117 }
118