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;
35 public CmdList(String _title, String _command)
42 public String toString()
48 public class CommandListActivity extends ListActivity
50 private static final String TAG = "wpadebug";
51 private static final String cmdfile = "/data/local/wpadebug.cmds";
53 private void read_commands(ArrayList<CmdList> list, Scanner in)
56 while (in.hasNext()) {
57 String title = in.next();
58 String cmd = in.nextLine().substring(1);
59 list.add(new CmdList(title, cmd));
65 public void onCreate(Bundle savedInstanceState)
67 super.onCreate(savedInstanceState);
69 ArrayList<CmdList> list = new ArrayList<CmdList>();
73 in = new FileReader(cmdfile);
74 read_commands(list, new Scanner(in));
75 } catch (IOException e) {
76 Toast.makeText(this, "Could not read " + cmdfile,
77 Toast.LENGTH_SHORT).show();
82 inres = getResources().openRawResource(R.raw.shell_commands);
83 read_commands(list, new Scanner(inres));
84 } catch (android.content.res.Resources.NotFoundException e) {
85 Toast.makeText(this, "Could not read internal resource",
86 Toast.LENGTH_SHORT).show();
89 ArrayAdapter<CmdList> listAdapter;
90 listAdapter = new ArrayAdapter<CmdList>(this, android.R.layout.simple_list_item_1, list);
92 setListAdapter(listAdapter);
96 protected void onListItemClick(ListView l, View v, int position, long id)
98 CmdList item = (CmdList) getListAdapter().getItem(position);
99 Toast.makeText(this, "Running: " + item.command,
100 Toast.LENGTH_SHORT).show();
101 String message = run(item.command);
104 Intent intent = new Intent(this, DisplayMessageActivity.class);
105 intent.putExtra(MainActivity.EXTRA_MESSAGE, message);
106 startActivity(intent);
109 private String run(String cmd)
112 Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/mksh-su", "-c", cmd});
113 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
114 StringBuffer output = new StringBuffer();
116 char[] buffer = new char[1024];
117 while ((read = reader.read(buffer)) > 0)
118 output.append(buffer, 0, read);
121 return output.toString();
122 } catch (IOException e) {
123 Toast.makeText(this, "Could not run command",
124 Toast.LENGTH_LONG).show();
126 } catch (InterruptedException e) {
127 throw new RuntimeException(e);