Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tink-expo/cs470-auto-wifi
Browse files Browse the repository at this point in the history
…into id-pw-pick
  • Loading branch information
tink-expo committed Nov 30, 2019
2 parents b2af80a + 3830ae3 commit 886fe63
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
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;

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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions android/app/src/main/res/layout/activity_fail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,16 @@
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

<Button
android:id="@+id/retryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:text="Retry"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

0 comments on commit 886fe63

Please sign in to comment.