package com.edufound.reader.util; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.text.TextUtils; import android.util.DisplayMetrics; import android.view.View; import com.edufound.reader.activity.LoginAlertActivity; import com.edufound.reader.activity.LoginQRAlertActivity; import com.edufound.reader.activity.PayActivity; import com.edufound.reader.activity.TmailPayActivity; import com.edufound.reader.activity.WebActivity; import com.edufound.reader.apiserver.ActivityEventServerImpl; import com.edufound.reader.apiserver.OrderApiServerImpl; import com.edufound.reader.bean.ChannelCodeEnum; import com.edufound.reader.bean.EventListBean; import com.edufound.reader.bean.HttpResultBean; import com.edufound.reader.botsdk.BotRegisterListener; import com.edufound.reader.interfaces.EfunboxInterfaces; import com.edufound.reader.miutil.MiSoundSDKUtil; import com.edufound.reader.popwindow.PopWindowUtil; import com.edufound.reader.presenter.PopWindowPresneter; import com.edufound.reader.receiver.ShowPageLoadingReceiver; import com.edufound.reader.tmailsdk.TMailSDKUtil; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.okhttplib.HttpInfo; import com.okhttplib.callback.Callback; import com.orhanobut.logger.Logger; import java.io.IOException; import java.text.DecimalFormat; import java.util.HashMap; import java.util.List; import java.util.Map; public class EfunboxUtil { /** * 得到宽高缩放比 */ public static void initDesignSize(Context context) { final DisplayMetrics appDisplayMetrics = context.getResources().getDisplayMetrics(); int designWidth = 1280; int designHeight = 720; float ds = 1.0f * designWidth / designHeight; float ts = 1.0f * appDisplayMetrics.widthPixels / appDisplayMetrics.heightPixels; if (ds >= ts) { int targetWidth = appDisplayMetrics.widthPixels; float designScale = 1.0f * targetWidth / designWidth; setDisplay(context, designScale); } else { int targetHeight = appDisplayMetrics.heightPixels; float designScale = 1.0f * targetHeight / designHeight; setDisplay(context, designScale); } } /** * 设置dpi缩放比 */ public static void setDisplay(Context context, float designScale) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); displayMetrics.density = designScale; displayMetrics.scaledDensity = displayMetrics.density; displayMetrics.xdpi = displayMetrics.density * 160; displayMetrics.ydpi = displayMetrics.xdpi; displayMetrics.densityDpi = Float.valueOf(displayMetrics.xdpi).intValue(); } /** * 判断字符串是否符合手机号码格式 * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188 * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186 * 电信号段: 133,149,153,170,173,177,180,181,189 * * @param mobileNums * @return 待检测的字符串 */ public static boolean isMobileNO(String mobileNums) { String telRegex = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(147,145))\\d{8}$"; if (TextUtils.isEmpty(mobileNums)) { return false; } else { return mobileNums.matches(telRegex); } } /** * 根据中文年纪选择换成后台枚举 */ public static String getEnumByCNGrade(String cnGrade) { // PRESCHOOL("学前"), // PRIMARY_FIRST_GRADE("小学一年级"), // PRIMARY_SECOND_GRADE("小学二年级"), // PRIMARY_THREE_GRADE("小学三年级"), // PRIMARY_SENIOR_GRADE("小学四年级"); switch (cnGrade) { case "一年级": return "PRIMARY_FIRST_GRADE"; case "二年级": return "PRIMARY_SECOND_GRADE"; case "三年级": return "PRIMARY_THREE_GRADE"; case "四年级": return "PRIMARY_SENIOR_GRADE"; case "学前": return "PRESCHOOL"; } return "undefinde"; } public static String getCNGradeByEnum(String gradeEnum) { switch (gradeEnum) { case "PRIMARY_FIRST_GRADE": return "一年级"; case "PRIMARY_SECOND_GRADE": return "二年级"; case "PRIMARY_THREE_GRADE": return "三年级"; case "PRIMARY_SENIOR_GRADE": return "四年级"; case "PRESCHOOL": return "学前"; } return "undefinde"; } public static int getIndexGradeByEnum(String gradeEnum) { switch (gradeEnum) { case "PRIMARY_FIRST_GRADE": return 0; case "PRIMARY_SECOND_GRADE": return 1; case "PRIMARY_THREE_GRADE": return 2; case "PRIMARY_SENIOR_GRADE": return 3; case "PRESCHOOL": return 4; } return -1; } public static boolean checkLogin(Context context, View parent) { if (!Consts.getUID().equals(Consts.getDefaultUid())) { //已登录,无论登不登录都调用登录... return true; } userLogin(context, parent); return false; } public static boolean checkIsVip() { if (Consts.getUmengChannel().equals(ChannelCodeEnum.BAIDU.getChannelCode())) { if (Consts.getmConstsBaiDuUserBean() == null || Consts.getmConstsBaiDuUserBean().getUserInfo() == null) { return false; } return (Consts.getmConstsBaiDuUserBean().getUserInfo().getIsVip() == 1); } else { //除了百度都走这里 return Consts.isIsYFVip(); } } public static void userLogin(Context context, View parent) { if (Consts.getUmengChannel().equals(ChannelCodeEnum.BAIDU.getChannelCode())) { //百度渠道调用百度的授权 BotRegisterListener.getBaiDuVipInfo(); } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.TMAIL.getChannelCode())) { TMailSDKUtil.TMailLogin(context); } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.MISOUND.getChannelCode())) { PopWindowUtil.miSoundLoginWindow(context, parent, MiSoundSDKUtil.MI_SOUND_QRCODE_TYPE, miSoundLoginCallBack); } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.JINGDONGFANG.getChannelCode()) || Consts.getUmengChannel().equals(ChannelCodeEnum.BUBUGAO.getChannelCode()) || Consts.getUmengChannel().equals(ChannelCodeEnum.YIFANG.getChannelCode()) || Consts.getUmengChannel().equals(ChannelCodeEnum.LENOVO_PAD.getChannelCode())) { //京东方、、步步高、义方、联想pad Intent intent = new Intent(context, LoginQRAlertActivity.class); context.startActivity(intent); } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.HUAWEI_LIANYUN.getChannelCode())) { } } static PopWindowPresneter.MISoundLoginCallBack miSoundLoginCallBack; public static void setMISoundLoginCallBack(PopWindowPresneter.MISoundLoginCallBack callBack) { miSoundLoginCallBack = callBack; } public static void clearMISoundLoginCallBack() { miSoundLoginCallBack = null; } public static void userPay(Activity activity) { if (Consts.getUmengChannel().equals(ChannelCodeEnum.BAIDU.getChannelCode())) { //百度渠道调用百度的支付 BotRegisterListener.payBaiDuVip(); } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.YIFANG.getChannelCode()) || Consts.getUmengChannel().equals(ChannelCodeEnum.JINGDONGFANG.getChannelCode()) || Consts.getUmengChannel().equals(ChannelCodeEnum.LENOVO_PAD.getChannelCode()) || Consts.getUmengChannel().equals(ChannelCodeEnum.BUBUGAO.getChannelCode())) { //义方调用普通版支付,联想也用 Intent topay = new Intent(activity, PayActivity.class); activity.startActivity(topay); } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.TMAIL.getChannelCode())) { //去天猫支付界面 Intent topay = new Intent(activity, TmailPayActivity.class); activity.startActivity(topay); } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.MISOUND.getChannelCode())) { Intent topay = new Intent(activity, PayActivity.class); activity.startActivity(topay); } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.HUAWEI_LIANYUN.getChannelCode())) { } } /** * 判断是否是vip */ public static void getYFVip(Context context, EfunboxInterfaces.getYFVipInterfaces callback) { if (!Consts.getUmengChannel().equals(ChannelCodeEnum.BAIDU.getChannelCode())) { //不是百度渠道,检查是否是义方VIP if (Consts.getmConstsUserBean() != null) { OrderApiServerImpl api = new OrderApiServerImpl(); api.getAuth(null, new Callback() { @Override public void onSuccess(HttpInfo info) throws IOException { HttpResultBean bean = info.getRetDetail(new TypeToken>() { }.getType()); if (!TextUtils.isEmpty(bean.getData())) { Consts.setIsYFVip(true); if (callback != null) { callback.success(); } } else { Consts.setIsYFVip(false); if (callback != null) { callback.error(); } } } @Override public void onFailure(HttpInfo info) throws IOException { // Toast.makeText(mView.getActivity(),"") Consts.setIsYFVip(false); if (callback != null) { callback.error(); } } }); } } else { if (callback != null) { callback.error(); } } } public static String checkNum10000(String num) { if (TextUtils.isEmpty(num)) { return "0"; } if (Float.valueOf(num) > 10000f) { float a = Float.parseFloat(num) / 10000; DecimalFormat decimalFormat = new DecimalFormat("#.#"); return String.valueOf(decimalFormat.format(a)) + "万"; } return num; } /** * 手机号用****号隐藏中间数字 * * @param phone * @return */ public static String setPhoneMiddle(String phone) { String phone_s = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); return phone_s; } /** * 邮箱用****号隐藏前面的字母 * * @return */ public static String setEmailMiddle(String email) { String emails = email.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4"); return emails; } /** * 检查是否有活动 */ public static void checkEvents(Context context) { //检查有没有活动 ActivityEventServerImpl mEventApi = new ActivityEventServerImpl(); mEventApi.getEventListByType(new Callback() { @Override public void onSuccess(HttpInfo info) throws IOException { try { HttpResultBean> activityBean = info.getRetDetail(new TypeToken>>() { }.getType()); if (Boolean.valueOf(activityBean.getSuccess())) { //接口返回成功 if (activityBean.getData() != null && activityBean.getData().size() > 0) { Consts.setHasOtherEvent(true); // Consts.setIsHaveFlowerEvent(true); // //有活动 // PopWindowUtil.webviewEventsWindow(context, parent, new PopUtilClickListener() { // @Override // public void clickCancel() { // // } // // @Override // public void clickSubmit(Object object) { // //去webview界面,加载活动地址 // toWebFlowerActivity(context); // } // }, needWindow); } else { //没活动 Consts.setHasOtherEvent(false); } } } catch (Exception e) { e.printStackTrace(); } finally { Logger.e("发送了设置index的广播"); Intent intent = new Intent("com.edufound.reader.main.setindex"); context.sendBroadcast(intent); } } @Override public void onFailure(HttpInfo info) throws IOException { Logger.e("获取活动失败:" + info.getRetDetail()); Intent intent = new Intent("com.edufound.reader.main.setindex"); context.sendBroadcast(intent); } }); } public static void toWebFlowerActivity(Context context, String url, String activityId) { Intent showLoadingIntent = new Intent(ShowPageLoadingReceiver.PAGE_LOADING_SHOW); Consts.getmApplicAtion().getApplicationContext().sendBroadcast(showLoadingIntent); Intent intent = new Intent(context, WebActivity.class); intent.putExtra("loadUrl", url); Map map = new HashMap<>(); map.put("uid", Consts.getUID()); map.put("channelCode", Consts.getUmengChannel()); map.put("versionCode", DeviceUtil.getVersionCode(context)); if (activityId != null) { map.put("activityId", activityId); } intent.putExtra("paramsJson", new Gson().toJson(map)); context.startActivity(intent); } }