Adding database information insertion features in the phone app
[iot2.git] / others / lede-gui / src / main / java / com / example / lede2 / ListActivity.java
1 package com.example.lede2;\r
2 \r
3 import android.content.Intent;\r
4 import android.support.v7.app.AppCompatActivity;\r
5 import android.os.Bundle;\r
6 import android.util.Log;\r
7 import android.util.SparseBooleanArray;\r
8 import android.view.View;\r
9 import android.widget.AdapterView;\r
10 import android.widget.ArrayAdapter;\r
11 import android.widget.Button;\r
12 import android.widget.EditText;\r
13 import android.widget.ListView;\r
14 import android.widget.TextView;\r
15 import android.widget.Toast;\r
16 \r
17 import java.util.List;\r
18 \r
19 public class ListActivity extends AppCompatActivity implements View.OnClickListener {\r
20 \r
21     Button delete;//delete button in UI\r
22     Button deviceInfo;//select button in UI\r
23     ListView listview;//listview in UI\r
24     SSH ssh;//Connection object between Android & Router\r
25     List<String> tmp;//data structure which has IoT device information already registered on LEDE2\r
26     ArrayAdapter adapter;//adapter between tmp and listview\r
27     String device_info;\r
28     protected String deviceIp;\r
29     TextView ip;\r
30     TextView mac;\r
31 \r
32     @Override\r
33     protected void onCreate(Bundle savedInstanceState) {\r
34         super.onCreate(savedInstanceState);\r
35         setContentView(R.layout.activity_list);\r
36 \r
37         ssh = new SSH();\r
38         try {\r
39             tmp = ssh.execute("-ln").get();\r
40             Thread.sleep(1000);//To execute asyntask in ssh object, we have to sleep main thread\r
41         } catch (Exception e) {\r
42         }\r
43 \r
44         delete = (Button) findViewById(R.id.delete);\r
45         deviceInfo = (Button) findViewById(R.id.deviceInfo);\r
46         listview = (ListView) findViewById(R.id.listView1);\r
47         ip = (TextView) findViewById(R.id.txt_ip);\r
48         mac = (TextView) findViewById(R.id.txt_mac);\r
49 \r
50         adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, tmp);//register tmp array to adapter\r
51 \r
52         delete.setOnClickListener(this);\r
53         deviceInfo.setOnClickListener(this);\r
54         listview.setAdapter(adapter);\r
55         listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {\r
56             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {\r
57                 device_info = listview.getItemAtPosition(position).toString();\r
58         }});\r
59     }\r
60 \r
61     @Override\r
62     public void onClick(View v) {\r
63         if (v == delete) {\r
64             //SparseBooleanArray's data is Ture or False\r
65             SparseBooleanArray checkedItems = listview.getCheckedItemPositions();//to check which devices are checked in listview(check -> true, no check -> false)\r
66             int count = adapter.getCount();//number of items in listview\r
67             String command = "-dn "; //after, +'name '\r
68 \r
69             for (int i = count - 1; i >= 0; i--) {//scan from back\r
70                 //i : index of IoT device which will be removed in tmp array\r
71                 if (checkedItems.get(i)) {//if check\r
72                     String rmName = tmp.get(i).toString();//save the name of checked IoT device\r
73                     command += rmName + " ";//complete command\r
74                     //remove this information on the listview\r
75                     tmp.remove(i);\r
76                     //deviceIp = checkedItems.\r
77                 }\r
78             }\r
79             try {\r
80                 //delete IoT device information in the router by sending command line to router\r
81                 ssh = new SSH();\r
82                 ssh.execute(command);\r
83                 Thread.sleep(1000);//To execute asyntask in ssh object, we have to sleep main thread\r
84             } catch (Exception e) {\r
85                 Log.d("SLEEP EXCEPTION", "SLEEP EXCEPTION occurs in onClick method of ListActivity");\r
86             }\r
87             //update\r
88             adapter.notifyDataSetChanged();\r
89             //delete checked mark in listview\r
90             listview.clearChoices();\r
91 \r
92         } else if (v == deviceInfo) {\r
93             String[] devInfo = device_info.split("\\s+");\r
94             ip.setText(devInfo[1]);\r
95             mac.setText(devInfo[2]);\r
96             Toast t = Toast.makeText(this, device_info, Toast.LENGTH_SHORT);\r
97             t.show();\r
98             /*} else if (v == selectAll) {\r
99             int count;\r
100             count = adapter.getCount();\r
101 \r
102             for (int i = 0; i < count; i++) {\r
103                 listview.setItemChecked(i, true);\r
104             }*/\r
105         }\r
106     }\r
107 }\r