1 /* 2 * Copyright (C) 2017 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.accessibility; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.support.annotation.NonNull; 24 import android.support.v14.preference.SwitchPreference; 25 import android.support.v17.preference.LeanbackPreferenceFragment; 26 import android.support.v7.preference.Preference; 27 import android.support.v7.preference.PreferenceScreen; 28 import android.support.v7.preference.TwoStatePreference; 29 import android.text.TextUtils; 30 31 import com.android.settingslib.accessibility.AccessibilityUtils; 32 import com.android.tv.settings.R; 33 34 import java.util.Set; 35 36 /** 37 * Fragment for controlling accessibility service 38 */ 39 public class AccessibilityServiceFragment extends LeanbackPreferenceFragment implements 40 AccessibilityServiceConfirmationFragment.OnAccessibilityServiceConfirmedListener { 41 private static final String ARG_PACKAGE_NAME = "packageName"; 42 private static final String ARG_SERVICE_NAME = "serviceName"; 43 private static final String ARG_SETTINGS_ACTIVITY_NAME = "settingsActivityName"; 44 private static final String ARG_LABEL = "label"; 45 46 private TwoStatePreference mEnablePref; 47 48 /** 49 * Put args in bundle 50 * @param args Bundle to prepare 51 * @param packageName Package of accessibility service 52 * @param serviceName Class of accessibility service 53 * @param activityName Class of accessibility service settings activity 54 * @param label Screen title 55 */ 56 public static void prepareArgs(@NonNull Bundle args, String packageName, String serviceName, 57 String activityName, String label) { 58 args.putString(ARG_PACKAGE_NAME, packageName); 59 args.putString(ARG_SERVICE_NAME, serviceName); 60 args.putString(ARG_SETTINGS_ACTIVITY_NAME, activityName); 61 args.putString(ARG_LABEL, label); 62 } 63 64 @Override 65 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 66 final Context themedContext = getPreferenceManager().getContext(); 67 final PreferenceScreen screen = 68 getPreferenceManager().createPreferenceScreen(themedContext); 69 screen.setTitle(getArguments().getString(ARG_LABEL)); 70 71 mEnablePref = new SwitchPreference(themedContext); 72 mEnablePref.setTitle(R.string.system_accessibility_status); 73 mEnablePref.setFragment(AccessibilityServiceConfirmationFragment.class.getName()); 74 screen.addPreference(mEnablePref); 75 76 final Preference settingsPref = new Preference(themedContext); 77 settingsPref.setTitle(R.string.system_accessibility_config); 78 final String activityName = getArguments().getString(ARG_SETTINGS_ACTIVITY_NAME); 79 if (!TextUtils.isEmpty(activityName)) { 80 final String packageName = getArguments().getString(ARG_PACKAGE_NAME); 81 settingsPref.setIntent(new Intent(Intent.ACTION_MAIN) 82 .setComponent(new ComponentName(packageName, activityName))); 83 } else { 84 settingsPref.setEnabled(false); 85 } 86 screen.addPreference(settingsPref); 87 88 setPreferenceScreen(screen); 89 } 90 91 @Override 92 public void onResume() { 93 super.onResume(); 94 95 final String packageName = getArguments().getString(ARG_PACKAGE_NAME); 96 final String serviceName = getArguments().getString(ARG_SERVICE_NAME); 97 98 final ComponentName serviceComponent = new ComponentName(packageName, serviceName); 99 final Set<ComponentName> enabledServices = 100 AccessibilityUtils.getEnabledServicesFromSettings(getActivity()); 101 final boolean enabled = enabledServices.contains(serviceComponent); 102 103 mEnablePref.setChecked(enabled); 104 AccessibilityServiceConfirmationFragment.prepareArgs(mEnablePref.getExtras(), 105 new ComponentName(packageName, serviceName), 106 getArguments().getString(ARG_LABEL), !enabled); 107 } 108 109 @Override 110 public void onAccessibilityServiceConfirmed(ComponentName componentName, boolean enabling) { 111 AccessibilityUtils.setAccessibilityServiceState(getActivity(), 112 componentName, enabling); 113 } 114 } 115