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

Commit 799ae4ea authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Merge branch 'issue_122_networkListener' into 'oreo_fileObserver'

Issue 122 network listener

See merge request e/apps/eDrive!48
parents 01af5c48 590fa448
Loading
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -111,5 +111,16 @@ http://www.gnu.org/licenses/gpl.html

        <!--.....-->

        <receiver
            android:name=".receivers.ConnectivityReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
                <action android:name="android.net.wifi.STATE_CHANGE"/>
            </intent-filter>
        </receiver>


    </application>
</manifest>
+53 −0
Original line number Diff line number Diff line
package foundation.e.drive.receivers;

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

import java.io.IOException;

public class ConnectivityReceiver
        extends BroadcastReceiver {

    public static ConnectivityReceiverListener connectivityReceiverListener;

    public ConnectivityReceiver() {
        super();
    }

    public  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;
        }
    }

    @Override
    public void onReceive(Context context, Intent arg1) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        boolean isConnected = activeNetwork != null
                && activeNetwork.isConnectedOrConnecting();

        //if (connectivityReceiverListener != null) {
        Log.e("TAG", "ConnectivityReceiver onNetworkConnectionChanged...." + isConnected);
        //connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
        //}
    }

    public interface ConnectivityReceiverListener {
        void onNetworkConnectionChanged(boolean isConnected);
    }
}
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ public class ScreenOffReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive");
        // i will use this code for check file sync list

        /*String intentAction = intent.getAction();
        if(intentAction == null){
            Log.e(TAG, "intent Action is null");
+29 −1
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
@@ -33,6 +34,7 @@ import java.util.List;

import foundation.e.drive.models.SyncedFolder;
import foundation.e.drive.operations.CreateInitialFolderRemoteOperation;
import foundation.e.drive.receivers.ConnectivityReceiver;
import foundation.e.drive.receivers.ScreenOffReceiver;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;
@@ -47,7 +49,8 @@ import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES
/**
 * @author Vincent Bourgmayer
 */
public class InitializerService extends Service implements OnRemoteOperationListener {
public class InitializerService extends Service
        implements OnRemoteOperationListener, ConnectivityReceiver.ConnectivityReceiverListener {
    final private String TAG = InitializerService.class.getSimpleName();
    //Complex properties
    private int existingRemoteFolderCounter; //@dev-only; Temporarily used to know if all remotePath exist
@@ -56,6 +59,7 @@ public class InitializerService extends Service implements OnRemoteOperationList
    private Handler mHandler;
    private Account mAccount;
    private int restartFolderCreationCounter =0;
    private ConnectivityReceiver connectivityReceiver;

    @Override
    public void onCreate() {
@@ -63,8 +67,16 @@ public class InitializerService extends Service implements OnRemoteOperationList
        super.onCreate();
        this.existingRemoteFolderCounter = 0;
        //JobUtils.scheduleInitializerJob(getApplicationContext());
        connectivityReceiver = new ConnectivityReceiver();
        registerConnectivityReceiver();

    }

    private void registerConnectivityReceiver() {

        registerReceiver(connectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    }
    @Override
    public int onStartCommand( Intent intent, int flags, int startId ) {
        Log.i(TAG, "onStartCommand(...)");
@@ -311,6 +323,14 @@ public class InitializerService extends Service implements OnRemoteOperationList
        this.mCloudClient = null;
        if(this.mSyncedFolders != null) this.mSyncedFolders.clear();
        this.mSyncedFolders = null;
        unregisterConnectivityReceiver();
    }
    protected void unregisterConnectivityReceiver() {
        try {
            unregisterReceiver(connectivityReceiver);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    @Nullable
@@ -318,4 +338,12 @@ public class InitializerService extends Service implements OnRemoteOperationList
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onNetworkConnectionChanged(boolean isConnected) {

        //just for testing code for now
        Log.e("TAG", "onNetworkConnectionChanged...." + isConnected);

    }
}
 No newline at end of file