Skip to content

Commit 8f7a244

Browse files
committed
Commit WiFiScanner Android code
1 parent a8ce67b commit 8f7a244

35 files changed

+984
-0
lines changed

app/.DS_Store

6 KB
Binary file not shown.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 27
5+
defaultConfig {
6+
applicationId "ro.pub.acs.wifiscanner"
7+
minSdkVersion 21
8+
targetSdkVersion 27
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(dir: 'libs', include: ['*.jar'])
23+
//noinspection GradleCompatible
24+
implementation 'com.android.support:appcompat-v7:27.1.1'
25+
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
26+
testImplementation 'junit:junit:4.12'
27+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
28+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
29+
}
30+

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ro.pub.acs.wifiscanner;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("ro.pub.acs.wifiscanner", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="ro.pub.acs.wifiscanner">
4+
5+
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
6+
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
7+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
8+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
9+
<uses-permission
10+
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
11+
android:maxSdkVersion="18" />
12+
13+
<application
14+
android:allowBackup="true"
15+
android:icon="@mipmap/ic_launcher"
16+
android:label="@string/app_name"
17+
android:roundIcon="@mipmap/ic_launcher_round"
18+
android:supportsRtl="true"
19+
android:theme="@style/AppTheme">
20+
<activity android:name=".MainActivity">
21+
<intent-filter>
22+
<action android:name="android.intent.action.MAIN" />
23+
24+
<category android:name="android.intent.category.LAUNCHER" />
25+
</intent-filter>
26+
</activity>
27+
</application>
28+
29+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package ro.pub.acs.wifiscanner;
2+
3+
import android.os.Handler;
4+
import android.os.HandlerThread;
5+
6+
import java.util.Random;
7+
8+
public class Alarm {
9+
// Callback for alarm trigger --------------------------------------------------------------------------------------
10+
public interface Callback {
11+
public void onTriggered();
12+
}
13+
// -----------------------------------------------------------------------------------------------------------------
14+
15+
private class Timer implements Runnable {
16+
private long timeout;
17+
private boolean repeating;
18+
19+
public Timer(long timeout, boolean repeating) {
20+
this.timeout = timeout;
21+
this.repeating = repeating;
22+
}
23+
24+
@Override
25+
public void run() {
26+
mCallback.onTriggered();
27+
if (repeating) {
28+
mHandler.postDelayed(this, timeout);
29+
}
30+
}
31+
}
32+
33+
private String mId;
34+
private Callback mCallback;
35+
private boolean mSet = false;
36+
private Handler mHandler;
37+
38+
public Alarm(String id, Callback callback) {
39+
mId = id;
40+
mCallback = callback;
41+
}
42+
43+
public void set(long timeout, boolean repeating) {
44+
synchronized (this) {
45+
if (!mSet) {
46+
HandlerThread thread = new HandlerThread(mId + "-" + new Random().nextLong());
47+
thread.start();
48+
mHandler = new Handler(thread.getLooper());
49+
mHandler.post(new Timer(timeout, repeating));
50+
mSet = true;
51+
}
52+
}
53+
}
54+
55+
public void cancel() {
56+
synchronized (this) {
57+
if (mSet) {
58+
mSet = false;
59+
mHandler.getLooper().quit();
60+
}
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ro.pub.acs.wifiscanner;
2+
3+
import android.Manifest;
4+
import android.content.Context;
5+
import android.content.SharedPreferences;
6+
import android.content.pm.PackageManager;
7+
import android.os.Build;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.os.Bundle;
10+
import android.util.Log;
11+
import android.view.View;
12+
import android.widget.Button;
13+
14+
public class MainActivity extends AppCompatActivity {
15+
16+
WiFiReceiver mReceiver;
17+
public static final String TAG = "WiFiScanner";
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
setContentView(R.layout.activity_main);
23+
24+
mReceiver = new WiFiReceiver(this);
25+
26+
initButton();
27+
}
28+
29+
private void initButton() {
30+
setScanning(false);
31+
32+
Button button = findViewById(R.id.button);
33+
button.setText(isScanning() ? "Stop scan" : "Start scan");
34+
35+
button.setOnClickListener(new Button.OnClickListener() {
36+
@Override
37+
public void onClick(View v) {
38+
if (isScanning()) {
39+
Log.v(TAG, "Stop scanning");
40+
mReceiver.stopReceiver();
41+
setScanning(false);
42+
((Button) v).setText("Start scan");
43+
} else {
44+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
45+
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
46+
1001);
47+
} else {
48+
Log.v(TAG, "Start scanning");
49+
mReceiver.startReceiver();
50+
setScanning(true);
51+
((Button) v).setText("Stop scan");
52+
}
53+
}
54+
}
55+
});
56+
}
57+
58+
@Override
59+
public void onRequestPermissionsResult(int requestCode, String[] permissions,
60+
int[] grantResults) {
61+
if (requestCode == 1001 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
62+
Log.v(TAG, "Start scanning");
63+
mReceiver.startReceiver();
64+
setScanning(true);
65+
66+
Button button = findViewById(R.id.button);
67+
button.setText("Stop scan");
68+
}
69+
}
70+
71+
private boolean isScanning() {
72+
SharedPreferences prefs = this.getSharedPreferences("scan_status", Context.MODE_PRIVATE);
73+
return prefs.getBoolean("scanning", false);
74+
}
75+
76+
private void setScanning(boolean status) {
77+
SharedPreferences prefs = this.getSharedPreferences("scan_status", Context.MODE_PRIVATE);
78+
SharedPreferences.Editor editor = prefs.edit();
79+
editor.putBoolean("scanning", status);
80+
editor.commit();
81+
}
82+
}

0 commit comments

Comments
 (0)