Skip to content

Commit

Permalink
Ethernet: modify broadcast receiver to receive connectivity changes
Browse files Browse the repository at this point in the history
Previously the network connectivity receiver was only created along the
Activity where it is now started at bootup and stays in background.

Signed-off-by: Gary Bisson <gary.bisson@boundarydevices.com>
  • Loading branch information
gibsson committed May 25, 2015
1 parent ec9aed1 commit b972bc8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 68 deletions.
3 changes: 2 additions & 1 deletion Ethernet/AndroidManifest.xml
Expand Up @@ -27,11 +27,12 @@
</intent-filter>
</activity>
<receiver
android:name=".EthernetBootReceiver"
android:name=".EthernetReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
Expand Down
38 changes: 0 additions & 38 deletions Ethernet/com/fsl/ethernet/EthernetBootReceiver.java

This file was deleted.

6 changes: 0 additions & 6 deletions Ethernet/com/fsl/ethernet/EthernetManager.java
Expand Up @@ -21,7 +21,6 @@
import java.util.Iterator;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
import android.content.Context;
Expand All @@ -40,8 +39,6 @@
import android.net.LinkProperties;
import android.net.InterfaceConfiguration;
import android.net.ProxyProperties;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.Parcel;
import android.os.SystemProperties;
Expand Down Expand Up @@ -100,7 +97,6 @@ public class EthernetManager {
private EthernetDataTracker mTracker;
private INetworkManagementService mNMService;
private DhcpInfo mDhcpInfo;
private Handler mTrackerTarget;
private String mode;
private String ip_address;
private String dns_address;
Expand Down Expand Up @@ -133,8 +129,6 @@ public EthernetManager(Context context) {
Log.e(TAG, "Could not get list of interfaces " + e);
}

HandlerThread dhcpThread = new HandlerThread("DHCP Handler Thread");
dhcpThread.start();
mDhcpInfo = new DhcpInfo();
}

Expand Down
59 changes: 59 additions & 0 deletions Ethernet/com/fsl/ethernet/EthernetReceiver.java
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2015 Boundary Devices, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.fsl.ethernet;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.util.Log;
import com.fsl.ethernet.EthernetManager;

public class EthernetReceiver extends BroadcastReceiver {

private static final String TAG = "EthernetReceiver";

@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive " + intent.getAction());

if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.d(TAG, "resetting interface");
EthernetManager ethManager = new EthernetManager(context);
ethManager.resetInterface();
} else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
NetworkInfo info =
intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (info != null) {
if (info.getType() == ConnectivityManager.TYPE_ETHERNET) {
Log.i(TAG,"Ethernet state: " + info.getState());
if (info.getState() == State.DISCONNECTED) {
ConnectivityManager connMgr = (ConnectivityManager)context.
getSystemService(Context.CONNECTIVITY_SERVICE);
connMgr.setGlobalProxy(null);
} else if (info.getState() == State.CONNECTED) {
EthernetManager ethManager = new EthernetManager(context);
ethManager.resetInterface();
ethManager.initProxy();
}
}
}
}
}
}
23 changes: 0 additions & 23 deletions Ethernet/com/fsl/ethernet/MainActivity.java
Expand Up @@ -45,32 +45,13 @@ public class MainActivity extends Activity {
private Button mBtnConfig;
private Button mBtnCheck;
private EthernetDevInfo mSaveConfig;
private ConnectivityManager mConnMgr;
private String TAG = "EthernetMainActivity";
private static String Mode_dhcp = "dhcp";
private boolean shareprefences_flag = false;
private boolean first_run = true;
public static final String FIRST_RUN = "ethernet";
private Button mBtnAdvanced;
private EthernetAdvDialog mEthAdvancedDialog;
private final BroadcastReceiver mEthernetReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){
NetworkInfo info =
intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (info != null) {
Log.i(TAG,"getState()="+info.getState() + "getType()=" +
info.getType());
if (info.getType() == ConnectivityManager.TYPE_ETHERNET) {
if (info.getState() == State.DISCONNECTED)
mConnMgr.setGlobalProxy(null);
if (info.getState() == State.CONNECTED)
mEthEnabler.getManager().initProxy();
}
}
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -86,10 +67,8 @@ protected void onCreate(Bundle savedInstanceState) {
addListenerOnBtnConfig();
addListenerOnBtnCheck();
addListenerOnBtnAdvanced();
mConnMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mEthernetReceiver, filter);
}


Expand Down Expand Up @@ -153,7 +132,5 @@ public void onClick(View v) {
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onStop() will force clear global proxy set by ethernet");
mConnMgr.setGlobalProxy(null);
unregisterReceiver(mEthernetReceiver);
}
}

0 comments on commit b972bc8

Please sign in to comment.