444 lines
15 KiB
Java
444 lines
15 KiB
Java
package cn.shangyu.gdxzExpert.utils;
|
||
|
||
import android.app.ActivityManager;
|
||
import android.app.ActivityManager.RunningAppProcessInfo;
|
||
import android.content.Context;
|
||
import android.content.res.ColorStateList;
|
||
import android.graphics.drawable.Drawable;
|
||
import android.net.ConnectivityManager;
|
||
import android.net.NetworkInfo;
|
||
import android.os.Handler;
|
||
import android.view.View;
|
||
import android.widget.Toast;
|
||
|
||
import java.util.List;
|
||
|
||
import android.app.Activity;
|
||
import android.content.Intent;
|
||
import com.lidroid.xutils.exception.HttpException;
|
||
import com.lidroid.xutils.http.RequestParams;
|
||
import com.lidroid.xutils.http.ResponseInfo;
|
||
import com.lidroid.xutils.http.callback.RequestCallBack;
|
||
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
|
||
import cn.shangyu.gdxzExpert.activity.LoginNewActivity;
|
||
import cn.shangyu.gdxzExpert.application.BaseApplication;
|
||
import cn.shangyu.gdxzExpert.bean.ChangeMyInfoBean;
|
||
import cn.shangyu.gdxzExpert.http.HttpHelper;
|
||
/**
|
||
*
|
||
* UIUtils.java
|
||
* @author gaofei
|
||
* @date 2015-8-31 上午9:54:49
|
||
* Copyright (C) 2015 河南商宇科技有限公司
|
||
*
|
||
* 功能说明:一些基本的工具类,1、获取上下文 2、得到主线程id以及主线程 3、得到handler 4、获取资源文件
|
||
* 6、把资源id转化为string,7、把资源id转化为drawable 8、把资源id转化为stringArray 9、dip--->px 10、px---->dp
|
||
* 11、java代码区设置颜色选择器的方法 12、资源id获得颜色 14、检测网络是否可用 15 、检测Sdcard是否存在
|
||
*
|
||
* 等等。
|
||
*/
|
||
public class UIUtils {
|
||
|
||
private static Toast mToast;
|
||
//得到上下文
|
||
public static Context getContext(){
|
||
return BaseApplication.getContext();
|
||
}
|
||
//得到主线程id
|
||
public static int getMainThreadId(){
|
||
return BaseApplication.getMainThreadId();
|
||
}
|
||
//得到主线程
|
||
public static Thread getMainThread(){
|
||
return BaseApplication.getMainThread();
|
||
}
|
||
//得到handler
|
||
public static Handler getHandler(){
|
||
return BaseApplication.getHandler();
|
||
}
|
||
|
||
//把资源id转化为string
|
||
public static String getString(int id){
|
||
return getContext().getString(id);
|
||
}
|
||
//把资源id转化为drawable
|
||
public static Drawable getDrawable(int id){
|
||
return getContext().getResources().getDrawable(id);
|
||
}
|
||
//把资源id转化为stringArray
|
||
public static String[] getStringArray(int id){
|
||
return getContext().getResources().getStringArray(id);
|
||
}
|
||
|
||
//dip--->px 1dp = 1px 1dp = 2px
|
||
public static int dip2px(int dip){
|
||
//dp和px的转换关系比例值
|
||
float density = getContext().getResources().getDisplayMetrics().density;
|
||
return (int)(dip*density+0.5);
|
||
}
|
||
//px---->dp
|
||
public static int px2dip(int px){
|
||
//dp和px的转换关系比例值
|
||
float density = getContext().getResources().getDisplayMetrics().density;
|
||
return (int)(px/density+0.5);
|
||
}
|
||
|
||
//判断是否是主线的方法
|
||
public static boolean isRunInMainThread(){
|
||
return getMainThreadId() == android.os.Process.myTid();
|
||
}
|
||
|
||
/**
|
||
* 在主线程执行runnable
|
||
*/
|
||
public static boolean post(Runnable runnable) {
|
||
return getHandler().post(runnable);
|
||
}
|
||
|
||
//保证当前的UI操作在主线程里面运行
|
||
public static void runInMainThread(Runnable runnable){
|
||
if(isRunInMainThread()){
|
||
//如果现在就是在珠现场中,就直接运行run方法
|
||
runnable.run();
|
||
}else{
|
||
//否则将其传递到主线程中运行
|
||
post(runnable);
|
||
}
|
||
}
|
||
|
||
//java代码区设置颜色选择器的方法
|
||
public static ColorStateList getColorStateList(int mTabTextColorResId) {
|
||
return getContext().getResources().getColorStateList(mTabTextColorResId);
|
||
}
|
||
/**
|
||
* 把xml文件转化为view
|
||
*
|
||
*/
|
||
public static View inflate(int id){
|
||
return View.inflate(getContext(), id, null);
|
||
}
|
||
|
||
public static int getDimens(int id) {
|
||
//根据dimens中提供的id,将其对应的dp值转换成相应的像素值大小
|
||
return UIUtils.getContext().getResources().getDimensionPixelSize(id);
|
||
}
|
||
|
||
public static void postDelayed(Runnable runnable,long delayTime) {
|
||
getHandler().postDelayed(runnable, delayTime);
|
||
}
|
||
|
||
public static void removeCallback(Runnable runnable) {
|
||
//移除在当前handler中维护的任务(传递进来的任务)
|
||
getHandler().removeCallbacks(runnable);
|
||
|
||
}
|
||
/**
|
||
* 资源id获得颜色
|
||
*
|
||
*/
|
||
public static int getColor(int id) {
|
||
return getContext().getResources().getColor(id);
|
||
}
|
||
|
||
|
||
/**
|
||
*
|
||
* @author gaofei
|
||
* @date 2015-9-6 下午7:41:31
|
||
*
|
||
* 功能说明:检测网络是否可用
|
||
*/
|
||
public static boolean isNetWorkConnected(Context context) {
|
||
if (context != null) {
|
||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
|
||
if (mNetworkInfo != null) {
|
||
return mNetworkInfo.isAvailable();
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @author gaofei
|
||
* @date 2015-9-6 下午7:41:31
|
||
*
|
||
* 功能说明:检测Sdcard是否存在
|
||
*/
|
||
public static boolean isExitsSdcard() {
|
||
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
|
||
return true;
|
||
else
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
*
|
||
* isLogined
|
||
* 判断是否登录,以后要加上容联的,以及自动登录需要考虑,现在只是简单读取sp文件
|
||
* true表示已登录
|
||
* @return
|
||
* boolean
|
||
*/
|
||
public static boolean isAccountLogined() {
|
||
boolean isLogin = SharePrefUtil.getBoolean(getContext(), Constant.IS_LOGIN, false);
|
||
String uuid = SharePrefUtil.getString(getContext(), Constant.uuid, "");
|
||
return isLogin && !StringUtil.isEmpty(uuid);
|
||
}
|
||
|
||
public static boolean isLogined() {
|
||
return isAccountLogined();
|
||
}
|
||
|
||
/**
|
||
* 判断当前用户是否已通过医师/专家资质审核(已登录账号 且 state == 6)
|
||
* 仅用于【我的患者】模块与【随访服务】模块
|
||
*/
|
||
public static boolean isDoctorVerified() {
|
||
return isLogined() && SharePrefUtil.getInt(getContext(), "state", 0) == 6;
|
||
}
|
||
|
||
public interface OnDoctorStateRefreshListener {
|
||
void onStateRefreshed(int state, String checkInfo);
|
||
}
|
||
|
||
/**
|
||
* 将 getExpertByUuid 返回的专家详情及审核状态同步到本地 SharePrefUtil
|
||
*/
|
||
public static void syncExpertInfoToSp(Context context, ChangeMyInfoBean bean) {
|
||
if (bean == null || !"1".equals(bean.code) || bean.expert == null) {
|
||
return;
|
||
}
|
||
Context ctx = context != null ? context : getContext();
|
||
ChangeMyInfoBean.InfoData data = bean.expert;
|
||
if (data.state > 0) {
|
||
SharePrefUtil.saveInt(ctx, "state", data.state);
|
||
}
|
||
if (!StringUtil.isEmpty(data.checkInfo)) {
|
||
SharePrefUtil.saveString(ctx, "checkInfo", data.checkInfo);
|
||
}
|
||
if (!StringUtil.isEmpty(data.realName)) {
|
||
SharePrefUtil.saveString(ctx, Constant.realName, data.realName);
|
||
}
|
||
if (!StringUtil.isEmpty(data.photo)) {
|
||
SharePrefUtil.saveString(ctx, Constant.photo, data.photo);
|
||
}
|
||
if (!StringUtil.isEmpty(data.hospitalUuid)) {
|
||
SharePrefUtil.saveString(ctx, "hospitalUuid", data.hospitalUuid);
|
||
}
|
||
if (!StringUtil.isEmpty(data.hospitalName)) {
|
||
SharePrefUtil.saveString(ctx, "hospitalName", data.hospitalName);
|
||
}
|
||
if (!StringUtil.isEmpty(data.officeUuid)) {
|
||
SharePrefUtil.saveString(ctx, "officeUuid", data.officeUuid);
|
||
}
|
||
if (!StringUtil.isEmpty(data.officeName)) {
|
||
SharePrefUtil.saveString(ctx, "officeName", data.officeName);
|
||
}
|
||
if (!StringUtil.isEmpty(data.officePhone)) {
|
||
SharePrefUtil.saveString(ctx, "officePhone", data.officePhone);
|
||
}
|
||
if (!StringUtil.isEmpty(data.positionUuid)) {
|
||
SharePrefUtil.saveString(ctx, "positionUuid", data.positionUuid);
|
||
}
|
||
if (!StringUtil.isEmpty(data.positionName)) {
|
||
SharePrefUtil.saveString(ctx, "positionName", data.positionName);
|
||
}
|
||
if (!StringUtil.isEmpty(data.certificate)) {
|
||
SharePrefUtil.saveString(ctx, "certificate", data.certificate);
|
||
}
|
||
if (!StringUtil.isEmpty(data.certificateImg)) {
|
||
SharePrefUtil.saveString(ctx, "certificateImg", data.certificateImg);
|
||
}
|
||
if (!StringUtil.isEmpty(data.qrcode)) {
|
||
SharePrefUtil.saveString(ctx, "qrcode", data.qrcode);
|
||
}
|
||
if (!StringUtil.isEmpty(data.isStar)) {
|
||
SharePrefUtil.saveString(ctx, "isStar", data.isStar);
|
||
}
|
||
SharePrefUtil.saveInt(ctx, "sex", data.sex);
|
||
}
|
||
|
||
/**
|
||
* 当账号已登录但尚未认证通过(state != 6)时,静默向服务端拉取最新审核状态(state)并同步到本地
|
||
*/
|
||
public static void refreshDoctorStateIfNeeded(final Context context, final OnDoctorStateRefreshListener listener) {
|
||
if (!isLogined() || isDoctorVerified()) {
|
||
return;
|
||
}
|
||
final Context ctx = context != null ? context : getContext();
|
||
String uuid = SharePrefUtil.getString(ctx, Constant.uuid, "");
|
||
if (StringUtil.isEmpty(uuid)) {
|
||
return;
|
||
}
|
||
RequestParams params = new RequestParams();
|
||
params.addBodyParameter("uuid", uuid);
|
||
HttpHelper.loadData(HttpMethod.POST, Url.getExpertByUuid, params, new RequestCallBack<String>() {
|
||
@Override
|
||
public void onSuccess(ResponseInfo<String> info) {
|
||
if (info == null || StringUtil.isEmpty(info.result)) {
|
||
return;
|
||
}
|
||
try {
|
||
ChangeMyInfoBean bean = GsonTools.fromGsonToBean(info.result, ChangeMyInfoBean.class);
|
||
if (bean != null && "1".equals(bean.code) && bean.expert != null) {
|
||
SharePrefUtil.saveString(ctx, Url.getExpertByUuid, info.result);
|
||
syncExpertInfoToSp(ctx, bean);
|
||
if (listener != null) {
|
||
String checkInfo = !StringUtil.isEmpty(bean.expert.checkInfo)
|
||
? bean.expert.checkInfo
|
||
: SharePrefUtil.getString(ctx, "checkInfo", "");
|
||
listener.onStateRefreshed(bean.expert.state, checkInfo);
|
||
}
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(HttpException arg0, String arg1) {
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 校验是否已通过医师资质审核(state == 6)
|
||
* - 未登录账号:跳 LoginNewActivity
|
||
* - 已登录账号但 state != 6:跳 LoginSetInfoActivity(完善个人资料/查看审核状态)
|
||
*/
|
||
public static boolean checkDoctorVerifiedAndJump(Context context) {
|
||
if (isDoctorVerified()) {
|
||
return true;
|
||
}
|
||
if (!isLogined()) {
|
||
return checkLoginAndJump(context);
|
||
}
|
||
long now = System.currentTimeMillis();
|
||
if (now - lastJumpLoginTime < 800) {
|
||
return false;
|
||
}
|
||
lastJumpLoginTime = now;
|
||
Context targetContext = context != null ? context : getContext();
|
||
Intent intent = new Intent(targetContext, cn.shangyu.gdxzExpert.activity.LoginSetInfoActivity.class);
|
||
intent.putExtra("state", SharePrefUtil.getInt(getContext(), "state", 0));
|
||
intent.putExtra("checkInfo", SharePrefUtil.getString(getContext(), "checkInfo", ""));
|
||
intent.putExtra("phone", SharePrefUtil.getString(getContext(), Constant.mobile, ""));
|
||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||
if (!(targetContext instanceof Activity)) {
|
||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
}
|
||
targetContext.startActivity(intent);
|
||
return false;
|
||
}
|
||
|
||
public static boolean checkAccountLoginAndJump(Context context) {
|
||
if (isAccountLogined()) {
|
||
return true;
|
||
}
|
||
long now = System.currentTimeMillis();
|
||
if (now - lastJumpLoginTime < 800) {
|
||
return false;
|
||
}
|
||
lastJumpLoginTime = now;
|
||
Context targetContext = context != null ? context : getContext();
|
||
Intent intent;
|
||
if (isAccountLogined()) {
|
||
intent = new Intent(targetContext, cn.shangyu.gdxzExpert.activity.LoginSetInfoActivity.class);
|
||
intent.putExtra("state", SharePrefUtil.getInt(getContext(), "state", 0));
|
||
intent.putExtra("checkInfo", SharePrefUtil.getString(getContext(), "checkInfo", ""));
|
||
intent.putExtra("phone", SharePrefUtil.getString(getContext(), Constant.mobile, ""));
|
||
} else {
|
||
intent = new Intent(targetContext, LoginNewActivity.class);
|
||
}
|
||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||
if (!(targetContext instanceof Activity)) {
|
||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
}
|
||
targetContext.startActivity(intent);
|
||
return false;
|
||
}
|
||
|
||
private static boolean unusedOldIsLogined() {
|
||
// TODO Auto-generated method stub
|
||
boolean isLogin = SharePrefUtil.getBoolean(getContext(), Constant.IS_LOGIN, false);
|
||
String uuid = SharePrefUtil.getString(getContext(), Constant.uuid, "");
|
||
return isLogin && !StringUtil.isEmpty(uuid);
|
||
}
|
||
|
||
/**
|
||
* 校验是否已登录,未登录则拉起登录页
|
||
*
|
||
* @param context 上下文
|
||
* @return true 已登录;false 未登录(已跳转登录页)
|
||
*/
|
||
private static long lastJumpLoginTime = 0L;
|
||
|
||
public static boolean checkLoginAndJump(Context context) {
|
||
if (isLogined()) {
|
||
return true;
|
||
}
|
||
long now = System.currentTimeMillis();
|
||
if (now - lastJumpLoginTime < 800) {
|
||
return false;
|
||
}
|
||
lastJumpLoginTime = now;
|
||
Context targetContext = context != null ? context : getContext();
|
||
Intent intent;
|
||
if (isAccountLogined()) {
|
||
intent = new Intent(targetContext, cn.shangyu.gdxzExpert.activity.LoginSetInfoActivity.class);
|
||
intent.putExtra("state", SharePrefUtil.getInt(getContext(), "state", 0));
|
||
intent.putExtra("checkInfo", SharePrefUtil.getString(getContext(), "checkInfo", ""));
|
||
intent.putExtra("phone", SharePrefUtil.getString(getContext(), Constant.mobile, ""));
|
||
} else {
|
||
intent = new Intent(targetContext, LoginNewActivity.class);
|
||
}
|
||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||
if (!(targetContext instanceof Activity)) {
|
||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
}
|
||
targetContext.startActivity(intent);
|
||
return false;
|
||
}
|
||
|
||
public static boolean isBackground() {
|
||
ActivityManager activityManager = (ActivityManager) getContext()
|
||
.getSystemService(Context.ACTIVITY_SERVICE);
|
||
List<RunningAppProcessInfo> appProcesses = activityManager
|
||
.getRunningAppProcesses();
|
||
for (RunningAppProcessInfo appProcess : appProcesses) {
|
||
if (appProcess.processName.equals( getContext() .getPackageName())) {
|
||
/*
|
||
BACKGROUND=400 EMPTY=500 FOREGROUND=100
|
||
GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
|
||
*/
|
||
// LogUtil.i( getContext() .getPackageName(), "appimportace ="
|
||
// + appProcess.importance
|
||
// + ",context.getClass().getName()="
|
||
// + getContext() .getClass().getName());
|
||
if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
|
||
// LogUtil.i( getContext() .getPackageName(), "处于后台"
|
||
// + appProcess.processName);
|
||
return true;
|
||
} else {
|
||
// LogUtil.i( getContext() .getPackageName(), "处于前台"
|
||
// + appProcess.processName);
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
// public static Boolean isActivityBackground()
|
||
// {
|
||
// ActivityManager manager = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE);
|
||
// ActivityManager.RunningTaskInfo info = manager.getRunningTasks(1).get(0);
|
||
// String shortClassName = info.topActivity.getShortClassName(); // 类名
|
||
// String className = info.topActivity.getClassName(); // 完整类名
|
||
// String packageName = info.topActivity.getPackageName(); // 包
|
||
// return true;
|
||
// }
|
||
|
||
}
|