content: package ui.auth;
  
  import android.Manifest;
  import android.annotation.SuppressLint;
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.location.Address;
  import android.location.Geocoder;
  import android.location.Location;
  import android.location.LocationManager;
  import android.os.Build;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.util.Log;
  import android.view.View;
  import android.view.Window;
  import android.view.WindowManager;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.appcompat.app.AlertDialog;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.core.app.ActivityCompat;
  import androidx.core.content.ContextCompat;
  
  import com.example.munche.MainActivity;
  import com.example.munche.R;
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.GoogleApiAvailability;
  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.firestore.FirebaseFirestore;
  
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Locale;
  import java.util.Map;
  
  public class AddInfoActivity extends AppCompatActivity  implements LocationListener,
          GoogleApiClient.ConnectionCallbacks,
          GoogleApiClient.OnConnectionFailedListener {
  
      private FirebaseFirestore db;
      private String phoneNum;
      private String uid;
      private String devicetoken;
      private String city;
      private String postalCode;
      private String knownName;
      private String subLocality;
      private String finalAddress;
      private Double latitude,longitude;
      private EditText mUserName, mUserEmail, mUserAddress;
      private Button mSaveInfoBtn;
      private List<Address> addresses;
      final String TAG = "GPS";
      private long UPDATE_INTERVAL = 2 * 1000;  /* 10 secs */
      private long FASTEST_INTERVAL = 10000; /* 2 sec */
      static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
  
      GoogleApiClient gac;
      LocationRequest locationRequest;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_add_info);
  
          changestatusbarcolor();
          init();
          db = FirebaseFirestore.getInstance();
          phoneNum = getIntent().getStringExtra("PHONENUMBER");
          uid = getIntent().getStringExtra("UID");
          devicetoken = getIntent().getStringExtra("TOKEN");
  
          mSaveInfoBtn.setOnClickListener(view -> {
  
              String name = mUserName.getText().toString();
              String email = mUserEmail.getText().toString();
  
              if (name.isEmpty() || email.isEmpty()) {
                  Toast.makeText(this, "Please Enter Valid Info", Toast.LENGTH_SHORT).show();
              } else{
                  HashMap<String,Object> userData = new HashMap<>();
                  userData.put("uid", uid);
                  userData.put("phonenumber", phoneNum);
                  userData.put("name", name);
                  userData.put("email", email);
                  userData.put("devicetoken", devicetoken);
                  userData.put("latitude", latitude);
                  userData.put("longitude",longitude);
                  userData.put("address", finalAddress);
                  userData.put("knownname", knownName);
                  userData.put("sublocality", subLocality);
                  userData.put("city", city);
                  userData.put("postalcode", postalCode);
  
                  db.collection("UserList").document(uid).set(userData).addOnSuccessListener(o -> {
  
                      Intent intent = new Intent(this, MainActivity.class);
                      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                      startActivity(intent);
                      finish();
  
                  }).addOnFailureListener(e -> {
                      Toast.makeText(this, "Something went Wrong!", Toast.LENGTH_SHORT).show();
                  });
              }
          });
      }
  
      private void init() {
          mUserName = findViewById(R.id.userName);
          mUserEmail = findViewById(R.id.userEmail);
          mSaveInfoBtn = findViewById(R.id.addInfo);
          mUserAddress = findViewById(R.id.userAddress);
  
          isGooglePlayServicesAvailable();
  
          if(!isLocationEnabled())
              showAlert();
  
          locationRequest = new LocationRequest();
          locationRequest.setInterval(UPDATE_INTERVAL);
          locationRequest.setFastestInterval(FASTEST_INTERVAL);
          locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
          gac = new GoogleApiClient.Builder(this)
                  .addConnectionCallbacks(this)
                  .addOnConnectionFailedListener(this)
                  .addApi(LocationServices.API)
                  .build();
  
      }
  
      public void changestatusbarcolor() {
          Window window = this.getWindow();
          window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
          window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
          window.setStatusBarColor(ContextCompat.getColor(this, R.color.white));
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
          }
      }
  
      @Override
      protected void onStart() {
          gac.connect();
          super.onStart();
      }
  
      @Override
      protected void onStop() {
          gac.disconnect();
          super.onStop();
      }
  
      @Override
      public void onLocationChanged(@NonNull Location location) {
          if (location != null) {
              updateUI(location);
          }
      }
  
      @Override
      public void onConnected(@Nullable Bundle bundle) {
          if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                  != PackageManager.PERMISSION_GRANTED
                  && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                  != PackageManager.PERMISSION_GRANTED) {
              ActivityCompat.requestPermissions(AddInfoActivity.this,
                      new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                      MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
  
              return;
          }
          Log.d(TAG, "onConnected");
  
          Location ll = LocationServices.FusedLocationApi.getLastLocation(gac);
          Log.d(TAG, "LastLocation: " + ll.toString());
  
          LocationServices.FusedLocationApi.requestLocationUpdates(gac, locationRequest, this);
      }
  
      @Override
      public void onRequestPermissionsResult(
              int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          switch (requestCode) {
              case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
                  // If request is cancelled, the result arrays are empty.
                  if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  
                      try {
                          LocationServices.FusedLocationApi.requestLocationUpdates(
                                  gac, locationRequest, this);
                      } catch (SecurityException e) {
                          Toast.makeText(AddInfoActivity.this, "SecurityException:\n" + e.toString(),
                                  Toast.LENGTH_LONG).show();
                      }
                  } else {
                      Toast.makeText(AddInfoActivity.this, "Permission denied!", Toast.LENGTH_LONG).show();
                  }
                  return;
              }
          }
      }
  
      @Override
      public void onConnectionSuspended(int i) {}
  
      @Override
      public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
          Toast.makeText(AddInfoActivity.this, "onConnectionFailed: \n" + connectionResult.toString(),
                  Toast.LENGTH_LONG).show();
          Log.d("DDD", connectionResult.toString());
      }
  
      @SuppressLint("SetTextI18n")
      private void updateUI(Location loc) {
          Log.d(TAG, "updateUI");
          Geocoder geocoder = new Geocoder(AddInfoActivity.this, Locale.getDefault());
  
          try {
              addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
  
              latitude = loc.getLatitude();
              longitude = loc.getLongitude();
  
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          if (addresses != null && addresses.size() > 0){
              city = addresses.get(0).getLocality();
              postalCode = addresses.get(0).getPostalCode();
              knownName = addresses.get(0).getFeatureName();
              subLocality = addresses.get(0).getSubLocality();
              finalAddress = knownName + ", " + subLocality +  ", " + city + ", " + postalCode;
              mUserAddress.setText(finalAddress);
  
          }
  
      }
  
      private boolean isLocationEnabled() {
          LocationManager locationManager =
                  (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                  locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      }
  
      private boolean isGooglePlayServicesAvailable() {
          final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
          GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
          int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
          if (resultCode != ConnectionResult.SUCCESS) {
              if (apiAvailability.isUserResolvableError(resultCode)) {
                  apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                          .show();
              } else {
                  Log.d(TAG, "This device is not supported.");
                  finish();
              }
              return false;
          }
          Log.d(TAG, "This device is supported.");
          return true;
      }
  
      private void showAlert() {
          final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
          dialog.setTitle("Enable Location")
                  .setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
                          "use this app")
                  .setPositiveButton("Location Settings", (paramDialogInterface, paramInt) -> {
  
                      Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                      startActivity(myIntent);
                  })
                  .setNegativeButton("Cancel", (paramDialogInterface, paramInt) -> {
  
                  });
          dialog.show();
      }
  }