1 /*
2  * Copyright (C) 2014 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.settings.inputmethod;
18 
19 import android.content.ActivityNotFoundException;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.text.TextUtils;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.view.textservice.SpellCheckerInfo;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.appcompat.app.AlertDialog.Builder;
30 import androidx.preference.PreferenceViewHolder;
31 
32 import com.android.settings.CustomListPreference;
33 import com.android.settings.R;
34 
35 /**
36  * Spell checker service preference.
37  *
38  * This preference represents a spell checker service. It is used for two purposes. 1) A radio
39  * button on the left side is used to choose the current spell checker service. 2) A settings
40  * icon on the right side is used to invoke the setting activity of the spell checker service.
41  */
42 class SpellCheckerPreference extends CustomListPreference {
43 
44     private final SpellCheckerInfo[] mScis;
45     @VisibleForTesting
46     Intent mIntent;
47 
SpellCheckerPreference(final Context context, final SpellCheckerInfo[] scis)48     public SpellCheckerPreference(final Context context, final SpellCheckerInfo[] scis) {
49         super(context, null);
50         mScis = scis;
51         setLayoutResource(
52                 com.android.settingslib.widget.preference.twotarget.R.layout.preference_two_target);
53 
54         setWidgetLayoutResource(R.layout.preference_widget_gear);
55         if (scis == null) {
56             return;
57         }
58         CharSequence[] labels = new CharSequence[scis.length];
59         CharSequence[] values = new CharSequence[scis.length];
60         for (int i = 0 ; i < scis.length; i++) {
61             labels[i] = scis[i].loadLabel(context.getPackageManager());
62             // Use values as indexing since ListPreference doesn't support generic objects.
63             values[i] = String.valueOf(i);
64         }
65         setEntries(labels);
66         setEntryValues(values);
67     }
68 
69     @Override
onPrepareDialogBuilder(Builder builder, DialogInterface.OnClickListener listener)70     protected void onPrepareDialogBuilder(Builder builder,
71             DialogInterface.OnClickListener listener) {
72         builder.setTitle(R.string.choose_spell_checker);
73         builder.setSingleChoiceItems(getEntries(), findIndexOfValue(getValue()), listener);
74     }
75 
setSelected(SpellCheckerInfo currentSci)76     public void setSelected(SpellCheckerInfo currentSci) {
77         if (currentSci == null) {
78             setValue(null);
79             return;
80         }
81         for (int i = 0; i < mScis.length; i++) {
82             if (mScis[i].getId().equals(currentSci.getId())) {
83                 setValueIndex(i);
84                 return;
85             }
86         }
87     }
88 
89     @Override
setValue(String value)90     public void setValue(String value) {
91         super.setValue(value);
92         int index = value != null ? Integer.parseInt(value) : -1;
93         if (index == -1) {
94             mIntent = null;
95             return;
96         }
97         SpellCheckerInfo sci = mScis[index];
98         final String settingsActivity = sci.getSettingsActivity();
99         if (TextUtils.isEmpty(settingsActivity)) {
100             mIntent = null;
101         } else {
102             mIntent = new Intent(Intent.ACTION_MAIN);
103             mIntent.setClassName(sci.getPackageName(), settingsActivity);
104         }
105     }
106 
107     @Override
callChangeListener(Object newValue)108     public boolean callChangeListener(Object newValue) {
109         newValue = newValue != null ? mScis[Integer.parseInt((String) newValue)] : null;
110         return super.callChangeListener(newValue);
111     }
112 
113     @Override
onBindViewHolder(PreferenceViewHolder view)114     public void onBindViewHolder(PreferenceViewHolder view) {
115         super.onBindViewHolder(view);
116         final View divider = view.findViewById(
117                 com.android.settingslib.widget.preference.twotarget.R.id.two_target_divider);
118         final View widgetFrame = view.findViewById(android.R.id.widget_frame);
119         if (divider != null) {
120             divider.setVisibility(mIntent != null ? View.VISIBLE : View.GONE);
121         }
122         if (widgetFrame != null) {
123             widgetFrame.setVisibility(mIntent != null ? View.VISIBLE : View.GONE);
124         }
125 
126         View settingsButton = view.findViewById(R.id.settings_button);
127         if (settingsButton != null) {
128             settingsButton.setVisibility(mIntent != null ? View.VISIBLE : View.INVISIBLE);
129             settingsButton.setOnClickListener(new OnClickListener() {
130                 @Override
131                 public void onClick(View v) {
132                     onSettingsButtonClicked();
133                 }
134             });
135         }
136     }
137 
onSettingsButtonClicked()138     private void onSettingsButtonClicked() {
139         final Context context = getContext();
140         try {
141             final Intent intent = mIntent;
142             if (intent != null) {
143                 // Invoke a settings activity of an spell checker.
144                 context.startActivity(intent);
145             }
146         } catch (final ActivityNotFoundException e) {
147         }
148     }
149 }
150