DetailActivity.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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.util.Log;
  12. import android.view.Gravity;
  13. import android.view.KeyEvent;
  14. import android.view.LayoutInflater;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17. import android.view.View;
  18. import android.widget.EditText;
  19. import android.widget.FrameLayout;
  20. import android.widget.TabHost;
  21. import android.widget.TextView;
  22. import com.usai.apex.Result.SearchResultActivity;
  23. import com.usai.apex.mainframe.RootActivity;
  24. import com.usai.util.commonUtil;
  25. import com.usai.util.dbUtil;
  26. import java.util.HashMap;
  27. import java.util.Iterator;
  28. import java.util.LinkedHashMap;
  29. import java.util.List;
  30. public class DetailActivity extends AppCompatActivity implements
  31. TabHost.TabContentFactory
  32. {
  33. boolean showlogin = false;
  34. // int radomid = 1 + (int) (Math.random() * 15);
  35. TabHost mTabHost;
  36. LinkedHashMap<String, Integer> tabmap = new LinkedHashMap<String, Integer>();
  37. HashMap<String,String> contentmap = new HashMap<String ,String>();
  38. // HashMap<String, DetailFragment> fragments = new HashMap<String,
  39. // DetailFragment>();
  40. String function_name;
  41. String _id;
  42. String cargo_criterion;
  43. int criterion_type;
  44. TextView mtitleview;
  45. @Override
  46. public void setTitle(CharSequence title) {
  47. // View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null);
  48. //
  49. // TextView mtitleviewtitleview = mActionBarView.findViewById(R.id.title);
  50. if(title==null)
  51. title="";
  52. if(mtitleview!=null)
  53. mtitleview.setText(title);
  54. }
  55. private void setCustomActionBar() {
  56. ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
  57. View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null);
  58. mtitleview= mActionBarView.findViewById(R.id.title);
  59. function_name = getIntent().getStringExtra("function_name");
  60. String title = "";
  61. if(function_name.equals("Ocean Booking"))
  62. title="Booking Detail";
  63. else if(function_name.equals("Ocean B/L info."))
  64. title="B/L info. Detail";
  65. else if(function_name.equals("Container detail"))
  66. title="Container Detail";
  67. else if(function_name.equals("Cargo Tracking"))
  68. title="Cargo Detail";
  69. mtitleview.setText(title);
  70. //
  71. // mActionBarView.setBackgroundColor(Color.YELLOW);
  72. // titleview.setBackgroundColor(Color.BLUE);
  73. ActionBar actionBar = getSupportActionBar();
  74. actionBar.setCustomView(mActionBarView, lp);
  75. actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  76. actionBar.setDisplayShowCustomEnabled(true);
  77. actionBar.setDisplayHomeAsUpEnabled(true);
  78. // actionBar.setIcon(getNumberDrawable());
  79. // actionBar.setDisplayShowHomeEnabled(true);
  80. actionBar.setDisplayShowTitleEnabled(false);
  81. }
  82. public DetailActivity get_instance()
  83. {
  84. return this;
  85. }
  86. public void save_content(String which,String content)
  87. {
  88. contentmap.put(which, content);
  89. }
  90. @Override
  91. protected void onCreate(Bundle savedInstanceState)
  92. {
  93. Log.d("DetailActivity", "onCreate");
  94. super.onCreate(savedInstanceState);
  95. setContentView(R.layout.activity_detail);
  96. setCustomActionBar();
  97. List<Fragment> fragments = getSupportFragmentManager().getFragments();
  98. if (fragments != null)
  99. {
  100. Log.d("DetailActivity", "fragments count=" + fragments.size());
  101. // for (int i = 0; i < fragments.size(); i++)
  102. // Log.e("DetailFragment", "fragment name="
  103. // + fragments.get(i).getTag());
  104. }
  105. if (savedInstanceState != null)
  106. {
  107. tabmap = (LinkedHashMap<String, Integer>) savedInstanceState
  108. .getSerializable("tabmap");
  109. contentmap = (HashMap<String, String>) savedInstanceState.getSerializable("contentmap");
  110. }
  111. else
  112. {
  113. int count = getIntent().getIntExtra("actions_count", 0);
  114. for (int i = 0; i < count; i++)
  115. {
  116. int vid = commonUtil.generateViewId();
  117. tabmap.put(getIntent().getStringExtra("action" + i), vid);
  118. }
  119. }
  120. function_name = getIntent().getStringExtra("function_name");
  121. if(function_name.equals("Ocean Booking"))
  122. setTitle("Booking Detail");
  123. else if(function_name.equals("Ocean B/L info."))
  124. setTitle("B/L info. Detail");
  125. else if(function_name.equals("Container detail"))
  126. setTitle("Container Detail");
  127. // else if(function_name.equals("Cargo Tracking"))
  128. // setTitle("Cargo Detail");
  129. //
  130. if (function_name.equals("Cargo Tracking"))
  131. {
  132. criterion_type = getIntent().getIntExtra("criterion_type", 0);
  133. cargo_criterion = getIntent().getStringExtra("cargo_criterion");
  134. setTitle("Cargo Detail");
  135. }
  136. _id = getIntent().getStringExtra("_id");
  137. setupTabs();
  138. }
  139. // @Override
  140. // protected void onSaveInstanceState(Bundle outState)
  141. // {
  142. // Iterator<String> iter=fragments.keySet().iterator();
  143. //
  144. // while (iter.hasNext())
  145. // {
  146. // String tag = (String) iter.next();
  147. // getSupportFragmentManager().putFragment(outState,tag,fragments.get(tag));
  148. //
  149. // }
  150. // outState.putStringArray("tags", fragments.keySet().toArray(new
  151. // String[0]));
  152. // Log.d("onSaveInstanceState","save fragments" +
  153. // fragments.keySet().size());
  154. // super.onSaveInstanceState(outState);
  155. // }
  156. //
  157. // @Override
  158. // protected void onRestoreInstanceState(Bundle savedInstanceState)
  159. // {
  160. // String [] tags =savedInstanceState.getStringArray("tags");
  161. // Log.d("onRestoreInstanceState","load fragments" + tags.length);
  162. // for(int i=0;i<tags.length;i++)
  163. // {
  164. // Log.d("onRestoreInstanceState","load fragment" + tags[i]);
  165. // fragments.put(tags[i],(DetailFragment)
  166. // getSupportFragmentManager().getFragment(savedInstanceState,tags[i]));
  167. // }
  168. //
  169. // super.onRestoreInstanceState(savedInstanceState);
  170. // }
  171. /** {@inheritDoc} */
  172. public View createTabContent(String tag)
  173. {
  174. FrameLayout fl = new FrameLayout(this);
  175. fl.setId(tabmap.get(tag));
  176. Fragment f;
  177. Bundle bundle = new Bundle();
  178. if (function_name.equals("Cargo Tracking"))
  179. {
  180. if (criterion_type == 0)
  181. bundle.putString("hbol", cargo_criterion);
  182. else
  183. bundle.putString("container_no", cargo_criterion);
  184. }
  185. bundle.putString("action_type", tag);
  186. bundle.putString("id", _id);
  187. bundle.putString("module_name", function_name);
  188. // Log.e("findFragmentByTag", "tag=" + tag + ", isnull= "
  189. // + (getSupportFragmentManager().findFragmentByTag(tag) == null));
  190. f = getSupportFragmentManager().findFragmentById(tabmap.get(tag));
  191. if (f == null)
  192. {
  193. Log.d("createTabContent", "create fragment" + tag);
  194. f = new DetailFragment();
  195. f.setArguments(bundle);
  196. }
  197. ((DetailFragment)f).set_content( contentmap.get(tag));
  198. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  199. ft.replace(fl.getId(), f);
  200. ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  201. ft.addToBackStack(tag);
  202. ft.commit();
  203. return fl;
  204. }
  205. @Override
  206. protected void onSaveInstanceState(Bundle outState)
  207. {
  208. outState.putSerializable("tabmap", tabmap);
  209. outState.putSerializable("contentmap", contentmap);
  210. super.onSaveInstanceState(outState);
  211. }
  212. // TabHost mTabHost;
  213. // private FrameLayout mTabContent;
  214. // @Override
  215. // public boolean onKeyDown(int keyCode, KeyEvent event)
  216. // {
  217. // if (keyCode == KeyEvent.KEYCODE_BACK)
  218. // {
  219. // // Intent myIntent = new Intent();
  220. // // myIntent = new Intent(EditActivity.this, tabActivity.class);
  221. // // startActivity(myIntent);
  222. // finish();
  223. // return true;
  224. // }
  225. // return super.onKeyDown(keyCode, event);
  226. // }
  227. @Override
  228. public boolean onKeyUp(int keyCode, KeyEvent event)
  229. {
  230. if (keyCode == KeyEvent.KEYCODE_BACK)
  231. {
  232. // Intent myIntent = new Intent();
  233. // myIntent = new Intent(EditActivity.this, tabActivity.class);
  234. // startActivity(myIntent);
  235. finish();
  236. return true;
  237. }
  238. return super.onKeyUp(keyCode, event);
  239. }
  240. //
  241. // @Override
  242. // protected void onCreate(Bundle savedInstanceState)
  243. // {
  244. // super.onCreate(savedInstanceState);
  245. // setContentView(R.layout.activity_detail);
  246. // // Show the Up button in the action bar.
  247. // setupActionBar();
  248. // setupTabs();
  249. // }
  250. //
  251. // /**
  252. // * Set up the {@link android.app.ActionBar}.
  253. // */
  254. // private void setupActionBar()
  255. // {
  256. //
  257. // getActionBar().setDisplayHomeAsUpEnabled(true);
  258. //
  259. // }
  260. //
  261. // // private View createTabIndicatorView(ViewGroup parent, CharSequence
  262. // label, Drawable icon) {
  263. // // final LayoutInflater inflater = LayoutInflater.from(this);
  264. // // final View tabIndicator = inflater.inflate(R.layout.tab_indicator,
  265. // parent, false);
  266. // //
  267. // //// final TextView tv = (TextView)
  268. // tabIndicator.findViewById(R.id.tab_title);
  269. // //// tv.setText(label);
  270. // ////
  271. // //// final ImageView iconView = (ImageView)
  272. // tabIndicator.findViewById(R.id.tab_icon);
  273. // //// iconView.setImageDrawable(icon);
  274. // //
  275. // // return tabIndicator;
  276. // // }
  277. //
  278. // @Override
  279. // public View createTabContent(String tag)
  280. // {
  281. // final TextView tv = new TextView(this);
  282. // tv.setText("Content for tab with tag " + tag);
  283. // return tv;
  284. // }
  285. private void setupTabs()
  286. {
  287. mTabHost = (TabHost) findViewById(R.id.tabhost);
  288. // mTabContent = (FrameLayout) findViewById(android.R.id.tabcontent);
  289. // FrameLayout tab1 = new FrameLayout(this);
  290. mTabHost.setup();
  291. Iterator<String> iter = tabmap.keySet().iterator();
  292. while (iter.hasNext())
  293. {
  294. String name = (String) iter.next();
  295. mTabHost.addTab(mTabHost.newTabSpec(name).setIndicator(name)
  296. .setContent(this));
  297. // String val = (String) tabmap.get(field);
  298. // parms.putString(field, val);
  299. // // Cursor c = (Cursor) m_listadapter.getItem(key);
  300. // Log.d("@@@@@@@@@", field + " : " + val);
  301. }
  302. // for (int i=1; i <= tabmap.size(); i++) {
  303. // String name = tabmap.keySet().iterator().;
  304. // mTabHost.addTab(mTabHost.newTabSpec(name)
  305. // .setIndicator(name)
  306. // .setContent(this));
  307. // }
  308. // // if(true)
  309. // // return;
  310. //
  311. // // View tab1 = createTabIndicatorView(mTabHost.getTabWidget(),
  312. // // "Login",null);
  313. // // tabhost.addTab(
  314. // // tabhost.newTabSpec("tab" + i).setIndicator(tab)
  315. // // .setContent(Mytabfirst.this)); tabhost.setCurrentTab(i);
  316. // // i++; tab = null;
  317. //
  318. // // // ��ɵײ��Զ�����ʽ�İ�ť
  319. // // String[] title = new String[] { "Login", "Not login" };
  320. // // int[] tabIds = new int[] { R.id.tab1, R.id.tab2 };
  321. // mTabHost.addTab(mTabHost.newTabSpec("Login").setIndicator("Login")
  322. // .setContent(this));
  323. // //
  324. // mTabHost.addTab(mTabHost.newTabSpec("Direct Tracking").setIndicator("Direct Tracking").setContent(R.id.tab2));
  325. // mTabHost.addTab(mTabHost.newTabSpec("Service Location")
  326. // .setIndicator("Service Location").setContent(this));
  327. // FragmentManager manager = this.getFragmentManager();
  328. // Instantiate a new fragment.
  329. // Fragment loginFragment = new LoginFragment();
  330. // // Fragment dtFragement = new DirectTrackingFragment();
  331. // Fragment slFragment = new ServiceLocationFragment();
  332. // // Add the fragment to the activity, pushing this transaction
  333. // // on to the back stack.
  334. //
  335. // FragmentTransaction ft =
  336. // getSupportFragmentManager().beginTransaction();
  337. // ft.replace(mTabHost.getTabContentView().getChildAt(0).getId(),
  338. // loginFragment);
  339. // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  340. // ft.addToBackStack("Login");
  341. //
  342. // // ft.replace(R.id.tab2, dtFragement);
  343. // // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  344. // // ft.addToBackStack("Tracking");
  345. //
  346. // ft.replace(R.id.tab3, (Fragment) slFragment);
  347. // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  348. // ft.addToBackStack("Location");
  349. //
  350. // ft.commit();
  351. // if (manager.findFragmentByTag(tabId) == null)
  352. // {
  353. // FragmentTransaction trans = manager.beginTransaction();
  354. // trans.replace(contentViewID, frag, tabId);
  355. // trans.commit();
  356. // }
  357. // for (int i = 0; i < title.length; i++) {
  358. // Button button = new Button(this);
  359. // button.setText(title[i]);
  360. // button.setBackgroundDrawable(this.getResources().getDrawable(
  361. // R.drawable.tab_lable)); //�Զ��尴ť��ʽ
  362. // mTabHost.addTab(mTabHost.newTabSpec(title[i]).setIndicator(button)
  363. // .setContent(tabIds[i]));
  364. // }
  365. // mTabHost.setOnTabChangedListener(this);
  366. }
  367. @Override
  368. public boolean onCreateOptionsMenu(Menu menu)
  369. {
  370. // Inflate the menu; this adds items to the action bar if it is present.
  371. getMenuInflater().inflate(R.menu.detail, menu);
  372. return true;
  373. }
  374. @Override
  375. public boolean onOptionsItemSelected(MenuItem item)
  376. {
  377. switch (item.getItemId())
  378. {
  379. case R.id.action_close:
  380. finish();
  381. // if (SearchResultActivity.instance != null) {
  382. // SearchResultActivity.instance.finish();
  383. // }
  384. // if (SearchListActivity.instance != null) {
  385. // SearchListActivity.instance.finish();
  386. // }
  387. Intent intent = new Intent(this, RootActivity.class);
  388. startActivity(intent);
  389. break;
  390. case android.R.id.home:
  391. finish();
  392. break;
  393. case R.id.action_addto_favorite:
  394. // LayoutInflater factory = LayoutInflater.from(this);
  395. // final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
  396. final View edit = new EditText(this);
  397. new AlertDialog.Builder(this)
  398. .setIconAttribute(android.R.attr.alertDialogIcon)
  399. .setTitle(R.string.str_createname)
  400. .setView(edit)
  401. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  402. public void onClick(DialogInterface dialog, int whichButton) {
  403. String name = ((EditText)edit).getText().toString();
  404. String param = getIntent().toUri(0);
  405. SQLiteDatabase db = dbUtil.OpenDB(
  406. ApexTrackingApplication.get_instance(), null, true);
  407. db.execSQL("insert into favorites(name,params,user,create_time,module_name) values('"
  408. + name
  409. + "','"
  410. + param
  411. + "','"
  412. + ApexTrackingApplication.get_user()
  413. + "',"
  414. + System.currentTimeMillis()
  415. + ",'"
  416. +getIntent().getStringExtra("function_name")+ "')");
  417. dbUtil.CloseDB(db);
  418. /* User clicked OK so do some stuff */
  419. }
  420. })
  421. .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
  422. public void onClick(DialogInterface dialog, int whichButton) {
  423. /* User clicked cancel so do some stuff */
  424. }
  425. })
  426. .create().show();
  427. return true;
  428. }
  429. return super.onOptionsItemSelected(item);
  430. }
  431. }