diff --git a/app/src/main/java/com/future/nkwlan/MainActivity.java b/app/src/main/java/com/future/nkwlan/MainActivity.java index 41ecdc0..e29da0e 100644 --- a/app/src/main/java/com/future/nkwlan/MainActivity.java +++ b/app/src/main/java/com/future/nkwlan/MainActivity.java @@ -1,5 +1,6 @@ package com.future.nkwlan; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.wifi.WifiManager; @@ -31,6 +32,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe private TextView messageTxt, uidTxt, ssidTxt, isNkuTxt, loginInfoTxt; private String ssid = "to be getting"; private String isNKU = "是"; + private Context context; @Override protected void onCreate(Bundle savedInstanceState) { @@ -39,7 +41,6 @@ protected void onCreate(Bundle savedInstanceState) { User user = NkCache.getAccount(this); - messageTxt = (TextView) findViewById(R.id.main_message_txt); uidTxt = (TextView) findViewById(R.id.main_uid_txt); loginInfoTxt = (TextView) findViewById(R.id.main_login_info_txt); @@ -50,8 +51,6 @@ protected void onCreate(Bundle savedInstanceState) { loginBtn = (Button) findViewById(R.id.main_login_btn); loginBtn.setOnClickListener(this); // wifiImg.setOnClickListener(this); - WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE); - ssid = wifiManager.getConnectionInfo().getSSID().replace("\"", ""); Log.e("nkwlan", "onCreate"); if (user.uid.length() == 0) { @@ -62,6 +61,9 @@ protected void onCreate(Bundle savedInstanceState) { public void onResume() { super.onResume(); + context = this; + WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE); + ssid = wifiManager.getConnectionInfo().getSSID().replace("\"", ""); new UpdateStatus().execute(); Log.e("nkwlan", "onResume"); } @@ -86,8 +88,13 @@ public void onClick(DialogInterface dialog, int which) { Toast.LENGTH_LONG).show(); } }); - if (!ssid.equals("NKU_WLAN")) + if (ssid.equals("NKU_WLAN")) { + new Login().execute(user.uid, user.psw); + Toast.makeText(getApplicationContext(), "正在登录...", + Toast.LENGTH_LONG).show(); + } else { builder.show(); + } break; case NetworkInfo.ONLINE: builder.setMessage(Html.fromHtml( @@ -145,11 +152,12 @@ public void onClick(DialogInterface dialog, int which) { builder.show(); } - private class UpdateStatus extends AsyncTask { @Override protected NetworkInfo doInBackground(String... params) { + if (!NkNetwork.isWifi(context)) + return new NetworkInfo(NetworkInfo.UNCONNECTED); return NkNetwork.getLogStatus(); } diff --git a/app/src/main/java/com/future/nkwlan/NkNetwork.java b/app/src/main/java/com/future/nkwlan/NkNetwork.java index 6754045..204925c 100644 --- a/app/src/main/java/com/future/nkwlan/NkNetwork.java +++ b/app/src/main/java/com/future/nkwlan/NkNetwork.java @@ -1,5 +1,7 @@ package com.future.nkwlan; +import android.content.Context; +import android.net.ConnectivityManager; import android.util.Log; import com.squareup.okhttp.Call; @@ -60,6 +62,7 @@ public static NetworkInfo getLogStatus() { public static NetworkInfo login(String uid, String psw) { NetworkInfo tmp = getLogStatus(); +// Log.e("wlan", String.format("status check before login:%s,%s,%s",tmp.status,uid,psw)); if (tmp.status != NetworkInfo.UN_LOGIN) return tmp; RequestBody requestBody = RequestBody.create( @@ -70,8 +73,9 @@ public static NetworkInfo login(String uid, String psw) { Request request = new Request.Builder().post(requestBody).url(hosts[0] + login_path).build(); try { String result = client.newCall(request).execute().body().string(); - Log.e("future.wlan", result); + Log.e("wlan", result); } catch (IOException e) { + Log.e("wlan", e.toString()); e.printStackTrace(); } return getLogStatus(); @@ -118,4 +122,13 @@ private static String find(String raw, String tag) { String raw2 = raw1.substring(0, raw1.indexOf("'")); return raw2.trim(); } + + public static boolean isWifi(Context context) { + ConnectivityManager connectivityManager = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + android.net.NetworkInfo wifiNetworkInfo = + connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + return wifiNetworkInfo.isConnected(); + + } } diff --git a/app/src/main/java/service/NetworkChangeReceiver.java b/app/src/main/java/service/NetworkChangeReceiver.java index e27e9c5..8aed850 100644 --- a/app/src/main/java/service/NetworkChangeReceiver.java +++ b/app/src/main/java/service/NetworkChangeReceiver.java @@ -5,27 +5,91 @@ import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.os.AsyncTask; import android.util.Log; +import com.future.nkwlan.NkCache; +import com.future.nkwlan.NkNetwork; +import com.future.nkwlan.User; + /** + * 接收网络状况变化的广播 * Created by future on 2016/3/28 0028. */ public class NetworkChangeReceiver extends BroadcastReceiver { + private String userId; + private String pwd; + @Override public void onReceive(Context context, Intent intent) { - Log.e("wlan","onReceive"); - ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); - NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); - if (activeNetInfo != null) { - Log.e("wlan", "NCR: Active Network Type : " + activeNetInfo.getTypeName()); -// Toast.makeText(context, "Active Network Type : " + -// activeNetInfo.getTypeName(), Toast.LENGTH_SHORT).show(); + Log.e("wlan", "onReceive"); + ConnectivityManager connectivityManager = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + + NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + if (wifiInfo.isConnected()) { + new UpdateStatus().execute(); + } + User user = NkCache.getAccount(context); + userId = user.uid; + pwd = user.psw; + } + + private class UpdateStatus extends AsyncTask { + + @Override + protected com.future.nkwlan.NetworkInfo doInBackground(String... params) { + + return NkNetwork.getLogStatus(); } - if (mobNetInfo != null) { - Log.e("wlan", "NCR: Mobile Network Type : " + mobNetInfo.getTypeName()); + + @Override + protected void onPostExecute(com.future.nkwlan.NetworkInfo info) { + switch (info.status) { + case com.future.nkwlan.NetworkInfo.UNCONNECTED: + + Log.e("wlan", "Receiver:UNCONNECTED, nothing to do"); + break; + case com.future.nkwlan.NetworkInfo.UN_LOGIN://登录&保存 + if (userId == null) + Log.e("wlan", "Receiver:UNLOGIN, and no user saved"); + else { + Log.e("wlan", "Receiver:UNLOGIN, perfoming login" + userId + pwd); + new Login().execute(userId, pwd); + } + break; + case com.future.nkwlan.NetworkInfo.ONLINE: + Log.e("wlan", "Receiver:online, nothing to do"); + break; + } + } + } + + private class Login extends AsyncTask { + + @Override + protected com.future.nkwlan.NetworkInfo doInBackground(String... params) { + + return NkNetwork.login(params[0], params[1]); + } + + @Override + protected void onPostExecute(com.future.nkwlan.NetworkInfo info) { + + switch (info.status) { + case com.future.nkwlan.NetworkInfo.ONLINE: + Log.e("wlan", "Receiver.login: login success. notify user this good news!"); + break; + case com.future.nkwlan.NetworkInfo.UN_LOGIN: + + Log.e("wlan", "Receiver.login: 登录失败,请确认账号密码正确,且余额充足"); + break; + case com.future.nkwlan.NetworkInfo.UNCONNECTED: + Log.e("wlan", "Receiver.login: 网络连接失败,弹一个通知让用户进APP内部操作"); + break; + } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1814315..c5d4d72 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,14 +1,14 @@ - NKU-Helper + 无线南开 - + 使用帮助:

输入你的NKU_WLAN用户名和密码,APP将会保存在本地,在当你连入NKU_WLAN时,APP将自动为你登录。 ]]>
- + 校园网\t:\t%s
@@ -16,42 +16,42 @@ ]]>
- + ]]> - + ]]> - + ]]> - + %s
是否更换账号? ]]>
- + %s 登录 %s ? ]]> - + %s 在WiFi %s 的登录? ]]> - + 账户余额 :%.2f 元