isFromMockProvider

939 views
Skip to first unread message

froyop...@gmail.com

unread,
Mar 9, 2016, 5:38:11 AM3/9/16
to Tasker
Return:
%mp

Class Or Object:
Location


Function:
isFromMockProvider
{boolean}

I get this error

Java Function: java.lang.NullPointerException: expected receiver of type android.location.Location, but got null

My SDK value check returns 19.

Trying to get this bit of code implemented but it's proving to be more difficult for me than I thought.

if (android.os.Build.VERSION.SDK_INT >= 18) {
    isMock
= location.isFromMockProvider();
} else {
    isMock
= Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");
}

Help would be appreciated.

Juergen Gruen

unread,
Mar 9, 2016, 2:03:25 PM3/9/16
to Tasker
Hi!


Am Mittwoch, 9. März 2016 05:38:11 UTC-5 schrieb Froyo:
Return:
%mp

Class Or Object:
Location


Function:
isFromMockProvider
{boolean}
hod
In this context, "Location" is a class. I guess you typed in the function "isFromMockProvider" manually? There is no such static method.


 

if (android.os.Build.VERSION.SDK_INT >= 18) {
    isMock
= location.isFromMockProvider();
} else {
    isMock
= Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");
}

 Here, "location" is an instance of the class "Location". Where from is this example? How do you create "location"?

Froyo

unread,
Mar 9, 2016, 3:38:34 PM3/9/16
to Tasker
Got the example code from here:


To create location, I hit the magnifying glass button and for filter I typed in "location". I selected the entry that looks like this:

Location
android
.location

Froyo

unread,
Mar 9, 2016, 3:41:21 PM3/9/16
to Tasker
And yes, I had to type "isFromMockProvider" manually. I read somewhere tasker can't list all the functions yet and it had to be entered manually. I don't remember where I read that.

Juergen Gruen

unread,
Mar 9, 2016, 4:22:47 PM3/9/16
to Tasker


Am Mittwoch, 9. März 2016 15:41:21 UTC-5 schrieb Froyo:
And yes, I had to type "isFromMockProvider" manually. I read somewhere tasker can't list all the functions yet and it had to be entered manually. I don't remember where I read that.
... but not in this case. You need some basics in object-oriented programming.

"location" (object) is different from "Location" (class) as mentioned above.

And I do not think you can create a location object with the java capabilities of tasker (in order to check for mock locations).

You can implement the second part:

isMock = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");


IsMockLocation (25)
    A1: Java Function [ Return:cr Class Or Object:CONTEXT Function:getContentResolver
{ContentResolver} () Param: Param: Param: Param: Param: Param: Param: ]
    A2: Java Function [ Return:%mock Class Or Object:Secure Function:getString
{String} (ContentResolver, String) Param:cr Param:mock_location Param: Param: Param: Param: Param: ]
    A3: Flash [ Text:%mock Long:Off ]


Juergen Gruen

unread,
Mar 9, 2016, 4:54:01 PM3/9/16
to Tasker
Where does your location data (lat, lon, ...) come from?

Froyo

unread,
Mar 9, 2016, 8:09:59 PM3/9/16
to Tasker
So that's what I was doing wrong for the second part. I was entering "ALLOW_MOCK_LOCATION" for the parameter instead of "mock_location"

Also, I didn't notice the magnifying glass pulls up "Class" while the java cup pulls up "Java Object"

Currently, I'm toying with "Public Constructors". Picking "new {Location} (String)" opens up isFromMockProvider to me but all data from the new object(?) is empty.

A1: Java Function [ Return:locationobject Class Or Object:Location Function:new
{Location} (String) Param: Param: Param: Param: Param: Param: Param: ]
A2
: Java Function [ Return:%mock Class Or Object:locationobject Function:isFromMockProvider
{boolean} () Param: Param: Param: Param: Param: Param: Param: ]  
A3
: Flash [ Text:%mock Long:Off ]

 
The second option is new {Location} (Location) but again I don't really understand what I'm suppose to pass as a parameter.

Construct a new Location object that is copied from an existing one.


If that is what I think it is, it might be what I'm looking for.

Froyo

unread,
Mar 9, 2016, 8:11:45 PM3/9/16
to Tasker


On Wednesday, March 9, 2016 at 3:54:01 PM UTC-6, Juergen Gruen wrote:
Where does your location data (lat, lon, ...) come from?


