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