Skip to content

Commit

Permalink
refine logics to send login-lost message
Browse files Browse the repository at this point in the history
  • Loading branch information
zfdang committed Apr 29, 2022
1 parent 1a305cf commit 60ad0f1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
9 changes: 5 additions & 4 deletions app/src/main/java/com/zfdang/SMTHApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ public class SMTHApplication extends Application {
// IP database
public static GEODatabase geoDB;

public static boolean isValidUser() {
return activeUser != null && !activeUser.getId().equals("guest");
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
Expand All @@ -63,6 +59,11 @@ protected void attachBaseContext(Context base) {

// current logined user
public static UserStatus activeUser;
public static String displayedUserId;
public static boolean isValidUser() {
return activeUser != null && !activeUser.getId().equals("guest");
}


public void onCreate() {
super.onCreate();
Expand Down
39 changes: 19 additions & 20 deletions app/src/main/java/com/zfdang/zsmth_android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ public class MainActivity extends SMTHBaseActivity
mUsername.setOnClickListener(this);

// http://stackoverflow.com/questions/27097126/marquee-title-in-toolbar-actionbar-in-android-with-lollipop-sdk
TextView titleTextView = null;
TextView titleTextView;
try {
Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
titleTextView = (TextView) f.get(toolbar);
assert titleTextView != null;
titleTextView.setEllipsize(TextUtils.TruncateAt.START);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
Expand Down Expand Up @@ -315,7 +316,7 @@ private void showNotification(String text) {
Notification notification = mBuilder.build();
mNotifyMgr.notify(notificationID, notification);
} catch (Exception se) {
Log.e(TAG, "showNotification: " + se.toString());
Log.e(TAG, "showNotification: " + se);
}
}

Expand Down Expand Up @@ -348,6 +349,7 @@ private void showNotification(String text) {

@Override protected void onNewIntent(Intent intent) {
// this method will be triggered by showNotification(message);
super.onNewIntent(intent);
FragmentManager fm = getSupportFragmentManager();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Expand All @@ -357,7 +359,7 @@ private void showNotification(String text) {
// java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
String message = bundle.getString(SMTHApplication.SERVICE_NOTIFICATION_MESSAGE);
if (message != null) {
if(message.contains(SMTHApplication.NOTIFICATION_LOGIN_LOST)) {
if (message.contains(SMTHApplication.NOTIFICATION_LOGIN_LOST)) {
// login status lost, show login menu
onLogin();
} else {
Expand Down Expand Up @@ -427,17 +429,18 @@ public void UpdateNavigationViewHeader() {
getWindow().invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);

if (SMTHApplication.isValidUser()) {
// update user to login user
// update user to logined user
mUsername.setText(SMTHApplication.activeUser.getId());
String faceURL = SMTHApplication.activeUser.getFace_url();
if (faceURL != null) {
mAvatar.setImageFromStringURL(faceURL);
}
} else {
// only user to guest
// when user is invalid, set notice to login
mUsername.setText(getString(R.string.nav_header_click_to_login));
mAvatar.setImageResource(R.drawable.ic_person_black_48dp);
}
SMTHApplication.displayedUserId = mUsername.getText().toString();
}

@Override public void onBackPressed() {
Expand Down Expand Up @@ -601,31 +604,29 @@ public void onLogout() {

public boolean onNavigationItemID(int menuID) {
// Handle navigation view item clicks here.
int id = menuID;

Fragment fragment = null;
String title = "";

if (id == R.id.nav_guidance) {
if (menuID == R.id.nav_guidance) {
fragment = hotTopicFragment;
title = "首页导读";
} else if (id == R.id.nav_favorite) {
} else if (menuID == R.id.nav_favorite) {
fragment = favoriteBoardFragment;
title = "收藏夹";
} else if (id == R.id.nav_all_boards) {
} else if (menuID == R.id.nav_all_boards) {
fragment = allBoardFragment;
title = "所有版面";
} else if (id == R.id.nav_mail) {
} else if (menuID == R.id.nav_mail) {
fragment = mailListFragment;
title = "邮件";
} else if (id == R.id.nav_setting) {
} else if (menuID == R.id.nav_setting) {
// fragment = settingFragment;
fragment = preferenceFragment;
title = "设置";
} else if (id == R.id.nav_about) {
} else if (menuID == R.id.nav_about) {
fragment = aboutFragment;
title = "关于";
} else if(id == R.id.nav_night_mode) {
} else if(menuID == R.id.nav_night_mode) {
boolean bNightMode = Settings.getInstance().isNightMode();
Settings.getInstance().setNightMode(!bNightMode);
setApplicationNightMode();
Expand Down Expand Up @@ -655,12 +656,10 @@ public void setApplicationNightMode() {
}

Activity activity = this;
if (activity != null) {
Intent intent = new Intent(activity.getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
activity.finish();
}
Intent intent = new Intent(activity.getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
activity.finish();
}

public void testCodes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,19 @@ public void onNext(UserStatus userStatus) {
//Log.d(TAG, "4.0 onNext: " + userStatus.toString());
// cache user if necessary, so we don't have to query User avatar url again in the future
boolean updateUserIcon = false;
if (SMTHApplication.activeUser == null || !TextUtils.equals(SMTHApplication.activeUser.getId(), userStatus.getId())) {
if (!TextUtils.equals(userStatus.getId(), SMTHApplication.displayedUserId)) {
// active user is null, or active user is different with userstatus, update the icon
// Log.d(TAG, "onNext: " + "4.1 cache userStatus as activeUser");
updateUserIcon = true;
}
SMTHApplication.activeUser = userStatus;

String message = "";
if(!TextUtils.equals(SMTHApplication.activeUser.getId(), "guest")) {
// get message if user is not guest
if(SMTHApplication.isValidUser()) {
// get message if user is valid user
message = getNotificationMessage(SMTHApplication.activeUser);
} else if(updateUserIcon == true) {
// if it's guest but updateUserIcon == true, means login status has lost
// not a valid user, but updateUserIcon is true, means login status has lost
message = SMTHApplication.NOTIFICATION_LOGIN_LOST;
}

Expand All @@ -218,7 +218,8 @@ public void onNext(UserStatus userStatus) {
bundle.putString(SMTHApplication.SERVICE_NOTIFICATION_MESSAGE, message);
}
// Here we call send passing a resultCode and the bundle of extras
mUserStatusReceiver.send(Activity.RESULT_OK, bundle); }
mUserStatusReceiver.send(Activity.RESULT_OK, bundle);
}
}

@Override
Expand Down

0 comments on commit 60ad0f1

Please sign in to comment.