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