mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Get location realtime
This commit is contained in:
@@ -52,12 +52,14 @@ dependencies {
|
||||
implementation 'com.google.firebase:firebase-core:11.8.0'
|
||||
implementation 'com.google.firebase:firebase-database:11.8.0'
|
||||
implementation 'com.google.firebase:firebase-auth:11.8.0'
|
||||
implementation 'com.google.android.gms:play-services:11.8.0'
|
||||
|
||||
implementation 'com.github.jd-alexander:android-flat-button:v1.1'
|
||||
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
|
||||
implementation 'com.stepstone.apprating:app-rating:2.0.0'
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
|
||||
implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
|
||||
implementation 'com.squareup.retrofit2:converter-scalars:2.7.1'
|
||||
implementation 'com.github.d-max:spots-dialog:0.7@aar'
|
||||
|
||||
implementation 'com.squareup.picasso:picasso:2.5.2'
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="com.waterbase.foodify">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
|
||||
|
||||
@@ -1,22 +1,35 @@
|
||||
package com.waterbase.foodify;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Location;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.google.android.gms.common.ConnectionResult;
|
||||
import com.google.android.gms.common.GooglePlayServicesUtil;
|
||||
import com.google.android.gms.common.api.GoogleApiClient;
|
||||
import com.google.android.gms.location.LocationListener;
|
||||
import com.google.android.gms.location.LocationRequest;
|
||||
import com.google.android.gms.location.LocationServices;
|
||||
import com.google.firebase.database.DataSnapshot;
|
||||
import com.google.firebase.database.DatabaseError;
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
@@ -33,6 +46,7 @@ import com.waterbase.foodify.Model.Request;
|
||||
import com.waterbase.foodify.Model.Sender;
|
||||
import com.waterbase.foodify.Model.Token;
|
||||
import com.waterbase.foodify.Remote.APIService;
|
||||
import com.waterbase.foodify.Remote.IGoogleService;
|
||||
import com.waterbase.foodify.ViewHolder.CartAdapter;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
@@ -49,7 +63,8 @@ import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class Cart extends AppCompatActivity {
|
||||
public class Cart extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
|
||||
LocationListener {
|
||||
|
||||
RecyclerView recyclerView;
|
||||
RecyclerView.LayoutManager layoutManager;
|
||||
@@ -65,6 +80,21 @@ public class Cart extends AppCompatActivity {
|
||||
|
||||
APIService mService;
|
||||
|
||||
//Location
|
||||
private LocationRequest mLocationRequest;
|
||||
private GoogleApiClient mGoogleApiClient;
|
||||
private Location mLastLocation;
|
||||
|
||||
private static int UPDATE_INTERVAL = 5000;
|
||||
private static int FATEST_INTERVAL = 3000;
|
||||
private static int DISPLACEMENT = 10;
|
||||
|
||||
private static final int LOCATION_REQUEST_CODE = 9999;
|
||||
private static final int PLAY_SERVICES_REQUEST = 9997;
|
||||
|
||||
//Declare Google Map API Retrofit
|
||||
IGoogleService mGoogleMapService;
|
||||
|
||||
@Override
|
||||
protected void attachBaseContext(Context newBase) {
|
||||
super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
|
||||
@@ -84,8 +114,25 @@ public class Cart extends AppCompatActivity {
|
||||
|
||||
setContentView(R.layout.activity_cart);
|
||||
|
||||
//Runtime permission
|
||||
if(checkLocatePermission())
|
||||
{
|
||||
ActivityCompat.requestPermissions(this, new String[]
|
||||
{
|
||||
Manifest.permission.ACCESS_COARSE_LOCATION,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
}, LOCATION_REQUEST_CODE
|
||||
);
|
||||
} else {
|
||||
if(checkPlayServices()) {
|
||||
buildGoogleApiClient();
|
||||
createLocationRequest();
|
||||
}
|
||||
}
|
||||
|
||||
//Init
|
||||
mService = Common.getFCMService();
|
||||
mGoogleMapService = Common.getGoogleMapAPI();
|
||||
|
||||
//Firebase
|
||||
database = FirebaseDatabase.getInstance();
|
||||
@@ -120,6 +167,55 @@ public class Cart extends AppCompatActivity {
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
switch (requestCode) {
|
||||
case LOCATION_REQUEST_CODE:
|
||||
{
|
||||
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
if(checkPlayServices()) {
|
||||
buildGoogleApiClient();
|
||||
createLocationRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void createLocationRequest() {
|
||||
mLocationRequest = new LocationRequest();
|
||||
mLocationRequest.setInterval(UPDATE_INTERVAL);
|
||||
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
|
||||
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
|
||||
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
|
||||
}
|
||||
|
||||
private synchronized void buildGoogleApiClient() {
|
||||
|
||||
mGoogleApiClient = new GoogleApiClient.Builder(this)
|
||||
.addConnectionCallbacks(this)
|
||||
.addOnConnectionFailedListener(this)
|
||||
.addApi(LocationServices.API).build();
|
||||
|
||||
mGoogleApiClient.connect();
|
||||
}
|
||||
|
||||
private boolean checkPlayServices() {
|
||||
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
|
||||
if(resultCode != ConnectionResult.SUCCESS) {
|
||||
if(GooglePlayServicesUtil.isUserRecoverableError(resultCode))
|
||||
GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_REQUEST).show();
|
||||
else
|
||||
{
|
||||
Toast.makeText(this, "Thiết bị này chưa được hỗ trợ!", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void showAlertDialog() {
|
||||
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
|
||||
alertDialog.setTitle("Còn 1 bước nữa!");
|
||||
@@ -132,6 +228,23 @@ public class Cart extends AppCompatActivity {
|
||||
MaterialEditText edtComment = (MaterialEditText) order_address_comment.findViewById(R.id.edtComment);
|
||||
|
||||
|
||||
//Radio
|
||||
RadioButton rdiShipToAddress = (RadioButton) order_address_comment.findViewById(R.id.rdiShipToAddress);
|
||||
RadioButton rdiHomeAddress = (RadioButton) order_address_comment.findViewById(R.id.rdiHomeAddress);
|
||||
|
||||
//Add event
|
||||
rdiShipToAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
if(isChecked) {
|
||||
if(isChecked){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
alertDialog.setView(order_address_comment);
|
||||
alertDialog.setIcon(R.drawable.ic_baseline_shopping_cart_24);
|
||||
|
||||
@@ -156,10 +269,6 @@ public class Cart extends AppCompatActivity {
|
||||
|
||||
sendNotificationOrder(order_number);
|
||||
|
||||
//Delete Cart
|
||||
// new Database(getBaseContext()).cleanCart();
|
||||
// Toast.makeText(Cart.this, "Đặt hàng thành công!", Toast.LENGTH_SHORT).show();
|
||||
// finish();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -263,4 +372,50 @@ public class Cart extends AppCompatActivity {
|
||||
//Refresh
|
||||
loadListFood();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(@Nullable Bundle bundle) {
|
||||
displayLocation();
|
||||
startLocationUpdates();
|
||||
}
|
||||
|
||||
private void startLocationUpdates() {
|
||||
if(checkLocatePermission()){
|
||||
return;
|
||||
}
|
||||
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
|
||||
}
|
||||
|
||||
private void displayLocation() {
|
||||
if(checkLocatePermission())
|
||||
return;
|
||||
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
|
||||
if(mLastLocation != null) {
|
||||
Log.d("LOCATION", "Your location: " + mLastLocation.getLatitude() + ", " + mLastLocation.getLongitude());
|
||||
}
|
||||
else {
|
||||
Log.d("LOCATION", "Could not get your location!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionSuspended(int i) {
|
||||
mGoogleApiClient.connect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
|
||||
|
||||
}
|
||||
|
||||
private boolean checkLocatePermission() {
|
||||
return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
|
||||
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
mLastLocation = location;
|
||||
displayLocation();
|
||||
}
|
||||
}
|
||||
@@ -6,17 +6,23 @@ import android.net.NetworkInfo;
|
||||
|
||||
import com.waterbase.foodify.Model.User;
|
||||
import com.waterbase.foodify.Remote.APIService;
|
||||
import com.waterbase.foodify.Remote.IGoogleService;
|
||||
import com.waterbase.foodify.Remote.RetrofitClient;
|
||||
|
||||
public class Common {
|
||||
public static User currentUser;
|
||||
|
||||
private static final String BASE_URL = "https://fcm.googleapis.com/";
|
||||
private static final String GOOGLE_API_URL = "https://maps.googleapis.com/";
|
||||
|
||||
public static APIService getFCMService(){
|
||||
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
|
||||
}
|
||||
|
||||
public static IGoogleService getGoogleMapAPI(){
|
||||
return RetrofitClient.getGoogleClient(GOOGLE_API_URL).create(IGoogleService.class);
|
||||
}
|
||||
|
||||
public static final String DELETE = "Xoá";
|
||||
public static final String USER_KEY = "User";
|
||||
public static final String PWD_KEY = "Password";
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.waterbase.foodify.Remote;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Url;
|
||||
|
||||
public interface IGoogleService {
|
||||
|
||||
@GET
|
||||
Call<String> getAddressName(@Url String url);
|
||||
}
|
||||
@@ -17,4 +17,15 @@ public class RetrofitClient {
|
||||
}
|
||||
return retrofit;
|
||||
}
|
||||
|
||||
public static Retrofit getGoogleClient(String baseURL)
|
||||
{
|
||||
if(retrofit == null) {
|
||||
retrofit = new Retrofit.Builder()
|
||||
.baseUrl(baseURL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
}
|
||||
return retrofit;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,28 @@
|
||||
app:met_primaryColor="@color/colorPrimary"
|
||||
app:met_singleLineEllipsis="true"
|
||||
/>
|
||||
|
||||
<RadioGroup
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
>
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rdiShipToAddress"
|
||||
android:text="Giao hàng đến vị trí của bạn"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rdiHomeAddress"
|
||||
android:text="Giao hàng đến địa chỉ trên"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
|
||||
|
||||
</RadioGroup>
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
Reference in New Issue
Block a user