DetailFragment.java 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. package com.usai.apex;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.app.AlertDialog;
  5. import android.app.ProgressDialog;
  6. import android.content.ClipboardManager;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.database.Cursor;
  10. import android.graphics.Bitmap;
  11. import android.graphics.Color;
  12. import android.net.Uri;
  13. import android.os.AsyncTask;
  14. import android.os.Build;
  15. import android.os.Bundle;
  16. import android.provider.ContactsContract;
  17. import android.support.constraint.ConstraintLayout;
  18. import android.support.v4.app.Fragment;
  19. import android.text.Editable;
  20. import android.text.Html;
  21. import android.text.TextWatcher;
  22. import android.util.Log;
  23. import android.util.SparseIntArray;
  24. import android.view.GestureDetector;
  25. import android.view.Gravity;
  26. import android.view.LayoutInflater;
  27. import android.view.MotionEvent;
  28. import android.view.View;
  29. import android.view.View.OnClickListener;
  30. import android.view.ViewGroup;
  31. import android.webkit.WebResourceRequest;
  32. import android.webkit.WebResourceResponse;
  33. import android.webkit.WebView;
  34. import android.webkit.WebViewClient;
  35. import android.widget.Button;
  36. import android.widget.EditText;
  37. import android.widget.ImageButton;
  38. import android.widget.ImageView;
  39. import android.widget.LinearLayout;
  40. import android.widget.RelativeLayout;
  41. import android.widget.ScrollView;
  42. import android.widget.TextView;
  43. import android.widget.Toast;
  44. import com.usai.apex.ShipMap.ShipMap;
  45. import com.usai.apex.mainframe.NewDetailActivity;
  46. import com.usai.apex.pdf.PDFPreviewActivity;
  47. import com.usai.util.Network;
  48. import com.usai.util.RAUtil;
  49. import com.usai.util.commonUtil;
  50. import org.apache.http.util.EncodingUtils;
  51. import org.json.JSONException;
  52. import org.json.JSONObject;
  53. import java.io.ByteArrayInputStream;
  54. import java.io.File;
  55. import java.io.InputStream;
  56. import java.util.regex.Pattern;
  57. public class DetailFragment extends Fragment implements OnClickListener /*
  58. * ,
  59. * OnGestureListener
  60. */
  61. {
  62. private static final int People_Pick_Code = 1357;
  63. private String copiedKey;
  64. private String copiedVal;
  65. private View touchedView;
  66. private class GestureListener extends GestureDetector.SimpleOnGestureListener {
  67. @Override
  68. public boolean onDoubleTapEvent(MotionEvent e) {
  69. if (e.getAction() == MotionEvent.ACTION_UP) {
  70. if (touchedView == null) {
  71. ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
  72. cm.setText(copiedVal);
  73. Toast.makeText(mContext, "Copied " + copiedKey, Toast.LENGTH_LONG).show();
  74. } else {
  75. String type = (String) touchedView.getTag();
  76. if (type.equals("tracking")) {
  77. TextView tvgroup = (TextView) touchedView.findViewById(R.id.tv_group);
  78. if (tvgroup != null) {
  79. String val = tvgroup.getText().toString();
  80. ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
  81. cm.setText(val);
  82. Toast.makeText(mContext, "Copied " + val, Toast.LENGTH_LONG).show();
  83. }
  84. }
  85. }
  86. }
  87. copiedKey = null;
  88. copiedVal = null;
  89. touchedView = null;
  90. return false;
  91. }
  92. @Override
  93. public boolean onSingleTapConfirmed(MotionEvent e) {
  94. if (touchedView != null) {
  95. onClick(touchedView);
  96. }
  97. touchedView = null;
  98. return false;
  99. }
  100. }
  101. private static final int REQUEST_LOGINACTIVITY = 1;
  102. // DataFragment mdataFragment;
  103. Context mContext;
  104. ProgressDialog progressDialog;
  105. private GestureDetector detector;
  106. protected void showProgressDialog(final String title, final String msg) {
  107. getActivity().runOnUiThread(new Runnable() {
  108. @Override
  109. public void run() {
  110. try {
  111. progressDialog.setTitle(title);
  112. progressDialog.setMessage(msg);
  113. progressDialog.show();
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. });
  119. }
  120. /* private GestureDetector mGestureDetector; */
  121. @Override
  122. public void onCreate(Bundle savedInstanceState)
  123. {
  124. // TODO Auto-generated method stub
  125. super.onCreate(savedInstanceState);
  126. if (progressDialog == null) {
  127. progressDialog = new ProgressDialog(mContext);
  128. progressDialog.setCancelable(false);
  129. }
  130. if (detector == null) {
  131. detector = new GestureDetector(mContext, new GestureListener());
  132. }
  133. }
  134. // @Override
  135. // public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  136. // super.onActivityCreated(savedInstanceState);
  137. //
  138. // progressDialog = new ProgressDialog(mContext);
  139. // progressDialog.setCancelable(false);
  140. // }
  141. @Override
  142. public void onAttach(Context context) {
  143. super.onAttach(context);
  144. mContext = context;
  145. }
  146. @Override
  147. public void onDestroyView()
  148. {
  149. Log.d("DetailFragment", "onDestroyView()");
  150. super.onDestroyView();
  151. }
  152. // public String get_content()
  153. // {
  154. // return fragment_content;
  155. // }
  156. LinearLayout ll_root;
  157. ScrollView sl_root;
  158. String mimeType = "text/html";
  159. private SearchTask m_task = null;
  160. SparseIntArray control = new SparseIntArray();
  161. // private TextView mStatusMessageView;
  162. private View mStatusView;
  163. String fragment_content = null;
  164. public void requestdata()
  165. {
  166. if (m_task != null)
  167. {
  168. return;
  169. }
  170. if (fragment_content != null)
  171. {
  172. LayoutInflater inflater = (LayoutInflater) ApexTrackingApplication
  173. .get_instance().getSystemService(
  174. Context.LAYOUT_INFLATER_SERVICE);
  175. init(fragment_content, inflater);
  176. return;
  177. }
  178. // if (mdataFragment.get_result() != null) {
  179. // LayoutInflater inflater = (LayoutInflater) ApexTrackingApplication
  180. // .get_instance().getSystemService(
  181. // Context.LAYOUT_INFLATER_SERVICE);
  182. // init(mdataFragment.get_result(), inflater);
  183. // }
  184. // if (fragment_content != null)
  185. // return;
  186. // mStatusMessageView.setText("Loading");
  187. showProgress(true);
  188. m_task = new SearchTask();
  189. m_task.execute();
  190. }
  191. public void set_content(String content)
  192. {
  193. fragment_content = content;
  194. }
  195. private void showProgress(final boolean show)
  196. {
  197. // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  198. // for very easy animations. If available, use these APIs to fade-in
  199. // the progress spinner.
  200. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
  201. {
  202. int shortAnimTime = ApexTrackingApplication.get_instance()
  203. .getResources()
  204. .getInteger(android.R.integer.config_shortAnimTime);
  205. mStatusView.setVisibility(View.VISIBLE);
  206. mStatusView.animate().setDuration(shortAnimTime)
  207. .alpha(show ? 1 : 0)
  208. .setListener(new AnimatorListenerAdapter()
  209. {
  210. @Override
  211. public void onAnimationEnd(Animator animation)
  212. {
  213. mStatusView.setVisibility(show ? View.VISIBLE
  214. : View.INVISIBLE);
  215. }
  216. });
  217. ll_root.setVisibility(View.VISIBLE);
  218. ll_root.animate().setDuration(shortAnimTime).alpha(show ? 0 : 1)
  219. .setListener(new AnimatorListenerAdapter()
  220. {
  221. @Override
  222. public void onAnimationEnd(Animator animation)
  223. {
  224. ll_root.setVisibility(show ? View.INVISIBLE
  225. : View.VISIBLE);
  226. }
  227. });
  228. }
  229. else
  230. {
  231. // The ViewPropertyAnimator APIs are not available, so simply show
  232. // and hide the relevant UI components.
  233. mStatusView.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
  234. ll_root.setVisibility(show ? View.INVISIBLE : View.VISIBLE);
  235. }
  236. }
  237. // @Override
  238. // public void onActivityCreated(Bundle savedInstanceState)
  239. // {
  240. // FragmentManager fm = getFragmentManager();
  241. //
  242. // // Check to see if we have retained the worker fragment.
  243. // mdataFragment = (DataFragment)fm.findFragmentByTag("work");
  244. //
  245. // // If not retained (or first time running), we need to create it.
  246. // if (mdataFragment == null) {
  247. // mdataFragment = new DataFragment();
  248. // // Tell it who it is working with.
  249. // mdataFragment.setTargetFragment(this, 0);
  250. // fm.beginTransaction().add(mdataFragment, "work").commit();
  251. // }
  252. // super.onActivityCreated(savedInstanceState);
  253. // }
  254. private ShipMap mTrackingMap;
  255. private int mSelectTrackingPosition = -1;
  256. @Override
  257. public void onSaveInstanceState(Bundle outState) {
  258. super.onSaveInstanceState(outState);
  259. if (mContent != null) {
  260. outState.putString("content",mContent.toString());
  261. }
  262. outState.putInt("selectContactPosition",mSelectContactPosition);
  263. outState.putInt("selectTrackingPosition",mSelectTrackingPosition);
  264. }
  265. @Override
  266. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  267. Bundle savedInstanceState)
  268. {
  269. final Context context = getContext();
  270. /* mGestureDetector = new GestureDetector(this); */
  271. View view = null;
  272. if(this.getArguments().getString("action_type").equals("Tracing")||this.getArguments().getString("action_type").equals("Tracking")) {
  273. view = inflater.inflate(R.layout.detail_fragment_withmap, null);
  274. mTrackingMap = view.findViewById(R.id.tracking_map);
  275. } else {
  276. view = inflater.inflate(R.layout.detail_fragment, null);
  277. mTrackingMap = null;
  278. }
  279. if (savedInstanceState != null) {
  280. fragment_content = savedInstanceState.getString("content");
  281. mSelectContactPosition = savedInstanceState.getInt("selectContactPosition");
  282. mSelectTrackingPosition = savedInstanceState.getInt("selectTrackingPosition");
  283. }
  284. // if(this.getArguments().getString("action_type").equals("Tracing"))
  285. // {
  286. // LinearLayout ll1 = view.findViewById(R.id.detail_linear);
  287. //
  288. // ShipMap shipMap = new ShipMap(context);
  289. //// mShipMap = shipMap;
  290. //
  291. //
  292. // LinearLayout.LayoutParams mapLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0,1.0f);
  293. // mapLayoutParams.setMargins(commonUtil.dp2px(context,5),commonUtil.dp2px(context,2),commonUtil.dp2px(context,5),commonUtil.dp2px(context,2));
  294. //
  295. // ll1.addView(shipMap,mapLayoutParams);
  296. //
  297. // }
  298. sl_root = (ScrollView) view.findViewById(R.id.sl_root);
  299. /*
  300. * sl_root.setOnTouchListener(new View.OnTouchListener() {
  301. *
  302. * @Override public boolean onTouch(View v, MotionEvent event) { return
  303. * mGestureDetector.onTouchEvent(event); } });
  304. */
  305. ll_root = (LinearLayout) view.findViewById(R.id.ll_root);
  306. mStatusView = view.findViewById(R.id.status);
  307. LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_refresh);
  308. Button btn_refresh = (Button) ll.findViewById(R.id.btn_refresh);
  309. btn_refresh.setOnClickListener(new View.OnClickListener()
  310. {
  311. @Override
  312. public void onClick(View v)
  313. {
  314. LinearLayout ll = (LinearLayout) ll_root
  315. .findViewById(R.id.ll_refresh);
  316. ll.setVisibility(View.GONE);
  317. requestdata();
  318. }
  319. });
  320. // FragmentManager fm = getFragmentManager();
  321. // List<Fragment> fragments = fm.getFragments();
  322. // if (fragments != null) {
  323. // Log.e("DetailFragment", "fragments count=" + fragments.size());
  324. // // for (int i = 0; i < fragments.size(); i++)
  325. // // Log.e("DetailFragment", "fragments name=" + fragments.size());
  326. //
  327. // }
  328. // // this.getFragmentManager();
  329. // String tag = getArguments().getString("module_name");
  330. // // Check to see if we have retained the worker fragment.
  331. // mdataFragment = (DataFragment) fm.findFragmentById(12345);
  332. //
  333. // // If not retained (or first time running), we need to create it.
  334. // if (mdataFragment == null) {
  335. // Log.e("dataFragment creaded ", "tag=data_" + tag);
  336. // mdataFragment = new DataFragment();
  337. // // Tell it who it is working with.
  338. // mdataFragment.setTargetFragment(this, 0);
  339. // fm.beginTransaction().add(12345,mdataFragment, "data+" +
  340. // tag).commit();
  341. // }
  342. requestdata();
  343. return view;
  344. }
  345. protected JSONObject loadfakecontent(int rawid)
  346. {
  347. String jstring = "";
  348. try
  349. {
  350. InputStream in = getResources().openRawResource(rawid);
  351. // 获取文件的字节数
  352. int lenght = in.available();
  353. // 创建byte数组
  354. byte[] buffer = new byte[lenght];
  355. // 将文件中的数据读到byte数组中
  356. in.read(buffer);
  357. jstring = EncodingUtils.getString(buffer, "UTF-8");
  358. JSONObject jsobj = new JSONObject(jstring);
  359. return jsobj;
  360. // JSONObject objrecords = jsobj.getJSONObject("records");
  361. }
  362. catch (Exception e)
  363. {
  364. e.printStackTrace();
  365. }
  366. return null;
  367. }
  368. public JSONObject mContent;
  369. private int mSelectContactPosition = -1;
  370. void init(String jstr, LayoutInflater inflater)
  371. {
  372. String TAG = "init@DetailFragment";
  373. Log.d(TAG, jstr);
  374. try
  375. {
  376. JSONObject jsonObj = new JSONObject(jstr);
  377. int group_count = jsonObj.getInt("count");
  378. // if (getArguments().getString("action_type").equals("Detail")) {
  379. // JSONObject commObj = loadfakecontent(R.raw.fake_comm);
  380. // jsonObj.put("group" + group_count++,commObj);
  381. // jsonObj.put("count",group_count);
  382. // }
  383. mContent = jsonObj;
  384. if (group_count == 0)
  385. {
  386. TextView tv_empty = (TextView) ll_root
  387. .findViewById(R.id.tv_empty);
  388. tv_empty.setVisibility(View.VISIBLE);
  389. }
  390. for (int i = 0; i < group_count; i++)
  391. {
  392. // String key = (String) it.next();
  393. // if (key.equals("result"))
  394. // continue;
  395. final JSONObject groupobj = jsonObj.getJSONObject("group" + i);
  396. View v = inflater.inflate(R.layout.group_tag, null);
  397. // v.setOnClickListener(this);
  398. TextView tvgroup = (TextView) v.findViewById(R.id.tv_group);
  399. String group_name = groupobj.getString("_name");
  400. tvgroup.setText(group_name);
  401. v.setId(commonUtil.generateViewId());
  402. ll_root.addView(v);
  403. String grouptype = groupobj.getString("_type");
  404. v.setTag(grouptype);
  405. v.setOnTouchListener(new View.OnTouchListener() {
  406. @Override
  407. public boolean onTouch(View view, MotionEvent motionEvent) {
  408. touchedView = view;
  409. detector.onTouchEvent(motionEvent);
  410. return true;
  411. }
  412. });
  413. // groupobj.remove("_type");
  414. if (grouptype.equals("mapping"))
  415. {
  416. LinearLayout mappingview = (LinearLayout) inflater.inflate(
  417. R.layout.key_mapping, null);
  418. mappingview.setId(commonUtil.generateViewId());
  419. control.put(v.getId(), mappingview.getId());
  420. ll_root.addView(mappingview);
  421. int map_count = groupobj.getInt("count");
  422. for (int j = 0; j < map_count; j++)
  423. {
  424. // String keygroup = (String) itgroup.next();
  425. JSONObject itemobj = groupobj.getJSONObject("item" + j);
  426. final String key = (String) itemobj.keys().next();
  427. final String val = itemobj.getString(key);
  428. LinearLayout mappingitem = (LinearLayout) inflater
  429. .inflate(R.layout.keymapping_item, null);
  430. TextView tvkey = (TextView) mappingitem
  431. .findViewById(R.id.tv_key);
  432. tvkey.setText(key);
  433. // tvkey.setBackgroundResource(R.drawable.detail_item);
  434. TextView tvval = (TextView) mappingitem
  435. .findViewById(R.id.tv_value);
  436. tvval.setText(Html.fromHtml(val));
  437. // tvval.setBackgroundResource(R.drawable.detail_item);
  438. tvval.setOnTouchListener(new View.OnTouchListener() {
  439. @Override
  440. public boolean onTouch(View view, MotionEvent motionEvent) {
  441. copiedKey = key;
  442. copiedVal = val;
  443. detector.onTouchEvent(motionEvent);
  444. return true;
  445. }
  446. });
  447. mappingview.addView(mappingitem);
  448. }
  449. // Iterator itgroup = groupobj.keys();
  450. // while (itgroup.hasNext())
  451. // {
  452. //
  453. //
  454. // }
  455. }
  456. else if (grouptype.equals("table"))
  457. {
  458. String content = groupobj.getString("content");
  459. WebView wv;
  460. wv = (WebView) inflater.inflate(R.layout.web_content, null);
  461. wv.getSettings().setDefaultTextEncodingName("UTF-8");
  462. wv.setId(commonUtil.generateViewId());
  463. wv.setWebViewClient(new MyWebViewClient());
  464. // wv.setDownloadListener(new DownloadListener()
  465. // {
  466. //
  467. // @Override
  468. // public void onDownloadStart(String url,
  469. // String userAgent, String contentDisposition,
  470. // String mimetype, long contentLength)
  471. // {
  472. // if(!commonUtil.localdirExist())
  473. // {
  474. //
  475. // new AlertDialog.Builder(getActivity())
  476. // .setIconAttribute(android.R.attr.alertDialogIcon)
  477. // .setTitle(R.string.str_localdir_error)
  478. // .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  479. // public void onClick(DialogInterface dialog, int whichButton) {
  480. //
  481. // /* User clicked OK so do some stuff */
  482. // }
  483. // })
  484. // .create().show();
  485. // return;
  486. // }
  487. //// String filename = contentDisposition.replace("attachment;filename=\"", "");
  488. //// filename = filename.replace("\"", "");
  489. ////
  490. //// Log.d("WebView", "Download Action");
  491. //// Log.d("tag", "url=" + url);
  492. //// // Log.i("tag", "userAgent="+userAgent);
  493. //// // Log.i("tag",
  494. //// // "contentDisposition="+contentDisposition);
  495. //// // Log.i("tag", "mimetype="+mimetype);
  496. //// // Log.i("tag", "contentLength="+contentLength);
  497. //// // Uri uri = Uri.parse(url);
  498. //// // Intent intent = new Intent(Intent.ACTION_VIEW,
  499. //// // uri);
  500. //// // startActivity(intent);
  501. ////
  502. //// final DownloadManager downloadManager = (DownloadManager) ApexTrackingApplication
  503. //// .get_instance().getSystemService(
  504. //// Context.DOWNLOAD_SERVICE);
  505. ////
  506. //// Uri uri = Uri.parse(url);
  507. //// final Request request = new Request(uri);
  508. ////
  509. //// // 设置允许使用的网络类型,这里是移动网络和wifi都可以
  510. //// request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
  511. //// | DownloadManager.Request.NETWORK_WIFI);
  512. ////
  513. ////
  514. ////
  515. //// request.setDestinationInExternalPublicDir("Apex Mobile",filename);
  516. ////
  517. //// // 禁止发出通知,既后台下载,如果要使用这一句必须声明一个权限:android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
  518. //// // request.setShowRunningNotification(false);
  519. ////
  520. //// // 不显示下载界面
  521. //// request.setVisibleInDownloadsUi(false);
  522. //// request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  523. ////
  524. //// /*
  525. //// * 设置下载后文件存放的位置,如果sdcard不可用,那么设置这个将报错,
  526. //// * 因此最好不设置如果sdcard可用,下载后的文件 在/mnt/sdcard/Android/
  527. //// * data/packageName/files目录下面
  528. //// * ,如果sdcard不可用,设置了下面这个将报错,不设置,下载后的文件在/cache这个 目录下面
  529. //// */
  530. //// // request.setDestinationInExternalFilesDir(this,
  531. //// // null, "tar.apk");
  532. ////
  533. ////// getActivity();
  534. //// AlertDialog.Builder builder = new Builder(
  535. //// getActivity());
  536. //// builder.setMessage(getString(R.string.str_downloadmessage));
  537. ////
  538. //// builder.setTitle(getString(R.string.str_confirmdownload));
  539. ////
  540. //// builder.setPositiveButton(
  541. //// getString(R.string.str_download),
  542. //// new Dialog.OnClickListener()
  543. //// {
  544. ////
  545. //// @Override
  546. //// public void onClick(
  547. //// DialogInterface dialog,
  548. //// int which)
  549. //// {
  550. //// downloadManager.enqueue(request);
  551. //// dialog.dismiss();
  552. ////
  553. //// }
  554. //// });
  555. ////
  556. //// builder.setNegativeButton(
  557. //// getString(R.string.str_cancel),
  558. //// new Dialog.OnClickListener()
  559. //// {
  560. ////
  561. //// @Override
  562. //// public void onClick(
  563. //// DialogInterface dialog,
  564. //// int which)
  565. //// {
  566. //// dialog.dismiss();
  567. //// }
  568. //// });
  569. ////
  570. //// builder.create().show();
  571. //
  572. // }
  573. //
  574. // });
  575. wv.setTag(i);
  576. control.put(v.getId(), wv.getId());
  577. wv.loadData(content, mimeType, null);
  578. // wv.setOnTouchListener(new OnTouchListener()
  579. // {
  580. // @Override
  581. // public boolean onTouch(View v, MotionEvent event)
  582. // {
  583. //
  584. // if (event.getAction() == MotionEvent.ACTION_UP)
  585. // sl_root.requestDisallowInterceptTouchEvent(false);
  586. // else
  587. // sl_root.requestDisallowInterceptTouchEvent(true);
  588. //
  589. // return false;
  590. // }
  591. // });
  592. ll_root.addView(wv);
  593. Log.d("table content", content);
  594. }
  595. if (grouptype.equals("tracking"))
  596. {
  597. LinearLayout listview = (LinearLayout) inflater.inflate(
  598. R.layout.list_content, null);
  599. listview.setId(commonUtil.generateViewId());
  600. ViewGroup.LayoutParams lp=listview.getLayoutParams();
  601. control.put(v.getId(), listview.getId());
  602. ll_root.addView(listview);
  603. long list_count = groupobj.getInt("count");
  604. for (int j = 0; j < list_count; j++)
  605. {
  606. final JSONObject itemobj = groupobj.getJSONObject("item" + j);
  607. ConstraintLayout listitem = (ConstraintLayout) inflater
  608. .inflate(R.layout.detail_tracking_cell, null);
  609. ImageView icon = (ImageView) listitem
  610. .findViewById(R.id.iv_thumb);
  611. String iconname = itemobj.getString("icon");
  612. int rid = commonUtil.iconName2Rid(getActivity(),iconname);
  613. icon.setImageResource(rid);
  614. if(j!=0)
  615. {
  616. icon.setColorFilter(Color.GRAY);
  617. // ColorMatrix matrix = new ColorMatrix();
  618. // matrix.setSaturation(0);
  619. // ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
  620. // icon..image.setColorFilter(filter);
  621. }
  622. TextView tvitem = (TextView) listitem
  623. .findViewById(R.id.tv_title);
  624. String val = itemobj.getString("msg");
  625. tvitem.setText(val);
  626. // tvitem.setBackgroundResource(R.drawable.detail_item);
  627. // listitem.removeView(tvitem);
  628. int px=commonUtil.dp2px(getActivity(),56);
  629. listitem.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, px, 1));
  630. listview.addView(listitem);
  631. // ViewGroup.LayoutParams lp1=listitem.getLayoutParams();
  632. final int selectIndex = i;
  633. listitem.setOnClickListener(new OnClickListener() {
  634. @Override
  635. public void onClick(View v) {
  636. if (mTrackingMap != null) {
  637. JSONObject location = groupobj.optJSONObject("location");
  638. if (location != null) {
  639. mTrackingMap.showShipAnnotation(location);
  640. }
  641. mSelectTrackingPosition = selectIndex;
  642. }
  643. }
  644. });
  645. if (mSelectTrackingPosition == i) {
  646. if (mTrackingMap != null) {
  647. JSONObject location = groupobj.optJSONObject("location");
  648. if (location != null) {
  649. mTrackingMap.showShipAnnotation(location);
  650. }
  651. }
  652. }
  653. }
  654. }
  655. if (grouptype.equals("list"))
  656. {
  657. LinearLayout listview = (LinearLayout) inflater.inflate(
  658. R.layout.list_content, null);
  659. listview.setId(commonUtil.generateViewId());
  660. control.put(v.getId(), listview.getId());
  661. ll_root.addView(listview);
  662. long list_count = groupobj.getInt("count");
  663. for (int j = 0; j < list_count; j++)
  664. {
  665. RelativeLayout listitem = (RelativeLayout) inflater
  666. .inflate(R.layout.list_item, null);
  667. TextView tvitem = (TextView) listitem
  668. .findViewById(R.id.tv_item);
  669. String val = groupobj.getString("line" + j);
  670. tvitem.setText(val);
  671. // tvitem.setBackgroundResource(R.drawable.detail_item);
  672. listitem.removeView(tvitem);
  673. listview.addView(tvitem);
  674. }
  675. // Iterator itgroup = groupobj.keys();
  676. // while (itgroup.hasNext())
  677. // {
  678. // String keygroup = (String) itgroup.next();
  679. //
  680. // }
  681. }
  682. if (grouptype.equals("communication"))
  683. {
  684. LinearLayout listview = (LinearLayout) inflater.inflate(
  685. R.layout.list_content, null);
  686. listview.setId(commonUtil.generateViewId());
  687. control.put(v.getId(), listview.getId());
  688. ll_root.addView(listview);
  689. // 添加收件人和cc
  690. ConstraintLayout commu_header = (ConstraintLayout) inflater
  691. .inflate(R.layout.commu_item_header, null);
  692. TextView toTv = commu_header.findViewById(R.id.comm_to_tv);
  693. EditText ccTv = commu_header.findViewById(R.id.comm_cc_tv);
  694. ImageButton contactBtn = commu_header.findViewById(R.id.comm_contact_btn);
  695. ccTv.setTag("group_" + i + "_cc");
  696. String to = groupobj.optString("receipt");
  697. String cc = groupobj.optString("cc");
  698. toTv.setText(to);
  699. ccTv.setText(cc);
  700. TextFieldTextWatcher ccWatcher = new TextFieldTextWatcher(getContext(),ccTv,i,0);
  701. ccTv.addTextChangedListener(ccWatcher);
  702. final int selectIndex = i;
  703. contactBtn.setOnClickListener(new OnClickListener() {
  704. @Override
  705. public void onClick(View v) {
  706. mSelectContactPosition = selectIndex;
  707. Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
  708. startActivityForResult(intent, People_Pick_Code);
  709. }
  710. });
  711. listview.addView(commu_header);
  712. long list_count = groupobj.getInt("count");
  713. for (int j = 0; j < list_count; j++)
  714. {
  715. ConstraintLayout msgitem = (ConstraintLayout) inflater
  716. .inflate(R.layout.commu_item_msg, null);
  717. JSONObject msgObj = groupobj.getJSONObject("item" + j);
  718. String sender = msgObj.optString("Sender");
  719. String msg = msgObj.optString("msg");
  720. String time = msgObj.optString("mst_time");
  721. final String email_url = msgObj.optString("email_url");
  722. TextView senderTv = msgitem.findViewById(R.id.comm_sender_tv);
  723. TextView msgTv = msgitem.findViewById(R.id.comm_msg_tv);
  724. TextView timeTv = msgitem.findViewById(R.id.comm_time_tv);
  725. Button sendBtn = msgitem.findViewById(R.id.comm_send_msg_btn);
  726. senderTv.setText(sender);
  727. msgTv.setText(msg);
  728. timeTv.setText(time);
  729. sendBtn.setOnClickListener(new OnClickListener() {
  730. @Override
  731. public void onClick(View v) {
  732. Uri content_url = Uri.parse(email_url);
  733. Intent intent = new Intent(Intent.ACTION_VIEW,content_url);
  734. // Intent intent = new Intent();
  735. // intent.setAction("android.intent.action.VIEW");
  736. // intent.setData(content_url);
  737. startActivity(intent);
  738. }
  739. });
  740. if (email_url == null || email_url.length() == 0) {
  741. sendBtn.setVisibility(View.INVISIBLE);
  742. }
  743. listview.addView(msgitem);
  744. }
  745. //添加 消息输入和发送
  746. ConstraintLayout commu_footer = (ConstraintLayout) inflater
  747. .inflate(R.layout.commu_item_footer, null);
  748. EditText editTv = commu_footer.findViewById(R.id.comm_edit_tv);
  749. Button sendEmailBtn = commu_footer.findViewById(R.id.comm_send_email_btn);
  750. TextFieldTextWatcher editWatcher = new TextFieldTextWatcher(getContext(),editTv,i,1);
  751. editTv.addTextChangedListener(editWatcher);
  752. String edit_content = groupobj.optString("edit_content");
  753. editTv.setText(edit_content);
  754. final int groupIndex = i;
  755. sendEmailBtn.setOnClickListener(new OnClickListener() {
  756. @Override
  757. public void onClick(View v) {
  758. JSONObject commGroup = getGroup(groupIndex);
  759. final String cc = commGroup.optString("cc");
  760. final String content = commGroup.optString("edit_content");
  761. if (content == null || content.length() == 0) {
  762. new AlertDialog.Builder(mContext)
  763. .setTitle("Warning")
  764. .setMessage("The communication message that will be sent is blank")
  765. .setPositiveButton("Ok",null)
  766. .show();
  767. return;
  768. }
  769. if (cc != null && cc.length() > 0) {
  770. // 校验邮箱地址
  771. String[] ccArr = cc.split(";");
  772. for (int k = 0; k < ccArr.length; k++) {
  773. String ccStr = ccArr[k];
  774. if (ccStr.length() > 0) {
  775. boolean isEmail = validateEmail(ccStr);
  776. if (!isEmail) {
  777. new AlertDialog.Builder(mContext)
  778. .setTitle("Warning")
  779. .setMessage("The communication cc is not right")
  780. .setPositiveButton("Ok",null)
  781. .show();
  782. return;
  783. }
  784. }
  785. }
  786. }
  787. // send
  788. final ProgressDialog pd = new ProgressDialog(mContext);
  789. pd.setTitle(null);
  790. pd.setMessage("send email");
  791. pd.setCancelable(false);
  792. pd.show();
  793. final String serialNo = commGroup.optString("serial_no");
  794. new Thread(new Runnable() {
  795. @Override
  796. public void run() {
  797. final JSONObject response = Network.sendEmail(content,cc,serialNo);
  798. getActivity().runOnUiThread(new Runnable() {
  799. @Override
  800. public void run() {
  801. pd.dismiss();
  802. boolean showAlert = false;
  803. String msg = null;
  804. if (response != null) {
  805. int result = response.optInt("result",Network.RESULT_FALSE);
  806. if (result != Network.RESULT_TRUE) {
  807. showAlert = true;
  808. msg = response.optString("err_msg");
  809. }
  810. } else {
  811. showAlert = true;
  812. }
  813. if (msg == null) {
  814. msg = "Sorry,something wrong";
  815. }
  816. if (showAlert) {
  817. new AlertDialog.Builder(mContext)
  818. .setTitle(null)
  819. .setMessage(msg)
  820. .setPositiveButton("Ok",null)
  821. .show();
  822. }
  823. }
  824. });
  825. }
  826. }).start();
  827. }
  828. });
  829. listview.addView(commu_footer);
  830. listview.forceLayout();
  831. }
  832. // String value = obj.getString(key);
  833. // JSONArray array = obj.getJSONArray(key);
  834. // for(int i=0;i<array.length();i++){
  835. // JSONObject jsonobject = array.getJSONObject(i);
  836. // jsonobject.put("name", key);
  837. // jsonobject.put("exp", key+"="+jsonobject.getString("value"));
  838. // newArray.put(jsonobject);
  839. }
  840. // Iterator it = jsonObj.keys();
  841. // while (it.hasNext())
  842. // {
  843. //
  844. // }
  845. }
  846. catch (JSONException e)
  847. {
  848. // TODO Auto-generated catch block
  849. e.printStackTrace();
  850. }
  851. }
  852. private JSONObject getGroup(int group) {
  853. if (mContent != null) {
  854. JSONObject groupobj = mContent.optJSONObject("group" + group);
  855. return groupobj;
  856. }
  857. return null;
  858. }
  859. private boolean validateEmail(String email) {
  860. String emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  861. return Pattern.matches(emailRegex,email);
  862. }
  863. @Override
  864. public void onClick(View v)
  865. {
  866. View view = ll_root.findViewById(control.get(v.getId()));
  867. ImageView expand_iv = v.findViewById(R.id.group_expand_indicator);
  868. if (view.getVisibility() == View.VISIBLE) {
  869. expand_iv.setImageResource(R.drawable.ic_expand_less);
  870. view.setVisibility(View.GONE);
  871. }
  872. else {
  873. expand_iv.setImageResource(R.drawable.ic_expand_more);
  874. view.setVisibility(View.VISIBLE);
  875. }
  876. }
  877. @Override
  878. public void onDestroy()
  879. {
  880. if (m_task != null)
  881. m_task.cancel(true);
  882. super.onDestroy();
  883. }
  884. @Override
  885. public void onActivityResult(int requestCode, int resultCode, Intent data)
  886. {
  887. // ((NewDetailActivity) getActivity()).showlogin = false;
  888. super.onActivityResult(requestCode, resultCode, data);
  889. if (requestCode == People_Pick_Code) {
  890. Uri contactData = data.getData();
  891. Cursor c = mContext.getContentResolver().query(contactData, null, null, null, null);
  892. if (c.moveToFirst()) {
  893. String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
  894. Cursor cursor = mContext.getContentResolver().query(
  895. ContactsContract.CommonDataKinds.Email.CONTENT_URI,
  896. new String[]{ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.TYPE}, ContactsContract.Data.CONTACT_ID + "='" + contactId + "'", null, null);
  897. while(cursor.moveToNext()) {
  898. String email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
  899. String emailType = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
  900. email = email.replace("\r\n",";");
  901. email = email.replace("\r",";");
  902. email = email.replace("\n",";");
  903. String[] emailArr = email.split(";");
  904. for (int i = 0; i < emailArr.length; i++) {
  905. email = emailArr[i];
  906. if (email.length() > 0) {
  907. if (emailType != null && Integer.valueOf(emailType) == ContactsContract.CommonDataKinds.Email.TYPE_WORK) {
  908. appendContactEmail(email,mSelectContactPosition);
  909. return;
  910. }
  911. }
  912. }
  913. }
  914. }
  915. mSelectContactPosition = -1;
  916. }
  917. }
  918. @Override
  919. public void onDetach()
  920. {
  921. if (m_task != null)
  922. m_task.cancel(false);
  923. super.onDetach();
  924. }
  925. class SearchTask extends AsyncTask<Void, Void, Boolean>
  926. {
  927. int errorcode;
  928. @Override
  929. protected Boolean doInBackground(Void... params)
  930. {
  931. Log.d("SearchTask", "doInBackground");
  932. if (!Network.NetworkIsAvailable())
  933. {
  934. errorcode = Network.RESULT_NET_NOTAVAILABLE;
  935. return false;
  936. }
  937. String jstr = Network.get_detail(getArguments());
  938. if (jstr == null || jstr.length() <= 0)
  939. {
  940. // Log.d(TAG, "json is wrong");
  941. errorcode = Network.RESULT_NET_ERROR;
  942. return false;
  943. }
  944. // mdataFragment.put_result(jstr);
  945. fragment_content = jstr;
  946. NewDetailActivity activity = (NewDetailActivity) getActivity();
  947. activity.save_content(getArguments().getString("action_type"), jstr);
  948. errorcode = Network.RESULT_TRUE;
  949. return true;
  950. }
  951. @Override
  952. protected void onPostExecute(Boolean success)
  953. {
  954. Log.i("onPostExecute", "entry");
  955. m_task = null;
  956. showProgress(false);
  957. // if (ApexTrackingApplication.get_authorization() == false)
  958. // {
  959. // if(((DetailActivity) getActivity()).showlogin == true)
  960. // return;
  961. // ((DetailActivity) getActivity()).showlogin = true;
  962. // LinearLayout ll = (LinearLayout) ll_root
  963. // .findViewById(R.id.ll_refresh);
  964. // ll.setVisibility(View.VISIBLE);
  965. // Toast toast = Toast.makeText(
  966. // ApexTrackingApplication.get_instance(),
  967. // getText(R.string.msg_net_passwordchangedremote) , Toast.LENGTH_LONG);
  968. // toast.setGravity(Gravity.CENTER, 0, 0);
  969. // toast.show();
  970. // Intent i = new Intent();
  971. // i.setClass(getActivity(), ApexActivity.class);
  972. // startActivityForResult(i, REQUEST_LOGINACTIVITY);
  973. // return;
  974. // }
  975. switch (errorcode)
  976. {
  977. case Network.RESULT_NET_NOTAVAILABLE:
  978. {
  979. Toast toast = Toast.makeText(
  980. ApexTrackingApplication.get_instance(),
  981. getText(R.string.msg_connection_none),
  982. Toast.LENGTH_LONG);
  983. toast.setGravity(Gravity.CENTER, 0, 0);
  984. toast.show();
  985. break;
  986. }
  987. case Network.RESULT_NET_ERROR:
  988. {
  989. Toast toast = Toast.makeText(
  990. ApexTrackingApplication.get_instance(),
  991. getText(R.string.msg_net_error), Toast.LENGTH_LONG);
  992. toast.setGravity(Gravity.CENTER, 0, 0);
  993. toast.show();
  994. break;
  995. }
  996. case Network.RESULT_ERROR:
  997. // case Network.RESULT_RESPONSE_NULL:
  998. {
  999. Toast toast = Toast.makeText(
  1000. ApexTrackingApplication.get_instance(),
  1001. getText(R.string.msg_net_resulterror) + ":"
  1002. + errorcode, Toast.LENGTH_LONG);
  1003. toast.setGravity(Gravity.CENTER, 0, 0);
  1004. toast.show();
  1005. break;
  1006. }
  1007. default:
  1008. break;
  1009. }
  1010. if (success)
  1011. {
  1012. LayoutInflater inflater = (LayoutInflater) ApexTrackingApplication
  1013. .get_instance().getSystemService(
  1014. Context.LAYOUT_INFLATER_SERVICE);
  1015. init(fragment_content, inflater/*
  1016. * getActivity().
  1017. * getLayoutInflater()
  1018. */);
  1019. }
  1020. else
  1021. {
  1022. LinearLayout ll = (LinearLayout) ll_root
  1023. .findViewById(R.id.ll_refresh);
  1024. ll.setVisibility(View.VISIBLE);
  1025. }
  1026. super.onPostExecute(success);
  1027. }
  1028. @Override
  1029. protected void onCancelled()
  1030. {
  1031. m_task = null;
  1032. showProgress(false);
  1033. }
  1034. }
  1035. private void showQuickLook(final String url) {
  1036. showProgressDialog("Please wait","Downloading...");
  1037. new Thread(new Runnable() {
  1038. @Override
  1039. public void run() {
  1040. String cacheDir = ApexTrackingApplication.getInstance().getExternalCacheDir().getAbsolutePath();//ApexTrackingApplication.getInstance().getDocumentDir();
  1041. final Bundle param = new Bundle();
  1042. // 下载文件
  1043. final File downloadFile = Network.downloadFile(param,url,cacheDir);
  1044. getActivity().runOnUiThread(new Runnable() {
  1045. @Override
  1046. public void run() {
  1047. progressDialog.dismiss();
  1048. if (downloadFile != null && downloadFile.exists()) {
  1049. String email = null,email_subject = null,email_content = null;
  1050. //此处需要新增保存文档的本地表
  1051. try {
  1052. email = mContent.getString("email");
  1053. email_subject = mContent.getString("email_subject");
  1054. email_content = mContent.getString("email_content");
  1055. } catch (JSONException e) {
  1056. e.printStackTrace();
  1057. }
  1058. // email=param.getString("email");
  1059. // email_subject=param.getString("email_subject");
  1060. // email_content=param.getString("email_content");
  1061. previewPDF(downloadFile.getAbsolutePath(),email,email_subject,email_content);
  1062. } else {
  1063. showAlert("Sorry,there is a wrong.");
  1064. }
  1065. }
  1066. });
  1067. }
  1068. }).start();
  1069. }
  1070. // public class DataFragment extends Fragment {
  1071. //
  1072. // String result = null;
  1073. //
  1074. // @Override
  1075. // public void onCreate(Bundle savedInstanceState) {
  1076. // setRetainInstance(true);
  1077. // super.onCreate(savedInstanceState);
  1078. // }
  1079. //
  1080. // public String get_result() {
  1081. // return result;
  1082. //
  1083. // }
  1084. //
  1085. // public void put_result(String s) {
  1086. // result = s;
  1087. // }
  1088. //
  1089. // }
  1090. public void showAlert(String msg) {
  1091. if (msg == null || msg.isEmpty()) {
  1092. return;
  1093. }
  1094. new android.support.v7.app.AlertDialog.Builder(mContext)
  1095. .setMessage(msg)
  1096. .show();
  1097. }
  1098. protected void previewPDF(String file,String email,String subject,String content) {
  1099. Intent myIntent = new Intent();
  1100. myIntent = new Intent(getActivity(), PDFPreviewActivity.class);
  1101. myIntent.putExtra("file",file);
  1102. myIntent.putExtra("iscache",true);
  1103. try {
  1104. JSONObject json = new JSONObject();
  1105. // email = "66666@qq.com";
  1106. // subject = "test subject";
  1107. // content = "test body";
  1108. if (email != null) {
  1109. json.put("email",email);
  1110. }
  1111. if (subject != null) {
  1112. json.put("subject",subject);
  1113. }
  1114. if (content != null) {
  1115. json.put("content",content);
  1116. }
  1117. myIntent.putExtra("email",json.toString());
  1118. } catch (JSONException e) {
  1119. e.printStackTrace();
  1120. }
  1121. startActivity(myIntent);
  1122. }
  1123. public void download(String url) {
  1124. final String download_url = url;
  1125. showQuickLook(url);
  1126. if(true)
  1127. return;
  1128. showProgressDialog("Please wait","Downloading...");
  1129. new Thread(new Runnable() {
  1130. @Override
  1131. public void run() {
  1132. // String cacheDir = ApexTrackingApplication.getInstance().getDocumentDir();
  1133. // 下载文件
  1134. final File downloadFile = Network.downloadFile(null,download_url,null);
  1135. getActivity().runOnUiThread(new Runnable() {
  1136. @Override
  1137. public void run() {
  1138. progressDialog.dismiss();
  1139. RAUtil.openFile(getActivity(),downloadFile);
  1140. }
  1141. });
  1142. }
  1143. }).start();
  1144. }
  1145. public class MyWebViewClient extends WebViewClient
  1146. {
  1147. // 如果页面中链接,如果希望点击链接继续在当前browser中响应,
  1148. // 而不是新开Android的系统browser中响应该链接,必须覆盖 webview的WebViewClient对象。
  1149. public boolean shouldOverviewUrlLoading(WebView view, String url)
  1150. {
  1151. Log.d("Web Client", "shouldOverviewUrlLoading: " + url);
  1152. // L.i("shouldOverviewUrlLoading");
  1153. view.loadUrl(url);
  1154. return true;
  1155. }
  1156. @Override
  1157. public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
  1158. Log.d("Web Client", "shouldOverrideUrlLoading: " + request);
  1159. return super.shouldOverrideUrlLoading(view, request);
  1160. }
  1161. public void onPageStarted(WebView view, String url, Bitmap favicon)
  1162. {
  1163. // L.i("onPageStarted");
  1164. // showProgress();
  1165. Log.d("Web Client", "onPageStarted: ");
  1166. }
  1167. public void onPageFinished(WebView view, String url)
  1168. {
  1169. // L.i("onPageFinished");
  1170. // closeProgress();
  1171. Log.d("Web Client", "onPageFinished: ");
  1172. }
  1173. public void onReceivedError(WebView view, int errorCode,
  1174. String description, String failingUrl)
  1175. {
  1176. // L.i("onReceivedError");
  1177. // closeProgress();
  1178. Log.d("Web Client", "onReceivedError: ");
  1179. }
  1180. @Override
  1181. public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
  1182. Log.d("Web Client", "shouldInterceptRequest: " + url);
  1183. if (url.startsWith("http") && url.contains("action_type=download_doc")) {
  1184. download(url);
  1185. String content = contentOfWebView(view);
  1186. WebResourceResponse response = new WebResourceResponse("text/html",
  1187. "utf-8",
  1188. new ByteArrayInputStream(content.getBytes()));
  1189. return response;
  1190. } else {
  1191. return super.shouldInterceptRequest(view,url);
  1192. }
  1193. }
  1194. String contentOfWebView(WebView webView) {
  1195. int i = (int)webView.getTag();
  1196. try {
  1197. JSONObject groupobj = mContent.getJSONObject("group" + i);
  1198. String content = groupobj.getString("content");
  1199. return content;
  1200. } catch (JSONException e) {
  1201. e.printStackTrace();
  1202. }
  1203. return null;
  1204. }
  1205. // @Override
  1206. // public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
  1207. // Log.d("Web Client", "shouldInterceptRequest: " + request);
  1208. //
  1209. // if (request.getUrl().toString().contains("action_type=download_doc")) {
  1210. // return null;
  1211. // } else {
  1212. // return super.shouldInterceptRequest(view,request);
  1213. // }
  1214. //
  1215. //
  1216. // }
  1217. }
  1218. private void appendContactEmail(String email,int group) {
  1219. try {
  1220. String groupKey = "group" + group;
  1221. JSONObject groupObj = mContent.getJSONObject(groupKey);
  1222. String groupType = groupObj.getString("_type");
  1223. if (groupType.equals("communication")) {
  1224. String oldCC = groupObj.optString("cc");
  1225. if (oldCC == null || oldCC.length() == 0) {
  1226. oldCC = email + ";";
  1227. } else {
  1228. if (oldCC.substring(oldCC.length() - 1).equals(";")) {
  1229. oldCC = oldCC + email + ";";
  1230. } else {
  1231. oldCC = oldCC + ";" + email + ";";
  1232. }
  1233. }
  1234. commContentChange(oldCC,group,0);
  1235. EditText ccTv = ll_root.findViewWithTag("group_" + group + "_cc");
  1236. if (ccTv != null) {
  1237. ccTv.setText(oldCC);
  1238. ccTv.requestFocus();
  1239. ccTv.setSelection(oldCC.length());
  1240. }
  1241. }
  1242. } catch (JSONException e) {
  1243. e.printStackTrace();
  1244. }
  1245. }
  1246. private void commContentChange(String content,int group,int child) {
  1247. try {
  1248. String groupKey = "group" + group;
  1249. JSONObject groupObj = mContent.getJSONObject(groupKey);
  1250. String groupType = groupObj.getString("_type");
  1251. if (groupType.equals("communication")) {
  1252. if (child == 0) {
  1253. groupObj.put("cc",content);
  1254. } else if (child == 1) {
  1255. groupObj.put("edit_content",content);
  1256. }
  1257. mContent.put(groupKey,groupObj);
  1258. }
  1259. } catch (JSONException e) {
  1260. e.printStackTrace();
  1261. }
  1262. }
  1263. public class TextFieldTextWatcher implements TextWatcher {
  1264. Context mCtx;
  1265. int group;
  1266. int child;
  1267. EditText textView;
  1268. public TextFieldTextWatcher(Context ctx,EditText textView,int group,int child) {
  1269. this.mCtx = ctx;
  1270. this.textView = textView;
  1271. this.group = group;
  1272. this.child = child;
  1273. }
  1274. String origin_text;
  1275. String string;
  1276. int start_position;
  1277. int change_length; // 修改原文长度
  1278. int new_string_length;
  1279. /**
  1280. * s 改变前的内容
  1281. * start 增加内容前光标位置,删除内容后光标位置 (起始位置)
  1282. * count 选中s中的内容长度(发生改变的原内容长度),未选中为0
  1283. * after 新添加的内容长度,删除为0
  1284. * */
  1285. @Override
  1286. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  1287. // Log.d("TextChange", "before Text Changed: " + s + " Start: " + start + " Count: " + count + " After: " + after);
  1288. origin_text = s.toString();
  1289. start_position = start;
  1290. change_length = count;
  1291. new_string_length = after;
  1292. }
  1293. /**
  1294. * s 改变后的内容
  1295. * start 增加内容前光标位置,删除内容后光标位置 (起始位置)
  1296. * before 选中s中的内容长度(发生改变的原内容长度),未选中为0
  1297. * count 增加内容长度,删除内容为0
  1298. * */
  1299. @Override
  1300. public void onTextChanged(CharSequence s, int start, int before, int count) {
  1301. // Log.d("TextChange", "on Text Changed: " + s + " Start: " + start + " Before: " + before + " Count: " + count);
  1302. }
  1303. @Override
  1304. public void afterTextChanged(Editable s) {
  1305. commContentChange(s.toString(),group,child);
  1306. }
  1307. }
  1308. }