content: package amotz.example.com.mocklocationfordeveloper;
  
  import android.app.AppOpsManager;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.location.Location;
  import android.os.Build;
  import android.provider.Settings;
  import android.support.annotation.NonNull;
  import android.support.annotation.Nullable;
  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.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Toast;
  
  import com.google.android.gms.common.ConnectionResult;
  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 java.util.Locale;
  
  public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
          GoogleApiClient.OnConnectionFailedListener,
          LocationListener {
  
      private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 1;
      String TAG = "MainActivity";
  
      /* use for test */
      final float dummyLat = -10;
      final float dummylong = 10;
      GoogleApiClient mGoogleApiClient;
      private LocationRequest mLocationRequest;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if (!isMockSettingsON()) {
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
              builder.setMessage("In order to use this app you must enable mock location do you want to enable it now?").setTitle("Mock location is not enable");
              builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                      Intent i = new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
                      startActivity(i);
                  }
              });
              builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                      finish();
                  }
              });
              AlertDialog dialog = builder.create();
              dialog.show();
          }
  
          if (mGoogleApiClient == null) {
              mGoogleApiClient = new GoogleApiClient.Builder(this)
                      .addConnectionCallbacks(this)
                      .addOnConnectionFailedListener(this)
                      .addApi(LocationServices.API)
                      .build();
          }
          createLocationRequest();
  
      }
  
      @Override
      protected void onStart() {
          mGoogleApiClient.connect();
          super.onStart();
  
      }
  
      @Override
      protected void onStop() {
          mGoogleApiClient.disconnect();
          super.onStop();
      }
  
      private boolean isMockSettingsON() {
          boolean isMockLocation = false;
          try
          {
              //if marshmallow
              if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
              {
                  AppOpsManager opsManager = (AppOpsManager) this.getSystemService(Context.APP_OPS_SERVICE);
                  isMockLocation = (opsManager.checkOp(AppOpsManager.OPSTR_MOCK_LOCATION, android.os.Process.myUid(), BuildConfig.APPLICATION_ID)== AppOpsManager.MODE_ALLOWED);
              }
              else
              {
                  // in marshmallow this will always return true
                  isMockLocation = !android.provider.Settings.Secure.getString(this.getContentResolver(), "mock_location").equals("0");
              }
          }
          catch (Exception e)
          {
              return isMockLocation;
          }
  
          return isMockLocation;
  
      }
  
      public void buttonCmd(View view) {
      }
  
      public void buttonInterval(View view) {
      }
  
      public void testMock(View view) { // On some version do like this
          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) {
              askRunTimePermissions();
              return;
          }
          if (mGoogleApiClient.isConnected()) {
              LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
              Intent intent = new Intent();
              intent.putExtra("lat", String.valueOf(dummyLat));
              intent.putExtra("lon", String.valueOf(dummylong));
              intent.setAction("send.mock");
              sendBroadcast(intent);
          } else {
              Toast.makeText(this, "Failed - Google play services is not connected", Toast.LENGTH_SHORT).show();
          }
      }
  
      @Override
      public void onConnected(@Nullable Bundle bundle) {
          Log.i(TAG, "onConnected");
      }
  
      @Override
      public void onConnectionSuspended(int i) {
  
      }
  
      @Override
      public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  
      }
  
      private void askRunTimePermissions() {
          ActivityCompat.requestPermissions(this
                  ,new  String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                  MY_PERMISSIONS_REQUEST_FINE_LOCATION);
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode,
                                             @NonNull String permissions[], @NonNull int[] grantResults) {
          switch (requestCode) {
              case MY_PERMISSIONS_REQUEST_FINE_LOCATION: {
                  // If request is cancelled, the result arrays are empty.
                  if (grantResults.length > 0
                          && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                      Toast.makeText(this,"Got permission location ",Toast.LENGTH_SHORT).show();
  
                  } else {
                      Toast.makeText(this,"No location permissions",Toast.LENGTH_SHORT).show();
                      finish();
                  }
              }
          }
      }
  
      private void createLocationRequest() {
          mLocationRequest = new LocationRequest();
          mLocationRequest.setInterval(0);
          mLocationRequest.setSmallestDisplacement(1);
          mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
      }
  
      @Override
      public void onLocationChanged(Location location) {
          if (location.getLatitude() == dummyLat && location.getLongitude() == dummylong) {
              Toast.makeText(this,"Mock location application is working",Toast.LENGTH_SHORT).show();
          }else {
              Toast.makeText(this,String.format(Locale.US,"setting mock to Latitude=%f, Longitude=%f Altitude=%f Accuracy=%f",
                      location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getAccuracy()),Toast.LENGTH_SHORT).show();
          }
      }
  }
  
  