-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFreeWifiAutoConnect.java
430 lines (371 loc) · 15.4 KB
/
FreeWifiAutoConnect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package com.phonetest.phonetest;
import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.CaptivePortal;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import android.provider.Settings;
import android.speech.RecognizerIntent;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.TelecomManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
final int REQ_CODE_SPEECH_INPUT = 100;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
scrollView.setHorizontalScrollBarEnabled(true);
TelecomManager tm = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return;
}
final TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
final TableRow tableRow = new TableRow(this);
final Button btnStartRecording = new Button(this);
btnStartRecording.setText("Start Rec");
final Button btnStopRecording = new Button(this);
btnStopRecording.setText("Stop Rec");
btnStartRecording.setEnabled(true);
btnStopRecording.setEnabled(false);
tableRow.addView(btnStartRecording);
tableRow.addView(btnStopRecording);
tableRow.addView(new Button(this));
tableLayout.addView(tableRow);
btnStartRecording.setOnClickListener(new View.OnClickListener() {
int nRow = 0;
@Override
public void onClick(View v) {
//screenLog("test" + (nRow++));
//startSpeechRecognizer();
//test record audio
//startAudioRecording();
//btnStartRecording.setEnabled(false);
//btnStopRecording.setEnabled(true);
networkProcessing();
}
});
btnStopRecording.setOnClickListener(new View.OnClickListener() {
int nRow = 0;
@Override
public void onClick(View v) {
//screenLog("test" + (nRow++));
//startSpeechRecognizer();
//test record audio
//stopAudioRecording();
//btnStartRecording.setEnabled(true);
//btnStopRecording.setEnabled(false);
}
});
}
private void showToastMessage(String str) {
Toast toast = new Toast(this);
TextView textView = new TextView(this);
textView.setText(str);
textView.setBackgroundColor(Color.BLACK);
textView.setTextColor(Color.WHITE);
toast.setView(textView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
WifiManager wifiManager = null;
private void networkProcessing() {
try {
if (wifiManager == null) {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()){
log("wifi is enabled");
} else{
log("wifi is not enabled");
return;
}
}
} catch (Exception e) {
log("Exception:" + e.getMessage());
return;
}
int ip = wifiManager.getConnectionInfo().getIpAddress();
String ipString = String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
log("ip=" + ipString);
log("bssid=" + wifiManager.getConnectionInfo().getBSSID());
if (!ipString.equals("0.0.0.0")){
log("Une adresse ip est déjà obtenue");
} else {
log("Le périphérique n'est pas affecté d'une adresse IP");
}
if (1==1)
return;
listVisibleSSIDs();
listPreconfiguredSSID();
deleteAllPreconfiguredSSID("FreeWifi");
addFreeWifiConfiguration();
connectToFreeWifiConfiguration();
}
List<ScanResult> lstScanResult = null;
private void listVisibleSSIDs() {
wifiManager.startScan();
lstScanResult = wifiManager.getScanResults();
if (lstScanResult != null) {
log("---- Résultat du scan des SSID visibles aux alentours ----");
for (int i = 0; i < lstScanResult.size(); i++) {
String SSID = lstScanResult.get(i).SSID;
String BSSID = lstScanResult.get(i).BSSID;
String LEVEL = "" + lstScanResult.get(i).level;
log("SSID " + i + " visible actuellement : SSID [" + SSID + "] ; BSSID = " + BSSID + " ; level = " + LEVEL);
}
}
}
private void listPreconfiguredSSID(){
log("---- Liste des réseaux SSID déjà configurés ----");
List<WifiConfiguration> lstWifiConf = wifiManager.getConfiguredNetworks();
for(int i=0;i<lstWifiConf.size();i++) {
log("SSID préconfiguré " + i + " = " + lstWifiConf.get(i).SSID + " ; Network Id = " + lstWifiConf.get(i).networkId);
}
}
private void deleteAllPreconfiguredSSID(String ssidToDelete) {
log("---- Suppression de tous les SSID préconfigurés nommés " + ssidToDelete + " ----");
List<WifiConfiguration> lstWifiConf = wifiManager.getConfiguredNetworks();
for (int i = 0; i < lstWifiConf.size(); i++) {
//log("Processing : " + lstWifiConf.get(i).SSID + " ; Searching for : " + ssidToDelete );
if (lstWifiConf.get(i).SSID.toLowerCase().trim().contains(ssidToDelete.trim().toLowerCase())) {
log("Un SSID " + ssidToDelete + " préconfiguré a été trouvé ; Suppression en cours.");
boolean r = wifiManager.removeNetwork(lstWifiConf.get(i).networkId);
log("Résultat de la suppression = " + r);
}
}
}
String excludeBSSID = "";//"7a:04:32:XX:XX:XX";
private void addFreeWifiConfiguration() {
// Ici reconfigurer une connexion FreeWifi
log("* Ajout d'une configuration FreeWifi *");
wifiManager.startScan();
lstScanResult = wifiManager.getScanResults();
if (lstScanResult != null) {
log("---- Recherche d'un SSID FreeWifi visible aux alentours ----");
for (int i = 0; i < lstScanResult.size(); i++) {
String SSID = lstScanResult.get(i).SSID;
String BSSID = lstScanResult.get(i).BSSID;
String LEVEL = "" + lstScanResult.get(i).level;
if (SSID.toLowerCase().trim().equals("freewifi") && !BSSID.equals(excludeBSSID)) {
log("FreeWifi visible aux alentours : SSID " + SSID + " found ; BSSID = " + BSSID + " ; level = " + LEVEL);
WifiConfiguration wifiConf = new WifiConfiguration();
wifiConf.SSID = SSID;
wifiConf.BSSID = BSSID;
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(wifiConf);
log("Une Configuration FreeWifi a été ajoutée");
break; // Pour n'ajouter qu'une seule configuration relative au premier SSID FreeWifi détecté
// Si non (s'il n'y a pas de break) il y a autant d'ajout que de SSIDs FreeWifi visibles (peut être intéressant à exploiter...)
}
}
}
log("* Fin Ajout d'une configuration FreeWifi *");
}
private void connectToFreeWifiConfiguration(){
// Connexion au réseau FreeWifi dont la configuration a été ajoutée
log("---- Liste des réseaux SSID déjà configurés et connexion au SSID FreeWifi configuré ----");
List<WifiConfiguration> lstWifiConf = wifiManager.getConfiguredNetworks();
int networkId = -1;
for(int i=0;i<lstWifiConf.size();i++) {
log("lstwificonf " + i + "=" + lstWifiConf.get(i).SSID);
if (lstWifiConf.get(i).SSID.toLowerCase().trim().contains(("freewifi")) && !lstWifiConf.get(i).BSSID.equals(excludeBSSID)) {
log("Un réseau FreeWifi a été trouvé ; Récupération de l'id");
networkId = lstWifiConf.get(i).networkId;
log("networkId FreeWifi = " + networkId);
}
}
if (networkId != -1){
// Connexion proprement dite au SSID FreeWifi qui vient d'être ajouté
wifiManager.disconnect();
wifiManager.enableNetwork(networkId, true);
boolean r = wifiManager.reconnect();
log("reconnect r = " + r);
}
FreeWifiHttpRequest freeWifiHttpRequest = new FreeWifiHttpRequest();
String login="0000000000";
String password="abcabcabcabca";
URL url = null;
try {
url = new URL("https://wifi.free.fr/Auth?login=" + login + "&password=" + password);
} catch (MalformedURLException e) {
e.printStackTrace();
}
// Attendre que la connexion soit établie : TODO: Passer en évènement attendu (connecté à FreeWifi)...
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
freeWifiHttpRequest.setWifiManager(wifiManager);
freeWifiHttpRequest.execute(url);
}
public void log(String s) {
if (s.trim().equals("")){
s = " ";
}
System.out.println(s);
screenLog(s);
}
TableLayout tableLayout2;
TableRow newTableRow;
TextView textView;
ScrollView scrollView;
private void screenLog(String str){
tableLayout2 = (TableLayout) findViewById(R.id.tableLayout2);
newTableRow = new TableRow(getBaseContext());
textView = new TextView(getBaseContext());
textView.setText(str);
textView.setTextColor(Color.BLACK);
newTableRow.addView(textView);
tableLayout2.addView(newTableRow);
scrollView = (ScrollView) findViewById(R.id.scrollView);
scrollView.pageScroll(View.FOCUS_DOWN);
}
String mFileName;
FileOutputStream mFileOut;
Socket mSocket;
ParcelFileDescriptor mPFD;
FileDescriptor mFD;
MediaRecorder mRecorder;
MediaPlayer mPlayer;
void startAudioRecording(){
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.mp4";
// Use a filedescriptor instead of direct file
// This will enable easy transition to sockets later
try {
mFileOut = new FileOutputStream(mFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
mFD = mFileOut.getFD();
} catch (IOException e) {
e.printStackTrace();
}
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(mFileName);
try {
mRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mRecorder.start();
}
private void stopAudioRecording() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.mp4";
mRecorder.stop();
mRecorder.release();
mRecorder = null;
try {
mFileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
mFileOut = null;
// Use a filedescriptor instead of direct file
// This will enable easy transition to sockets later
FileInputStream fileIn;
try {
fileIn = new FileInputStream(mFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//mFD = fileIn.getFD();
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(mFileName);
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare(); // might take long! (for buffering, etc)
} catch (IOException e) {
e.printStackTrace();
}
mPlayer.start();
}
/**
* Démarre la reconnaissance vocale et loggue à l'écran ce qui a été reconnu.
*/
void startSpeechRecognizer(){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().getDisplayLanguage());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Vous pouvez parler ...");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), "Désolé, votre appareil ne supporte pas d'entrée vocale...", Toast.LENGTH_SHORT).show();
}
}
/**
* Evenèment déclenché à la fin d'une reconnaissance vocale.
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
log("onActivityResult");
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> buffer = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String result = buffer.get(0);
System.out.println("result=" + result);
screenLog(result);
}
break;
}
default:
break;
}
}
}