1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.inputmethod;
import com.android.settings.R;
import com.android.settings.Utils;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.preference.Preference;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.textservice.SpellCheckerInfo;
import android.view.textservice.SpellCheckerSubtype;
import android.view.textservice.TextServicesManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;
// TODO: Make this non-persistent.
class SingleSpellCheckerPreference extends Preference {
private static final String TAG = SingleSpellCheckerPreference.class.getSimpleName();
private static final boolean DBG = false;
private final SpellCheckerInfo mSpellCheckerInfo;
private final SpellCheckersSettings mFragment;
private final Resources mRes;
private final TextServicesManager mTsm;
private AlertDialog mDialog = null;
private View mPrefAll;
private RadioButton mRadioButton;
private View mPrefLeftButton;
private View mSettingsButton;
private ImageView mSubtypeButton;
private Intent mSettingsIntent;
private boolean mSelected;
public SingleSpellCheckerPreference(SpellCheckersSettings fragment, SpellCheckerInfo sci,
TextServicesManager tsm) {
super(fragment.getActivity(), null, 0);
mFragment = fragment;
mRes = fragment.getActivity().getResources();
mTsm = tsm;
setLayoutResource(R.layout.preference_spellchecker);
mSpellCheckerInfo = sci;
mSelected = false;
final String settingsActivity = mSpellCheckerInfo.getSettingsActivity();
if (!TextUtils.isEmpty(settingsActivity)) {
mSettingsIntent = new Intent(Intent.ACTION_MAIN);
mSettingsIntent.setClassName(mSpellCheckerInfo.getPackageName(), settingsActivity);
} else {
mSettingsIntent = null;
}
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
mPrefAll = view.findViewById(R.id.pref_all);
mRadioButton = (RadioButton)view.findViewById(R.id.pref_radio);
mPrefLeftButton = view.findViewById(R.id.pref_left_button);
mPrefLeftButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View arg0) {
onLeftButtonClicked(arg0);
}
});
mSubtypeButton = (ImageView)view.findViewById(R.id.pref_right_button2);
mSubtypeButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View arg0) {
onSubtypeButtonClicked(arg0);
}
});
mSettingsButton = view.findViewById(R.id.pref_right_button1);
mSettingsButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View arg0) {
onSettingsButtonClicked(arg0);
}
});
updateSelectedState(mSelected);
}
private void onLeftButtonClicked(View arg0) {
mFragment.onPreferenceClick(this);
}
public SpellCheckerInfo getSpellCheckerInfo() {
return mSpellCheckerInfo;
}
private void updateSelectedState(boolean selected) {
if (mPrefAll != null) {
mRadioButton.setChecked(selected);
enableButtons(selected);
}
}
public void setSelected(boolean selected) {
mSelected = selected;
updateSelectedState(selected);
}
private void onSubtypeButtonClicked(View arg0) {
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(mFragment.getActivity());
builder.setTitle(R.string.phone_language);
final int size = mSpellCheckerInfo.getSubtypeCount();
final CharSequence[] items = new CharSequence[size + 1];
items[0] = mRes.getString(R.string.use_system_language_to_select_input_method_subtypes);
for (int i = 0; i < size; ++i) {
final SpellCheckerSubtype subtype = mSpellCheckerInfo.getSubtypeAt(i);
final CharSequence label = subtype.getDisplayName(
mFragment.getActivity(), mSpellCheckerInfo.getPackageName(),
mSpellCheckerInfo.getServiceInfo().applicationInfo);
items[i + 1] = label;
}
// default: "Use system language"
int checkedItem = 0;
// Allow no implicitly selected subtypes
final SpellCheckerSubtype currentScs = mTsm.getCurrentSpellCheckerSubtype(false);
if (currentScs != null) {
for (int i = 0; i < size; ++i) {
if (mSpellCheckerInfo.getSubtypeAt(i).equals(currentScs)) {
checkedItem = i + 1;
break;
}
}
}
builder.setSingleChoiceItems(items, checkedItem, new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
mTsm.setSpellCheckerSubtype(null);
} else {
mTsm.setSpellCheckerSubtype(mSpellCheckerInfo.getSubtypeAt(which - 1));
}
if (DBG) {
final SpellCheckerSubtype subtype = mTsm.getCurrentSpellCheckerSubtype(true);
Log.d(TAG, "Current spell check locale is "
+ subtype == null ? "null" : subtype.getLocale());
}
dialog.dismiss();
}
});
mDialog = builder.create();
mDialog.show();
}
private void onSettingsButtonClicked(View arg0) {
if (mFragment != null && mSettingsIntent != null) {
try {
mFragment.startActivity(mSettingsIntent);
} catch (ActivityNotFoundException e) {
final String msg = mFragment.getString(R.string.failed_to_open_app_settings_toast,
mSpellCheckerInfo.loadLabel(mFragment.getActivity().getPackageManager()));
Toast.makeText(mFragment.getActivity(), msg, Toast.LENGTH_LONG).show();
}
}
}
private void enableButtons(boolean enabled) {
if (mSettingsButton != null) {
if (mSettingsIntent == null) {
mSettingsButton.setVisibility(View.GONE);
} else {
mSettingsButton.setEnabled(enabled);
mSettingsButton.setClickable(enabled);
mSettingsButton.setFocusable(enabled);
if (!enabled) {
mSettingsButton.setAlpha(Utils.DISABLED_ALPHA);
}
}
}
if (mSubtypeButton != null) {
if (mSpellCheckerInfo.getSubtypeCount() <= 0) {
mSubtypeButton.setVisibility(View.GONE);
} else {
mSubtypeButton.setEnabled(enabled);
mSubtypeButton.setClickable(enabled);
mSubtypeButton.setFocusable(enabled);
if (!enabled) {
mSubtypeButton.setAlpha(Utils.DISABLED_ALPHA);
}
}
}
}
}
|