From 522eae4607c3dddbeabf2f9f9f86aaa099962441 Mon Sep 17 00:00:00 2001 From: zealotzealot Date: Sun, 1 Dec 2019 00:23:33 +0900 Subject: [PATCH 1/2] Refactor WifiScanConnect.BroadcastReceiver to get timer as parameter --- .../auto_wifi/ProcessingActivity.java | 16 +--------------- .../hsh0908y/auto_wifi/WifiScanConnect.java | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/ProcessingActivity.java b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/ProcessingActivity.java index 25d23c3..0b16eeb 100644 --- a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/ProcessingActivity.java +++ b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/ProcessingActivity.java @@ -126,21 +126,7 @@ public void tryWifiConnect() { SsidPwPick ssidPwPick = new SsidPwPick(receivedTextBlockList, receivedWifiDataList); final SsidPw ssidPw = getSsidPw(); - wifiConnectReceiver = WifiScanConnect.connectAndRegisterReceiver(this, wifiManager, new IntentFilter(), ssidPw); - Log.d(TAG, "Start timer"); - timer.schedule(new TimerTask() { - @Override - public void run() { - Log.d(TAG, "Run timer"); - ProcessingActivity activity = mWeakReference.get(); - if (activity != null && !activity.isFinishing()) { - Intent failIntent = new Intent(activity, FailActivity.class); - failIntent.putExtra("id", ssidPw.ssid); - failIntent.putExtra("pw", ssidPw.pw); - startActivity(failIntent); - } - } - }, 3500); + wifiConnectReceiver = WifiScanConnect.connectAndRegisterReceiver(this, wifiManager, new IntentFilter(), ssidPw, timer); } } diff --git a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/WifiScanConnect.java b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/WifiScanConnect.java index 3f826bb..647a126 100644 --- a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/WifiScanConnect.java +++ b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/WifiScanConnect.java @@ -18,6 +18,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.Timer; +import java.util.TimerTask; public class WifiScanConnect { final static String TAG = "WifiScanConnect"; @@ -36,7 +38,7 @@ public static BroadcastReceiver scanAndRegisterReceiver( public static BroadcastReceiver connectAndRegisterReceiver( final AppCompatActivity activity, final WifiManager wifiManager, - IntentFilter intentFilter, final SsidPw ssidPw) { + IntentFilter intentFilter, final SsidPw ssidPw, Timer timer) { if (ssidPw == null || ssidPw.ssid == null) { return null; } @@ -80,6 +82,21 @@ public void onReceive(Context context, Intent intent) { activity.registerReceiver(connectReceiver, intentFilter); wifiManager.enableNetwork(netId, true); // wifiManager.reconnect(); + + Log.d(TAG, "Start timer"); + timer.schedule(new TimerTask() { + @Override + public void run() { + Log.d(TAG, "Run timer"); + if (activity != null && !activity.isFinishing()) { + Intent failIntent = new Intent(activity, FailActivity.class); + failIntent.putExtra("id", ssidPw.ssid); + failIntent.putExtra("pw", ssidPw.pw); + activity.startActivity(failIntent); + } + } + }, 3500); + return connectReceiver; } } From 21b4bfebc876efd1d894259328473378e719e1b6 Mon Sep 17 00:00:00 2001 From: zealotzealot Date: Sun, 1 Dec 2019 00:33:57 +0900 Subject: [PATCH 2/2] Implement retry from FailActivity --- .../hsh0908y/auto_wifi/FailActivity.java | 39 +++++++++++++++++++ .../app/src/main/res/layout/activity_fail.xml | 12 ++++++ 2 files changed, 51 insertions(+) diff --git a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/FailActivity.java b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/FailActivity.java index 3edff64..0fb68f2 100644 --- a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/FailActivity.java +++ b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/FailActivity.java @@ -1,18 +1,28 @@ package com.example.hsh0908y.auto_wifi; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.IntentFilter; +import android.net.wifi.WifiManager; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.InputType; +import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; +import com.example.hsh0908y.auto_wifi.common.SsidPw; + +import java.util.Timer; + public class FailActivity extends AppCompatActivity { static final int REQUEST_CODE = 0; + static final String TAG = "FailActivity"; private String recognizedId; private String recognizedPw; @@ -20,6 +30,10 @@ public class FailActivity extends AppCompatActivity { private String currentId; private String currentPw; + private Timer timer; + private BroadcastReceiver wifiConnectReceiver = null; + private WifiManager wifiManager; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -68,6 +82,31 @@ public void onClick(DialogInterface dialog, int which) { dialogBuilder.show(); } }); + + timer = new Timer(); + wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); + + Button retryButton = (Button) findViewById(R.id.retryButton); + retryButton.setOnClickListener(new Button.OnClickListener() { + @Override + public void onClick(View view) { + wifiConnectReceiver = WifiScanConnect.connectAndRegisterReceiver(FailActivity.this, wifiManager, new IntentFilter(), new SsidPw(currentId, currentPw), timer); + } + }); + } + + + @Override + protected void onPause() { + super.onPause(); + + timer.cancel(); + + try { + unregisterReceiver(wifiConnectReceiver); + } catch (IllegalArgumentException e) { + Log.d(TAG, e.getMessage()); + } } protected void onActivityResult(int requestCode, int resultCode, Intent intent) { diff --git a/android/app/src/main/res/layout/activity_fail.xml b/android/app/src/main/res/layout/activity_fail.xml index 4211bd4..7cddd67 100644 --- a/android/app/src/main/res/layout/activity_fail.xml +++ b/android/app/src/main/res/layout/activity_fail.xml @@ -78,4 +78,16 @@ app:layout_constraintTop_toTopOf="parent" /> + +