PageRenderTime 768ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/uprint/android_pack/cloudprint4androidmanager/activity/ImproveInfoActivity.java

https://gitlab.com/zhangxiaang/CloudPrint4AndroidManager
Java | 441 lines | 397 code | 33 blank | 11 comment | 72 complexity | e05ddb4abe84cc74a485a0d3620b7b45 MD5 | raw file
  1. package com.uprint.android_pack.cloudprint4androidmanager.activity;
  2. import android.content.DialogInterface;
  3. import android.content.Intent;
  4. import android.graphics.Bitmap;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.provider.MediaStore;
  8. import android.support.v7.app.AlertDialog;
  9. import android.support.v7.widget.Toolbar;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.AdapterView;
  15. import android.widget.ArrayAdapter;
  16. import android.widget.ImageView;
  17. import android.widget.RadioGroup;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20. import com.alibaba.fastjson.JSON;
  21. import com.alibaba.fastjson.JSONArray;
  22. import com.alibaba.fastjson.JSONObject;
  23. import com.rengwuxian.materialedittext.MaterialAutoCompleteTextView;
  24. import com.rengwuxian.materialedittext.MaterialEditText;
  25. import com.uprint.android_pack.cloudprint4androidmanager.CPBaseActivity;
  26. import com.uprint.android_pack.cloudprint4androidmanager.R;
  27. import com.uprint.android_pack.cloudprint4androidmanager.adapter.SimpleBuildingAdapter;
  28. import com.uprint.android_pack.cloudprint4androidmanager.dataType.SchoolInfo;
  29. import com.uprint.android_pack.cloudprint4androidmanager.network.ICallBack;
  30. import com.uprint.android_pack.cloudprint4androidmanager.network.NetValue;
  31. import com.uprint.android_pack.cloudprint4androidmanager.network.biz.CommonGetBiz;
  32. import com.uprint.android_pack.cloudprint4androidmanager.network.biz.CommonPostBiz;
  33. import com.uprint.android_pack.cloudprint4androidmanager.utils.BitmapUtils;
  34. import com.uprint.android_pack.cloudprint4androidmanager.utils.EmptyViewUtil;
  35. import com.uprint.android_pack.cloudprint4androidmanager.utils.RegexUtils;
  36. import com.uprint.android_pack.cloudprint4androidmanager.utils.SharedPreferenceUtil;
  37. import java.io.IOException;
  38. import java.lang.ref.WeakReference;
  39. import java.util.ArrayList;
  40. import java.util.HashMap;
  41. import java.util.List;
  42. import java.util.Map;
  43. import butterknife.Bind;
  44. import butterknife.ButterKnife;
  45. /**
  46. * Created by zhangxiaang on 15/10/16.
  47. */
  48. public class ImproveInfoActivity extends CPBaseActivity {
  49. public static final String TAG = "ImproveInfoActivity";
  50. private TextView title;
  51. @Bind(R.id.nickname)
  52. MaterialEditText name;
  53. @Bind(R.id.qqaccount)
  54. MaterialEditText qq;
  55. @Bind(R.id.school)
  56. MaterialAutoCompleteTextView school;
  57. @Bind(R.id.email)
  58. MaterialEditText email;
  59. @Bind(R.id.zhifubao)
  60. MaterialEditText zhifubao;
  61. @Bind(R.id.graduatedYear)
  62. MaterialEditText graduatedYear;
  63. @Bind(R.id.dormname)
  64. MaterialEditText dromname;
  65. @Bind(R.id.studentCard)
  66. ImageView studentCardImage;
  67. @Bind(R.id.select_shop)
  68. MaterialEditText buildings;
  69. @Bind(R.id.sendBuilding)
  70. MaterialEditText sendBuilding;
  71. @Bind(R.id.radioGroup)
  72. RadioGroup group;
  73. @Bind(R.id.idnum)
  74. MaterialEditText idnum;
  75. private Bitmap studentCardBtmp;
  76. private String encode_studentCardBtmp;
  77. private int is_girl;//0->boy 1->girl default ->boy
  78. private int is_printer = 1;//0->printer 1->sender default->sender
  79. private int single_choice;
  80. private Map params_map;
  81. private ArrayList<SchoolInfo> schoolInfos;
  82. private ArrayList<SchoolInfo.DormInfo> infos;
  83. private boolean school_select;
  84. private SimpleBuildingAdapter adapter1;
  85. @Override
  86. protected void onCreate(Bundle savedInstanceState) {
  87. super.onCreate(savedInstanceState);
  88. setContentView(R.layout.improveinfo);
  89. ButterKnife.bind(this);
  90. init();
  91. }
  92. private void init() {
  93. mToolbar = (Toolbar) findViewById(R.id.toolbar);
  94. mToolbar.setNavigationIcon(R.drawable.goback);
  95. title = (TextView) mToolbar.findViewById(R.id.toolbar_title);
  96. title.setText("完善楼长信息");
  97. setSupportActionBar(mToolbar);
  98. if (getSupportActionBar() != null) {
  99. getSupportActionBar().setHomeButtonEnabled(false);
  100. getSupportActionBar().setDisplayShowHomeEnabled(false);
  101. getSupportActionBar().setDisplayShowTitleEnabled(false);
  102. }
  103. params_map = new HashMap<>();
  104. group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  105. @Override
  106. public void onCheckedChanged(RadioGroup group, int checkedId) {
  107. if (checkedId == R.id.boy) {
  108. is_girl = 0;
  109. } else if (checkedId == R.id.girl) {
  110. is_girl = 1;
  111. }
  112. }
  113. });
  114. school.addTextChangedListener(new TextWatcher() {
  115. WeakReference<ImproveInfoActivity> ref = new WeakReference<>(ImproveInfoActivity.this);
  116. @Override
  117. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  118. school.dismissDropDown();
  119. }
  120. @Override
  121. public void onTextChanged(CharSequence s, int start, int before, int count) {
  122. school.dismissDropDown();
  123. }
  124. @Override
  125. public void afterTextChanged(Editable s) {
  126. String keyWord = s.toString();
  127. if (keyWord != null && keyWord.trim().length() != 0 && !school_select) {
  128. Map<String, String> params = new HashMap<>();
  129. params.put("keyword", keyWord);
  130. CommonGetBiz biz = new CommonGetBiz(ref.get());
  131. biz.request(new ICallBack() {
  132. @Override
  133. public void displayResult(int status, Object... params) {
  134. Log.e(TAG, params[0].toString());
  135. List<String> school_name = new ArrayList<>();
  136. schoolInfos = new ArrayList<>();
  137. JSONObject result = (JSONObject) params[0];
  138. if (result.getInteger("error") != 0) {
  139. Toast.makeText(ImproveInfoActivity.this, result.getString("msg"), Toast.LENGTH_SHORT).show();
  140. return;
  141. }
  142. JSONArray array = result.getJSONArray("results");
  143. if (array.size() == 0) {
  144. Toast.makeText(ImproveInfoActivity.this, "抱歉,您的学校暂时未收录", Toast.LENGTH_SHORT).show();
  145. return;
  146. }
  147. for (int i = 0; i < array.size(); i++) {
  148. SchoolInfo school = JSON.parseObject(array.getJSONObject(i).toJSONString(), SchoolInfo.class);
  149. schoolInfos.add(school);
  150. }
  151. for (int i = 0; i < schoolInfos.size(); i++) {
  152. school_name.add(schoolInfos.get(i).getName());
  153. }
  154. ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.school_item_layout, school_name);
  155. school.setAdapter(adapter);
  156. school.showDropDown();
  157. }
  158. }, NetValue.GET_SCHOOL(), params);
  159. }
  160. school_select = false;
  161. }
  162. });
  163. school.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  164. @Override
  165. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  166. infos = schoolInfos.get(position).getBuildings();
  167. if (infos != null && infos.size() != 0) {
  168. HashMap schoolInfo = new HashMap();
  169. schoolInfo.put("ID", schoolInfos.get(position).getId());
  170. schoolInfo.put("name", schoolInfos.get(position).getName());
  171. params_map.put("school", schoolInfo);
  172. school_select = true;
  173. adapter1 = new SimpleBuildingAdapter(ImproveInfoActivity.this, infos);
  174. } else {
  175. AlertDialog.Builder builder = new AlertDialog.Builder(ImproveInfoActivity.this);
  176. builder.setMessage("您的学校尚无录入相应的宿舍,请联系我们的客服").setPositiveButton("确认", new DialogInterface.OnClickListener() {
  177. @Override
  178. public void onClick(DialogInterface dialog, int which) {
  179. dialog.dismiss();
  180. }
  181. }).create().show();
  182. }
  183. }
  184. });
  185. buildings.setOnClickListener(new View.OnClickListener() {
  186. HashMap building = new HashMap();
  187. @Override
  188. public void onClick(View v) {
  189. if (infos == null) {
  190. Toast.makeText(ImproveInfoActivity.this, "请先选择学校", Toast.LENGTH_SHORT).show();
  191. return;
  192. }
  193. AlertDialog.Builder builder = new AlertDialog.Builder(ImproveInfoActivity.this);
  194. builder.setSingleChoiceItems(adapter1, -1, new DialogInterface.OnClickListener() {
  195. @Override
  196. public void onClick(DialogInterface dialog, int which) {
  197. single_choice = which;
  198. }
  199. }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
  200. @Override
  201. public void onClick(DialogInterface dialog, int which) {
  202. building.put("ID", infos.get(single_choice).getId());
  203. building.put("name", infos.get(single_choice).getName());
  204. building.put("status", infos.get(single_choice).getStatus());
  205. params_map.put("building", building);
  206. buildings.setHint(infos.get(single_choice).getName());
  207. }
  208. }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
  209. @Override
  210. public void onClick(DialogInterface dialog, int which) {
  211. dialog.dismiss();
  212. }
  213. }).create().show();
  214. }
  215. });
  216. sendBuilding.setOnClickListener(new View.OnClickListener() {
  217. @Override
  218. public void onClick(View v) {
  219. if (infos == null) {
  220. Toast.makeText(ImproveInfoActivity.this, "请先选择学校", Toast.LENGTH_SHORT).show();
  221. return;
  222. }
  223. final ArrayList<SchoolInfo.DormInfo> list = new ArrayList<>(20);
  224. CharSequence[] names = new CharSequence[infos.size()];
  225. for (int i = 0; i < infos.size(); i++) {
  226. names[i] = infos.get(i).getName();
  227. }
  228. final AlertDialog.Builder builder = new AlertDialog.Builder(ImproveInfoActivity.this);
  229. builder.setMultiChoiceItems(names, null, new DialogInterface.OnMultiChoiceClickListener() {
  230. @Override
  231. public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  232. if (isChecked) {
  233. if (list.size() >= 20) {
  234. int index = infos.indexOf(list.get(0));
  235. ((AlertDialog) dialog).getListView().setItemChecked(index, false);
  236. list.remove(0);
  237. }
  238. list.add(infos.get(which));
  239. } else {
  240. list.remove(infos.get(which));
  241. }
  242. }
  243. }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
  244. @Override
  245. public void onClick(DialogInterface dialog, int which) {
  246. ArrayList<HashMap> send_building = new ArrayList<>();
  247. for (int i = 0; i < list.size(); i++) {
  248. HashMap map = new HashMap<>();
  249. map.put("ID", list.get(i).getId());
  250. map.put("status", list.get(i).getStatus());
  251. map.put("name", list.get(i).getName());
  252. send_building.add(map);
  253. }
  254. StringBuilder builder = new StringBuilder();
  255. for (int i = 0; i < list.size(); i++) {
  256. builder.append(list.get(i).getName() + ",");
  257. }
  258. sendBuilding.setHint(getBuildingListName(list));
  259. params_map.put("manage_buildings", send_building);
  260. }
  261. }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
  262. @Override
  263. public void onClick(DialogInterface dialog, int which) {
  264. dialog.dismiss();
  265. }
  266. }).create().show();
  267. }
  268. });
  269. }
  270. private String getBuildingListName(ArrayList<SchoolInfo.DormInfo> list) {
  271. StringBuilder builder = new StringBuilder();
  272. for (SchoolInfo.DormInfo info : list) {
  273. builder.append(info.getName() + ",");
  274. }
  275. int length = builder.length();
  276. builder.deleteCharAt(length - 1);
  277. return builder.toString();
  278. }
  279. public void route1(View view) {
  280. int resId = view.getId();
  281. switch (resId) {
  282. case R.id.improveBtn:
  283. if (checkoutAllInfo()) {
  284. postAllInfo();
  285. }
  286. break;
  287. case R.id.studentcardgroup:
  288. Intent intent2 = new Intent();
  289. intent2.setType("image/*");
  290. intent2.setAction(Intent.ACTION_GET_CONTENT);
  291. startActivityForResult(intent2, 2);
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. /**
  298. * why pick the image cost so much memory
  299. * how to get the image effect?
  300. *
  301. * @param requestCode
  302. * @param resultCode
  303. * @param data
  304. */
  305. @Override
  306. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  307. if ((requestCode == 2) && (resultCode == RESULT_OK)) {
  308. Uri uri = data.getData();
  309. try {
  310. studentCardBtmp = BitmapUtils.createScaledBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri), 500, 500);
  311. } catch (IOException e) {
  312. e.printStackTrace();
  313. }
  314. studentCardImage.setImageBitmap(studentCardBtmp);
  315. }
  316. super.onActivityResult(requestCode, resultCode, data);
  317. }
  318. public boolean checkoutAllInfo() {
  319. name.validateWith(RegexUtils.name());
  320. qq.validateWith(RegexUtils.qq());
  321. email.validateWith(RegexUtils.email());
  322. zhifubao.validateWith(RegexUtils.zhifubao());
  323. graduatedYear.validateWith(RegexUtils.year());
  324. idnum.validateWith(RegexUtils.idnum());
  325. if (name.getText().toString().trim().length() == 0 || !name.validate()) {
  326. Toast.makeText(this, "2-5个中文", Toast.LENGTH_SHORT).show();
  327. return false;
  328. }
  329. if (school.getText().toString().trim().length() == 0) {
  330. Toast.makeText(this, "学校信息未填", Toast.LENGTH_SHORT).show();
  331. return false;
  332. }
  333. if (graduatedYear.getText().toString().trim().length() == 0 || !graduatedYear.validate()) {
  334. Toast.makeText(this, "毕业时间格式错误", Toast.LENGTH_SHORT).show();
  335. return false;
  336. }
  337. if (qq.getText().toString().trim().length() == 0 || !qq.validate()) {
  338. Toast.makeText(this, "qq格式错误", Toast.LENGTH_SHORT).show();
  339. return false;
  340. }
  341. if (idnum.getText().toString().trim().length() == 0 || !idnum.validate()) {
  342. Toast.makeText(this, "身份证号格式错误", Toast.LENGTH_SHORT).show();
  343. return false;
  344. }
  345. if (dromname.getText().toString().trim().length() == 0) {
  346. Toast.makeText(this, "宿舍房号未填", Toast.LENGTH_SHORT).show();
  347. return false;
  348. }
  349. if (email.getText().toString().trim().length() == 0 || !email.validate()) {
  350. Toast.makeText(this, "邮件格式错误", Toast.LENGTH_SHORT).show();
  351. return false;
  352. }
  353. if (zhifubao.getText().toString().trim().length() == 0 || !zhifubao.validate()) {
  354. Toast.makeText(this, "支付宝账号格式错误", Toast.LENGTH_SHORT).show();
  355. return false;
  356. }
  357. if (studentCardBtmp == null) {
  358. Toast.makeText(this, "学生证证件页未上传", Toast.LENGTH_SHORT).show();
  359. return false;
  360. }
  361. if (params_map.get("building") == null || params_map.get("building").toString().trim().length() == 0) {
  362. Toast.makeText(this, "未选择宿舍", Toast.LENGTH_SHORT).show();
  363. return false;
  364. }
  365. return true;
  366. }
  367. public void postAllInfo() {
  368. encode_studentCardBtmp = BitmapUtils.encode_bitmap(studentCardBtmp);
  369. SharedPreferenceUtil.checkToken(this);
  370. params_map.put("token", SharedPreferenceUtil.get_Token(this));
  371. params_map.put("certificate", encode_studentCardBtmp);
  372. params_map.put("manager_name", name.getText().toString());
  373. params_map.put("qq", qq.getText().toString());
  374. params_map.put("sex", is_girl);
  375. params_map.put("manager_type", is_printer);
  376. params_map.put("email", email.getText().toString());
  377. params_map.put("graduate_year", graduatedYear.getText().toString());
  378. params_map.put("alipay_account", zhifubao.getText().toString());
  379. params_map.put("dorm", dromname.getText().toString());
  380. params_map.put("ID_number", idnum.getText().toString());
  381. CommonPostBiz biz = new CommonPostBiz(this);
  382. biz.request(new ICallBack() {
  383. @Override
  384. public void displayResult(int status, Object... params) {
  385. JSONObject result = (JSONObject) params[0];
  386. EmptyViewUtil.closeLoadingDialog();
  387. if (result.getInteger("error") == 0) {
  388. AlertDialog.Builder builder = new AlertDialog.Builder(ImproveInfoActivity.this);
  389. builder.setMessage(result.getString("msg")).setPositiveButton("确认", new DialogInterface.OnClickListener() {
  390. @Override
  391. public void onClick(DialogInterface dialog, int which) {
  392. finish();
  393. }
  394. }).setCancelable(false).create().show();
  395. }
  396. }
  397. }, NetValue.IMPROVE_INFO(), params_map);
  398. EmptyViewUtil.showLoadingDialog(this, "UpLoading...", false);
  399. }
  400. @Override
  401. protected void onDestroy() {
  402. if (studentCardBtmp != null) {
  403. studentCardBtmp.recycle();
  404. }
  405. ButterKnife.unbind(this);
  406. super.onDestroy();
  407. }
  408. }