Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.83 KB | None | 0 0
  1. /*
  2. * Copyright 2019 RONINGRUM. All rights reserved.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13.  
  14. package co.id.roningrum.dolanapptugasakhir.hotel;
  15.  
  16. import android.annotation.SuppressLint;
  17. import android.content.Intent;
  18. import android.net.Uri;
  19. import android.support.annotation.NonNull;
  20. import android.support.design.widget.AppBarLayout;
  21. import android.support.design.widget.CollapsingToolbarLayout;
  22. import android.support.v7.app.AppCompatActivity;
  23. import android.os.Bundle;
  24. import android.support.v7.widget.Toolbar;
  25. import android.util.Log;
  26. import android.view.MenuItem;
  27. import android.view.View;
  28. import android.widget.ImageView;
  29. import android.widget.LinearLayout;
  30. import android.widget.Switch;
  31. import android.widget.TextView;
  32. import android.widget.Toast;
  33.  
  34. import com.bumptech.glide.Glide;
  35. import com.google.android.gms.maps.CameraUpdateFactory;
  36. import com.google.android.gms.maps.GoogleMap;
  37. import com.google.android.gms.maps.MapView;
  38. import com.google.android.gms.maps.OnMapReadyCallback;
  39. import com.google.android.gms.maps.model.LatLng;
  40. import com.google.android.gms.maps.model.MarkerOptions;
  41. import com.google.firebase.database.DataSnapshot;
  42. import com.google.firebase.database.DatabaseError;
  43. import com.google.firebase.database.DatabaseReference;
  44. import com.google.firebase.database.FirebaseDatabase;
  45. import com.google.firebase.database.Query;
  46. import com.google.firebase.database.ValueEventListener;
  47.  
  48. import co.id.roningrum.dolanapptugasakhir.R;
  49. import co.id.roningrum.dolanapptugasakhir.handler.GPSHandler;
  50. import co.id.roningrum.dolanapptugasakhir.item.CategoryItem;
  51. import co.id.roningrum.dolanapptugasakhir.item.HotelItem;
  52.  
  53. public class DetailHotelActivity extends AppCompatActivity implements OnMapReadyCallback, View.OnClickListener {
  54. public static final String EXTRA_HOTEL_KEY = "hotel_key";
  55. public static final String MAP_VIEW_KEY = "mapViewBundle";
  56.  
  57. private final static String TAG = "Pesan";
  58.  
  59. private GoogleMap hotelGoogleMap;
  60. private MapView hotelMapView;
  61.  
  62. private DatabaseReference hotelDetailRef;
  63.  
  64. private GPSHandler gpsHandler;
  65. private ValueEventListener valueEventListener;
  66.  
  67. private TextView tvNameHotelDetail, tvAddressHotelDetail, tvDistanceHotelEducation;
  68.  
  69. private ImageView imgHotelDetail;
  70. private LinearLayout btnOrderHotel1, btnOrderHotel;
  71. private CollapsingToolbarLayout collapsingToolbarHotel;
  72.  
  73. private double startLat;
  74. private double startlng;
  75. private double endlat;
  76. private double endLng;
  77. private double distance;
  78.  
  79. @Override
  80. protected void onCreate(Bundle savedInstanceState) {
  81. super.onCreate(savedInstanceState);
  82. setContentView(R.layout.activity_detail_hotel);
  83. tvNameHotelDetail = findViewById(R.id.name_place_hotel_detail);
  84. tvAddressHotelDetail = findViewById(R.id.address_place_hotel_detail);
  85. tvDistanceHotelEducation = findViewById(R.id.distance_place_hotel_detail);
  86. imgHotelDetail = findViewById(R.id.img_hotel_detail);
  87. hotelMapView = findViewById(R.id.loc_hotel_map_detail);
  88. collapsingToolbarHotel = findViewById(R.id.collapseToolbar_hotel);
  89. btnOrderHotel = findViewById(R.id.btn_order_link_hotel);
  90. btnOrderHotel1 = findViewById(R.id.btn_order_link_hotel1);
  91.  
  92. Toolbar toolbarHotel = findViewById(R.id.toolbar_hotel_detail);
  93. setSupportActionBar(toolbarHotel);
  94. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  95. getSupportActionBar().setDisplayShowHomeEnabled(true);
  96.  
  97. Bundle mapViewBundle = null;
  98. if (savedInstanceState != null) {
  99. mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_KEY);
  100. }
  101. hotelMapView.onCreate(mapViewBundle);
  102. hotelMapView.getMapAsync(this);
  103.  
  104. String hotelKey = getIntent().getStringExtra(EXTRA_HOTEL_KEY);
  105. if (hotelKey == null) {
  106. throw new IllegalArgumentException("Must pass Extra");
  107. }
  108. hotelDetailRef = FirebaseDatabase.getInstance().getReference().child("Hotel").child(hotelKey);
  109. gpsHandler = new GPSHandler(this);
  110.  
  111. btnOrderHotel1.setOnClickListener(this);
  112. btnOrderHotel.setOnClickListener(this);
  113.  
  114. LoadHotelDetail();
  115.  
  116. }
  117.  
  118. private void LoadHotelDetail() {
  119. if (gpsHandler.isCanGetLocation()) {
  120. ValueEventListener eventListener = new ValueEventListener() {
  121. @SuppressLint("SetTextI18n")
  122. @Override
  123. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  124. final HotelItem hotelItem = dataSnapshot.getValue(HotelItem.class);
  125. startLat = gpsHandler.getLatitude();
  126. startlng = gpsHandler.getLongitude();
  127. assert hotelItem != null;
  128. endlat = hotelItem.getLat_location_hotel();
  129. endLng = hotelItem.getLng_location_hotel();
  130. distance = calculateDistance(startLat, startlng, endlat, endLng);
  131.  
  132. @SuppressLint("DefaultLocale") String distanceFormat = String.format("%.2f", distance);
  133. tvDistanceHotelEducation.setText("" + distanceFormat + " km");
  134. tvNameHotelDetail.setText(hotelItem.getName_hotel());
  135. tvAddressHotelDetail.setText(hotelItem.getLocation_hotel());
  136. Glide.with(getApplicationContext()).load(hotelItem.getUrl_photo_hotel()).into(imgHotelDetail);
  137. AppBarLayout appBarLayout = findViewById(R.id.app_bar_hotel);
  138. appBarLayout.addOnOffsetChangedListener(new AppBarLayout.BaseOnOffsetChangedListener() {
  139. boolean isShow = true;
  140. int scrollRange = -1;
  141.  
  142. @Override
  143. public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
  144. if (scrollRange == -1) {
  145. scrollRange = appBarLayout.getTotalScrollRange();
  146. }
  147. if (scrollRange + verticalOffset == 0) {
  148. collapsingToolbarHotel.setTitle(hotelItem.getName_hotel());
  149. isShow = true;
  150. } else {
  151. collapsingToolbarHotel.setTitle(" ");
  152. isShow = false;
  153. }
  154.  
  155. }
  156. });
  157.  
  158. }
  159.  
  160. @Override
  161. public void onCancelled(@NonNull DatabaseError databaseError) {
  162. Log.e(TAG, "Firebase Database Error" + databaseError.getMessage());
  163. }
  164. };
  165. hotelDetailRef.addValueEventListener(eventListener);
  166. valueEventListener = eventListener;
  167. }
  168. }
  169.  
  170. private double calculateDistance(double startLat, double startlng, double endlat, double endLng) {
  171. double earthRadius = 6371;
  172. double latDiff = Math.toRadians(startLat - endlat);
  173. double lngDiff = Math.toRadians(startlng - endLng);
  174. double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2) +
  175. Math.cos(Math.toRadians(startLat)) * Math.cos(Math.toRadians(endlat)) *
  176. Math.sin(lngDiff / 2) * Math.sin(lngDiff / 2);
  177. double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  178. double distance = earthRadius * c;
  179.  
  180. int meterConversion = 1609;
  181.  
  182. return (distance * meterConversion / 1000);
  183. }
  184.  
  185. @Override
  186. protected void onSaveInstanceState(Bundle outState) {
  187. super.onSaveInstanceState(outState);
  188. Bundle mapViewBundle = outState.getBundle(MAP_VIEW_KEY);
  189. if (mapViewBundle == null) {
  190. mapViewBundle = new Bundle();
  191. outState.putBundle(MAP_VIEW_KEY, mapViewBundle);
  192. hotelMapView.onSaveInstanceState(mapViewBundle);
  193. }
  194.  
  195. }
  196.  
  197. @Override
  198. public void onMapReady(GoogleMap googleMap) {
  199. hotelGoogleMap = googleMap;
  200. ValueEventListener eventListener = new ValueEventListener() {
  201. @Override
  202. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  203. HotelItem hotelItem = dataSnapshot.getValue(HotelItem.class);
  204. assert hotelItem != null;
  205. endlat = hotelItem.getLat_location_hotel();
  206. endLng = hotelItem.getLng_location_hotel();
  207.  
  208. LatLng location = new LatLng(endlat, endLng);
  209. hotelGoogleMap.addMarker(new MarkerOptions().position(location));
  210. hotelGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 16.0f));
  211. }
  212.  
  213. @Override
  214. public void onCancelled(@NonNull DatabaseError databaseError) {
  215. Log.e(TAG, "Firebase Database Error" + databaseError.getMessage());
  216. }
  217. };
  218. hotelDetailRef.addValueEventListener(eventListener);
  219. valueEventListener = eventListener;
  220.  
  221. }
  222.  
  223. @Override
  224. public void onClick(View v) {
  225. switch (v.getId()) {
  226. case R.id.btn_order_link_hotel:
  227. getOrderKamarHotel();
  228. break;
  229. case R.id.btn_order_link_hotel1:
  230. getOrderKamarHotel1();
  231. break;
  232. }
  233.  
  234. }
  235.  
  236. private void getOrderKamarHotel1() {
  237. final Query orderHotelLinkQuery= hotelDetailRef.orderByChild("order_link_hotel1");
  238. orderHotelLinkQuery.addListenerForSingleValueEvent(new ValueEventListener() {
  239. @Override
  240. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  241. HotelItem hotelItem = new HotelItem();
  242. String orderLink = hotelItem.getOrder_link_hotel1();
  243.  
  244. if(orderLink.contains("tidak tersedia")){
  245. Toast.makeText(getApplicationContext(), "Maaf lik tidak tersedia", Toast.LENGTH_SHORT).show();
  246. }
  247. else{
  248. Intent linkOrderNow = new Intent(Intent.ACTION_VIEW, Uri.parse(orderLink));
  249. startActivity(linkOrderNow);
  250. }
  251. // for (DataSnapshot s : dataSnapshot.getChildren()){
  252. // if(s.getKey().equals("tidak tersedia")){
  253. //
  254. // }
  255. // else{
  256. // HotelItem hotelItem = new HotelItem();
  257. // String orderLink = hotelItem.getOrder_link_hotel1();
  258. //
  259. // }
  260. // }
  261. }
  262.  
  263. @Override
  264. public void onCancelled(@NonNull DatabaseError databaseError) {
  265.  
  266. }
  267. });
  268.  
  269. }
  270.  
  271. private void getOrderKamarHotel() {
  272. ValueEventListener orderEventListener = new ValueEventListener() {
  273. @Override
  274. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  275. HotelItem hotelItem = dataSnapshot.getValue(HotelItem.class);
  276. String orderLink = hotelItem.getOrder_link_hotel();
  277. Intent linkOrderNow = new Intent(Intent.ACTION_VIEW, Uri.parse(orderLink));
  278. startActivity(linkOrderNow);
  279.  
  280. }
  281.  
  282. @Override
  283. public void onCancelled(@NonNull DatabaseError databaseError) {
  284. Log.e(TAG, "" + databaseError.getMessage());
  285. }
  286. };
  287. hotelDetailRef.addValueEventListener(orderEventListener);
  288. valueEventListener = orderEventListener;
  289. }
  290.  
  291. @Override
  292. public boolean onOptionsItemSelected(MenuItem item) {
  293. // Respond to the action bar's Up/Home button
  294. if (item.getItemId() == android.R.id.home) {
  295. finish();
  296. return true;
  297. }
  298. return super.onOptionsItemSelected(item);
  299. }
  300.  
  301. @Override
  302. protected void onResume() {
  303. super.onResume();
  304. hotelMapView.onResume();
  305. }
  306.  
  307. @Override
  308. protected void onStart() {
  309. super.onStart();
  310. LoadHotelDetail();
  311. hotelMapView.onStart();
  312. }
  313.  
  314. @Override
  315. protected void onStop() {
  316. super.onStop();
  317. hotelMapView.onStop();
  318. hotelDetailRef.removeEventListener(valueEventListener);
  319.  
  320. }
  321.  
  322. @Override
  323. protected void onPause() {
  324. super.onPause();
  325. hotelMapView.onPause();
  326. }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement