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();
62 InputStream inres
= getResources().openRawResource(R
.raw
.wpa_commands
);
63 read_commands(list
, new Scanner(inres
));
65 ArrayAdapter
<CmdList
> listAdapter
;
66 listAdapter
= new ArrayAdapter
<CmdList
>(this, android
.R
.layout
.simple_list_item_1
, list
);
68 setListAdapter(listAdapter
);
72 protected void onListItemClick(ListView l
, View v
, int position
, long id
)
74 CmdList item
= (CmdList
) getListAdapter().getItem(position
);
75 Toast
.makeText(this, "Running: " + item
.command
,
76 Toast
.LENGTH_SHORT
).show();
77 String message
= run(item
.command
);
80 Intent intent
= new Intent(this, DisplayMessageActivity
.class);
81 intent
.putExtra(MainActivity
.EXTRA_MESSAGE
, message
);
82 startActivity(intent
);
85 private String
run(String cmd
)
88 Process proc
= Runtime
.getRuntime().exec(new String
[]{"/system/bin/mksh-su", "-c", "wpa_cli " + cmd
});
89 BufferedReader reader
= new BufferedReader(new InputStreamReader(proc
.getInputStream()));
90 StringBuffer output
= new StringBuffer();
92 char[] buffer
= new char[1024];
93 while ((read
= reader
.read(buffer
)) > 0)
94 output
.append(buffer
, 0, read
);
97 return output
.toString();
98 } catch (IOException e
) {
99 Toast
.makeText(this, "Could not run command",
100 Toast
.LENGTH_LONG
).show();
102 } catch (InterruptedException e
) {
103 throw new RuntimeException(e
);