author | Zhiwei Gong <zhiwei.gong@amlogic.com> | 2015-08-28 04:14:42 (GMT) |
---|---|---|
committer | zhiwei.gong <zhiwei.gong@amlogic.com> | 2015-09-28 08:15:45 (GMT) |
commit | 9697b8f39330fbac624e4b624b68cee0537b1391 (patch) | |
tree | fe286049ca8eb5b44a52555b9b21294a75b81cf8 | |
parent | e0c72efb625454fd281fc886aff5695498b5630c (diff) | |
download | PPPoE-9697b8f39330fbac624e4b624b68cee0537b1391.zip PPPoE-9697b8f39330fbac624e4b624b68cee0537b1391.tar.gz PPPoE-9697b8f39330fbac624e4b624b68cee0537b1391.tar.bz2 |
PD#111704:PD#111707:fix pppoe Autoconnect error
Change-Id: Ia8d3f6cc333dd771c833da34cfcbc0616a0bd92d
-rw-r--r-- | src/com/droidlogic/PPPoE/MyPppoeService.java | 115 | ||||
-rw-r--r-- | src/com/droidlogic/PPPoE/PPPoEActivity.java | 49 | ||||
-rw-r--r-- | src/com/droidlogic/PPPoE/PppoeBroadcastReceiver.java | 102 | ||||
-rw-r--r-- | src/com/droidlogic/PPPoE/PppoeConfigDialog.java | 9 |
4 files changed, 135 insertions, 140 deletions
diff --git a/src/com/droidlogic/PPPoE/MyPppoeService.java b/src/com/droidlogic/PPPoE/MyPppoeService.java index be0e584..31ae423 100644 --- a/src/com/droidlogic/PPPoE/MyPppoeService.java +++ b/src/com/droidlogic/PPPoE/MyPppoeService.java @@ -10,38 +10,48 @@ import android.content.Intent; import android.content.BroadcastReceiver; import android.content.IntentFilter; import android.content.Context; +import android.content.SharedPreferences; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.util.Log; -import com.amlogic.pppoe.PppoeOperation; -import com.android.server.net.BaseNetworkObserver; import android.os.INetworkManagementService; import android.os.ServiceManager; import android.os.RemoteCallbackList; import android.os.RemoteException; - +import android.net.wifi.WifiManager; +import com.amlogic.pppoe.PppoeOperation; +import com.android.server.net.BaseNetworkObserver; +import com.droidlogic.app.SystemControlManager; public class MyPppoeService extends Service { private static final String TAG = "MyPppoeService"; + private static final String eth_device_sysfs = "/sys/class/ethernet/linkspeed"; private NotificationManager mNM; - private Handler mHandler; + private WifiManager mWifiManager; private Handler mPppoeHandler; private PppoeOperation operation = null; private InterfaceObserver mInterfaceObserver; private INetworkManagementService mNMService; + private boolean mScreenon; + private boolean mConnected; + private int mTimes; public static final int MSG_PPPOE_START = 0xabcd0080; - + private static final int PPPOE_DELAYD = 10000; + private Context mContext; + private SystemControlManager mSystemControlManager; @Override public void onCreate() { - Log.d(TAG, ">>>>>>onCreate"); + Log.d(TAG, "onCreate"); IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); mNMService = INetworkManagementService.Stub.asInterface(b); - + mScreenon=false; + mContext=this; mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - - mHandler = new DMRHandler(); + mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); + mSystemControlManager = new SystemControlManager(mContext); + operation = new PppoeOperation(); mPppoeHandler = new PppoeHandler(); mInterfaceObserver = new InterfaceObserver(); try { @@ -49,10 +59,6 @@ public class MyPppoeService extends Service } catch (RemoteException e) { Log.e(TAG, "Could not register InterfaceObserver " + e); } - - /* start check after 5s */ - mHandler.sendEmptyMessageDelayed(0, 5000); - IntentFilter f = new IntentFilter(); f.addAction(Intent.ACTION_SHUTDOWN); @@ -82,27 +88,41 @@ public class MyPppoeService extends Service this.sendBroadcast(intent); } } - - private class InterfaceObserver extends BaseNetworkObserver { - @Override - public void interfaceLinkStateChanged(String iface, boolean up) { - updateInterfaceState(iface, up); + private boolean getAutoDialFlag(Context context) { + SharedPreferences sharedata = context.getSharedPreferences("inputdata", 0); + if (sharedata != null && sharedata.getAll().size() > 0) + { + return sharedata.getBoolean(PppoeConfigDialog.INFO_AUTO_DIAL_FLAG, false); } + return false; + } + private boolean isEthDeviceAdded(Context context) { + String str = mSystemControlManager.readSysFs(eth_device_sysfs); + if (str == null) + return false ; + if (str.contains("unlink")) { + return false; + }else{ + return true; + } } - private class DMRHandler extends Handler - { + private class InterfaceObserver extends BaseNetworkObserver { @Override - public void handleMessage(Message msg) { - super.handleMessage(msg); - - Log.d(TAG, "handleMessage"); - /* check per 10s */ - mHandler.sendEmptyMessageDelayed(0, 1000000); + public void interfaceLinkStateChanged(String iface, boolean up) { + Log.d(TAG , "interfaceLinkStateChanged+"+iface+"up"+up); + if (iface.contains("eth0") && up) { + if (!mScreenon) { + if (getAutoDialFlag(mContext)) { + mPppoeHandler.sendEmptyMessageDelayed(MSG_PPPOE_START, PPPOE_DELAYD); + } + } + } else if (iface.contains("ppp0")) { + mConnected = up; + } } } - private class PppoeHandler extends Handler { @Override @@ -110,7 +130,26 @@ public class MyPppoeService extends Service super.handleMessage(msg); switch (msg.what) { case MSG_PPPOE_START: - updateInterfaceState("eth0", true); + if (!mConnected) { + int wifiState = mWifiManager.getWifiState(); + if ((wifiState == WifiManager.WIFI_STATE_ENABLING) || + (wifiState == WifiManager.WIFI_STATE_ENABLED)) { + mWifiManager.setWifiEnabled(false); + } else { + if (isEthDeviceAdded(mContext)) + updateInterfaceState("eth0", true); + } + if (mTimes < 3) { + mPppoeHandler.sendEmptyMessageDelayed(MSG_PPPOE_START, PPPOE_DELAYD+mTimes*1000); + mTimes++; + } else { + mScreenon = false; + mTimes = 0; + } + } else { + mScreenon = false; + mTimes = 0; + } break; default: Log.d(TAG, "handleMessage: " + msg.what); @@ -123,14 +162,18 @@ public class MyPppoeService extends Service @Override public void onReceive(Context context, Intent intent) { Log.d(TAG , "onReceive :" +intent.getAction()); - if ((Intent.ACTION_SCREEN_OFF).equals(intent.getAction())) { - operation = new PppoeOperation(); - operation.disconnect(); - } - if ((Intent.ACTION_SCREEN_ON).equals(intent.getAction())) { - operation = new PppoeOperation(); - operation.disconnect(); - mPppoeHandler.sendEmptyMessageDelayed(MSG_PPPOE_START, 10000); + mScreenon = false; + if (getAutoDialFlag(mContext)) { + if ((Intent.ACTION_SCREEN_OFF).equals(intent.getAction())) { + mScreenon = true; + operation.disconnect(); + } + if ((Intent.ACTION_SCREEN_ON).equals(intent.getAction())) { + mScreenon = true; + mConnected = false; + mTimes=0; + mPppoeHandler.sendEmptyMessageDelayed(MSG_PPPOE_START, PPPOE_DELAYD); + } } } }; diff --git a/src/com/droidlogic/PPPoE/PPPoEActivity.java b/src/com/droidlogic/PPPoE/PPPoEActivity.java index 2fdb31d..d94f6e0 100644 --- a/src/com/droidlogic/PPPoE/PPPoEActivity.java +++ b/src/com/droidlogic/PPPoE/PPPoEActivity.java @@ -34,8 +34,6 @@ public class PPPoEActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.main); - Log.d(TAG, "Create PppoeConfigDialog"); mSystemControlManager = new SystemControlManager(this); String eth_link = mSystemControlManager.readSysFs("/sys/class/ethernet/linkspeed"); @@ -44,36 +42,35 @@ public class PPPoEActivity extends Activity { toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); finish(); - } - mPppoeConfigDialog = new PppoeConfigDialog(this); - ConnectivityManager cm = (ConnectivityManager)this.getSystemService - ( Context.CONNECTIVITY_SERVICE); - - NetworkInfo info = cm.getActiveNetworkInfo(); - if (info != null) { - Log.d(TAG, info.toString()); - } - - IBinder b = ServiceManager.getService("pppoe"); - IPppoeManager PppoeService = IPppoeManager.Stub.asInterface(b); - mPppoeManager = new PppoeManager(PppoeService, this); - - mPppoeInfo = mPppoeManager.getSavedPppoeConfig(); - if (mPppoeInfo != null) { - Log.d(TAG, "IP: " + mPppoeInfo.getIpAddress()); - Log.d(TAG, "MASK: " + mPppoeInfo.getNetMask()); - Log.d(TAG, "GW: " + mPppoeInfo.getRouteAddr()); - Log.d(TAG, "DNS: " + mPppoeInfo.getDnsAddr()); - } - if (mPppoeConfigDialog != null) { - Log.d(TAG, "Show PppoeConfigDialog"); - mPppoeConfigDialog.show(); + } else { + setContentView(R.layout.main); + mPppoeConfigDialog = new PppoeConfigDialog(this); + ConnectivityManager cm = (ConnectivityManager)this.getSystemService( Context.CONNECTIVITY_SERVICE); + NetworkInfo info = cm.getActiveNetworkInfo(); + if (info != null) { + Log.d(TAG, info.toString()); + } + IBinder b = ServiceManager.getService("pppoe"); + IPppoeManager PppoeService = IPppoeManager.Stub.asInterface(b); + mPppoeManager = new PppoeManager(PppoeService, this); + mPppoeInfo = mPppoeManager.getSavedPppoeConfig(); + if (mPppoeInfo != null) { + Log.d(TAG, "IP: " + mPppoeInfo.getIpAddress()); + Log.d(TAG, "MASK: " + mPppoeInfo.getNetMask()); + Log.d(TAG, "GW: " + mPppoeInfo.getRouteAddr()); + Log.d(TAG, "DNS: " + mPppoeInfo.getDnsAddr()); + } + if (mPppoeConfigDialog != null) { + Log.d(TAG, "Show PppoeConfigDialog"); + mPppoeConfigDialog.show(); + } } } @Override public void onDestroy() { + super.onDestroy(); if (mPppoeConfigDialog != null) mPppoeConfigDialog.dismiss(); mPppoeConfigDialog=null; diff --git a/src/com/droidlogic/PPPoE/PppoeBroadcastReceiver.java b/src/com/droidlogic/PPPoE/PppoeBroadcastReceiver.java index 1facc26..088567e 100644 --- a/src/com/droidlogic/PPPoE/PppoeBroadcastReceiver.java +++ b/src/com/droidlogic/PPPoE/PppoeBroadcastReceiver.java @@ -24,10 +24,7 @@ public class PppoeBroadcastReceiver extends BroadcastReceiver { "android.intent.action.BOOT_COMPLETED"; public static final String ETH_STATE_CHANGED_ACTION = "android.net.ethernet.ETH_STATE_CHANGED"; - public static final String EXTRA_ETH_STATE = "eth_state"; - public static final int EVENT_HW_PHYCONNECTED = 5; - public static final int EVENT_HW_DISCONNECTED = 4; - public static final int EVENT_HW_CONNECTED = 3; + public static final int DELAY_TIME = 2000; private Handler mHandler = null; private boolean mAutoDialFlag = false; private String mInterfaceSelected = null; @@ -88,9 +85,9 @@ public class PppoeBroadcastReceiver extends BroadcastReceiver { mUserName = getUserName(context); mPassword = getPassword(context); if (ACTION_BOOT_COMPLETED.equals(intent.getAction())) { - context.startService(new Intent(context, - MyPppoeService.class)); - mFirstAutoDialDone = true; + context.startService(new Intent(context, + MyPppoeService.class)); + mFirstAutoDialDone = true; } if (null == mInterfaceSelected @@ -99,7 +96,6 @@ public class PppoeBroadcastReceiver extends BroadcastReceiver { || null == mPassword) { mFirstAutoDialDone = false; return; - } if (mHandler == null) { @@ -108,63 +104,20 @@ public class PppoeBroadcastReceiver extends BroadcastReceiver { if (operation == null) { operation = new PppoeOperation(); } - Log.d(TAG , ">>>>>onReceive :" +intent.getAction()); + Log.d(TAG , "onReceive :" +intent.getAction()); if ("com.droidlogic.linkchange".equals(action) || mFirstAutoDialDone) { - mFirstAutoDialDone = false; - if (!mInterfaceSelected.startsWith("eth") ) - return; - int event = intent.getIntExtra(EXTRA_ETH_STATE, -1); - if (event == EVENT_HW_PHYCONNECTED) { - Log.d(TAG, "EVENT_HW_PHYCONNECTED"); - if (mMandatoryDialTimer != null) { - mMandatoryDialTimer.cancel(); - mMandatoryDialTimer = null; - } - - Log.d(TAG, "EVENT_HW_PHYCONNECTED trigger AUTO DIAL"); - - set_pppoe_running_flag(); - operation.terminate(); - operation.disconnect(); - mHandler.sendEmptyMessageDelayed(PPPoEActivity.MSG_START_DIAL, 30000); - } - else { - if (event == EVENT_HW_DISCONNECTED ) { - Log.d(TAG, "EVENT_HW_DISCONNECTED"); - } - else if (event == EVENT_HW_CONNECTED ) - Log.d(TAG, "EVENT_HW_CONNECTED"); - else - Log.d(TAG, "EVENT=" + event); - - if (event != EVENT_HW_DISCONNECTED) { - Log.d(TAG, "EVENT_HW_PHYCONNECTED LOST"); - mMandatoryDialTimer = new Timer(); - TimerTask check_task = new TimerTask() { - public void run() - { - Message message = new Message(); - message.what = PPPoEActivity.MSG_MANDATORY_DIAL; - Log.d(TAG, "Send MSG_MANDATORY_DIAL"); - mHandler.sendMessage(message); - } - }; - - //Timeout after 5 seconds - mMandatoryDialTimer.schedule(check_task, 5000); - } + if (mFirstAutoDialDone) { + mHandler.sendEmptyMessageDelayed(PPPoEActivity.MSG_MANDATORY_DIAL, DELAY_TIME); + mFirstAutoDialDone = false; + } else { + if (!mInterfaceSelected.startsWith("eth") ) + return; + //Timeout after 5 seconds + mHandler.sendEmptyMessageDelayed(PPPoEActivity.MSG_START_DIAL, DELAY_TIME); } } } - public static boolean isEthConnected(Context context) { - ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - State mEthState = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET).getState(); - if (State.CONNECTED == mEthState) { - return true; - }else{ - return false; - } - } + void set_pppoe_running_flag() { @@ -192,27 +145,24 @@ public class PppoeBroadcastReceiver extends BroadcastReceiver { super.handleMessage(msg); switch (msg.what) { - case PPPoEActivity.MSG_MANDATORY_DIAL: - Log.d(TAG, "handleMessage: MSG_MANDATORY_DIAL"); - set_pppoe_running_flag(); - operation.terminate(); - - operation.disconnect(); - - mHandler.sendEmptyMessageDelayed(PPPoEActivity.MSG_START_DIAL, 2000); + case PPPoEActivity.MSG_MANDATORY_DIAL: + Log.d(TAG, "handleMessage: MSG_MANDATORY_DIAL"); + set_pppoe_running_flag(); + operation.terminate(); + operation.disconnect(); + mHandler.sendEmptyMessageDelayed(PPPoEActivity.MSG_START_DIAL, DELAY_TIME); break; - case PPPoEActivity.MSG_START_DIAL: - Log.d(TAG, "handleMessage: MSG_START_DIAL"); - operation.connect(mInterfaceSelected, mUserName, mPassword); + case PPPoEActivity.MSG_START_DIAL: + Log.d(TAG, "handleMessage: MSG_START_DIAL"); + set_pppoe_running_flag(); + operation.connect(mInterfaceSelected, mUserName, mPassword); break; - default: - Log.d(TAG, "handleMessage: " + msg.what); + default: + Log.d(TAG, "handleMessage: " + msg.what); break; } - - } } } diff --git a/src/com/droidlogic/PPPoE/PppoeConfigDialog.java b/src/com/droidlogic/PPPoE/PppoeConfigDialog.java index 4b9db41..5b08ae4 100644 --- a/src/com/droidlogic/PPPoE/PppoeConfigDialog.java +++ b/src/com/droidlogic/PPPoE/PppoeConfigDialog.java @@ -64,7 +64,7 @@ public class PppoeConfigDialog extends AlertDialog implements DialogInterface.On public static final String INFO_NETWORK_INTERFACE_SELECTED = "network_if_selected"; public static final String INFO_AUTO_DIAL_FLAG = "auto_dial_flag"; - private final String TAG = "PppoeCfgDlg"; + private final String TAG = "PppoeConfigDialog"; private View mView; private EditText mPppoeName; private EditText mPppoePasswd; @@ -363,6 +363,9 @@ public class PppoeConfigDialog extends AlertDialog implements DialogInterface.On void showWaitDialog(int id) { + if (waitDialog == null) { + return; + } waitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); waitDialog.setTitle(""); waitDialog.setMessage(this.context.getResources().getString(id)); @@ -488,7 +491,8 @@ public class PppoeConfigDialog extends AlertDialog implements DialogInterface.On case PPPoEActivity.MSG_CONNECT_TIMEOUT: Log.d(TAG, "handleMessage: MSG_CONNECT_TIMEOUT"); pppoe_state = PPPOE_STATE_CONNECT_FAILED; - waitDialog.cancel(); + if (waitDialog != null) + waitDialog.cancel(); showAlertDialog(context.getResources().getString(R.string.pppoe_connect_failed)); SystemProperties.set("net.pppoe.isConnected", "false"); break; @@ -699,6 +703,7 @@ public class PppoeConfigDialog extends AlertDialog implements DialogInterface.On Log.d(TAG, "unregisterReceiver pppoeReceiver"); context.unregisterReceiver(pppoeReceiver); } + ((PPPoEActivity)context).onDestroy(); ((PPPoEActivity)context).finish(); /* |