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 import com.android.tv.settings.overlay.FlavorUtils;
31 
32 import java.util.List;
33 
34 /** An OEM hook for starting a specific accessibility service settings directly. */
35 public class AccessibilityServiceActivity extends TvSettingsActivity {
36 
37     private static final String TAG = "A11yServiceOemLink";
38     private static final String A11Y_SERVICE_INFO_EXTRA = "accessibilityServiceInfo";
39 
40     @Override
createSettingsFragment()41     protected Fragment createSettingsFragment()  {
42         if (getIntent() == null || getIntent().getExtras() == null
43                 || getIntent().getExtras().getParcelable(A11Y_SERVICE_INFO_EXTRA) == null) {
44             Log.e(TAG, "No accessibility info extras, returning null");
45             return null;
46         }
47         AccessibilityServiceInfo a11yServiceInfo =
48                 getIntent().getExtras().getParcelable(A11Y_SERVICE_INFO_EXTRA);
49         final List<AccessibilityServiceInfo> installedA11yServiceInfos =
50                 getSystemService(AccessibilityManager.class)
51                         .getInstalledAccessibilityServiceList();
52         if (installedA11yServiceInfos == null || installedA11yServiceInfos.isEmpty()
53                 || !installedA11yServiceInfos.contains(a11yServiceInfo)) {
54             Log.e(TAG, "Input accessibility service info is absent on device, returning null");
55             return null;
56         }
57         Bundle args = new Bundle();
58         AccessibilityServiceFragment.prepareArgs(
59                 args,
60                 a11yServiceInfo.getResolveInfo().serviceInfo.packageName,
61                 a11yServiceInfo.getResolveInfo().serviceInfo.name,
62                 a11yServiceInfo.getSettingsActivityName(),
63                 a11yServiceInfo.getResolveInfo().loadLabel(this.getPackageManager()).toString());
64         return FlavorUtils.getFeatureFactory(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