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