1 /*
2  * Copyright (C) 2020 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.tv.settings.oemlink;
18 
19 import static com.android.tv.settings.overlay.FlavorUtils.X_EXPERIENCE_FLAVORS_MASK;
20 
21 import android.accessibilityservice.AccessibilityServiceInfo;
22 import android.os.Bundle;
23 import android.util.Log;
24 import android.view.accessibility.AccessibilityManager;
25 
26 import androidx.fragment.app.Fragment;
27 
28 import com.android.tv.settings.TvSettingsActivity;
29 import com.android.tv.settings.accessibility.AccessibilityServiceFragment;
30 
31 import java.util.List;
32 
33 /** An OEM hook for starting a specific accessibility service settings directly. */
34 public class AccessibilityServiceActivity extends TvSettingsActivity {
35 
36     private static final String TAG = "A11yServiceOemLink";
37     private static final String A11Y_SERVICE_INFO_EXTRA = "accessibilityServiceInfo";
38 
39     @Override
createSettingsFragment()40     protected Fragment createSettingsFragment() {
41         if (getIntent() == null || getIntent().getExtras() == null
42                 || getIntent().getExtras().getParcelable(A11Y_SERVICE_INFO_EXTRA) == null) {
43             Log.e(TAG, "No accessibility info extras, returning null");
44             return null;
45         }
46         AccessibilityServiceInfo a11yServiceInfo =
47                 getIntent().getExtras().getParcelable(A11Y_SERVICE_INFO_EXTRA);
48         final List<AccessibilityServiceInfo> installedA11yServiceInfos =
49                 getSystemService(AccessibilityManager.class)
50                         .getInstalledAccessibilityServiceList();
51         if (installedA11yServiceInfos == null || installedA11yServiceInfos.isEmpty()
52                 || !installedA11yServiceInfos.contains(a11yServiceInfo)) {
53             Log.e(TAG, "Input accessibility service info is absent on device, returning null");
54             return null;
55         }
56         Bundle args = new Bundle();
57         AccessibilityServiceFragment.prepareArgs(
58                 args,
59                 a11yServiceInfo.getResolveInfo().serviceInfo.packageName,
60                 a11yServiceInfo.getResolveInfo().serviceInfo.name,
61                 a11yServiceInfo.getSettingsActivityName(),
62                 a11yServiceInfo.getResolveInfo().loadLabel(this.getPackageManager()).toString());
63         return com.android.tv.settings.overlay.FlavorUtils.getFeatureFactory(
64                 this).getSettingsFragmentProvider()
65                 .newSettingsFragment(AccessibilityServiceFragment.class.getName(), args);
66     }
67 
68     @Override
getAvailableFlavors()69     protected int getAvailableFlavors() {
70         return X_EXPERIENCE_FLAVORS_MASK;
71     }
72 }
73