2 * wpadebug - wpa_supplicant and Wi-Fi debugging app for Android
3 * Copyright (c) 2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 package w1.fi.wpadebug;
11 import java.util.ArrayList;
12 import java.util.Scanner;
13 import java.io.FileReader;
14 import java.io.BufferedReader;
15 import java.io.InputStreamReader;
16 import java.io.InputStream;
17 import java.io.IOException;
19 import android.app.ListActivity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.Parcelable;
23 import android.view.View;
24 import android.widget.ListView;
25 import android.widget.ArrayAdapter;
26 import android.widget.Toast;
27 import android.text.method.ScrollingMovementMethod;
28 import android.util.Log;
30 public class WpaCommandListActivity extends ListActivity
32 private static final String TAG = "wpadebug";
33 private static final String cmdfile = "/data/local/wpadebug.wpacmds";
35 private void read_commands(ArrayList<CmdList> list, Scanner in)
38 while (in.hasNext()) {
39 String title = in.next();
40 String cmd = in.nextLine().substring(1);
41 list.add(new CmdList(title, cmd));
47 public void onCreate(Bundle savedInstanceState)
49 super.onCreate(savedInstanceState);
51 ArrayList<CmdList> list = new ArrayList<CmdList>();
55 in = new FileReader(cmdfile);
56 read_commands(list, new Scanner(in));
57 } catch (IOException e) {
58 Toast.makeText(this, "Could not read " + cmdfile,
59 Toast.LENGTH_SHORT).show();
64 inres = getResources().openRawResource(R.raw.wpa_commands);
65 read_commands(list, new Scanner(inres));
66 } catch (android.content.res.Resources.NotFoundException e) {
67 Toast.makeText(this, "Could not read internal resource",
68 Toast.LENGTH_SHORT).show();
71 ArrayAdapter<CmdList> listAdapter;
72 listAdapter = new ArrayAdapter<CmdList>(this, android.R.layout.simple_list_item_1, list);
74 setListAdapter(listAdapter);
78 protected void onListItemClick(ListView l, View v, int position, long id)
80 CmdList item = (CmdList) getListAdapter().getItem(position);
81 Toast.makeText(this, "Running: " + item.command,
82 Toast.LENGTH_SHORT).show();
83 String message = run(item.command);
86 Intent intent = new Intent(this, DisplayMessageActivity.class);
87 intent.putExtra(MainActivity.EXTRA_MESSAGE, message);
88 startActivity(intent);
91 private String run(String cmd)
94 Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/mksh-su", "-c", "wpa_cli " + cmd});
95 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
96 StringBuffer output = new StringBuffer();
98 char[] buffer = new char[1024];
99 while ((read = reader.read(buffer)) > 0)
100 output.append(buffer, 0, read);
103 return output.toString();
104 } catch (IOException e) {
105 Toast.makeText(this, "Could not run command",
106 Toast.LENGTH_LONG).show();
108 } catch (InterruptedException e) {
109 throw new RuntimeException(e);