From 9938f64be9b45b2b43821bf8ee0d5212a4b9048d Mon Sep 17 00:00:00 2001 From: gaurav Date: Sat, 14 Dec 2019 17:34:08 +0530 Subject: [PATCH 1/2] add maps activity and auth --- app/build.gradle | 14 +- app/google-services.json | 48 ++++++ app/src/main/AndroidManifest.xml | 11 ++ .../example/android/geotag/MainActivity.java | 100 +++++++++++ .../java/com/example/android/geotag/map.java | 159 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 41 ++++- app/src/main/res/layout/activity_map.xml | 17 ++ build.gradle | 5 +- gradle.properties | 2 + 9 files changed, 386 insertions(+), 11 deletions(-) create mode 100644 app/google-services.json create mode 100644 app/src/main/java/com/example/android/geotag/map.java create mode 100644 app/src/main/res/layout/activity_map.xml diff --git a/app/build.gradle b/app/build.gradle index 57ca55b..23b54a7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,11 +1,12 @@ apply plugin: 'com.android.application' +apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { applicationId "com.example.android.geotag" - minSdkVersion 15 + minSdkVersion 23 targetSdkVersion 29 versionCode 1 versionName "1.0" @@ -23,7 +24,18 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation 'com.google.firebase:firebase-auth:16.0.5' + implementation 'com.firebaseui:firebase-ui-auth:4.3.1' + /*implementation 'com.google.android.gms:play-services:12.0.1'*/ testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' + + implementation 'com.google.android.gms:play-services-maps:16.1.0' + implementation 'com.google.android.gms:play-services-auth:16.0.1' + implementation 'com.google.android.gms:play-services-location:16.0.0' + implementation 'com.google.android.gms:play-services-analytics:16.0.7' + implementation 'com.google.android.gms:play-services-places:16.0.0' + implementation 'com.google.android.gms:play-services-gcm:16.0.0' + implementation 'com.google.android.gms:play-services-base:16.1.0' } diff --git a/app/google-services.json b/app/google-services.json new file mode 100644 index 0000000..f3d003b --- /dev/null +++ b/app/google-services.json @@ -0,0 +1,48 @@ +{ + "project_info": { + "project_number": "738961992255", + "firebase_url": "https://geotag-132b1.firebaseio.com", + "project_id": "geotag-132b1", + "storage_bucket": "geotag-132b1.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:738961992255:android:ecd6ace57e76d9a0a5e68a", + "android_client_info": { + "package_name": "com.example.android.geotag" + } + }, + "oauth_client": [ + { + "client_id": "738961992255-77psa62dbf840amvf4n62s9pbqe57fse.apps.googleusercontent.com", + "client_type": 1, + "android_info": { + "package_name": "com.example.android.geotag", + "certificate_hash": "be538e123a2a75a47a281f9a25722d3c07cb7568" + } + }, + { + "client_id": "738961992255-ehpu3gd9kvtetfge2p84v94aedp77bgl.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyAkm6w7-QM9XiJ3uhiDQKiwxkWf9XT_bvw" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "738961992255-ehpu3gd9kvtetfge2p84v94aedp77bgl.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dd1ca4d..82e5cc8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> + @@ -16,6 +17,16 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/android/geotag/MainActivity.java b/app/src/main/java/com/example/android/geotag/MainActivity.java index b41cdc6..153af1a 100644 --- a/app/src/main/java/com/example/android/geotag/MainActivity.java +++ b/app/src/main/java/com/example/android/geotag/MainActivity.java @@ -1,14 +1,114 @@ package com.example.android.geotag; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import android.content.Intent; import android.os.Bundle; +import android.view.View; +import android.widget.ListView; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +import com.firebase.ui.auth.AuthUI; +import com.firebase.ui.auth.IdpResponse; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseUser; + +import java.util.Arrays; +import java.util.List; + public class MainActivity extends AppCompatActivity { + public static final int RC_SIGN_IN = 1; + + private FirebaseAuth mFirebaseAuth; + private FirebaseAuth.AuthStateListener mAuthStateListener; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + mFirebaseAuth = FirebaseAuth.getInstance(); + + mAuthStateListener = new FirebaseAuth.AuthStateListener() { + + @Override + public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { + updateUi(); + } + }; + } + + private void updateUi() { + FirebaseUser user = mFirebaseAuth.getCurrentUser(); + if (user == null) { + // Choose authentication providers + List providers = Arrays.asList( + new AuthUI.IdpConfig.EmailBuilder().build(), + new AuthUI.IdpConfig.GoogleBuilder().build()); + // Create and launch sign-in intent + startActivityForResult( + AuthUI.getInstance() + .createSignInIntentBuilder() + .setAvailableProviders(providers) + .build(), + RC_SIGN_IN); + + } else { + + TextView nameText = findViewById(R.id.nameText); + TextView emailText = findViewById(R.id.emailText); + nameText.setText(mFirebaseAuth.getCurrentUser().getDisplayName()); + emailText.setText(mFirebaseAuth.getCurrentUser().getEmail()); + + } + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + if (requestCode == RC_SIGN_IN) { + IdpResponse response = IdpResponse.fromResultIntent(data); + + if (resultCode == RESULT_OK) { + // Successfully signed in + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); + // ... + } else { + // Sign in failed. If response is null the user canceled the + // sign-in flow using the back button. Otherwise check + // response.getError().getErrorCode() and handle the error. + // ... + } + } + } + + @Override + protected void onResume() { + super.onResume(); + mFirebaseAuth.addAuthStateListener(mAuthStateListener); + } + + @Override + protected void onPause() { + super.onPause(); + if (mAuthStateListener != null) { + mFirebaseAuth.removeAuthStateListener(mAuthStateListener); + } + } + + //signout the user + public void signOut(View view){ + FirebaseAuth.getInstance().signOut(); + } + + //go to activity_map.xml + public void map(View view) { + startActivity(new Intent(MainActivity.this, map.class)); } } diff --git a/app/src/main/java/com/example/android/geotag/map.java b/app/src/main/java/com/example/android/geotag/map.java new file mode 100644 index 0000000..d5da530 --- /dev/null +++ b/app/src/main/java/com/example/android/geotag/map.java @@ -0,0 +1,159 @@ +package com.example.android.geotag; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.FragmentActivity; + +import android.Manifest; +import android.content.pm.PackageManager; +import android.location.Address; +import android.location.Geocoder; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Build; +import android.os.Bundle; +import android.view.View; + +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.location.LocationRequest; +import com.google.android.gms.location.LocationServices; +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.LatLng; +import com.google.android.gms.maps.model.Marker; +import com.google.android.gms.maps.model.MarkerOptions; + +import java.io.IOException; +import java.util.List; + +public class map extends AppCompatActivity + implements OnMapReadyCallback { + + private GoogleMap mMap; + LocationManager locationManager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // Retrieve the content view that renders the map. + setContentView(R.layout.activity_map); + // Get the SupportMapFragment and request notification + // when the map is ready to be used. + SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() + .findFragmentById(R.id.map); + mapFragment.getMapAsync(this); + // mMap.setMyLocationEnabled(true); + + locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); + if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // Activity#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 Activity#requestPermissions for more details. + return; + } + //check network provider + if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ + locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000000, 0, new LocationListener() { + @Override + public void onLocationChanged(Location location) { + //get latitude + double latitude = location.getLatitude(); + //get longitude + double longitude = location.getLongitude(); + //instantiate LatLng class + LatLng latLng = new LatLng(latitude, longitude); + //instantiate Geocoder class + Geocoder geocoder = new Geocoder(getApplicationContext()); + try { + List
addressList = geocoder.getFromLocation(latitude, longitude,1); + String str = addressList.get(0).getLocality()+", "; + str += addressList.get(0).getCountryName(); + mMap.addMarker(new MarkerOptions().position(latLng).title(str)); + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f)); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + + } + + @Override + public void onProviderEnabled(String provider) { + + } + + @Override + public void onProviderDisabled(String provider) { + + } + }); + } + + //check gps provider + else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000000, 0, new LocationListener() { + @Override + public void onLocationChanged(Location location) { + //get latitude + double latitude = location.getLatitude(); + //get longitude + double longitude = location.getLongitude(); + //instantiate LatLng class + LatLng latLng = new LatLng(latitude, longitude); + //instantiate Geocoder class + Geocoder geocoder = new Geocoder(getApplicationContext()); + try { + List
addressList = geocoder.getFromLocation(latitude, longitude,1); + String str = addressList.get(0).getLocality()+", "; + str += addressList.get(0).getCountryName(); + mMap.addMarker(new MarkerOptions().position(latLng).title(str)); + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f)); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + + } + + @Override + public void onProviderEnabled(String provider) { + + } + + @Override + public void onProviderDisabled(String provider) { + + } + }); + } + + } + + @Override + public void onMapReady(GoogleMap googleMap) { + mMap = googleMap; + mMap.setMyLocationEnabled(true); + // Add a marker in Sydney, Australia, + // and move the map's camera to the same location. +// LatLng sydney = new LatLng(-33.852, 151.211); +// mMap.addMarker(new MarkerOptions().position(sydney) +// .title("Marker in Sydney")); +// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 10.2f)); + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4fc2444..65bef2c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,14 +5,39 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> + - + + + + +