Java tutorial
package com.newshiqi.yushi; import java.io.File; import java.util.HashMap; import java.util.Map; import com.mine.common.command.HttpDataRequest; import com.mine.common.command.HttpTagDispatch; import com.mine.common.command.HttpTagDispatch.HttpTag; import com.mine.common.constant.Constant; import com.mine.common.http.HttpEngine.HttpCode; import com.mine.common.http.request.HttpReqFactory; import com.mine.common.http.session.SessionData; import com.mine.common.model.BaseDataLoader; import com.mine.common.model.BaseReq; import com.mine.common.model.CacheDataLoader; import com.mine.common.utils.FileUtil; import com.mine.common.utils.ImageUtil; import com.mine.common.utils.ImgDownLoader; import com.mine.common.utils.ImgFileUtils; import com.mine.common.utils.MobileUtil; import com.mine.common.utils.StringUtil; import com.mine.common.utils.ImgDownLoader.onImageLoaderListener; import com.newshiqi.yushi.model.ProductInfo; import com.newshiqi.yushi.model.UploadInfo; import com.newshiqi.yushi.model.UserInfo; import com.newshiqi.yushi.model.UserInfoCom; import com.newshiqi.yushi.model.UserInfoTalent; import com.newshiqi.yushi.model.YSH; import com.newshiqi.yushi.utils.Util; import com.newshiqi.yushi.view.MyDialog; import com.newshiqi.yushi.view.MyDialog.EditCompleteListener; import com.newshiqi.yushi.R; import android.app.Dialog; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.view.Gravity; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class ProfileActivity extends BaseActivity { private TextView user_name; private ImageView user_icon; private Button btnLogout; private Dialog dialog; private Dialog progressDialog; private View bottomView; private UserInfo userInfo; private String memberId; private ImgDownLoader imgDownloader; public static final int RESULT_LOGOUT = 3; public static final int RESULT_UPDATE_INFO = 4; private static final String TEMP_FILE_NAME = Constant.CACHE_PICTURE_DOWNLOAD_PATH + "temp.png"; private static final String TEMP_LICENSE_FILE_NAME = Constant.CACHE_PICTURE_DOWNLOAD_PATH + "temp_license.png"; // private static final String[] userTypes = {getString(R.string.type_normal_user), // getString(R.string.type_pro_user), // getString(R.string.type_pro_user)}; private String[] userTypeValue = { "U", "T", "C" }; private String[] sexValue = { "1", "0", "-1" }; // private String[] sexs = {getString(R.string.sex_male), // getString(R.string.sex_female), // getString(R.string.sex_hiden)}; private String mobile; private String userType; private String name; private String age; private String addr; private String brief; private String qq; private File tempFile = null; private String uploadType; public static final String INIT_DATALOADER = "init_user_dataloader"; private CacheDataLoader initLoader = new CacheDataLoader(INIT_DATALOADER, this); public static final String UPDATE_DATALOADER = "update_user_dataloader"; private CacheDataLoader updateLoader = new CacheDataLoader(UPDATE_DATALOADER, this); private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_profile); setPageTitle(getString(R.string.title_profile)); imgDownloader = new ImgDownLoader(this); user_name = (TextView) findViewById(R.id.user_name); user_icon = (ImageView) findViewById(R.id.user_icon); btnLogout = (Button) findViewById(R.id.p_logout); bottomView = findViewById(R.id.bottom_layout); memberId = getIntent().getStringExtra(YSH.KEY_MEMBER_ID); progressDialog = showProgressDialog(true, true, null); initRequest(memberId); btnLogout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // logout logoutRequest(memberId); SessionData.get().clear(); setResult(RESULT_LOGOUT); finish(); } }); } // @Override // public boolean onKeyDown(int keyCode, KeyEvent event) { // if (keyCode == KeyEvent.KEYCODE_BACK) { // set // this.finish(); // return true; // } // return super.onKeyDown(keyCode, event); // } private void logoutRequest(String memberId) { BaseReq req = new BaseReq(); req.setM("User"); req.setA("Logout"); Map<String, String> params = new HashMap<String, String>(); params.put("memberId", memberId); req.setParams(params); HttpDataRequest request = HttpReqFactory.getInstance().post2(req); CacheDataLoader logoutLoader = new CacheDataLoader("logout", this); logoutLoader.loadData(BaseDataLoader.LOADER_FROM_SRV, request); } private void initRequest(String memberId) { BaseReq req = new BaseReq(); req.setM("User"); req.setA("GetUser"); Map<String, String> params = new HashMap<String, String>(); params.put("memberId", memberId); params.put("identifier", MobileUtil.getImei()); req.setParams(params); HttpDataRequest request = HttpReqFactory.getInstance().post2(req); initLoader.loadData(BaseDataLoader.LOADER_FROM_SRV, request); } private void initView() { userInfo = MainApplication.getInstance().getUserInfo(); if (userInfo == null) { userInfo = new UserInfo(); userInfo.setMemberId(StringUtil.getSavedValue(SessionData.KEY_USERID)); userInfo.setUserType(StringUtil.getSavedValue(SessionData.KEY_USER_TYPE)); userInfo.setNickname(StringUtil.getSavedValue(SessionData.KEY_NICKNAME)); userInfo.setMobile(StringUtil.getSavedValue(SessionData.KEY_PHONE)); userInfo.setAvatar(StringUtil.getSavedValue(SessionData.KEY_USER_AVATAR)); } userType = userInfo.getUserType(); Bitmap bitmap = null; String avatar = Util.getImageUrl(userInfo.getAvatar()); /*if ("C".equals(userType) && userInfo.getCompany() != null) { if (!TextUtils.isEmpty(userInfo.getCompany().getLogo())) { avatar = Util.getImageUrl(userInfo.getCompany().getLogo()); } }*/ if (!TextUtils.isEmpty(avatar)) { bitmap = imgDownloader.downloadImage(avatar, new onImageLoaderListener() { @Override public void onImageLoader(Bitmap bitmap, String url) { if (bitmap != null) { bitmap = ImageUtil.resizeBitmapSmooth(bitmap, Util.dip2px(ProfileActivity.this, 60), Util.dip2px(ProfileActivity.this, 60)); bitmap = ImageUtil.getRoundedCornerBitmap(bitmap, Util.dip2px(ProfileActivity.this, 27)); user_icon.setImageBitmap(bitmap); } } }); } if (bitmap != null) { bitmap = ImageUtil.resizeBitmapSmooth(bitmap, Util.dip2px(this, 60), Util.dip2px(this, 60)); bitmap = ImageUtil.getRoundedCornerBitmap(bitmap, Util.dip2px(this, 27)); user_icon.setImageBitmap(bitmap); } else { user_icon.setImageResource(R.drawable.user_logined); } user_icon.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { uploadType = "avatar"; FileUtil.deleteFile(TEMP_FILE_NAME); try { tempFile = FileUtil.makeDIRAndCreateFile(TEMP_FILE_NAME); } catch (Exception e) { } Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//android i.putExtra("crop", "true"); // i.putExtra("aspectX", 1); i.putExtra("output", Uri.fromFile(tempFile)); i.putExtra("outputFormat", "JPEG"); i.putExtra("aspectX", 1); //? ? X ? i.putExtra("aspectY", 1); i.putExtra("outputX", 350); i.putExtra("outputY", 350); startActivityForResult(i, 2); } }); // // if ("T".equals(userType)) { // if (userInfo.getTalent() != null) { // name = userInfo.getTalent().getName(); // } // } else if ("C".equals(userType)) { // if (userInfo.getCompany() != null) { // name = userInfo.getCompany().getName(); // } // } else { name = userInfo.getNickname(); // } if (TextUtils.isEmpty(name)) { initItem(R.id.p_username, getText(R.string.p_username), getString(R.string.not_defined), true); } else { initItem(R.id.p_username, getText(R.string.p_username), name, true); } mobile = userInfo.getMobile(); if (TextUtils.isEmpty(mobile)) { mobile = userInfo.getUsername(); } initItem(R.id.p_usertype, getText(R.string.p_usertype), getUserTypeDesp(userType), true); initItem(R.id.p_phone, getText(R.string.p_phonenum), mobile, true); initItem(R.id.p_modify_pwd, getText(R.string.p_modify_pwd), "", false); initViewByUserType(); } private void initViewByUserType() { LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) bottomView.getLayoutParams(); if ("T".equals(userType)) { initItem(R.id.p_sex, getText(R.string.p_sex), getSexDesp(userInfo.getSex()), true); initItem(R.id.p_age, getText(R.string.p_age), userInfo.getAge(), true); initItem(R.id.p_qq, getText(R.string.p_qq), userInfo.getQq(), true); UserInfoTalent t = userInfo.getTalent(); initItem(R.id.p_addr, getText(R.string.p_addr), userInfo.getAddress(), true); initItem(R.id.p_intro, getText(R.string.p_t_intro), t != null ? t.getDescription() : "", true); findViewById(R.id.p_license).setVisibility(View.GONE); lp.topMargin = Util.dip2px(this, 30); lp.bottomMargin = Util.dip2px(this, 10); } else if ("C".equals(userType)) { initItem(R.id.p_license, getText(R.string.p_license), "", true); initItem(R.id.p_qq, getText(R.string.p_qq), userInfo.getQq(), true); UserInfoCom c = userInfo.getCompany(); initItem(R.id.p_addr, getText(R.string.p_addr), userInfo.getAddress(), true); initItem(R.id.p_intro, getText(R.string.p_c_intro), c != null ? c.getDescription() : "", true); findViewById(R.id.p_age).setVisibility(View.GONE); findViewById(R.id.p_sex).setVisibility(View.GONE); lp.topMargin = Util.dip2px(this, 40); lp.bottomMargin = Util.dip2px(this, 10); } else { initItem(R.id.p_sex, getText(R.string.p_sex), getSexDesp(userInfo.getSex()), true); findViewById(R.id.p_age).setVisibility(View.GONE); findViewById(R.id.p_qq).setVisibility(View.GONE); findViewById(R.id.p_addr).setVisibility(View.GONE); findViewById(R.id.p_intro).setVisibility(View.GONE); findViewById(R.id.p_license).setVisibility(View.GONE); lp.topMargin = Util.dip2px(this, 120); } bottomView.requestLayout(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (resultCode) { case RESULT_OK: { if (tempFile.exists()) { progressDialog = showProgressDialog(true, true, null); uploadIcon(); } } break; case RESULT_CANCELED:// ? break; } } private void uploadIcon() { Thread thread = new Thread(new Runnable() { @Override public void run() { String result = Util.uploadFile(tempFile.getAbsolutePath()); if (!TextUtils.isEmpty(result)) { Object obj = HttpTagDispatch.dispatch(HttpTag.UPLOAD_IMG, result); if (obj != null && obj instanceof UploadInfo) { UploadInfo info = (UploadInfo) obj; Map<String, String> p = new HashMap<String, String>(); p.put(uploadType, info.getPath()); updateUserRequest(p); return; } } mHandler.post(new Runnable() { @Override public void run() { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } Toast.makeText(ProfileActivity.this, "", Toast.LENGTH_SHORT).show(); } }); } }); thread.start(); } private void updateUserRequest(Map<String, String> p) { BaseReq req = new BaseReq(); req.setM("User"); req.setA("Update"); String userType = userInfo.getUserType(); Map<String, String> params = new HashMap<String, String>(); params.put("memberId", userInfo.getMemberId()); params.put("userType", userType); if (p != null && p.size() > 0) { params.putAll(p); } req.setParams(params); HttpDataRequest request = HttpReqFactory.getInstance().post2(req); updateLoader.loadData(BaseDataLoader.LOADER_FROM_SRV, request); } @Override public void onQueryComplete(BaseDataLoader dataloader, boolean cache) { super.onQueryComplete(dataloader, cache); Object data = dataloader.getData(); if (data != null) { if (UPDATE_DATALOADER.equals(dataloader.getTag())) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } Object obj = HttpTagDispatch.dispatch(HttpTag.UPDATE_USER, data.toString()); if (obj != null) { UserInfo userInfo = (UserInfo) obj; MainApplication.getInstance().setUserInfo(userInfo); if (!TextUtils.isEmpty(userInfo.getMemberId())) { SessionData.get().setVal(SessionData.KEY_USERID, userInfo.getMemberId()); SessionData.get().setVal(SessionData.KEY_USER_TYPE, userInfo.getUserType()); SessionData.get().setVal(SessionData.KEY_NICKNAME, userInfo.getNickname()); SessionData.get().setVal(SessionData.KEY_PHONE, userInfo.getUsername()); SessionData.get().setVal(SessionData.KEY_USER_AVATAR, userInfo.getAvatar()); } initView(); refreshMinePage(); } Toast.makeText(ProfileActivity.this, "??", Toast.LENGTH_SHORT).show(); } else if (INIT_DATALOADER.equals(dataloader.getTag())) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } Object obj = HttpTagDispatch.dispatch(HttpTag.GET_USER, data.toString()); if (obj != null) { UserInfo userInfo = (UserInfo) obj; MainApplication.getInstance().setUserInfo(userInfo); if (!TextUtils.isEmpty(userInfo.getMemberId())) { SessionData.get().setVal(SessionData.KEY_USERID, userInfo.getMemberId()); SessionData.get().setVal(SessionData.KEY_USER_TYPE, userInfo.getUserType()); SessionData.get().setVal(SessionData.KEY_NICKNAME, userInfo.getNickname()); SessionData.get().setVal(SessionData.KEY_PHONE, userInfo.getUsername()); SessionData.get().setVal(SessionData.KEY_USER_AVATAR, userInfo.getAvatar()); } } initView(); } } else { initView(); } } @Override public void onQueryError(BaseDataLoader dataloader, HttpCode retCode, String buisCode, String errorMsg) { super.onQueryError(dataloader, retCode, buisCode, errorMsg); if (INIT_DATALOADER.equals(dataloader.getTag())) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } if (!TextUtils.isEmpty(errorMsg)) { Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show(); if (errorMsg.contains("?")) { SessionData.get().setVal(SessionData.KEY_USERID, ""); SessionData.get().setVal(SessionData.KEY_USER_TYPE, ""); } } else { Toast.makeText(this, "???", Toast.LENGTH_SHORT).show(); } finish(); refreshMinePage(); // initView(); } else if (UPDATE_DATALOADER.equals(dataloader.getTag())) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } if (!TextUtils.isEmpty(errorMsg)) { Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "??", Toast.LENGTH_SHORT).show(); } } } /** * ?? * */ private View initItem(int viewId, CharSequence label, String value, boolean showLine) { View view = findViewById(viewId); if (view != null) { view.setVisibility(View.VISIBLE); TextView labelView = (TextView) view.findViewById(R.id.label_tv); if (labelView != null) { labelView.setText(label); } TextView valueView = (TextView) view.findViewById(R.id.value_tv); if (valueView != null) { if (!TextUtils.isEmpty(value)) { if (value.length() > 11) { value = value.substring(0, 10) + ".."; } } valueView.setText(value); } ImageView lineView = (ImageView) view.findViewById(R.id.bottom_line); if (lineView != null) { if (showLine) { lineView.setVisibility(View.VISIBLE); } else { lineView.setVisibility(View.GONE); } } initOnClick(viewId); } return view; } // public void initOnClick(int viewId) { View viewSet = findViewById(viewId); if (viewSet == null) { return; } switch (viewId) { case R.id.p_username: viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { MyDialog.showDialogEditText(ProfileActivity.this, getString(R.string.modify_title), name != null ? name : "", new MyDialog.EditCompleteListener() { @Override public void onComplete(String value) { if (!TextUtils.isEmpty(value) && !value.equals(name)) { Map<String, String> p = new HashMap<String, String>(); if ("T".equals(userType)) { p.put("nickname", value); } else if ("C".equals(userType)) { p.put("nickname", value); } else { p.put("nickname", value); } progressDialog = showProgressDialog(true, true, null); updateUserRequest(p); } } }); } }); break; case R.id.p_usertype: viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { final String[] userTypes = { getString(R.string.type_normal_user), getString(R.string.type_pro_user), getString(R.string.type_company1) }; dialog = MyDialog.showDialogList(ProfileActivity.this, getString(R.string.modify_title), userTypes, new MyDialog.ItemSelectedListener() { @Override public void onItemSelected(int position) { dialog.dismiss(); if (!userTypeValue[position].equals(userInfo.getUserType())) { userInfo.setUserType(userTypeValue[position]); Map<String, String> p = new HashMap<String, String>(); progressDialog = showProgressDialog(true, true, null); updateUserRequest(p); } // TODO // initItem(R.id.p_usertype, getText(R.string.p_usertype), // userTypes[position], true); } }); } }); break; case R.id.p_age: viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { age = userInfo.getAge(); MyDialog.showDialogEditText(ProfileActivity.this, getString(R.string.modify_title), age != null ? age : "", MyDialog.INPUT_TYPE_NUMBER, new MyDialog.EditCompleteListener() { @Override public void onComplete(String value) { if (!TextUtils.isEmpty(value) && !value.equals(age)) { Map<String, String> p = new HashMap<String, String>(); p.put("age", value); // TODO progressDialog = showProgressDialog(true, true, null); updateUserRequest(p); } } }); } }); break; case R.id.p_qq: viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { qq = userInfo.getQq(); MyDialog.showDialogEditText(ProfileActivity.this, getString(R.string.modify_title), qq != null ? qq : "", MyDialog.INPUT_TYPE_NUMBER, new MyDialog.EditCompleteListener() { @Override public void onComplete(String value) { if (!TextUtils.isEmpty(value) && !value.equals(qq)) { Map<String, String> p = new HashMap<String, String>(); p.put("qq", value); progressDialog = showProgressDialog(true, true, null); updateUserRequest(p); } } }); } }); break; case R.id.p_addr: viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { /*if ("T".equals(userType)) { UserInfoTalent t = userInfo.getTalent(); if (t != null) { addr = t.getMapPoint(); } } else if ("C".equals(userType)) { UserInfoCom c = userInfo.getCompany(); if (c != null) { addr = c.getMapPoint(); } }*/ addr = userInfo.getAddress(); MyDialog.showDialogEditText(ProfileActivity.this, getString(R.string.modify_title), addr != null ? addr : "", new MyDialog.EditCompleteListener() { @Override public void onComplete(String value) { if (!TextUtils.isEmpty(value) && !value.equals(addr)) { Map<String, String> p = new HashMap<String, String>(); if ("T".equals(userType)) { p.put("address", value); } else if ("C".equals(userType)) { p.put("address", value); } progressDialog = showProgressDialog(true, true, null); updateUserRequest(p); } } }); } }); break; case R.id.p_intro: viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { if ("T".equals(userType)) { UserInfoTalent t = userInfo.getTalent(); if (t != null) { brief = t.getDescription(); } } else if ("C".equals(userType)) { UserInfoCom c = userInfo.getCompany(); if (c != null) { brief = c.getDescription(); } } MyDialog.showDialogEditText(ProfileActivity.this, getString(R.string.modify_title), brief != null ? brief : "", MyDialog.INPUT_TYPE_TEXTAREA, new MyDialog.EditCompleteListener() { @Override public void onComplete(String value) { if (!TextUtils.isEmpty(value) && !value.equals(brief)) { Map<String, String> p = new HashMap<String, String>(); if ("T".equals(userType)) { p.put("description", value); } else if ("C".equals(userType)) { p.put("description", value); } progressDialog = showProgressDialog(true, true, null); updateUserRequest(p); } } }); } }); break; case R.id.p_license: viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { uploadType = "companyLicenses"; FileUtil.deleteFile(TEMP_LICENSE_FILE_NAME); try { tempFile = FileUtil.makeDIRAndCreateFile(TEMP_LICENSE_FILE_NAME); } catch (Exception e) { } Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//android i.putExtra("crop", "true"); // i.putExtra("aspectX", 1); i.putExtra("output", Uri.fromFile(tempFile)); i.putExtra("outputFormat", "JPEG"); i.putExtra("aspectX", 1); //? ? X ? i.putExtra("aspectY", 1); i.putExtra("outputX", 350); i.putExtra("outputY", 350); startActivityForResult(i, 3); } }); break; case R.id.p_sex: viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { final String[] sexs = { getString(R.string.sex_male), getString(R.string.sex_female), getString(R.string.sex_hiden) }; dialog = MyDialog.showDialogList(ProfileActivity.this, getString(R.string.modify_title), sexs, new MyDialog.ItemSelectedListener() { @Override public void onItemSelected(int position) { dialog.dismiss(); if (!sexValue[position].equals(userInfo.getSex())) { Map<String, String> p = new HashMap<String, String>(); p.put("sex", sexValue[position]); progressDialog = showProgressDialog(true, true, null); updateUserRequest(p); } } }); } }); break; case R.id.p_phone: /*viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { MyDialog.showDialogEditText(ProfileActivity.this, getString(R.string.modify_title), mobile != null ? mobile : "", new MyDialog.EditCompleteListener() { @Override public void onComplete(String value) { if (!TextUtils.isEmpty(value) && !value.equals(mobile)) { Map<String, String> p = new HashMap<String, String>(); p.put("mobile", value); progressDialog = showProgressDialog(true, true, null); updateUserRequest(p); } } }); } });*/ break; case R.id.p_modify_pwd: viewSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(ProfileActivity.this, ModifyPassword.class); intent.putExtra(YSH.KEY_MEMBER_ID, memberId); startActivity(intent); } }); break; default: break; } } private String getUserTypeDesp(String userType) { if ("U".equals(userType)) { return getString(R.string.type_normal_user); } else if ("T".equals(userType)) { return getString(R.string.type_pro_user); } else if ("C".equals(userType)) { return getString(R.string.type_company1); } return userType; } private String getSexDesp(String sex) { if (sexValue[0].equals(sex)) { return getString(R.string.sex_male); } else if (sexValue[1].equals(sex)) { return getString(R.string.sex_female); } else if (sexValue[2].equals(sex)) { return getString(R.string.sex_hiden); } return sex; } }