| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- package com.usai.apex;
- import java.io.File;
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.usai.util.Network;
- import com.usai.util.commonUtil;
- import android.animation.Animator;
- import android.animation.AnimatorListenerAdapter;
- 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.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.support.v4.app.Fragment;
- import android.text.Html;
- import android.util.Log;
- import android.util.SparseIntArray;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.webkit.DownloadListener;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.ScrollView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class DetailFragment extends Fragment implements OnClickListener /*
- * ,
- * OnGestureListener
- */
- {
- private static final int REQUEST_LOGINACTIVITY = 1;
- // DataFragment mdataFragment;
- /* private GestureDetector mGestureDetector; */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- }
- @Override
- public void onDestroyView()
- {
- Log.d("DetailFragment", "onDestroyView()");
- super.onDestroyView();
- }
- // public String get_content()
- // {
- // return fragment_content;
- // }
- LinearLayout ll_root;
- ScrollView sl_root;
- String mimeType = "text/html";
- private SearchTask m_task = null;
- SparseIntArray control = new SparseIntArray();
- // private TextView mStatusMessageView;
- private View mStatusView;
- String fragment_content = null;
- public void requestdata()
- {
- if (m_task != null)
- {
- return;
- }
- if (fragment_content != null)
- {
- LayoutInflater inflater = (LayoutInflater) ApexTrackingApplication
- .get_instance().getSystemService(
- Context.LAYOUT_INFLATER_SERVICE);
- init(fragment_content, inflater);
- return;
- }
- // if (mdataFragment.get_result() != null) {
- // LayoutInflater inflater = (LayoutInflater) ApexTrackingApplication
- // .get_instance().getSystemService(
- // Context.LAYOUT_INFLATER_SERVICE);
- // init(mdataFragment.get_result(), inflater);
- // }
- // if (fragment_content != null)
- // return;
- // mStatusMessageView.setText("Loading");
- showProgress(true);
- m_task = new SearchTask();
- m_task.execute();
- }
- public void set_content(String content)
- {
- fragment_content = content;
- }
- 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 = ApexTrackingApplication.get_instance()
- .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);
- }
- });
- ll_root.setVisibility(View.VISIBLE);
- ll_root.animate().setDuration(shortAnimTime).alpha(show ? 0 : 1)
- .setListener(new AnimatorListenerAdapter()
- {
- @Override
- public void onAnimationEnd(Animator animation)
- {
- ll_root.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);
- ll_root.setVisibility(show ? View.INVISIBLE : View.VISIBLE);
- }
- }
- // @Override
- // public void onActivityCreated(Bundle savedInstanceState)
- // {
- // FragmentManager fm = getFragmentManager();
- //
- // // Check to see if we have retained the worker fragment.
- // mdataFragment = (DataFragment)fm.findFragmentByTag("work");
- //
- // // If not retained (or first time running), we need to create it.
- // if (mdataFragment == null) {
- // mdataFragment = new DataFragment();
- // // Tell it who it is working with.
- // mdataFragment.setTargetFragment(this, 0);
- // fm.beginTransaction().add(mdataFragment, "work").commit();
- // }
- // super.onActivityCreated(savedInstanceState);
- // }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState)
- {
- /* mGestureDetector = new GestureDetector(this); */
- View view = inflater.inflate(R.layout.detail_fragment, null);
- sl_root = (ScrollView) view.findViewById(R.id.sl_root);
- /*
- * sl_root.setOnTouchListener(new View.OnTouchListener() {
- *
- * @Override public boolean onTouch(View v, MotionEvent event) { return
- * mGestureDetector.onTouchEvent(event); } });
- */
- ll_root = (LinearLayout) view.findViewById(R.id.ll_root);
- mStatusView = view.findViewById(R.id.status);
- LinearLayout ll = (LinearLayout) view.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) ll_root
- .findViewById(R.id.ll_refresh);
- ll.setVisibility(View.GONE);
- requestdata();
- }
- });
- // FragmentManager fm = getFragmentManager();
- // List<Fragment> fragments = fm.getFragments();
- // if (fragments != null) {
- // Log.e("DetailFragment", "fragments count=" + fragments.size());
- // // for (int i = 0; i < fragments.size(); i++)
- // // Log.e("DetailFragment", "fragments name=" + fragments.size());
- //
- // }
- // // this.getFragmentManager();
- // String tag = getArguments().getString("module_name");
- // // Check to see if we have retained the worker fragment.
- // mdataFragment = (DataFragment) fm.findFragmentById(12345);
- //
- // // If not retained (or first time running), we need to create it.
- // if (mdataFragment == null) {
- // Log.e("dataFragment creaded ", "tag=data_" + tag);
- // mdataFragment = new DataFragment();
- // // Tell it who it is working with.
- // mdataFragment.setTargetFragment(this, 0);
- // fm.beginTransaction().add(12345,mdataFragment, "data+" +
- // tag).commit();
- // }
- requestdata();
- return view;
- }
- void init(String jstr, LayoutInflater inflater)
- {
- String TAG = "init@DetailFragment";
- Log.d(TAG, jstr);
- try
- {
- JSONObject jsonObj = new JSONObject(jstr);
- int group_count = jsonObj.getInt("count");
- if (group_count == 0)
- {
- TextView tv_empty = (TextView) ll_root
- .findViewById(R.id.tv_empty);
- tv_empty.setVisibility(View.VISIBLE);
- }
- for (int i = 0; i < group_count; i++)
- {
- // String key = (String) it.next();
- // if (key.equals("result"))
- // continue;
- JSONObject groupobj = jsonObj.getJSONObject("group" + i);
- View v = inflater.inflate(R.layout.group_tag, null);
- v.setOnClickListener(this);
- TextView tvgroup = (TextView) v.findViewById(R.id.tv_group);
- String group_name = groupobj.getString("_name");
- tvgroup.setText(group_name);
- v.setId(commonUtil.generateViewId());
- ll_root.addView(v);
- String grouptype = groupobj.getString("_type");
- // groupobj.remove("_type");
- if (grouptype.equals("mapping"))
- {
- LinearLayout mappingview = (LinearLayout) inflater.inflate(
- R.layout.key_mapping, null);
- mappingview.setId(commonUtil.generateViewId());
- control.put(v.getId(), mappingview.getId());
- ll_root.addView(mappingview);
- int map_count = groupobj.getInt("count");
- for (int j = 0; j < map_count; j++)
- {
- // String keygroup = (String) itgroup.next();
- JSONObject itemobj = groupobj.getJSONObject("item" + j);
- String key = (String) itemobj.keys().next();
- String val = itemobj.getString(key);
- LinearLayout mappingitem = (LinearLayout) inflater
- .inflate(R.layout.keymapping_item, null);
- TextView tvkey = (TextView) mappingitem
- .findViewById(R.id.tv_key);
- tvkey.setText(key);
- // tvkey.setBackgroundResource(R.drawable.detail_item);
- TextView tvval = (TextView) mappingitem
- .findViewById(R.id.tv_value);
- tvval.setText(Html.fromHtml(val));
- // tvval.setBackgroundResource(R.drawable.detail_item);
- mappingview.addView(mappingitem);
- }
- // Iterator itgroup = groupobj.keys();
- // while (itgroup.hasNext())
- // {
- //
- //
- // }
- }
- else if (grouptype.equals("table"))
- {
- String content = groupobj.getString("content");
- WebView wv;
- wv = (WebView) inflater.inflate(R.layout.web_content, null);
- wv.getSettings().setDefaultTextEncodingName("UTF-8");
- wv.setId(commonUtil.generateViewId());
- wv.setWebViewClient(new MyWebViewClient());
- wv.setDownloadListener(new DownloadListener()
- {
- @Override
- public void onDownloadStart(String url,
- String userAgent, String contentDisposition,
- String mimetype, long contentLength)
- {
- if(!commonUtil.localdirExist())
- {
-
- new AlertDialog.Builder(getActivity())
- .setIconAttribute(android.R.attr.alertDialogIcon)
- .setTitle(R.string.str_localdir_error)
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- /* User clicked OK so do some stuff */
- }
- })
- .create().show();
- return;
- }
- String filename = contentDisposition.replace("attachment;filename=\"", "");
- filename = filename.replace("\"", "");
-
- Log.d("WebView", "Download Action");
- Log.i("tag", "url=" + url);
- // Log.i("tag", "userAgent="+userAgent);
- // Log.i("tag",
- // "contentDisposition="+contentDisposition);
- // Log.i("tag", "mimetype="+mimetype);
- // Log.i("tag", "contentLength="+contentLength);
- // Uri uri = Uri.parse(url);
- // Intent intent = new Intent(Intent.ACTION_VIEW,
- // uri);
- // startActivity(intent);
- final DownloadManager downloadManager = (DownloadManager) ApexTrackingApplication
- .get_instance().getSystemService(
- Context.DOWNLOAD_SERVICE);
- Uri uri = Uri.parse(url);
- final Request request = new Request(uri);
- // 设置允许使用的网络类型,这里是移动网络和wifi都可以
- request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
- | DownloadManager.Request.NETWORK_WIFI);
-
-
-
- request.setDestinationInExternalPublicDir("Apex Mobile",filename);
- // 禁止发出通知,既后台下载,如果要使用这一句必须声明一个权限: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");
- // getActivity();
- AlertDialog.Builder builder = new Builder(
- getActivity());
- builder.setMessage(getString(R.string.str_downloadmessage));
- builder.setTitle(getString(R.string.str_confirmdownload));
- builder.setPositiveButton(
- getString(R.string.str_download),
- new Dialog.OnClickListener()
- {
- @Override
- public void onClick(
- DialogInterface dialog,
- int which)
- {
- downloadManager.enqueue(request);
- dialog.dismiss();
- }
- });
- builder.setNegativeButton(
- getString(R.string.str_cancel),
- new Dialog.OnClickListener()
- {
- @Override
- public void onClick(
- DialogInterface dialog,
- int which)
- {
- dialog.dismiss();
- }
- });
- builder.create().show();
- }
- });
- control.put(v.getId(), wv.getId());
- wv.loadData(content, mimeType, null);
- // wv.setOnTouchListener(new OnTouchListener()
- // {
- // @Override
- // public boolean onTouch(View v, MotionEvent event)
- // {
- //
- // if (event.getAction() == MotionEvent.ACTION_UP)
- // sl_root.requestDisallowInterceptTouchEvent(false);
- // else
- // sl_root.requestDisallowInterceptTouchEvent(true);
- //
- // return false;
- // }
- // });
- ll_root.addView(wv);
- Log.d("table content", content);
- }
- if (grouptype.equals("list"))
- {
- LinearLayout listview = (LinearLayout) inflater.inflate(
- R.layout.list_content, null);
- listview.setId(commonUtil.generateViewId());
- control.put(v.getId(), listview.getId());
- ll_root.addView(listview);
- long list_count = groupobj.getInt("count");
- for (int j = 0; j < list_count; j++)
- {
- RelativeLayout listitem = (RelativeLayout) inflater
- .inflate(R.layout.list_item, null);
- TextView tvitem = (TextView) listitem
- .findViewById(R.id.tv_item);
- String val = groupobj.getString("line" + j);
- tvitem.setText(val);
- // tvitem.setBackgroundResource(R.drawable.detail_item);
- listitem.removeView(tvitem);
- listview.addView(tvitem);
- }
- // Iterator itgroup = groupobj.keys();
- // while (itgroup.hasNext())
- // {
- // String keygroup = (String) itgroup.next();
- //
- // }
- }
- // String value = obj.getString(key);
- // JSONArray array = obj.getJSONArray(key);
- // for(int i=0;i<array.length();i++){
- // JSONObject jsonobject = array.getJSONObject(i);
- // jsonobject.put("name", key);
- // jsonobject.put("exp", key+"="+jsonobject.getString("value"));
- // newArray.put(jsonobject);
- }
- // Iterator it = jsonObj.keys();
- // while (it.hasNext())
- // {
- //
- // }
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public void onClick(View v)
- {
- View view = ll_root.findViewById(control.get(v.getId()));
- if (view.getVisibility() == View.VISIBLE)
- view.setVisibility(View.GONE);
- else
- view.setVisibility(View.VISIBLE);
- }
- @Override
- public void onDestroy()
- {
- if (m_task != null)
- m_task.cancel(true);
- super.onDestroy();
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- ((DetailActivity) getActivity()).showlogin = false;
- super.onActivityResult(requestCode, resultCode, data);
- }
- @Override
- public void onDetach()
- {
- if (m_task != null)
- m_task.cancel(false);
- super.onDetach();
- }
- class SearchTask extends AsyncTask<Void, Void, Boolean>
- {
- int errorcode;
- @Override
- protected Boolean doInBackground(Void... params)
- {
- Log.d("SearchTask", "doInBackground");
- if (!Network.NetworkIsAvailable())
- {
- errorcode = Network.RESULT_NET_NOTAVAILABLE;
- return false;
- }
- String jstr = Network.get_detail(getArguments());
- if (jstr == null || jstr.length() <= 0)
- {
- // Log.d(TAG, "json is wrong");
- errorcode = Network.RESULT_NET_ERROR;
- return false;
- }
- // mdataFragment.put_result(jstr);
- fragment_content = jstr;
- DetailActivity activity = (DetailActivity) getActivity();
- activity.save_content(getArguments().getString("action_type"), jstr);
- errorcode = Network.RESULT_TRUE;
- return true;
- }
- @Override
- protected void onPostExecute(Boolean success)
- {
- Log.i("onPostExecute", "entry");
- m_task = null;
- showProgress(false);
- if (ApexTrackingApplication.get_authorization() == false)
- {
- if(((DetailActivity) getActivity()).showlogin == true)
- return;
- ((DetailActivity) getActivity()).showlogin = true;
- LinearLayout ll = (LinearLayout) ll_root
- .findViewById(R.id.ll_refresh);
- ll.setVisibility(View.VISIBLE);
- Toast toast = Toast.makeText(
- ApexTrackingApplication.get_instance(),
- getText(R.string.msg_net_passwordchangedremote) , Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- Intent i = new Intent();
- i.setClass(getActivity(), ApexActivity.class);
- startActivityForResult(i, REQUEST_LOGINACTIVITY);
- return;
- }
-
- switch (errorcode)
- {
- case Network.RESULT_NET_NOTAVAILABLE:
- {
- Toast toast = Toast.makeText(
- ApexTrackingApplication.get_instance(),
- getText(R.string.msg_connection_none),
- Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- break;
- }
- case Network.RESULT_NET_ERROR:
- {
- Toast toast = Toast.makeText(
- ApexTrackingApplication.get_instance(),
- getText(R.string.msg_net_error), Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- break;
- }
- case Network.RESULT_ERROR:
- // case Network.RESULT_RESPONSE_NULL:
- {
- Toast toast = Toast.makeText(
- ApexTrackingApplication.get_instance(),
- getText(R.string.msg_net_resulterror) + ":"
- + errorcode, Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- break;
- }
- default:
- break;
- }
- if (success)
- {
- LayoutInflater inflater = (LayoutInflater) ApexTrackingApplication
- .get_instance().getSystemService(
- Context.LAYOUT_INFLATER_SERVICE);
- init(fragment_content, inflater/*
- * getActivity().
- * getLayoutInflater()
- */);
- }
- else
- {
- LinearLayout ll = (LinearLayout) ll_root
- .findViewById(R.id.ll_refresh);
- ll.setVisibility(View.VISIBLE);
- }
- super.onPostExecute(success);
- }
- @Override
- protected void onCancelled()
- {
- m_task = null;
- showProgress(false);
- }
- }
- // public class DataFragment extends Fragment {
- //
- // String result = null;
- //
- // @Override
- // public void onCreate(Bundle savedInstanceState) {
- // setRetainInstance(true);
- // super.onCreate(savedInstanceState);
- // }
- //
- // public String get_result() {
- // return result;
- //
- // }
- //
- // public void put_result(String s) {
- // result = s;
- // }
- //
- // }
- public class MyWebViewClient extends WebViewClient
- {
- // 如果页面中链接,如果希望点击链接继续在当前browser中响应,
- // 而不是新开Android的系统browser中响应该链接,必须覆盖 webview的WebViewClient对象。
- public boolean shouldOverviewUrlLoading(WebView view, String url)
- {
- // L.i("shouldOverviewUrlLoading");
- view.loadUrl(url);
- return true;
- }
- public void onPageStarted(WebView view, String url, Bitmap favicon)
- {
- // L.i("onPageStarted");
- // showProgress();
- }
- public void onPageFinished(WebView view, String url)
- {
- // L.i("onPageFinished");
- // closeProgress();
- }
- public void onReceivedError(WebView view, int errorCode,
- String description, String failingUrl)
- {
- // L.i("onReceivedError");
- // closeProgress();
- }
- }
- }
|