1 /*
2  * wpadebug - wpa_supplicant and Wi-Fi debugging app for Android
3  * Copyright (c) 2018, The Linux Foundation
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 package w1.fi.wpadebug;
10 
11 import android.app.Activity;
12 import android.os.Bundle;
13 import android.text.Editable;
14 import android.text.TextWatcher;
15 import android.view.View;
16 import android.widget.Button;
17 import android.widget.EditText;
18 import android.util.Log;
19 
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 
25 public class InputUri extends Activity {
26 
27     private EditText mEditText;
28     private Button mSubmitButton;
29     private String mUriText;
30     private static final String FILE_NAME = "wpadebug_qrdata.txt";
31     private static final String TAG = "wpadebug";
32 
33     @Override
onCreate(Bundle savedInstanceState)34     protected void onCreate(Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36         setContentView(R.layout.input_uri);
37         mEditText = (EditText)findViewById(R.id.edit_uri);
38         mSubmitButton = (Button)findViewById(R.id.submit_uri);
39 
40 	mEditText.addTextChangedListener(new TextWatcher() {
41 		@Override
42 		public void onTextChanged(CharSequence s, int start, int before,
43 					  int count) {
44 		    mUriText = mEditText.getText().toString();
45 		    if (mUriText.startsWith("DPP:") &&
46 			mUriText.endsWith(";;")) {
47 			writeToFile(mUriText);
48 			finish();
49 		    }
50 		}
51 
52 		@Override
53 		public void beforeTextChanged(CharSequence s, int start,
54 					      int count, int after) {
55 		}
56 
57 		@Override
58 		public void afterTextChanged(Editable s) {
59 		}
60 	    });
61     }
62 
63     @Override
onResume()64     protected void onResume() {
65         super.onResume();
66         mSubmitButton.setOnClickListener(new View.OnClickListener() {
67             @Override
68             public void onClick(View view) {
69                 mUriText = mEditText.getText().toString();
70                 new Thread(new Runnable() {
71                     @Override
72                     public void run() {
73                         writeToFile(mUriText);
74 
75                         InputUri.this.runOnUiThread(new Runnable() {
76                             @Override
77                             public void run() {
78                                 finish();
79                             }
80                         });
81                     }
82                 }).start();
83 
84             }
85 
86         });
87     }
88 
writeToFile(String data)89     public void writeToFile(String data)
90     {
91         File file = new File("/sdcard", FILE_NAME);
92         try
93         {
94             file.createNewFile();
95             FileOutputStream fOut = new FileOutputStream(file);
96             OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
97             myOutWriter.append(mUriText);
98             myOutWriter.close();
99 
100             fOut.flush();
101             fOut.close();
102         }
103         catch (IOException e)
104         {
105             Log.e(TAG, "File write failed: " + e.toString());
106         }
107     }
108 }
109