public void buildCache() { // 获取我所有的好友关系 List<Friend> friends = NIMClient.getService(FriendService.class).getFriends(); for (Friend f : friends) { friendMap.put(f.getAccount(), f); } // 获取我所有好友的帐号 List<String> accounts = NIMClient.getService(FriendService.class).getFriendAccounts(); if (accounts == null || accounts.isEmpty()) { return; } // 排除黑名单 List<String> blacks = NIMClient.getService(FriendService.class).getBlackList(); accounts.removeAll(blacks); // 排除掉自己 accounts.remove(NimUIKit.getAccount()); // 确定缓存 friendAccountSet.addAll(accounts); LogUtil.i( UIKitLogTag.FRIEND_CACHE, "build FriendDataCache completed, friends count = " + friendAccountSet.size()); }
/** 添加群成员 */ private void addMembersToTeam(final ArrayList<String> selected) { // add members DialogMaker.showProgressDialog(this, getString(R.string.empty), true); NIMClient.getService(TeamService.class) .addMembers(teamId, selected) .setCallback( new RequestCallback<Void>() { @Override public void onSuccess(Void param) { DialogMaker.dismissProgressDialog(); addMember(selected, false); Toast.makeText( NormalTeamInfoActivity.this, R.string.invite_member_success, Toast.LENGTH_SHORT) .show(); } @Override public void onFailed(int code) { DialogMaker.dismissProgressDialog(); Toast.makeText( NormalTeamInfoActivity.this, R.string.invite_member_failed, Toast.LENGTH_SHORT) .show(); } @Override public void onException(Throwable exception) { DialogMaker.dismissProgressDialog(); } }); }
/** 移除群成员 */ @Override public void onRemoveMember(final String account) { DialogMaker.showProgressDialog(this, getString(R.string.empty), true); NIMClient.getService(TeamService.class) .removeMember(teamId, account) .setCallback( new RequestCallback<Void>() { @Override public void onSuccess(Void param) { DialogMaker.dismissProgressDialog(); removeMember(account); Toast.makeText( NormalTeamInfoActivity.this, R.string.remove_member_success, Toast.LENGTH_SHORT) .show(); } @Override public void onFailed(int code) { DialogMaker.dismissProgressDialog(); Toast.makeText( NormalTeamInfoActivity.this, R.string.remove_member_failed, Toast.LENGTH_SHORT) .show(); } @Override public void onException(Throwable exception) { DialogMaker.dismissProgressDialog(); } }); }
@Override public void onEvent(FriendChangedNotify friendChangedNotify) { List<Friend> addedOrUpdatedFriends = friendChangedNotify.getAddedOrUpdatedFriends(); List<String> myFriendAccounts = new ArrayList<>(addedOrUpdatedFriends.size()); List<String> friendAccounts = new ArrayList<>(addedOrUpdatedFriends.size()); List<String> deletedFriendAccounts = friendChangedNotify.getDeletedFriends(); // 如果在黑名单中,那么不加到好友列表中 String account; for (Friend f : addedOrUpdatedFriends) { account = f.getAccount(); friendMap.put(account, f); friendAccounts.add(account); if (NIMClient.getService(FriendService.class).isInBlackList(account)) { continue; } myFriendAccounts.add(account); } // 更新我的好友关系 if (!myFriendAccounts.isEmpty()) { // update cache friendAccountSet.addAll(myFriendAccounts); // log DataCacheManager.Log(myFriendAccounts, "on add friends", UIKitLogTag.FRIEND_CACHE); } // 通知好友关系更新 if (!friendAccounts.isEmpty()) { for (FriendDataChangedObserver o : friendObservers) { o.onAddedOrUpdatedFriends(friendAccounts); } } // 处理被删除的好友关系 if (!deletedFriendAccounts.isEmpty()) { // update cache friendAccountSet.removeAll(deletedFriendAccounts); for (String a : deletedFriendAccounts) { friendMap.remove(a); } // log DataCacheManager.Log( deletedFriendAccounts, "on delete friends", UIKitLogTag.FRIEND_CACHE); // notify for (FriendDataChangedObserver o : friendObservers) { o.onDeletedFriends(deletedFriendAccounts); } } }
@Override public void onEvent(BlackListChangedNotify blackListChangedNotify) { List<String> addedAccounts = blackListChangedNotify.getAddedAccounts(); List<String> removedAccounts = blackListChangedNotify.getRemovedAccounts(); if (!addedAccounts.isEmpty()) { // 拉黑,即从好友名单中移除 friendAccountSet.removeAll(addedAccounts); // log DataCacheManager.Log( addedAccounts, "on add users to black list", UIKitLogTag.FRIEND_CACHE); // notify for (FriendDataChangedObserver o : friendObservers) { o.onAddUserToBlackList(addedAccounts); } // 拉黑,要从最近联系人列表中删除该好友 for (String account : addedAccounts) { NIMClient.getService(MsgService.class) .deleteRecentContact2(account, SessionTypeEnum.P2P); } } if (!removedAccounts.isEmpty()) { // 移出黑名单,判断是否加入好友名单 for (String account : removedAccounts) { if (NIMClient.getService(FriendService.class).isMyFriend(account)) { friendAccountSet.add(account); } } // log DataCacheManager.Log( removedAccounts, "on remove users from black list", UIKitLogTag.FRIEND_CACHE); // 通知观察者 for (FriendDataChangedObserver o : friendObservers) { o.onRemoveUserFromBlackList(removedAccounts); } } }
@Override public void OnChanged(View v, final boolean checkState) { if (!NetworkUtil.isNetAvailable(NormalTeamInfoActivity.this)) { Toast.makeText( NormalTeamInfoActivity.this, R.string.network_is_not_available, Toast.LENGTH_SHORT) .show(); noticeBtn.setCheck(!checkState); return; } NIMClient.getService(TeamService.class) .muteTeam(team.getId(), !checkState) .setCallback( new RequestCallback<Void>() { @Override public void onSuccess(Void param) { if (checkState) { Toast.makeText(NormalTeamInfoActivity.this, "开启消息提醒", Toast.LENGTH_SHORT) .show(); } else { Toast.makeText(NormalTeamInfoActivity.this, "关闭消息提醒", Toast.LENGTH_SHORT) .show(); } } @Override public void onFailed(int code) { if (code == 408) { Toast.makeText( NormalTeamInfoActivity.this, R.string.network_is_not_available, Toast.LENGTH_SHORT) .show(); } else { Toast.makeText( NormalTeamInfoActivity.this, "on failed:" + code, Toast.LENGTH_SHORT) .show(); } noticeBtn.setCheck(!checkState); } @Override public void onException(Throwable exception) {} }); }
/** 非群主退出群 */ private void quitTeam() { DialogMaker.showProgressDialog(this, getString(R.string.empty), true); NIMClient.getService(TeamService.class) .quitTeam(teamId) .setCallback( new RequestCallback<Void>() { @Override public void onSuccess(Void param) { DialogMaker.dismissProgressDialog(); Toast.makeText( NormalTeamInfoActivity.this, R.string.quit_normal_team_success, Toast.LENGTH_SHORT) .show(); setResult( Activity.RESULT_OK, new Intent() .putExtra( TeamExtras.RESULT_EXTRA_REASON, TeamExtras.RESULT_EXTRA_REASON_QUIT)); NIMClient.getService(MsgService.class) .deleteRecentContact2(teamId, SessionTypeEnum.Team); finish(); } @Override public void onFailed(int code) { DialogMaker.dismissProgressDialog(); Toast.makeText( NormalTeamInfoActivity.this, R.string.quit_normal_team_failed, Toast.LENGTH_SHORT) .show(); } @Override public void onException(Throwable exception) { DialogMaker.dismissProgressDialog(); } }); }
/** 缓存监听SDK */ public void registerObservers(boolean register) { NIMClient.getService(FriendServiceObserve.class) .observeFriendChangedNotify(friendChangedNotifyObserver, register); NIMClient.getService(FriendServiceObserve.class) .observeBlackListChangedNotify(blackListChangedNotifyObserver, register); }