Commit 697ec137 authored by narinder Rana's avatar narinder Rana
Browse files

implement handler for maintain connection

parent 372201e6
Pipeline #135493 failed with stage
in 1 minute and 21 seconds
......@@ -24,6 +24,7 @@ http://www.gnu.org/licenses/gpl.html
android:protectionLevel="signature" />
<application
android:name=".services.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_eelo"
android:label="@string/app_name"
......
......@@ -35,7 +35,7 @@ public class ScannerJob extends JobService {
filter.addAction(Intent.ACTION_SCREEN_OFF);
getApplicationContext().registerReceiver(ScreenOffReceiver.getInstance(), filter);
if(ConnectivityReceiver.isConnected(getApplicationContext())){
if(ConnectivityReceiver.isConnected()){
Intent observerServiceIntent = new Intent(this, ObserverService.class);
this.startService(observerServiceIntent);
jobFinished(params, false);
......
......@@ -6,67 +6,74 @@ import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import java.io.IOException;
import foundation.e.drive.services.FileObserverService;
import foundation.e.drive.services.InitializerService;
import foundation.e.drive.services.MyApplication;
public class ConnectivityReceiver
extends BroadcastReceiver {
public static ConnectivityReceiverListener connectivityReceiverListener;
public boolean isConnected;
public ConnectivityReceiver() {
super();
}
public static boolean isConnected(Context context) {
// String command = "ping -c 1 e.foundation";
// return (Runtime.getRuntime().exec(command).waitFor() == 0);
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
return (netInfo != null && netInfo.isConnected());
} catch (NullPointerException e) {
e.printStackTrace();
return false;
}
public static boolean isConnected() {
ConnectivityManager
cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
}
@Override
public void onReceive(Context context, Intent arg1) {
public void onReceive(final Context context, Intent arg1) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
// boolean isConnected = activeNetwork != null
// && activeNetwork.isConnectedOrConnecting();
boolean isConnected =isConnected(context);
//if (connectivityReceiverListener != null) {
Log.e("TAG", "ConnectivityReceiver onNetworkConnectionChanged...." + isConnected);
//connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
//}
if(isConnected){
Intent observersServiceIntent = new Intent(context, foundation.e.drive.services.ObserverService.class);
if(InitializerService.schedulerFlag){
context.startService(observersServiceIntent);
}else if(InitializerService.fileObserverFlag) {
//
Bundle mBundle = new Bundle();
mBundle.putBoolean("isFileObserverService", true);
observersServiceIntent.putExtras(mBundle);
context.startService(observersServiceIntent);
final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
isConnected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
if (connectivityReceiverListener != null) {
Log.e("TAG", "connectivityReceiverListener...." + isConnected);
connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
}
if (isConnected) {
Intent observersServiceIntent = new Intent(context, foundation.e.drive.services.ObserverService.class);
if (InitializerService.schedulerFlag) {
context.startService(observersServiceIntent);
} else if (InitializerService.fileObserverFlag) {
//
Bundle mBundle = new Bundle();
mBundle.putBoolean("isFileObserverService", true);
observersServiceIntent.putExtras(mBundle);
context.startService(observersServiceIntent);
}
InitializerService.schedulerFlag = false;
InitializerService.fileObserverFlag = false;
}
}
}, 20000);
InitializerService.schedulerFlag=false;
InitializerService.fileObserverFlag=false;
}
}
......
......@@ -75,7 +75,7 @@ public class FileObserverService extends Service {
//InitializerService.fileObserver.add(file);
//call to ObserverService >> getSyncedFileState >> HandleLocal File
if(ConnectivityReceiver.isConnected(getApplicationContext())){
if(ConnectivityReceiver.isConnected()){
try
{
if(observerFlag == -1){
......
package foundation.e.drive.services;
import android.app.Application;
import foundation.e.drive.receivers.ConnectivityReceiver;
public class MyApplication extends Application {
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
ConnectivityReceiver.connectivityReceiverListener = listener;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment