Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / wpadebug / src / w1 / fi / wpadebug / MainActivity.java
1 /*
2  * wpadebug - wpa_supplicant and Wi-Fi debugging app for Android
3  * Copyright (c) 2013, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 package w1.fi.wpadebug;
10
11 import java.io.BufferedReader;
12 import java.io.InputStreamReader;
13 import java.io.IOException;
14
15 import android.app.Activity;
16 import android.app.AlertDialog;
17 import android.os.Bundle;
18 import android.view.View;
19 import android.content.Intent;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.widget.EditText;
23 import android.widget.Toast;
24 import android.util.Log;
25 import android.net.wifi.WifiManager;
26 import android.net.wifi.WifiInfo;
27 import android.net.wifi.WifiConfiguration;
28 import android.nfc.NdefMessage;
29 import android.nfc.NdefRecord;
30 import android.nfc.NfcAdapter;
31
32 public class MainActivity extends Activity
33 {
34     public final static String EXTRA_MESSAGE = "w1.fi.wpadebug.MESSAGE";
35     private static final String TAG = "wpadebug";
36
37     @Override
38     public void onCreate(Bundle savedInstanceState)
39     {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.main);
42     }
43
44     public void runCommands(View view)
45     {
46         Intent intent = new Intent(this, CommandListActivity.class);
47         startActivity(intent);
48     }
49
50     public void runWpaCommands(View view)
51     {
52         Intent intent = new Intent(this, WpaCommandListActivity.class);
53         startActivity(intent);
54     }
55
56     public void runWpaCredentials(View view)
57     {
58         Intent intent = new Intent(this, WpaCredActivity.class);
59         startActivity(intent);
60     }
61
62     public void runWpaCliCmd(View view)
63     {
64         Intent intent = new Intent(this, DisplayMessageActivity.class);
65         EditText editText = (EditText) findViewById(R.id.edit_cmd);
66         String cmd = editText.getText().toString();
67         if (cmd.trim().length() == 0) {
68             show_alert("wpa_cli command", "Invalid command");
69             return;
70         }
71         wpaCmd(view, cmd);
72     }
73
74     public void wpaLogLevelInfo(View view)
75     {
76         wpaCmd(view, "LOG_LEVEL INFO 1");
77     }
78
79     public void wpaLogLevelDebug(View view)
80     {
81         wpaCmd(view, "LOG_LEVEL DEBUG 1");
82     }
83
84     public void wpaLogLevelExcessive(View view)
85     {
86         wpaCmd(view, "LOG_LEVEL EXCESSIVE 1");
87     }
88
89     private void wpaCmd(View view, String cmd)
90     {
91         Intent intent = new Intent(this, DisplayMessageActivity.class);
92         String message = run("wpa_cli " + cmd);
93         if (message == null)
94             return;
95         intent.putExtra(EXTRA_MESSAGE, message);
96         startActivity(intent);
97     }
98
99     private String run(String cmd)
100     {
101         try {
102             Log.d(TAG, "Running external process: " + cmd);
103             Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/mksh-su", "-c", cmd});
104             BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
105             StringBuffer output = new StringBuffer();
106             int read;
107             char[] buffer = new char[1024];
108             while ((read = reader.read(buffer)) > 0)
109                 output.append(buffer, 0, read);
110             reader.close();
111             proc.waitFor();
112             Log.d(TAG, "External process completed - exitValue " +
113                   proc.exitValue());
114             return output.toString();
115         } catch (IOException e) {
116             show_alert("Could not run external program",
117                        "Execution of an external program failed. " +
118                        "Maybe mksh-su was not installed.");
119             return null;
120         } catch (InterruptedException e) {
121             throw new RuntimeException(e);
122         }
123     }
124
125     private void show_alert(String title, String message)
126     {
127         AlertDialog.Builder alert = new AlertDialog.Builder(this);
128         alert.setTitle(title);
129         alert.setMessage(message);
130         alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
131                 public void onClick(DialogInterface dialog, int id)
132                 {
133                 }
134             });
135         alert.create().show();
136     }
137
138     public void wifiManagerInfo(View view)
139     {
140         Intent intent = new Intent(this, DisplayMessageActivity.class);
141         WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
142         String message = "WifiState: " + manager.getWifiState() + "\n" +
143             "WifiEnabled: " + manager.isWifiEnabled() + "\n" +
144             "pingSupplicant: " + manager.pingSupplicant() + "\n" +
145             "DhcpInfo: " + manager.getDhcpInfo().toString() + "\n";
146         intent.putExtra(EXTRA_MESSAGE, message);
147         startActivity(intent);
148     }
149
150     public void wifiInfo(View view)
151     {
152         Intent intent = new Intent(this, DisplayMessageActivity.class);
153         WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
154         WifiInfo wifi = manager.getConnectionInfo();
155         String message = wifi.toString() + "\n" + wifi.getSupplicantState();
156         intent.putExtra(EXTRA_MESSAGE, message);
157         startActivity(intent);
158     }
159
160     public void wifiConfiguredNetworks(View view)
161     {
162         Intent intent = new Intent(this, DisplayMessageActivity.class);
163         WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
164         StringBuilder sb = new StringBuilder();
165         for (WifiConfiguration n: manager.getConfiguredNetworks())
166             sb.append(n.toString() + "\n");
167         intent.putExtra(EXTRA_MESSAGE, sb.toString());
168         startActivity(intent);
169     }
170
171     public void nfcWpsHandoverRequest(View view)
172     {
173         NfcAdapter nfc;
174         nfc = NfcAdapter.getDefaultAdapter(this);
175         if (nfc == null) {
176             Toast.makeText(this, "NFC is not available",
177                            Toast.LENGTH_LONG).show();
178             return;
179         }
180
181         NdefMessage msg;
182         msg = new NdefMessage(new NdefRecord[] {
183                 NdefRecord.createMime("application/vnd.wfa.wsc",
184                                       new byte[0])
185             });
186
187         nfc.setNdefPushMessage(msg, this);
188         Toast.makeText(this, "NFC push message (WSC) configured",
189                        Toast.LENGTH_LONG).show();
190     }
191 }