Skip to content

Commit

Permalink
Sensitivity implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Siamak committed Jun 30, 2019
1 parent cab24ef commit 2362538
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected void onResume() {
.setCallBack(this)
.setLogsEnabled(true)
.setBannerTypeDialog(true)
.setSensitivity(4)
.build();

super.onResume();
Expand Down
15 changes: 13 additions & 2 deletions netwatch/src/main/java/ir/drax/netwatch/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ public Builder setLogsEnabled(boolean logsEnabled){
return this;
}

public void setMaxDelay(int maxDelay){
public Builder setMaxDelay(int maxDelay){
NetworkChangeReceiver.setGeneralPingIntervalMaxDelay(maxDelay);
return this;
}

public void setMinDelay(int minDelay){
public Builder setMinDelay(int minDelay){
NetworkChangeReceiver.setGeneralPingIntervalMinDelay(minDelay);
return this;
}

public int getMaxDelay(){
Expand All @@ -168,4 +170,13 @@ public int getMaxDelay(){
public int getMinDelay(){
return NetworkChangeReceiver.getGeneralPingIntervalMinDelay();
}

public int getSensitivity(){
return NetworkChangeReceiver.getSensitivity();
}

public Builder setSensitivity(int sensitivity){
NetworkChangeReceiver.setSensitivity(sensitivity);
return this;
}
}
45 changes: 31 additions & 14 deletions netwatch/src/main/java/ir/drax/netwatch/NetworkChangeReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import ir.drax.netwatch.cb.NetworkChangeReceiver_navigator;
Expand Down Expand Up @@ -56,11 +54,12 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
private static int notificationIcon = R.drawable.ic_nosignal;
private static NetworkChangeReceiver_navigator uiNavigator;
private static String message ;
private static int repeat = 1 ;
private static int repeat = 1, sensitivity=5 ;
private static boolean cancelable = true, notificationEnabled = true , bannerTypeDialog =false,logsEnabled=false;
private static NotificationCompat.Builder mBuilder;
private static Dialog netBanner;
private static RelativeLayout windowedDialog;
private static Ping ping;


public NetworkChangeReceiver() {
Expand All @@ -71,9 +70,10 @@ public NetworkChangeReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (LAST_STATE == getConnectivityStatus(context))return;
unchanged_counter = 0; //Reduce delay so checker will be more sensitive for a while

if (ping != null)ping.resume();

unchanged_counter = 0;
checkState(context , 2);
}

private static void detectAndAct(Context context, int status){
Expand Down Expand Up @@ -287,19 +287,23 @@ public static void setMessage(String message) {
}

public static void checkState(Context context){
LAST_STATE= -1;
checkState(context,repeat);
if (ping==null) {
LAST_STATE= -1;
repeat=1;
initPing(context,repeat);
}
// else ping.resume();
}

/**
* checkState() Tries to detects and understand connectivity in 2 available ways.
* initPing() Tries to detects and understand connectivity in 2 available ways.
*
* @param context
* @param repeat
*
* restarts ping interval to make detection more sensitive by shorter ping delays
*/
public static void checkState(final Context context, int repeat){
public static void initPing(final Context context, int repeat){
try{
if (repeat==0){
NetworkChangeReceiver.repeat = 1;
Expand All @@ -308,7 +312,8 @@ public static void checkState(final Context context, int repeat){
NetworkChangeReceiver.repeat = repeat;
}

new Ping().setCb(new Ping_navigator() {

ping=new Ping().setCb(new Ping_navigator() {
@Override
public void timeout(Context context) {
if (NetworkChangeReceiver.repeat == 1) {
Expand All @@ -320,16 +325,20 @@ public void timeout(Context context) {
public void replied(Context context) {

detectAndAct(context ,NetworkChangeReceiver.CONNECTED);
NetworkChangeReceiver.repeat = 1 ;
NetworkChangeReceiver.repeat = sensitivity;
}

@Override
public void ended(Context context) {
unchanged_counter ++;
NetworkChangeReceiver.repeat = NetworkChangeReceiver.repeat - 1;
checkState(context ,NetworkChangeReceiver.repeat );
NetworkChangeReceiver.repeat = NetworkChangeReceiver.repeat > 1 ? NetworkChangeReceiver.repeat - 1:1;
initPing(context ,NetworkChangeReceiver.repeat );
}
}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,context);
});
ping.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,context);



}catch (Exception e){
e.printStackTrace();
}
Expand Down Expand Up @@ -438,4 +447,12 @@ public static int getGeneralPingIntervalMinDelay() {
public static void setGeneralPingIntervalMinDelay(int generalPingIntervalMinDelay) {
GENERAL_PING_INTERVAL_MIN_DELAY = generalPingIntervalMinDelay;
}

public static int getSensitivity() {
return sensitivity;
}

public static void setSensitivity(int sensitivity) {
NetworkChangeReceiver.sensitivity = sensitivity;
}
}
55 changes: 37 additions & 18 deletions netwatch/src/main/java/ir/drax/netwatch/Ping.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;

Expand All @@ -10,33 +11,25 @@
class Ping extends AsyncTask<Context, Void, Context> {
private Ping_navigator cb;
private int mExitValue = 0;
private Thread activeThread;


Ping() { }
Ping() {
}

@Override
protected Context doInBackground(Context... contexts) {
try
{
activeThread=Thread.currentThread();
Thread.sleep(NetworkChangeReceiver.getDelay());
Runtime runtime = Runtime.getRuntime();
Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 "+ contexts[0].getString(R.string.netwatch_target_ping_server_ip_add));
mExitValue = mIpAddrProcess.waitFor();
if (NetworkChangeReceiver.isLogsEnabled())
System.out.println(" Ping mExitValue "+mExitValue);
}
catch (InterruptedException ignore)
{
ignore.printStackTrace();
if (NetworkChangeReceiver.isLogsEnabled())
System.out.println("Ping Exception:"+ignore);
}
catch (IOException e)
{
e.printStackTrace();
pingProcess(contexts[0]);

} catch (InterruptedException ignore) {
pingProcess(contexts[0]);
if (NetworkChangeReceiver.isLogsEnabled())
System.out.println("Ping Exception:"+e);
System.out.println("Ping Exception:"+ignore.getMessage());
}

return contexts[0];
}
@Override
Expand All @@ -52,6 +45,27 @@ protected void onPostExecute(Context context) {
super.onPostExecute(context);
}

private void pingProcess(Context context){
try {
Runtime runtime = Runtime.getRuntime();
Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 " + context.getString(R.string.netwatch_target_ping_server_ip_add));
mExitValue = mIpAddrProcess.waitFor();
if (NetworkChangeReceiver.isLogsEnabled())
System.out.println(" Ping mExitValue " + mExitValue);


} catch (InterruptedException ignore) {
ignore.printStackTrace();
if (NetworkChangeReceiver.isLogsEnabled())
System.out.println("Ping Exception:"+ignore);

}catch (IOException e) {
e.printStackTrace();
if (NetworkChangeReceiver.isLogsEnabled())
System.out.println("Ping Exception:"+e);
}
}

public Ping(Ping_navigator cb) {
this.cb = cb;
}
Expand All @@ -60,4 +74,9 @@ Ping setCb(Ping_navigator cb) {
this.cb = cb;
return this;
}

void resume(){
activeThread.interrupt();
}

}

0 comments on commit 2362538

Please sign in to comment.