Java tutorial
package com.yoval.community.chatapp; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentSender; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationManager; import android.net.Uri; import android.net.wifi.WifiManager; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; import android.support.multidex.MultiDex; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; import com.firebase.ui.auth.AuthUI; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.appindexing.Thing; 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.common.api.PendingResult; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.LocationSettingsRequest; import com.google.android.gms.location.LocationSettingsResult; import com.google.android.gms.location.LocationSettingsStatusCodes; import com.google.android.gms.maps.CameraUpdate; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import com.yoval.community.localisation.CustomizedLocationListener; import com.yoval.community.model.User; //icons from: http://support.flaticon.com/ //<div>Icons made by <a href="http://www.freepik.com" title="Freepik">Freepik</a> from <a href="http://www.flaticon.com" title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></div> public class DistanceActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { private static final int SIGN_IN_REQUEST_CODE = 10; private static final int REQUEST_CHECK_SETTINGS = 1; private GoogleMap googleMap; LocationManager locationManager; GoogleApiClient googleApiClient; LatLng userLocation = new LatLng(45.4170084, -72.1142148); //Sherbrooke int radius = 1; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_distance); displayLocationSettingsRequest(getApplicationContext()); if (googleServicesAvalaible()) { //Toast.makeText(this, "Perfect", Toast.LENGTH_LONG).show(); initMap(); } else { Toast.makeText(this, "Play services problem", Toast.LENGTH_LONG).show(); } //Check for WIfi services enabled WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); if (!wifi.isWifiEnabled()) { showWifiRequest(); } CustomizedLocationListener locationListener = new CustomizedLocationListener(getApplicationContext()); locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); if (ContextCompat.checkSelfPermission(this, Manifest.permission.MAPS_RECEIVE) != PackageManager.PERMISSION_GRANTED) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5 * 60 * 1000, 10, locationListener); } if (FirebaseAuth.getInstance().getCurrentUser() == null) { // Start sign in/sign up activity startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_REQUEST_CODE); } else { // User is already signed in. Therefore, display // a welcome Toast Toast.makeText(this, R.string.Bienvenue + " " + FirebaseAuth.getInstance().getCurrentUser().getDisplayName(), Toast.LENGTH_LONG).show(); } Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarDistance); setSupportActionBar(toolbar); SeekBar distanceBar = (SeekBar) findViewById(R.id.seekBarDistance); // make seekbar object distanceBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub TextView textViewDistance = (TextView) findViewById(R.id.textViewDistance); textViewDistance.setText(progress + " Km"); radius = progress; SaveUserInformations(radius); updateZoomMap(); } }); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.distNext); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { goToDetailView(); } }); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } private void showWifiRequest() { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); final WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE); // set title alertDialogBuilder.setTitle("Wifi Settings"); // set dialog message alertDialogBuilder.setMessage("Do you want to enable WIFI ?").setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //enable wifi wifiMan.setWifiEnabled(true); } }).setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //disable wifi //wifiMan.setWifiEnabled(false); finish(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } private void displayLocationSettingsRequest(Context context) { GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(LocationServices.API).build(); googleApiClient.connect(); LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(10000); locationRequest.setFastestInterval(10000 / 2); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest); builder.setAlwaysShow(true); PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi .checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // Log.i(TAG, "All location settings are satisfied."); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: //Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings "); try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). status.startResolutionForResult(DistanceActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { // Log.i(TAG, "PendingIntent unable to execute request."); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: //Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created."); break; } } }); } private void SaveUserInformations(int radius) { FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser(); String userId = firebaseUser.getUid(); String userName = firebaseUser.getDisplayName(); User user = new User(userId, userName, radius, userLocation); FirebaseDatabase.getInstance().getReference().child("users").child(userId).setValue(user); } @Override protected void attachBaseContext(Context context) { super.attachBaseContext(context); MultiDex.install(this); } private void initMap() { SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.mapFragment); mapFragment.getMapAsync(this); } public boolean googleServicesAvalaible() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int isAvalaible = api.isGooglePlayServicesAvailable(this); if (isAvalaible == ConnectionResult.SUCCESS) { return true; } else if (api.isUserResolvableError(isAvalaible)) { Dialog dialog = api.getErrorDialog(this, isAvalaible, 0); dialog.show(); } else { Toast.makeText(this, "Can't connect to play services", Toast.LENGTH_LONG).show(); } return false; } private void goToDetailView() { Intent intent = new Intent(this, UserAction.class); startActivity(intent); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { // Check for the integer request code originally supplied to startResolutionForResult(). case REQUEST_CHECK_SETTINGS: switch (resultCode) { case Activity.RESULT_OK: //User agreed to make required location settings changes. break; case Activity.RESULT_CANCELED: //User chose not to make required location settings changes // Close the app finish(); break; } break; case SIGN_IN_REQUEST_CODE: if (resultCode == RESULT_OK) { Toast.makeText(this, "Successfully signed in. Welcome!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "We couldn't sign you in. Please try again later.", Toast.LENGTH_LONG).show(); // Close the app finish(); } break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_sign_out) { AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { Toast.makeText(getApplicationContext(), "You have been signed out.", Toast.LENGTH_LONG).show(); // Close activity finish(); } }); } return true; } @Override public void onMapReady(GoogleMap map) { googleMap = map; googleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API) .addConnectionCallbacks(this).addOnConnectionFailedListener(this).build(); googleApiClient.connect(); } LocationRequest locationRequest; @Override public void onConnected(@Nullable Bundle bundle) { locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER); locationRequest.setInterval(5000); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { } @Override public void onLocationChanged(Location location) { String userId = ""; if (location == null) { Toast.makeText(getApplicationContext(), "Can't get current location.", Toast.LENGTH_LONG).show(); } else { userLocation = new LatLng(location.getLatitude(), location.getLongitude()); CameraUpdate update = CameraUpdateFactory.newLatLngZoom(userLocation, getZoomLevel(radius)); googleMap.animateCamera(update); FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser(); if (firebaseUser != null) { userId = firebaseUser.getUid(); FirebaseDatabase.getInstance().getReference().child("users").child(userId).child("location") .setValue(userLocation); FirebaseDatabase.getInstance().getReference().child("users/" + userId + "/radius") .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { // radius = ((Long)snapshot.getValue()).intValue(); //prints "Do you have data? You'll love Firebase." } @Override public void onCancelled(DatabaseError databaseError) { } }); } } } private void updateZoomMap() { float zoom = getZoomLevel(radius); CameraUpdate update = CameraUpdateFactory.newLatLngZoom(userLocation, zoom); MarkerOptions currentLocationMarker = new MarkerOptions().position(userLocation).title("Votre localisation") .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_placeholder)); googleMap.addMarker(currentLocationMarker); if (googleMap != null) { googleMap.animateCamera(update); } } private float getZoomLevel(int radius) { double scale = (radius * 1000) / 500; float zoom = (float) (16 - Math.log(scale) / Math.log(2)); return zoom; } /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ public Action getIndexApiAction() { Thing object = new Thing.Builder().setName("Distance Page") // TODO: Define a title for the content shown. // TODO: Make sure this auto-generated URL is correct. .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]")).build(); return new Action.Builder(Action.TYPE_VIEW).setObject(object).setActionStatus(Action.STATUS_TYPE_COMPLETED) .build(); } @Override public void onStart() { super.onStart(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client.connect(); AppIndex.AppIndexApi.start(client, getIndexApiAction()); } @Override public void onStop() { super.onStop(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. AppIndex.AppIndexApi.end(client, getIndexApiAction()); client.disconnect(); } }