aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dialog/EditInputDialogFragment.java
blob: 068cd707beea8c7e475c89fea8e532a6623cf115 (plain)
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
/*
 * Copyright (C) 2014 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.tv.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.tv.TvInputInfo;
import android.media.tv.TvInputManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.tv.R;
import com.android.tv.util.TvSettings;
import com.android.tv.util.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EditInputDialogFragment extends DialogFragment implements OnItemSelectedListener {
    public static final String DIALOG_TAG = EditInputDialogFragment.class.getName();

    private TvInputManager mTvInputManager;
    private SharedPreferences mPreferences;

    // A mapping from the display name of each TV input to its ID (String).
    private final Map<String, String> mInputIdMap = new HashMap<String, String>();

    // A list of display names for all TV inputs.
    private String[] mDisplayNames;

    private ArrayAdapter<String> mAdapter;
    private int mSelected;
    private EditText mEditText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPreferences = Utils.getSharedPreferencesOfDisplayNameForInput(getActivity());
        mAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
                new ArrayList<String>());

        mTvInputManager = (TvInputManager) getActivity().getSystemService(Context.TV_INPUT_SERVICE);
        setupAdapter();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_edit_input, null);
        Spinner dropdown = (Spinner) view.findViewById(R.id.spinner);
        dropdown.setAdapter(mAdapter);
        dropdown.setOnItemSelectedListener(this);
        mEditText = (EditText) view.findViewById(R.id.input_edit_text);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                AlertDialog dialog = (AlertDialog) getDialog();
                dialog.getButton(DialogInterface.BUTTON_POSITIVE)
                        .setEnabled(!TextUtils.isEmpty(s.toString()));
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                /* do nothing */
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                /* do nothing */
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.new_input_device_name)
                .setView(view)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        if (mSelected < 0 || mSelected >= mDisplayNames.length) {
                            return;
                        }
                        String oldDisplayName = mDisplayNames[mSelected];
                        String newDisplayName = mEditText.getText().toString().trim();
                        if (oldDisplayName.equals(newDisplayName)) {
                            return;
                        }
                        if (mInputIdMap.get(newDisplayName) != null) {
                            Toast.makeText(getActivity(), R.string.name_already_exists,
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        String inputId = mInputIdMap.get(oldDisplayName);
                        String key = TvSettings.PREF_DISPLAY_INPUT_NAME + inputId;
                        mPreferences.edit().putString(key, newDisplayName).commit();
                    }
                })
                .setNeutralButton(R.string.reset, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        if (mSelected < 0 || mSelected >= mDisplayNames.length) {
                            return;
                        }
                        String oldDisplayName = mDisplayNames[mSelected];
                        String inputId = mInputIdMap.get(oldDisplayName);
                        mPreferences.edit().remove(
                                TvSettings.PREF_DISPLAY_INPUT_NAME + inputId).commit();
                    }
                })
                .setNegativeButton(android.R.string.cancel, null);

        final AlertDialog dialog = builder.create();
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dlg) {
                dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
            }
        });

        return dialog;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mSelected = position;
        mEditText.requestFocus();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }

    private void setupAdapter() {
        List<TvInputInfo> inputs = mTvInputManager.getTvInputList();
        if (inputs.size() < 1) {
            return;
        }
        for (TvInputInfo input : inputs) {
            String id = input.getId();
            String name = Utils.getDisplayNameForInput(getActivity(), input);
            mInputIdMap.put(name, id);
        }
        mDisplayNames = mInputIdMap.keySet().toArray(new String[0]);
        mAdapter.addAll(mDisplayNames);
        mAdapter.notifyDataSetChanged();
    }
}