package com.example.student.ledssh; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.File; public class MainActivitySSH extends AppCompatActivity { protected EditText etUsername; protected EditText etHost; protected EditText etPort; protected EditText etPrivateKeyFileName; protected EditText etPassphrase; protected TextView tvKeyFilePath; protected Intent intentLED; public final static String USERNAME_KEY = "com.example.student.ledssh.USERNAME"; public final static String HOST_KEY = "com.example.student.ledssh.HOST"; public final static String PORT_KEY = "com.example.student.ledssh.PORT"; public final static String PRIVATEKEYFILENAME_KEY = "com.example.student.ledssh.PRIVATEKEYFILENAME"; public final static String PASSPHRASE_KEY = "com.example.student.ledssh.PASSPHRASE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_ssh); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); etUsername = (EditText) findViewById(R.id.etUsername); etHost = (EditText) findViewById(R.id.etHost); etPort = (EditText) findViewById(R.id.etPort); etPrivateKeyFileName = (EditText) findViewById(R.id.etPrivateKeyFileName); etPassphrase = (EditText) findViewById(R.id.etPassphrase); tvKeyFilePath = (TextView) findViewById(R.id.keyFilePath); tvKeyFilePath.setText(Environment.getExternalStorageDirectory() .getAbsolutePath()); intentLED = new Intent(this, OperateLEDs.class); System.out.println(getString(R.string.enter_key_passphrase)); System.out.println(getString(R.string.continue_text)); System.out.println(getString(R.string.sending_command_text)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main_activity_ssh, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public String checkConfig() { //String response = ""; System.out.println("Starting check config"); String username = etUsername.getText().toString(); System.out.print("username: "); System.out.println(username); if (username.equals("")) { return "Please enter a username."; } String host = etHost.getText().toString(); System.out.print("host: "); System.out.println(host); if (host.equals("")) { return "Please enter a host name or an IP address."; } // Look up the host as error check System.out.println("Checking port."); Integer port; try { port = Integer.valueOf(etPort.getText().toString()); System.out.print("port: "); System.out.println(port); if (port <= 0 || port > 65536) { return "Please enter a port number between 1 and 65535."; } } catch (NumberFormatException ex) { String s = ex.toString(); System.out.println(s); return "Please enter a port number between 1 and 65535."; } String privateKeyFileName = etPrivateKeyFileName.getText().toString(); System.out.print("privateKeyFileName: "); System.out.println(privateKeyFileName); if (privateKeyFileName.equals("")) { return "Please enter a private key file name (relative to the SD card root path)."; } // look for the file and error check String state = Environment.getExternalStorageState(); String fileName = ""; boolean dontHavePKF = true; File idFile; if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { File currentFolder; currentFolder = new File(Environment.getExternalStorageDirectory() .getAbsolutePath()); System.out.println(currentFolder); idFile = new File(currentFolder, privateKeyFileName); if (idFile.canRead()) { dontHavePKF = false; } fileName = idFile.getAbsolutePath(); } if (dontHavePKF) { Toast.makeText(getApplicationContext(), "Can't read: " + fileName, Toast.LENGTH_LONG).show(); return "Could not read the private key file: " + fileName; } String passphrase = etPassphrase.getText().toString(); System.out.println("passphrase: *****"); if (passphrase.equals("")) { return "Please enter a passphrase"; } System.out.println("Setting intent extras."); intentLED.putExtra(USERNAME_KEY, username); intentLED.putExtra(HOST_KEY, host); intentLED.putExtra(PORT_KEY, port); intentLED.putExtra(PRIVATEKEYFILENAME_KEY, privateKeyFileName); intentLED.putExtra(PASSPHRASE_KEY, passphrase); return ""; } public String operateLEDs(View view) { if (view.getId() == R.id.btnOperateLEDs) { String result = checkConfig(); if (!result.equals("")) { Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); return result; } else { System.out.print("Result is blank: "); System.out.println(result); } if (intentLED != null) { startActivity(intentLED); } else { System.out.println("Null intent, activity not started"); } } return ""; } // shared preferences @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override protected void onStop() { super.onStop(); //etPassphrase.setText(""); } }