private void getFromCloud() { try { groups = new ArrayList<>(); AVUser user = AVUser.getCurrentUser(); JSONArray array = user.getJSONArray("groups"); if (array != null) { for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); WrapGroup wrapGroup = new WrapGroup(); Group group = new Group(); group.setWidth(236); group.setHeight(354); group.setImageurl(object.getString("imageurl")); group.setTitle(object.getString("title")); group.setUrl(object.getString("url")); wrapGroup.setGroup(group); wrapGroup.setIscollected(true); wrapGroup.setDate(new Date().getTime()); wrapGroup.setGroupid(object.getString("groupid")); groups.add(wrapGroup); } saveDb(groups); mAdapter.replaceWith(groups); showSnackBar("云端同步成功"); } } catch (JSONException e) { e.printStackTrace(); KLog.e(e); } }
/** 登陆 */ public void login() { String username = setUsername.getText().toString(); String password = setPassword.getText().toString(); if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) return; if (AVUser.getCurrentUser() != null) { showLoadingDialog("当前用户为:" + AVUser.getCurrentUser().getUsername() + "\n正在切换用户..."); } else { showLoadingDialog("正在登陆..."); } AVUser.logInInBackground( username, password, new LogInCallback<AVUser>() { @Override public void done(AVUser u, AVException e) { if (e == null) { Utils.showToast(UserActivity.this, "登陆成功"); User user = AVUser.getCurrentUser(User.class); textView.setText( "Username:"******"\n" + "DisplayName:" + user.getDisplayName() + "\n"); Log.i("Session-token", user.getUsername() + user.getSessionToken()); } else { textView.setText(e.getMessage()); } cancelLoadingDialog(); } }); }
public void saveData(AVUser mAvUser) { mAvUser.put("selectSubjectName", selectSubjectName); mAvUser.put("selectSubjectId", selectSubjectId); mAvUser.saveInBackground( new SaveCallback() { @Override public void done(AVException arg0) { // TODO Auto-generated method stub if (arg0 == null) { ShareTools.getInstance() .saveInitMemory(LoginInitMemory.this, selectSubjectId, selectSubjectName); Intent intent = new Intent(); intent.setClass(LoginInitMemory.this, MainHomeActivity.class); Bundle mBundle = new Bundle(); mBundle.putString("subject_id", selectSubjectId); mBundle.putString("subject_name", selectSubjectName); intent.putExtras(mBundle); startActivity(intent); overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right); LoginInitMemory.this.finish(); } else { Toast.makeText( LoginInitMemory.this, "初始化失败,请重试" + arg0.getMessage(), Toast.LENGTH_SHORT) .show(); } } }); }
public static AVUser signUp(String name, String password) throws AVException { AVUser user = new AVUser(); user.setUsername(name); user.setPassword(password); user.signUp(); return user; }
@Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_user_friend, null); } TextView alpha = ViewHolder.findViewById(convertView, R.id.alpha); TextView nameView = ViewHolder.findViewById(convertView, R.id.tv_friend_name); ImageView avatarView = ViewHolder.findViewById(convertView, R.id.img_friend_avatar); AVUser friend = data.get(position); final String name = friend.getUsername(); final String avatarUrl = getAvatarUrl(friend); UserService.displayAvatar(avatarUrl, avatarView); nameView.setText(name); int section = getSectionForPosition(position); if (position == getPositionForSection(section)) { alpha.setVisibility(View.VISIBLE); alpha.setText(getSortLetters(friend)); } else { alpha.setVisibility(View.GONE); } return convertView; }
private void saveToCloud() { try { JSONArray array = new JSONArray(); for (WrapGroup group : groups) { JSONObject object = new JSONObject(); object.put("groupid", group.getGroupid()); object.put("title", group.getGroup().getTitle()); object.put("imageurl", group.getGroup().getImageurl()); object.put("url", group.getGroup().getUrl()); array.put(object); } AVUser user = AVUser.getCurrentUser(); user.put("groups", array); user.saveInBackground( new SaveCallback() { @Override public void done(AVException e) { if (e == null) { showSnackBar("云端同步完成"); } } }); } catch (JSONException e) { e.printStackTrace(); KLog.e(e); } }
public void inflatData() { AVUser currentUser = AVUser.getCurrentUser(); if (currentUser != null) { profile_image.setImageResource(R.drawable.user_profile); loginStatus.setText(currentUser.getUsername()); } }
/** 修改email(API上说的开启邮箱验证不知道怎么开???) */ public void updEmail() { String email = setEmail.getText().toString(); AVUser currentUser = AVUser.getCurrentUser(); if (TextUtils.isEmpty(email) || currentUser == null || !currentUser.isAuthenticated()) return; showLoadingDialog("修改邮箱..."); currentUser.setEmail(email); currentUser.saveInBackground(saveCallback); }
public static void updateUserInfo() { AVUser user = AVUser.getCurrentUser(); if (user != null) { AVInstallation installation = AVInstallation.getCurrentInstallation(); if (installation != null) { user.put(User.INSTALLATION, installation); user.saveInBackground(); } } }
public static void removeFriend(String friendId, final SaveCallback saveCallback) { AVUser user = AVUser.getCurrentUser(); user.unfollowInBackground( friendId, new FollowCallback() { @Override public void done(AVObject object, AVException e) { saveCallback.done(e); } }); }
public static void findFriendsWithCachePolicy( AVQuery.CachePolicy cachePolicy, FindCallback<AVUser> findCallback) { AVUser curUser = AVUser.getCurrentUser(); AVQuery<AVUser> q = null; try { q = curUser.followeeQuery(AVUser.class); } catch (Exception e) { throw new NullPointerException(); } q.setCachePolicy(cachePolicy); q.include("followee"); q.findInBackground(findCallback); }
private String getSortLetters(AVUser user) { String username = user.getUsername(); if (username != null) { String pinyin = CharacterParser.getPingYin(user.getUsername()); String sortString = pinyin.substring(0, 1).toUpperCase(); if (sortString.matches("[A-Z]")) { return sortString.toUpperCase(); } else { return "#"; } } else { return "#"; } }
public static List<AVUser> searchUser(String searchName, int skip) throws AVException { AVQuery<AVUser> q = AVUser.getQuery(AVUser.class); q.whereContains(User.USERNAME, searchName); q.limit(Constant.PAGE_SIZE); q.skip(skip); AVUser user = AVUser.getCurrentUser(); List<String> friendIds = new ArrayList<String>(CacheService.getFriendIds()); friendIds.add(user.getObjectId()); q.whereNotContainedIn(Constant.OBJECT_ID, friendIds); q.orderByDescending(Constant.UPDATED_AT); q.setCachePolicy(AVQuery.CachePolicy.NETWORK_ELSE_CACHE); List<AVUser> users = q.find(); CacheService.registerUsers(users); return users; }
@Click(R.id.id_btn_follow) void follow() { AVUser user = AVUser.getCurrentUser(); AVRelation<AVObject> relation = user.getRelation("follow"); relation.add(mDream.getAVUser("user")); user.saveInBackground( new SaveCallback() { @Override public void done(AVException e) { if (e == null) { showToast("关注成功"); } } }); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_collect: showToast("收藏"); AVUser user = AVUser.getCurrentUser(); final AVObject collect = new AVObject("user_collect"); collect.put("user", user); collect.put("event", mDream); collect.saveInBackground( new SaveCallback() { @Override public void done(AVException e) { if (e == null) { showToast("收藏成功"); mDream.increment("collectNum"); mDream.saveInBackground(); } } }); break; case R.id.action_share: showToast("分享"); break; case android.R.id.home: finish(); break; } return super.onOptionsItemSelected(item); }
public static void launch(Activity fromActivity) { ChatManager chatManager = ChatManager.getInstance(); chatManager.setupManagerWithUserId(AVUser.getCurrentUser().getObjectId()); chatManager.openClient(null); Intent intent = new Intent(fromActivity, MainActivity.class); fromActivity.startActivity(intent); }
/** 设置手机号,等待验证码(注册时可用,重置手机号往往应该先验证一下当前的(主动发送一次验证码到当前手机号)) */ public void setMobile() { final String mobile = setMobilePhone.getText().toString(); User user = AVUser.getCurrentUser(User.class); if (TextUtils.isEmpty(mobile) || mobile.length() != 11 || user == null || !user.isAuthenticated()) return; showLoadingDialog("设置手机号..."); user.setMobilePhoneNumber(mobile); // 先设置手机号 user.saveInBackground( new SaveCallback() { @Override public void done(AVException e) { if (e == null) { AVUser.requestMobilePhoneVerifyInBackground( mobile, new RequestMobileCodeCallback() { @Override public void done(AVException e) { if (e == null) { Utils.showToast(UserActivity.this, "已发送验证码短信,请验证!"); } else { textView.setText(e.getMessage()); } cancelLoadingDialog(); } }); } else { textView.setText(e.getMessage()); cancelLoadingDialog(); } } }); }
public static void displayAvatar(AVUser user, ImageView avatarView) { if (user != null) { String avatarUrl = User.getAvatarUrl(user); if (avatarUrl != null) { // ImageLoader.getInstance().displayImage(avatarUrl, avatarView, // PhotoUtils.avatarImageOptions); avatarView.setImageBitmap( ColoredBitmapProvider.getInstance() .createColoredBitmapByHashString(user.getObjectId())); } else { avatarView.setImageBitmap( ColoredBitmapProvider.getInstance() .createColoredBitmapByHashString(user.getObjectId())); } } }
private void setSelectCourse(int position) { selectSubjectName = mSubjectModels.get(position).getSubjectName(); selectSubjectId = mSubjectModels.get(position).getSubjectId(); AVQuery<AVUser> mAvQuery = AVUser.getQuery(); mAvQuery.whereEqualTo( "username", ShareTools.getInstance().readUserInfo(LoginInitMemory.this).get(ShareTools.USER_NAME)); mAvQuery.findInBackground( new FindCallback<AVUser>() { @Override public void done(List<AVUser> arg0, AVException arg1) { // TODO Auto-generated method stub if (arg1 == null) { if (arg0 != null && !arg0.isEmpty()) { Message message = Message.obtain(); message.what = 1000; message.obj = arg0.get(0); mHandler.sendMessage(message); } else { Toast.makeText(LoginInitMemory.this, "用户为空", Toast.LENGTH_SHORT).show(); } } else { Log.e("ShareTools", arg1.getMessage()); } } }); }
private void register(final String username, final String password) { AVUser user = new AVUser(); user.setUsername(username); user.setPassword(password); user.signUpInBackground( new SignUpCallback() { @Override public void done(AVException e) { LogUtil.i(TAG, "register----" + e.getMessage()); if (e == null) { login(username, password); } else { showToast(e.getMessage()); } } }); }
public String getAvatarUrl(AVUser user) { AVFile avatar = user.getAVFile("avatar"); if (avatar != null) { return avatar.getUrl(); } else { return null; } }
public static String nameOfConversation(AVIMConversation conversation) { if (isValidConversation(conversation)) { if (typeOfConversation(conversation) == ConversationType.Single) { String otherId = otherIdOfConversation(conversation); AVUser user = AVUserCacheUtils.getCachedUser(otherId); if (user != null) { return user.getUsername(); } else { LogUtils.e("use is null"); return "对话"; } } else { return conversation.getName(); } } else { return ""; } }
/** 验证手机号 */ public void signMobile() { String code = setCode.getText().toString(); AVUser user = AVUser.getCurrentUser(); if (TextUtils.isEmpty(code) || user == null || !user.isAuthenticated()) return; showLoadingDialog("正在验证手机号"); AVUser.verifyMobilePhoneInBackground( code, new AVMobilePhoneVerifyCallback() { @Override public void done(AVException e) { if (e == null) { Utils.showToast(UserActivity.this, "手机号验证成功!"); } else { textView.setText(e.getMessage()); } cancelLoadingDialog(); } }); }
/** 重置密码(通过邮箱验证,会先检验邮箱正确与否),往往在忘记密码时候使用,所以无需注销 */ public void resetPwd() { final String email = setEmail.getText().toString(); AVUser currentUser = AVUser.getCurrentUser(); if (TextUtils.isEmpty(email) || currentUser == null || !currentUser.isAuthenticated()) return; showLoadingDialog("重置密码中..."); AVUser.requestPasswordResetInBackground( email, new RequestPasswordResetCallback() { @Override public void done(AVException e) { if (e == null) { Utils.showToast(UserActivity.this, "重置密码成功,请去" + email + "邮箱验证"); } else { textView.setText(e.getMessage()); } cancelLoadingDialog(); } }); }
/** 手动更改密码,更改后自动重新登陆即可同步session-token */ public void updPassword() { final String password = setPassword.getText().toString(); final AVUser currentUser = AVUser.getCurrentUser(); if (TextUtils.isEmpty(password) || currentUser == null || !currentUser.isAuthenticated()) return; showLoadingDialog("正在修改密码..."); currentUser.setPassword(password); // 修改完密码后session-token会失效(再去修改用户信息时异常),重新登陆(重置了session-token) currentUser.saveInBackground( new SaveCallback() { @Override public void done(AVException e) { if (e == null) { // 修改成功 cancelLoadingDialog(); showLoadingDialog("正在重新登陆"); AVUser.logInInBackground( currentUser.getUsername(), password, new LogInCallback<User>() { @Override public void done(User newUser, AVException e) { if (e == null) { Utils.showToast(UserActivity.this, "重新登陆成功"); textView.setText( "Username:"******"\n" + "DisplayName:" + newUser.getDisplayName() + "\n"); } else { textView.setText(e.getMessage()); } cancelLoadingDialog(); } }, User.class); } else { textView.setText(e.getMessage()); cancelLoadingDialog(); } } }); }
/** 更新用户名(login和signUp得到的user是授权的) */ public void updUsername() { AVUser user = AVUser.getCurrentUser(); String usernmae = setUsername.getText().toString(); if (TextUtils.isEmpty(usernmae)) return; if (user == null) { Utils.showToast(this, "当前无登陆用户"); return; } if (!user.isAuthenticated()) { Utils.showToast(this, "当前登陆用户无授权,请重新登录"); return; } showLoadingDialog("正在更改username"); user.setUsername(usernmae); // 自己的用户信息登陆后可以修改 user.saveInBackground( new SaveCallback() { @Override public void done(AVException e) { if (e == null) { textView.setText("保存成功"); } else { textView.setText(e.getMessage()); } AVUser user1 = AVUser.getCurrentUser(); if (user1 != null) { Log.i("Session-token", user1.getUsername() + user1.getSessionToken()); } cancelLoadingDialog(); } }); // 查询得到的user都是没有授权的 /*ParseQuery<ParseUser> query = ParseUser.getQuery(); query.getInBackground("WXIMPuEaUZ", new GetCallback<ParseUser>() { @Override public void done(ParseUser object, ParseException e) { if (object != null) { object.setUsername("李某某"); object.saveInBackground(saveCallback); } } });*/ }
private void initData() { Intent intent = getIntent(); Bundle b = intent.getBundleExtra("bundle"); bookInfo = b.getParcelable("bookinfo"); loginUser = StatusService.getInstance().getUser(); if (loginUser.getUsername().equals(bookInfo.getOwner())) { isOwner = true; } else { isOwner = false; } mContext = this; }
/** 注销 */ public void logout() { if (AVUser.getCurrentUser() != null) { showLoadingDialog("当前用户为:" + AVUser.getCurrentUser().getUsername() + "\n正在注销用户..."); } else { Utils.showToast(UserActivity.this, "当前无用户登陆"); // 无用户登陆时注销也不会错 return; } AVUser.logOut(); // 直接注销,清除缓存 Utils.showToast(UserActivity.this, "注销成功"); // AVUser.logOutInBackground(new LogOutCallback() { // @Override // public void done(AVException e) { // if (e == null) {//注销成功后currentUser为null // Utils.showToast(UserActivity.this, "注销成功"); // } else { // textView.setText(e.getMessage()); // } // dismissLoadingDialog(); // } // }); }
@Override public View getView(int position, View conView, ViewGroup parent) { // TODO Auto-generated method stub if (conView == null) { conView = inflater.inflate(R.layout.contact_add_friend_item, null); } final AddRequest addRequest = datas.get(position); TextView nameView = ViewHolder.findViewById(conView, R.id.name); ImageView avatarView = ViewHolder.findViewById(conView, R.id.avatar); final Button addBtn = ViewHolder.findViewById(conView, R.id.add); View agreedView = ViewHolder.findViewById(conView, R.id.agreedView); AVUser from = addRequest.getFromUser(); UserService.displayAvatar(from, avatarView); if (from != null) { nameView.setText(from.getUsername()); } int status = addRequest.getStatus(); if (status == AddRequest.STATUS_WAIT) { addBtn.setVisibility(View.VISIBLE); agreedView.setVisibility(View.GONE); addBtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (listener != null) { listener.onAgreeAddRequest(addRequest); } } }); } else if (status == AddRequest.STATUS_DONE) { addBtn.setVisibility(View.GONE); agreedView.setVisibility(View.VISIBLE); } return conView; }
public static void saveAvatar(String path) throws IOException, AVException { AVUser user = AVUser.getCurrentUser(); final AVFile file = AVFile.withAbsoluteLocalPath(user.getUsername(), path); file.save(); user.put(User.AVATAR, file); user.save(); user.fetch(); }