DetailActivity.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. package com.usai.apex;
  2. import android.app.AlertDialog;
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.os.Bundle;
  7. import android.support.v4.app.Fragment;
  8. import android.support.v4.app.FragmentTransaction;
  9. import android.support.v7.app.ActionBar;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.text.TextUtils;
  12. import android.util.Log;
  13. import android.view.Gravity;
  14. import android.view.KeyEvent;
  15. import android.view.LayoutInflater;
  16. import android.view.Menu;
  17. import android.view.MenuItem;
  18. import android.view.View;
  19. import android.widget.EditText;
  20. import android.widget.FrameLayout;
  21. import android.widget.TabHost;
  22. import android.widget.TabWidget;
  23. import android.widget.TextView;
  24. import com.usai.apex.Result.AMResultActivity;
  25. import com.usai.apex.mainframe.RootActivity;
  26. import com.usai.util.commonUtil;
  27. import com.usai.util.dbUtil;
  28. import org.json.JSONException;
  29. import org.json.JSONObject;
  30. import java.util.HashMap;
  31. import java.util.Iterator;
  32. import java.util.LinkedHashMap;
  33. import java.util.List;
  34. public class DetailActivity extends AppCompatActivity implements
  35. TabHost.TabContentFactory
  36. {
  37. boolean showlogin = false;
  38. // int radomid = 1 + (int) (Math.random() * 15);
  39. TabHost mTabHost;
  40. LinkedHashMap<String, Integer> tabmap = new LinkedHashMap<String, Integer>();
  41. HashMap<String,String> contentmap = new HashMap<String ,String>();
  42. // HashMap<String, DetailFragment> fragments = new HashMap<String,
  43. // DetailFragment>();
  44. String function_name;
  45. String _id;
  46. String cargo_criterion;
  47. int criterion_type;
  48. String email_to = null,email_subject = null,email_content = null;
  49. TextView mtitleview;
  50. // @Override
  51. // public void setTitle(CharSequence title) {
  52. //// View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null);
  53. ////
  54. //// TextView mtitleviewtitleview = mActionBarView.findViewById(R.id.title);
  55. //
  56. // if(title==null)
  57. // title="";
  58. // if(mtitleview!=null)
  59. // mtitleview.setText(title);
  60. // }
  61. private void setCustomActionBar() {
  62. ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
  63. View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null);
  64. mtitleview= mActionBarView.findViewById(R.id.title);
  65. function_name = getIntent().getStringExtra("function_name");
  66. String title = "";
  67. if(function_name.equals("Ocean Booking"))
  68. title="Booking Detail";
  69. else if(function_name.equals("Ocean B/L info."))
  70. title="B/L info. Detail";
  71. else if(function_name.equals("Container detail"))
  72. title="Container Detail";
  73. else if(function_name.equals("Cargo Tracking"))
  74. title="Cargo Detail";
  75. mtitleview.setText(title);
  76. setTitle(title);
  77. //
  78. // mActionBarView.setBackgroundColor(Color.YELLOW);
  79. // titleview.setBackgroundColor(Color.BLUE);
  80. ActionBar actionBar = getSupportActionBar();
  81. actionBar.setCustomView(mActionBarView, lp);
  82. // actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  83. // actionBar.setDisplayShowCustomEnabled(true);
  84. actionBar.setDisplayHomeAsUpEnabled(true);
  85. // actionBar.setIcon(getNumberDrawable());
  86. // actionBar.setDisplayShowHomeEnabled(true);
  87. actionBar.setDisplayShowTitleEnabled(true);
  88. }
  89. public DetailActivity get_instance()
  90. {
  91. return this;
  92. }
  93. // String email_to = null,email_subject = null,email_content = null;
  94. public void save_content(String which,String content)
  95. {
  96. contentmap.put(which, content);
  97. try {
  98. JSONObject jsonObject = new JSONObject(content);
  99. email_to = jsonObject.getString("email");
  100. email_subject = jsonObject.getString("email_subject");
  101. email_content = jsonObject.getString("email_content");
  102. } catch (JSONException e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. @Override
  107. protected void onCreate(Bundle savedInstanceState)
  108. {
  109. Log.d("DetailActivity", "onCreate");
  110. super.onCreate(savedInstanceState);
  111. setContentView(R.layout.activity_detail);
  112. setCustomActionBar();
  113. List<Fragment> fragments = getSupportFragmentManager().getFragments();
  114. if (fragments != null)
  115. {
  116. Log.d("DetailActivity", "fragments count=" + fragments.size());
  117. // for (int i = 0; i < fragments.size(); i++)
  118. // Log.e("DetailFragment", "fragment name="
  119. // + fragments.get(i).getTag());
  120. }
  121. if (savedInstanceState != null)
  122. {
  123. tabmap = (LinkedHashMap<String, Integer>) savedInstanceState
  124. .getSerializable("tabmap");
  125. contentmap = (HashMap<String, String>) savedInstanceState.getSerializable("contentmap");
  126. }
  127. else
  128. {
  129. int count = getIntent().getIntExtra("actions_count", 0);
  130. for (int i = 0; i < count; i++)
  131. {
  132. int vid = commonUtil.generateViewId();
  133. tabmap.put(getIntent().getStringExtra("action" + i), vid);
  134. }
  135. }
  136. function_name = getIntent().getStringExtra("function_name");
  137. if(function_name.equals("Ocean Booking"))
  138. setTitle("Booking Detail");
  139. else if(function_name.equals("Ocean B/L info."))
  140. setTitle("B/L info. Detail");
  141. else if(function_name.equals("Container detail"))
  142. setTitle("Container Detail");
  143. // else if(function_name.equals("Cargo Tracking"))
  144. // setTitle("Cargo Detail");
  145. //
  146. if (function_name.equals("Cargo Tracking"))
  147. {
  148. criterion_type = getIntent().getIntExtra("criterion_type", 0);
  149. cargo_criterion = getIntent().getStringExtra("cargo_criterion");
  150. setTitle("Cargo Detail");
  151. }
  152. _id = getIntent().getStringExtra("_id");
  153. setupTabs();
  154. }
  155. // @Override
  156. // protected void onSaveInstanceState(Bundle outState)
  157. // {
  158. // Iterator<String> iter=fragments.keySet().iterator();
  159. //
  160. // while (iter.hasNext())
  161. // {
  162. // String tag = (String) iter.next();
  163. // getSupportFragmentManager().putFragment(outState,tag,fragments.get(tag));
  164. //
  165. // }
  166. // outState.putStringArray("tags", fragments.keySet().toArray(new
  167. // String[0]));
  168. // Log.d("onSaveInstanceState","save fragments" +
  169. // fragments.keySet().size());
  170. // super.onSaveInstanceState(outState);
  171. // }
  172. //
  173. // @Override
  174. // protected void onRestoreInstanceState(Bundle savedInstanceState)
  175. // {
  176. // String [] tags =savedInstanceState.getStringArray("tags");
  177. // Log.d("onRestoreInstanceState","load fragments" + tags.length);
  178. // for(int i=0;i<tags.length;i++)
  179. // {
  180. // Log.d("onRestoreInstanceState","load fragment" + tags[i]);
  181. // fragments.put(tags[i],(DetailFragment)
  182. // getSupportFragmentManager().getFragment(savedInstanceState,tags[i]));
  183. // }
  184. //
  185. // super.onRestoreInstanceState(savedInstanceState);
  186. // }
  187. /** {@inheritDoc} */
  188. public View createTabContent(String tag)
  189. {
  190. FrameLayout fl = new FrameLayout(this);
  191. fl.setId(tabmap.get(tag));
  192. Fragment f;
  193. Bundle bundle = new Bundle();
  194. if (function_name.equals("Cargo Tracking"))
  195. {
  196. if (criterion_type == 0)
  197. bundle.putString("hbol", cargo_criterion);
  198. else
  199. bundle.putString("container_no", cargo_criterion);
  200. }
  201. bundle.putString("action_type", tag);
  202. bundle.putString("id", _id);
  203. bundle.putString("module_name", function_name);
  204. // Log.e("findFragmentByTag", "tag=" + tag + ", isnull= "
  205. // + (getSupportFragmentManager().findFragmentByTag(tag) == null));
  206. f = getSupportFragmentManager().findFragmentById(tabmap.get(tag));
  207. if (f == null)
  208. {
  209. Log.d("createTabContent", "create fragment" + tag);
  210. f = new DetailFragment();
  211. f.setArguments(bundle);
  212. }
  213. ((DetailFragment)f).set_content( contentmap.get(tag));
  214. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  215. ft.replace(fl.getId(), f);
  216. ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  217. ft.addToBackStack(tag);
  218. ft.commit();
  219. return fl;
  220. }
  221. @Override
  222. protected void onSaveInstanceState(Bundle outState)
  223. {
  224. outState.putSerializable("tabmap", tabmap);
  225. outState.putSerializable("contentmap", contentmap);
  226. super.onSaveInstanceState(outState);
  227. }
  228. // TabHost mTabHost;
  229. // private FrameLayout mTabContent;
  230. // @Override
  231. // public boolean onKeyDown(int keyCode, KeyEvent event)
  232. // {
  233. // if (keyCode == KeyEvent.KEYCODE_BACK)
  234. // {
  235. // // Intent myIntent = new Intent();
  236. // // myIntent = new Intent(EditActivity.this, tabActivity.class);
  237. // // startActivity(myIntent);
  238. // finish();
  239. // return true;
  240. // }
  241. // return super.onKeyDown(keyCode, event);
  242. // }
  243. @Override
  244. public boolean onKeyUp(int keyCode, KeyEvent event)
  245. {
  246. if (keyCode == KeyEvent.KEYCODE_BACK)
  247. {
  248. // Intent myIntent = new Intent();
  249. // myIntent = new Intent(EditActivity.this, tabActivity.class);
  250. // startActivity(myIntent);
  251. finish();
  252. return true;
  253. }
  254. return super.onKeyUp(keyCode, event);
  255. }
  256. //
  257. // @Override
  258. // protected void onCreate(Bundle savedInstanceState)
  259. // {
  260. // super.onCreate(savedInstanceState);
  261. // setContentView(R.layout.activity_detail);
  262. // // Show the Up button in the action bar.
  263. // setupActionBar();
  264. // setupTabs();
  265. // }
  266. //
  267. // /**
  268. // * Set up the {@link android.app.ActionBar}.
  269. // */
  270. // private void setupActionBar()
  271. // {
  272. //
  273. // getActionBar().setDisplayHomeAsUpEnabled(true);
  274. //
  275. // }
  276. //
  277. // // private View createTabIndicatorView(ViewGroup parent, CharSequence
  278. // label, Drawable icon) {
  279. // // final LayoutInflater inflater = LayoutInflater.from(this);
  280. // // final View tabIndicator = inflater.inflate(R.layout.tab_indicator,
  281. // parent, false);
  282. // //
  283. // //// final TextView tv = (TextView)
  284. // tabIndicator.findViewById(R.id.tab_title);
  285. // //// tv.setText(label);
  286. // ////
  287. // //// final ImageView iconView = (ImageView)
  288. // tabIndicator.findViewById(R.id.tab_icon);
  289. // //// iconView.setImageDrawable(icon);
  290. // //
  291. // // return tabIndicator;
  292. // // }
  293. //
  294. // @Override
  295. // public View createTabContent(String tag)
  296. // {
  297. // final TextView tv = new TextView(this);
  298. // tv.setText("Content for tab with tag " + tag);
  299. // return tv;
  300. // }
  301. private void setupTabs()
  302. {
  303. mTabHost = (TabHost) findViewById(R.id.tabhost);
  304. // mTabContent = (FrameLayout) findViewById(android.R.id.tabcontent);
  305. // FrameLayout tab1 = new FrameLayout(this);
  306. mTabHost.setup();
  307. Iterator<String> iter = tabmap.keySet().iterator();
  308. while (iter.hasNext())
  309. {
  310. String name = (String) iter.next();
  311. mTabHost.addTab(mTabHost.newTabSpec(name).setIndicator(name)
  312. .setContent(this));
  313. // String val = (String) tabmap.get(field);
  314. // parms.putString(field, val);
  315. // // Cursor c = (Cursor) m_listadapter.getItem(key);
  316. // Log.d("@@@@@@@@@", field + " : " + val);
  317. }
  318. TabWidget tabWidget=mTabHost.getTabWidget();
  319. for (int i = 0; i < tabWidget.getChildCount(); i++) {
  320. tabWidget.getChildAt(i).getLayoutParams().height = commonUtil.dp2px(this,39);
  321. tabWidget.getChildAt(i).setPadding(24,0,24,0);
  322. // tabWidget.getChildAt(i).getLayoutParams().height = 60;
  323. TextView tv=(TextView)tabWidget.getChildAt(i).findViewById(android.R.id.title);
  324. // tv.setGravity(BIND_AUTO_CREATE);
  325. // tv.setPadding(10, 10,10, 10);
  326. tv.setTextSize(12);//设置字体的大小;
  327. // tv.setTextColor(Color.WHITE);//设置字体的颜色;
  328. //获取tabs图片;
  329. // ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
  330. }
  331. // for (int i=1; i <= tabmap.size(); i++) {
  332. // String name = tabmap.keySet().iterator().;
  333. // mTabHost.addTab(mTabHost.newTabSpec(name)
  334. // .setIndicator(name)
  335. // .setContent(this));
  336. // }
  337. // // if(true)
  338. // // return;
  339. //
  340. // // View tab1 = createTabIndicatorView(mTabHost.getTabWidget(),
  341. // // "Login",null);
  342. // // tabhost.addTab(
  343. // // tabhost.newTabSpec("tab" + i).setIndicator(tab)
  344. // // .setContent(Mytabfirst.this)); tabhost.setCurrentTab(i);
  345. // // i++; tab = null;
  346. //
  347. // // // ��ɵײ��Զ�����ʽ�İ�ť
  348. // // String[] title = new String[] { "Login", "Not login" };
  349. // // int[] tabIds = new int[] { R.id.tab1, R.id.tab2 };
  350. // mTabHost.addTab(mTabHost.newTabSpec("Login").setIndicator("Login")
  351. // .setContent(this));
  352. // //
  353. // mTabHost.addTab(mTabHost.newTabSpec("Direct Tracking").setIndicator("Direct Tracking").setContent(R.id.tab2));
  354. // mTabHost.addTab(mTabHost.newTabSpec("Service Location")
  355. // .setIndicator("Service Location").setContent(this));
  356. // FragmentManager manager = this.getFragmentManager();
  357. // Instantiate a new fragment.
  358. // Fragment loginFragment = new LoginFragment();
  359. // // Fragment dtFragement = new DirectTrackingFragment();
  360. // Fragment slFragment = new ServiceLocationFragment();
  361. // // Add the fragment to the activity, pushing this transaction
  362. // // on to the back stack.
  363. //
  364. // FragmentTransaction ft =
  365. // getSupportFragmentManager().beginTransaction();
  366. // ft.replace(mTabHost.getTabContentView().getChildAt(0).getId(),
  367. // loginFragment);
  368. // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  369. // ft.addToBackStack("Login");
  370. //
  371. // // ft.replace(R.id.tab2, dtFragement);
  372. // // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  373. // // ft.addToBackStack("Tracking");
  374. //
  375. // ft.replace(R.id.tab3, (Fragment) slFragment);
  376. // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  377. // ft.addToBackStack("Location");
  378. //
  379. // ft.commit();
  380. // if (manager.findFragmentByTag(tabId) == null)
  381. // {
  382. // FragmentTransaction trans = manager.beginTransaction();
  383. // trans.replace(contentViewID, frag, tabId);
  384. // trans.commit();
  385. // }
  386. // for (int i = 0; i < title.length; i++) {
  387. // Button button = new Button(this);
  388. // button.setText(title[i]);
  389. // button.setBackgroundDrawable(this.getResources().getDrawable(
  390. // R.drawable.tab_lable)); //�Զ��尴ť��ʽ
  391. // mTabHost.addTab(mTabHost.newTabSpec(title[i]).setIndicator(button)
  392. // .setContent(tabIds[i]));
  393. // }
  394. // mTabHost.setOnTabChangedListener(this);
  395. }
  396. @Override
  397. public boolean onCreateOptionsMenu(Menu menu)
  398. {
  399. // Inflate the menu; this adds items to the action bar if it is present.
  400. if (function_name.equals("Cargo Tracking")) {
  401. getMenuInflater().inflate(R.menu.cargo_menu, menu);
  402. } else {
  403. getMenuInflater().inflate(R.menu.detail, menu);
  404. }
  405. return true;
  406. }
  407. @Override
  408. public boolean onOptionsItemSelected(MenuItem item)
  409. {
  410. switch (item.getItemId())
  411. {
  412. case R.id.action_close:
  413. finish();
  414. // if (SearchResultActivity.instance != null) {
  415. // SearchResultActivity.instance.finish();
  416. // }
  417. // if (SearchListActivity.instance != null) {
  418. // SearchListActivity.instance.finish();
  419. // }
  420. Intent intent = new Intent(this, RootActivity.class);
  421. startActivity(intent);
  422. break;
  423. case android.R.id.home:
  424. finish();
  425. break;
  426. case R.id.action_addto_favorite:
  427. // LayoutInflater factory = LayoutInflater.from(this);
  428. // final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
  429. final View edit = new EditText(this);
  430. new AlertDialog.Builder(this)
  431. .setIconAttribute(android.R.attr.alertDialogIcon)
  432. .setTitle(R.string.str_createname)
  433. .setView(edit)
  434. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  435. public void onClick(DialogInterface dialog, int whichButton) {
  436. String name = ((EditText)edit).getText().toString();
  437. if(TextUtils.isEmpty(name))
  438. {
  439. new android.app.AlertDialog.Builder(DetailActivity.this)
  440. .setTitle("Warning")
  441. .setMessage("Name can not be empty.")
  442. .setPositiveButton("OK", new DialogInterface.OnClickListener() {
  443. @Override
  444. public void onClick(DialogInterface dialog, int which) {
  445. }
  446. })
  447. .show();
  448. return;
  449. }
  450. String param = getIntent().toUri(0);
  451. SQLiteDatabase db = dbUtil.OpenDB(
  452. ApexTrackingApplication.get_instance(), null, true);
  453. db.execSQL("insert into favorites(name,params,user,create_time,module_name) values('"
  454. + name
  455. + "','"
  456. + param
  457. + "','"
  458. + ApexTrackingApplication.get_user()
  459. + "',"
  460. + System.currentTimeMillis()
  461. + ",'"
  462. +getIntent().getStringExtra("function_name")+ "')");
  463. dbUtil.CloseDB(db);
  464. /* User clicked OK so do some stuff */
  465. }
  466. })
  467. .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
  468. public void onClick(DialogInterface dialog, int whichButton) {
  469. /* User clicked cancel so do some stuff */
  470. }
  471. })
  472. .create().show();
  473. return true;
  474. case R.id.action_search_document: {
  475. String cargo_str = contentmap.get("Tracking");
  476. try {
  477. JSONObject cargo_json = new JSONObject(cargo_str);
  478. String hbol = cargo_json.optString("hbol");
  479. if (hbol != null && hbol.length() > 0) {
  480. Bundle parms = new Bundle();
  481. parms.putString("bol",hbol);
  482. parms.putString("module_name","Download Document");
  483. Intent resultIntent = new Intent(this, AMResultActivity.class);
  484. resultIntent.putExtra("query_params",parms);
  485. startActivity(resultIntent);
  486. } else {
  487. showAlter("There is no hbol to search");
  488. }
  489. } catch (JSONException e) {
  490. e.printStackTrace();
  491. showAlter("There is no hbol to search");
  492. }
  493. }
  494. break;
  495. case R.id.action_share_detail: {
  496. Intent share = new Intent();
  497. share.setAction(Intent.ACTION_SEND);
  498. // share.putExtra(Intent.EXTRA_STREAM, uri);
  499. share.setType("text/plain");
  500. if (email_content != null) {
  501. share.putExtra(Intent.EXTRA_TEXT, email_content); //附带的说明信息
  502. }
  503. if (email_subject != null) {
  504. share.putExtra(Intent.EXTRA_SUBJECT, email_subject);
  505. }
  506. if (email_to != null) {
  507. share.putExtra(Intent.EXTRA_EMAIL, email_to.split(","));
  508. }
  509. // share.putExtra(Intent.EXTRA_CC, new String[]{"ray.zhang@united-cn.net"});
  510. // startActivity(Intent.createChooser(share,getString(R.string.str_sendto)));
  511. startActivity(Intent.createChooser(share, "Share"));
  512. }
  513. break;
  514. }
  515. return super.onOptionsItemSelected(item);
  516. }
  517. public void showAlter(String msg) {
  518. new AlertDialog.Builder(this)
  519. .setMessage(msg)
  520. .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  521. @Override
  522. public void onClick(DialogInterface dialogInterface, int i) {
  523. }
  524. })
  525. .show();
  526. }
  527. }