Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from cabify/performance-improvements
Browse files Browse the repository at this point in the history
Force hardware acceleration, recalculate layout in another thread
  • Loading branch information
cicloon committed Mar 29, 2016
2 parents 164af2a + d619253 commit b160147
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 73 deletions.
67 changes: 40 additions & 27 deletions src/android/plugin/google/maps/GoogleMaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ public void run() {
e.printStackTrace();
}
*/
if (Build.VERSION.SDK_INT >= 21 || "org.xwalk.core.XWalkView".equals(view.getClass().getName())){
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}

view.setLayerType(View.LAYER_TYPE_HARDWARE, null);


root.setBackgroundColor(Color.WHITE);
if (VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
Expand Down Expand Up @@ -587,6 +587,7 @@ public void onMapReady(GoogleMap googleMap) {
if (args.length() == 3) {
GoogleMaps.this.mapDivLayoutJSON = args.getJSONObject(1);
mPluginLayout.attachMyView(mapView);
Log.e("client", "-- calling GoogleMaps.this.resizeMap");
GoogleMaps.this.resizeMap(args, callbackContext);
}
callbackContext.success();
Expand Down Expand Up @@ -784,33 +785,45 @@ private void resizeMap(JSONArray args, CallbackContext callbackContext) throws J
return;
}
mapDivLayoutJSON = args.getJSONObject(args.length() - 2);
JSONArray HTMLs = args.getJSONArray(args.length() - 1);
JSONObject elemInfo, elemSize;
String elemId;
float divW, divH, divLeft, divTop;
if (mPluginLayout == null) {
this.sendNoResult(callbackContext);
return;
}
final JSONArray HTMLs = args.getJSONArray(args.length() - 1);

this.mPluginLayout.clearHTMLElement();

for (int i = 0; i < HTMLs.length(); i++) {
elemInfo = HTMLs.getJSONObject(i);
try {
elemId = elemInfo.getString("id");
elemSize = elemInfo.getJSONObject("size");

divW = contentToView(elemSize.getLong("width"));
divH = contentToView(elemSize.getLong("height"));
divLeft = contentToView(elemSize.getLong("left"));
divTop = contentToView(elemSize.getLong("top"));
mPluginLayout.putHTMLElement(elemId, divLeft, divTop, divLeft + divW, divTop + divH);
} catch (Exception e){
e.printStackTrace();
cordova.getThreadPool().execute(new Runnable() {

@Override
public void run(){

try {
JSONObject elemInfo, elemSize;
String elemId;
float divW, divH, divLeft, divTop;
for (int i = 0; i < HTMLs.length(); i++) {
elemInfo = HTMLs.getJSONObject(i);
try {
elemId = elemInfo.getString("id");
elemSize = elemInfo.getJSONObject("size");

divW = contentToView(elemSize.getLong("width"));
divH = contentToView(elemSize.getLong("height"));
divLeft = contentToView(elemSize.getLong("left"));
divTop = contentToView(elemSize.getLong("top"));
mPluginLayout.putHTMLElement(elemId, divLeft, divTop, divLeft + divW, divTop + divH);
} catch (Exception e){
e.printStackTrace();
}
}
} catch (Exception e){}
//mPluginLayout.inValidate();
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateMapViewLayout();
}
});
}
}
//mPluginLayout.inValidate();
updateMapViewLayout();
});

this.sendNoResult(callbackContext);
}

Expand Down
93 changes: 47 additions & 46 deletions src/android/plugin/google/maps/MyPluginLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.util.Log;

