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 
read_commands(ArrayList<CmdList> list, Scanner in)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
onCreate(Bundle savedInstanceState)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;
63 	try {
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();
69 	}
70 
71 	ArrayAdapter<CmdList> listAdapter;
72 	listAdapter = new ArrayAdapter<CmdList>(this, android.R.layout.simple_list_item_1, list);
73 
74 	setListAdapter(listAdapter);
75     }
76 
77     @Override
onListItemClick(ListView l, View v, int position, long id)78     protected void onListItemClick(ListView l, View v, int position, long id)
79     {
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);
84 	if (message == null)
85 	    return;
86 	Intent intent = new Intent(this, DisplayMessageActivity.class);
87 	intent.putExtra(MainActivity.EXTRA_MESSAGE, message);
88 	startActivity(intent);
89     }
90 
run(String cmd)91     private String run(String cmd)
92     {
93 	try {
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();
97 	    int read;
98 	    char[] buffer = new char[1024];
99 	    while ((read = reader.read(buffer)) > 0)
100 		output.append(buffer, 0, read);
101 	    reader.close();
102 	    proc.waitFor();
103 	    return output.toString();
104 	} catch (IOException e) {
105 	    Toast.makeText(this, "Could not run command",
106 			   Toast.LENGTH_LONG).show();
107 	    return null;
108 	} catch (InterruptedException e) {
109 	    throw new RuntimeException(e);
110 	}
111     }
112 }
113