Skip to content

Commit

Permalink
Replacing LocationClient with FusedLocationApi #158
Browse files Browse the repository at this point in the history
Change the location provider flow #157
  • Loading branch information
wf9a5m75 committed Sep 1, 2014
1 parent 2ae3095 commit 86f1d14
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 160 deletions.
Binary file modified platforms/android/libs/google-play-services.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions platforms/android/res/values/version.xml
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="google_play_services_version">4452000</integer>
</resources>
<integer name="google_play_services_version">5089000</integer>
</resources>
2 changes: 1 addition & 1 deletion plugin.xml
Expand Up @@ -39,7 +39,7 @@
</feature>
</config-file>

<dependency id="com.google.playservices" url="https://github.com/wf9a5m75/google-play-services" commit="master" />
<dependency id="com.google.playservices" />
<dependency id="android.support.v4" />

<!-- plugin src files -->
Expand Down
6 changes: 3 additions & 3 deletions plugins/android.json
Expand Up @@ -51,14 +51,14 @@
"plugin.google.maps": {
"PACKAGE_NAME": "com.example.myapp"
},
"com.google.playservices": {
"PACKAGE_NAME": "com.example.myapp"
},
"plugin.http.request": {
"PACKAGE_NAME": "com.example.myapp"
},
"org.apache.cordova.device": {
"PACKAGE_NAME": "com.example.myapp"
},
"com.google.playservices": {
"PACKAGE_NAME": "com.example.myapp"
}
},
"dependent_plugins": {}
Expand Down
2 changes: 1 addition & 1 deletion plugins/com.google.playservices/.fetch.json
@@ -1 +1 @@
{"source":{"type":"git","url":"https://github.com/wf9a5m75/google-play-services.git","subdir":"."}}
{"source":{"type":"local","path":"/Users/wf9a5m75/.plugman/cache/com.google.playservices/19.0.0/package"}}
3 changes: 2 additions & 1 deletion plugins/com.google.playservices/README.md
Expand Up @@ -13,4 +13,5 @@ See for example [chrome.identity](https://github.com/MobileChromeApps/mobile-chr
1. Install the .jar via the Android SDK Manager (`android sdk`)
2. Copy it from `android-sdk/extras/google/google_play_services/libproject/google-play-services_lib/libs/google-play-services.jar`
3. Update the version number in `plugin.xml` to match `Pkg.Revision` from `android-sdk/extras/google/google_play_services/source.properties`
4. Run `plugman publish .` to publish to registry
4. Copy in the new `version.xml` from `android-sdk/extras/google/google_play_services/libproject/google-play-services_lib/res/values`
5. Run `plugman publish .` to publish to registry
Binary file modified plugins/com.google.playservices/google-play-services.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion plugins/com.google.playservices/plugin.xml
Expand Up @@ -6,7 +6,7 @@
-->
<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
id="com.google.playservices"
version="17.0.0">
version="19.0.0">
<engines>
<engine name="cordova" version=">=3.0.0" />
</engines>
Expand Down
4 changes: 2 additions & 2 deletions plugins/com.google.playservices/version.xml
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="google_play_services_version">4452000</integer>
</resources>
<integer name="google_play_services_version">5089000</integer>
</resources>
6 changes: 3 additions & 3 deletions plugins/ios.json
Expand Up @@ -111,14 +111,14 @@
"plugin.google.maps": {
"PACKAGE_NAME": "com.example.myapp"
},
"com.google.playservices": {
"PACKAGE_NAME": "com.example.myapp"
},
"plugin.http.request": {
"PACKAGE_NAME": "com.example.myapp"
},
"org.apache.cordova.device": {
"PACKAGE_NAME": "com.example.myapp"
},
"com.google.playservices": {
"PACKAGE_NAME": "com.example.myapp"
}
},
"dependent_plugins": {}
Expand Down
139 changes: 90 additions & 49 deletions src/android/plugin/google/maps/GoogleMaps.java
@@ -1,6 +1,5 @@
package plugin.google.maps;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -24,7 +23,6 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
Expand All @@ -35,38 +33,32 @@
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Typeface;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebChromeClient;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
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 com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
Expand Down Expand Up @@ -97,7 +89,8 @@ public class GoogleMaps extends CordovaPlugin implements View.OnClickListener, O
OnInfoWindowClickListener, OnMapClickListener, OnMapLongClickListener,
OnCameraChangeListener, OnMapLoadedCallback, OnMarkerDragListener,
OnMyLocationButtonClickListener,
ConnectionCallbacks, OnConnectionFailedListener, InfoWindowAdapter {
ConnectionCallbacks, OnConnectionFailedListener, InfoWindowAdapter,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private final String TAG = "GoogleMapsPlugin";
private final HashMap<String, PluginEntry> plugins = new HashMap<String, PluginEntry>();
private float density;
Expand All @@ -117,9 +110,9 @@ private enum TEXT_STYLE_ALIGNMENTS {
private ViewGroup root;
private final int CLOSE_LINK_ID = 0x7f999990; //random
private final int LICENSE_LINK_ID = 0x7f99991; //random
public LocationClient locationClient = null;
private final String PLUGIN_VERSION = "1.2.0 beta3";
private MyPluginLayout mPluginLayout = null;
public GoogleApiClient googleApiClient = null;

@Override
public void initialize(final CordovaInterface cordova, final CordovaWebView webView) {
Expand Down Expand Up @@ -172,6 +165,7 @@ public void sendPluginResult(PluginResult pluginResult) {
cordova.getActivity().runOnUiThread(new Runnable() {
@SuppressLint("NewApi")
public void run() {
root.setBackgroundColor(Color.WHITE);
mPluginLayout = new MyPluginLayout(webView);
if (VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Expand Down Expand Up @@ -498,28 +492,20 @@ public void onClick(DialogInterface dialog,int id) {
map = mapView.getMap();

//controls
Boolean isEnabled = true;
if (params.has("controls")) {
JSONObject controls = params.getJSONObject("controls");

if (controls.has("myLocationButton")) {
isEnabled = controls.getBoolean("myLocationButton");
Boolean isEnabled = controls.getBoolean("myLocationButton");
map.setMyLocationEnabled(isEnabled);
}
}

if (isEnabled) {
try {
Constructor<LocationClient> constructor = LocationClient.class.getConstructor(Context.class, GooglePlayServicesClient.ConnectionCallbacks.class, GooglePlayServicesClient.OnConnectionFailedListener.class);
this.locationClient = constructor.newInstance(this.activity, this, this);
} catch (Exception e) {}

//this.locationClient = new LocationClient(this.activity, this, this);
if (this.locationClient != null) {
// The LocationClient class is available.
this.locationClient.connect();
}
}
try {
this.googleApiClient = new GoogleApiClient.Builder(this.activity)
.addApi(LocationServices.API)
.build();
} catch (Exception e) {}

// Set event listener
map.setOnCameraChangeListener(this);
Expand Down Expand Up @@ -824,8 +810,72 @@ private void isAvailable(final JSONArray args, final CallbackContext callbackCon
callbackContext.success();
}
private void getMyLocation(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
JSONObject result = null;
final LocationRequest locationRequest = new LocationRequest();
int priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
JSONObject params = args.getJSONObject(0);
if (params.has("enableHighAccuracy")) {
boolean enableHighAccuracy = params.getBoolean("enableHighAccuracy");
if (enableHighAccuracy) {
priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
}
}
locationRequest.setPriority(priority);


if (googleApiClient.isConnected() == false) {
googleApiClient.connect();

googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnectionSuspended(int cause) {}

@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest, new LocationListener() {

@Override
public void onLocationChanged(Location location) {
JSONObject result;
try {
result = PluginUtil.location2Json(location);
result.put("status", true);
callbackContext.success(result);
} catch (JSONException e) {}
googleApiClient.disconnect();
}

});
}
});
googleApiClient.registerConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {

@Override
public void onConnectionFailed(ConnectionResult result) {
Log.e("CordovaLog", result.toString());
callbackContext.error("Can not connect to Google APIs client");
}
});
return;
} else {
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest, new LocationListener() {

@Override
public void onLocationChanged(Location location) {
Log.d("CordovaLog", "-->onLocationChanged");
JSONObject result;
try {
result = PluginUtil.location2Json(location);
result.put("status", true);
callbackContext.success(result);
} catch (JSONException e) {}
}

});
}

/*
if (this.locationClient == null) {
LocationManager locationManager = (LocationManager) this.activity.getSystemService(Context.LOCATION_SERVICE);
Expand Down Expand Up @@ -931,23 +981,8 @@ public void onClick(DialogInterface dialog, int which) {
result.put("error_message", "Location provider is not available.");
} catch (JSONException e) {}
callbackContext.error(result);
/*
JSONObject latLng = new JSONObject();
latLng.put("lat", 0);
latLng.put("lng", 0);
result = new JSONObject();
result.put("latLng", latLng);
result.put("speed", null);
result.put("bearing", null);
result.put("altitude", 0);
result.put("accuracy", null);
result.put("provider", null);
result.put("elapsedRealtimeNanos", System.nanoTime());
result.put("time", System.currentTimeMillis());
result.put("hashCode", -1);
*/
}
*/
}

private void showLicenseText() {
Expand Down Expand Up @@ -1359,8 +1394,8 @@ public void onResume(boolean multitasking) {

@Override
public void onDestroy() {
if (this.locationClient != null) {
this.locationClient.disconnect();
if (this.googleApiClient != null) {
this.googleApiClient.disconnect();
}
if (mapView != null) {
mapView.onDestroy();
Expand Down Expand Up @@ -1602,8 +1637,14 @@ private void remove(JSONArray args, CallbackContext callbackContext) {
mapView = null;
windowLayer = null;
mapDivLayoutJSON = null;
locationClient.disconnect();
locationClient = null;
googleApiClient.disconnect();
googleApiClient = null;
callbackContext.success();
}

@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub

}
}
6 changes: 2 additions & 4 deletions src/android/plugin/google/maps/MyPluginLayout.java
Expand Up @@ -8,7 +8,6 @@

import org.apache.cordova.CordovaWebView;

import android.animation.LayoutTransition;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
Expand All @@ -17,7 +16,6 @@
import android.graphics.RectF;
import android.os.Build;
import android.os.Build.VERSION;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -70,6 +68,7 @@ public MyPluginLayout(CordovaWebView webView) {
scrollFrameLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

this.touchableWrapper = new TouchableWrapper(this.context);

}

public void setDrawingRect(float left, float top, float right, float bottom) {
Expand Down Expand Up @@ -195,7 +194,6 @@ public void attachMyView(ViewGroup pluginView) {

frontLayer.addView(webView);
this.addView(frontLayer);

root.addView(this);
}

Expand All @@ -212,7 +210,7 @@ public void scrollTo(int x, int y) {


public void setBackgroundColor(int color) {
this.backgroundView.setBackgroundColor(color);
this.scrollView.setBackgroundColor(color);
}

public void inValidate() {
Expand Down

0 comments on commit 86f1d14

Please sign in to comment.