1 /*
2  * Copyright (C) 2023 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 foo.bar.proxy;
18 
19 import static android.accessibilityservice.cts.utils.MultiProcessUtils.ACCESSIBILITY_SERVICE_STATE;
20 import static android.accessibilityservice.cts.utils.MultiProcessUtils.ACCESSIBILITY_STATE;
21 import static android.accessibilityservice.cts.utils.MultiProcessUtils.EXTRA_ENABLED;
22 import static android.accessibilityservice.cts.utils.MultiProcessUtils.EXTRA_ENABLED_SERVICES;
23 import static android.accessibilityservice.cts.utils.MultiProcessUtils.SEPARATE_PROCESS_ACTIVITY_TITLE;
24 import static android.accessibilityservice.cts.utils.MultiProcessUtils.TOUCH_EXPLORATION_STATE;
25 
26 import android.accessibilityservice.AccessibilityServiceInfo;
27 import android.app.Activity;
28 import android.content.Intent;
29 import android.os.Bundle;
30 import android.view.accessibility.AccessibilityManager;
31 
32 import java.util.List;
33 
34 /**
35  * Activity used in AccessibilityDisplayProxyTest.
36  */
37 public class NonProxySeparateAppActivity extends Activity {
38     @Override
onCreate(Bundle savedInstanceState)39     public void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.non_proxy_separate_app_activity);
42         setTitle(SEPARATE_PROCESS_ACTIVITY_TITLE);
43         final AccessibilityManager a11yManager = getSystemService(AccessibilityManager.class);
44         final AccessibilityManager.AccessibilityStateChangeListener stateChangeListener =
45                 enabled -> sendBroadcast(createIntentWithAction(ACCESSIBILITY_STATE)
46                         .putExtra(EXTRA_ENABLED, enabled));
47         final AccessibilityManager.AccessibilityServicesStateChangeListener
48                 servicesStateChangeListener = manager -> {
49                     final List<AccessibilityServiceInfo> enabled =
50                             manager.getEnabledAccessibilityServiceList(
51                                     AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
52                     final CharSequence[] enabledServiceList = new CharSequence[enabled.size()];
53                     for (int i = 0; i < enabled.size(); i++) {
54                         enabledServiceList[i] = enabled.get(i).getResolveInfo().serviceInfo.name;
55                     }
56                     sendBroadcast(createIntentWithAction(ACCESSIBILITY_SERVICE_STATE)
57                             .putExtra(EXTRA_ENABLED_SERVICES, enabledServiceList.clone()));
58                 };
59 
60         final AccessibilityManager.TouchExplorationStateChangeListener touchExplorationListener =
61                 enabled -> sendBroadcast(createIntentWithAction(TOUCH_EXPLORATION_STATE)
62                         .putExtra(EXTRA_ENABLED, enabled));
63 
64         a11yManager.addAccessibilityStateChangeListener(stateChangeListener);
65         a11yManager.addAccessibilityServicesStateChangeListener(servicesStateChangeListener);
66         a11yManager.addTouchExplorationStateChangeListener(touchExplorationListener);
67     }
68 
createIntentWithAction(String broadcastAction)69     private Intent createIntentWithAction(String broadcastAction) {
70         return new Intent(broadcastAction)
71                 .setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
72     }
73 }
74