mirror of
https://github.com/Nezumi-2711/Foodify-Shipper.git
synced 2026-09-22 13:38:42 +00:00
Send location to server
This commit is contained in:
@@ -44,6 +44,7 @@ dependencies {
|
||||
implementation 'com.google.firebase:firebase-auth:21.0.3'
|
||||
implementation 'com.google.firebase:firebase-storage:20.0.1'
|
||||
implementation 'com.google.android.gms:play-services-maps:18.1.0'
|
||||
implementation 'com.google.firebase:firebase-database:20.0.4'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||
|
||||
@@ -20,6 +20,7 @@ import android.content.pm.PackageManager;
|
||||
import android.location.Location;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
@@ -104,8 +105,7 @@ public class OrderDetailActivity extends AppCompatActivity {
|
||||
private LocationRequest mLocationRequest;
|
||||
private LocationSettingsRequest mLocationSettingsRequest;
|
||||
private LocationCallback mLocationCallBack;
|
||||
private Location mCurrentLocation;
|
||||
private boolean mRequestingLocationUpdates = false;
|
||||
private Location mCurrentLocation = null;
|
||||
private ImageView back_image;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -141,6 +141,8 @@ public class OrderDetailActivity extends AppCompatActivity {
|
||||
back_image.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Common.CURRENT_LOCATION = null;
|
||||
stopLocationService();
|
||||
startActivity(new Intent(OrderDetailActivity.this, MainActivity.class));
|
||||
finish();
|
||||
}
|
||||
@@ -175,15 +177,14 @@ public class OrderDetailActivity extends AppCompatActivity {
|
||||
btn_confirm_ship_completed.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
changeOrderStatus(COMPLETE_STATUS);
|
||||
stopLocationService();
|
||||
updateStatus(COMPLETE_STATUS);
|
||||
}
|
||||
});
|
||||
|
||||
btn_cancel_order.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
changeOrderStatus(CANCEL_STATUS);
|
||||
updateStatus(CANCEL_STATUS);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -191,8 +192,34 @@ public class OrderDetailActivity extends AppCompatActivity {
|
||||
geofencingClient = LocationServices.getGeofencingClient(this);
|
||||
geofenceHelper = new GeofenceHelper(this);
|
||||
|
||||
if(!order.getStatus().equals(COMPLETE_STATUS) && !order.getStatus().equals(CANCEL_STATUS)){
|
||||
getLocation();
|
||||
|
||||
getLocation();
|
||||
final Handler handler = new Handler(Looper.getMainLooper());
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if(mCurrentLocation != null){
|
||||
stopLocationUpdates();
|
||||
Common.CURRENT_LOCATION = mCurrentLocation;
|
||||
getDistanceAndCalculateShipCost(order.getAddress());
|
||||
changeLayoutButton(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
|
||||
} else {
|
||||
Toast.makeText(OrderDetailActivity.this, "Chưa thể lấy được vị trí!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}, 5000);
|
||||
} else {
|
||||
btn_shipping.setVisibility(View.GONE);
|
||||
layoutConfirmOrder.setVisibility(View.GONE);
|
||||
progressLayout.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateStatus(String status) {
|
||||
Common.CURRENT_LOCATION = null;
|
||||
changeOrderStatus(status);
|
||||
stopLocationService();
|
||||
}
|
||||
|
||||
private void changeOrderStatus(String status){
|
||||
@@ -414,15 +441,7 @@ public class OrderDetailActivity extends AppCompatActivity {
|
||||
public void onLocationResult(@NonNull LocationResult locationResult) {
|
||||
super.onLocationResult(locationResult);
|
||||
|
||||
stopLocationUpdates();
|
||||
mCurrentLocation = locationResult.getLastLocation();
|
||||
|
||||
Common.CURRENT_LOCATION = mCurrentLocation;
|
||||
getDistanceAndCalculateShipCost(order.getAddress());
|
||||
|
||||
changeLayoutButton(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
|
||||
|
||||
progressLayout.setVisibility(View.GONE);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -25,11 +25,14 @@ import androidx.annotation.Nullable;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import com.capstone.foodify.shipper.Model.GoogleMap.Location;
|
||||
import com.google.android.gms.location.LocationCallback;
|
||||
import com.google.android.gms.location.LocationRequest;
|
||||
import com.google.android.gms.location.LocationResult;
|
||||
import com.google.android.gms.location.LocationServices;
|
||||
import com.google.android.gms.location.Priority;
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
|
||||
public class LocationService extends Service {
|
||||
private static final String TAG = "LocationService";
|
||||
@@ -43,9 +46,11 @@ public class LocationService extends Service {
|
||||
double lat = locationResult.getLastLocation().getLatitude();
|
||||
double lng = locationResult.getLastLocation().getLongitude();
|
||||
|
||||
Log.d(TAG, lat + ", " + lng);
|
||||
Log.d(TAG, "Location: " + lat + ", " + lng);
|
||||
|
||||
Common.CURRENT_LOCATION = locationResult.getLastLocation();
|
||||
|
||||
updateLocationShipper(lat, lng);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -115,4 +120,16 @@ public class LocationService extends Service {
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
private void updateLocationShipper(double lat, double lng){
|
||||
FirebaseDatabase database = FirebaseDatabase.getInstance();
|
||||
DatabaseReference order = database.getReference("Order");
|
||||
|
||||
if(Common.CURRENT_ORDER != null){
|
||||
Location location = new Location();
|
||||
location.setLat(lat);
|
||||
location.setLng(lng);
|
||||
order.child(Common.CURRENT_ORDER.getOrderTrackingNumber()).setValue(location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,9 +295,11 @@
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:text="Đang giao"
|
||||
android:text="-"
|
||||
android:layout_marginStart="5dp"
|
||||
android:fontFamily="@font/opensans"
|
||||
android:textColor="@color/black"
|
||||
android:textColor="@color/primaryColor"
|
||||
android:textAllCaps="true"
|
||||
android:textStyle="bold"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
@@ -315,6 +317,7 @@
|
||||
android:layout_marginTop="25dp"
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginEnd="10dp"
|
||||
android:paddingBottom="20dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/status_order_layout"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
@@ -362,7 +365,6 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
android:drawableLeft="@drawable/baseline_local_shipping_24"
|
||||
android:drawableTint="@color/white"
|
||||
android:visibility="gone"
|
||||
/>
|
||||
|
||||
<LinearLayout
|
||||
@@ -374,7 +376,7 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
android:layout_marginTop="20dp"
|
||||
android:visibility="visible"
|
||||
android:visibility="gone"
|
||||
>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user