1 package com.example.lede2;
\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
12 import java.util.List;
\r
14 public class ListActivity extends AppCompatActivity implements View.OnClickListener {
\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
24 protected void onCreate(Bundle savedInstanceState) {
\r
25 super.onCreate(savedInstanceState);
\r
26 setContentView(R.layout.activity_list);
\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
35 delete = (Button) findViewById(R.id.delete);
\r
36 selectAll = (Button) findViewById(R.id.selectAll);
\r
37 listview = (ListView) findViewById(R.id.listView1);
\r
39 adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, tmp);//register tmp array to adapter
\r
41 delete.setOnClickListener(this);
\r
42 selectAll.setOnClickListener(this);
\r
43 listview.setAdapter(adapter);
\r
47 public void onClick(View v) {
\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
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
64 //delete IoT device information in the router by sending command line to router
\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
73 adapter.notifyDataSetChanged();
\r
75 //delete checked mark in listview
\r
76 listview.clearChoices();
\r
78 } else if (v == selectAll) {
\r
80 count = adapter.getCount();
\r
82 for (int i = 0; i < count; i++) {
\r
83 listview.setItemChecked(i, true);
\r