| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- package com.usai.apex;
- import java.util.ArrayList;
- import java.util.HashMap;
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.usai.util.Network;
- import com.usai.util.dbUtil;
- import android.R.bool;
- import android.R.integer;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.animation.Animator;
- import android.animation.AnimatorListenerAdapter;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ResultActivity extends Activity
- {
- String user = null;
- String password = null;
- String function_name = null;
- private SearchTask m_task = null;
- Bundle searchParms = null;
- SearchResult searchresult = new SearchResult();
- HashMap<String, Integer> showfieldmap = new HashMap<String, Integer>();
- private TextView mStatusMessageView;
- private View mSearchFormView;
- private View mStatusView;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- user = getIntent().getStringExtra("user");
- password = getIntent().getStringExtra("password");
- function_name = getIntent().getStringExtra("function_name");
- searchParms = getIntent().getBundleExtra("searchParms");
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_result);
- mSearchFormView = findViewById(R.id.search_form);
- mStatusView = findViewById(R.id.status);
- mStatusMessageView = (TextView) findViewById(R.id.status_message);
- initTable();
- requestdata(true);
- }
- void initTable()
- {
- SQLiteDatabase db = dbUtil.OpenDB(this, null, false);
- Cursor cursor = db.query("fields_info",
- new String[] { "aname", "_id" }, "function_name='"
- + function_name + "' and user='" + user
- + "' and behavior=" + Network.BEHAVIOR_RESULT
- + " and show = 1", null, null, null,
- "priority , aname", null);
- TableRow headerRow = (TableRow) findViewById(R.id.tr_header);
- int pos = 0;
- showfieldmap.clear();
- headerRow.removeAllViews();
- while (cursor.moveToNext())
- {
- String aname = cursor.getString(0);
- Button btn = new Button(this);
- btn.setText(aname);
- headerRow.addView(btn);
- showfieldmap.put(aname, pos);
- }
- dbUtil.CloseCursor(cursor);
- dbUtil.CloseDB(db);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.result, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- switch (item.getItemId())
- {
- case R.id.action_custom_fields:
- Intent intent = new Intent();
- intent.setClass(this, CustomizeFieldsActivity.class);
- intent.putExtra("user", user);
- // intent.putExtra("password", password);
- intent.putExtra("function_name", function_name);
- intent.putExtra("behavior", Network.BEHAVIOR_RESULT);
- startActivity(intent);
- break;
- default:
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- public void requestdata(boolean requestcount)
- {
- if (m_task != null)
- {
- return;
- }
- mStatusMessageView.setText(R.string.login_progress_signing_in);
- showProgress(true);
- m_task = new SearchTask();
- m_task.execute(requestcount);
- }
- private void showProgress(final boolean show)
- {
- // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
- // for very easy animations. If available, use these APIs to fade-in
- // the progress spinner.
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
- {
- int shortAnimTime = getResources().getInteger(
- android.R.integer.config_shortAnimTime);
- mStatusView.setVisibility(View.VISIBLE);
- mStatusView.animate().setDuration(shortAnimTime)
- .alpha(show ? 1 : 0)
- .setListener(new AnimatorListenerAdapter()
- {
- @Override
- public void onAnimationEnd(Animator animation)
- {
- mStatusView.setVisibility(show ? View.VISIBLE
- : View.INVISIBLE);
- }
- });
- mSearchFormView.setVisibility(View.VISIBLE);
- mSearchFormView.animate().setDuration(shortAnimTime)
- .alpha(show ? 0 : 1)
- .setListener(new AnimatorListenerAdapter()
- {
- @Override
- public void onAnimationEnd(Animator animation)
- {
- mSearchFormView.setVisibility(show ? View.INVISIBLE
- : View.VISIBLE);
- }
- });
- }
- else
- {
- // The ViewPropertyAnimator APIs are not available, so simply show
- // and hide the relevant UI components.
- mStatusView.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
- mSearchFormView.setVisibility(show ? View.INVISIBLE : View.VISIBLE);
- }
- }
- public class SearchTask extends AsyncTask<Boolean, Void, Boolean>
- {
- // int err_code = ERR_CODE_NONE;
- int errorcode;
- @Override
- protected Boolean doInBackground(Boolean... params)
- {
- if (!Network.NetworkIsAvailable())
- {
- errorcode = Network.RESULT_NET_NOTAVAILABLE;
- return false;
- }
- if (params[0])
- {
- int ret = Network.get_recordcount(user, password, searchParms);
- if (ret >= 0)
- {
- searchresult.put_totalcount(ret);
- }
- else
- {
- errorcode = ret;
- return false;
- }
- }
- String jstr = Network.get_records(user, password, searchParms);
- if (jstr == null || jstr.length() <= 0)
- {
- // Log.d(TAG, "json is wrong");
- errorcode = Network.RESULT_NET_ERROR;
- return false;
- }
- JSONObject jsobj;
- //
- // array = new JSONArray(json);
- try
- {
- jsobj = new JSONObject(jstr);
- if (searchresult.get_fieldscount() == 0)
- {
- JSONObject objfields = jsobj.getJSONObject("fields");
- searchresult.init_fields(objfields.toString());
- }
- JSONObject objrecords = jsobj.getJSONObject("records");
- searchresult.add_records(objrecords.toString());
- errorcode = Network.RESULT_TRUE;
- return true;
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- errorcode = Network.RESULT_NET_ERROR;
- return false;
- }
- @Override
- protected void onPostExecute(final Boolean success)
- {
- Log.i("onPostExecute", "entry");
- m_task = null;
- showProgress(false);
- switch (errorcode)
- {
- case Network.RESULT_NET_NOTAVAILABLE:
- {
- Toast toast = Toast.makeText(getApplicationContext(),
- getText(R.string.msg_connection_none),
- Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- return;
- }
- case Network.RESULT_NET_ERROR:
- {
- Toast toast = Toast.makeText(getApplicationContext(),
- getText(R.string.msg_net_error), Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- return;
- }
- case Network.RESULT_ERROR:
- // case Network.RESULT_RESPONSE_NULL:
- {
- Toast toast = Toast.makeText(getApplicationContext(),
- getText(R.string.msg_net_resulterror),
- Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- return;
- }
- default:
- break;
- }
- if (success)
- {
- TextView tv_head = (TextView) findViewById(R.id.tv_head);
- tv_head.setText(searchresult.get_totalcount()+"");
- // SharedPreferences.Editor editor = RunOnce.edit();
- // editor.putBoolean("FirstRun"+globalUtil.getVerName(this),
- // false);
- // // Don't forget to commit your edits!!!
- // editor.commit();
- // Intent intent = new Intent();
- // intent.setClass(getActivity(), FunctionSelectActivity.class);
- // intent.putExtra("user", m_sUser);
- // intent.putExtra("password", m_sPassword);
- // startActivity(intent);
- // getActivity().finish();
- }
- else
- {
- // m_etPassword
- // .setError(getString(R.string.error_incorrect_password));
- // m_etPassword.requestFocus();
- }
- }
- @Override
- protected void onCancelled()
- {
- m_task = null;
- showProgress(false);
- }
- }
- private class SearchResult
- {
- int total_count = 0;
- // int count = 0;
- ArrayList<record> records = new ArrayList<record>();
- ArrayList<field> fields = new ArrayList<field>();
- public void init_fields(String jsonfields)
- {
- String TAG = "init_fields@ResultActivity.SearchResult";
- Log.d(TAG, jsonfields);
- }
- public void add_records(String records)
- {
- String TAG = "init_fields@ResultActivity.SearchResult";
- Log.d(TAG, records);
- }
- public int get_totalcount()
- {
- return total_count;
- }
- public void put_totalcount(int c)
- {
- total_count = c;
- }
- public int get_count()
- {
- return records.size();
- }
- public int get_fieldscount()
- {
- return fields.size();
- }
- private class record
- {
- ArrayList<Object> values;
- }
- private class field
- {
- String name = null;
- String aliasname = null;
- String type = null;
- public String get_name()
- {
- return name;
- }
- public String get_aliasname()
- {
- return aliasname;
- }
- public String get_type()
- {
- return type;
- }
- }
- }
- }
|