Skip to content

Commit

Permalink
優化通知欄顯示, 優化GA
Browse files Browse the repository at this point in the history
  • Loading branch information
hearsilent committed Jun 17, 2015
1 parent 3a03175 commit 7139c21
Show file tree
Hide file tree
Showing 10 changed files with 436 additions and 257 deletions.
512 changes: 322 additions & 190 deletions KUASWifiAutoLogin/.idea/workspace.xml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions KUASWifiAutoLogin/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "tw.edu.kuas.wifiautologin"
minSdkVersion 9
targetSdkVersion 22
versionCode 212
versionName "2.1.2"
versionCode 213
versionName "2.1.3"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tw.edu.kuas.wifiautologin;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
Expand Down Expand Up @@ -132,20 +131,20 @@ private void saveAndLogin() {
if (ssid.equals(Constant.EXPECTED_SSIDS[2]))
loginType = "Dorm";

LoginHelper.login(this, userData.split(",")[1], userData.split(",")[0],
LoginHelper.login(this, userData.split(",")[0],
password, loginType, new GeneralCallback() {

@Override
public void onSuccess(String message) {
mDebugTextView.setTextColor(Color.DKGRAY);
mDebugTextView.setTextColor(getResources().getColor(R.color.md_grey_900));
showMessage(message, false);
finish();
}

@Override
public void onFail(String reason) {
mDebugTextView.setTextColor(Color.RED);
showMessage(reason , true);
mDebugTextView.setTextColor(getResources().getColor(R.color.md_red_a700));
showMessage(reason, true);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public void onReceive(final Context context, Intent intent) {
if (user != null && password != null) {
String userData = tranUser(user);
if (ssid.equals(Constant.EXPECTED_SSIDS[2]))
LoginHelper.login(context, userData.split(",")[1], userData.split(",")[0],
LoginHelper.login(context, userData.split(",")[0],
password, "Dorm", null);
else
LoginHelper.login(context, userData.split(",")[1], userData.split(",")[0],
LoginHelper.login(context, userData.split(",")[0],
password, userData.split(",")[2], null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Constant {
public static final String TAG = "HearSilent";

public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0";
public static final int TIMEOUT = 5000;
public static final int TIMEOUT = 8000;
public static final String JIANGONG_WIFI_SERVER = "172.16.61.253";
public static final String YANCHAO_WIFI_SERVER = "172.16.109.253";

Expand All @@ -16,5 +16,6 @@ public class Constant {
// Memory (Shared Preferences) Keys
public static final String MEMORY_KEY_USER = "MEMORY_KEY_USER";
public static final String MEMORY_KEY_PASSWORD = "MEMORY_KEY_PASSWORD";
public static final String MEMORY_KEY_ERRORTIMES = "MEMORY_KEY_ERRORTIMES";

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;
import org.apache.http.HttpStatus;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import tw.edu.kuas.wifiautologin.MainActivity;
import tw.edu.kuas.wifiautologin.R;
import tw.edu.kuas.wifiautologin.callbacks.Constant;
import tw.edu.kuas.wifiautologin.callbacks.GeneralCallback;
import tw.edu.kuas.wifiautologin.callbacks.Memory;

public class LoginHelper {
private static AsyncHttpClient mClient = init();
Expand All @@ -51,7 +52,7 @@ private static AsyncHttpClient init() {
return client;
}

public static void login(final Context context, String idType, String user, String password,
public static void login(final Context context, String user, String password,
final String loginType, final GeneralCallback callback) {
// init GA
analytics = GoogleAnalytics.getInstance(context);
Expand All @@ -77,7 +78,7 @@ public static void login(final Context context, String idType, String user, Stri
return;
}

final Map<String, String> paramsMap = new LinkedHashMap<>();
final LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
paramsMap.put("username", user);
paramsMap.put("userpwd", password);
paramsMap.put("login", "");
Expand All @@ -86,7 +87,7 @@ public static void login(final Context context, String idType, String user, Stri
mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle(context.getString(R.string.app_name)).setContentText(
mBuilder.setContentTitle(context.getString(R.string.kuas_wifi_auto_login)).setContentText(
String.format(context.getString(R.string.login_to_ssid), currentSsid))
.setSmallIcon(R.drawable.ic_stat_login).setProgress(0, 0, true).setOngoing(false);

Expand All @@ -98,20 +99,25 @@ public static void login(final Context context, String idType, String user, Stri
public void onSuccess(int statusCode, Header[] headers, byte[] bytes) {
String resultString = context.getString(R.string.login_ready);
String _IP = getIPAddress(context);
if (statusCode == 200) {
if (statusCode == HttpStatus.SC_OK) {
Log.d(Constant.TAG, "Already Login.");

tracker.send(new HitBuilders.EventBuilder()
.setCategory("alreadyLogin")
.setAction("onSuccess")
.setLabel(_IP + "/" + loginType)
.build());

if (callback != null)
{
loginSuccess(context, loginType, callback,
(_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17")) ? Constant.JIANGONG_WIFI_SERVER : Constant.YANCHAO_WIFI_SERVER
, resultString, false);
Toast.makeText(context, resultString, Toast.LENGTH_SHORT).show();

tracker.send(new HitBuilders.EventBuilder()
.setCategory("onSuccess")
.setAction("alreadyLogin")
.setLabel(_IP + "/" + loginType)
.build());

callback.onSuccess(resultString);
}

loginSuccess(context, loginType, callback, "建工", resultString, false);
Toast.makeText(context, resultString, Toast.LENGTH_SHORT).show();
} else {
mNotificationManager.notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build());

Expand Down Expand Up @@ -158,7 +164,7 @@ else if (_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17"))
});
}

private static void loginWithHeader(final Context context, final Map<String, String> paramsMap, final String loginType,
private static void loginWithHeader(final Context context, final LinkedHashMap<String, String> paramsMap, final String loginType,
final GeneralCallback callback, final boolean retry, final String loginServer) {
final Handler refresh = new Handler(Looper.getMainLooper());
new Thread(new Runnable() {
Expand All @@ -175,34 +181,56 @@ public void run() {
.method(Connection.Method.POST)
.execute();

final int statusCode = response.statusCode();
final String _response = response.body();
final int _statusCode = response.statusCode();

if(statusCode == 200) {
if(_statusCode == HttpStatus.SC_OK) {
refresh.post(new Runnable() {
public void run() {
mClient.get(context, "http://www.example.com/", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(final int statusCode, Header[] headers, byte[] bytes) {
if (statusCode == 200)
if (statusCode == HttpStatus.SC_OK)
loginSuccess(context, loginType, callback, loginServer, "", true);
else
retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "");
retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "",
retry ? "" : _response);
}

@Override
public void onFailure(final int statusCode, Header[] headers, byte[] bytes, Throwable e) {
e.printStackTrace();

retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "");
retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "",
retry ? "" : _response);
}
});
}
});
}
else {
if (_statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
Log.d(Constant.TAG, Integer.toString(_statusCode));
refresh.post(new Runnable() {
public void run() {
retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "");
mClient.get(context, "http://www.example.com/", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(final int statusCode, Header[] headers, byte[] bytes) {
if (statusCode == HttpStatus.SC_OK)
loginSuccess(context, loginType, callback, loginServer, "", true);
else
retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "",
retry ? "" : _response);
}

@Override
public void onFailure(final int statusCode, Header[] headers, byte[] bytes, Throwable e) {
e.printStackTrace();

retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "",
retry ? "" : _response);
}
});
}
});
}
Expand All @@ -211,16 +239,16 @@ public void run() {
refresh.post(new Runnable() {
public void run() {
retryLogin(context, paramsMap, loginType, callback, retry, loginServer,
context.getString(R.string.login_timeout));
context.getString(R.string.login_timeout), "Time Out");
}
});
}
}
}).start();
}

private static void retryLogin(Context context, final Map<String, String> paramsMap, String loginType,
GeneralCallback callback, boolean retry, String loginServer, String resultString)
private static void retryLogin(Context context, final LinkedHashMap<String, String> paramsMap, String loginType,
GeneralCallback callback, boolean retry, String loginServer, String resultString, String response)
{
if (retry) {
tracker.send(new HitBuilders.EventBuilder()
Expand All @@ -238,28 +266,34 @@ private static void retryLogin(Context context, final Map<String, String> params

if (resultString.equals(""))
resultString = context.getString(R.string.failed_to_login);
mBuilder.setContentTitle(context.getString(R.string.app_name))

mBuilder.setContentTitle(context.getString(R.string.kuas_wifi_auto_login))
.setContentText(resultString).setSmallIcon(R.drawable.ic_stat_login)
.setContentIntent(getFailPendingIntent(context))
.setAutoCancel(true)
.setVibrate(new long[]{300, 200, 300, 200})
.setLights(Color.RED, 800, 800)
.setProgress(0, 0, false);

if (callback != null) {
if (callback != null)
callback.onFail(resultString);
}
// Show error details in the expanded notification

int errorTimes = Memory.getInt(context, Constant.MEMORY_KEY_ERRORTIMES, 0);

mBuilder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(resultString));

mNotificationManager
.notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build());
if (!(callback == null && errorTimes >= 3))
mNotificationManager
.notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build());

errorTimes++;
Memory.setInt(context, Constant.MEMORY_KEY_ERRORTIMES, errorTimes);

tracker.send(new HitBuilders.EventBuilder()
.setCategory("UX")
.setAction("onFailure")
.setLabel((loginServer.equals(Constant.JIANGONG_WIFI_SERVER) ? "建工" : "燕巢") + "/" + loginType)
.setCategory("onFailure")
.setAction((loginServer.equals(Constant.JIANGONG_WIFI_SERVER) ? "建工" : "燕巢") + "/" + loginType)
.setLabel(response)
.build());
}

Expand Down Expand Up @@ -293,29 +327,40 @@ private static void loginSuccess(Context context, String loginType, GeneralCallb
}

if (callback != null)
callback.onSuccess(resultString);
{
if (resultString.equals(context.getString(R.string.login_ready)))
callback.onSuccess(resultString);
else
callback.onSuccess(resultString.split(",")[0] + ",\n" + resultString.split(",")[1]);
}

mBuilder.setContentTitle(context.getString(R.string.app_name))
mBuilder.setContentTitle(context.getString(R.string.kuas_wifi_auto_login))
.setContentText(resultString).setSmallIcon(R.drawable.ic_stat_login)
.setContentIntent(getDefaultPendingIntent(context))
.setAutoCancel(true)
.setProgress(0, 0, false);

Memory.setInt(context, Constant.MEMORY_KEY_ERRORTIMES, 0);

if (notify)
{
mBuilder.setVibrate(new long[]{300, 200, 300, 200})
.setLights(Color.GREEN, 800, 800)
.setDefaults(Notification.DEFAULT_SOUND);

tracker.send(new HitBuilders.EventBuilder()
.setCategory("UX")
.setAction("onSuccess")
.setLabel((loginServer.equals(Constant.JIANGONG_WIFI_SERVER) ? "建工" : "燕巢") + "/" + loginType)
.setCategory("onSuccess")
.setAction((loginServer.equals(Constant.JIANGONG_WIFI_SERVER) ? "建工" : "燕巢"))
.setLabel(loginType)
.build());
}

mBuilder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(resultString));
if (resultString.equals(context.getString(R.string.login_ready)))
mBuilder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(resultString));
else
mBuilder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(resultString.split(",")[0] + ",\n" + resultString.split(",")[1]));

mNotificationManager
.notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ncuwl_login"
android:text="@string/kuas_wifi_auto_login"
android:textColor="@color/primary_material_dark"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"/>
Expand Down Expand Up @@ -98,16 +98,17 @@
app:pv_circular="false"
app:pv_progressStyle="@style/ColorsLinearProgressDrawableStyle"
app:pv_progressMode="indeterminate"
android:layout_marginTop="8sp"
android:layout_marginRight="20sp"
android:layout_marginLeft="20sp"
android:layout_marginRight="21sp"
android:layout_marginLeft="21sp"
android:visibility="gone"/>

<TextView
android:id="@+id/textView_debug"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"
android:gravity="center"
android:visibility="gone"/>

Expand Down
9 changes: 5 additions & 4 deletions KUASWifiAutoLogin/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ncuwl_login"
android:text="@string/kuas_wifi_auto_login"
android:textColor="@color/primary_material_dark"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"/>
Expand Down Expand Up @@ -88,16 +88,17 @@
app:pv_circular="false"
app:pv_progressStyle="@style/ColorsLinearProgressDrawableStyle"
app:pv_progressMode="indeterminate"
android:layout_marginTop="8sp"
android:layout_marginRight="40sp"
android:layout_marginLeft="40sp"
android:layout_marginRight="41sp"
android:layout_marginLeft="41sp"
android:visibility="gone"/>

<TextView
android:id="@+id/textView_debug"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginLeft="40sp"
android:layout_marginRight="40sp"
android:gravity="center"
android:visibility="gone"/>

Expand Down
Loading

0 comments on commit 7139c21

Please sign in to comment.