Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initial commit of version 1
  • Loading branch information
SharkFourSix committed Oct 26, 2019
1 parent 23dba6c commit 8d1f8c5
Show file tree
Hide file tree
Showing 44 changed files with 1,521 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
A Lifecycle aware network state change listener library for Android

Supports SDK


[x] SDK >=19

Usage

```java
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import lib.gintec_rdl.network_state.network.NetworkSpecs;
import lib.gintec_rdl.network_state.network.NetworkState;

public class ExampleActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);

ArrayList<String> loglist = new ArrayList<>();

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, loglist);

((ListView) findViewById(R.id.listView)).setAdapter(adapter);

NetworkState.of(NetworkSpecs.WIFI_SPEC, this)
.v21()
.ifSupported(networkSpec -> adapter.add("Network is supported"))
.ifNotSupported(networkSpec -> adapter.add("Network not supported"))
.whenLosing(networkSpec -> adapter.add("Losing network..."))
.whenNotAvailable(networkSpec -> adapter.add("Network not available"))
.whenLost(networkSpec -> adapter.add("Network lost"))
.whenAvailable(networkSpec -> adapter.add("Network available"))
.ifFailed(networkSpec -> adapter.add("Connection failed"))
.whenAuthenticating(networkSpec -> adapter.add("Authenticating"))
.whenCheckingCaptivePortal(networkSpec -> adapter.add("Checking captive portal..."))
.whenObtainingIpAddress(networkSpec -> adapter.add("Obtaining IP..."))
.ifBlocked(networkSpec -> adapter.add("Blocked"))
.ifSuspended(networkSpec -> adapter.add("Suspended"))
.whenConnecting(networkSpec -> adapter.add("Connecting..."))
.whenIdle(networkSpec -> adapter.add("Idle"))
.whenScanning(networkSpec -> adapter.add("Scanning..."))
.whenVerifyingPoorLink(networkSpec -> adapter.add("Verifying poor link..."))
.create()
.attachTo(this);

}
}
```
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildToolsVersion = '28.0.3'
}

dependencies {
def lifecycle_version = "2.1.0"

implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.13-beta-3'
androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'

implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lib.gintec_rdl.network_state;

import android.content.Context;

import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import lib.gintec_rdl.network_state.network.NetworkSpecs;
import lib.gintec_rdl.network_state.network.NetworkState;

import static org.junit.Assert.assertEquals;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("app.gintec_rdl.network_state", appContext.getPackageName());

NetworkState.of(NetworkSpecs.VPN_SPEC, appContext)
.whenConnected(networkSpec -> {
})
.whenLost(networkSpec -> {

})
.create()
.observe(null);
}
}
28 changes: 28 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="lib.gintec_rdl.network_state">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">

<!--<activity android:name=".ExampleActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>-->

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lib.gintec_rdl.network_state;

