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 17package com.android.tv.settings.accessibility; 18 19import android.app.Fragment; 20import android.content.ComponentName; 21import android.content.Context; 22import android.os.Bundle; 23import android.support.annotation.NonNull; 24import android.support.v17.leanback.app.GuidedStepFragment; 25import android.support.v17.leanback.widget.GuidanceStylist; 26import android.support.v17.leanback.widget.GuidedAction; 27 28import com.android.tv.settings.R; 29 30import java.util.List; 31 32/** 33 * Fragment for confirming [de]activation of accessibility service 34 */ 35public class AccessibilityServiceConfirmationFragment extends GuidedStepFragment { 36 private static final String ARG_LABEL = "label"; 37 private static final String ARG_COMPONENT = "component"; 38 private static final String ARG_ENABLING = "enabling"; 39 40 /** 41 * Callback for dialog completion 42 */ 43 public interface OnAccessibilityServiceConfirmedListener { 44 /** 45 * Called when enabling/disabling was confirmed by the user, not called otherwise. 46 * @param componentName Service in question 47 * @param enabling True for enabling 48 */ 49 void onAccessibilityServiceConfirmed(ComponentName componentName, boolean enabling); 50 } 51 52 /** 53 * Create a new instance of the fragment 54 * @param cn Component of service 55 * @param label Human readable label 56 * @param enabling True for enabling 57 * @return new fragment instance 58 */ 59 public static AccessibilityServiceConfirmationFragment newInstance(ComponentName cn, 60 CharSequence label, boolean enabling) { 61 Bundle args = new Bundle(3); 62 prepareArgs(args, cn, label, enabling); 63 AccessibilityServiceConfirmationFragment fragment = 64 new AccessibilityServiceConfirmationFragment(); 65 fragment.setArguments(args); 66 return fragment; 67 } 68 69 /** 70 * Put args in bundle 71 * @param args Bundle to prepare 72 * @param cn Component of service 73 * @param label Human readable label 74 * @param enabling True for enabling 75 */ 76 public static void prepareArgs(@NonNull Bundle args, ComponentName cn, CharSequence label, 77 boolean enabling) { 78 args.putParcelable(ARG_COMPONENT, cn); 79 args.putCharSequence(ARG_LABEL, label); 80 args.putBoolean(ARG_ENABLING, enabling); 81 } 82 83 @NonNull 84 @Override 85 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 86 final CharSequence label = getArguments().getCharSequence(ARG_LABEL); 87 if (getArguments().getBoolean(ARG_ENABLING)) { 88 return new GuidanceStylist.Guidance( 89 getString(R.string.system_accessibility_service_on_confirm_title, 90 label), 91 getString(R.string.system_accessibility_service_on_confirm_desc, 92 label), 93 null, 94 getActivity().getDrawable(R.drawable.ic_accessibility_new_132dp) 95 ); 96 } else { 97 return new GuidanceStylist.Guidance( 98 getString(R.string.system_accessibility_service_off_confirm_title, 99 label), 100 getString(R.string.system_accessibility_service_off_confirm_desc, 101 label), 102 null, 103 getActivity().getDrawable(R.drawable.ic_accessibility_new_132dp) 104 ); 105 } 106 } 107 108 @Override 109 public void onCreateActions(@NonNull List<GuidedAction> actions, 110 Bundle savedInstanceState) { 111 final Context context = getActivity(); 112 actions.add(new GuidedAction.Builder(context) 113 .clickAction(GuidedAction.ACTION_ID_OK).build()); 114 actions.add(new GuidedAction.Builder(context) 115 .clickAction(GuidedAction.ACTION_ID_CANCEL).build()); 116 } 117 118 @Override 119 public void onGuidedActionClicked(GuidedAction action) { 120 if (action.getId() == GuidedAction.ACTION_ID_OK) { 121 final ComponentName component = getArguments().getParcelable(ARG_COMPONENT); 122 final Fragment fragment = getTargetFragment(); 123 final boolean enabling = getArguments().getBoolean(ARG_ENABLING); 124 if (fragment instanceof OnAccessibilityServiceConfirmedListener) { 125 ((OnAccessibilityServiceConfirmedListener) fragment) 126 .onAccessibilityServiceConfirmed(component, enabling); 127 } else { 128 throw new IllegalStateException("Target fragment is not an " 129 + "OnAccessibilityServiceConfirmedListener"); 130 } 131 getFragmentManager().popBackStack(); 132 } else if (action.getId() == GuidedAction.ACTION_ID_CANCEL) { 133 getFragmentManager().popBackStack(); 134 } else { 135 super.onGuidedActionClicked(action); 136 } 137 } 138} 139