WifiConfigUiForSetupWizardXL.java revision 115ea8aa37d82bda1e3328a9af776d6941d498e7
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.app.Activity;
22import android.content.Context;
23import android.os.Handler;
24import android.util.Log;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.View.OnFocusChangeListener;
28import android.view.ViewGroup;
29import android.view.inputmethod.InputMethodManager;
30import android.widget.Button;
31import android.widget.EditText;
32
33/**
34 * Shows simplified UI for configuring a wifi network. Used only in SetupWizard for XLarge
35 * screen.
36 */
37public class WifiConfigUiForSetupWizardXL implements WifiConfigUiBase, OnFocusChangeListener {
38    private static final String TAG = "SetupWizard";
39
40    private Button mConnectButton;
41    private Button mForgetButton;
42    private Button mCancelButton;
43
44    private final Activity mActivity;
45    private View mView;
46    private WifiConfigController mController;
47    private AccessPoint mAccessPoint;
48    private boolean mEdit;
49    private Handler mHandler = new Handler();
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            Activity activity, ViewGroup parent, AccessPoint accessPoint, boolean edit) {
62        mActivity = activity;
63        mConnectButton = (Button)activity.findViewById(R.id.wifi_setup_connect);
64        mForgetButton = (Button)activity.findViewById(R.id.wifi_setup_forget);
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, false);
71        mController = new WifiConfigController(this, mView, mAccessPoint, edit);
72
73        // Set Focus to password View.
74        final View viewToBeFocused = mView.findViewById(R.id.password);
75        if (viewToBeFocused != null && viewToBeFocused.getVisibility() == View.VISIBLE &&
76                viewToBeFocused instanceof EditText) {
77            // After acquiring the focus, we show software keyboard.
78            viewToBeFocused.setOnFocusChangeListener(this);
79            final boolean requestFocusResult = viewToBeFocused.requestFocus();
80            Log.i(TAG, String.format("Focus request  %s.",
81                    (requestFocusResult ? "successful" : "failed")));
82            if (!requestFocusResult) {
83                viewToBeFocused.setOnFocusChangeListener(null);
84            }
85        }
86    }
87
88    public View getView() {
89        return mView;
90    }
91
92    public AccessPoint getAccessPoint() {
93        return mAccessPoint;
94    }
95
96    @Override
97    public WifiConfigController getController() {
98        return mController;
99    }
100
101    @Override
102    public boolean isEdit() {
103        return mEdit;
104    }
105
106    @Override
107    public LayoutInflater getLayoutInflater() {
108        return mInflater;
109    }
110
111    @Override
112    public Button getSubmitButton() {
113        return mConnectButton;
114    }
115
116    @Override
117    public Button getForgetButton() {
118        return mForgetButton;
119    }
120
121    @Override
122    public Button getCancelButton() {
123        return mCancelButton;
124    }
125
126    @Override
127    public void setSubmitButton(CharSequence text) {
128        mConnectButton.setVisibility(View.VISIBLE);
129        mConnectButton.setText(text);
130
131        // test
132        mForgetButton.setVisibility(View.GONE);
133    }
134
135    @Override
136    public void setForgetButton(CharSequence text) {
137        // In XL setup screen, we won't show Forget button for simplifying the UI.
138        // mForgetButton.setVisibility(View.VISIBLE);
139        // mForgetButton.setText(text);
140    }
141
142    @Override
143    public void setCancelButton(CharSequence text) {
144        mCancelButton.setVisibility(View.VISIBLE);
145        // We don't want "cancel" label given from caller.
146        // mCancelButton.setText(text);
147    }
148
149    @Override
150    public Context getContext() {
151        return mActivity;
152    }
153
154    @Override
155    public void setTitle(int id) {
156        Log.d(TAG, "Ignoring setTitle");
157    }
158
159    @Override
160    public void setTitle(CharSequence title) {
161        Log.d(TAG, "Ignoring setTitle");
162    }
163
164    private static class FocusRunnable implements Runnable {
165        final InputMethodManager mInputMethodManager;
166        final View mViewToBeFocused;
167        public FocusRunnable(Context context, View viewToBeFocused) {
168            mInputMethodManager = (InputMethodManager)
169                    context.getSystemService(Context.INPUT_METHOD_SERVICE);
170            mViewToBeFocused = viewToBeFocused;
171        }
172
173        @Override
174        public void run() {
175            // mInputMethodManager.focusIn(mViewToBeFocused);
176            final boolean showSoftInputResult =
177                    mInputMethodManager.showSoftInput(mViewToBeFocused, 0);
178            if (!showSoftInputResult) {
179                Log.w(TAG, "Failed to show software keyboard ");
180            }
181        }
182    }
183
184    @Override
185    public void onFocusChange(View view, boolean hasFocus) {
186        view.setOnFocusChangeListener(null);
187        if (hasFocus) {
188            mHandler.post(new FocusRunnable(mActivity, view));
189        }
190    }
191}