public class ExampleActivity /*extends AppCompatActivity*/ {
/*
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
ArrayList<String> loglist = new ArrayList<>();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, loglist);
((ListView) findViewById(R.id.listView)).setAdapter(adapter);
NetworkState.of(NetworkSpecs.ETHERNET_SPEC, this)
.v21()
.ifSupported(networkSpec -> adapter.add("Network is supported"))
.ifNotSupported(networkSpec -> adapter.add("Network not supported"))
.whenLosing(networkSpec -> adapter.add("Losing network..."))
.whenNotAvailable(networkSpec -> adapter.add("Network not available"))
.whenLost(networkSpec -> adapter.add("Network lost"))
.whenAvailable(networkSpec -> adapter.add("Network available"))
.ifFailed(networkSpec -> adapter.add("Connection failed"))
.whenAuthenticating(networkSpec -> adapter.add("Authenticating"))
.whenCheckingCaptivePortal(networkSpec -> adapter.add("Checking captive portal..."))
.whenObtainingIpAddress(networkSpec -> adapter.add("Obtaining IP..."))
.ifBlocked(networkSpec -> adapter.add("Blocked"))
.ifSuspended(networkSpec -> adapter.add("Suspended"))
.whenConnecting(networkSpec -> adapter.add("Connecting..."))
.whenIdle(networkSpec -> adapter.add("Idle"))
.whenScanning(networkSpec -> adapter.add("Scanning..."))
.whenVerifyingPoorLink(networkSpec -> adapter.add("Verifying poor link..."))
.create()
.attachTo(this);
}*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lib.gintec_rdl.network_state;

import lib.gintec_rdl.network_state.network.NetworkSpec;

public interface NetworkStateCallback {
void action(NetworkSpec networkSpec);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lib.gintec_rdl.network_state;

public class NetworkStateException extends RuntimeException {
public NetworkStateException(Throwable throwable) {
super(throwable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package lib.gintec_rdl.network_state.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import lib.gintec_rdl.network_state.utils.PlatformUtils;

final class LegacyNetworkChangeListener extends BroadcastReceiver implements NetworkChangeListener {
private static final String TAG = "LegacyListener";
private final IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);

private final NetworkSpec networkSpec;

private boolean registered;

public LegacyNetworkChangeListener(NetworkSpec networkSpec) {
this.networkSpec = networkSpec;
}

@Override
public void onReceive(Context context, Intent intent) {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
final ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getActiveNetworkInfo();
if (networkInfo != null) {
boolean isTargetNetwork = networkInfo.getType() == networkSpec.builder.legacyNetworkType;

if (!isTargetNetwork) {
if (networkSpec.builder.legacyNetworkType == ConnectivityManager.TYPE_BLUETOOTH) {
networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH);
isTargetNetwork = true;
}
if (networkSpec.builder.legacyNetworkType == ConnectivityManager.TYPE_ETHERNET) {
networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
isTargetNetwork = true;
} else if (networkSpec.builder.legacyNetworkType == ConnectivityManager.TYPE_VPN) {
if (PlatformUtils.isLollipop()) {
networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_VPN);
isTargetNetwork = true;
}
}
}

// Type all
if (!isTargetNetwork && networkSpec.builder.legacyNetworkType == NetworkSpecs.ANY_SPEC.legacyType) {
isTargetNetwork = true;
}

if (isTargetNetwork) {
final NetworkInfo.DetailedState detailedState = networkInfo.getDetailedState();
switch (detailedState) {
case IDLE:
callback(networkSpec.builder.whenIdle, networkSpec);
break;
case FAILED:
callback(networkSpec.builder.ifFailed, networkSpec);
break;
case BLOCKED:
callback(networkSpec.builder.ifBlocked, networkSpec);
break;
case SCANNING:
callback(networkSpec.builder.whenScanning, networkSpec);
break;
case CONNECTED:
callback(networkSpec.builder.whenAvailable, networkSpec);
break;
case SUSPENDED:
callback(networkSpec.builder.ifSuspended, networkSpec);
break;
case CONNECTING:
callback(networkSpec.builder.whenConnecting, networkSpec);
break;
case AUTHENTICATING:
callback(networkSpec.builder.whenAuthenticating, networkSpec);
break;
case DISCONNECTED:
callback(networkSpec.builder.whenLost, networkSpec);
break;
case DISCONNECTING:
callback(networkSpec.builder.whenLosing, networkSpec);
break;
case CAPTIVE_PORTAL_CHECK:
callback(networkSpec.builder.whenCheckingCaptivePortal, networkSpec);
break;
case OBTAINING_IPADDR:
callback(networkSpec.builder.whenObtainingIpAddress, networkSpec);
break;
case VERIFYING_POOR_LINK:
callback(networkSpec.builder.whenVerifyingPoorLink, networkSpec);
break;
}
} else {
Log.w(TAG, "NetworkSpec type not registered:" + networkInfo.getSubtype());
}
} else {
callback(networkSpec.builder.whenNotAvailable, networkSpec);
}
}
}

@Override
public void registerSelf(Context context) {
synchronized (this) {
context.registerReceiver(this, intentFilter);
registered = true;
}
}

@Override
public void unregisterSelf(Context context) {
synchronized (this) {
context.unregisterReceiver(this);
registered = false;
}
}

@Override
public boolean isRegistered() {
synchronized (this) {
return registered;
}
}
}
Loading

0 comments on commit 8d1f8c5

Please sign in to comment.