Skip to content

Commit

Permalink
新增登出功能, 減少冗贅程式碼
Browse files Browse the repository at this point in the history
  • Loading branch information
hearsilent committed Jun 18, 2015
1 parent 7139c21 commit 7546ca6
Show file tree
Hide file tree
Showing 13 changed files with 583 additions and 423 deletions.
583 changes: 274 additions & 309 deletions KUASWifiAutoLogin/.idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class MainActivity extends Activity {
@InjectView(R.id.button_login)
Button mLoginButton;

@InjectView(R.id.button_logout)
Button mLogoutButton;

@InjectView(R.id.editText_user)
EditText mUsernameEditText;

Expand Down Expand Up @@ -70,7 +73,10 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
}
});

// init GA
initGA();
}

private void initGA() {
analytics = GoogleAnalytics.getInstance(this);
analytics.setLocalDispatchPeriod(30);

Expand All @@ -89,40 +95,81 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
}

@OnClick (R.id.button_login)
public void submit() {
public void login() {
tracker.send(new HitBuilders.EventBuilder()
.setCategory("UX")
.setAction("Click")
.setLabel("Save & Login")
.build());

disableViews();
saveAndLogin();
}

@OnClick(R.id.button_logout)
public void logout() {
tracker.send(new HitBuilders.EventBuilder()
.setCategory("UX")
.setAction("Click")
.setLabel("Logout")
.build());

disableViews();
LoginHelper.logout(this, new GeneralCallback() {

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

@Override
public void onFail(String reason) {
mDebugTextView.setTextColor(getResources().getColor(R.color.md_red_a700));
showMessage(reason, true);
}
});
}

private void disableViews()
{
mDebugTextView.setVisibility(View.GONE);
mLoginButton.setEnabled(false);
mTableLayout.setEnabled(false);
mLogoutButton.setEnabled(false);
mLoginButton.setBackgroundResource(R.drawable.button_bluegrey);
mLogoutButton.setBackgroundResource(R.drawable.button_bluegrey);
mTableLayout.setEnabled(false);
mProgressView.setVisibility(View.VISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mUsernameEditText.getWindowToken(), 0);
imm.hideSoftInputFromWindow(mPasswordEditText.getWindowToken(), 0);
mUsernameEditText.clearFocus();
mPasswordEditText.clearFocus();
saveAndLogin();
}

private void enableViews()
{
mLoginButton.setEnabled(true);
mLogoutButton.setEnabled(true);
mTableLayout.setEnabled(true);
mProgressView.setVisibility(View.GONE);
mLoginButton.setBackgroundResource(R.drawable.button_blue);
mLogoutButton.setBackgroundResource(R.drawable.button_red);
}

private void saveAndLogin() {
Memory.setString(this, Constant.MEMORY_KEY_USER, mUsernameEditText.getText().toString());
Memory.setString(this, Constant.MEMORY_KEY_PASSWORD,
mPasswordEditText.getText().toString());
Memory.setString(this, Constant.MEMORY_KEY_PASSWORD, mPasswordEditText.getText().toString());

String userData;
String password = mPasswordEditText.getText().toString();
if (mUsernameEditText.getText().toString().equals("") || mPasswordEditText.getText().toString().equals(""))
{
userData = tranUser("0937808285@guest");
userData = Utils.tranUser("0937808285@guest");
password = "1306";
}
else
userData = tranUser(mUsernameEditText.getText().toString());
userData = Utils.tranUser(mUsernameEditText.getText().toString());

String loginType = userData.split(",")[2];
String ssid = Utils.getCurrentSsid(this);
Expand All @@ -149,36 +196,12 @@ public void onFail(String reason) {
});
}

private String tranUser(String user)
{
if (user.contains("@kuas.edu.tw") || user.contains("@gm.kuas.edu.tw"))
if (user.contains("@kuas.edu.tw"))
return user + ",1,Student";
else
return user + ",@gm.kuas.edu.tw,Student";
else if (user.length() == 10 && !user.substring(0,2).equals("09"))
if (Integer.parseInt(user.substring(1,4)) <= 102)
return user + "@kuas.edu.tw" + ",1,Student";
else
return user + "@gm.kuas.edu.tw" + ",@gm.kuas.edu.tw,Student";
else if (user.contains("@") && !user.contains("@guest"))
return user + ",,Cyber";
else
if (user.contains("@guest"))
return user + ",,Guest";
else
return user + "@guest,,Guest";
}

private void showMessage(CharSequence message, boolean shake) {
mDebugTextView.setVisibility(View.VISIBLE);
mDebugTextView.setText(message);
if (shake)
YoYo.with(Techniques.Shake).duration(700).playOn(mDebugTextView);
mLoginButton.setEnabled(true);
mTableLayout.setEnabled(true);
mProgressView.setVisibility(View.GONE);
mLoginButton.setBackgroundResource(R.drawable.button_blue);
enableViews();

tracker.send(new HitBuilders.EventBuilder()
.setCategory("UX")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void onReceive(final Context context, Intent intent) {
String user = Memory.getString(context, Constant.MEMORY_KEY_USER, null);
String password = Memory.getString(context, Constant.MEMORY_KEY_PASSWORD, null);
if (user != null && password != null) {
String userData = tranUser(user);
String userData = Utils.tranUser(user);
if (ssid.equals(Constant.EXPECTED_SSIDS[2]))
LoginHelper.login(context, userData.split(",")[0],
password, "Dorm", null);
Expand All @@ -53,25 +53,4 @@ public void onReceive(final Context context, Intent intent) {
}
}
}

private String tranUser(String user)
{
if (user.contains("@kuas.edu.tw") || user.contains("@gm.kuas.edu.tw"))
if (user.contains("@kuas.edu.tw"))
return user + ",1,Student";
else
return user + ",@gm.kuas.edu.tw,Student";
else if (user.length() == 10 && !user.substring(0,2).equals("09"))
if (Integer.parseInt(user.substring(1,4)) <= 102)
return user + "@kuas.edu.tw" + ",1,Student";
else
return user + "@gm.kuas.edu.tw" + ",@gm.kuas.edu.tw,Student";
else if (user.contains("@") && !user.contains("@guest"))
return user + ",,Cyber";
else
if (user.contains("@guest"))
return user + ",,Guest";
else
return user + "@guest,,Guest";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ 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 = 8000;
public static final int TIMEOUT_LOGOUT = 8000;
public static final int TIMEOUT_LOGIN = 6000;
public static final String JIANGONG_WIFI_SERVER = "172.16.61.253";
public static final String YANCHAO_WIFI_SERVER = "172.16.109.253";

Expand Down
Loading

0 comments on commit 7546ca6

Please sign in to comment.