Skip to content

Commit

Permalink
feat(android): add headingTime, fix headingFilter (#12923)
Browse files Browse the repository at this point in the history
* feat(android): add headingTime, fix headingFilter

* apidoc
  • Loading branch information
m1ga committed Aug 10, 2022
1 parent 20d2ed1 commit 6170a1e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
Expand Up @@ -992,6 +992,30 @@ private KrollDict buildLocationEvent(Location location, LocationProvider locatio
return event;
}

@Kroll.getProperty
public int getHeadingTime()
{
return tiCompass.headingTime;
}

@Kroll.setProperty
public void setHeadingTime(int value)
{
tiCompass.headingTime = value;
}

@Kroll.getProperty
public int getHeadingFilter()
{
return tiCompass.headingFilter;
}

@Kroll.setProperty
public void getHeadingFilter(int value)
{
tiCompass.headingFilter = value;
}

/**
* Convenience method used to package a error into a consumable form
* for the Titanium developer before it is fired back to Javascript.
Expand Down
Expand Up @@ -14,7 +14,6 @@
import org.appcelerator.kroll.KrollPromise;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiSensorHelper;

import android.hardware.GeomagneticField;
Expand Down Expand Up @@ -45,6 +44,8 @@ public class TiCompass implements SensorEventListener
private final Criteria locationCriteria = new Criteria();
private Location geomagneticFieldLocation;
private long lastDeclinationCheck;
public int headingTime = 250;
public int headingFilter = 0;

public TiCompass(GeolocationModule geolocationModule, TiLocation tiLocation)
{
Expand All @@ -71,24 +72,20 @@ public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
long eventTimestamp = event.timestamp / 1000000;

if (eventTimestamp - lastEventInUpdate > 250) {
if (eventTimestamp - lastEventInUpdate > headingTime) {
long actualTimestamp = baseTime.getTimeInMillis() + (eventTimestamp - sensorTimerStart);

lastEventInUpdate = eventTimestamp;

Object filter = geolocationModule.getProperty(TiC.PROPERTY_HEADING_FILTER);
if (filter != null) {
float headingFilter = TiConvert.toFloat(filter);

if (headingFilter != 0) {
if (Math.abs(event.values[0] - lastHeading) < headingFilter) {
return;
}

lastHeading = event.values[0];
}

geolocationModule.fireEvent(TiC.EVENT_HEADING, eventToHashMap(event, actualTimestamp));
geolocationModule.fireSyncEvent(TiC.EVENT_HEADING, eventToHashMap(event, actualTimestamp));
}
}
}
Expand Down
Expand Up @@ -470,7 +470,6 @@ public class TiC
public static final String PROPERTY_HEADER_TITLE = "headerTitle";
public static final String PROPERTY_HEADER_VIEW = "headerView";
public static final String PROPERTY_HEADING = "heading";
public static final String PROPERTY_HEADING_FILTER = "headingFilter";
public static final String PROPERTY_HEIGHT = "height";
public static final String PROPERTY_HIDE_ANNOTATION_WHEN_TOUCH_MAP = "hideAnnotationWhenTouchMap";
public static final String PROPERTY_HIGHLIGHTED_COLOR = "highlightedColor";
Expand Down
54 changes: 33 additions & 21 deletions apidoc/Titanium/Geolocation/Geolocation.yml
Expand Up @@ -112,7 +112,7 @@ description: |
to `true`. In manual mode, the `accuracy` property is not used, and all
configuration is done through the <Titanium.Geolocation.Android> module.
As of Titanium SDK 7.1.0 and later, including the [`ti.playservices`](https://github.com/appcelerator-modules/ti.playservices) module will allow Google Play Services
As of Titanium SDK 7.1.0 and later, including the [`ti.playservices`](https://github.com/appcelerator-modules/ti.playservices) module will allow Google Play Services
to be used by default to obtain location information. This means the provider passed into [createLocationProvider](Titanium.Geolocation.Android.createLocationProvider)
will be ignored, as Google Play Services will select the best provider based on current device conditions.
If Google Play Services is not available it will fallback to previous behaviour of using Android location APIs.
Expand Down Expand Up @@ -305,14 +305,14 @@ methods:
- name: requestTemporaryFullAccuracyAuthorization
summary: Requests the user's permission to temporarily use location services with full accuracy.
description: |
If your app doesn't have permission to access accurate location (see [locationAccuracyAuthorization](Titanium.Geolocation.locationAccuracyAuthorization),
you can use this method to request temporary access to accurate location. This access will expire automatically,
but it won't expire while the user is still engaged with your app. So, for example, while your app is in
the foreground your app will retain the temporary access it was granted. Similarly, if your app starts a
Continuous Background Location session with the background location indicator enabled (see [showBackgroundLocationIndicator](Titanium.Geolocation.showBackgroundLocationIndicator),
your access to accurate location will remain as long as the background location indicator remains enabled.
This allows your app to provide session-oriented experiences which require accurate location (e.g. fitness or navigation),
even if the user has decided not to grant your app persistent access to accurate location.
If your app doesn't have permission to access accurate location (see [locationAccuracyAuthorization](Titanium.Geolocation.locationAccuracyAuthorization),
you can use this method to request temporary access to accurate location. This access will expire automatically,
but it won't expire while the user is still engaged with your app. So, for example, while your app is in
the foreground your app will retain the temporary access it was granted. Similarly, if your app starts a
Continuous Background Location session with the background location indicator enabled (see [showBackgroundLocationIndicator](Titanium.Geolocation.showBackgroundLocationIndicator),
your access to accurate location will remain as long as the background location indicator remains enabled.
This allows your app to provide session-oriented experiences which require accurate location (e.g. fitness or navigation),
even if the user has decided not to grant your app persistent access to accurate location.
You need to add key `NSLocationTemporaryUsageDescriptionDictionary` in `tiapp.xml`. See following example -
Expand Down Expand Up @@ -342,14 +342,14 @@ methods:
</ti:app>
```
When CoreLocation prepares the prompt for display, it will look at the NSLocationTemporaryUsageDescriptionDictionary key
in your tiapp.xml. The value should be a dictionary containing usage descriptions. The purposeKey you provide to
When CoreLocation prepares the prompt for display, it will look at the NSLocationTemporaryUsageDescriptionDictionary key
in your tiapp.xml. The value should be a dictionary containing usage descriptions. The purposeKey you provide to
this method must correspond to an entry in that dictionary.
parameters:
- name: purposeKey
summary: |
A key in the NSLocationTemporaryUsageDescriptionDictionary dictionary of the app's tiapp.xml file.
The value for this key is an app-provided string that describes the reason for accessing
A key in the NSLocationTemporaryUsageDescriptionDictionary dictionary of the app's tiapp.xml file.
The value for this key is an app-provided string that describes the reason for accessing
location data with full accuracy.
type: String
- name: callback
Expand Down Expand Up @@ -564,11 +564,11 @@ properties:
The level of accuracy used when an app isn’t authorized for full accuracy location data.
description: |
Use with [accuracy](Titanium.Geolocation.accuracy).
The accuracy of location data is reduced in both space and time using approaches like
selecting a nearby point of interest and updating the location at most a few times per hour.
The approximate location preserves the user’s country, typically preserves the city,
The accuracy of location data is reduced in both space and time using approaches like
selecting a nearby point of interest and updating the location at most a few times per hour.
The approximate location preserves the user’s country, typically preserves the city,
and is usually within 1–20 kilometers of the actual location.
If your app is authorized to access location information with full accuracy, you can use
If your app is authorized to access location information with full accuracy, you can use
this constant to access location data as if the app didn’t have that authorization.
type: Number
permission: read-only
Expand Down Expand Up @@ -798,7 +798,19 @@ properties:
Set to a value greater than zero to reduce the number of heading events generated.
type: Number
default: 0 (No limit on heading updates)
platforms: [iphone, ipad, macos]
platforms: [android, iphone, ipad, macos]
since: { android: "10.1.0", iphone: "0.8", ipad: "0.8" }

- name: headingTime
summary: Interval in milliseconds before a `heading` event is fired.
description: |
Set to a value greater than zero to reduce the number of heading events generated.
If you target Android 12 and want to use a time below 200ms you need to add the
HIGH_SAMPLING_RATE_SENSORS permission. See [Motion sensors are rate-limited](https://developer.android.com/about/versions/12/behavior-changes-12#motion-sensor-rate-limiting)
type: Number
default: 250
platforms: [android]
since: { android: "10.1.0" }

- name: locationServicesAuthorization
summary: Returns an authorization constant indicating if the application has access to location services.
Expand Down Expand Up @@ -922,8 +934,8 @@ properties:
- name: locationAccuracyAuthorization
summary: A value that indicates the level of location accuracy the app has permission to use.
description: |
If the value of this property is <Titanium.Geolocation.ACCURACY_AUTHORIZATION_FULL>,
you can set any constant to [accuracy](Titanium.Geolocation.accuracy). If value is
If the value of this property is <Titanium.Geolocation.ACCURACY_AUTHORIZATION_FULL>,
you can set any constant to [accuracy](Titanium.Geolocation.accuracy). If value is
<Titanium.Geolocation.ACCURACY_AUTHORIZATION_REDUCED> setting [accuracy](Titanium.Geolocation.accuracy)
to a value other than <Titanium.Geolocation.ACCURACY_REDUCED> has no effect on the location information.
type: Number
Expand Down Expand Up @@ -1074,7 +1086,7 @@ properties:
Otherwise, this value will be -1.
type: Number
since: { android: "3.1.0", iphone: "3.1.0", ipad: "3.1.0" }

- name: accuracy
summary: Estimated accuracy of the geocoding, in meters.
type: Number
Expand Down

0 comments on commit 6170a1e

Please sign in to comment.