@Override protected void onSuccess(TokenInfo tokenInfo) { super.onSuccess(tokenInfo); // 同一登录账户 if (tokenInfo != null && AppContext.isLoggedIn() && AppContext.getAccount().getUid().equals(tokenInfo.getUid())) { if (BaseActivity.getRunningActivity() != null && BaseActivity.getRunningActivity() instanceof MainActivity) { if (getParams()[0].getAccessToken().isExpired()) requestLogin(BaseActivity.getRunningActivity(), getParams()[0]); } } }
private void setupMenu(Bundle savedInstanceState) { if (savedInstanceState == null) { int menuId = MenuFragment.MENU_MAIN; Intent intent = getIntent(); if (intent != null && !TextUtils.isEmpty(intent.getAction())) { menuId = getActionType(intent, intent.getAction()); } if (menuId == MenuFragment.MENU_MD) { menuFragment = MenuFragment.newInstance(MenuFragment.MENU_MD); getFragmentManager() .beginTransaction() .add(R.id.menu_frame, menuFragment, "MenuFragment") .commit(); } else if (menuId == MenuFragment.MENU_FRIENDSHIP) { menuFragment = MenuFragment.newInstance(MenuFragment.MENU_MAIN); getFragmentManager() .beginTransaction() .add(R.id.menu_frame, menuFragment, "MenuFragment") .commit(); FriendshipPagerFragment.launch(this, AppContext.getAccount().getUser(), 1); } else { menuFragment = MenuFragment.newInstance(menuId); getFragmentManager() .beginTransaction() .add(R.id.menu_frame, menuFragment, "MenuFragment") .commit(); } } else { menuFragment = (MenuFragment) getFragmentManager().findFragmentByTag("MenuFragment"); } }
@Override protected void onSuccess(TokenInfo tokenInfo) { super.onSuccess(tokenInfo); if (tokenInfo != null && account.getUser().getIdstr().equalsIgnoreCase(AppContext.getUser().getIdstr())) { long validSecond = Long.parseLong(tokenInfo.getCreate_at()) + Long.parseLong(tokenInfo.getExpire_in()); if (System.currentTimeMillis() > validSecond * 1000) { new AlertDialogWrapper.Builder(MainActivity.this) .setTitle(R.string.remind) .setMessage(R.string.account_expired) .setNegativeButton(R.string.no, null) .setPositiveButton( R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { LoginFragment.launch( MainActivity.this, account.getAccount(), account.getPassword(), 2000); } }) .show(); } } }
@Override public boolean onLongClick(View v) { String username = ""; String password = ""; username = AppContext.getAccount().getAccount(); password = AppContext.getAccount().getPassword(); // 这里导出Hprof文件,跟踪分析内存 // String path = SdcardUtils.getSdcardPath() + File.separator + "aisenweibo" + // File.separator; // path = path + "hprof" + File.separator; // HprofUtils.dumpHprof(path); WeicoLoginFragment.launch(this, username, password, 1000); return true; }
@Override protected void onSuccess(UnreadCount result) { super.onSuccess(result); // 通知消息 unreadCountNotifier.notinfyUnreadCount(AppContext.getUnreadCount()); sendUnreadBroadcast(); }
/** * 触发一次离线,如果没有设置过离线分组,优先设置后再离线 * * @param context */ public static void toggleOffline(final Activity context) { if (!AppContext.isLogedin()) return; List<Group> groups = SinaDB.getOfflineSqlite() .select(new Extra(AppContext.getUser().getIdstr(), null), Group.class); if (groups.size() == 0) { Logger.d(TAG, "离线分组未设置过"); new AlertDialogWrapper.Builder(context) .setMessage(R.string.offline_none_groups_remind) .setNegativeButton(R.string.cancel, null) .setPositiveButton( R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showOfflineGroupsModifyDialog( context, new ArrayList<Group>(), new OnOfflineGroupSetCallback() { @Override public void onChanged(List<Group> newGroups) { // 设置离线分组 Logger.d(TAG, "设置离线分组%d个", newGroups.size()); if (newGroups.size() > 0) { SinaDB.getOfflineSqlite().insert(getLoggedExtra(null), newGroups); toggleOffline(context); } } }, R.string.offline_groups_dialog); } }) .show(); } else { OfflineService.startOffline((ArrayList) groups); } }
@Override protected void onResume() { super.onResume(); if (!AppContext.isLogedin()) finish(); setFabType(); BaiduAnalyzeUtils.onPageStart("首页"); }
@Override public WeiBoUser workInBackground(String... params) throws TaskException { // 1、请求授权 AccessToken accessToken = SinaSDK.getInstance(null).getAccessToken(params[0]); AccountBean account = new AccountBean(); account.setToken(accessToken); // 2、加载用户信息 publishProgress(R.string.account_load_userinfo); WeiBoUser user = SinaSDK.getInstance(accessToken).userShow(accessToken.getUid(), null); // 3、加载分组信息 publishProgress(R.string.account_load_groups); Groups groups = SinaSDK.getInstance(accessToken).friendshipGroups(); // 4、更新新账户到DB account.setUser(user); account.setGroups(groups); account.setAccount(mAccount); account.setPassword(mPassword); account.setUserId(user.getIdstr()); // 2014-09-18 移除了所有账户信息,包括LoggedIn,所以需要调用AccountFragment.login() AccountDB.remove(account.getUserId()); AccountDB.newAccount(account); if (AppContext.isLogedin() && // 2014-09-18 仅更新登录用户数据 AppContext.getUser().getIdstr().equals(user.getIdstr())) { AccountFragment.login(account, false); } if (getActivity() != null) { Intent data = new Intent(); data.putExtra("token", accessToken); getActivity().setResult(Activity.RESULT_OK, data); } return user; }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_AUTH) { if (resultCode == Activity.RESULT_OK) { AccountBean accountBean = (AccountBean) data.getSerializableExtra("account"); AppContext.getAccount().setAccessToken(accountBean.getAccessToken()); if (accountBean.getUser() != null) { AppContext.getAccount().setUser(accountBean.getUser()); } if (accountBean.getGroups() != null) { AppContext.getAccount().setGroups(accountBean.getGroups()); } AccountUtils.newAccount(AppContext.getAccount()); AccountUtils.setLogedinAccount(AppContext.getAccount()); login(); } } }
@Override protected void onResume() { super.onResume(); // 修复偶尔Fab按钮不见了 if (menuFragment != null && menuFragment.getSelectedId() == 1) { if (fabBtn.getVisibility() != View.VISIBLE) { fabBtn.setVisibility(View.VISIBLE); } } if (!AppContext.isLoggedIn()) { finish(); return; } if (AppContext.getAccount().getAccessToken().isExpired()) { requestLogin(this, AppContext.getAccount()); } invalidateOptionsMenu(); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1000 && RESULT_OK == resultCode) { showMessage(R.string.weico_success); AccessToken token = (AccessToken) data.getSerializableExtra("token"); Logger.e(token); SinaDB.getSqlite().deleteAll(null, AccessToken.class); SinaDB.getSqlite().insert(null, token); AppContext.setAdvancedToken(token); } else if (requestCode == 2000 && RESULT_OK == resultCode) { login(); } }
private void setupFab(Bundle savedInstanceState) { ArrayList<Group> groupList = new ArrayList<>(); // 全部微博 Group group = new Group(); group.setName(getString(R.string.timeline_all)); group.setId("statusesFriendsTimeLine"); groupList.add(group); // 互相关注 group = new Group(); group.setName(getString(R.string.timeline_bilateral)); group.setId("statusesBilateralTimeLine"); groupList.add(group); // 发给我的 group = new Group(); group.setName(getString(R.string.timeline_tome)); group.setId("statusesToMe"); groupList.add(group); // 分组微博 groupList.addAll(AppContext.getAccount().getGroups().getLists()); fabGroupsFragment = (FabGroupsFragment) getFragmentManager().findFragmentById(R.id.fragmentFabGroups); fabGroupsFragment.resetSelectedPosition(); fabGroupsFragment.setItems(groupList); View sheetView = findViewById(R.id.fab_sheet); View overlay = findViewById(R.id.overlay); int sheetColor = getResources().getColor(R.color.comm_white); int fabColor = MDHelper.resolveColor(this, R.attr.colorPrimary, Color.BLACK); materialSheetFab = new MaterialSheetFab(fabBtn, sheetView, overlay, sheetColor, fabColor); materialSheetFab.setEventListener( new MaterialSheetFabEventListener() { @Override public void onShowSheet() { super.onShowSheet(); fabGroupsFragment.show(); MobclickAgent.onEvent(MainActivity.this, "fab_groups"); } }); materialSheetFab.showFab(); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { if (unreadCountNotifier == null) unreadCountNotifier = new UnreadCountNotifier(this); if (!AppContext.isLogedin()) { stopSelf(); return super.onStartCommand(intent, flags, startId); } String action = intent != null ? intent.getAction() : ""; if (ACTION_GET.equals(action)) { resetTheTime(); new UnreadTask().execute(); } else if (ACTION_UPDATE.equals(action)) { Logger.v(TAG, "刷新时间"); resetTheTime(); } return super.onStartCommand(intent, flags, startId); }
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (intent == null) return; String action = intent.getAction(); // 切换账号 if (ACTION_LOGIN.equals(action)) { setupFab(null); menuFragment.changeAccount(); } else { if (ACTION_NOTIFICATION.equals(action) && MenuFragment.MENU_FRIENDSHIP == Integer.parseInt(intent.getStringExtra("type"))) { FriendshipPagerFragment.launch(this, AppContext.getAccount().getUser(), 1); } else { int menuId = getActionType(intent, action); menuFragment.triggerMenuClick(menuId); } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.as_ui_main); AdTokenUtils.loadIfExpired(); new GetTokenInfoTask(AppContext.getAccount()).execute(); BizFragment.getBizFragment(this); if (Build.VERSION.SDK_INT >= 19) { ViewGroup drawerRoot = (ViewGroup) findViewById(R.id.layDrawerRoot); drawerRoot.setPadding( drawerRoot.getPaddingLeft(), SystemBarUtils.getStatusBarHeight(this), drawerRoot.getPaddingRight(), drawerRoot.getBottom()); } if (Build.VERSION.SDK_INT == 19) { ViewGroup rootMain = (ViewGroup) findViewById(R.id.layMainRoot); rootMain.setPadding( rootMain.getPaddingLeft(), rootMain.getPaddingTop(), rootMain.getPaddingRight(), rootMain.getBottom() + SystemBarUtils.getNavigationBarHeight(this)); } mToolbar = getToolbar(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(false); mDrawerToggle = new ActionBarDrawerToggle( this, mDrawerLayout, getToolbar(), R.string.draw_open, R.string.draw_close) { public void onDrawerClosed(View view) { super.onDrawerClosed(view); invalidateOptionsMenu(); if (isToolbarShown()) btnFab.show(true); } public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); invalidateOptionsMenu(); btnFab.hide(true); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); lastSelectedMenu = savedInstanceState == null ? null : (MenuBean) savedInstanceState.getSerializable("menu"); if (ActivityHelper.getBooleanShareData("isFirstLaunch", true)) { ActivityHelper.putBooleanShareData("isFirstLaunch", false); mDrawerLayout.openDrawer(Gravity.LEFT); btnFab.hide(); getSupportActionBar().setTitle(R.string.draw_timeline); } else { if (lastSelectedMenu != null) getSupportActionBar().setTitle(lastSelectedMenu.getTitleRes()); else getSupportActionBar().setTitle(R.string.draw_timeline); } if (savedInstanceState == null) { String action = getIntent() != null ? getIntent().getAction() : null; String type = getActionType(getIntent(), action); menuFragment = MenuFragment.newInstance(type); getFragmentManager() .beginTransaction() .add(R.id.menu_frame, menuFragment, "MenuFragment") .commit(); // 添加右侧的抽屉 // GroupsMenuFragment groupsMenuFragment = GroupsMenuFragment.newInstance(); // getFragmentManager().beginTransaction().add(R.id.groups_frame, // groupsMenuFragment, "GroupsMenuFragment").commit(); } else { menuFragment = (MenuFragment) getFragmentManager().findFragmentByTag("MenuFragment"); // 2014-8-30 解决因为状态保存而导致的耗时阻塞 if (lastSelectedMenu.getType().equals("1")) onMenuSelected(lastSelectedMenu, true, null); } // 更新FAB的颜色 btnFab.setColorNormal(AisenUtils.getThemeColor(this)); btnFab.setColorPressed(AisenUtils.getThemeColor(this)); btnFab.setColorRipple(AisenUtils.getThemeColor(this)); btnFab.setOnLongClickListener(this); }
/** * 显示一个Dialog,用来设置分组 * * @param activity * @param selectedGroups * @param callback * @param titleId */ public static void showOfflineGroupsModifyDialog( Activity activity, List<Group> selectedGroups, final OnOfflineGroupSetCallback callback, int titleId) { String[] items = new String[AppContext.getGroups().getLists().size()]; final boolean[] editCheckedItems = new boolean[AppContext.getGroups().getLists().size()]; for (int i = 0; i < AppContext.getGroups().getLists().size(); i++) { Group group = AppContext.getGroups().getLists().get(i); items[i] = group.getName(); editCheckedItems[i] = false; for (Group groupSelectd : selectedGroups) { if (groupSelectd.getId().equals(group.getIdstr())) { editCheckedItems[i] = true; break; } } } AlertDialogWrapper.Builder dialogBuilder = new AlertDialogWrapper.Builder(activity) .setTitle(titleId) .setMultiChoiceItems( items, editCheckedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { editCheckedItems[which] = isChecked; } }); dialogBuilder .setNegativeButton(R.string.cancel, null) .setPositiveButton( R.string.confirm, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 艹,这个控件这里有bug GlobalContext.getInstance() .getHandler() .postDelayed( new Runnable() { @Override public void run() { List<Group> groups = new ArrayList<Group>(); for (int i = 0; i < editCheckedItems.length; i++) { if (editCheckedItems[i]) { groups.add(AppContext.getGroups().getLists().get(i)); } } callback.onChanged(groups); } }, 300); } }) .show(); }
public static Extra getLoggedExtra(String key) { return new Extra(AppContext.getUser().getIdstr(), key); }
public static boolean isLoggedUser(WeiBoUser user) { return user.getIdstr().equalsIgnoreCase(AppContext.getUser().getIdstr()); }
@Override public UnreadCount workInBackground(Void... params) throws TaskException { if (!AppContext.isLogedin()) return null; UnreadCount result = SinaSDK.getInstance(AppContext.getToken()).remindUnread(AppContext.getUser().getIdstr()); // result.setMention_status(500); // result.setMention_cmt(100); // result.setCmt(20); // result.setFollower(10); AppContext.setUnreadCount(new UnreadCount()); ; if (AppSettings.isNotifyComment()) AppContext.getUnreadCount().setCmt(result.getCmt()); if (AppSettings.isNotifyCommentMention()) AppContext.getUnreadCount().setMention_cmt(result.getMention_cmt()); if (AppSettings.isNotifyStatusMention()) AppContext.getUnreadCount().setMention_status(result.getMention_status()); if (AppSettings.isNotifyFollower()) AppContext.getUnreadCount().setFollower(result.getFollower()); if (AppSettings.isNotifyDm()) AppContext.getUnreadCount().setDm(result.getDm()); // 更新DB result.setId(AppContext.getUser().getIdstr()); SinaDB.getSqlite().insert(null, AppContext.getUnreadCount()); return result; }
public static UnreadCount getUnreadCount() { if (!AppContext.isLogedin()) return null; return SinaDB.getSqlite().selectById(null, UnreadCount.class, AppContext.getUser().getIdstr()); }