ResultActivity.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package com.usai.apex;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6. import com.usai.util.Network;
  7. import com.usai.util.dbUtil;
  8. import android.R.bool;
  9. import android.R.integer;
  10. import android.os.AsyncTask;
  11. import android.os.Build;
  12. import android.os.Bundle;
  13. import android.animation.Animator;
  14. import android.animation.AnimatorListenerAdapter;
  15. import android.app.Activity;
  16. import android.content.Intent;
  17. import android.database.Cursor;
  18. import android.database.sqlite.SQLiteDatabase;
  19. import android.util.Log;
  20. import android.view.Gravity;
  21. import android.view.Menu;
  22. import android.view.MenuItem;
  23. import android.view.View;
  24. import android.widget.Button;
  25. import android.widget.TableRow;
  26. import android.widget.TextView;
  27. import android.widget.Toast;
  28. public class ResultActivity extends Activity
  29. {
  30. String user = null;
  31. String password = null;
  32. String function_name = null;
  33. private SearchTask m_task = null;
  34. Bundle searchParms = null;
  35. SearchResult searchresult = new SearchResult();
  36. HashMap<String, Integer> showfieldmap = new HashMap<String, Integer>();
  37. private TextView mStatusMessageView;
  38. private View mSearchFormView;
  39. private View mStatusView;
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState)
  42. {
  43. user = getIntent().getStringExtra("user");
  44. password = getIntent().getStringExtra("password");
  45. function_name = getIntent().getStringExtra("function_name");
  46. searchParms = getIntent().getBundleExtra("searchParms");
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.activity_result);
  49. mSearchFormView = findViewById(R.id.search_form);
  50. mStatusView = findViewById(R.id.status);
  51. mStatusMessageView = (TextView) findViewById(R.id.status_message);
  52. initTable();
  53. requestdata(true);
  54. }
  55. void initTable()
  56. {
  57. SQLiteDatabase db = dbUtil.OpenDB(this, null, false);
  58. Cursor cursor = db.query("fields_info",
  59. new String[] { "aname", "_id" }, "function_name='"
  60. + function_name + "' and user='" + user
  61. + "' and behavior=" + Network.BEHAVIOR_RESULT
  62. + " and show = 1", null, null, null,
  63. "priority , aname", null);
  64. TableRow headerRow = (TableRow) findViewById(R.id.tr_header);
  65. int pos = 0;
  66. showfieldmap.clear();
  67. headerRow.removeAllViews();
  68. while (cursor.moveToNext())
  69. {
  70. String aname = cursor.getString(0);
  71. Button btn = new Button(this);
  72. btn.setText(aname);
  73. headerRow.addView(btn);
  74. showfieldmap.put(aname, pos);
  75. }
  76. dbUtil.CloseCursor(cursor);
  77. dbUtil.CloseDB(db);
  78. }
  79. @Override
  80. public boolean onCreateOptionsMenu(Menu menu)
  81. {
  82. // Inflate the menu; this adds items to the action bar if it is present.
  83. getMenuInflater().inflate(R.menu.result, menu);
  84. return true;
  85. }
  86. @Override
  87. public boolean onOptionsItemSelected(MenuItem item)
  88. {
  89. switch (item.getItemId())
  90. {
  91. case R.id.action_custom_fields:
  92. Intent intent = new Intent();
  93. intent.setClass(this, CustomizeFieldsActivity.class);
  94. intent.putExtra("user", user);
  95. // intent.putExtra("password", password);
  96. intent.putExtra("function_name", function_name);
  97. intent.putExtra("behavior", Network.BEHAVIOR_RESULT);
  98. startActivity(intent);
  99. break;
  100. default:
  101. break;
  102. }
  103. return super.onOptionsItemSelected(item);
  104. }
  105. public void requestdata(boolean requestcount)
  106. {
  107. if (m_task != null)
  108. {
  109. return;
  110. }
  111. mStatusMessageView.setText(R.string.login_progress_signing_in);
  112. showProgress(true);
  113. m_task = new SearchTask();
  114. m_task.execute(requestcount);
  115. }
  116. private void showProgress(final boolean show)
  117. {
  118. // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  119. // for very easy animations. If available, use these APIs to fade-in
  120. // the progress spinner.
  121. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
  122. {
  123. int shortAnimTime = getResources().getInteger(
  124. android.R.integer.config_shortAnimTime);
  125. mStatusView.setVisibility(View.VISIBLE);
  126. mStatusView.animate().setDuration(shortAnimTime)
  127. .alpha(show ? 1 : 0)
  128. .setListener(new AnimatorListenerAdapter()
  129. {
  130. @Override
  131. public void onAnimationEnd(Animator animation)
  132. {
  133. mStatusView.setVisibility(show ? View.VISIBLE
  134. : View.INVISIBLE);
  135. }
  136. });
  137. mSearchFormView.setVisibility(View.VISIBLE);
  138. mSearchFormView.animate().setDuration(shortAnimTime)
  139. .alpha(show ? 0 : 1)
  140. .setListener(new AnimatorListenerAdapter()
  141. {
  142. @Override
  143. public void onAnimationEnd(Animator animation)
  144. {
  145. mSearchFormView.setVisibility(show ? View.INVISIBLE
  146. : View.VISIBLE);
  147. }
  148. });
  149. }
  150. else
  151. {
  152. // The ViewPropertyAnimator APIs are not available, so simply show
  153. // and hide the relevant UI components.
  154. mStatusView.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
  155. mSearchFormView.setVisibility(show ? View.INVISIBLE : View.VISIBLE);
  156. }
  157. }
  158. public class SearchTask extends AsyncTask<Boolean, Void, Boolean>
  159. {
  160. // int err_code = ERR_CODE_NONE;
  161. int errorcode;
  162. @Override
  163. protected Boolean doInBackground(Boolean... params)
  164. {
  165. if (!Network.NetworkIsAvailable())
  166. {
  167. errorcode = Network.RESULT_NET_NOTAVAILABLE;
  168. return false;
  169. }
  170. if (params[0])
  171. {
  172. int ret = Network.get_recordcount(user, password, searchParms);
  173. if (ret >= 0)
  174. {
  175. searchresult.put_totalcount(ret);
  176. }
  177. else
  178. {
  179. errorcode = ret;
  180. return false;
  181. }
  182. }
  183. String jstr = Network.get_records(user, password, searchParms);
  184. if (jstr == null || jstr.length() <= 0)
  185. {
  186. // Log.d(TAG, "json is wrong");
  187. errorcode = Network.RESULT_NET_ERROR;
  188. return false;
  189. }
  190. JSONObject jsobj;
  191. //
  192. // array = new JSONArray(json);
  193. try
  194. {
  195. jsobj = new JSONObject(jstr);
  196. if (searchresult.get_fieldscount() == 0)
  197. {
  198. JSONObject objfields = jsobj.getJSONObject("fields");
  199. searchresult.init_fields(objfields.toString());
  200. }
  201. JSONObject objrecords = jsobj.getJSONObject("records");
  202. searchresult.add_records(objrecords.toString());
  203. errorcode = Network.RESULT_TRUE;
  204. return true;
  205. }
  206. catch (JSONException e)
  207. {
  208. // TODO Auto-generated catch block
  209. e.printStackTrace();
  210. }
  211. errorcode = Network.RESULT_NET_ERROR;
  212. return false;
  213. }
  214. @Override
  215. protected void onPostExecute(final Boolean success)
  216. {
  217. Log.i("onPostExecute", "entry");
  218. m_task = null;
  219. showProgress(false);
  220. switch (errorcode)
  221. {
  222. case Network.RESULT_NET_NOTAVAILABLE:
  223. {
  224. Toast toast = Toast.makeText(getApplicationContext(),
  225. getText(R.string.msg_connection_none),
  226. Toast.LENGTH_LONG);
  227. toast.setGravity(Gravity.CENTER, 0, 0);
  228. toast.show();
  229. return;
  230. }
  231. case Network.RESULT_NET_ERROR:
  232. {
  233. Toast toast = Toast.makeText(getApplicationContext(),
  234. getText(R.string.msg_net_error), Toast.LENGTH_LONG);
  235. toast.setGravity(Gravity.CENTER, 0, 0);
  236. toast.show();
  237. return;
  238. }
  239. case Network.RESULT_ERROR:
  240. // case Network.RESULT_RESPONSE_NULL:
  241. {
  242. Toast toast = Toast.makeText(getApplicationContext(),
  243. getText(R.string.msg_net_resulterror),
  244. Toast.LENGTH_LONG);
  245. toast.setGravity(Gravity.CENTER, 0, 0);
  246. toast.show();
  247. return;
  248. }
  249. default:
  250. break;
  251. }
  252. if (success)
  253. {
  254. TextView tv_head = (TextView) findViewById(R.id.tv_head);
  255. tv_head.setText(searchresult.get_totalcount()+"");
  256. // SharedPreferences.Editor editor = RunOnce.edit();
  257. // editor.putBoolean("FirstRun"+globalUtil.getVerName(this),
  258. // false);
  259. // // Don't forget to commit your edits!!!
  260. // editor.commit();
  261. // Intent intent = new Intent();
  262. // intent.setClass(getActivity(), FunctionSelectActivity.class);
  263. // intent.putExtra("user", m_sUser);
  264. // intent.putExtra("password", m_sPassword);
  265. // startActivity(intent);
  266. // getActivity().finish();
  267. }
  268. else
  269. {
  270. // m_etPassword
  271. // .setError(getString(R.string.error_incorrect_password));
  272. // m_etPassword.requestFocus();
  273. }
  274. }
  275. @Override
  276. protected void onCancelled()
  277. {
  278. m_task = null;
  279. showProgress(false);
  280. }
  281. }
  282. private class SearchResult
  283. {
  284. int total_count = 0;
  285. // int count = 0;
  286. ArrayList<record> records = new ArrayList<record>();
  287. ArrayList<field> fields = new ArrayList<field>();
  288. public void init_fields(String jsonfields)
  289. {
  290. String TAG = "init_fields@ResultActivity.SearchResult";
  291. Log.d(TAG, jsonfields);
  292. }
  293. public void add_records(String records)
  294. {
  295. String TAG = "init_fields@ResultActivity.SearchResult";
  296. Log.d(TAG, records);
  297. }
  298. public int get_totalcount()
  299. {
  300. return total_count;
  301. }
  302. public void put_totalcount(int c)
  303. {
  304. total_count = c;
  305. }
  306. public int get_count()
  307. {
  308. return records.size();
  309. }
  310. public int get_fieldscount()
  311. {
  312. return fields.size();
  313. }
  314. private class record
  315. {
  316. ArrayList<Object> values;
  317. }
  318. private class field
  319. {
  320. String name = null;
  321. String aliasname = null;
  322. String type = null;
  323. public String get_name()
  324. {
  325. return name;
  326. }
  327. public String get_aliasname()
  328. {
  329. return aliasname;
  330. }
  331. public String get_type()
  332. {
  333. return type;
  334. }
  335. }
  336. }
  337. }