From 74f31a67de3880947d222fa1ab7923712a30bdb8 Mon Sep 17 00:00:00 2001 From: xkli <569664668@qq.com> Date: Mon, 13 Mar 2017 17:35:56 +0800 Subject: [PATCH] * [android] update modify nullpoint --- .../java/com/alibaba/weex/WXPageActivity.java | 2 +- .../module/location/DefaultLocation.java | 110 ++++++++++-------- 2 files changed, 61 insertions(+), 51 deletions(-) diff --git a/android/playground/app/src/main/java/com/alibaba/weex/WXPageActivity.java b/android/playground/app/src/main/java/com/alibaba/weex/WXPageActivity.java index af74bd79f9..f8c9b9635c 100755 --- a/android/playground/app/src/main/java/com/alibaba/weex/WXPageActivity.java +++ b/android/playground/app/src/main/java/com/alibaba/weex/WXPageActivity.java @@ -358,7 +358,7 @@ public void run() { Rect outRect = new Rect(); ctx.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect); mConfigMap.put("bundleUrl", mUri.toString()); - String path = mUri.getScheme().equals("file") ? assembleFilePath(mUri) : mUri.toString(); + String path = "file".equals(mUri.getScheme()) ? assembleFilePath(mUri) : mUri.toString(); mInstance.render(TAG, WXFileUtils.loadAsset(path, WXPageActivity.this), mConfigMap, null, WXRenderStrategy.APPEND_ASYNC); diff --git a/android/playground/app/src/main/java/com/alibaba/weex/extend/module/location/DefaultLocation.java b/android/playground/app/src/main/java/com/alibaba/weex/extend/module/location/DefaultLocation.java index 807d0fcfbd..d1ad55bf8a 100644 --- a/android/playground/app/src/main/java/com/alibaba/weex/extend/module/location/DefaultLocation.java +++ b/android/playground/app/src/main/java/com/alibaba/weex/extend/module/location/DefaultLocation.java @@ -219,12 +219,12 @@ import android.support.v4.app.ActivityCompat; import android.text.TextUtils; -import com.taobao.weex.WXEnvironment; import com.taobao.weex.WXSDKInstance; -import com.taobao.weex.WXSDKManager; +import com.taobao.weex.bridge.SimpleJSCallback; import com.taobao.weex.utils.WXLogUtils; import org.json.JSONException; +import org.json.JSONObject; import java.util.ArrayList; import java.util.Collection; @@ -233,9 +233,6 @@ import java.util.Map; import java.util.UUID; -/** - * Created by lixinke on 16/9/10. - */ public class DefaultLocation implements ILocatable { private static final String TAG = "DefaultLocation"; @@ -261,12 +258,10 @@ public void setWXSDKInstance(WXSDKInstance WXSDKInstance) { @Override public void getCurrentPosition(final String successCallback, final String errorCallback, final String params) { - if(WXEnvironment.isApkDebugable()) { - WXLogUtils.d(TAG, "into--[getCurrentPosition] successCallback:" + successCallback + " \nerrorCallback:" + errorCallback + " \nparams:" + params); - } + WXLogUtils.d(TAG, "into--[getCurrentPosition] successCallback:" + successCallback + " \nerrorCallback:" + errorCallback + " \nparams:" + params); if (!TextUtils.isEmpty(params)) { try { - org.json.JSONObject jsObj = new org.json.JSONObject(params); + JSONObject jsObj = new JSONObject(params); boolean enableHighAccuracy = jsObj.optBoolean("enableHighAccuracy"); boolean enableAddress = jsObj.optBoolean("address"); WXLocationListener listener = findLocation(null, successCallback, errorCallback, enableHighAccuracy, enableAddress); @@ -281,11 +276,13 @@ public void getCurrentPosition(final String successCallback, final String errorC Map options = new HashMap<>(); options.put(ERROR_CODE, ErrorCode.PARAMS_ERROR); options.put(ERROR_MSG, ErrorMsg.PARAMS_ERROR); - WXSDKManager.getInstance().callback(mWXSDKInstance.getInstanceId(), errorCallback, options); + if (mWXSDKInstance != null) { + new SimpleJSCallback(mWXSDKInstance.getInstanceId(), errorCallback).invoke(options); + } } private WXLocationListener findLocation(String watchId, String sucCallback, String errorCallback, boolean enableHighAccuracy, boolean enableAddress) { -// WXLogUtils.d(TAG, "into--[findLocation] mWatchId:" + watchId + "\nsuccessCallback:" + sucCallback + "\nerrorCallback:" + errorCallback + "\nenableHighAccuracy:" + enableHighAccuracy + "\nmEnableAddress:" + enableAddress); + WXLogUtils.d(TAG, "into--[findLocation] mWatchId:" + watchId + "\nsuccessCallback:" + sucCallback + "\nerrorCallback:" + errorCallback + "\nenableHighAccuracy:" + enableHighAccuracy + "\nmEnableAddress:" + enableAddress); if (mLocationManager == null) { mLocationManager = (LocationManager) mWXSDKInstance.getContext().getSystemService(Context.LOCATION_SERVICE); @@ -297,14 +294,20 @@ private WXLocationListener findLocation(String watchId, String sucCallback, Stri //String provider = locationManager.getBestProvider(criteria, false); if (ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { WXLocationListener WXLocationListener = new WXLocationListener(mLocationManager, mWXSDKInstance, watchId, sucCallback, errorCallback, enableAddress); - mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, WXLocationListener); - mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, WXLocationListener); + if (mLocationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)) { + mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, WXLocationListener); + } + if (mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) { + mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, WXLocationListener); + } return WXLocationListener; } else { Map options = new HashMap<>(); options.put(ERROR_CODE, ErrorCode.NO_PERMISSION_ERROR); options.put(ERROR_MSG, ErrorMsg.NO_PERMISSION_ERROR); - WXSDKManager.getInstance().callback(mWXSDKInstance.getInstanceId(), errorCallback, options); + if (mWXSDKInstance != null) { + new SimpleJSCallback(mWXSDKInstance.getInstanceId(), errorCallback).invoke(options); + } } return null; } @@ -314,7 +317,7 @@ public void watchPosition(final String successCallback, final String errorCallba WXLogUtils.d("into--[watchPosition] successCallback:" + successCallback + " errorCallback:" + errorCallback + "\nparams:" + params); if (!TextUtils.isEmpty(params)) { try { - org.json.JSONObject jsObj = new org.json.JSONObject(params); + JSONObject jsObj = new JSONObject(params); boolean enableHighAccuracy = jsObj.optBoolean("enableHighAccuracy"); boolean enableAddress = jsObj.optBoolean("address"); @@ -331,7 +334,9 @@ public void watchPosition(final String successCallback, final String errorCallba Map options = new HashMap<>(); options.put(ERROR_CODE, ErrorCode.PARAMS_ERROR); options.put(ERROR_MSG, ErrorMsg.PARAMS_ERROR); - WXSDKManager.getInstance().callback(mWXSDKInstance.getInstanceId(), errorCallback, options); + if (mWXSDKInstance != null) { + new SimpleJSCallback(mWXSDKInstance.getInstanceId(), errorCallback).invoke(options); + } } @Override @@ -383,39 +388,31 @@ public void destroy() { static class WXLocationListener implements LocationListener, Handler.Callback { - private WXSDKInstance mWXSDKInstance; + private Context mContext = null; private String mWatchId; - private String mSucCallback; - private String mErrorCallback; + private SimpleJSCallback mSucCallback = null; + private SimpleJSCallback mErrorCallback = null; private boolean mEnableAddress; private Handler mHandler; private LocationManager mLocationManager; private WXLocationListener(LocationManager locationManager, WXSDKInstance instance, String watchId, String sucCallback, String errorCallback, boolean enableAddress) { - mWXSDKInstance = instance; this.mWatchId = watchId; - this.mSucCallback = sucCallback; - this.mErrorCallback = errorCallback; + if (instance != null) { + this.mSucCallback = new SimpleJSCallback(instance.getInstanceId(), sucCallback); + this.mErrorCallback = new SimpleJSCallback(instance.getInstanceId(), errorCallback); + mContext = instance.getContext(); + } this.mEnableAddress = enableAddress; mHandler = new Handler(this); mLocationManager = locationManager; -// WVThreadPool.getInstance().execute(new Runnable() { -// public void run() { - mHandler.sendEmptyMessageDelayed(TIME_OUT_WHAT, GPS_TIMEOUT); -// } -// }); + mHandler.sendEmptyMessageDelayed(TIME_OUT_WHAT, GPS_TIMEOUT); } @Override public void onLocationChanged(Location location) { mHandler.removeMessages(TIME_OUT_WHAT); - if(WXEnvironment.isApkDebugable()) { - WXLogUtils.d(TAG, "into--[onLocationChanged] location:" + location == null ? "Location is NULL!" : location.toString()); - } - - if (mWXSDKInstance == null || mWXSDKInstance.isDestroy()) { - return; - } + WXLogUtils.d(TAG, "into--[onLocationChanged] location:" + location == null ? "Location is NULL!" : location.toString()); if (location != null) { Map result = new HashMap<>(); @@ -457,7 +454,15 @@ public void onLocationChanged(Location location) { if (!TextUtils.isEmpty(mWatchId)) { result.put(WATCH_ID, mWatchId); } - WXSDKManager.getInstance().callback(mWXSDKInstance.getInstanceId(), mSucCallback, result, TextUtils.isEmpty(mWatchId) ? false : true); + + if (mSucCallback != null) { + boolean isKeepAlive = TextUtils.isEmpty(mWatchId) ? false : true; + if (isKeepAlive) { + mSucCallback.invokeAndKeepAlive(result); + } else { + mSucCallback.invoke(result); + } + } } else { Map options = new HashMap<>(); options.put(ERROR_CODE, ErrorCode.LOCATION_ERROR); @@ -465,10 +470,17 @@ public void onLocationChanged(Location location) { if (!TextUtils.isEmpty(mWatchId)) { options.put(WATCH_ID, mWatchId); } - WXSDKManager.getInstance().callback(mWXSDKInstance.getInstanceId(), mErrorCallback, options, TextUtils.isEmpty(mWatchId) ? false : true); + if (mErrorCallback != null) { + boolean isKeepAlive = TextUtils.isEmpty(mWatchId) ? false : true; + if (isKeepAlive) { + mErrorCallback.invokeAndKeepAlive(options); + } else { + mErrorCallback.invoke(options); + } + } } - if (TextUtils.isEmpty(mWatchId)) { - if (mLocationManager != null && ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + if (TextUtils.isEmpty(mWatchId) && mContext != null) { + if (mLocationManager != null && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { destroy(); mLocationManager.removeUpdates(this); } @@ -493,13 +505,11 @@ public void onProviderDisabled(String provider) { @Override public boolean handleMessage(Message msg) { if (msg.what == TIME_OUT_WHAT) { - if(WXEnvironment.isApkDebugable()) { - WXLogUtils.d(TAG, "into--[handleMessage] Location Time Out!"); - } - if (mWXSDKInstance == null || mWXSDKInstance.isDestroy() || mLocationManager == null) { + WXLogUtils.d(TAG, "into--[handleMessage] Location Time Out!"); + if (mContext == null || mLocationManager == null) { return false; } - if (ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mLocationManager.removeUpdates(this); } Map options = new HashMap<>(); @@ -508,7 +518,9 @@ public boolean handleMessage(Message msg) { if (!TextUtils.isEmpty(mWatchId)) { options.put(WATCH_ID, mWatchId); } - WXSDKManager.getInstance().callback(mWXSDKInstance.getInstanceId(), mErrorCallback, options); + if (mErrorCallback != null) { + mErrorCallback.invoke(options); + } return true; } return false; @@ -518,14 +530,12 @@ public boolean handleMessage(Message msg) { * get address info */ private Address getAddress(double latitude, double longitude) { - if(WXEnvironment.isApkDebugable()) { - WXLogUtils.d(TAG, "into--[getAddress] latitude:" + latitude + " longitude:" + longitude); - } + WXLogUtils.d(TAG, "into--[getAddress] latitude:" + latitude + " longitude:" + longitude); try { - if (mWXSDKInstance == null || mWXSDKInstance.isDestroy()) { + if (mContext == null) { return null; } - Geocoder gc = new Geocoder(mWXSDKInstance.getContext()); + Geocoder gc = new Geocoder(mContext); List
list = gc.getFromLocation(latitude, longitude, 1); if (list != null && list.size() > 0) { return list.get(0); @@ -539,7 +549,7 @@ private Address getAddress(double latitude, double longitude) { public void destroy() { if (mHandler != null) { mHandler.removeMessages(TIME_OUT_WHAT); - mWXSDKInstance=null; + mContext = null; } } }