1 package com.example.lede2;
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;
13 import java.io.IOException;
14 import java.io.InputStream;
16 public class RelationActivity extends AppCompatActivity implements View.OnClickListener,View.OnFocusChangeListener {
20 EditText databaseInfo;
22 EditText idDestination;
23 private SSH_MySQL ssh;//Connection object between Android & Host
26 protected void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.activity_relation);
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);
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
44 InputStream is = getAssets().open(MainActivity.DEF_ADD_DEVICE_COMM_FILE);
45 int size = is.available();
46 byte[] buffer = new byte[size];
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!");
60 public void onClick(View v) {
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);
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());
80 public void onFocusChange(View view, boolean hasFocus) {
81 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
83 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
85 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);