Checking in LEDE GUI for device registration; this was ported from the implementation...
[iot2.git] / others / lede-gui / src / main / java / com / example / lede2 / EnrollDeviceActivity.java
1 package com.example.lede2;\r
2 \r
3 import android.content.Context;\r
4 import android.content.Intent;\r
5 import android.net.ConnectivityManager;\r
6 import android.net.NetworkInfo;\r
7 import android.net.wifi.WifiInfo;\r
8 import android.net.wifi.WifiManager;\r
9 import android.support.v7.app.AppCompatActivity;\r
10 import android.os.Bundle;\r
11 import android.view.View;\r
12 import android.view.inputmethod.InputMethodManager;\r
13 import android.widget.Button;\r
14 import android.widget.EditText;\r
15 import android.widget.TextView;\r
16 import android.widget.Toast;\r
17 import android.util.Log;\r
18 \r
19 import java.util.Random;\r
20 \r
21 public class EnrollDeviceActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {\r
22 \r
23     private static final int REQUEST_RESULT = 1001;\r
24     Button done;//Done button in UI\r
25     Button wifi;//wifi button in UI\r
26     SSH ssh;//Connection object between Android & Router\r
27     Context context;\r
28 \r
29     TextView psk;//red letter in UI\r
30     String newpsk;//same as psk (different data type)\r
31 \r
32     EditText name;//device name newly registered in UI\r
33     String deviceName;//same as name(different data type)\r
34 \r
35     @Override\r
36     protected void onCreate(Bundle savedInstanceState) {\r
37         super.onCreate(savedInstanceState);\r
38         setContentView(R.layout.activity_enroll_device);\r
39 \r
40         ssh = new SSH();\r
41         psk = (TextView) findViewById(R.id.add_psk);\r
42         done = (Button) findViewById(R.id.done);\r
43         wifi = (Button) findViewById(R.id.wifi);\r
44         name = (EditText) findViewById(R.id.name);\r
45 \r
46         done.setOnClickListener(this);\r
47         wifi.setOnClickListener(this);\r
48         name.setOnFocusChangeListener(this);\r
49 \r
50         //Make random password and show the password through EditText\r
51         newpsk = generateRandomPassword();\r
52         psk.setText(newpsk);\r
53         // execute shell script  (script's function -> change router password to newpsk)\r
54         ssh.execute("-ch " + newpsk);\r
55         try {//To execute asyntask in ssh object, we have to sleep main thread\r
56             Thread.sleep(1000);\r
57         } catch (Exception e) {\r
58             Log.d("SLEEP EXCEPTION", "SLEEP EXCEPTION occurs in onCreate method of EnrollDeviceActivity");\r
59         }\r
60     }\r
61 \r
62     @Override\r
63     public void onFocusChange(View view, boolean hasFocus) {//function not to modify randomly generated password for newly registered device\r
64         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);\r
65         if (hasFocus) {\r
66             imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);\r
67         } else {\r
68             imm.hideSoftInputFromWindow(view.getWindowToken(), 0);\r
69         }\r
70     }\r
71 \r
72     boolean isNetworkAvailable() {//check whether wifi connection is or not\r
73         Context context = getApplicationContext();\r
74         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);\r
75         NetworkInfo activeNetwork = cm.getActiveNetworkInfo();\r
76         boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();\r
77         return isConnected;\r
78     }\r
79 \r
80     @Override\r
81     public void onClick(View v) {\r
82         if (v == done) {\r
83             //Users try to execute shell scripts by pushing button,\r
84             //but problem could occur(application stop) if user push the button without wifi connection\r
85             //As a result, below function have to be implemented.\r
86             if (isNetworkAvailable() == false) {//without wifi connection\r
87                 Toast t = Toast.makeText(this, R.string.connect, Toast.LENGTH_SHORT);\r
88                 t.show();\r
89                 startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));\r
90             } else {//with wifi connection\r
91                 deviceName = name.getText().toString();\r
92                 if(deviceName.equals("")){\r
93                     Toast t = Toast.makeText(this, R.string.empty_name,Toast.LENGTH_SHORT);\r
94                     t.show();\r
95                     return;\r
96                 }\r
97                 context = getApplicationContext();\r
98                 if (getWifiName(context).equals("\""+ ConfigActivity.RSSID +"\"")) {//if wifi name is LEDE2\r
99                     String networkPass = psk.getText().toString();//random password\r
100                     ssh = new SSH();\r
101 \r
102                     // execute shell script  (script's function -> Save contents(Mac,Ip,Key,Name) on hostapd-psk)\r
103                     ssh.execute("-co " + networkPass + " " + deviceName +" "+ ConfigActivity.RPWD);//review!!!!!!\r
104                     try {//To execute asyntask in ssh object, we have to sleep main thread\r
105                         Thread.sleep(1000);\r
106                     } catch (Exception e) {\r
107                         Log.d("SLEEP EXCEPTION", "SLEEP EXCEPTION occurs in onClick method of EnrollDeviceActivity");\r
108                     }\r
109                     finish();//Go back to the Main Activity\r
110                 } else { //if name of wifi is not LEDE2 -> go to wifi configuration screen to change wifi type\r
111                     Toast t = Toast.makeText(this, R.string.try_again, Toast.LENGTH_SHORT);\r
112                     t.show();\r
113                     startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));\r
114                 }\r
115             }\r
116         } else if (v == wifi) {\r
117             startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));\r
118         }\r
119     }\r
120 \r
121     /******************************************************************************************************************************************************************/\r
122     //Detect you are connected to a specific network.\r
123     /******************************************************************************************************************************************************************/\r
124     public String getWifiName(Context context) {\r
125         WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);\r
126         if (manager.isWifiEnabled()) {\r
127             WifiInfo wifiInfo = manager.getConnectionInfo();\r
128             if (wifiInfo != null) {\r
129                 NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());\r
130                 if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR) {\r
131                     return wifiInfo.getSSID();\r
132                 }\r
133             }\r
134         }\r
135         return null;\r
136     }\r
137 \r
138     /******************************************************************************************************************************************************************/\r
139     // Generate a random password and return it\r
140     /******************************************************************************************************************************************************************/\r
141     public String generateRandomPassword() {\r
142         String password = new String();\r
143 \r
144         StringBuffer rndpassword = new StringBuffer();\r
145         Random rnd = new Random();\r
146         int digitnum = 20;\r
147         // Generate random 20digit password with upper / lower case alphabet + numbers\r
148         // There are 10 int nums, 26 lower alphabets, 26 upper alphabets. Total 62\r
149         // So 2/12 possiblity of int, 5/12 lower, 5/12 upper alphabets.\r
150         for (int i = 0; i < digitnum; i++) {\r
151             int rIndex = rnd.nextInt(12);\r
152             if (rIndex >= 0 && rIndex < 2) { // 0 - 9\r
153                 rndpassword.append((rnd.nextInt(10)));\r
154             } else if (rIndex >= 2 && rIndex < 7) { // a-z\r
155                 rndpassword.append((char) ((int) (rnd.nextInt(26)) + 97));\r
156             } else {    // A-Z\r
157                 rndpassword.append((char) ((int) (rnd.nextInt(26)) + 65));\r
158             }\r
159         }\r
160         password = rndpassword.toString();\r
161         return password;\r
162     }\r
163 }\r