Skip to content

Commit

Permalink
AsyncTask enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Siamak committed Jun 1, 2019
1 parent 2f8d57c commit 5c8a1fb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ protected void onCreate(Bundle savedInstanceState) {
.setNotificationCancelable(false)
/* setCallBack(): Network interaction events will be notified using this callback */
.setCallBack(this)
.setBannerTypeDialog(false)
.setLogsEnabled(true)
.setBannerTypeDialog(true)
.build();
}

Expand All @@ -46,9 +47,9 @@ public void onConnected(int source) {
*/
@Override
public View onDisconnected() {
statusTv.setText(R.string.disconnected);
/*statusTv.setText(R.string.disconnected);
statusTv.setTextColor(getResources().getColor(android.R.color.holo_red_light));

*/
return getLayoutInflater().inflate(R.layout.disconnected_banner,null,false);
}
}
Expand Down
8 changes: 8 additions & 0 deletions netwatch/src/main/java/ir/drax/netwatch/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,12 @@ public Builder setBannerTypeDialog(boolean bannerTypeDialog){
NetworkChangeReceiver.setBannerTypeDialog(bannerTypeDialog);
return this;
}

public boolean logsEnabled() {
return NetworkChangeReceiver.isLogsEnabled();
}
public Builder setLogsEnabled(boolean logsEnabled){
NetworkChangeReceiver.setLogsEnabled(logsEnabled);
return this;
}
}
54 changes: 29 additions & 25 deletions netwatch/src/main/java/ir/drax/netwatch/NetworkChangeReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.Gravity;
Expand Down Expand Up @@ -55,9 +54,7 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
private static NetworkChangeReceiver_navigator uiNavigator;
private static String message ;
private static int repeat = 1 ;
private static Handler pingHandler = new Handler();
private static Ping ping = new Ping();
private static boolean cancelable = true, notificationEnabled = true , bannerTypeDialog =false;
private static boolean cancelable = true, notificationEnabled = true , bannerTypeDialog =false,logsEnabled=false;
private static NotificationCompat.Builder mBuilder;
private static Dialog netBanner;
private static RelativeLayout windowedDialog;
Expand Down Expand Up @@ -88,9 +85,9 @@ private static void detectAndAct(Context context, int status){
View view = uiNavigator.onDisconnected();
if (view!=null)
if (bannerTypeDialog)
showDialogBanner(context,view);
showDialogBanner(view);
else
showWindowedBanner(context,view);
showWindowedBanner(view);
}

