]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpadebug/src/w1/fi/wpadebug/WpaCommandListActivity.java
wpadebug: Add generic control interface command mechanism
[thirdparty/hostap.git] / wpadebug / src / w1 / fi / wpadebug / WpaCommandListActivity.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.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;
18
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;
29
30 public class WpaCommandListActivity extends ListActivity
31 {
32 private static final String TAG = "wpadebug";
33 private static final String cmdfile = "/data/local/wpadebug.wpacmds";
34
35 private void read_commands(ArrayList<CmdList> list, Scanner in)
36 {
37 in.useDelimiter("@");
38 while (in.hasNext()) {
39 String title = in.next();
40 String cmd = in.nextLine().substring(1);
41 list.add(new CmdList(title, cmd));
42 }
43 in.close();
44 }
45
46 @Override
47 public void onCreate(Bundle savedInstanceState)
48 {
49 super.onCreate(savedInstanceState);
50
51 ArrayList<CmdList> list = new ArrayList<CmdList>();
52
53 FileReader in;
54 try {
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();
60 }
61
62 InputStream inres = getResources().openRawResource(R.raw.wpa_commands);
63 read_commands(list, new Scanner(inres));
64
65 ArrayAdapter<CmdList> listAdapter;
66 listAdapter = new ArrayAdapter<CmdList>(this, android.R.layout.simple_list_item_1, list);
67
68 setListAdapter(listAdapter);
69 }
70
71 @Override
72 protected void onListItemClick(ListView l, View v, int position, long id)
73 {
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);
78 if (message == null)
79 return;
80 Intent intent = new Intent(this, DisplayMessageActivity.class);
81 intent.putExtra(MainActivity.EXTRA_MESSAGE, message);
82 startActivity(intent);
83 }
84
85 private String run(String cmd)
86 {
87 try {
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();
91 int read;
92 char[] buffer = new char[1024];
93 while ((read = reader.read(buffer)) > 0)
94 output.append(buffer, 0, read);
95 reader.close();
96 proc.waitFor();
97 return output.toString();
98 } catch (IOException e) {
99 Toast.makeText(this, "Could not run command",
100 Toast.LENGTH_LONG).show();
101 return null;
102 } catch (InterruptedException e) {
103 throw new RuntimeException(e);
104 }
105 }
106 }