Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Mock Location is enabled or disabled in Flutter

Tags:

flutter

My question is that I am using flutter platform to develop an app for my client and I want that my developed app should be able to be detect mock location status from android phone settings so I can check whether the location is coming from gps provider or mock location app. And if mock location is enabled then my app should throw an error msg

like image 901
CA VIVEK GARG Avatar asked Feb 28 '19 16:02

CA VIVEK GARG


People also ask

How can you tell if someone is using a fake location?

There is no way to be sure of the location provided to Maps. Maps does not control the location information, rather it asks the device for its location and uses whatever the device reports back. If the device reports the wrong location Maps has no way of knowing it is wrong.

How do I find a mock location?

Go to your “Settings”, “Systems”, “About Device” and tap multiple times on “Build number” and activate the Developer Mode. A new “Developer Options” menu will be available under “Settings” / “Systems” In the “Developer Options” menu, scroll down to “Debugging” and activate the “Allow mock locations”.

How can I change my location without mock location?

Fake Location App Fone - Virtual Location, another app you can use to fake GPS without mock location-enabled is Fake GPS Location. This app is quite common as many people use it to spoof their location. Downloading this app is easy because you can get it from Google Play Store.


2 Answers

Barzan's answer is very good, there's also a Flutter package named trust_location, you can find it here.

You can use it as following to check mock location:

bool isMockLocation = await TrustLocation.isMockLocation;

So, I recommend to use it.

like image 125
Shady Boshra Avatar answered Sep 20 '22 20:09

Shady Boshra


i had the same problem and i fixed it by coding in java and implement in flutter project. here is what i did: 1) add this to your Main_Activity in flutter project.

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

import java.util.List;

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "samples.flutter.io/location";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);

        new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
                new MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall call, Result result) {

                        if (call.method.equals("getLocation")) {
                            boolean b = getMockLocation();
                            result.success(b);
                        } else {
                            result.notImplemented();
                        }
                    }
                });

    }

    public static boolean isMockSettingsON(Context context) {
        // returns true if mock location enabled, false if not enabled.
        if (VERSION.SDK_INT >= VERSION_CODES.CUPCAKE) {
            if (Settings.Secure.getString(context.getContentResolver(),
                    Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
                return false;
            else
                return true;
        }
        return false;
    }



    public static boolean areThereMockPermissionApps(Context context) {
        int count = 0;

        PackageManager pm = context.getPackageManager();
        List<ApplicationInfo> packages =
                pm.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo applicationInfo : packages) {
            try {
                PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName,
                        PackageManager.GET_PERMISSIONS);

                // Get Permissions
                String[] requestedPermissions = packageInfo.requestedPermissions;

                if (requestedPermissions != null) {
                    for (int i = 0; i < requestedPermissions.length; i++) {
                        if (requestedPermissions[i]
                                .equals("android.permission.ACCESS_MOCK_LOCATION")
                                && !applicationInfo.packageName.equals(context.getPackageName())) {
                            count++;
                        }
                    }
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.e("Got exception " , e.getMessage());
            }
        }

        if (count > 0)
            return true;
        return false;
    }


    private boolean getMockLocation() {
        boolean b ;
        b= areThereMockPermissionApps(MainActivity.this);
        return b;
    }
}

2) use it in your flutter_dart Code like this:

  static const platform = const MethodChannel('samples.flutter.io/location');



bool mocklocation = false;
  Future<void> _getMockLocation() async {
    bool b;
    try {
      final bool result = await platform.invokeMethod('getLocation');
      b = result;
    } on PlatformException catch (e) {
      b = false;
    }

    mocklocation = b;
  }


if (mocklocation == true) {

       return showDialog(
            barrierDismissible: false,
            context: context,
            builder: (BuildContext context) {
              return WillPopScope(
                onWillPop: (){},
                              child: AlertDialog(
                  title: Text('Location'),
                  content: Text('Your Location is fake'),
                ),
              );
            });
    }

3) for more information and example: https://flutter.dev/docs/development/platform-integration/platform-channels

like image 33
Barzan Saeedpour Avatar answered Sep 21 '22 20:09

Barzan Saeedpour