I run "Get Location GPS" to update the coordinates and then I pull the data from %LOC variable.

Juergen Gruen

unread,
Mar 10, 2016, 2:30:58 AM3/10/16
to Tasker
Tasker only returns two floating point values, lat and lon. How would you decide if this comes from a mock provider?

You need a location object created by the android api, I guess.


Check out LocationManager at developer.android.com (BTW, always check the documentation here, when working with Java Function in Tasker. Extremely helpful!)

Maybe you can use

public Location getLastKnownLocation (String provider)

or


public void requestSingleUpdate (String provider, PendingIntent intent)


Froyo

unread,
Mar 10, 2016, 4:33:55 AM3/10/16
to Tasker
Wow, it worked. Thank you. 

For anyone else interested:

A1: Get Location [ Source:GPS Timeout (Seconds):100 Continue Task Immediately:Off Keep Tracking:Off ]
A2
: Java Function [ Return:newlocationmanager Class Or Object:CONTEXT Function:getSystemService
{Object} (String) Param:location Param: Param: Param: Param: Param: Param: ]
A3
: Java Function [ Return:lastknownlocation Class Or Object:newlocationmanager Function:getLastKnownLocation
{Location} (String) Param:gps Param: Param: Param: Param: Param: Param: ]
A4
: Java Function [ Return:locationobject Class Or Object:Location Function:new
{Location} (Location) Param:lastknownlocation Param: Param: Param: Param: Param: Param: ]
A5
: Java Function [ Return:%mock Class Or Object:locationobject Function:isFromMockProvider
{boolean} () Param: Param: Param: Param: Param: Param: Param: ]
A6
: Flash [ Text:%mock Long:Off ]

Note: If the last location came from a spoof and the spoof is disabled, lastknownlocation will reference an object that doesn't exist and the script crashes when the script attempts to access the object. Correct me if I'm wrong.

Froyo

unread,
Mar 10, 2016, 4:36:01 AM3/10/16
to Tasker
Addition to the note above, that's assuming GPS doesn't refresh. I added the "Get Location" at the top to prevent the crash.

Froyo

unread,
Mar 10, 2016, 4:41:19 AM3/10/16
to Tasker
Another note! This is one of those times when the function won't be listed for the object. It probably has something to with the return from getSystemService being "Object" as opposed to "LocationManager".

Froyo

unread,
Mar 10, 2016, 4:53:16 AM3/10/16
to Tasker
So many notes, please merge.

Last note, I swear! You can avoid the situation where the functions aren't listed by "type casting" the variable before assigning it as a return variable. I forgot I did that earlier and removed it because it wasn't necessary for the code to function but it IS convenient.

request (40)

A1
: Get Location [ Source:GPS Timeout (Seconds):100 Continue Task Immediately:Off Keep Tracking:Off ]

A2
: Java Function [ Return:newlocationmanager Class Or Object:LocationManager Function:new
{LocationManager} (Context, android.location.ILocationManager) Param: Param: Param: Param: Param: Param: Param: ]
A3
: Java Function [ Return:newlocationmanager Class Or Object:CONTEXT Function:getSystemService
{Object} (String) Param:location Param: Param: Param: Param: Param: Param: ]

A4
: Java Function [ Return:lastknownlocation Class Or Object:newlocationmanager Function:getLastKnownLocation
{Location} (String) Param:gps Param: Param: Param: Param: Param: Param: ]

A5
: Java Function [ Return:locationobject Class Or Object:Location Function:new

{Location} (Location) Param:lastknownlocation Param: Param: Param: Param: Param: Param: ]

A6
: Java Function [ Return:%mock Class Or Object:locationobject Function:isFromMockProvider
{boolean} () Param: Param: Param: Param: Param: Param: Param: ]

A7
: Flash [ Text:%mock Long:Off ]


Juergen Gruen

unread,
Mar 10, 2016, 8:35:15 AM3/10/16
to Tasker
lastknownlocation already is an object of class Location. So you do not need A5. Just call lastknownlocation.isFromMockProvider() directly...

Froyo Pancake

unread,
Mar 10, 2016, 1:12:22 PM3/10/16
to tas...@googlegroups.com

I overlooked that completely. Was so focused on getting that second public constructor to work that I lost sight of the fact that the location it needs to work is already a location object.

A face palm moment.

Thank you, again.

Reply all
Reply to author
Forward
0 new messages