• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.settings.wifi.p2p;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.net.wifi.WpsInfo;
23 import android.net.wifi.p2p.WifiP2pConfig;
24 import android.net.wifi.p2p.WifiP2pDevice;
25 import android.os.Bundle;
26 import android.view.View;
27 import android.widget.AdapterView;
28 import android.widget.CheckBox;
29 import android.widget.Spinner;
30 import android.widget.TextView;
31 
32 import com.android.settings.R;
33 
34 /**
35  * Dialog to setup a p2p connection
36  */
37 public 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 
WifiP2pDialog(Context context, DialogInterface.OnClickListener listener, WifiP2pDevice device)56     public WifiP2pDialog(Context context, DialogInterface.OnClickListener listener,
57             WifiP2pDevice device) {
58         super(context);
59         mListener = listener;
60         mDevice = device;
61     }
62 
getConfig()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
onCreate(Bundle savedInstanceState)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
onItemSelected(AdapterView<?> parent, View view, int position, long id)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
onNothingSelected(AdapterView<?> parent)129     public void onNothingSelected(AdapterView<?> parent) {
130     }
131 
132 }
133