Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / wpadebug / src / w1 / fi / wpadebug / CommandListActivity.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 class CmdList
31 {
32     String title;
33     String command;
34
35     public CmdList(String _title, String _command)
36     {
37         title = _title;
38         command = _command;
39     }
40
41     @Override
42     public String toString()
43     {
44         return title;
45     }
46 }
47
48 public class CommandListActivity extends ListActivity
49 {
50     private static final String TAG = "wpadebug";
51     private static final String cmdfile = "/data/local/wpadebug.cmds";
52
53     private void read_commands(ArrayList<CmdList> list, Scanner in)
54     {
55         in.useDelimiter("@");
56         while (in.hasNext()) {
57             String title = in.next();
58             String cmd = in.nextLine().substring(1);
59             list.add(new CmdList(title, cmd));
60         }
61         in.close();
62     }
63
64     @Override
65     public void onCreate(Bundle savedInstanceState)
66     {
67         super.onCreate(savedInstanceState);
68
69         ArrayList<CmdList> list = new ArrayList<CmdList>();
70
71         FileReader in;
72         try {
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();
78         }
79
80         InputStream inres;
81         try {
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();
87         }
88
89         ArrayAdapter<CmdList> listAdapter;
90         listAdapter = new ArrayAdapter<CmdList>(this, android.R.layout.simple_list_item_1, list);
91
92         setListAdapter(listAdapter);
93     }
94
95     @Override
96     protected void onListItemClick(ListView l, View v, int position, long id)
97     {
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);
102         if (message == null)
103             return;
104         Intent intent = new Intent(this, DisplayMessageActivity.class);
105         intent.putExtra(MainActivity.EXTRA_MESSAGE, message);
106         startActivity(intent);
107     }
108
109     private String run(String cmd)
110     {
111         try {
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();
115             int read;
116             char[] buffer = new char[1024];
117             while ((read = reader.read(buffer)) > 0)
118                 output.append(buffer, 0, read);
119             reader.close();
120             proc.waitFor();
121             return output.toString();
122         } catch (IOException e) {
123             Toast.makeText(this, "Could not run command",
124                            Toast.LENGTH_LONG).show();
125             return null;
126         } catch (InterruptedException e) {
127             throw new RuntimeException(e);
128         }
129     }
130 }