package com.usai.apex; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.TabHost; import android.widget.TabWidget; import android.widget.TextView; import com.usai.apex.Result.AMResultActivity; import com.usai.apex.mainframe.RootActivity; import com.usai.util.commonUtil; import com.usai.util.dbUtil; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; public class DetailActivity extends AppCompatActivity implements TabHost.TabContentFactory { boolean showlogin = false; // int radomid = 1 + (int) (Math.random() * 15); TabHost mTabHost; LinkedHashMap tabmap = new LinkedHashMap(); HashMap contentmap = new HashMap(); // HashMap fragments = new HashMap(); String function_name; String _id; String cargo_criterion; int criterion_type; String email_to = null,email_subject = null,email_content = null; TextView mtitleview; // @Override // public void setTitle(CharSequence title) { //// View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null); //// //// TextView mtitleviewtitleview = mActionBarView.findViewById(R.id.title); // // if(title==null) // title=""; // if(mtitleview!=null) // mtitleview.setText(title); // } private void setCustomActionBar() { ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER); View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null); mtitleview= mActionBarView.findViewById(R.id.title); function_name = getIntent().getStringExtra("function_name"); String title = ""; if(function_name.equals("Ocean Booking")) title="Booking Detail"; else if(function_name.equals("Ocean B/L info.")) title="B/L info. Detail"; else if(function_name.equals("Container detail")) title="Container Detail"; else if(function_name.equals("Cargo Tracking")) title="Cargo Detail"; mtitleview.setText(title); setTitle(title); // // mActionBarView.setBackgroundColor(Color.YELLOW); // titleview.setBackgroundColor(Color.BLUE); ActionBar actionBar = getSupportActionBar(); actionBar.setCustomView(mActionBarView, lp); // actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); // actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); // actionBar.setIcon(getNumberDrawable()); // actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayShowTitleEnabled(true); } public DetailActivity get_instance() { return this; } // String email_to = null,email_subject = null,email_content = null; public void save_content(String which,String content) { contentmap.put(which, content); try { JSONObject jsonObject = new JSONObject(content); email_to = jsonObject.getString("email"); email_subject = jsonObject.getString("email_subject"); email_content = jsonObject.getString("email_content"); } catch (JSONException e) { e.printStackTrace(); } } @Override protected void onCreate(Bundle savedInstanceState) { Log.d("DetailActivity", "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); setCustomActionBar(); List fragments = getSupportFragmentManager().getFragments(); if (fragments != null) { Log.d("DetailActivity", "fragments count=" + fragments.size()); // for (int i = 0; i < fragments.size(); i++) // Log.e("DetailFragment", "fragment name=" // + fragments.get(i).getTag()); } if (savedInstanceState != null) { tabmap = (LinkedHashMap) savedInstanceState .getSerializable("tabmap"); contentmap = (HashMap) savedInstanceState.getSerializable("contentmap"); } else { int count = getIntent().getIntExtra("actions_count", 0); for (int i = 0; i < count; i++) { int vid = commonUtil.generateViewId(); tabmap.put(getIntent().getStringExtra("action" + i), vid); } } function_name = getIntent().getStringExtra("function_name"); if(function_name.equals("Ocean Booking")) setTitle("Booking Detail"); else if(function_name.equals("Ocean B/L info.")) setTitle("B/L info. Detail"); else if(function_name.equals("Container detail")) setTitle("Container Detail"); // else if(function_name.equals("Cargo Tracking")) // setTitle("Cargo Detail"); // if (function_name.equals("Cargo Tracking")) { criterion_type = getIntent().getIntExtra("criterion_type", 0); cargo_criterion = getIntent().getStringExtra("cargo_criterion"); setTitle("Cargo Detail"); } _id = getIntent().getStringExtra("_id"); setupTabs(); } // @Override // protected void onSaveInstanceState(Bundle outState) // { // Iterator iter=fragments.keySet().iterator(); // // while (iter.hasNext()) // { // String tag = (String) iter.next(); // getSupportFragmentManager().putFragment(outState,tag,fragments.get(tag)); // // } // outState.putStringArray("tags", fragments.keySet().toArray(new // String[0])); // Log.d("onSaveInstanceState","save fragments" + // fragments.keySet().size()); // super.onSaveInstanceState(outState); // } // // @Override // protected void onRestoreInstanceState(Bundle savedInstanceState) // { // String [] tags =savedInstanceState.getStringArray("tags"); // Log.d("onRestoreInstanceState","load fragments" + tags.length); // for(int i=0;i iter = tabmap.keySet().iterator(); while (iter.hasNext()) { String name = (String) iter.next(); mTabHost.addTab(mTabHost.newTabSpec(name).setIndicator(name) .setContent(this)); // String val = (String) tabmap.get(field); // parms.putString(field, val); // // Cursor c = (Cursor) m_listadapter.getItem(key); // Log.d("@@@@@@@@@", field + " : " + val); } TabWidget tabWidget=mTabHost.getTabWidget(); for (int i = 0; i < tabWidget.getChildCount(); i++) { tabWidget.getChildAt(i).getLayoutParams().height = commonUtil.dp2px(this,39); tabWidget.getChildAt(i).setPadding(24,0,24,0); // tabWidget.getChildAt(i).getLayoutParams().height = 60; TextView tv=(TextView)tabWidget.getChildAt(i).findViewById(android.R.id.title); // tv.setGravity(BIND_AUTO_CREATE); // tv.setPadding(10, 10,10, 10); tv.setTextSize(12);//设置字体的大小; // tv.setTextColor(Color.WHITE);//设置字体的颜色; //获取tabs图片; // ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon); } // for (int i=1; i <= tabmap.size(); i++) { // String name = tabmap.keySet().iterator().; // mTabHost.addTab(mTabHost.newTabSpec(name) // .setIndicator(name) // .setContent(this)); // } // // if(true) // // return; // // // View tab1 = createTabIndicatorView(mTabHost.getTabWidget(), // // "Login",null); // // tabhost.addTab( // // tabhost.newTabSpec("tab" + i).setIndicator(tab) // // .setContent(Mytabfirst.this)); tabhost.setCurrentTab(i); // // i++; tab = null; // // // // ��ɵײ��Զ�����ʽ�İ�ť // // String[] title = new String[] { "Login", "Not login" }; // // int[] tabIds = new int[] { R.id.tab1, R.id.tab2 }; // mTabHost.addTab(mTabHost.newTabSpec("Login").setIndicator("Login") // .setContent(this)); // // // mTabHost.addTab(mTabHost.newTabSpec("Direct Tracking").setIndicator("Direct Tracking").setContent(R.id.tab2)); // mTabHost.addTab(mTabHost.newTabSpec("Service Location") // .setIndicator("Service Location").setContent(this)); // FragmentManager manager = this.getFragmentManager(); // Instantiate a new fragment. // Fragment loginFragment = new LoginFragment(); // // Fragment dtFragement = new DirectTrackingFragment(); // Fragment slFragment = new ServiceLocationFragment(); // // Add the fragment to the activity, pushing this transaction // // on to the back stack. // // FragmentTransaction ft = // getSupportFragmentManager().beginTransaction(); // ft.replace(mTabHost.getTabContentView().getChildAt(0).getId(), // loginFragment); // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); // ft.addToBackStack("Login"); // // // ft.replace(R.id.tab2, dtFragement); // // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); // // ft.addToBackStack("Tracking"); // // ft.replace(R.id.tab3, (Fragment) slFragment); // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); // ft.addToBackStack("Location"); // // ft.commit(); // if (manager.findFragmentByTag(tabId) == null) // { // FragmentTransaction trans = manager.beginTransaction(); // trans.replace(contentViewID, frag, tabId); // trans.commit(); // } // for (int i = 0; i < title.length; i++) { // Button button = new Button(this); // button.setText(title[i]); // button.setBackgroundDrawable(this.getResources().getDrawable( // R.drawable.tab_lable)); //�Զ��尴ť��ʽ // mTabHost.addTab(mTabHost.newTabSpec(title[i]).setIndicator(button) // .setContent(tabIds[i])); // } // mTabHost.setOnTabChangedListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. if (function_name.equals("Cargo Tracking")) { getMenuInflater().inflate(R.menu.cargo_menu, menu); } else { getMenuInflater().inflate(R.menu.detail, menu); } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_close: finish(); // if (SearchResultActivity.instance != null) { // SearchResultActivity.instance.finish(); // } // if (SearchListActivity.instance != null) { // SearchListActivity.instance.finish(); // } Intent intent = new Intent(this, RootActivity.class); startActivity(intent); break; case android.R.id.home: finish(); break; case R.id.action_addto_favorite: // LayoutInflater factory = LayoutInflater.from(this); // final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null); final View edit = new EditText(this); new AlertDialog.Builder(this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.str_createname) .setView(edit) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String name = ((EditText)edit).getText().toString(); if(TextUtils.isEmpty(name)) { new android.app.AlertDialog.Builder(DetailActivity.this) .setTitle("Warning") .setMessage("Name can not be empty.") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); return; } String param = getIntent().toUri(0); SQLiteDatabase db = dbUtil.OpenDB( ApexTrackingApplication.get_instance(), null, true); db.execSQL("insert into favorites(name,params,user,create_time,module_name) values('" + name + "','" + param + "','" + ApexTrackingApplication.get_user() + "'," + System.currentTimeMillis() + ",'" +getIntent().getStringExtra("function_name")+ "')"); dbUtil.CloseDB(db); /* User clicked OK so do some stuff */ } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked cancel so do some stuff */ } }) .create().show(); return true; case R.id.action_search_document: { String cargo_str = contentmap.get("Tracking"); try { JSONObject cargo_json = new JSONObject(cargo_str); String hbol = cargo_json.optString("hbol"); if (hbol != null && hbol.length() > 0) { Bundle parms = new Bundle(); parms.putString("bol",hbol); parms.putString("module_name","Download Document"); Intent resultIntent = new Intent(this, AMResultActivity.class); resultIntent.putExtra("query_params",parms); startActivity(resultIntent); } else { showAlter("There is no hbol to search"); } } catch (JSONException e) { e.printStackTrace(); showAlter("There is no hbol to search"); } } break; case R.id.action_share_detail: { Intent share = new Intent(); share.setAction(Intent.ACTION_SEND); // share.putExtra(Intent.EXTRA_STREAM, uri); share.setType("text/plain"); if (email_content != null) { share.putExtra(Intent.EXTRA_TEXT, email_content); //附带的说明信息 } if (email_subject != null) { share.putExtra(Intent.EXTRA_SUBJECT, email_subject); } if (email_to != null) { share.putExtra(Intent.EXTRA_EMAIL, email_to.split(",")); } // share.putExtra(Intent.EXTRA_CC, new String[]{"ray.zhang@united-cn.net"}); // startActivity(Intent.createChooser(share,getString(R.string.str_sendto))); startActivity(Intent.createChooser(share, "Share")); } break; } return super.onOptionsItemSelected(item); } public void showAlter(String msg) { new AlertDialog.Builder(this) .setMessage(msg) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .show(); } }