165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.connectivity.setup;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Activity;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Fragment;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Handler;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.text.InputType;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.text.TextUtils;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.text.method.PasswordTransformationMethod;
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.KeyEvent;
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.LayoutInflater;
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.View;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.ViewGroup;
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.inputmethod.InputMethodManager;
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.EditText;
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.ImageView;
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.TextView;
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.R;
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.util.AccessibilityHelper;
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Displays a UI for text input in the "wizard" style.
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * TODO: Merge with EditTextFragment
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class TextInputWizardFragment extends Fragment {
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int INPUT_TYPE_NORMAL = InputType.TYPE_CLASS_TEXT
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            | InputType.TYPE_TEXT_VARIATION_NORMAL;
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int INPUT_TYPE_PASSWORD = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            | InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int INPUT_TYPE_EMAIL = InputType.TYPE_CLASS_TEXT
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int INPUT_TYPE_NO_SUGGESTIONS = InputType.TYPE_CLASS_TEXT
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            | InputType.TYPE_TEXT_VARIATION_NORMAL | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int INPUT_TYPE_NUMERIC = InputType.TYPE_CLASS_NUMBER
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            | InputType.TYPE_NUMBER_VARIATION_NORMAL;
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public interface Listener {
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        /**
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         * Called when text input is complete.
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         *
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         * @param text the text that was input.
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         * @return true if the text is acceptable; false if not.
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         */
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean onTextInputComplete(String text);
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_TITLE = "title";
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_DESCRIPTION = "description";
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_INPUT_TYPE = "input_type";
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_PREFILL = "prefill";
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static TextInputWizardFragment newInstance(
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String title, String description, int inputType, String prefill) {
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        TextInputWizardFragment fragment = new TextInputWizardFragment();
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle args = new Bundle();
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putString(EXTRA_TITLE, title);
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putString(EXTRA_DESCRIPTION, description);
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putInt(EXTRA_INPUT_TYPE, inputType);
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putString(EXTRA_PREFILL, prefill);
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        fragment.setArguments(args);
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return fragment;
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private Handler mHandler;
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private EditText mTextInput;
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) {
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mHandler = new Handler();
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        View view = inflater.inflate(R.layout.account_content_area, container, false);
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        View content = inflater.inflate(R.layout.wifi_content, null);
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        View action = inflater.inflate(R.layout.wifi_text_input, null);
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ((ViewGroup) view.findViewById(R.id.description)).addView(content);
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ((ViewGroup) view.findViewById(R.id.action)).addView(action);
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        TextView titleText = (TextView) content.findViewById(R.id.title_text);
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        TextView descriptionText = (TextView) content.findViewById(R.id.description_text);
9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mTextInput = (EditText) action.findViewById(R.id.text_input);
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle args = getArguments();
10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String title = args.getString(EXTRA_TITLE);
10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String description = args.getString(EXTRA_DESCRIPTION);
10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int inputType = args.getInt(EXTRA_INPUT_TYPE);
10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String prefill = args.getString(EXTRA_PREFILL);
10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean forceFocusable = AccessibilityHelper.forceFocusableViews(getActivity());
10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (title != null) {
10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            titleText.setText(title);
10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            titleText.setVisibility(View.VISIBLE);
10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (forceFocusable) {
11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                titleText.setFocusable(true);
11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                titleText.setFocusableInTouchMode(true);
11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            titleText.setVisibility(View.GONE);
11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (description != null) {
11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            descriptionText.setText(description);
11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            descriptionText.setVisibility(View.VISIBLE);
12065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (forceFocusable) {
12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                descriptionText.setFocusable(true);
12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                descriptionText.setFocusableInTouchMode(true);
12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
12565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            descriptionText.setVisibility(View.GONE);
12665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
12765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if ((inputType & InputType.TYPE_TEXT_VARIATION_PASSWORD) != 0) {
12965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mTextInput.setTransformationMethod(new PasswordTransformationMethod());
13065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
13165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mTextInput.setInputType(inputType);
13265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (prefill != null) {
13465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mTextInput.setText(prefill);
13565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mTextInput.setSelection(mTextInput.getText().length(), mTextInput.getText().length());
13665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
13765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mTextInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
13965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
14065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
14165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (event == null || event.getAction() == KeyEvent.ACTION_UP) {
14265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    Activity a = getActivity();
14365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (a instanceof Listener) {
14465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        return ((Listener) a).onTextInputComplete(v.getText().toString());
14565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
14665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return false;
14765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
14865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return true;  // If we don't return true on ACTION_DOWN, we don't get the ACTION_UP.
14965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
15065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        });
15165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
15265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return view;
15365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
15465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
15565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
15665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onResume() {
15765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onResume();
15865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mHandler.post(new Runnable() {
15965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
16065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void run() {
16165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Activity a = getActivity();
16265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (a != null) {
16365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    InputMethodManager inputMethodManager = (InputMethodManager) a.getSystemService(
16465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            Context.INPUT_METHOD_SERVICE);
16565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    inputMethodManager.showSoftInput(mTextInput, 0);
16665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mTextInput.requestFocus();
16765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
16865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
16965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        });
17065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
17165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
17265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
17365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onPause() {
17465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        InputMethodManager imm = (InputMethodManager) getActivity()
17565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .getSystemService(Context.INPUT_METHOD_SERVICE);
17665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        imm.hideSoftInputFromWindow(mTextInput.getWindowToken(), 0);
17765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onPause();
17865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
17965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
180