Browse Source

修改自动外勤和自动打卡

RaoMeng 9 years ago
parent
commit
8d1693e264

+ 133 - 138
WeiChat/src/main/java/com/xzjmyk/pm/activity/BdLocationHelper.java

@@ -1,74 +1,78 @@
 package com.xzjmyk.pm.activity;
 
-import android.content.Intent;
+import android.content.Context;
 import android.util.Log;
 
 import com.baidu.location.BDLocation;
 import com.baidu.location.BDLocationListener;
 import com.baidu.location.LocationClient;
 import com.baidu.location.LocationClientOption;
-import com.xzjmyk.pm.activity.bean.LocationEntity;
+import com.baidu.location.Poi;
+import com.xzjmyk.pm.activity.sp.LocationSp;
 import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
-import com.xzjmyk.pm.activity.ui.tool.ThreadUtil;
 
-import static com.xzjmyk.pm.activity.ui.erp.activity.oa.WorkLogsActivity.resultCode;
+import java.util.List;
 
 public class BdLocationHelper {
-    public static final String UPLOCATION_ACTION = "UPLOCATION_ACTION";//更新位置时候广播数据
-    private LocationEntity locationEntity;//位置对象
-    private final Intent broadcast = new Intent(UPLOCATION_ACTION);//更新后广播
-    private LocationClient mLocationClient = null;
-    private int mFaildCount = 0;//失败次数
-    private int scanSpan = 0;
-    private boolean runingLocation = false;
-
-    BdLocationHelper() {
-        initLocation();
-    }
 
-    private void initLocation() {
-        mLocationClient = new LocationClient(MyApplication.getInstance().getApplicationContext()); // 声明LocationClient类
-        mLocationClient.registerLocationListener(mLocationListener); // 注册监听函数
-        mLocationClient.setLocOption(getOptionByGPS());
-    }
-
-    /**
-     * 获取配置
-     *
-     * @return
-     */
-    private LocationClientOption getOptionByGPS() {
+    private Context mContext;
+    private double mLongitude;
+    private double mLatitude;
+    private String mAddress;
+    private String name;
+    private String mProvinceName;// 省份
+    private String mCityName;// 城市
+    private String mDistrictName;// 街道
+    private boolean isLocationUpdate;// 本次程序启动后,位置有没有成功更新一次
+    private LocationClient mLocationClient = null;
+    private int mFaildCount = 0;
+    private int resultCode;
+    private List<Poi> pois;
+    private int scanSpan = 20 * 1000;
+
+    BdLocationHelper(Context context) {
+        mContext = context;
+        initLocation(context);
+    }
+
+    private void initLocation(Context context) {
+
+        // 获取上一次的定位数据
+//        mLongitude = LocationSp.getInstance(context).getLongitude(0);
+//        mLatitude = LocationSp.getInstance(context).getLatitude(0);
+//        mAddress = LocationSp.getInstance(context).getAddress("");
+//        mProvinceName = LocationSp.getInstance(context).getProvinceName("");
+//        mCityName = LocationSp.getInstance(context).getCityName("");
+//        mDistrictName = LocationSp.getInstance(context).getDistrictName("");
+        //关闭缓存
+        mLongitude = 0;
+        mLatitude = 0;
+        mAddress = "";
+        mProvinceName = "";
+        mCityName = "";
+        mDistrictName = "";
+
+        mLocationClient = new LocationClient(context.getApplicationContext()); // 声明LocationClient类
+        mLocationClient.registerLocationListener(mMyLocationListener); // 注册监听函数
         LocationClientOption option = new LocationClientOption();
         option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
-        option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
-        option.setScanSpan(0);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
+        option.setCoorType("bd09ll");//可选,默认gcj02  bd09ll,设置返回的定位结果坐标系
+        option.setScanSpan(scanSpan);// 设置发起定位请求的间隔时间为10s;//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
         option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
         option.setOpenGps(true);//可选,默认false,设置是否使用gps
-        option.setLocationNotify(false);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
+        option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
         option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
-        option.setIsNeedLocationPoiList(false);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
+        option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
         option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
         option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
-        option.setEnableSimulateGps(true);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要
+        option.setEnableSimulateGps(true);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
         option.setNeedDeviceDirect(false);
-        return option;
+//        option.disableCache(true);//是否禁用缓存数据
+        mLocationClient.setLocOption(option);
+        //requestLocation();//重新定位
     }
 
-    /**
-     * 获取配置
-     *
-     * @return
-     */
-    private LocationClientOption getOptionNotGPS() {
-        LocationClientOption option = getOptionByGPS();
-        option.setOpenGps(false);
-        return option;
-    }
 
-
-    /**
-     * 关闭定位
-     **/
     public void release() {
         if (mLocationClient.isStarted()) {
             mLocationClient.stop();
@@ -77,142 +81,133 @@ public class BdLocationHelper {
 
     private static final String TAG = "BdLocationHelper";
 
-    /**
-     * 重新定位
-     **/
     public void requestLocation() {
-        if (mLocationClient == null) return;
-        if (!mLocationClient.isStarted()) {
+        if (mLocationClient != null && !mLocationClient.isStarted()) {
             mFaildCount = 0;
             mLocationClient.start();
+            Log.i(TAG, "requestLocation:start()");
         } else {
-            mLocationClient.requestLocation();
-        }
-    }
-
-    /**
-     * 重新定位,循环
-     **/
-    public void requestLocation(final long time) {
-        if (runingLocation || mLocationClient == null) {//如果已经启动了
-            return;
-        }
-        ThreadUtil.getInstance().addLoopTask(new Runnable() {
-            @Override
-            public void run() {
-                while (runingLocation) {
-                    mLocationClient.requestNotifyLocation();
-                    try {
-                        Thread.sleep(time);
-                    } catch (InterruptedException e) {
-                        e.printStackTrace();
-                    }
-                }
+            int scanSpan = mLocationClient.getLocOption().getScanSpan();
+            if (scanSpan < 1000) {
+                mLocationClient.getLocOption().setScanSpan(5000);
             }
-        });
+            Log.i(TAG, "requestLocation:request()");
+        }
     }
 
-    private BDLocationListener mLocationListener = new BDLocationListener() {
+    private BDLocationListener mMyLocationListener = new BDLocationListener() {
         @Override
         public void onReceiveLocation(BDLocation location) {
-            try {
-                if (location == null)
-                    locationEntity = new LocationEntity();
-                if (location.getLocType() == BDLocation.TypeGpsLocation// GPS定位结果
-                        || location.getLocType() == BDLocation.TypeNetWorkLocation//网络定位
-                        || location.getLocType() == BDLocation.TypeOffLineLocation//离线定位(未验证离线定位的有效性)
-                        ) {
-                    //定位成功
-                    log("定位成功");
-                    log("getLatitude()" + location.getLatitude());
-                    log("getLongitude" + location.getLongitude());
-                    log("getAddrStr" + location.getAddrStr());
-                    log("getLocationDescribe" + location.getLocationDescribe());
-                    log("getProvince" + location.getProvince());
-                    log("getCity" + location.getCity());
-                    log("getDistrict" + location.getDistrict());
-                    locationEntity.setLocationOk(true);
-                    locationEntity.setLatitude(location.getLatitude());
-                    locationEntity.setLongitude(location.getLongitude());
-                    locationEntity.setAddress(location.getAddrStr());
-                    locationEntity.setLocation(location.getLocationDescribe());
-                    locationEntity.setProvince(location.getProvince());
-                    locationEntity.setCityName(location.getCity());
-                    locationEntity.setDistrict(location.getDistrict());
-                } else {
-                    //统一未定位失败
-                    locationEntity.setLocationOk(false);
-                    log("定位失败");
-                    if (location.getLocType() == BDLocation.TypeServerError) {
-                        //服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因
-                        locationEntity.setErrorMessage("服务端网络定位失败");
-                        log("服务端网络定位失败");
-                    } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
-                        //网络不同导致定位失败,请检查网络是否通畅
-                        locationEntity.setErrorMessage("网络不同导致定位失败,请检查网络是否通畅");
-                        log("网络不同导致定位失败,请检查网络是否通畅");
-                    } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
-                        //无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机
-                        locationEntity.setErrorMessage("无法获取有效定位依据导致定位失败");
-                        log("无法获取有效定位依据导致定位失败");
-                    } else {
-                        locationEntity.setErrorMessage("未知错误");
-                        log("未知错误");
-                    }
+            int resultCode = 0;
+            if (location != null) {
+                resultCode = location.getLocType();
+            }
+            Log.i("gongpengming", "resultCode=" + resultCode);
+            BdLocationHelper.this.resultCode = resultCode;
+            // 百度定位失败,不符合下列定位返回类型  TODO 应该把缓存定位去除
+            if (resultCode != BDLocation.TypeGpsLocation //GPS定位成功
+//                    && resultCode != BDLocation.TypeCacheLocation//定位缓存结果
+//                    && resultCode != BDLocation.TypeOffLineLocation//离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果。
+                    && resultCode != BDLocation.TypeNetWorkLocation// 网络定位结果,网络定位成功。
+                    ) {
+                if (AppConfig.DEBUG) {
+                    Log.d(AppConfig.TAG, "百度定位失败");
+                }
+                mFaildCount++;
+                if (mFaildCount > 3) {// 停止定位
+                    mLocationClient.stop();
+                }
+                if (listener != null) {
+                    listener.result(false);
                 }
-                //TODO 发送广播
-                MyApplication.getInstance().sendBroadcast(broadcast);
-            } catch (Exception e) {
+                return;
+            }
+            // 百度定位成功
+            mLongitude = location.getLongitude();
+            mLatitude = location.getLatitude();
+            name = location.getLocationDescribe();
+            pois = location.getPoiList();
+            if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
+                mAddress = location.getAddrStr();
+                mProvinceName = location.getProvince();
+                mCityName = location.getCity();
+                mDistrictName = location.getDistrict();
+                if (AppConfig.DEBUG) {
+                    Log.d(AppConfig.TAG,
+                            "百度定位信息  City:" + location.getCity() + "  CityCode:" + location.getCityCode() + "  区:" + location.getDistrict());
+                }
+            }
+            if (!isLocationUpdate) {
+                LocationSp.getInstance(mContext).setLongitude((float) mLongitude);
+                LocationSp.getInstance(mContext).setLatitude((float) mLatitude);
+                LocationSp.getInstance(mContext).setAddress(mAddress);
+                LocationSp.getInstance(mContext).setProvinceName(mProvinceName);
+                LocationSp.getInstance(mContext).setCityName(mCityName);
+                LocationSp.getInstance(mContext).setDistrictName(mDistrictName);
+                isLocationUpdate = true;
+            }
+            if (AppConfig.DEBUG) {
+                Log.d(AppConfig.TAG, "百度定位信息  mLongitude:" + mLongitude + "  mLatitude:" + mLatitude + "  mAddressDetail:" + mAddress);
+                Log.d(AppConfig.TAG, "resultCode:" + resultCode);
+            }
+//            release();
+            // if (isTimingScan()) {// 停止定时定位
+            // mLocationClient.getLocOption().setScanSpan(100);
+            // }
+            if (listener != null) {
+                listener.result(true);
             }
         }
     };
 
-    private void log(String message) {
-        try {
-            if (!AppConfig.DEBUG && StringUtils.isEmpty(message)) return;
-            Log.i("gongpengming", message);
-        } catch (Exception e) {
 
-        }
+    //是否定位成功
+    public boolean locationOk() {
+        return resultCode == BDLocation.TypeGpsLocation || resultCode == BDLocation.TypeNetWorkLocation;
     }
 
-
     public String getName() {
-        return locationEntity == null ? "" : StringUtils.isEmpty(locationEntity.getLocation()) ? "" : locationEntity.getLocation();
+        return pois == null ? (StringUtils.isEmail(name) ? "当前位置" : name) : (pois.get(0) == null ? (name == null ? "当前位置" : name) : pois.get(0).getName());
     }
 
+    public void setName(String name) {
+        this.name = name;
+    }
 
     // 获取经纬度
     public double getLongitude() {
-        return locationEntity == null ? 0 : locationEntity.getLongitude();
+        return mLongitude;
     }
 
     // 获取经纬度
     public double getLatitude() {
-        return locationEntity == null ? 0 : locationEntity.getLatitude();
+        return mLatitude;
     }
 
     // 获取地址详情
     public String getAddress() {
-        return locationEntity == null ? "" : locationEntity.getAddress();
+        return mAddress;
     }
 
     public String getProvinceName() {
-        return locationEntity == null ? "" : locationEntity.getProvince();
+        return mProvinceName;
     }
 
     public String getCityName() {
-        return locationEntity == null ? "" : locationEntity.getCityName();
+        return mCityName;
     }
 
     public String getDistrictName() {
-        return locationEntity == null ? "" : locationEntity.getDistrict();
+        return mDistrictName;
     }
 
     public boolean isLocationUpdate() {
-        return true;
+        return isLocationUpdate;
     }
 
+    public boolean hasData() {
+        return mLatitude != 0 && mLongitude != 0;
+    }
 
     private OnBaiduResultListener listener = null;
 

+ 1 - 2
WeiChat/src/main/java/com/xzjmyk/pm/activity/MyApplication.java

@@ -197,10 +197,9 @@ public class MyApplication extends Application {
     }
 
     public BdLocationHelper getBdLocationHelper() {
-
         {//TODO 原定位
             if (mBdLocationHelper == null) {
-                mBdLocationHelper = new BdLocationHelper();
+                mBdLocationHelper = new BdLocationHelper(this);
             }
             mBdLocationHelper.requestLocation();
             return mBdLocationHelper;

+ 1 - 0
WeiChat/src/main/java/com/xzjmyk/pm/activity/ui/erp/activity/oa/MissionActivity.java

@@ -117,6 +117,7 @@ public class MissionActivity extends BaseActivity implements View.OnClickListene
 
 
     private void endActivity() {
+        unregisterReceiver(receiver);
         sendBroadcast(new Intent(AlarmService.UPDATA_MISSION_PLAN));
         boolean isAuto = PreferenceUtils.getBoolean(AppConfig.AUTO_MISSION, false);
         if (isAuto) {

+ 6 - 5
WeiChat/src/main/java/com/xzjmyk/pm/activity/ui/erp/activity/oa/OAActivity.java

@@ -379,11 +379,12 @@ public class OAActivity extends BaseActivity implements View.OnClickListener, Wo
                     intent.putExtra("data", object.toString());
                     startActivityForResult(intent, isMe ? 0x20 : 0x21);
                 } else if (adapter.isOutplan(object)) {
-                    intent = new Intent(activity, VisitReportAddActivity.class);
-                    intent.putExtra("type", 3);
-                    intent.putExtra("isMe", isMe);//可以提交拜访报告
-                    intent.putExtra("object", object.toString());
-                    startActivityForResult(intent, isMe ? 0x20 : 0x21);
+                    //TODO 发布版本关闭
+//                    intent = new Intent(activity, VisitReportAddActivity.class);
+//                    intent.putExtra("type", 3);
+//                    intent.putExtra("isMe", isMe);//可以提交拜访报告
+//                    intent.putExtra("object", object.toString());
+//                    startActivityForResult(intent, isMe ? 0x20 : 0x21);
                 }
             }
         });

+ 13 - 1
WeiChat/src/main/java/com/xzjmyk/pm/activity/ui/erp/activity/oa/TestActivity.java

@@ -14,8 +14,10 @@ import com.lidroid.xutils.view.annotation.ViewInject;
 import com.xzjmyk.pm.activity.AppConfig;
 import com.xzjmyk.pm.activity.MyApplication;
 import com.xzjmyk.pm.activity.R;
+import com.xzjmyk.pm.activity.db.dao.MissionDao;
 import com.xzjmyk.pm.activity.ui.base.BaseActivity;
 import com.xzjmyk.pm.activity.ui.erp.entity.ErrorEntity;
+import com.xzjmyk.pm.activity.ui.erp.model.MissionModel;
 import com.xzjmyk.pm.activity.ui.erp.util.ListUtils;
 import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
 
@@ -49,7 +51,17 @@ public class TestActivity extends BaseActivity implements View.OnClickListener {
     public void onClick(View view) {
         switch (view.getId()) {
             case R.id.error_list:
-                MyApplication.getInstance().getHelper().requestLocation();
+                List<MissionModel > models= MissionDao.getInstance().queryByEnCode();
+                if(ListUtils.isEmpty(models)){
+                    Log.i("gongpengming", "空的");
+                }else{
+                    for (MissionModel e:models){
+                        Log.i("gongpengming", "getCompanyName="+e.getCompanyName());
+                        Log.i("gongpengming", "getRemark="+e.getRemark());
+                        Log.i("gongpengming", "getVisitTime="+e.getVisitTime());
+                    }
+                }
+
                 break;
             case R.id.test_btn:
                 MyApplication.getInstance().getHelper().requestLocation(5000);

+ 2 - 5
WeiChat/src/main/java/com/xzjmyk/pm/activity/ui/erp/adapter/MissionAdapter.java

@@ -79,11 +79,8 @@ public class MissionAdapter extends RecyclerView.Adapter<MissionAdapter.BaseView
         holder.item_time_tv.setText(getStringNotNull(entity.getRecorddate()));
         holder.item_location_tv.setText(getStringNotNull(entity.getLocation()));
         holder.item_remark_tv.setText(getStringNotNull(entity.getRemark()));
-        if (entity.getStatus() == 1) {
-            
-        } else {
-
-        }
+        holder.item_remark_tv.setFocusable(entity.getStatus() != 1);
+        holder.item_remark_tv.setClickable(entity.getStatus() != 1);
     }
 
     private void initEvent(final BaseViewHolder holder, final int position) {

+ 6 - 2
WeiChat/src/main/java/com/xzjmyk/pm/activity/ui/erp/presenter/MissionPresenter.java

@@ -205,7 +205,10 @@ public class MissionPresenter implements IMissionPresenter, HttpHandler.OnResult
         } else if (e.getLatLng() == null) {
             return false;
         } else if (StringUtils.isEmpty(e.getVisitTime())) {
-            if (iMission != null) iMission.showToast("您的 目的地" + i + "预计时间未填", R.color.load_warning);
+            if (iMission != null) iMission.showToast("您的目的地" + i + "预计时间未填", R.color.load_warning);
+            return false;
+        } else if (StringUtils.isEmpty(e.getRemark())) {
+            if (iMission != null) iMission.showToast("您的目的地" + i + "出访目的未填", R.color.load_warning);
             return false;
         }
         return true;
@@ -361,10 +364,11 @@ public class MissionPresenter implements IMissionPresenter, HttpHandler.OnResult
                 }
                 entity.setStatus(1);
                 //TODO 添加时间和位置数据
-                entity.setRemark(object.getString("mpd_remark"));
+                entity.setRemark(object.getString("MPD_REMARK"));
                 entity.setDistance(object.getDoubleValue("MPD_DISTANCE"));
                 entity.setLocation(object.getString("MPD_LOCATION"));
                 entity.setRecorddate(object.getString("MPD_RECORDDATE"));
+                Log.i("gongpengming", "entity=" + entity.getRemark());
                 entities.add(entity);
             }
             if (!ListUtils.isEmpty(entities))

+ 1 - 5
WeiChat/src/main/java/com/xzjmyk/pm/activity/ui/erp/util/attendance/AutoErpService.java

@@ -53,7 +53,7 @@ public class AutoErpService extends Service {
 
     private final long MINUTE = 60 * 1000;//一分钟的时间戳
     private long workInterval = MINUTE;//计算循环的时间
-    private long missionInterval = 3 * MINUTE;//计算循环的时间
+    private long missionInterval = 20 * MINUTE;//计算循环的时间
     private AutoErpSigninUitl signinUitl;
 
     private final String[] flags = {
@@ -177,10 +177,6 @@ public class AutoErpService extends Service {
     }
 
 
-
-
-
-
     /**
      * 开启自动打卡和提醒任务
      * 在获取打卡列表之后,

+ 1 - 0
WeiChat/src/main/java/com/xzjmyk/pm/activity/ui/erp/util/attendance/AutoErpSigninUitl.java

@@ -702,6 +702,7 @@ public class AutoErpSigninUitl implements HttpHandler.OnResultListener {
                 entity.setStatus(1);
                 entity.setDistance(object.getDoubleValue("MPD_DISTANCE"));
                 entity.setLocation(object.getString("MPD_LOCATION"));
+                entity.setRemark(object.getString("MPD_REMARK"));
                 entity.setRecorddate(object.getString("MPD_RECORDDATE"));
                 entities.add(entity);
             }

+ 1 - 0
WeiChat/src/main/res/layout/fragment_me.xml

@@ -493,6 +493,7 @@
             android:id="@+id/test_rl"
             style="@style/IMTbleLine_UP_Me"
             android:layout_marginTop="7dp"
+            android:visibility="gone"
             android:background="@drawable/selector_me_menu_item_bg"
             >
             <TextView

+ 2 - 4
WeiChat/src/main/res/layout/item_mission.xml

@@ -178,15 +178,13 @@
     <View
         android:layout_width="match_parent"
         android:layout_height="2px"
-        android:background="@color/item_line"
-        android:visibility="gone" />
+        android:background="@color/item_line" />
 
     <RelativeLayout
         android:layout_width="wrap_content"
         android:layout_height="@dimen/misson_item_height"
         android:paddingLeft="@dimen/misson_item_pl"
-        android:paddingRight="@dimen/misson_item_pr"
-        android:visibility="gone">
+        android:paddingRight="@dimen/misson_item_pr">
 
         <TextView
             android:id="@+id/remark_tag"