if (notificationEnabled) {
Expand Down Expand Up @@ -124,10 +121,10 @@ private static void detectAndAct(Context context, int status){
LAST_STATE = status;
}

private static void showWindowedBanner(Context context, View view) {
private static void showWindowedBanner(View view) {
if (view==null)return;
if (windowedDialog==null) {
windowedDialog = new RelativeLayout(context);
windowedDialog = new RelativeLayout(view.getContext());
windowedDialog.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
windowedDialog.setBackgroundColor(Color.parseColor("#44000000"));
windowedDialog.setGravity(Gravity.CENTER);
Expand Down Expand Up @@ -162,20 +159,17 @@ private static void hideBanner() {
}
}

private static void showDialogBanner(Context context, View view) {
private static void showDialogBanner(View view) {
try {
if (view == null) return;

if (netBanner == null) {
netBanner = new Dialog(context);
netBanner.requestWindowFeature(Window.FEATURE_NO_TITLE);
netBanner.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
netBanner.setContentView(view);
netBanner.setCanceledOnTouchOutside(false);
netBanner.setCancelable(false);
netBanner.getWindow().getAttributes().gravity = Gravity.CENTER;

}
netBanner = new Dialog(view.getContext());
netBanner.requestWindowFeature(Window.FEATURE_NO_TITLE);
netBanner.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
netBanner.setContentView(view);
netBanner.setCanceledOnTouchOutside(false);
netBanner.setCancelable(false);
netBanner.getWindow().getAttributes().gravity = Gravity.CENTER;

if (!netBanner.isShowing()) {
netBanner.show();
Expand Down Expand Up @@ -282,15 +276,15 @@ public static void checkState(Context context){
*
* restarts ping interval to make detection more sensitive by shorter ping delays
*/
public static void checkState(Context context,int repeat){
public static void checkState(final Context context, int repeat){
if (repeat==0){
NetworkChangeReceiver.repeat = 1;

}else {
NetworkChangeReceiver.repeat = repeat;
}
pingHandler.removeCallbacks(ping);
pingHandler.postDelayed(ping.setContext(context).setCb(new Ping_navigator() {

new Ping().setCb(new Ping_navigator() {
@Override
public void timeout(Context context) {
if (NetworkChangeReceiver.repeat == 1) {
Expand All @@ -311,10 +305,12 @@ public void ended(Context context) {
NetworkChangeReceiver.repeat = NetworkChangeReceiver.repeat - 1;
checkState(context ,NetworkChangeReceiver.repeat );
}
}),getDelay());
}).execute(context);


}

private static long getDelay() {
static long getDelay() {
long delay;

delay = unchanged_counter * unchanged_counter * GENERAL_PING_INTERVAL_MULTIPLIER_MS;
Expand All @@ -325,7 +321,7 @@ else if (delay < GENERAL_PING_INTERVAL_MIN_DELAY)
delay=GENERAL_PING_INTERVAL_MIN_DELAY;


if (BuildConfig.DEBUG)
if (logsEnabled)
Log.e(TAG , unchanged_counter+"=="+delay);
return delay;
}
Expand Down Expand Up @@ -388,4 +384,12 @@ public static boolean bannerTypeDialog() {
public static void setBannerTypeDialog(boolean bannerTypeDialog) {
NetworkChangeReceiver.bannerTypeDialog = bannerTypeDialog;
}

public static boolean isLogsEnabled() {
return logsEnabled;
}

public static void setLogsEnabled(boolean logsEnabled) {
NetworkChangeReceiver.logsEnabled = logsEnabled;
}
}
65 changes: 33 additions & 32 deletions netwatch/src/main/java/ir/drax/netwatch/Ping.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
package ir.drax.netwatch;

import android.content.Context;
import android.os.Build;
import android.os.AsyncTask;

import java.io.IOException;

import ir.drax.netwatch.cb.Ping_navigator;

class Ping implements Runnable {
class Ping extends AsyncTask<Context, Void, Context> {
private Ping_navigator cb;
private Context context;
private int mExitValue = 0;


Ping() { }

public Ping(Ping_navigator cb) {
this.cb = cb;
}

Ping setCb(Ping_navigator cb) {
this.cb = cb;
return this;
}

Ping setContext(Context context) {
this.context = context;
return this;
}

@Override
public void run() {
Runtime runtime = Runtime.getRuntime();
protected Context doInBackground(Context... contexts) {
try
{
Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 "+ context.getString(R.string.netwatch_target_ping_server_ip_add));
int mExitValue = mIpAddrProcess.waitFor();
if (BuildConfig.DEBUG)
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);
if(mExitValue==0){
cb.replied(context);

}else{
cb.timeout(context);

}
}
catch (InterruptedException ignore)
{
ignore.printStackTrace();
if (BuildConfig.DEBUG)
if (NetworkChangeReceiver.isLogsEnabled())
System.out.println("Ping Exception:"+ignore);
}
catch (IOException e)
{
e.printStackTrace();
if (BuildConfig.DEBUG)
System.out.println("Ping Exception:"+e);
if (NetworkChangeReceiver.isLogsEnabled())
System.out.println("Ping Exception:"+e);
}
return contexts[0];
}
@Override
protected void onPostExecute(Context context) {
if(mExitValue==0){
cb.replied(context);

}else{
cb.timeout(context);

}
cb.ended(context);
super.onPostExecute(context);
}

public Ping(Ping_navigator cb) {
this.cb = cb;
}

Ping setCb(Ping_navigator cb) {
this.cb = cb;
return this;
}
}

0 comments on commit 5c8a1fb

Please sign in to comment.