--- /dev/null
+package com.example.lede2;\r
+\r
+import android.content.Context;\r
+import android.content.Intent;\r
+import android.net.ConnectivityManager;\r
+import android.net.NetworkInfo;\r
+import android.net.wifi.WifiInfo;\r
+import android.net.wifi.WifiManager;\r
+import android.support.v7.app.AppCompatActivity;\r
+import android.os.Bundle;\r
+import android.view.View;\r
+import android.view.inputmethod.InputMethodManager;\r
+import android.widget.Button;\r
+import android.widget.EditText;\r
+import android.widget.TextView;\r
+import android.widget.Toast;\r
+import android.util.Log;\r
+\r
+import java.util.Random;\r
+\r
+public class EnrollDeviceActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {\r
+\r
+ private static final int REQUEST_RESULT = 1001;\r
+ Button done;//Done button in UI\r
+ Button wifi;//wifi button in UI\r
+ SSH ssh;//Connection object between Android & Router\r
+ Context context;\r
+\r
+ TextView psk;//red letter in UI\r
+ String newpsk;//same as psk (different data type)\r
+\r
+ EditText name;//device name newly registered in UI\r
+ String deviceName;//same as name(different data type)\r
+\r
+ @Override\r
+ protected void onCreate(Bundle savedInstanceState) {\r
+ super.onCreate(savedInstanceState);\r
+ setContentView(R.layout.activity_enroll_device);\r
+\r
+ ssh = new SSH();\r
+ psk = (TextView) findViewById(R.id.add_psk);\r
+ done = (Button) findViewById(R.id.done);\r
+ wifi = (Button) findViewById(R.id.wifi);\r
+ name = (EditText) findViewById(R.id.name);\r
+\r
+ done.setOnClickListener(this);\r
+ wifi.setOnClickListener(this);\r
+ name.setOnFocusChangeListener(this);\r
+\r
+ //Make random password and show the password through EditText\r
+ newpsk = generateRandomPassword();\r
+ psk.setText(newpsk);\r
+ // execute shell script (script's function -> change router password to newpsk)\r
+ ssh.execute("-ch " + newpsk);\r
+ try {//To execute asyntask in ssh object, we have to sleep main thread\r
+ Thread.sleep(1000);\r
+ } catch (Exception e) {\r
+ Log.d("SLEEP EXCEPTION", "SLEEP EXCEPTION occurs in onCreate method of EnrollDeviceActivity");\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void onFocusChange(View view, boolean hasFocus) {//function not to modify randomly generated password for newly registered device\r
+ InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);\r
+ if (hasFocus) {\r
+ imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);\r
+ } else {\r
+ imm.hideSoftInputFromWindow(view.getWindowToken(), 0);\r
+ }\r
+ }\r
+\r
+ boolean isNetworkAvailable() {//check whether wifi connection is or not\r
+ Context context = getApplicationContext();\r
+ ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);\r
+ NetworkInfo activeNetwork = cm.getActiveNetworkInfo();\r
+ boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();\r
+ return isConnected;\r
+ }\r
+\r
+ @Override\r
+ public void onClick(View v) {\r
+ if (v == done) {\r
+ //Users try to execute shell scripts by pushing button,\r
+ //but problem could occur(application stop) if user push the button without wifi connection\r
+ //As a result, below function have to be implemented.\r
+ if (isNetworkAvailable() == false) {//without wifi connection\r
+ Toast t = Toast.makeText(this, R.string.connect, Toast.LENGTH_SHORT);\r
+ t.show();\r
+ startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));\r
+ } else {//with wifi connection\r
+ deviceName = name.getText().toString();\r
+ if(deviceName.equals("")){\r
+ Toast t = Toast.makeText(this, R.string.empty_name,Toast.LENGTH_SHORT);\r
+ t.show();\r
+ return;\r
+ }\r
+ context = getApplicationContext();\r
+ if (getWifiName(context).equals("\""+ ConfigActivity.RSSID +"\"")) {//if wifi name is LEDE2\r
+ String networkPass = psk.getText().toString();//random password\r
+ ssh = new SSH();\r
+\r
+ // execute shell script (script's function -> Save contents(Mac,Ip,Key,Name) on hostapd-psk)\r
+ ssh.execute("-co " + networkPass + " " + deviceName +" "+ ConfigActivity.RPWD);//review!!!!!!\r
+ try {//To execute asyntask in ssh object, we have to sleep main thread\r
+ Thread.sleep(1000);\r
+ } catch (Exception e) {\r
+ Log.d("SLEEP EXCEPTION", "SLEEP EXCEPTION occurs in onClick method of EnrollDeviceActivity");\r
+ }\r
+ finish();//Go back to the Main Activity\r
+ } else { //if name of wifi is not LEDE2 -> go to wifi configuration screen to change wifi type\r
+ Toast t = Toast.makeText(this, R.string.try_again, Toast.LENGTH_SHORT);\r
+ t.show();\r
+ startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));\r
+ }\r
+ }\r
+ } else if (v == wifi) {\r
+ startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));\r
+ }\r
+ }\r
+\r
+ /******************************************************************************************************************************************************************/\r
+ //Detect you are connected to a specific network.\r
+ /******************************************************************************************************************************************************************/\r
+ public String getWifiName(Context context) {\r
+ WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);\r
+ if (manager.isWifiEnabled()) {\r
+ WifiInfo wifiInfo = manager.getConnectionInfo();\r
+ if (wifiInfo != null) {\r
+ NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());\r
+ if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR) {\r
+ return wifiInfo.getSSID();\r
+ }\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /******************************************************************************************************************************************************************/\r
+ // Generate a random password and return it\r
+ /******************************************************************************************************************************************************************/\r
+ public String generateRandomPassword() {\r
+ String password = new String();\r
+\r
+ StringBuffer rndpassword = new StringBuffer();\r
+ Random rnd = new Random();\r
+ int digitnum = 20;\r
+ // Generate random 20digit password with upper / lower case alphabet + numbers\r
+ // There are 10 int nums, 26 lower alphabets, 26 upper alphabets. Total 62\r
+ // So 2/12 possiblity of int, 5/12 lower, 5/12 upper alphabets.\r
+ for (int i = 0; i < digitnum; i++) {\r
+ int rIndex = rnd.nextInt(12);\r
+ if (rIndex >= 0 && rIndex < 2) { // 0 - 9\r
+ rndpassword.append((rnd.nextInt(10)));\r
+ } else if (rIndex >= 2 && rIndex < 7) { // a-z\r
+ rndpassword.append((char) ((int) (rnd.nextInt(26)) + 97));\r
+ } else { // A-Z\r
+ rndpassword.append((char) ((int) (rnd.nextInt(26)) + 65));\r
+ }\r
+ }\r
+ password = rndpassword.toString();\r
+ return password;\r
+ }\r
+}\r