@SuppressWarnings("deprecation")
public class MyPluginLayout extends FrameLayout {
Expand All @@ -44,19 +45,20 @@ public class MyPluginLayout extends FrameLayout {
private boolean isClickable = true;
private Map<String, RectF> HTMLNodes = new HashMap<String, RectF>();
private Activity mActivity = null;

@SuppressLint("NewApi")
public MyPluginLayout(View view, Activity activity) {
super(view.getContext());
mActivity = activity;
this.view = view;
this.root = (ViewGroup) view.getParent();
this.context = view.getContext();
if (VERSION.SDK_INT >= 21 || "org.xwalk.core.XWalkView".equals(view.getClass().getName())) {
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
Log.e("client", view.getClass().getName() );

view.setLayerType(View.LAYER_TYPE_HARDWARE, null);

frontLayer = new FrontLayerLayout(this.context);

scrollView = new ScrollView(this.context);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Expand All @@ -65,18 +67,18 @@ public MyPluginLayout(View view, Activity activity) {
backgroundView.setVerticalScrollBarEnabled(false);
backgroundView.setHorizontalScrollBarEnabled(false);
backgroundView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 9999));

scrollFrameLayout = new FrameLayout(this.context);
scrollFrameLayout.addView(backgroundView);
scrollFrameLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

scrollView.setHorizontalScrollBarEnabled(false);
scrollView.setVerticalScrollBarEnabled(false);

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

}

public void setDrawingRect(float left, float top, float right, float bottom) {
this.drawRect.left = left;
this.drawRect.top = top;
Expand All @@ -86,7 +88,7 @@ public void setDrawingRect(float left, float top, float right, float bottom) {
this.inValidate();
}
}

public void putHTMLElement(String domId, float left, float top, float right, float bottom) {
RectF rect = null;
if (this.HTMLNodes.containsKey(domId)) {
Expand Down Expand Up @@ -122,7 +124,7 @@ public void setClickable(boolean clickable) {
this.inValidate();
}
}

public void updateViewPosition() {
if (myView == null) {
return;
Expand Down Expand Up @@ -152,7 +154,7 @@ public void updateViewPosition() {
params.leftMargin = (int) this.drawRect.left;
params.gravity = Gravity.TOP;
myView.setLayoutParams(params);
}
}
if (android.os.Build.VERSION.SDK_INT < 11) {
// Force redraw
myView.requestLayout();
Expand All @@ -177,45 +179,44 @@ public void detachMyView() {
root.removeView(this);
this.removeView(frontLayer);
frontLayer.removeView(view);

scrollFrameLayout.removeView(myView);
myView.removeView(this.touchableWrapper);

this.removeView(this.scrollView);
this.scrollView.removeView(scrollFrameLayout);
if (orgLayoutParams != null) {
myView.setLayoutParams(orgLayoutParams);
}

root.addView(view);
myView = null;
mActivity.getWindow().getDecorView().requestFocus();
}

public void attachMyView(ViewGroup pluginView) {
view.setBackgroundColor(Color.TRANSPARENT);
if("org.xwalk.core.XWalkView".equals(view.getClass().getName())
|| "org.crosswalk.engine.XWalkCordovaView".equals(view.getClass().getName())) {
try {
/* view.setZOrderOnTop(true)
* Called just in time as with root.setBackground(...) the color
* come in front and take the whoel screen */
view.getClass().getMethod("setZOrderOnTop", boolean.class)
.invoke(view, true);
}
catch(Exception e) {}

try {
/* view.setZOrderOnTop(true)
* Called just in time as with root.setBackground(...) the color
* come in front and take the whoel screen */
view.getClass().getMethod("setZOrderOnTop", boolean.class)
.invoke(view, true);
}
catch(Exception e) {}

scrollView.setHorizontalScrollBarEnabled(false);
scrollView.setVerticalScrollBarEnabled(false);

scrollView.scrollTo(view.getScrollX(), view.getScrollY());
if (myView == pluginView) {
return;
} else {
this.detachMyView();
}
//backgroundView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (view.getContentHeight() * view.getScale() + view.getHeight())));

myView = pluginView;
ViewGroup.LayoutParams lParams = myView.getLayoutParams();
orgLayoutParams = null;
Expand All @@ -225,26 +226,26 @@ public void attachMyView(ViewGroup pluginView) {
root.removeView(view);
scrollView.addView(scrollFrameLayout);
this.addView(scrollView);

pluginView.addView(this.touchableWrapper);
scrollFrameLayout.addView(pluginView);

frontLayer.addView(view);
this.addView(frontLayer);
root.addView(this);
mActivity.getWindow().getDecorView().requestFocus();

scrollView.setHorizontalScrollBarEnabled(true);
scrollView.setVerticalScrollBarEnabled(true);
}

public void setPageSize(int width, int height) {
android.view.ViewGroup.LayoutParams lParams = backgroundView.getLayoutParams();
lParams.width = width;
lParams.height = height;
backgroundView.setLayoutParams(lParams);
}

public void scrollTo(int x, int y) {
this.scrollView.scrollTo(x, y);
}
Expand All @@ -253,19 +254,19 @@ public void scrollTo(int x, int y) {
public void setBackgroundColor(int color) {
this.backgroundView.setBackgroundColor(color);
}

public void inValidate() {
this.frontLayer.invalidate();
}


private class FrontLayerLayout extends FrameLayout {

public FrontLayerLayout(Context context) {
super(context);
this.setWillNotDraw(false);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (isClickable == false || myView == null || myView.getVisibility() != View.VISIBLE) {
Expand All @@ -280,7 +281,7 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
isScrolling = (contains == false && action == MotionEvent.ACTION_DOWN) ? true : isScrolling;
isScrolling = (action == MotionEvent.ACTION_UP) ? false : isScrolling;
contains = isScrolling == true ? false : contains;

if (contains) {
// Is the touch point on any HTML elements?
Set<Entry<String, RectF>> elements = MyPluginLayout.this.HTMLNodes.entrySet();
Expand Down Expand Up @@ -312,7 +313,7 @@ protected void onDraw(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
int scrollY = view.getScrollY();

Paint paint = new Paint();
paint.setColor(Color.argb(100, 0, 255, 0));
if (isClickable == false) {
Expand All @@ -323,10 +324,10 @@ protected void onDraw(Canvas canvas) {
canvas.drawRect(0, drawRect.top, drawRect.left, drawRect.bottom, paint);
canvas.drawRect(drawRect.right, drawRect.top, width, drawRect.bottom, paint);
canvas.drawRect(0, drawRect.bottom, width, height, paint);


paint.setColor(Color.argb(100, 255, 0, 0));

Set<Entry<String, RectF>> elements = MyPluginLayout.this.HTMLNodes.entrySet();
Iterator<Entry<String, RectF>> iterator = elements.iterator();
Entry <String, RectF> entry;
Expand All @@ -342,9 +343,9 @@ protected void onDraw(Canvas canvas) {
}
}
}

private class TouchableWrapper extends FrameLayout {

public TouchableWrapper(Context context) {
super(context);
}
Expand All @@ -357,5 +358,5 @@ public boolean dispatchTouchEvent(MotionEvent event) {
}
return super.dispatchTouchEvent(event);
}
}
}
}

0 comments on commit b160147

Please sign in to comment.