1/*
2 * Copyright (C) 2010 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.wifi;
18
19import com.android.settings.R;
20
21import android.content.Context;
22import android.os.Handler;
23import android.util.Log;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.View.OnFocusChangeListener;
27import android.view.ViewGroup;
28import android.view.inputmethod.InputMethodManager;
29import android.widget.Button;
30import android.widget.EditText;
31
32/**
33 * Shows simplified UI for configuring a wifi network. Used only in SetupWizard for XLarge
34 * screen.
35 */
36public class WifiConfigUiForSetupWizardXL implements WifiConfigUiBase, OnFocusChangeListener {
37    private static final String TAG = "SetupWizard";
38
39    private Button mConnectButton;
40    private Button mCancelButton;
41
42    private final WifiSettingsForSetupWizardXL mActivity;
43    private View mView;
44    private WifiConfigController mController;
45    private AccessPoint mAccessPoint;
46    private boolean mEdit;
47    private Handler mHandler = new Handler();
48
49    private final InputMethodManager mInputMethodManager;
50
51    private LayoutInflater mInflater;
52
53    /**
54     * @param activity Activity which creates this object.
55     * @param parent Parent ViewGroup (typically some layout) holding a view object created by
56     * this object
57     * @param accessPoint target AccessPoint to be configured.
58     * @param edit
59     */
60    public WifiConfigUiForSetupWizardXL(
61            WifiSettingsForSetupWizardXL activity, ViewGroup parent,
62            AccessPoint accessPoint, boolean edit) {
63        mActivity = activity;
64        mConnectButton = (Button)activity.findViewById(R.id.wifi_setup_connect);
65        mCancelButton = (Button)activity.findViewById(R.id.wifi_setup_cancel);
66        mAccessPoint = accessPoint;
67        mEdit = edit;
68        mInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
69
70        mView = mInflater.inflate(R.layout.wifi_config_ui_for_setup_wizard, parent, true);
71        mController = new WifiConfigController(this, mView, mAccessPoint, edit);
72
73        mInputMethodManager = (InputMethodManager)
74                activity.getSystemService(Context.INPUT_METHOD_SERVICE);
75
76        if (mView.findViewById(R.id.security_fields).getVisibility() == View.VISIBLE) {
77            requestFocusAndShowKeyboard(R.id.password);
78        } else if (mView.findViewById(R.id.type_ssid).getVisibility() == View.VISIBLE) {
79            // Add Network flow.
80            requestFocusAndShowKeyboard(R.id.ssid);
81        }
82    }
83
84    /**
85     * @param editViewId must be EditView
86     */
87    public void requestFocusAndShowKeyboard(int editViewId) {
88        // Set Focus to password View.
89        final View viewToBeFocused = mView.findViewById(editViewId);
90        if (viewToBeFocused == null) {
91            Log.w(TAG, "password field to be focused not found.");
92        } else if (!(viewToBeFocused instanceof EditText)) {
93            Log.w(TAG, "password field is not EditText");
94        } else {
95            if (viewToBeFocused.isFocused()) {
96                Log.i(TAG, "Already focused");
97                if (!mInputMethodManager.showSoftInput(viewToBeFocused, 0)) {
98                    Log.w(TAG, "Failed to show SoftInput");
99                }
100            } else {
101                // After acquiring the focus, we show software keyboard.
102                viewToBeFocused.setOnFocusChangeListener(this);
103                final boolean requestFocusResult = viewToBeFocused.requestFocus();
104                Log.i(TAG, String.format("Focus request: %s",
105                        (requestFocusResult ? "successful" : "failed")));
106                if (!requestFocusResult) {
107                    viewToBeFocused.setOnFocusChangeListener(null);
108                }
109            }
110        }
111    }
112
113    public View getView() {
114        return mView;
115    }
116
117    public AccessPoint getAccessPoint() {
118        return mAccessPoint;
119    }
120
121    @Override
122    public WifiConfigController getController() {
123        return mController;
124    }
125
126    @Override
127    public boolean isEdit() {
128        return mEdit;
129    }
130
131    @Override
132    public LayoutInflater getLayoutInflater() {
133        return mInflater;
134    }
135
136    @Override
137    public Button getSubmitButton() {
138        return mConnectButton;
139    }
140
141    @Override
142    public Button getForgetButton() {
143        return null;
144    }
145
146    @Override
147    public Button getCancelButton() {
148        return mCancelButton;
149    }
150
151    @Override
152    public void setSubmitButton(CharSequence text) {
153        mConnectButton.setVisibility(View.VISIBLE);
154        mConnectButton.setText(text);
155    }
156
157    @Override
158    public void setForgetButton(CharSequence text) {
159        // In XL setup screen, we won't show Forget button for simplifying the UI.
160    }
161
162    @Override
163    public void setCancelButton(CharSequence text) {
164        mCancelButton.setVisibility(View.VISIBLE);
165        // We don't want "cancel" label given from caller.
166        // mCancelButton.setText(text);
167    }
168
169    @Override
170    public Context getContext() {
171        return mActivity;
172    }
173
174    @Override
175    public void setTitle(int id) {
176        Log.d(TAG, "Ignoring setTitle");
177    }
178
179    @Override
180    public void setTitle(CharSequence title) {
181        Log.d(TAG, "Ignoring setTitle");
182    }
183
184    private class FocusRunnable implements Runnable {
185        final View mViewToBeFocused;
186        public FocusRunnable(View viewToBeFocused) {
187            mViewToBeFocused = viewToBeFocused;
188        }
189
190        @Override
191        public void run() {
192            // mInputMethodManager.focusIn(mViewToBeFocused);
193            final boolean showSoftInputResult =
194                    mInputMethodManager.showSoftInput(mViewToBeFocused, 0);
195            if (showSoftInputResult) {
196                mActivity.setPaddingVisibility(View.GONE);
197            } else {
198                Log.w(TAG, "Failed to show software keyboard ");
199            }
200        }
201    }
202
203    @Override
204    public void onFocusChange(View view, boolean hasFocus) {
205        view.setOnFocusChangeListener(null);
206        if (hasFocus) {
207            mHandler.post(new FocusRunnable(view));
208        }
209    }
210}