1/*
2 * Copyright (C) 2011 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.p2p;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.net.wifi.WpsInfo;
23import android.net.wifi.p2p.WifiP2pConfig;
24import android.net.wifi.p2p.WifiP2pDevice;
25import android.os.Bundle;
26import android.view.View;
27import android.widget.AdapterView;
28import android.widget.CheckBox;
29import android.widget.Spinner;
30import android.widget.TextView;
31
32import com.android.settings.R;
33
34/**
35 * Dialog to setup a p2p connection
36 */
37public class WifiP2pDialog extends AlertDialog implements AdapterView.OnItemSelectedListener {
38
39    static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
40
41    private final DialogInterface.OnClickListener mListener;
42
43    private View mView;
44    private TextView mDeviceName;
45    private TextView mDeviceAddress;
46
47    /* These values come from "wifi_p2p_wps_setup" resource array */
48    private static final int WPS_PBC = 0;
49    private static final int WPS_KEYPAD = 1;
50    private static final int WPS_DISPLAY = 2;
51
52    private int mWpsSetupIndex = WPS_PBC; //default is pbc
53
54    WifiP2pDevice mDevice;
55
56    public WifiP2pDialog(Context context, DialogInterface.OnClickListener listener,
57            WifiP2pDevice device) {
58        super(context);
59        mListener = listener;
60        mDevice = device;
61    }
62
63    public WifiP2pConfig getConfig() {
64        WifiP2pConfig config = new WifiP2pConfig();
65        config.deviceAddress = mDeviceAddress.getText().toString();
66        config.wps = new WpsInfo();
67        switch (mWpsSetupIndex) {
68            case WPS_PBC:
69                config.wps.setup = WpsInfo.PBC;
70                break;
71            case WPS_KEYPAD:
72                config.wps.setup = WpsInfo.KEYPAD;
73                config.wps.pin = ((TextView) mView.findViewById(R.id.wps_pin)).
74                        getText().toString();
75                break;
76            case WPS_DISPLAY:
77                config.wps.setup = WpsInfo.DISPLAY;
78                break;
79            default:
80                config.wps.setup = WpsInfo.PBC;
81                break;
82        }
83        return config;
84    }
85
86    @Override
87    protected void onCreate(Bundle savedInstanceState) {
88
89        mView = getLayoutInflater().inflate(R.layout.wifi_p2p_dialog, null);
90        Spinner mWpsSetup = ((Spinner) mView.findViewById(R.id.wps_setup));
91
92        setView(mView);
93        setInverseBackgroundForced(true);
94
95        Context context = getContext();
96
97        setTitle(R.string.wifi_p2p_settings_title);
98        mDeviceName = (TextView) mView.findViewById(R.id.device_name);
99        mDeviceAddress = (TextView) mView.findViewById(R.id.device_address);
100
101        setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_connect), mListener);
102        setButton(DialogInterface.BUTTON_NEGATIVE,
103                    context.getString(R.string.wifi_cancel), mListener);
104
105        if (mDevice != null) {
106            mDeviceName.setText(mDevice.deviceName);
107            mDeviceAddress.setText(mDevice.deviceAddress);
108            mWpsSetup.setSelection(mWpsSetupIndex); //keep pbc as default
109       }
110
111        mWpsSetup.setOnItemSelectedListener(this);
112
113        super.onCreate(savedInstanceState);
114    }
115
116    @Override
117    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
118        mWpsSetupIndex = position;
119
120        if (mWpsSetupIndex == WPS_KEYPAD) {
121            mView.findViewById(R.id.wps_pin_entry).setVisibility(View.VISIBLE);
122        } else {
123            mView.findViewById(R.id.wps_pin_entry).setVisibility(View.GONE);
124        }
125        return;
126    }
127
128    @Override
129    public void onNothingSelected(AdapterView<?> parent) {
130    }
131
132}
133