Skip to content

Commit a26a1d9

Browse files
committed
Mock Location Detector
1 parent b2e4277 commit a26a1d9

File tree

7 files changed

+70
-6
lines changed

7 files changed

+70
-6
lines changed
2.18 MB
Binary file not shown.

MockLocationDetector/app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ dependencies {
2626
testImplementation 'junit:junit:4.12'
2727
androidTestImplementation 'androidx.test:runner:1.2.0'
2828
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
29+
implementation 'com.google.android.gms:play-services-location:15.0.1'
2930
}

MockLocationDetector/app/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.codificador.mocklocationdetector">
44

5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
7+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
8+
59
<application
610
android:allowBackup="true"
711
android:icon="@mipmap/ic_launcher"
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,66 @@
11
package com.codificador.mocklocationdetector;
22

33
import androidx.appcompat.app.AppCompatActivity;
4+
import androidx.core.app.ActivityCompat;
5+
import androidx.core.content.ContextCompat;
46

7+
import android.Manifest;
8+
import android.content.pm.PackageManager;
9+
import android.location.Location;
510
import android.os.Bundle;
11+
import android.widget.TextView;
12+
13+
import com.google.android.gms.location.FusedLocationProviderClient;
14+
import com.google.android.gms.location.LocationServices;
15+
import com.google.android.gms.tasks.OnSuccessListener;
616

717
public class MainActivity extends AppCompatActivity {
818

19+
TextView textView;
20+
static final int REQUEST_CODE = 111;
21+
922
@Override
1023
protected void onCreate(Bundle savedInstanceState) {
1124
super.onCreate(savedInstanceState);
1225
setContentView(R.layout.activity_main);
26+
textView = findViewById(R.id.textView);
27+
permissionCheck();
28+
}
29+
30+
private void permissionCheck(){
31+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
32+
!= PackageManager.PERMISSION_GRANTED)
33+
{
34+
ActivityCompat.requestPermissions(this,
35+
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
36+
REQUEST_CODE);
37+
return;
38+
}
39+
}
40+
41+
@Override
42+
protected void onResume() {
43+
super.onResume();
44+
getCurrentLocation();
45+
}
46+
47+
public void getCurrentLocation() {
48+
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
49+
fusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
50+
@Override
51+
public void onSuccess(Location location) {
52+
if (location != null) {
53+
boolean isMock = location.isFromMockProvider();
54+
if(isMock){
55+
textView.setTextColor(getResources().getColor(R.color.colorAccent));
56+
textView.setText("MOCKED LOCATION");
57+
}
58+
else{
59+
textView.setTextColor(getResources().getColor(R.color.colorPrimary));
60+
textView.setText("Location from System Provider");
61+
}
62+
}
63+
}
64+
});
1365
}
14-
}
66+
}

MockLocationDetector/app/src/main/res/layout/activity_main.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
tools:context=".MainActivity">
88

99
<TextView
10+
android:id="@+id/textView"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13+
android:text="Mock Location Detector"
1314
app:layout_constraintBottom_toBottomOf="parent"
1415
app:layout_constraintLeft_toLeftOf="parent"
1516
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
17+
app:layout_constraintTop_toTopOf="parent"
18+
android:textSize="25sp"
19+
android:textStyle="bold"
20+
android:padding="25dp"
21+
/>
1722

1823
</androidx.constraintlayout.widget.ConstraintLayout>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<color name="colorPrimary">#008577</color>
4-
<color name="colorPrimaryDark">#00574B</color>
3+
<color name="colorPrimary">#52afc0</color>
4+
<color name="colorPrimaryDark">#2098ae</color>
55
<color name="colorAccent">#D81B60</color>
6+
<color name="green">#008577</color>
7+
68
</resources>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="app_name">MockLocationDetector</string>
2+
<string name="app_name">Mock Location Detector</string>
33
</resources>

0 commit comments

Comments
 (0)