InnerMapActivity.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package com.usai.apex;
  2. import android.app.AlertDialog;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.content.pm.PackageInfo;
  7. import android.content.pm.PackageManager.NameNotFoundException;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.support.v4.app.Fragment;
  13. import android.support.v4.app.FragmentManager;
  14. import android.support.v4.app.FragmentTransaction;
  15. import android.support.v7.app.ActionBar;
  16. import android.support.v7.app.AppCompatActivity;
  17. import android.view.Gravity;
  18. import android.view.KeyEvent;
  19. import android.view.LayoutInflater;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.widget.TextView;
  23. import com.baidu.mapapi.map.SupportMapFragment;
  24. import com.usai.Contacts.Contact;
  25. import com.usai.Contacts.ContactsManager;
  26. //import com.baidu.mapapi.MKGeneralListener;
  27. //import com.baidu.mapapi.map.MKEvent;
  28. public class InnerMapActivity extends AppCompatActivity implements BaiduMapFragment.BaiduMarkerClickListener,ServiceLocationFragment.MarkerClickListener
  29. {
  30. // public static boolean m_bKeyRight = true;
  31. // BMapManager mBMapManager = null;
  32. // public String strKey = getString(R.string.baidu_key);//"nqBQoSDbxrslhuzW91uViQX7";//release
  33. private static final String LTAG = "test";
  34. SupportMapFragment map;
  35. boolean m_bhasgoogleframework = true;
  36. // static class MyGeneralListener implements MKGeneralListener
  37. // {
  38. //
  39. // @Override
  40. // public void onGetNetworkState(int iError)
  41. // {
  42. // if (iError == MKEvent.ERROR_NETWORK_CONNECT)
  43. // {
  44. // Toast.makeText(
  45. // ApexTrackingApplication.get_instance()
  46. // .getApplicationContext(),
  47. // "????????????????????????", Toast.LENGTH_LONG).show();
  48. // }
  49. // else if (iError == MKEvent.ERROR_NETWORK_DATA)
  50. // {
  51. // Toast.makeText(
  52. // ApexTrackingApplication.get_instance()
  53. // .getApplicationContext(),
  54. // "??????????????????????????????", Toast.LENGTH_LONG)
  55. // .show();
  56. // }
  57. // // ...
  58. // }
  59. //
  60. // @Override
  61. // public void onGetPermissionState(int iError)
  62. // {
  63. // // ???????????????key???????????????
  64. // if (iError != 0)
  65. // {
  66. // // ??????Key?????????
  67. // Toast.makeText(
  68. // ApexTrackingApplication.get_instance()
  69. // .getApplicationContext(),
  70. // "?????? MyApplication.java???????????????????????????Key,??????????????????????????????????????????error: "
  71. // + iError, Toast.LENGTH_LONG).show();
  72. // m_bKeyRight = false;
  73. // }
  74. // else
  75. // {
  76. // m_bKeyRight = true;
  77. // Toast.makeText(
  78. // ApexTrackingApplication.get_instance()
  79. // .getApplicationContext(), "key????????????",
  80. // Toast.LENGTH_LONG).show();
  81. // }
  82. // }
  83. // }
  84. @Override
  85. public boolean onKeyDown(int keyCode, KeyEvent event)
  86. {
  87. if (keyCode == KeyEvent.KEYCODE_BACK)
  88. {
  89. // Intent myIntent = new Intent();
  90. // myIntent = new Intent(EditActivity.this, tabActivity.class);
  91. // startActivity(myIntent);
  92. finish();
  93. return true;
  94. }
  95. return super.onKeyDown(keyCode, event);
  96. }
  97. @Override
  98. public boolean onOptionsItemSelected(MenuItem item)
  99. {
  100. Intent intent = new Intent();
  101. switch (item.getItemId())
  102. {
  103. case android.R.id.home:
  104. finish();
  105. break;
  106. default:
  107. break;
  108. }
  109. return super.onOptionsItemSelected(item);
  110. }
  111. private void setCustomActionBar() {
  112. ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
  113. View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null);
  114. TextView titleview = mActionBarView.findViewById(R.id.title);
  115. titleview.setText("Service Location");
  116. setTitle("Service Location");
  117. ActionBar actionBar = getSupportActionBar();
  118. actionBar.setCustomView(mActionBarView, lp);
  119. // actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  120. // actionBar.setDisplayShowCustomEnabled(true);
  121. actionBar.setDisplayHomeAsUpEnabled(true);
  122. actionBar.setDisplayShowTitleEnabled(true);
  123. }
  124. private Context mContext;
  125. @Override
  126. protected void onCreate(Bundle savedInstanceState)
  127. {
  128. super.onCreate(savedInstanceState);
  129. mContext = this;
  130. PackageInfo packageInfo;
  131. try
  132. {
  133. packageInfo = this.getPackageManager().getPackageInfo(
  134. "com.google.android.gms", 0);
  135. }
  136. catch (NameNotFoundException e)
  137. {
  138. packageInfo = null;
  139. e.printStackTrace();
  140. }
  141. if (packageInfo == null)
  142. {
  143. m_bhasgoogleframework = false;
  144. System.out.println("没有安装");
  145. }
  146. else
  147. {
  148. m_bhasgoogleframework = true;
  149. System.out.println("已经安装");
  150. }
  151. // setUpMapIfNeeded();
  152. // m_bhasgoogleframework = false;
  153. // m_bhasgoogleframework= true;
  154. Fragment slFragment = null;
  155. if (m_bhasgoogleframework )
  156. {
  157. setContentView(R.layout.activity_inner_map);
  158. slFragment = new ServiceLocationFragment();
  159. ((ServiceLocationFragment)slFragment).markerClickListener = this;
  160. FragmentTransaction ft = getSupportFragmentManager()
  161. .beginTransaction();
  162. ft.replace(R.id.inner_map, slFragment);
  163. ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  164. ft.addToBackStack("Location");
  165. ft.commit();
  166. }
  167. else
  168. {
  169. // if (mBMapManager == null)
  170. // {
  171. // mBMapManager = new BMapManager(getApplicationContext());
  172. // /**
  173. // * ??????BMapManager???????????????????????????BMapManager
  174. // */
  175. // mBMapManager.init(strKey, new MyGeneralListener());
  176. // }
  177. // f.initEngineManager(this);
  178. setContentView(R.layout.activity_inner_map);
  179. // Log.d(LTAG, "onCreate");
  180. // // setContentView(R.layout.activity_main);
  181. // map = SupportMapFragment.newInstance();
  182. BaiduMapFragment f = new BaiduMapFragment();
  183. FragmentManager manager = getSupportFragmentManager();
  184. manager.beginTransaction().add(R.id.inner_map, f, "map_fragment")
  185. .commit();
  186. f.markerClickListener = this;
  187. }
  188. setCustomActionBar();
  189. // LocationDetail detail = new LocationDetail("Kerry Freight (USA), Inc","3200 NW 67th Ave. Bldg. 3 Suite 390 Miami, FL 33122 Miami","+(305)12345678","+(305)4567890",null,"mario.alfonso@kerrylogistics.com");
  190. // markerClicked(detail);
  191. }
  192. // @Override
  193. // public boolean onCreateOptionsMenu(Menu menu)
  194. // {
  195. // // Inflate the menu; this adds items to the action bar if it is present.
  196. // getMenuInflater().inflate(R.menu.inner_map, menu);
  197. // return true;
  198. // }
  199. // private void setUpMapIfNeeded() {
  200. // // Do a null check to confirm that we have not already instantiated the
  201. // map.
  202. // if (mMap == null) {
  203. // // Try to obtain the map from the SupportMapFragment.
  204. // mMap = ((SupportMapFragment)
  205. // getSupportFragmentManager().findFragmentById(R.id.map))
  206. // .getMap();
  207. // // Check if we were successful in obtaining the map.
  208. // if (mMap != null) {
  209. // setUpMap();
  210. // }
  211. // }
  212. // }
  213. @Override
  214. public void markerClicked(LocationDetail detail) {
  215. if (detail == null) {
  216. return;
  217. }
  218. String homePage = "http://www.apexshipping.com";
  219. Bitmap photo = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
  220. final Contact contact = new Contact();
  221. contact.setName(detail.Name);
  222. contact.setAddress(detail.Addr);
  223. contact.setNumber(detail.Tel);
  224. contact.setFax(detail.Fax);
  225. contact.setEmail(detail.Email);
  226. contact.setHomePage(homePage);
  227. contact.setPhoto(photo);
  228. final ContactsManager cm = new ContactsManager(this.getContentResolver());
  229. if (cm.contactExist(contact.getName())) {
  230. new AlertDialog.Builder(this)
  231. .setTitle("Warning")
  232. .setMessage(contact.getName() + " has exist in contacts,do you want to update it?")
  233. .setNegativeButton("No", new DialogInterface.OnClickListener() {
  234. @Override
  235. public void onClick(DialogInterface dialog, int which) {
  236. // 添加一条新记录
  237. cm.insertContact(contact,mContext);
  238. // Contact existContact = cm.searchContact(contact.getName());
  239. // String contactId = cm.getContactID(existContact.getName());
  240. // Intent editIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/contacts/"+contactId));
  241. // startActivity(editIntent);
  242. }
  243. })
  244. .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  245. @Override
  246. public void onClick(DialogInterface dialog, int which) {
  247. //Intent.ACTION_INSERT_OR_EDIT
  248. //参考 https://blog.csdn.net/mls1454001840/article/details/60764351 之二
  249. Contact existContact = cm.searchContact(contact.getName());
  250. cm.updateContact(existContact,contact);
  251. String contactId = cm.getContactID(existContact.getName());
  252. Intent editIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/contacts/"+contactId));
  253. startActivity(editIntent);
  254. //
  255. // // 显示编辑界面
  256. // String contactId = cm.getContactID(existContact.getName());
  257. // cm.editContact(contactId,mContext);
  258. }
  259. })
  260. .show();
  261. } else {
  262. // 自动保存,然后显示编辑
  263. // cm.addContact(contact,true);
  264. // // 显示编辑界面
  265. // String contactId = cm.getContactID(contact.getName());
  266. // editContact(contactId);
  267. // 需要用户手动保存
  268. cm.insertContact(contact,mContext);
  269. }
  270. }
  271. }