author | Zhiwei Gong <zhiwei.gong@amlogic.com> | 2014-12-31 09:28:42 (GMT) |
---|---|---|
committer | zhiwei.gong <zhiwei.gong@amlogic.com> | 2015-01-19 02:08:29 (GMT) |
commit | 6bce7cbd8fc939c7b4370ba0cd9887bdf790b925 (patch) | |
tree | d2787b4c96b3d60e3bb89bdab71b1cb335a12395 | |
parent | e7e92f76dd503a3f5306d950d7f57be81577c49b (diff) | |
download | PPPoE-6bce7cbd8fc939c7b4370ba0cd9887bdf790b925.zip PPPoE-6bce7cbd8fc939c7b4370ba0cd9887bdf790b925.tar.gz PPPoE-6bce7cbd8fc939c7b4370ba0cd9887bdf790b925.tar.bz2 |
add pppoe apk
Change-Id: I9b92a889e8ee358e9e7cd2c54b007a3b13241c0b
-rw-r--r--[-rwxr-xr-x] | Android.mk | 2 | ||||
-rw-r--r--[-rwxr-xr-x] | AndroidManifest.xml | 4 | ||||
-rw-r--r--[-rwxr-xr-x] | res/drawable-hdpi/icon.png | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | res/drawable-ldpi/icon.png | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | res/drawable-mdpi/icon.png | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | res/layout/main.xml | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | res/layout/pppoe_configure.xml | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | res/values-zh-rCN/strings.xml | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | res/values-zh-rTW/strings.xml | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | res/values/strings.xml | 0 | ||||
-rwxr-xr-x | src/com/amlogic/PPPoE/PppoeBroadcastReceiver.java | 167 | ||||
-rw-r--r--[-rwxr-xr-x] | src/com/droidlogic/PPPoE/MyPppoeService.java (renamed from src/com/amlogic/PPPoE/MyPppoeService.java) | 14 | ||||
-rw-r--r--[-rwxr-xr-x] | src/com/droidlogic/PPPoE/PPPoEActivity.java (renamed from src/com/amlogic/PPPoE/PPPoEActivity.java) | 30 | ||||
-rw-r--r-- | src/com/droidlogic/PPPoE/PppoeBroadcastReceiver.java | 223 | ||||
-rw-r--r--[-rwxr-xr-x] | src/com/droidlogic/PPPoE/PppoeConfigDialog.java (renamed from src/com/amlogic/PPPoE/PppoeConfigDialog.java) | 1360 |
15 files changed, 980 insertions, 820 deletions
diff --git a/Android.mk b/Android.mk index d212f1f..157af4b 100755..100644 --- a/Android.mk +++ b/Android.mk @@ -2,7 +2,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional -LOCAL_JAVA_LIBRARIES := amlogic.pppoe +LOCAL_JAVA_LIBRARIES := droidlogic.external.pppoe droidlogic droidlogic.frameworks.pppoe LOCAL_SRC_FILES := $(call all-java-files-under, src) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index f197bb5..b097164 100755..100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" - package="com.amlogic.PPPoE" + package="com.droidlogic.PPPoE" android:versionCode="1" android:versionName="1.0" android:sharedUserId="android.uid.system"> @@ -65,7 +65,7 @@ <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.net.ethernet.ETH_STATE_CHANGED"/> - <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> + <action android:name="android.net.wifi.STATE_CHANGE"/> </intent-filter> </receiver> diff --git a/res/drawable-hdpi/icon.png b/res/drawable-hdpi/icon.png index d705790..d705790 100755..100644 --- a/res/drawable-hdpi/icon.png +++ b/res/drawable-hdpi/icon.png diff --git a/res/drawable-ldpi/icon.png b/res/drawable-ldpi/icon.png index 211bfca..211bfca 100755..100644 --- a/res/drawable-ldpi/icon.png +++ b/res/drawable-ldpi/icon.png diff --git a/res/drawable-mdpi/icon.png b/res/drawable-mdpi/icon.png index 3347d1e..3347d1e 100755..100644 --- a/res/drawable-mdpi/icon.png +++ b/res/drawable-mdpi/icon.png diff --git a/res/layout/main.xml b/res/layout/main.xml index 7e4a852..7e4a852 100755..100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml diff --git a/res/layout/pppoe_configure.xml b/res/layout/pppoe_configure.xml index 7126039..7126039 100755..100644 --- a/res/layout/pppoe_configure.xml +++ b/res/layout/pppoe_configure.xml diff --git a/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml index 71a3cba..71a3cba 100755..100644 --- a/res/values-zh-rCN/strings.xml +++ b/res/values-zh-rCN/strings.xml diff --git a/res/values-zh-rTW/strings.xml b/res/values-zh-rTW/strings.xml index 33bc789..33bc789 100755..100644 --- a/res/values-zh-rTW/strings.xml +++ b/res/values-zh-rTW/strings.xml diff --git a/res/values/strings.xml b/res/values/strings.xml index c4f9c10..c4f9c10 100755..100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml diff --git a/src/com/amlogic/PPPoE/PppoeBroadcastReceiver.java b/src/com/amlogic/PPPoE/PppoeBroadcastReceiver.java deleted file mode 100755 index 5800547..0000000 --- a/src/com/amlogic/PPPoE/PppoeBroadcastReceiver.java +++ b/dev/null @@ -1,167 +0,0 @@ -package com.amlogic.PPPoE;
-
-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 android.net.wifi.WifiManager;
-import android.net.ethernet.EthernetStateTracker;
-import android.net.ethernet.EthernetManager;
-import android.content.SharedPreferences;
-import com.amlogic.pppoe.PppoeOperation;
-import android.os.Handler; -import android.os.IBinder; -import android.os.Message; -import android.os.SystemProperties; -
-public class PppoeBroadcastReceiver extends BroadcastReceiver {
- private static final String TAG = "PppoeBroadcastReceiver";
- private static final String ACTION_BOOT_COMPLETED =
- "android.intent.action.BOOT_COMPLETED";
-
- private Handler mHandler;
- private boolean mAutoDialFlag = false;
- private String mInterfaceSelected = null;
- private String mUserName = null;
- private String mPassword = null;
- private PppoeOperation operation = null;
-
- private String getNetworkInterfaceSelected(Context context)
- {
- SharedPreferences sharedata = context.getSharedPreferences("inputdata", 0);
- if(sharedata != null && sharedata.getAll().size() > 0)
- {
- return sharedata.getString(PppoeConfigDialog.INFO_NETWORK_INTERFACE_SELECTED, null);
- }
- return null;
- }
-
-
- 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 String getUserName(Context context)
- {
- SharedPreferences sharedata = context.getSharedPreferences("inputdata", 0);
- if(sharedata != null && sharedata.getAll().size() > 0)
- {
- return sharedata.getString(PppoeConfigDialog.INFO_USERNAME, null);
- }
- return null;
- }
-
- private String getPassword(Context context)
- {
- SharedPreferences sharedata = context.getSharedPreferences("inputdata", 0);
- if(sharedata != null && sharedata.getAll().size() > 0)
- {
- return sharedata.getString(PppoeConfigDialog.INFO_PASSWORD, null);
- }
- return null;
- }
-
-
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- mInterfaceSelected = getNetworkInterfaceSelected(context);
- mAutoDialFlag = getAutoDialFlag(context);
-
- mUserName = getUserName(context);
- mPassword = getPassword(context);
-
- if (ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
- context.startService(new Intent(context,
- MyPppoeService.class));
- }
-
- if (null == mInterfaceSelected
- || !mAutoDialFlag
- || null == mUserName
- || null == mPassword)
- return;
-
-
- if (EthernetManager.ETH_STATE_CHANGED_ACTION.equals(action)) {
- if (!mInterfaceSelected.startsWith("eth"))
- return;
-
- Log.d(TAG , ">>>>>onReceive :" +intent.getAction());
- int event = intent.getIntExtra(EthernetManager.EXTRA_ETH_STATE, -1);
- if (event == EthernetStateTracker.EVENT_HW_DISCONNECTED ) {
- Log.d(TAG, "EVENT_HW_DISCONNECTED");
- }
- else if (event == EthernetStateTracker.EVENT_HW_PHYCONNECTED ) {
- Log.d(TAG, "EVENT_HW_PHYCONNECTED");
- Log.d(TAG , "InterfaceSelected = " + mInterfaceSelected);
- operation = new PppoeOperation();
- set_pppoe_running_flag();
- operation.terminate();
-
- mHandler = new PppoeHandler();
- mHandler.sendEmptyMessageDelayed(0, 5000);
- }
- else if (event == EthernetStateTracker.EVENT_HW_CONNECTED )
- Log.d(TAG, "EVENT_HW_CONNECTED");
- else
- Log.d(TAG, "EVENT=" + event);
- }
-
- if ((ConnectivityManager.CONNECTIVITY_ACTION).equals(action)) {
- NetworkInfo netInfo = (NetworkInfo) intent.getExtra(WifiManager.EXTRA_NETWORK_INFO, null);
- if((netInfo != null) && (netInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED))
- {
- if (!mInterfaceSelected.startsWith("wlan"))
- return;
-
- operation = new PppoeOperation();
- set_pppoe_running_flag();
- operation.disconnect();
-
- mHandler = new PppoeHandler();
- mHandler.sendEmptyMessageDelayed(0, 5000);
- }
- }
- }
-
- void set_pppoe_running_flag()
- {
- SystemProperties.set(PppoeConfigDialog.ethernet_dhcp_repeat_flag, "disabled");
-
- SystemProperties.set(PppoeConfigDialog.pppoe_running_flag, "100");
- String propVal = SystemProperties.get(PppoeConfigDialog.pppoe_running_flag);
- int n = 0;
- if (propVal.length() != 0) {
- try {
- n = Integer.parseInt(propVal);
- Log.d(TAG, "set_pppoe_running_flag as " + n);
- } catch (NumberFormatException e) {}
- } else {
- Log.d(TAG, "failed to set_pppoe_running_flag");
- }
-
- return;
- }
-
- private class PppoeHandler extends Handler
- { - @Override - public void handleMessage(Message msg) { - super.handleMessage(msg); - - Log.d(TAG, "handleMessage");
-
- operation.connect(mInterfaceSelected, mUserName, mPassword);
- } - } -}
-
diff --git a/src/com/amlogic/PPPoE/MyPppoeService.java b/src/com/droidlogic/PPPoE/MyPppoeService.java index fd4d56b..db9c774 100755..100644 --- a/src/com/amlogic/PPPoE/MyPppoeService.java +++ b/src/com/droidlogic/PPPoE/MyPppoeService.java @@ -1,4 +1,4 @@ -package com.amlogic.PPPoE; +package com.droidlogic.PPPoE; import android.app.Notification; import android.app.NotificationManager; @@ -23,8 +23,8 @@ public class MyPppoeService extends Service private NotificationManager mNM; private Handler mHandler; private PppoeOperation operation = null; - - + + @Override public void onCreate() { Log.d(TAG, ">>>>>>onCreate"); @@ -38,8 +38,8 @@ public class MyPppoeService extends Service IntentFilter f = new IntentFilter(); - f.addAction(Intent.ACTION_SHUTDOWN); - f.addAction(Intent.ACTION_SCREEN_OFF); + f.addAction(Intent.ACTION_SHUTDOWN); + f.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(mShutdownReceiver, new IntentFilter(f)); } @@ -70,12 +70,12 @@ public class MyPppoeService extends Service } -private BroadcastReceiver mShutdownReceiver = +private BroadcastReceiver mShutdownReceiver = new BroadcastReceiver() { @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(); diff --git a/src/com/amlogic/PPPoE/PPPoEActivity.java b/src/com/droidlogic/PPPoE/PPPoEActivity.java index 5e0d722..1a40929 100755..100644 --- a/src/com/amlogic/PPPoE/PPPoEActivity.java +++ b/src/com/droidlogic/PPPoE/PPPoEActivity.java @@ -1,38 +1,52 @@ -package com.amlogic.PPPoE; +package com.droidlogic.PPPoE; -import android.net.pppoe.PppoeManager; -import android.net.pppoe.PppoeDevInfo; +import com.droidlogic.pppoe.PppoeManager; +import com.droidlogic.pppoe.IPppoeManager; +import com.droidlogic.pppoe.PppoeStateTracker; +import com.droidlogic.pppoe.PppoeDevInfo; import android.app.Activity; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.content.Context; import android.os.Bundle; +import android.os.IBinder; import android.util.Log; +import android.widget.Toast; +import android.view.Gravity; +import android.provider.Settings; +import android.os.ServiceManager; + public class PPPoEActivity extends Activity { private final String TAG = "PPPoEActivity"; private PppoeConfigDialog mPppoeConfigDialog; private PppoeDevInfo mPppoeInfo; private PppoeManager mPppoeManager; - + public static final int MSG_START_DIAL = 0xabcd0000; + public static final int MSG_MANDATORY_DIAL = 0xabcd0010; + public static final int MSG_CONNECT_TIMEOUT = 0xabcd0020; + public static final int MSG_DISCONNECT_TIMEOUT = 0xabcd0040; + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); - + Log.d(TAG, "Create PppoeConfigDialog"); mPppoeConfigDialog = new PppoeConfigDialog(this); ConnectivityManager cm = (ConnectivityManager)this.getSystemService - ( Context.CONNECTIVITY_SERVICE); - NetworkInfo info = cm.getActiveNetworkInfo(); + ( Context.CONNECTIVITY_SERVICE); + NetworkInfo info = cm.getActiveNetworkInfo(); if (info != null) { Log.d(TAG, info.toString()); } - mPppoeManager = (PppoeManager) this.getSystemService(Context.PPPOE_SERVICE); + IBinder b = ServiceManager.getService("pppoe"); + IPppoeManager PppoeService = IPppoeManager.Stub.asInterface(b); + mPppoeManager = new PppoeManager(PppoeService, this); mPppoeInfo = mPppoeManager.getSavedPppoeConfig(); if (mPppoeInfo != null) { diff --git a/src/com/droidlogic/PPPoE/PppoeBroadcastReceiver.java b/src/com/droidlogic/PPPoE/PppoeBroadcastReceiver.java new file mode 100644 index 0000000..de75501 --- a/dev/null +++ b/src/com/droidlogic/PPPoE/PppoeBroadcastReceiver.java @@ -0,0 +1,223 @@ +package com.droidlogic.PPPoE; + +import java.util.Timer; +import java.util.TimerTask; +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 android.net.wifi.WifiManager; +import android.content.SharedPreferences; +import com.amlogic.pppoe.PppoeOperation; +import android.os.Handler; +import android.os.IBinder; +import android.os.Message; +import android.os.SystemProperties; + +public class PppoeBroadcastReceiver extends BroadcastReceiver { + private static final String TAG = "PppoeBroadcastReceiver"; + private static final String ACTION_BOOT_COMPLETED = + "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; + private Handler mHandler = null; + private boolean mAutoDialFlag = false; + private String mInterfaceSelected = null; + private String mUserName = null; + private String mPassword = null; + private PppoeOperation operation = null; + private static boolean mFirstAutoDialDone = false; + private Timer mMandatoryDialTimer = null; + + private String getNetworkInterfaceSelected(Context context) + { + SharedPreferences sharedata = context.getSharedPreferences("inputdata", 0); + if (sharedata != null && sharedata.getAll().size() > 0) + { + return sharedata.getString(PppoeConfigDialog.INFO_NETWORK_INTERFACE_SELECTED, null); + } + return null; + } + + + 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 String getUserName(Context context) + { + SharedPreferences sharedata = context.getSharedPreferences("inputdata", 0); + if (sharedata != null && sharedata.getAll().size() > 0) + { + return sharedata.getString(PppoeConfigDialog.INFO_USERNAME, null); + } + return null; + } + + private String getPassword(Context context) + { + SharedPreferences sharedata = context.getSharedPreferences("inputdata", 0); + if (sharedata != null && sharedata.getAll().size() > 0) + { + return sharedata.getString(PppoeConfigDialog.INFO_PASSWORD, null); + } + return null; + } + + + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + mInterfaceSelected = getNetworkInterfaceSelected(context); + mAutoDialFlag = getAutoDialFlag(context); + + mUserName = getUserName(context); + mPassword = getPassword(context); + if (ACTION_BOOT_COMPLETED.equals(intent.getAction())) { + context.startService(new Intent(context, + MyPppoeService.class)); + } + + if (null == mInterfaceSelected + || !mAutoDialFlag + || null == mUserName + || null == mPassword) + return; + + if (mHandler == null) { + mHandler = new PppoeHandler(); + } + if (operation == null) { + operation = new PppoeOperation(); + } + + Log.d(TAG , ">>>>>onReceive :" +intent.getAction()); + if (ETH_STATE_CHANGED_ACTION.equals(action)) { + 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; + } + mFirstAutoDialDone = true; + + 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&& !mFirstAutoDialDone) { + Log.d(TAG, "EVENT_HW_PHYCONNECTED LOST"); + mFirstAutoDialDone = true; + 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); + } + } + } + else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { + NetworkInfo info = (NetworkInfo) intent.getParcelableExtra( + WifiManager.EXTRA_NETWORK_INFO); + if (!mInterfaceSelected.startsWith("wlan")) + return; + + if (info.isConnected()) { + Log.d(TAG, "AUTO DIAL OVER WIFI"); + set_pppoe_running_flag(); + operation.terminate(); + operation.disconnect(); + + mHandler = new PppoeHandler(); + mHandler.sendEmptyMessageDelayed(PPPoEActivity.MSG_START_DIAL, 5000); + } + } + } + + void set_pppoe_running_flag() + { + SystemProperties.set(PppoeConfigDialog.ethernet_dhcp_repeat_flag, "disabled"); + + SystemProperties.set(PppoeConfigDialog.pppoe_running_flag, "100"); + String propVal = SystemProperties.get(PppoeConfigDialog.pppoe_running_flag); + int n = 0; + if (propVal.length() != 0) { + try { + n = Integer.parseInt(propVal); + Log.d(TAG, "set_pppoe_running_flag as " + n); + } catch (NumberFormatException e) {} + } else { + Log.d(TAG, "failed to set_pppoe_running_flag"); + } + + return; + } + + private class PppoeHandler extends Handler + { + @Override + public void handleMessage(Message msg) { + 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); + break; + + case PPPoEActivity.MSG_START_DIAL: + Log.d(TAG, "handleMessage: MSG_START_DIAL"); + operation.connect(mInterfaceSelected, mUserName, mPassword); + break; + + default: + Log.d(TAG, "handleMessage: " + msg.what); + break; + } + + + } + } +} + diff --git a/src/com/amlogic/PPPoE/PppoeConfigDialog.java b/src/com/droidlogic/PPPoE/PppoeConfigDialog.java index 18e5360..5e6b04a 100755..100644 --- a/src/com/amlogic/PPPoE/PppoeConfigDialog.java +++ b/src/com/droidlogic/PPPoE/PppoeConfigDialog.java @@ -1,638 +1,728 @@ -package com.amlogic.PPPoE;
-
-import java.util.Timer;
-import java.util.TimerTask;
-import android.os.Message;
-import android.os.Handler;
+package com.droidlogic.PPPoE; + +import java.util.Timer; +import java.util.TimerTask; +import android.os.Message; +import android.os.Handler; import android.os.SystemProperties; -import android.app.AlertDialog;
-import android.app.ProgressDialog;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.content.SharedPreferences;
-import android.util.Log;
-import android.view.KeyEvent;
-import android.view.View;
-import android.widget.Button;
-import android.widget.CheckBox;
-import android.widget.CompoundButton;
-import android.widget.EditText;
-
-import android.widget.AdapterView;
-import android.widget.AdapterView.OnItemSelectedListener;
-import android.widget.ArrayAdapter;
-import android.widget.Spinner;
-import android.widget.TextView;
-
-import android.net.ethernet.EthernetManager; -import java.net.NetworkInterface;
+import android.app.AlertDialog; +import android.app.ProgressDialog; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.SharedPreferences; +import android.util.Log; +import android.text.TextUtils; +import android.text.TextWatcher; +import android.text.Editable; +import android.view.KeyEvent; +import android.view.View; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.EditText; + +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemSelectedListener; +import android.widget.ArrayAdapter; +import android.widget.Spinner; +import android.widget.TextView; + +import java.net.NetworkInterface; import java.util.Enumeration; -import java.util.ArrayList;
+import java.util.ArrayList; import java.net.SocketException; -
-import android.net.pppoe.PppoeManager;
-import android.net.pppoe.PppoeStateTracker;
-
-import com.amlogic.pppoe.PppoeOperation;
-
-public class PppoeConfigDialog extends AlertDialog implements DialogInterface.OnClickListener
-{
- private static final String PPPOE_DIAL_RESULT_ACTION =
- "PppoeConfigDialog.PPPOE_DIAL_RESULT";
-
- private static final int PPPOE_STATE_UNDEFINED = 0;
- private static final int PPPOE_STATE_DISCONNECTED = 1;
- private static final int PPPOE_STATE_CONNECTING = 2;
- private static final int PPPOE_STATE_DISCONNECTING = 3;
- private static final int PPPOE_STATE_CONNECT_FAILED = 4;
- private static final int PPPOE_STATE_CONNECTED = 5;
-
- private static final int MSG_CONNECT_TIMEOUT = 0xabcd0000;
- private static final int MSG_DISCONNECT_TIMEOUT = 0xabcd0010;
-
- private static final String EXTRA_NAME_STATUS = "status";
- private static final String EXTRA_NAME_ERR_CODE = "err_code";
-
- public static final String INFO_USERNAME = "name";
- public static final String INFO_PASSWORD = "passwd";
- 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 View mView;
- private EditText mPppoeName;
- private EditText mPppoePasswd;
- private String user_name = null;
- private String user_passwd = null;
- private ProgressDialog waitDialog = null;
- private PppoeOperation operation = null;
- Context context = null;
- private AlertDialog alertDia = null;
- private PppoeReceiver pppoeReceiver = null;
-
- private CheckBox mCbAutoDial;
-
- Timer connect_timer = null;
- Timer disconnect_timer = null;
-
- private int pppoe_state = PPPOE_STATE_UNDEFINED;
- private boolean isDialogOfDisconnect = false;
- public static final String pppoe_running_flag = "net.pppoe.running";
- public static final String ethernet_dhcp_repeat_flag = "net.dhcp.repeat";
-
- private TextView mNetworkInterfaces;
- private Spinner spinner;
- private ArrayAdapter<String> adapter;
- private EthernetManager mEthManager;
- private ArrayList<String> network_if_list;
- private String mNetIfSelected;
- private String tmp_name, tmp_passwd;
- private boolean mAtuoDialFlag = false;
-
- private boolean is_pppoe_running()
- {
- String propVal = SystemProperties.get(pppoe_running_flag);
- int n = 0;
- if (propVal.length() != 0) {
- try {
- n = Integer.parseInt(propVal);
- } catch (NumberFormatException e) {}
- } else {
- Log.d(TAG, "net.pppoe.running not FOUND");
- }
-
- return (n != 0);
- }
-
-
- private void set_pppoe_running_flag()
- {
- SystemProperties.set(ethernet_dhcp_repeat_flag, "disabled");
-
- SystemProperties.set(pppoe_running_flag, "100");
- String propVal = SystemProperties.get(pppoe_running_flag);
- int n = 0;
- if (propVal.length() != 0) {
- try {
- n = Integer.parseInt(propVal);
- Log.d(TAG, "set_pppoe_running_flag as " + n);
- } catch (NumberFormatException e) {}
- } else {
- Log.d(TAG, "failed to set_pppoe_running_flag");
- }
-
- return;
- }
-
-
- private void clear_pppoe_running_flag()
- {
- SystemProperties.set(ethernet_dhcp_repeat_flag, "enabled");
- SystemProperties.set(pppoe_running_flag, "0");
- String propVal = SystemProperties.get(pppoe_running_flag);
- int n = 0;
- if (propVal.length() != 0) {
- try {
- n = Integer.parseInt(propVal);
- Log.d(TAG, "clear_pppoe_running_flag as " + n);
- } catch (NumberFormatException e) {}
- } else {
- Log.d(TAG, "failed to clear_pppoe_running_flag");
- }
-
- return;
- }
-
-
- public PppoeConfigDialog(Context context)
- {
- super(context);
- this.context = context;
- operation = new PppoeOperation();
- buildDialog(context);
- waitDialog = new ProgressDialog(this.context);
-
- pppoeReceiver = new PppoeReceiver();
- IntentFilter filter = new IntentFilter();
- filter.addAction(PppoeManager.PPPOE_STATE_CHANGED_ACTION);
- Log.d(TAG, "registerReceiver pppoeReceiver");
- context.registerReceiver(pppoeReceiver, filter);
- }
-
- class SpinnerSelectedListener implements OnItemSelectedListener{
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- mNetIfSelected = network_if_list.get(arg2);
- Log.d(TAG, "interface selected: " + mNetIfSelected);
- }
-
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- }
-
- private void buildDialog(Context context)
- {
- Log.d(TAG, "buildDialog");
- setTitle(R.string.pppoe_config_title);
- this.setView(mView = getLayoutInflater().inflate(R.layout.pppoe_configure, null));
- mPppoeName = (EditText)mView.findViewById(R.id.pppoe_name_edit);
- mPppoePasswd = (EditText)mView.findViewById(R.id.pppoe_passwd_edit);
- mCbAutoDial = (CheckBox)mView.findViewById(R.id.auto_dial_checkbox);
- mCbAutoDial.setVisibility(View.VISIBLE);
- mCbAutoDial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- if(isChecked){
- Log.d(TAG, "Selected");
- }else{
- Log.d(TAG, "NO Selected");
- }
- }
- });
-
- mPppoeName.setEnabled(true);
- mPppoePasswd.setEnabled(true);
- this.setInverseBackgroundForced(true);
-
- network_if_list=new ArrayList<String>();
-
- try {
- for(Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();)
- {
- NetworkInterface netIf = list.nextElement();
- //Log.d(TAG, "network_interface: " + netIf.getDisplayName());
- if (netIf.isUp() &&
- !netIf.isLoopback() &&
- !netIf.isPointToPoint() &&
- !netIf.getDisplayName().startsWith("sit"))
- network_if_list.add(netIf.getDisplayName());
- }
- } catch (SocketException e) {
- return;
- }
-
- if (network_if_list.size() == 0 )
- network_if_list.add("eth0");
-
- mNetIfSelected = network_if_list.get(0);
-
- /*
- boolean eth_found = false;
- for(int i = 0; i < network_if_list.size(); i++) {
- if (network_if_list.get(i).startsWith("eth"))
- eth_found = true;
- }
-
- if (!eth_found) {
- network_if_list.add("eth0");
- }
- */
-
- /*
- mEthManager = (EthernetManager) context.getSystemService(Context.ETH_SERVICE);
- String[] network_if_list = mEthManager.getDeviceNameList();
- */
-
- mNetworkInterfaces = (TextView) mView.findViewById(R.id.network_interface_list_text);
- spinner = (Spinner) mView.findViewById(R.id.network_inteface_list_spinner);
- adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item,network_if_list);
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spinner.setAdapter(adapter);
- spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
- spinner.setVisibility(View.VISIBLE);
-
- if(connectStatus() != PppoeOperation.PPP_STATUS_CONNECTED)
- {
- isDialogOfDisconnect = false;
- this.setButton(BUTTON_POSITIVE, context.getText(R.string.pppoe_dial), this);
- }
- else {
- Log.d(TAG, "connectStatus is CONNECTED");
- isDialogOfDisconnect = true;
-
- //hide AutoDial CheckBox
- mCbAutoDial.setVisibility(View.GONE);
-
- //hide network interfaces
- mNetworkInterfaces.setVisibility(View.GONE);
- spinner.setVisibility(View.GONE);
-
- //hide Username
- mView.findViewById(R.id.user_pppoe_text).setVisibility(View.GONE);
- mPppoeName.setVisibility(View.GONE);
-
- //hide Password
- mView.findViewById(R.id.passwd_pppoe_text).setVisibility(View.GONE);
- mPppoePasswd.setVisibility(View.GONE);
-
- this.setButton(BUTTON_POSITIVE, context.getText(R.string.pppoe_disconnect), this);
-
- /*
- if (!is_pppoe_running()) {
- showAlertDialog(context.getResources().getString(R.string.pppoe_ppp_interface_occupied));
- return;
- }
- */
- }
-
- this.setButton(BUTTON_NEGATIVE, context.getText(R.string.menu_cancel), this);
-
- getInfoData();
-
- if(user_name != null
- && user_passwd != null
- && user_name.equals("")== false)
- {
- mPppoeName.setText(user_name);
- mPppoePasswd.setText(user_passwd);
- }
- else
- {
- mPppoeName.setText("");
- mPppoePasswd.setText("");
- }
-
- mCbAutoDial.setChecked(mAtuoDialFlag);
- }
-
-
- @Override
- public void show()
- {
- Log.d(TAG, "show");
-
- super.show();
- }
-
- void showWaitDialog(int id)
- {
- waitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- waitDialog.setTitle("");
- waitDialog.setMessage(this.context.getResources().getString(id));
- waitDialog.setIcon(null);
-
- if (id == R.string.pppoe_dial_waiting_msg){
- waitDialog.setButton(android.content.DialogInterface.BUTTON_POSITIVE,this.context.getResources().getString(R.string.menu_cancel),cancelBtnClickListener);
- }
-
- waitDialog.setIndeterminate(false);
- waitDialog.setCancelable(true);
- waitDialog.show();
-
- Button button = waitDialog.getButton(android.content.DialogInterface.BUTTON_POSITIVE);
- button.setFocusable(true);
- button.setFocusableInTouchMode(true);
- button.requestFocus();
- button.requestFocusFromTouch();
- }
-
- private void saveInfoData()
- {
- SharedPreferences.Editor sharedata = this.context.getSharedPreferences("inputdata", 0).edit();
- sharedata.clear();
- sharedata.putString(INFO_USERNAME, mPppoeName.getText().toString());
- sharedata.putString(INFO_PASSWORD, mPppoePasswd.getText().toString());
- sharedata.putString(INFO_NETWORK_INTERFACE_SELECTED, mNetIfSelected);
- sharedata.putBoolean(INFO_AUTO_DIAL_FLAG, mCbAutoDial.isChecked());
- sharedata.commit();
- }
-
- private void getInfoData()
- {
- SharedPreferences sharedata = this.context.getSharedPreferences("inputdata", 0);
- if(sharedata != null && sharedata.getAll().size() > 0)
- {
- user_name = sharedata.getString(INFO_USERNAME, null);
- user_passwd = sharedata.getString(INFO_PASSWORD, null);
- mNetIfSelected = sharedata.getString(INFO_NETWORK_INTERFACE_SELECTED, null);
- mAtuoDialFlag = sharedata.getBoolean(INFO_AUTO_DIAL_FLAG, false);
- }
- else
- {
- user_name = null;
- user_passwd = null;
- }
- }
-
- private int connectStatus()
- {
- if (null == mNetIfSelected){
- Log.d(TAG, "mNetIfSelected is null");
- return PppoeOperation.PPP_STATUS_DISCONNECTED;
- }
-
- return operation.status(mNetIfSelected);
- }
-
-
- private void showAlertDialog(final String msg)
- {
- Log.d(TAG, "showAlertDialog");
- AlertDialog.Builder ab = new AlertDialog.Builder(context);
- alertDia = ab.create();
- alertDia.setTitle(" ");
- alertDia.setMessage(msg);
- alertDia.setIcon(null);
-
- alertDia.setButton(android.content.DialogInterface.BUTTON_POSITIVE,this.context.getResources().getString(R.string.amlogic_ok),AlertClickListener);
-
- alertDia.setCancelable(true);
- alertDia.setInverseBackgroundForced(true);
- alertDia.show();
-
- Button button = alertDia.getButton(android.content.DialogInterface.BUTTON_POSITIVE);
- button.setFocusable(true);
- button.setFocusableInTouchMode(true);
- button.requestFocus();
- button.requestFocusFromTouch();
- }
-
- OnClickListener AlertClickListener = new OnClickListener()
- {
- public void onClick(DialogInterface dialog, int which)
- {
- switch (which) {
- case android.content.DialogInterface.BUTTON_POSITIVE:
- {
- alertDia.cancel();
- Log.d(TAG, "User click OK button, exit APK");
- clearSelf();
- }
- break;
- case android.content.DialogInterface.BUTTON_NEGATIVE:
- break;
- default:
- break;
- }
-
- }
- };
-
- private void handleStartDial()
- {
- Log.d(TAG, "handleStartDial");
- tmp_name = mPppoeName.getText().toString();
- tmp_passwd = mPppoePasswd.getText().toString();
- if(tmp_name != null && tmp_passwd != null)
- {
- saveInfoData();
-
- final Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG_CONNECT_TIMEOUT:
- pppoe_state = PPPOE_STATE_CONNECT_FAILED;
- waitDialog.cancel();
- showAlertDialog(context.getResources().getString(R.string.pppoe_connect_failed));
- SystemProperties.set("net.pppoe.isConnected", "false");
- break;
- }
-
- Log.d(TAG, "handleStartDial.handler");
- super.handleMessage(msg);
- }
- };
-
- connect_timer = new Timer();
- TimerTask check_task = new TimerTask()
- {
- public void run()
- {
- Message message = new Message();
- Log.d(TAG, "Send MSG_CONNECT_TIMEOUT");
- message.what = MSG_CONNECT_TIMEOUT;
- handler.sendMessage(message);
- }
- };
-
- connect_timer.schedule(check_task, 60000 * 2);
-
- showWaitDialog(R.string.pppoe_dial_waiting_msg);
- pppoe_state = PPPOE_STATE_CONNECTING;
- set_pppoe_running_flag();
- operation.connect(mNetIfSelected, tmp_name, tmp_passwd);
- }
- }
-
- private void handleStopDial()
- {
- Log.d(TAG, "handleStopDial");
- pppoe_state = PPPOE_STATE_DISCONNECTING;
- boolean result = operation.disconnect();
-
- final Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG_DISCONNECT_TIMEOUT:
- waitDialog.cancel();
- showAlertDialog(context.getResources().getString(R.string.pppoe_disconnect_failed));
- pppoe_state = PPPOE_STATE_DISCONNECTED;
- clear_pppoe_running_flag();
- SystemProperties.set("net.pppoe.isConnected", "false");
- break;
- }
-
- Log.d(TAG, "handleStopDial.handler");
- super.handleMessage(msg);
- }
- };
-
- disconnect_timer = new Timer();
- TimerTask check_task = new TimerTask()
- {
- public void run()
- {
- Message message = new Message();
- message.what = MSG_DISCONNECT_TIMEOUT;
- Log.d(TAG, "Send MSG_DISCONNECT_TIMEOUT");
- handler.sendMessage(message);
- }
- };
-
- //Timeout after 50 seconds
- disconnect_timer.schedule(check_task, 50000);
-
- showWaitDialog(R.string.pppoe_hangup_waiting_msg);
- }
-
-
- private void handleCancelDial()
- {
- operation.disconnect();
- }
-
-
- OnClickListener cancelBtnClickListener = new OnClickListener()
- {
- public void onClick(DialogInterface dialog, int which)
- {
- Log.d(TAG, "Cancel button is clicked");
- if (disconnect_timer != null)
- disconnect_timer.cancel();
-
- handleCancelDial();
- waitDialog.cancel();
- clearSelf();
- }
- };
-
-
- //@Override
- public void onClick(DialogInterface dialog, int which)
- {
- switch (which) {
- case BUTTON_POSITIVE:
- if(isDialogOfDisconnect) {
- if (connectStatus() != PppoeOperation.PPP_STATUS_CONNECTED) {
- Log.d(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
- Log.d(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
- Log.d(TAG, "@@DANGER: SHOULD CONNECTED when in DialogOfDisconnect@@");
- Log.d(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
- Log.d(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
- }
- handleStopDial();
- }
- else
- handleStartDial();
- break;
- case BUTTON_NEGATIVE:
- Log.d(TAG, "User click Discard button, exit APK");
- clearSelf();
- break;
- default:
- break;
- }
- }
-
- public class PppoeReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- String action = intent.getAction();
- Log.d(TAG, "#####PppoeReceiver: " + action);
-
- if(action.equals(PppoeManager.PPPOE_STATE_CHANGED_ACTION)) {
- int event = intent.getIntExtra(PppoeManager.EXTRA_PPPOE_STATE,PppoeManager.PPPOE_STATE_UNKNOWN);
- Log.d(TAG, "#####event " + event);
- if(event == PppoeStateTracker.EVENT_CONNECTED)
- {
- if (pppoe_state == PPPOE_STATE_CONNECTING) {
- waitDialog.cancel();
- connect_timer.cancel();
- }
- pppoe_state = PPPOE_STATE_CONNECTED;
- showAlertDialog(context.getResources().getString(R.string.pppoe_connect_ok));
- SystemProperties.set("net.pppoe.isConnected", "true");
- }
-
- if(event == PppoeStateTracker.EVENT_DISCONNECTED)
- {
- if (pppoe_state == PPPOE_STATE_DISCONNECTING) {
- waitDialog.cancel();
- disconnect_timer.cancel();
- }
- clear_pppoe_running_flag();
- pppoe_state = PPPOE_STATE_DISCONNECTED;
- showAlertDialog(context.getResources().getString(R.string.pppoe_disconnect_ok));
- SystemProperties.set("net.pppoe.isConnected", "false");
- }
-
- if(event == PppoeStateTracker.EVENT_CONNECT_FAILED)
- {
- String ppp_err = intent.getStringExtra(PppoeManager.EXTRA_PPPOE_ERRCODE);
- Log.d(TAG, "#####errcode: " + ppp_err);
-
- if (pppoe_state == PPPOE_STATE_CONNECTING) {
- waitDialog.cancel();
- connect_timer.cancel();
- clear_pppoe_running_flag();
- }
-
- pppoe_state = PPPOE_STATE_CONNECT_FAILED;
- String reason = "";
- if (ppp_err.equals("691"))
- reason = context.getResources().getString(R.string.pppoe_connect_failed_auth);
- else if (ppp_err.equals("650"))
- reason = context.getResources().getString(R.string.pppoe_connect_failed_server_no_response);
-
- showAlertDialog(context.getResources().getString(R.string.pppoe_connect_failed) + "\n" + reason);
- SystemProperties.set("net.pppoe.isConnected", "false");
- }
- }
- }
- }
-
- private void clearSelf()
- {
- if(pppoeReceiver != null) {
- Log.d(TAG, "unregisterReceiver pppoeReceiver");
- context.unregisterReceiver(pppoeReceiver);
- }
- ((PPPoEActivity)context).finish();
-
- /*
- Log.d(TAG, "killProcess");
- android.os.Process.killProcess(android.os.Process.myPid());
- System.exit(0);
- */
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if(keyCode == KeyEvent.KEYCODE_BACK){
- Log.d(TAG, "User BACK operation, exit APK");
- clearSelf();
- return true;
- }
-
- Log.d(TAG, "keyCode " + keyCode + " is down, do nothing");
- return super.onKeyDown(keyCode, event);
- }
-
-}
+ +import com.droidlogic.pppoe.PppoeManager; +import com.droidlogic.pppoe.PppoeStateTracker; +import com.droidlogic.app.SystemControlManager; +import com.amlogic.pppoe.PppoeOperation; + +public class PppoeConfigDialog extends AlertDialog implements DialogInterface.OnClickListener, TextWatcher +{ + private static final String PPPOE_DIAL_RESULT_ACTION = + "PppoeConfigDialog.PPPOE_DIAL_RESULT"; + public static final int EVENT_HW_PHYCONNECTED = 5; + public static final int EVENT_HW_DISCONNECTED = 4; + private static final int PPPOE_STATE_UNDEFINED = 0; + private static final int PPPOE_STATE_DISCONNECTED = 1; + private static final int PPPOE_STATE_CONNECTING = 2; + private static final int PPPOE_STATE_DISCONNECTING = 3; + private static final int PPPOE_STATE_CONNECT_FAILED = 4; + private static final int PPPOE_STATE_CONNECTED = 5; + + public static final String ETH_STATE_CHANGED_ACTION = + "android.net.ethernet.ETH_STATE_CHANGED"; + public static final String EXTRA_ETH_STATE = "eth_state"; + private static final String EXTRA_NAME_STATUS = "status"; + private static final String EXTRA_NAME_ERR_CODE = "err_code"; + + public static final String INFO_USERNAME = "name"; + public static final String INFO_PASSWORD = "passwd"; + 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 View mView; + private EditText mPppoeName; + private EditText mPppoePasswd; + private String user_name = null; + private String user_passwd = null; + private ProgressDialog waitDialog = null; + private PppoeOperation operation = null; + Context context = null; + private AlertDialog alertDia = null; + private PppoeReceiver pppoeReceiver = null; + private CheckBox mCbAutoDial; + private SystemControlManager sw = null; + + Timer connect_timer = null; + Timer disconnect_timer = null; + + private int pppoe_state = PPPOE_STATE_UNDEFINED; + private boolean isDialogOfDisconnect = false; + private static final String eth_device_sysfs = "/sys/class/ethernet/linkspeed"; + public static final String pppoe_running_flag = "net.pppoe.running"; + public static final String ethernet_dhcp_repeat_flag = "net.dhcp.repeat"; + + private TextView mNetworkInterfaces; + private Spinner spinner; + private ArrayAdapter<String> adapter; + private ArrayList<String> network_if_list; + private String mNetIfSelected = null; + private String tmp_name, tmp_passwd; + private boolean mAtuoDialFlag = false; + private Button mDial = null; + + private final Handler mTextViewChangedHandler; + + private boolean is_pppoe_running() + { + String propVal = SystemProperties.get(pppoe_running_flag); + int n = 0; + if (propVal.length() != 0) { + try { + n = Integer.parseInt(propVal); + } catch (NumberFormatException e) {} + } else { + Log.d(TAG, "net.pppoe.running not FOUND"); + } + + return (n != 0); + } + + + private void set_pppoe_running_flag() + { + SystemProperties.set(ethernet_dhcp_repeat_flag, "disabled"); + + SystemProperties.set(pppoe_running_flag, "100"); + String propVal = SystemProperties.get(pppoe_running_flag); + int n = 0; + if (propVal.length() != 0) { + try { + n = Integer.parseInt(propVal); + Log.d(TAG, "set_pppoe_running_flag as " + n); + } catch (NumberFormatException e) {} + } else { + Log.d(TAG, "failed to set_pppoe_running_flag"); + } + + return; + } + + + private void clear_pppoe_running_flag() + { + SystemProperties.set(ethernet_dhcp_repeat_flag, "enabled"); + SystemProperties.set(pppoe_running_flag, "0"); + String propVal = SystemProperties.get(pppoe_running_flag); + int n = 0; + if (propVal.length() != 0) { + try { + n = Integer.parseInt(propVal); + Log.d(TAG, "clear_pppoe_running_flag as " + n); + } catch (NumberFormatException e) {} + } else { + Log.d(TAG, "failed to clear_pppoe_running_flag"); + } + + return; + } + + + public PppoeConfigDialog(Context context) + { + super(context); + this.context = context; + sw = new SystemControlManager(context); + + mTextViewChangedHandler = new Handler(); + operation = new PppoeOperation(); + buildDialog(context); + waitDialog = new ProgressDialog(this.context); + + pppoeReceiver = new PppoeReceiver(); + IntentFilter filter = new IntentFilter(); + filter.addAction(PppoeManager.PPPOE_STATE_CHANGED_ACTION); + filter.addAction(ETH_STATE_CHANGED_ACTION); + Log.d(TAG, "registerReceiver pppoeReceiver"); + context.registerReceiver(pppoeReceiver, filter); + } + + class SpinnerSelectedListener implements OnItemSelectedListener{ + public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, + long arg3) { + mNetIfSelected = network_if_list.get(arg2); + Log.d(TAG, "interface selected: " + mNetIfSelected); + } + + public void onNothingSelected(AdapterView<?> arg0) { + } + } + private boolean isEthDeviceAdded() { + String str = sw.readSysFs(eth_device_sysfs); + if (str == null) + return false ; + if (str.contains("unlink")) { + return false; + } else { + return true; + } + } + + private void buildDialog(Context context) + { + String eth_name = null; + String wifi_name = null; + + Log.d(TAG, "buildDialog"); + setTitle(R.string.pppoe_config_title); + this.setView(mView = getLayoutInflater().inflate(R.layout.pppoe_configure, null)); + mPppoeName = (EditText)mView.findViewById(R.id.pppoe_name_edit); + mPppoePasswd = (EditText)mView.findViewById(R.id.pppoe_passwd_edit); + mCbAutoDial = (CheckBox)mView.findViewById(R.id.auto_dial_checkbox); + mCbAutoDial.setVisibility(View.VISIBLE); + mCbAutoDial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, + boolean isChecked) { + saveAutoDialFlag(); + if (isChecked) { + Log.d(TAG, "AutoDial Selected"); + }else{ + Log.d(TAG, "AutoDial Unselected"); + } + } + }); + + mPppoeName.setEnabled(true); + mPppoeName.addTextChangedListener(this); + mPppoePasswd.setEnabled(true); + mPppoePasswd.addTextChangedListener(this); + this.setInverseBackgroundForced(true); + + network_if_list=new ArrayList<String>(); + + try { + for (Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();) + { + NetworkInterface netIf = list.nextElement(); + if (netIf.isUp() && + !netIf.isLoopback() && + !netIf.isPointToPoint() && + !netIf.getDisplayName().startsWith("sit") && + !netIf.getDisplayName().startsWith("ip6tnl") && + !netIf.getDisplayName().startsWith("p2p")) { + Log.d(TAG, "network_interface: " + netIf.getDisplayName()); + Log.d(TAG, "Wire Intert: " + isEthDeviceAdded()); + if (netIf.getDisplayName().startsWith("eth") && isEthDeviceAdded()) + eth_name = netIf.getDisplayName(); + else if (netIf.getDisplayName().startsWith("wlan")) + wifi_name = netIf.getDisplayName(); + } + } + } catch (SocketException e) { + return; + } + + getInfoData(); + Log.d(TAG, "History network interface " + mNetIfSelected); + + if (mNetIfSelected != null) { + if (eth_name != null && mNetIfSelected.equals(eth_name)) { + network_if_list.add(eth_name); + eth_name = null; + } + else if (wifi_name != null && mNetIfSelected.equals(wifi_name)) { + network_if_list.add(wifi_name); + wifi_name = null; + } + } + if (eth_name != null) network_if_list.add(eth_name); + if (wifi_name != null) network_if_list.add(wifi_name); + + + mNetworkInterfaces = (TextView) mView.findViewById(R.id.network_interface_list_text); + spinner = (Spinner) mView.findViewById(R.id.network_inteface_list_spinner); + adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item,network_if_list); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + spinner.setOnItemSelectedListener(new SpinnerSelectedListener()); + spinner.setVisibility(View.VISIBLE); + + if (connectStatus() != PppoeOperation.PPP_STATUS_CONNECTED) + { + isDialogOfDisconnect = false; + this.setButton(BUTTON_POSITIVE, context.getText(R.string.pppoe_dial), this); + } + else { + Log.d(TAG, "connectStatus is CONNECTED"); + isDialogOfDisconnect = true; + + //hide AutoDial CheckBox + mCbAutoDial.setVisibility(View.GONE); + + //hide network interfaces + mNetworkInterfaces.setVisibility(View.GONE); + spinner.setVisibility(View.GONE); + + //hide Username + mView.findViewById(R.id.user_pppoe_text).setVisibility(View.GONE); + mPppoeName.setVisibility(View.GONE); + + //hide Password + mView.findViewById(R.id.passwd_pppoe_text).setVisibility(View.GONE); + mPppoePasswd.setVisibility(View.GONE); + + this.setButton(BUTTON_POSITIVE, context.getText(R.string.pppoe_disconnect), this); + + /* + if (!is_pppoe_running()) { + showAlertDialog(context.getResources().getString(R.string.pppoe_ppp_interface_occupied)); + return; + } + */ + } + this.setButton(BUTTON_NEGATIVE, context.getText(R.string.menu_cancel), this); + + if (user_name != null + && user_passwd != null + && user_name.equals("")== false) + { + mPppoeName.setText(user_name); + mPppoePasswd.setText(user_passwd); + } + else + { + mPppoeName.setText(""); + mPppoePasswd.setText(""); + } + checkDialEnable(); + + mCbAutoDial.setChecked(mAtuoDialFlag); + } + + private void checkDialEnable() { + Button mDial = this.getButton(android.content.DialogInterface.BUTTON_POSITIVE); + if (mDial == null) + return; + + if (mPppoeName.getVisibility() != View.VISIBLE || mPppoePasswd.getVisibility() != View.VISIBLE) + return; + + if (!TextUtils.isEmpty(mPppoeName.getText().toString()) && !TextUtils.isEmpty(mPppoePasswd.getText().toString())) + mDial.setEnabled(true); + else + mDial.setEnabled(false); + } + + @Override + public void show() + { + Log.d(TAG, "show"); + super.show(); + if (connectStatus() != PppoeOperation.PPP_STATUS_CONNECTED) { + mDial = this.getButton(android.content.DialogInterface.BUTTON_POSITIVE); + if (mDial != null && 0 == network_if_list.size()) + mDial.setEnabled(false); + } + } + + void showWaitDialog(int id) + { + waitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); + waitDialog.setTitle(""); + waitDialog.setMessage(this.context.getResources().getString(id)); + waitDialog.setIcon(null); + + if (id == R.string.pppoe_dial_waiting_msg) { + waitDialog.setButton(android.content.DialogInterface.BUTTON_POSITIVE,this.context.getResources().getString(R.string.menu_cancel),cancelBtnClickListener); + } + + waitDialog.setIndeterminate(false); + waitDialog.setCancelable(true); + waitDialog.show(); + + Button button = waitDialog.getButton(android.content.DialogInterface.BUTTON_POSITIVE); + button.setFocusable(true); + button.setFocusableInTouchMode(true); + button.requestFocus(); + button.requestFocusFromTouch(); + } + + private void saveInfoData() + { + SharedPreferences.Editor sharedata = this.context.getSharedPreferences("inputdata", 0).edit(); + sharedata.clear(); + sharedata.putString(INFO_USERNAME, mPppoeName.getText().toString()); + sharedata.putString(INFO_PASSWORD, mPppoePasswd.getText().toString()); + sharedata.putString(INFO_NETWORK_INTERFACE_SELECTED, mNetIfSelected); + sharedata.putBoolean(INFO_AUTO_DIAL_FLAG, mCbAutoDial.isChecked()); + sharedata.commit(); + } + + private void getInfoData() + { + SharedPreferences sharedata = this.context.getSharedPreferences("inputdata", 0); + if (sharedata != null && sharedata.getAll().size() > 0) + { + user_name = sharedata.getString(INFO_USERNAME, null); + user_passwd = sharedata.getString(INFO_PASSWORD, null); + mNetIfSelected = sharedata.getString(INFO_NETWORK_INTERFACE_SELECTED, null); + mAtuoDialFlag = sharedata.getBoolean(INFO_AUTO_DIAL_FLAG, false); + } + else + { + user_name = null; + user_passwd = null; + } + } + + private void saveAutoDialFlag() + { + SharedPreferences.Editor sharedata = this.context.getSharedPreferences("inputdata", 0).edit(); + //sharedata.clear(); + sharedata.putBoolean(INFO_AUTO_DIAL_FLAG, mCbAutoDial.isChecked()); + sharedata.commit(); + } + + private int connectStatus() + { + if (null == mNetIfSelected) { + Log.d(TAG, "mNetIfSelected is null"); + return PppoeOperation.PPP_STATUS_DISCONNECTED; + } + + return operation.status(mNetIfSelected); + } + + + private void showAlertDialog(final String msg) + { + Log.d(TAG, "showAlertDialog"); + AlertDialog.Builder ab = new AlertDialog.Builder(context); + alertDia = ab.create(); + alertDia.setTitle(" "); + alertDia.setMessage(msg); + alertDia.setIcon(null); + + alertDia.setButton(android.content.DialogInterface.BUTTON_POSITIVE,this.context.getResources().getString(R.string.amlogic_ok),AlertClickListener); + + alertDia.setCancelable(true); + alertDia.setInverseBackgroundForced(true); + alertDia.show(); + + Button button = alertDia.getButton(android.content.DialogInterface.BUTTON_POSITIVE); + button.setFocusable(true); + button.setFocusableInTouchMode(true); + button.requestFocus(); + button.requestFocusFromTouch(); + } + + OnClickListener AlertClickListener = new OnClickListener() + { + public void onClick(DialogInterface dialog, int which) + { + switch (which) { + case android.content.DialogInterface.BUTTON_POSITIVE: + { + alertDia.cancel(); + Log.d(TAG, "User click OK button, exit APK"); + clearSelf(); + } + break; + case android.content.DialogInterface.BUTTON_NEGATIVE: + break; + default: + break; + } + + } + }; + + private void handleStartDial() + { + Log.d(TAG, "handleStartDial"); + tmp_name = mPppoeName.getText().toString(); + tmp_passwd = mPppoePasswd.getText().toString(); + if (!TextUtils.isEmpty(tmp_name) && !TextUtils.isEmpty(tmp_passwd)) + { + saveInfoData(); + + final Handler handler = new Handler() { + public void handleMessage(Message msg) { + switch (msg.what) { + case PPPoEActivity.MSG_CONNECT_TIMEOUT: + Log.d(TAG, "handleMessage: MSG_CONNECT_TIMEOUT"); + pppoe_state = PPPOE_STATE_CONNECT_FAILED; + waitDialog.cancel(); + showAlertDialog(context.getResources().getString(R.string.pppoe_connect_failed)); + SystemProperties.set("net.pppoe.isConnected", "false"); + break; + } + + Log.d(TAG, "handleStartDial.handler"); + super.handleMessage(msg); + } + }; + + connect_timer = new Timer(); + TimerTask check_task = new TimerTask() + { + public void run() + { + Message message = new Message(); + Log.d(TAG, "Send MSG_CONNECT_TIMEOUT"); + message.what = PPPoEActivity.MSG_CONNECT_TIMEOUT; + handler.sendMessage(message); + } + }; + + connect_timer.schedule(check_task, 60000 * 2); + + showWaitDialog(R.string.pppoe_dial_waiting_msg); + pppoe_state = PPPOE_STATE_CONNECTING; + set_pppoe_running_flag(); + operation.connect(mNetIfSelected, tmp_name, tmp_passwd); + } + } + + private void handleStopDial() + { + Log.d(TAG, "handleStopDial"); + pppoe_state = PPPOE_STATE_DISCONNECTING; + boolean result = operation.disconnect(); + + final Handler handler = new Handler() { + public void handleMessage(Message msg) { + switch (msg.what) { + case PPPoEActivity.MSG_DISCONNECT_TIMEOUT: + waitDialog.cancel(); + showAlertDialog(context.getResources().getString(R.string.pppoe_disconnect_failed)); + pppoe_state = PPPOE_STATE_DISCONNECTED; + clear_pppoe_running_flag(); + SystemProperties.set("net.pppoe.isConnected", "false"); + break; + } + + Log.d(TAG, "handleStopDial.handler"); + super.handleMessage(msg); + } + }; + + disconnect_timer = new Timer(); + TimerTask check_task = new TimerTask() + { + public void run() + { + Message message = new Message(); + message.what = PPPoEActivity.MSG_DISCONNECT_TIMEOUT; + Log.d(TAG, "Send MSG_DISCONNECT_TIMEOUT"); + handler.sendMessage(message); + } + }; + + //Timeout after 50 seconds + disconnect_timer.schedule(check_task, 50000); + + showWaitDialog(R.string.pppoe_hangup_waiting_msg); + } + + + private void handleCancelDial() + { + operation.disconnect(); + } + + + OnClickListener cancelBtnClickListener = new OnClickListener() + { + public void onClick(DialogInterface dialog, int which) + { + Log.d(TAG, "Cancel button is clicked"); + if (disconnect_timer != null) + disconnect_timer.cancel(); + + handleCancelDial(); + waitDialog.cancel(); + clearSelf(); + } + }; + + + //@Override + public void onClick(DialogInterface dialog, int which) + { + switch (which) { + case BUTTON_POSITIVE: + if (isDialogOfDisconnect) { + if (connectStatus() != PppoeOperation.PPP_STATUS_CONNECTED) { + Log.d(TAG, "DANGER: SHOULD CONNECTED when in DialogOfDisconnect"); + } + handleStopDial(); + } + else + handleStartDial(); + break; + case BUTTON_NEGATIVE: + Log.d(TAG, "User click Discard button, exit APK"); + clearSelf(); + break; + default: + break; + } + } + + public class PppoeReceiver extends BroadcastReceiver + { + @Override + public void onReceive(Context context, Intent intent) + { + String action = intent.getAction(); + Log.d(TAG, "PppoeReceiver: " + action); + + if (action.equals(PppoeManager.PPPOE_STATE_CHANGED_ACTION)) { + int event = intent.getIntExtra(PppoeManager.EXTRA_PPPOE_STATE,PppoeManager.PPPOE_STATE_UNKNOWN); + Log.d(TAG, "event " + event); + if (event == PppoeStateTracker.EVENT_CONNECTED) + { + if (pppoe_state == PPPOE_STATE_CONNECTING) { + waitDialog.cancel(); + connect_timer.cancel(); + } + pppoe_state = PPPOE_STATE_CONNECTED; + showAlertDialog(context.getResources().getString(R.string.pppoe_connect_ok)); + SystemProperties.set("net.pppoe.isConnected", "true"); + } + + if (event == PppoeStateTracker.EVENT_DISCONNECTED) + { + if (pppoe_state == PPPOE_STATE_DISCONNECTING) { + waitDialog.cancel(); + disconnect_timer.cancel(); + } + clear_pppoe_running_flag(); + pppoe_state = PPPOE_STATE_DISCONNECTED; + showAlertDialog(context.getResources().getString(R.string.pppoe_disconnect_ok)); + SystemProperties.set("net.pppoe.isConnected", "false"); + } + + if (event == PppoeStateTracker.EVENT_CONNECT_FAILED) + { + String ppp_err = intent.getStringExtra(PppoeManager.EXTRA_PPPOE_ERRCODE); + Log.d(TAG, "#####errcode: " + ppp_err); + + if (pppoe_state == PPPOE_STATE_CONNECTING) { + waitDialog.cancel(); + connect_timer.cancel(); + clear_pppoe_running_flag(); + } + + pppoe_state = PPPOE_STATE_CONNECT_FAILED; + String reason = ""; + if (ppp_err.equals("691")) + reason = context.getResources().getString(R.string.pppoe_connect_failed_auth); + else if (ppp_err.equals("650")) + reason = context.getResources().getString(R.string.pppoe_connect_failed_server_no_response); + + showAlertDialog(context.getResources().getString(R.string.pppoe_connect_failed) + "\n" + reason); + SystemProperties.set("net.pppoe.isConnected", "false"); + } + } + else if (action.equals(ETH_STATE_CHANGED_ACTION)) { + int event = intent.getIntExtra(EXTRA_ETH_STATE, -1); + if (event == EVENT_HW_PHYCONNECTED && + isEthDeviceAdded()) { + Log.d(TAG, "Ether.EVENT_HW_PHYCONNECTED"); + network_if_list.add("eth0"); + if (connectStatus() != PppoeOperation.PPP_STATUS_CONNECTED) { + if (mDial != null && network_if_list.size() > 0) + mDial.setEnabled(true); + } + adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item,network_if_list); + spinner.setAdapter(adapter); + } + else if (event == EVENT_HW_DISCONNECTED) { + Log.d(TAG, "Ether.EVENT_HW_DISCONNECTED"); + + network_if_list.remove("eth0"); + if (connectStatus() != PppoeOperation.PPP_STATUS_CONNECTED) { + if (mDial != null && 0 == network_if_list.size()) + mDial.setEnabled(false); + } + + adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item,network_if_list); + spinner.setAdapter(adapter); + } + } + } + } + + private void clearSelf() + { + if (pppoeReceiver != null) { + Log.d(TAG, "unregisterReceiver pppoeReceiver"); + context.unregisterReceiver(pppoeReceiver); + } + ((PPPoEActivity)context).finish(); + + /* + Log.d(TAG, "killProcess"); + android.os.Process.killProcess(android.os.Process.myPid()); + System.exit(0); + */ + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK) { + Log.d(TAG, "User BACK operation, exit APK"); + clearSelf(); + return true; + } + + Log.d(TAG, "keyCode " + keyCode + " is down, do nothing"); + return super.onKeyDown(keyCode, event); + } + + @Override + public void afterTextChanged(Editable s) { + mTextViewChangedHandler.post(new Runnable() { + public void run() { + checkDialEnable(); + } + }); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + +} |