| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- package com.usai.apex;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Locale;
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.usai.util.Network;
- import com.usai.util.dbUtil;
- import android.net.Uri;
- 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.app.AlertDialog;
- import android.app.Dialog;
- import android.app.DownloadManager;
- import android.app.AlertDialog.Builder;
- import android.app.DownloadManager.Request;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.text.Html;
- import android.text.style.URLSpan;
- import android.util.Log;
- import android.util.SparseArray;
- import android.view.ContextMenu;
- import android.view.Gravity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.View.OnClickListener;
- import android.view.View.OnCreateContextMenuListener;
- import android.widget.AdapterView.AdapterContextMenuInfo;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.TableLayout;
- 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;
- int sel = -1;
- Bundle searchParms = null;
- SearchResult searchresult = new SearchResult();
- SparseArray<String> showfieldmap = new SparseArray<String>();
- // HashMap<Integer, String> showfieldmap = new HashMap<Integer, String>();
- private TextView mStatusMessageView;
- // private View mSearchFormView;
- private View footview;
- private View mStatusView;
- int actioncount=0;
- // private class resultAdapter extends SimpleAdapter
- // {
- //
- //
- // }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- user = ApexTrackingApplication.get_user();
- password = ApexTrackingApplication.get_pass();
- function_name = getIntent().getStringExtra("function_name");
- searchParms = getIntent().getBundleExtra("searchParms");
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_result);
-
- SQLiteDatabase db = dbUtil.OpenDB(ResultActivity.this, null, false);
- Cursor cursor = db.query("actions_info",
- new String[] { "count(*)" }, "function_name='"
- + function_name + "' and user='" + user + "'",
- null, null, null, "priority", null);
- if (cursor.moveToNext()) {
- actioncount = cursor.getInt(0);
- }
- dbUtil.CloseCursor(cursor);
- dbUtil.CloseDB(db);
-
-
- footview = findViewById(R.id.foot);
- // mSearchFormView = findViewById(R.id.search_form);
- Button btnnext = (Button) findViewById(R.id.btn_next);
- btnnext.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TextView tv_head = (TextView) findViewById(R.id.tv_head);
- // searchresult.set_direction(1);
- if (searchresult.get_totalcount() == -1)
- requestdata(true);
- else {
- // if (searchresult.get_direction() == -1)
- searchresult.set_offset(searchresult.get_offset() + 10);
- requestdata(false);
- }
- }
- });
- Button btnpre = (Button) findViewById(R.id.btn_pre);
- btnpre.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TextView tv_head = (TextView) findViewById(R.id.tv_head);
- // searchresult.set_direction(-1);
- if (searchresult.get_totalcount() == -1)
- requestdata(true);
- else {
- searchresult.set_offset(searchresult.get_offset() - 10);
- requestdata(false);
- }
- }
- });
- mStatusView = findViewById(R.id.status);
- mStatusMessageView = (TextView) findViewById(R.id.status_message);
- LinearLayout ll = (LinearLayout) findViewById(R.id.ll_refresh);
- Button btn_refresh = (Button) ll.findViewById(R.id.btn_refresh);
- btn_refresh.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- LinearLayout ll = (LinearLayout) findViewById(R.id.ll_refresh);
- ll.setVisibility(View.INVISIBLE);
- if (searchresult.get_totalcount() == -1)
- requestdata(true);
- else
- requestdata(false);
- }
- });
- SharedPreferences RunOnce = getSharedPreferences("Apex", 0);
- String vername;
- try {
- vername = getPackageManager().getPackageInfo("com.usai.apex", 0).versionName;
- boolean bFirstRun = RunOnce.getBoolean("FirstRun" + vername
- + "_result", true);
- if (bFirstRun) {
- SharedPreferences.Editor editor = RunOnce.edit();
- editor.putBoolean("FirstRun" + vername + "_result", false);
- // Don't forget to commit your edits!!!
- editor.commit();
- Intent intent = new Intent();
- intent.setClass(this, HelpActivity.class);
- intent.putExtra("caller", "result");
- startActivity(intent);
- }
- } catch (NameNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- initTableHeader();
- if(savedInstanceState!=null)
- {
- searchresult=(SearchResult) savedInstanceState.getSerializable("searchresult");
- if(searchresult.get_totalcount()==-1)
- requestdata(true);
- else
- initTable();
- }
- else
- requestdata(true);
- }
- @Override
- protected void onDestroy()
- {
- if(m_task!=null)
- m_task.cancel(false);
- super.onDestroy();
- }
- private void initTable()
- {
- // int loadcount = searchresult.get_count();
- int totalcount = searchresult.get_totalcount();
- TextView tv_head = (TextView) findViewById(R.id.head);
- if (totalcount == 0) {
- tv_head.setText("0 record found");
- return;
- } else
- tv_head.setText(/*
- * loadcount + "/" +
- */searchresult.get_totalcount()
- + " records");
- TableLayout tl = (TableLayout) findViewById(R.id.result_table);
- tl.removeViews(1, tl.getChildCount() - 1);
- // int newcount = loadcount - showcount + 1;// +1 header row
- for (int i = 0; i < searchresult.get_recordscount(); i++) {
- TableRow recordRow = new TableRow(ResultActivity.this);
- recordRow
- .setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- // TODO Auto-generated method stub
- // v.setBackgroundColor(Color.GRAY);
- // v.showContextMenu();
- // return true;
- TableRow tr = (TableRow) v;
- TextView tvno = (TextView) tr.getChildAt(0);
- sel = Integer.parseInt(tvno.getText()
- .toString()) % 10;
- registerForContextMenu(v);
- openContextMenu(v);
- unregisterForContextMenu(v);
- return true;
- }
- });
- // registerForContextMenu(recordRow);
- // recordRow.setClickable(true);
- // registerForContextMenu(recordRow);
- // recordRow.setOnCreateContextMenuListener(m_tableMenu);
- // recordRow.SETSE
- HashMap<String, String> record = searchresult.get_record(i);
- TextView field = new TextView(ResultActivity.this);
- field.setBackgroundResource(R.drawable.tableitem);
- field.setText(searchresult.get_offset() + i + "");
- Log.d("offset=" + searchresult.get_offset(), "");
- field.setGravity(Gravity.CENTER);
- field.setPadding(10, 5, 10, 5);
- field.setTextSize(20);
- recordRow.addView(field);
- for (int j = 0; j < showfieldmap.size(); j++) {
- TextView field1 = new TextView(ResultActivity.this);
- Log.d("onPostExecute", showfieldmap.get(j));
- String str = record.get(showfieldmap.get(j));
- field1.setBackgroundResource(R.drawable.tableitem);
- if (str.toLowerCase().trim().equals("null"))
- str = "";
- // field1.setBackgroundColor(0xFFFFFFFF);
- field1.setText(Html.fromHtml(str));
- if(actioncount==0)
- field1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- URLSpan span[] = ((TextView) v).getUrls();
- if (span.length < 1)
- return;
- span[0].getURL();
- Log.d("Text", span[0].getURL());
- final DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
- Uri uri = Uri.parse(span[0].getURL());
- final Request request = new Request(uri);
- // 设置允许使用的网络类型,这里是移动网络和wifi都可以
- request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
- | DownloadManager.Request.NETWORK_WIFI);
- // 禁止发出通知,既后台下载,如果要使用这一句必须声明一个权限:android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
- // request.setShowRunningNotification(false);
- // 不显示下载界面
- request.setVisibleInDownloadsUi(false);
- request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
- /*
- * 设置下载后文件存放的位置,如果sdcard不可用,那么设置这个将报错,
- * 因此最好不设置如果sdcard可用,下载后的文件
- * 在/mnt/sdcard/Android/
- * data/packageName/files目录下面
- * ,如果sdcard不可用,设置了下面这个将报错,不设置,下载后的文件在/cache这个
- * 目录下面
- */
- // request.setDestinationInExternalFilesDir(this,
- // null, "tar.apk");
-
- AlertDialog.Builder builder = new Builder(ResultActivity.this);
- builder.setMessage("Click start button to begin download");
- builder.setTitle("Confirm download");
- builder.setPositiveButton("Start", new Dialog.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- long id = downloadManager.enqueue(request);
- dialog.dismiss();
- }
- });
- builder.setNegativeButton("Cancel", new Dialog.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- builder.create().show();
-
- }
- });
- // field1.setText(str);
- field1.setGravity(Gravity.CENTER);
- field1.setPadding(10, 5, 10, 5);
- field1.setTextSize(20);
- // field.setTextAppearance(ResultActivity.this,
- // android.R.attr.textAppearanceMedium);
- recordRow.addView(field1);
- }
- tl.addView(recordRow);
- }
- Button btnnext = (Button) findViewById(R.id.btn_next);
- Button btnpre = (Button) findViewById(R.id.btn_pre);
- if (searchresult.get_offset() == 0)
- btnpre.setEnabled(false);
- else {
- btnpre.setEnabled(true);
- }
- if (searchresult.get_totalcount() - searchresult.get_offset() <= 10)
- btnnext.setEnabled(false);
- else
- btnnext.setEnabled(true);
- }
- private OnCreateContextMenuListener m_tableMenu = new OnCreateContextMenuListener() {
- @Override
- public void onCreateContextMenu(ContextMenu contextmenu, View view,
- ContextMenuInfo contextmenuinfo) {
- contextmenu.add(Menu.NONE, 0, 0, "Detail");
- }
- };
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- // menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
- // Log.d("table row select", sel+ "");
- Intent intent = new Intent();
- intent.setClass(this, DetailActivity.class);
- SQLiteDatabase db = dbUtil.OpenDB(this, null, false);
- Cursor cursor = db.query("actions_info",
- new String[] { "name", "_id" }, "function_name='"
- + function_name + "' and user='" + user + "'", null,
- null, null, "priority", null);
- int i = 0;
- while (cursor.moveToNext()) {
- String name = cursor.getString(0);
- Log.d("actionname", name);
- intent.putExtra("action" + i, name);
- i++;
- }
- intent.putExtra("function_name", function_name);
- intent.putExtra("actions_count", i);
- intent.putExtra("_id", searchresult.get_record(sel).get("_id")
- .toString());
- dbUtil.CloseCursor(cursor);
- dbUtil.CloseDB(db);
- startActivity(intent);
- return super.onContextItemSelected(item);
- }
- // @Override
- // public void onCreateContextMenu(ContextMenu menu, View v,
- // ContextMenuInfo menuInfo)
- // {
- // // // TODO Auto-generated method stub
- // menu.add(Menu.NONE, 0, 0, "Detail");
- // // // contextmenu.add(Menu.NONE, 1, 1, "�༭");
- // super.onCreateContextMenu(menu, v, menuInfo);
- // }
- void initTableHeader() {
- TableLayout tl = (TableLayout) findViewById(R.id.result_table);
- tl.setFocusable(true);
- tl.setOnCreateContextMenuListener(m_tableMenu);
- SQLiteDatabase db = dbUtil.OpenDB(this, null, false);
- Cursor cursor = db.query("fields_info", new String[] { "aname", "name",
- "_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();
- ArrayList<HashMap<String, String>> gridheader = new ArrayList<HashMap<String, String>>();
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("key", "no.");
- gridheader.add(map);
- Button btn = new Button(this);
- btn.setBackgroundResource(R.drawable.tablehead);
- btn.setPadding(0, 0, 0, 0);
- btn.setText("No.");
- headerRow.addView(btn);
- while (cursor.moveToNext()) {
- String aname = cursor.getString(0);
- Button btn1 = new Button(this);
- btn1.setBackgroundResource(R.drawable.tablehead);
- btn1.setText(aname);
- headerRow.addView(btn1);
- showfieldmap.put(pos, cursor.getString(1));
- map.put("key", "header");
- gridheader.add(map);
- pos++;
- }
- dbUtil.CloseCursor(cursor);
- dbUtil.CloseDB(db);
- // SimpleAdapter adapter = new SimpleAdapter(this,
- // gridheader,// �����Դ
- // R.layout.result_item,//XMLʵ��
- // new String[] { "key" }, // ��̬������ImageItem��Ӧ������
- // new int[] { R.id.tv_value });
- //
- // GridView gridview = (GridView) findViewById(R.id.grid_result);
- // gridview.setNumColumns(2);
- // gridview.setAdapter(adapter);
- }
- @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) {
- Intent intent = new Intent();
- switch (item.getItemId()) {
- case R.id.action_custom_fields:
- 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;
- case R.id.action_help:
- // Intent intent = new Intent();
- intent.setClass(this, HelpActivity.class);
- intent.putExtra("caller", "result");
- // // intent.putExtra("password", password);
- // intent.putExtra("function_name", function_name);
- // intent.putExtra("behavior", Network.BEHAVIOR_SEARCH);
- startActivity(intent);
- break;
- default:
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- public void requestdata(boolean requestcount) {
- if (m_task != null) {
- return;
- }
- mStatusMessageView.setText("Loading");
- 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);
- }
- });
- footview.setVisibility(View.VISIBLE);
- footview.animate().setDuration(shortAnimTime).alpha(show ? 0 : 1)
- .setListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- footview.setVisibility(show ? View.INVISIBLE
- : View.VISIBLE);
- }
- });
- // 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);
- footview.setVisibility(show ? View.INVISIBLE : View.VISIBLE);
- // 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);
- if (ret == 0)
- return true;
- } else {
- errorcode = ret;
- return false;
- }
- }
- // if (searchresult.get_direction() == -1)
- // searchresult.set_offset(searchresult.get_offset() - 20);
- searchParms.putString("offset", searchresult.get_offset() + "");
- 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");
- // if (objfields != null)
- // searchresult.init_fields(objfields.toString());
- // }
- JSONObject objrecords = jsobj.getJSONObject("records");
- if (objrecords != null)
- 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) {
- initTable();
- } else {
- LinearLayout ll = (LinearLayout) findViewById(R.id.ll_refresh);
- ll.setVisibility(View.VISIBLE);
- }
- }
- @Override
- protected void onCancelled() {
- m_task = null;
- showProgress(false);
- }
- }
-
- @Override
- protected void onSaveInstanceState(Bundle outState)
- {
- outState.putSerializable("searchresult", searchresult);
- super.onSaveInstanceState(outState);
- }
- }
|