Adding delete relation/communication feature
[iot2.git] / others / lede-gui / src / main / java / com / example / lede2 / RelationActivity.java
1 package com.example.lede2;
2
3 import android.content.Context;
4 import android.os.Bundle;
5 import android.support.v7.app.AppCompatActivity;
6 import android.util.Log;
7 import android.view.Gravity;
8 import android.view.View;
9 import android.view.inputmethod.InputMethodManager;
10 import android.widget.Button;
11 import android.widget.EditText;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15
16 public class RelationActivity extends AppCompatActivity implements View.OnClickListener,View.OnFocusChangeListener {
17
18     Button addButton;
19     Button deleteButton;
20     EditText databaseInfo;
21     EditText idSource;
22     EditText idDestination;
23     private SSH_MySQL ssh;//Connection object between Android & Host
24
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.activity_relation);
29
30         addButton = (Button) findViewById(R.id.addButton);
31         deleteButton = (Button) findViewById(R.id.delButton);
32         databaseInfo = (EditText)findViewById(R.id.textInfoComm);
33         idSource = (EditText)findViewById(R.id.id_source);
34         idDestination = (EditText)findViewById(R.id.id_destination);
35
36         addButton.setOnClickListener(this);
37         deleteButton.setOnClickListener(this);
38         databaseInfo.setOnFocusChangeListener(this);
39         idSource.setOnFocusChangeListener(this);
40         idDestination.setOnFocusChangeListener(this);
41         ssh = new SSH_MySQL();
42         // Set config text from file for device
43         try {
44             InputStream is = getAssets().open(MainActivity.DEF_ADD_DEVICE_COMM_FILE);
45             int size = is.available();
46             byte[] buffer = new byte[size];
47             is.read(buffer);
48             is.close();
49             String text = new String(buffer);
50             databaseInfo.setGravity(Gravity.LEFT);
51             databaseInfo.setText(text);
52             Log.d("LOADINGFILE", "Add comm info file is already loaded!");
53         } catch (IOException ex) {
54             Log.d("LOADINGFILE", "Add comm info file is NOT loaded!");
55             ex.printStackTrace();
56         }
57     }
58
59     @Override
60     public void onClick(View v) {
61         if(v == addButton){
62             // 1) Create a new file and insert the configuration
63             // 2) Run iotinstaller code for communication/relation installation
64             // 3) Remove the existing config file
65             ssh.execute("echo \"" + databaseInfo.getText().toString() + "\" >> " +
66                     MainActivity.DEF_MYSQL_CONFIG_FILE + ";" +
67                     MainActivity.DEF_INSTALL_RELATION_CMD + " " + MainActivity.DEF_MYSQL_CONFIG_FILE + ";" +
68                     "rm -rf " + MainActivity.DEF_MYSQL_CONFIG_FILE);
69             finish();
70         }
71         if(v == deleteButton){
72             // Delete a communication/relation entry
73             ssh.execute(MainActivity.DEF_DELETE_RELATION_CMD + " " + idSource.getText().toString()
74                     + " " + idDestination.getText().toString());
75             finish();
76         }
77     }
78
79     @Override
80     public void onFocusChange(View view, boolean hasFocus) {
81         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
82         if (hasFocus) {
83             imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
84         } else {
85             imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
86         }
87     }
88
89 }