Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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

implement handler for maintain connection

parent 372201e6
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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"
+1 −1
Original line number Diff line number Diff line
@@ -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);
+45 −38
Original line number Diff line number Diff line
@@ -6,50 +6,54 @@ 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();
        final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        boolean isConnected =isConnected(context);

        //if (connectivityReceiverListener != null) {
        Log.e("TAG", "ConnectivityReceiver onNetworkConnectionChanged...." + isConnected);
        //connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
        //}
        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) {
@@ -67,6 +71,9 @@ public class ConnectivityReceiver
                    InitializerService.schedulerFlag = false;
                    InitializerService.fileObserverFlag = false;
                }
            }
        }, 20000);


    }

+1 −1
Original line number Diff line number Diff line
@@ -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){
+25 −0
Original line number Diff line number Diff line
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