Adding database information insertion features in the phone app
[iot2.git] / others / lede-gui / src / main / java / com / example / lede2 / AddDeviceActivity.java
diff --git a/others/lede-gui/src/main/java/com/example/lede2/AddDeviceActivity.java b/others/lede-gui/src/main/java/com/example/lede2/AddDeviceActivity.java
new file mode 100644 (file)
index 0000000..fc4a075
--- /dev/null
@@ -0,0 +1,102 @@
+package com.example.lede2;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
+import android.util.Log;
+import android.view.Gravity;
+import android.view.View;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.TextView;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.*;
+
+
+public class AddDeviceActivity extends AppCompatActivity implements View.OnClickListener,View.OnFocusChangeListener {
+
+    Button doneButton;
+    TextView databaseInfo;
+    TextView databaseAddressInfo;
+    private SSH_MySQL ssh;//Connection object between Android & Host
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_add_device);
+
+        doneButton = (Button) findViewById(R.id.doneButton);
+        databaseInfo = (EditText)findViewById(R.id.textInfo);
+        databaseAddressInfo = (EditText)findViewById(R.id.textInfoAddress);
+
+        doneButton.setOnClickListener(this);
+        databaseInfo.setOnFocusChangeListener(this);
+        databaseAddressInfo.setOnFocusChangeListener(this);
+        ssh = new SSH_MySQL();
+        // Set config text from file for device
+        try {
+            InputStream is = getAssets().open(MainActivity.DEF_ADD_DEVICE_FILE);
+            int size = is.available();
+            byte[] buffer = new byte[size];
+            is.read(buffer);
+            is.close();
+            String text = new String(buffer);
+            databaseInfo.setGravity(Gravity.LEFT);
+            databaseInfo.setText(text);
+            Log.d("LOADINGFILE", "Add device info file is already loaded!");
+        } catch (IOException ex) {
+            Log.d("LOADINGFILE", "Add device info file is NOT loaded!");
+            ex.printStackTrace();
+        }
+        // Set config text from file for device address
+        try {
+            InputStream is = getAssets().open(MainActivity.DEF_ADD_DEVICE_ADDRESS_FILE);
+            int size = is.available();
+            byte[] buffer = new byte[size];
+            is.read(buffer);
+            is.close();
+            String text = new String(buffer);
+            databaseAddressInfo.setGravity(Gravity.LEFT);
+            databaseAddressInfo.setText(text);
+            Log.d("LOADINGFILE", "Add device address info file is already loaded!");
+        } catch (IOException ex) {
+            Log.d("LOADINGFILE", "Add device address info file is NOT loaded!");
+            ex.printStackTrace();
+        }
+
+    }
+
+    @Override
+    public void onClick(View v) {
+        if(v == doneButton){
+            // 1) Create a new file and insert the configuration
+            // 2) Run iotinstaller code for device installation
+            // 3) Remove the existing config file
+            // 4) Repeat 1, 2, and 3 for device address
+            ssh.execute("echo \"" + databaseInfo.getText().toString() + "\" >> " +
+                    MainActivity.DEF_MYSQL_CONFIG_FILE + ";" +
+                    MainActivity.DEF_INSTALL_CMD + " " + MainActivity.DEF_MYSQL_CONFIG_FILE + ";" +
+                    "rm -rf " + MainActivity.DEF_MYSQL_CONFIG_FILE + ";" +
+                    // repeat process for device address
+                    "echo \"" + databaseAddressInfo.getText().toString() + "\" >> " +
+                    MainActivity.DEF_MYSQL_CONFIG_FILE + ";" +
+                    MainActivity.DEF_INSTALL_ADDRESS_CMD + " " + MainActivity.DEF_MYSQL_CONFIG_FILE + ";" +
+                    "rm -rf " + MainActivity.DEF_MYSQL_CONFIG_FILE);
+            finish();
+        }
+    }
+
+    @Override
+    public void onFocusChange(View view, boolean hasFocus) {
+        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+        if (hasFocus) {
+            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
+        } else {
+            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
+        }
+    }
+}