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
Pipeline #139110 failed with stage
in 54 seconds
...@@ -111,5 +111,16 @@ http://www.gnu.org/licenses/gpl.html ...@@ -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> </application>
</manifest> </manifest>
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
...@@ -38,6 +38,8 @@ public class ScreenOffReceiver extends BroadcastReceiver { ...@@ -38,6 +38,8 @@ public class ScreenOffReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive"); Log.i(TAG, "onReceive");
// i will use this code for check file sync list
/*String intentAction = intent.getAction(); /*String intentAction = intent.getAction();
if(intentAction == null){ if(intentAction == null){
Log.e(TAG, "intent Action is null"); Log.e(TAG, "intent Action is null");
......
...@@ -15,6 +15,7 @@ import android.content.Context; ...@@ -15,6 +15,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
...@@ -33,6 +34,7 @@ import java.util.List; ...@@ -33,6 +34,7 @@ import java.util.List;
import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.models.SyncedFolder;
import foundation.e.drive.operations.CreateInitialFolderRemoteOperation; import foundation.e.drive.operations.CreateInitialFolderRemoteOperation;
import foundation.e.drive.receivers.ConnectivityReceiver;
import foundation.e.drive.receivers.ScreenOffReceiver; import foundation.e.drive.receivers.ScreenOffReceiver;
import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.CommonUtils;
...@@ -47,7 +49,8 @@ import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES ...@@ -47,7 +49,8 @@ import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES
/** /**
* @author Vincent Bourgmayer * @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(); final private String TAG = InitializerService.class.getSimpleName();
//Complex properties //Complex properties
private int existingRemoteFolderCounter; //@dev-only; Temporarily used to know if all remotePath exist private int existingRemoteFolderCounter; //@dev-only; Temporarily used to know if all remotePath exist
...@@ -56,6 +59,7 @@ public class InitializerService extends Service implements OnRemoteOperationList ...@@ -56,6 +59,7 @@ public class InitializerService extends Service implements OnRemoteOperationList
private Handler mHandler; private Handler mHandler;
private Account mAccount; private Account mAccount;
private int restartFolderCreationCounter =0; private int restartFolderCreationCounter =0;
private ConnectivityReceiver connectivityReceiver;
@Override @Override
public void onCreate() { public void onCreate() {
...@@ -63,8 +67,16 @@ public class InitializerService extends Service implements OnRemoteOperationList ...@@ -63,8 +67,16 @@ public class InitializerService extends Service implements OnRemoteOperationList
super.onCreate(); super.onCreate();
this.existingRemoteFolderCounter = 0; this.existingRemoteFolderCounter = 0;
//JobUtils.scheduleInitializerJob(getApplicationContext()); //JobUtils.scheduleInitializerJob(getApplicationContext());
connectivityReceiver = new ConnectivityReceiver();
registerConnectivityReceiver();
} }
private void registerConnectivityReceiver() {
registerReceiver(connectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override @Override
public int onStartCommand( Intent intent, int flags, int startId ) { public int onStartCommand( Intent intent, int flags, int startId ) {
Log.i(TAG, "onStartCommand(...)"); Log.i(TAG, "onStartCommand(...)");
...@@ -311,6 +323,14 @@ public class InitializerService extends Service implements OnRemoteOperationList ...@@ -311,6 +323,14 @@ public class InitializerService extends Service implements OnRemoteOperationList
this.mCloudClient = null; this.mCloudClient = null;
if(this.mSyncedFolders != null) this.mSyncedFolders.clear(); if(this.mSyncedFolders != null) this.mSyncedFolders.clear();
this.mSyncedFolders = null; this.mSyncedFolders = null;
unregisterConnectivityReceiver();
}
protected void unregisterConnectivityReceiver() {
try {
unregisterReceiver(connectivityReceiver);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
} }
@Nullable @Nullable
...@@ -318,4 +338,12 @@ public class InitializerService extends Service implements OnRemoteOperationList ...@@ -318,4 +338,12 @@ public class InitializerService extends Service implements OnRemoteOperationList
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
return null; 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
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