Skip to content

Commit c9d6642

Browse files
Merge branch 'master' into update-coverage-shield
2 parents 12ced16 + aa2c620 commit c9d6642

File tree

5 files changed

+240
-27
lines changed

5 files changed

+240
-27
lines changed

Parse/src/main/java/com/parse/OfflineQueryLogic.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,23 @@ private static boolean matchesGeoIntersectsConstraint(Object constraint, Object
480480
return target.containsPoint(point);
481481
}
482482

483+
/**
484+
* Matches $geoWithin constraints.
485+
*/
486+
private static boolean matchesGeoWithinConstraint(Object constraint, Object value)
487+
throws ParseException {
488+
if (value == null || value == JSONObject.NULL) {
489+
return false;
490+
}
491+
492+
@SuppressWarnings("unchecked")
493+
HashMap<String, List<ParseGeoPoint>> constraintMap =
494+
(HashMap<String, List<ParseGeoPoint>>) constraint;
495+
List<ParseGeoPoint> points = constraintMap.get("$polygon");
496+
ParsePolygon polygon = new ParsePolygon(points);
497+
ParseGeoPoint point = (ParseGeoPoint) value;
498+
return polygon.containsPoint(point);
499+
}
483500
/**
484501
* Returns true iff the given value matches the given operator and constraint.
485502
*
@@ -535,6 +552,9 @@ private static boolean matchesStatelessConstraint(String operator, Object constr
535552
case "$within":
536553
return matchesWithinConstraint(constraint, value);
537554

555+
case "$geoWithin":
556+
return matchesGeoWithinConstraint(constraint, value);
557+
538558
case "$geoIntersects":
539559
return matchesGeoIntersectsConstraint(constraint, value);
540560

Parse/src/main/java/com/parse/ParseQuery.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,12 @@ public Builder<T> whereWithin(String key, ParseGeoPoint southwest, ParseGeoPoint
468468
return addCondition(key, "$within", dictionary);
469469
}
470470

471+
public Builder<T> whereGeoWithin(String key, List<ParseGeoPoint> points) {
472+
Map<String, List<ParseGeoPoint>> dictionary = new HashMap<>();
473+
dictionary.put("$polygon", points);
474+
return addCondition(key, "$geoWithin", dictionary);
475+
}
476+
471477
public Builder<T> whereGeoIntersects(String key, ParseGeoPoint point) {
472478
Map<String, ParseGeoPoint> dictionary = new HashMap<>();
473479
dictionary.put("$point", point);
@@ -1848,6 +1854,28 @@ public ParseQuery<T> whereWithinGeoBox(
18481854
return this;
18491855
}
18501856

1857+
/**
1858+
* Adds a constraint to the query that requires a particular key's
1859+
* coordinates be contained within and on the bounds of a given polygon.
1860+
* Supports closed and open (last point is connected to first) paths
1861+
*
1862+
* Polygon must have at least 3 points
1863+
*
1864+
* @param key
1865+
* The key to be constrained.
1866+
* @param value
1867+
* List<ParseGeoPoint> or ParsePolygon
1868+
* @return this, so you can chain this call.
1869+
*/
1870+
public ParseQuery<T> whereWithinPolygon(String key, List<ParseGeoPoint> points) {
1871+
builder.whereGeoWithin(key, points);
1872+
return this;
1873+
}
1874+
1875+
public ParseQuery<T> whereWithinPolygon(String key, ParsePolygon polygon) {
1876+
return whereWithinPolygon(key, polygon.getCoordinates());
1877+
}
1878+
18511879
/**
18521880
* Add a constraint to the query that requires a particular key's
18531881
* coordinates that contains a {@link ParseGeoPoint}s

Parse/src/test/java/com/parse/OfflineQueryLogicTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,44 @@ public void testMatchesGeoIntersects() throws ParseException {
579579
assertFalse(matches(logic, query, object));
580580
}
581581

582+
@Test
583+
public void testMatchesGeoWithin() throws ParseException {
584+
List<ParseGeoPoint> smallBox = new ArrayList<ParseGeoPoint>();
585+
smallBox.add(new ParseGeoPoint(0,0));
586+
smallBox.add(new ParseGeoPoint(0,1));
587+
smallBox.add(new ParseGeoPoint(1,1));
588+
smallBox.add(new ParseGeoPoint(1,0));
589+
590+
List<ParseGeoPoint> largeBox = new ArrayList<ParseGeoPoint>();
591+
largeBox.add(new ParseGeoPoint(0,0));
592+
largeBox.add(new ParseGeoPoint(0,10));
593+
largeBox.add(new ParseGeoPoint(10,10));
594+
largeBox.add(new ParseGeoPoint(10,0));
595+
596+
ParseGeoPoint point = new ParseGeoPoint(5,5);
597+
598+
//ParsePolygon polygon = new ParsePolygon(points);
599+
600+
ParseObject object = new ParseObject("TestObject");
601+
object.put("point", point);
602+
603+
ParseQuery.State<ParseObject> query;
604+
OfflineQueryLogic logic = new OfflineQueryLogic(null);
605+
query = new ParseQuery.State.Builder<>("TestObject")
606+
.whereGeoWithin("point", largeBox)
607+
.build();
608+
assertTrue(matches(logic, query, object));
609+
610+
query = new ParseQuery.State.Builder<>("TestObject")
611+
.whereGeoWithin("point", smallBox)
612+
.build();
613+
assertFalse(matches(logic, query, object));
614+
615+
// Non-existant key
616+
object = new ParseObject("TestObject");
617+
assertFalse(matches(logic, query, object));
618+
}
619+
582620
//endregion
583621

584622
//region compare

Parse/src/test/java/com/parse/ParseQueryTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,50 @@ public void testWhereWithinGeoBox() throws Exception {
530530
assertTrue(list.contains(pointAgain));
531531
}
532532

533+
@Test
534+
public void testWhereWithinPolygon() throws Exception {
535+
ParseQuery<ParseObject> query = new ParseQuery<>("Test");
536+
ParseGeoPoint point1 = new ParseGeoPoint(10, 10);
537+
ParseGeoPoint point2 = new ParseGeoPoint(20, 20);
538+
ParseGeoPoint point3 = new ParseGeoPoint(30, 30);
539+
540+
List<ParseGeoPoint> points = Arrays.asList(point1, point2, point3);
541+
query.whereWithinPolygon("key", points);
542+
543+
// We generate a state to verify the content of the builder
544+
ParseQuery.State state = query.getBuilder().build();
545+
ParseQuery.QueryConstraints queryConstraints = state.constraints();
546+
ParseQuery.KeyConstraints keyConstraints = (ParseQuery.KeyConstraints) queryConstraints.get("key");
547+
Map map = (Map) keyConstraints.get("$geoWithin");
548+
List<Object> list = (List<Object>) map.get("$polygon");
549+
assertEquals(3, list.size());
550+
assertTrue(list.contains(point1));
551+
assertTrue(list.contains(point2));
552+
assertTrue(list.contains(point3));
553+
}
554+
555+
@Test
556+
public void testWhereWithinPolygonWithPolygon() throws Exception {
557+
ParseQuery<ParseObject> query = new ParseQuery<>("Test");
558+
ParseGeoPoint point1 = new ParseGeoPoint(10, 10);
559+
ParseGeoPoint point2 = new ParseGeoPoint(20, 20);
560+
ParseGeoPoint point3 = new ParseGeoPoint(30, 30);
561+
562+
List<ParseGeoPoint> points = Arrays.asList(point1, point2, point3);
563+
query.whereWithinPolygon("key", new ParsePolygon(points));
564+
565+
// We generate a state to verify the content of the builder
566+
ParseQuery.State state = query.getBuilder().build();
567+
ParseQuery.QueryConstraints queryConstraints = state.constraints();
568+
ParseQuery.KeyConstraints keyConstraints = (ParseQuery.KeyConstraints) queryConstraints.get("key");
569+
Map map = (Map) keyConstraints.get("$geoWithin");
570+
List<Object> list = (List<Object>) map.get("$polygon");
571+
assertEquals(3, list.size());
572+
assertTrue(list.contains(point1));
573+
assertTrue(list.contains(point2));
574+
assertTrue(list.contains(point3));
575+
}
576+
533577
@Test
534578
public void testWherePolygonContains() throws Exception {
535579
ParseQuery<ParseObject> query = new ParseQuery<>("Test");

README.md

Lines changed: 110 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,106 @@
11
# Parse SDK for Android
2-
[![Build Status][build-status-svg]][build-status-link]
3-
[![Coverage Status][coverage-status-svg]][coverage-status-link]
2+
43
[![Maven Central][maven-svg]][maven-link]
4+
[![Dependencies][dependencies-svg]][dependencies-link]
5+
[![References][references-svg]][references-link]
56
[![License][license-svg]][license-link]
67

7-
[![Join Chat](https://img.shields.io/badge/gitter-join%20chat%20%E2%86%92-brightgreen.svg)](https://gitter.im/ParsePlatform/Chat)
8+
[![Build Status][build-status-svg]][build-status-link]
9+
[![Coverage Status][coverage-status-svg]][coverage-status-link]
810

11+
[![Join Chat](https://img.shields.io/badge/gitter-join%20chat%20%E2%86%92-brightgreen.svg)](https://gitter.im/ParsePlatform/Chat)
912

1013
A library that gives you access to the powerful Parse cloud platform from your Android app.
11-
For more information about Parse and its features, see [the website][parseplatform.org] and [getting started][guide].
12-
13-
## Download
14-
Add the dependency in Gradle:
15-
16-
```groovy
17-
dependencies {
18-
compile 'com.parse:parse-android:1.15.8'
19-
}
20-
```
21-
22-
Snapshots of the development version are available in [jFrog's `snapshots` repository][snap].
14+
For more information about Parse and its features, see [the website][parseplatform.org], [blog][blog] and [getting started][guide].
15+
16+
## Getting Started
17+
### Installation
18+
- **Option 1:** Gradle
19+
20+
Add dependency to the application level `build.gradle` file.
21+
22+
```groovy
23+
dependencies {
24+
compile 'com.parse:parse-android:1.15.8'
25+
}
26+
```
27+
28+
Snapshots of the development version are available in [jFrog's `snapshots` repository][snap].
29+
30+
- **Option 2:** Compiling for yourself into AAR file
31+
32+
If you want to manually compile the SDK, begin by cloning the repository locally or retrieving the source code for a particular [release][releases]. Open the project in Android Studio and run the following commands in the Terminal of Android Studio:
33+
34+
```
35+
./gradlew clean jarRelease
36+
```
37+
Output file can be found in `Parse/build/outputs/` with extension .aar
38+
39+
You can link to your project to your AAR file as you please.
40+
41+
### Setup
42+
- **Option 1:** Setup in the Manifest
43+
44+
You may define `com.parse.SERVER_URL` and `com.parse.APPLICATION_ID` meta-data in your `AndroidManifest.xml`:
45+
46+
```
47+
<application ...>
48+
<meta-data
49+
android:name="com.parse.SERVER_URL"
50+
android:value="@string/parse_server_url" />
51+
<meta-data
52+
android:name="com.parse.APPLICATION_ID"
53+
android:value="@string/parse_app_id" />
54+
...
55+
</application>
56+
```
57+
58+
Initialize Parse in a custom class that extends `Application`:
59+
```
60+
import com.parse.Parse;
61+
import android.app.Application;
62+
63+
public class App extends Application {
64+
@Override
65+
public void onCreate() {
66+
super.onCreate();
67+
Parse.initialize(this);
68+
}
69+
}
70+
```
71+
72+
- **Option 2:** Setup in the Application
73+
74+
Initialize Parse in a custom class that extends `Application`:
75+
```
76+
import com.parse.Parse;
77+
import android.app.Application;
78+
79+
public class App extends Application {
80+
@Override
81+
public void onCreate() {
82+
super.onCreate();
83+
Parse.initialize(new Parse.Configuration.Builder(this)
84+
.applicationId("YOUR_APP_ID")
85+
.server("http://localhost:1337/parse/")
86+
.build()
87+
);
88+
}
89+
}
90+
```
91+
92+
For either option, the custom `Application` class must be registered in `AndroidManifest.xml`:
93+
```
94+
<application
95+
android:name=".App"
96+
...>
97+
...
98+
</application>
99+
```
23100

24101
## Usage
25102
Everything can done through the supplied gradle wrapper:
26103

27-
### Compile a JAR
28-
```
29-
./gradlew clean jarRelease
30-
```
31-
Outputs can be found in `Parse/build/libs/`
32-
33104
### Run the Tests
34105
```
35106
./gradlew clean testDebug
@@ -43,7 +114,7 @@ Results can be found in `Parse/build/reports/`
43114
Results can be found in `Parse/build/reports/`
44115

45116
## How Do I Contribute?
46-
We want to make contributing to this project as easy and transparent as possible. Please refer to the [Contribution Guidelines](CONTRIBUTING.md).
117+
We want to make contributing to this project as easy and transparent as possible. Please refer to the [Contribution Guidelines][contributing].
47118

48119
## Other Parse Projects
49120

@@ -65,23 +136,35 @@ We want to make contributing to this project as easy and transparent as possible
65136
As of April 5, 2017, Parse, LLC has transferred this code to the parse-community organization, and will no longer be contributing to or distributing this code.
66137

67138
[parseplatform.org]: http://parseplatform.org/
139+
[blog]: http://blog.parse.com/
68140
[guide]: http://docs.parseplatform.org/android/guide/
69141

70142
[latest]: https://search.maven.org/remote_content?g=com.parse&a=parse-android&v=LATEST
71143
[snap]: https://oss.jfrog.org/artifactory/oss-snapshot-local/com/parse/parse-android/
144+
145+
[maven-svg]: https://maven-badges.herokuapp.com/maven-central/com.parse/parse-android/badge.svg?style=flat
146+
[maven-link]: https://maven-badges.herokuapp.com/maven-central/com.parse/parse-android
147+
148+
[dependencies-svg]: https://www.versioneye.com/java/com.parse:parse-android/badge.svg?style=flat-square
149+
[dependencies-link]: https://www.versioneye.com/java/com.parse:parse-android
150+
151+
[references-svg]: https://www.versioneye.com/java/com.parse:parse-android/reference_badge.svg?style=flat-square
152+
[references-link]: https://www.versioneye.com/java/com.parse:parse-android/references
153+
154+
[license-svg]: https://img.shields.io/badge/license-BSD-lightgrey.svg
155+
[license-link]: https://github.com/parse-community/Parse-SDK-Android/blob/master/LICENSE
72156

73157
[build-status-svg]: https://travis-ci.org/parse-community/Parse-SDK-Android.svg?branch=master
74158
[build-status-link]: https://travis-ci.org/parse-community/Parse-SDK-Android
75159

76160
[coverage-status-svg]: https://img.shields.io/codecov/c/github/parse-community/Parse-SDK-Android/master.svg
77161
[coverage-status-link]: https://codecov.io/github/parse-community/Parse-SDK-Android?branch=master
78-
[maven-svg]: https://maven-badges.herokuapp.com/maven-central/com.parse/parse-android/badge.svg?style=flat
79-
[maven-link]: https://maven-badges.herokuapp.com/maven-central/com.parse/parse-android
80-
162+
81163
[parseui-link]: https://github.com/parse-community/ParseUI-Android
82164
[parselivequery-link]: https://github.com/parse-community/ParseLiveQuery-Android
165+
83166
[parsefacebookutils-link]: https://github.com/parse-community/ParseFacebookUtils-Android
84167
[parsetwitterutils-link]: https://github.com/parse-community/ParseTwitterUtils-Android
85168

86-
[license-svg]: https://img.shields.io/badge/license-BSD-lightgrey.svg
87-
[license-link]: https://github.com/parse-community/Parse-SDK-Android/blob/master/LICENSE
169+
[releases]: https://github.com/parse-community/Parse-SDK-Android/releases
170+
[contributing]: CONTRIBUTING.md

0 commit comments

Comments
 (0)