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