Java tutorial
/** * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ccxt.whl.activity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.Header; import org.w3c.dom.Comment; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.InputType; import android.text.TextUtils; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.baidu.frontia.Frontia; import com.baidu.frontia.FrontiaUser; import com.baidu.frontia.api.FrontiaAuthorization; import com.baidu.frontia.api.FrontiaAuthorization.MediaType; import com.baidu.frontia.api.FrontiaAuthorizationListener.AuthorizationListener; import com.baidu.frontia.api.FrontiaAuthorizationListener.UserInfoListener; import com.easemob.EMCallBack; import com.easemob.chat.EMChatManager; import com.easemob.chat.EMGroupManager; import com.ccxt.whl.Constant; import com.ccxt.whl.DemoApplication; import com.ccxt.whl.R; import com.ccxt.whl.db.UserDao; import com.ccxt.whl.domain.User; import com.ccxt.whl.utils.CommonUtils; import com.ccxt.whl.utils.DeviceUuidFactory; import com.ccxt.whl.utils.HttpRestClient; import com.ccxt.whl.utils.JsonToMapList; import com.ccxt.whl.utils.MD5; import com.ccxt.whl.utils.PreferenceUtils; import com.easemob.util.HanziToPinyin; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.BaseJsonHttpResponseHandler; import com.loopj.android.http.RequestParams; import com.loopj.android.http.ResponseHandlerInterface; /** * ? * */ public class LoginActivity extends BaseActivity { private EditText usernameEditText; private EditText passwordEditText; private boolean progressShow; private FrontiaAuthorization mAuthorization;// private String name = null; private String access_token = null; private String sex = null; private String birthday = null; private String headurl = null; private String province = null; private String city = null; private String meida_type = null; private String meida_uid = null; private static String uid = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); usernameEditText = (EditText) findViewById(R.id.username); usernameEditText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);//?? passwordEditText = (EditText) findViewById(R.id.password); // ???? // if (DemoApplication.getInstance().getUserName() != null && DemoApplication.getInstance().getPassword() != null) { if (DemoApplication.getInstance().getUser() != null && DemoApplication.getInstance().getPassword() != null) { startActivity(new Intent(this, MainActivity.class)); finish(); } // ???? usernameEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { passwordEditText.setText(null); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); mAuthorization = Frontia.getAuthorization();// DeviceUuidFactory uuid = new DeviceUuidFactory(this); uid = uuid.getDeviceUuid().toString(); } /** * * * @param view */ public void Slogin(View view) { mAuthorization.authorize(this, FrontiaAuthorization.MediaType.SINAWEIBO.toString(), new AuthorizationListener() { @Override public void onSuccess(FrontiaUser result) { Frontia.setCurrentAccount(result); //if (null != mResultTextView) { String log = "social id: " + result.getId() + "\n" + "token: " + result.getAccessToken() + "\n" + "expired: " + result.getExpiresIn() + "\n" + "name: " + result.getName() + "\n" + "MediaUserId: " + result.getMediaUserId(); Log.d("log nihao ===>" + log); access_token = result.getAccessToken(); name = result.getName(); meida_uid = result.getMediaUserId(); //??? userinfo(MediaType.SINAWEIBO.toString()); } @Override public void onFailure(int errorCode, String errorMessage) { // if (null != mResultTextView) { // mResultTextView.setText("errCode:" + errorCode // + ", errMsg:" + errorMessage); // } Log.d("log nihao ===>" + "errCode:" + errorCode + ", errMsg:" + errorMessage); Toast.makeText(getApplicationContext(), "?" + errorCode + ":" + errorMessage, 0) .show(); return; } @Override public void onCancel() { // if (null != mResultTextView) { // mResultTextView.setText("cancel"); // } Log.d("log nihao ===>onCancel"); } }); } /**** * qq? * @param view */ public void qqlogin(View view) { mAuthorization.authorize(this, FrontiaAuthorization.MediaType.QZONE.toString(), new AuthorizationListener() { @Override public void onSuccess(FrontiaUser result) { Frontia.setCurrentAccount(result); String log = "social id: " + result.getId() + "\n" + "token: " + result.getAccessToken() + "\n" + "expired: " + result.getExpiresIn() + "\n" + "name: " + result.getName() + "\n" + "MediaUserId: " + result.getMediaUserId(); Log.d("lognihao ===>" + log); access_token = result.getAccessToken(); name = result.getName(); meida_uid = result.getMediaUserId(); //??? userinfo(MediaType.QZONE.toString()); } @Override public void onFailure(int errorCode, String errorMessage) { Log.d("log nihao ===>" + "errCode:" + errorCode + ", errMsg:" + errorMessage); Toast.makeText(getApplicationContext(), "?" + errorCode + ":" + errorMessage, 0) .show(); return; } @Override public void onCancel() { Log.d("log nihao ===>onCancel"); } }); } private void userinfo(String accessToken) { final ProgressDialog pd = new ProgressDialog(this); pd.setMessage("?..."); pd.show(); mAuthorization.getUserInfo(accessToken, new UserInfoListener() { @Override public void onSuccess(FrontiaUser.FrontiaUserDetail result) { //if (null != mResultTextView) { Log.d("log useid===>" + result.getId()); Log.d("log AccessToken===>" + result.getAccessToken()); Log.d("log BaiduAccessToken===>" + result.getBaiduAccessToken()); Log.d("log MediaUserId===>" + result.getMediaUserId()); //Log.d("log MediaUserId===>"+result.get); String resultStr = "username:" + result.getName() + "\n" + "birthday:" + result.getBirthday() + "\n" + "city:" + result.getCity() + "\n" + "province:" + result.getProvince() + "\n" + "sex:" + result.getSex() + "\n" + "pic url:" + result.getHeadUrl() + "\n"; Log.d("log userinfo===>" + resultStr); sex = "" + result.getSex(); birthday = result.getBirthday(); headurl = result.getHeadUrl(); province = result.getProvince(); city = result.getCity(); RequestParams params = new RequestParams(); params.add("name", name); params.add("access_token", access_token); params.add("sex", sex); params.add("birthday", birthday); params.add("headurl", headurl); params.add("province", province); params.add("city", city); params.add("meida_type", "sinaweibo"); params.add("meida_uid", meida_uid); //id params.add("uid", uid); /* //Log.d("log",response); try { Map<String, Object> lm = JsonToMapList.getMap(response); Map<String, Object> result = JsonToMapList.getMap(lm.get("result").toString()); String resultStr = "status:" + lm.get("status") + "\n" + "message:" + lm.get("message") + "\n" + "result:" + lm.get("result") + "\n" + "uid:" + result.get("uid") + "\n" + "umd5:" + result.get("umd5") + "\n" + "is_res:" + result.get("is_res") + "\n"; Log.d("log",resultStr); } catch (Exception e) { // TODO: handle exception } //String huanxin_username = MD5.MD5Hash(result.getName()); //String huanxin_pwd = MD5.MD5Hash(result.getName()); //s_reg_login(huanxin_username, huanxin_pwd); */ HttpRestClient.get(Constant.S_LOGIN_URL, params, new BaseJsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, Object response) { // TODO Auto-generated method stub Log.d("log" + rawJsonResponse); //if(str != null && str.length()!= 0){//? //} if (!LoginActivity.this.isFinishing()) pd.dismiss(); if (CommonUtils.isNullOrEmpty(rawJsonResponse)) { Toast.makeText(getApplicationContext(), "?,?", 0) .show(); return; } Map<String, Object> lm = JsonToMapList.getMap(rawJsonResponse); if (CommonUtils.isNullOrEmpty(lm.get("result").toString())) { //if(false){ } else { Map<String, Object> result = JsonToMapList.getMap(lm.get("result").toString()); String resultStr = "status:" + lm.get("status") + "\n" + "message:" + lm.get("message") + "\n" + "result:" + lm.get("result") + "\n" + "uid:" + result.get("uid") + "\n" + "umd5:" + result.get("umd5") + "\n" + "is_res:" + result.get("is_res") + "\n" + "headurl:" + result.get("headurl") + "\n" + "name:" + result.get("name") + "\n" + "sex:" + result.get("sex") + "\n" + "age:" + result.get("age") + "\n" + "province:" + result.get("province") + "\n" + "city:" + result.get("city") + "\n" + "pwd:" + result.get("pwd") + "\n"; Log.d("log" + resultStr); //???id nullsaveinfo Preference //DemoApplication.getInstance().setUser(result.get("umd5").toString()); //System.out.println("========================"+result.get("umd5").toString()); /********/ PreferenceUtils.getInstance(getBaseContext()) .setSettingUserPic(result.get("headurl").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserNickName(result.get("name").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserSex(result.get("sex").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserAge(result.get("age").toString()); PreferenceUtils.getInstance(getBaseContext()).setSettingUserArea( result.get("province").toString() + " " + result.get("city").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserZhiye(result.get("zhiye").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserQianming(result.get("qianming").toString()); String huanxin_username = result.get("umd5").toString(); String huanxin_pwd = result.get("pwd").toString(); Log.d("log huanxin_username:" + huanxin_username + "|huanxin_pwd:" + huanxin_pwd + "|token:" + access_token); //??? if (result.get("is_res").toString().equals("yes")) { login(huanxin_username, huanxin_pwd); } else { s_reg_login(huanxin_username, huanxin_pwd); } } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, Object errorResponse) { pd.dismiss(); Toast.makeText(getApplicationContext(), ",?", 0).show(); return; // TODO Auto-generated method stub } @Override protected Object parseResponse(String rawJsonData, boolean isFailure) throws Throwable { // TODO Auto-generated method stub return null; } }); } @Override public void onFailure(int errCode, String errMsg) { Log.d("log nihao ===>" + "errCode:" + errCode + ", errMsg:" + errMsg); sex = ""; birthday = ""; headurl = ""; province = ""; city = ""; pd.dismiss(); Toast.makeText(getApplicationContext(), "????", 0).show(); return; } }); } /** * ?? */ public void s_reg_login(final String username, final String pwd) { if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(pwd)) { final ProgressDialog pd = new ProgressDialog(this); pd.setMessage("..."); pd.show(); new Thread(new Runnable() { public void run() { try { // sdk EMChatManager.getInstance().createAccountOnServer(username, pwd); runOnUiThread(new Runnable() { public void run() { if (!LoginActivity.this.isFinishing()) pd.dismiss(); // ??? // DemoApplication.getInstance().setUserName(username); DemoApplication.getInstance().setUser(username); Toast.makeText(getApplicationContext(), "?", 0).show(); login(username, pwd); //finish(); } }); } catch (final Exception e) { runOnUiThread(new Runnable() { public void run() { if (!LoginActivity.this.isFinishing()) pd.dismiss(); if (e != null && e.getMessage() != null) { String errorMsg = e.getMessage(); if (errorMsg.indexOf("EMNetworkUnconnectedException") != -1) { Toast.makeText(getApplicationContext(), "?", 0).show(); } else if (errorMsg.indexOf("conflict") != -1) { login(username, pwd); //Toast.makeText(getApplicationContext(), "?", 0).show(); } else if (errorMsg.indexOf("not support the capital letters") != -1) { Toast.makeText(getApplicationContext(), "??????", 0).show(); } else { Toast.makeText(getApplicationContext(), ": " + e.getMessage(), 1).show(); } } else { Toast.makeText(getApplicationContext(), ": ", 1).show(); } } }); } } }).start(); } } /** * ? * * @param view */ public void login(final String username, final String password) { if (!CommonUtils.isNetWorkConnected(this)) { Toast.makeText(this, R.string.network_isnot_available, Toast.LENGTH_SHORT).show(); return; } //final String username = usernameEditText.getText().toString(); //final String password = passwordEditText.getText().toString(); if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) { progressShow = true; final ProgressDialog pd = new ProgressDialog(LoginActivity.this); pd.setCanceledOnTouchOutside(false); pd.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { progressShow = false; } }); pd.setMessage("..."); pd.show(); // sdk?? EMChatManager.getInstance().login(username, password, new EMCallBack() { @Override public void onSuccess() { /***************** * ? *****************/ if (!progressShow) { return; } // ????? // DemoApplication.getInstance().setUserName(username); DemoApplication.getInstance().setUser(username); DemoApplication.getInstance().setPassword(password); DemoApplication.getInstance().setUserNickName(name); runOnUiThread(new Runnable() { public void run() { pd.setMessage("???..."); } }); try { // demo??????username?? List<String> usernames = EMChatManager.getInstance().getContactUserNames(); Map<String, User> userlist = new HashMap<String, User>(); for (String username : usernames) { User user = new User(); user.setUsername(username); setUserHearder(username, user); userlist.put(username, user); } // user"" User newFriends = new User(); newFriends.setUsername(Constant.NEW_FRIENDS_USERNAME); newFriends.setNick(""); newFriends.setHeader(""); userlist.put(Constant.NEW_FRIENDS_USERNAME, newFriends); // "?" User groupUser = new User(); groupUser.setUsername(Constant.GROUP_USERNAME); groupUser.setNick("?"); groupUser.setHeader(""); userlist.put(Constant.GROUP_USERNAME, groupUser); // DemoApplication.getInstance().setContactList(userlist); /***?MainActivity?db* / // db UserDao dao = new UserDao(LoginActivity.this); List<User> users = new ArrayList<User>(userlist.values()); dao.saveContactList(users);/**/ // ??,sdkEMGroupManagerdb EMGroupManager.getInstance().getGroupsFromServer(); // after login, we join groups in separate threads; EMGroupManager.getInstance().joinGroupsAfterLogin(); } catch (Exception e) { e.printStackTrace(); } if (!LoginActivity.this.isFinishing()) pd.dismiss(); // ? startActivity(new Intent(LoginActivity.this, MainActivity.class)); finish(); } @Override public void onProgress(int progress, String status) { } @Override public void onError(int code, final String message) { if (!progressShow) { return; } //Log.d("log","code"+code); runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getApplicationContext(), ": " + message, 0).show(); } }); } }); } } /** * * * @param view */ public void login(View view) { if (!CommonUtils.isNetWorkConnected(this)) { Toast.makeText(this, R.string.network_isnot_available, Toast.LENGTH_SHORT).show(); return; } final String email = usernameEditText.getText().toString(); final String password = passwordEditText.getText().toString(); if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) { progressShow = true; final ProgressDialog pd = new ProgressDialog(LoginActivity.this); pd.setCanceledOnTouchOutside(false); pd.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { progressShow = false; } }); pd.setMessage("..."); pd.show(); // sdk?? RequestParams params = new RequestParams(); params.add("tel_email", email); params.add("pwd", MD5.MD5Hash(password)); params.add("uid", uid); HttpRestClient.get(Constant.LOGIN_URL, params, new BaseJsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, Object response) { Log.d("login_res_json" + rawJsonResponse); pd.dismiss(); if (CommonUtils.isNullOrEmpty(rawJsonResponse)) { Toast.makeText(getApplicationContext(), "?,?", 0) .show(); return; } Map<String, Object> lm = JsonToMapList.getMap(rawJsonResponse); if (lm.get("status").toString() != null && lm.get("status").toString().equals("yes")) { Map<String, Object> result = JsonToMapList.getMap(lm.get("result").toString()); String resultStr = "status:" + lm.get("status") + "\n" + "message:" + lm.get("message") + "\n" + "result:" + lm.get("result") + "\n" + "uid:" + result.get("uid") + "\n" + "umd5:" + result.get("umd5") + "\n" + "is_res:" + result.get("is_res") + "\n" + "headurl:" + result.get("headurl") + "\n" + "name:" + result.get("name") + "\n" + "sex:" + result.get("sex") + "\n" + "age:" + result.get("age") + "\n" + "province:" + result.get("province") + "\n" + "city:" + result.get("city") + "\n" + "pwd:" + result.get("pwd") + "\n"; Log.d("login_res_obj" + resultStr); //???id nullsaveinfo Preference //DemoApplication.getInstance().setUser(result.get("umd5").toString()); //System.out.println("========================"+result.get("umd5").toString()); /********/ PreferenceUtils.getInstance(getBaseContext()) .setSettingUserPic(result.get("headurl").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserNickName(result.get("name").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserSex(result.get("sex").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserAge(result.get("age").toString()); PreferenceUtils.getInstance(getBaseContext()).setSettingUserArea( result.get("province").toString() + " " + result.get("city").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserZhiye(result.get("zhiye").toString()); PreferenceUtils.getInstance(getBaseContext()) .setSettingUserQianming(result.get("qianming").toString()); String huanxin_username = result.get("umd5").toString(); String huanxin_pwd = result.get("pwd").toString(); Log.d("log huanxin_username:" + huanxin_username + "|huanxin_pwd:" + huanxin_pwd); //?? login(huanxin_username, huanxin_pwd); /*??? progressShow = false; pd.dismiss();*/ } else { Toast.makeText(getApplicationContext(), lm.get("message").toString(), 0).show(); } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, Object errorResponse) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "?,?", 0).show(); } @Override protected Object parseResponse(String rawJsonData, boolean isFailure) throws Throwable { // TODO Auto-generated method stub return null; } }); } } /** * * * @param view */ public void register(View view) { startActivityForResult(new Intent(this, RegisterActivity.class), 0); } @Override protected void onResume() { super.onResume(); /*if (DemoApplication.getInstance().getUserName() != null) { usernameEditText.setText(DemoApplication.getInstance().getUserName()); }*/ if (DemoApplication.getInstance().getUser() != null) { //usernameEditText.setText(DemoApplication.getInstance().getUser()); } } /** * hearder?header??ABCD...???? * * @param username * @param user */ protected void setUserHearder(String username, User user) { String headerName = null; if (!TextUtils.isEmpty(user.getNick())) { headerName = user.getNick(); } else { headerName = user.getUsername(); } if (username.equals(Constant.NEW_FRIENDS_USERNAME)) { user.setHeader(""); } else if (Character.isDigit(headerName.charAt(0))) { user.setHeader("#"); } else { user.setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1)).get(0).target.substring(0, 1) .toUpperCase()); char header = user.getHeader().toLowerCase().charAt(0); if (header < 'a' || header > 'z') { user.setHeader("#"); } } } }