pdlivexp/common/src/main/java/com/yunbao/common/CommonAppConfig.java

676 lines
21 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.yunbao.common;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunbao.common.bean.ConfigBean;
import com.yunbao.common.bean.FansMedalBean;
import com.yunbao.common.bean.UserBean;
import com.yunbao.common.bean.UserItemBean;
import com.yunbao.common.http.CommonHttpUtil;
import com.yunbao.common.interfaces.CommonCallback;
import com.yunbao.common.utils.L;
import com.yunbao.common.utils.SpUtil;
import com.yunbao.common.utils.WordUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by cxf on 2017/8/4.
*/
public class CommonAppConfig {
//域名
public static final String HOST = getMetaDataString("SERVER_HOST");
//是否使用谷歌支付
public static final int IS_GOOGLE_PLAY = getMetaDataInt("IS_GOOGLE_PLAY");
//是否开启上报错误日志功能
public static final boolean IS_UPLOAD_ERROR_LOG = getMetaDataBoolean("IS_UPLOAD_ERROR_LOG");
//是否为插件包模式
public static final boolean IS_PLUGIN_MODEL = getMetaDataBoolean("IS_PLUGIN_MODEL");
public static final String BUILD_TIME = getMetaDataString("BUILD_TIME");
public static final boolean IS_SW_RELEASE = getMetaDataBoolean("SW_RELEASE_MODEL");
public static final int LINE_CHANNEL_ID = getMetaDataInt("LINE_ID");
//外部sd卡
public static final String DCMI_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
//内部存储 /data/data/<application package>/files目录
public static final String INNER_PATH = CommonAppContext.sInstance.getFilesDir().getAbsolutePath();
//文件夹名字
private static final String DIR_NAME = "yunbao";
//保存视频的时候在sd卡存储短视频的路径DCIM下
public static final String VIDEO_PATH = DCMI_PATH + "/" + DIR_NAME + "/video/";
public static final String VIDEO_RECORD_TEMP_PATH = VIDEO_PATH + "recordParts";
//下载贴纸的时候保存的路径 /storage/emulated/0/DCIM/yunbao/tieZhi/
public static final String VIDEO_TIE_ZHI_PATH = DCMI_PATH + "/" + DIR_NAME + "/tieZhi/";
//解压的美颜模型地址
public static String VIDEO_TIE_MODEl_PATH = "";
//360贴纸名字
public static final String TIEZHI_360_NAME = "sticker360";
//下载音乐的时候保存的路径
public static final String MUSIC_PATH = DCMI_PATH + "/" + DIR_NAME + "/music/";
//拍照时图片保存路径
public static final String CAMERA_IMAGE_PATH = DCMI_PATH + "/" + DIR_NAME + "/camera/";
//log保存路径
public static final String LOG_PATH = DCMI_PATH + "/" + DIR_NAME + "/log/";
public static final String GIF_PATH = INNER_PATH + "/gif/";
//QQ登录是否与PC端互通
public static final boolean QQ_LOGIN_WITH_PC = true;
//是否使用游戏
public static final boolean GAME_ENABLE = true;
//是否上下滑动切换直播间
public static final boolean LIVE_ROOM_SCROLL = false;
//直播sdk类型是否由后台控制的
public static final boolean LIVE_SDK_CHANGED = true;
//使用指定的直播sdk类型
public static final int LIVE_SDK_USED = Constants.LIVE_SDK_KSY;
private static CommonAppConfig sInstance;
public static boolean isGetNewWrap = false;//是否获取到新的包裹礼物
public static int alert_time = 1;
public static int alert_end_time = 1;
public static String SWToken = ""; //声网Token
public static final String SWChannelPrefix = "g";
//测试服
public static final String SWAPPId_debug = "4cf0376582d742ac8a96778b25b1079e";
//正式服
public static final String SWAPPId = "d4a5879524d74c20a7bdd667b0f2ca21";
public static String getSwAppId() {
if (IS_SW_RELEASE) {
return SWAPPId;
} else {
return SWAPPId_debug;
}
}
/**
* 获取通道
*
* @return
*/
public static String getChannelName() {
return SWChannelPrefix + getInstance().getUid();
}
private CommonAppConfig() {
}
public static CommonAppConfig getInstance() {
if (sInstance == null) {
synchronized (CommonAppConfig.class) {
if (sInstance == null) {
sInstance = new CommonAppConfig();
}
}
}
return sInstance;
}
private String mUid;
private String mToken;
private ConfigBean mConfig;
private double mLng;
private double mLat;
private String mProvince;//省
private String mCity;//市
private String mDistrict;//区
private UserBean mUserBean;
private String mVersion;
private boolean mLoginIM;//IM是否登录了
private boolean mLaunched;//App是否启动了
private String mJPushAppKey;//极光推送的AppKey
private List<UserItemBean> mUserItemList;//个人中心功能列表
private SparseArray<FansMedalBean> mAnchorFansMedalMap;
private String mGiftListJson;
private String mWrapListJson;
private String mTxMapAppKey;//腾讯定位地图的AppKey
private String mTxMapAppSecret;//腾讯地图的AppSecret
private boolean mFrontGround;
private int mAppIconRes;
private String mAppName;
private Boolean mTiBeautyEnable;//是否使用萌颜 true使用萌颜 false 使用基础美颜
public String getUid() {
if (TextUtils.isEmpty(mUid)) {
String[] uidAndToken = SpUtil.getInstance().getMultiStringValue(new String[]{SpUtil.UID, SpUtil.TOKEN});
if (uidAndToken != null) {
if (!TextUtils.isEmpty(uidAndToken[0]) && !TextUtils.isEmpty(uidAndToken[1])) {
mUid = uidAndToken[0];
mToken = uidAndToken[1];
}
} else {
return "-1";
}
}
return mUid;
}
public String getToken() {
return mToken;
}
public String getCoinName() {
ConfigBean configBean = getConfig();
if (configBean != null) {
return configBean.getCoinName();
}
return Constants.DIAMONDS;
}
public String getGoldCoinName() {
ConfigBean configBean = getConfig();
if (configBean != null) {
return configBean.getGoldCoinName();
}
return Constants.GOLD_NAME;
}
public String getVotesName() {
ConfigBean configBean = getConfig();
if (configBean != null) {
return configBean.getVotesName();
}
return Constants.VOTES;
}
public ConfigBean getConfig() {
if (mConfig == null) {
String configString = SpUtil.getInstance().getStringValue(SpUtil.CONFIG);
if (!TextUtils.isEmpty(configString)) {
mConfig = JSON.parseObject(configString, ConfigBean.class);
}
}
return mConfig;
}
public void getConfig(CommonCallback<ConfigBean> callback) {
if (callback == null) {
return;
}
ConfigBean configBean = getConfig();
if (configBean != null) {
callback.callback(configBean);
} else {
CommonHttpUtil.getConfig(null, callback);
}
}
private String[][] liveType = null;
public String[][] getLiveType() {
return liveType;
}
public void setConfig(ConfigBean config) {
mConfig = config;
liveType = config.getLiveType();
}
/**
* 经度
*/
public double getLng() {
if (mLng == 0) {
String lng = SpUtil.getInstance().getStringValue(SpUtil.LOCATION_LNG);
if (!TextUtils.isEmpty(lng)) {
try {
mLng = Double.parseDouble(lng);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return mLng;
}
/**
* 纬度
*/
public double getLat() {
if (mLat == 0) {
String lat = SpUtil.getInstance().getStringValue(SpUtil.LOCATION_LAT);
if (!TextUtils.isEmpty(lat)) {
try {
mLat = Double.parseDouble(lat);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return mLat;
}
/**
* 省
*/
public String getProvince() {
if (TextUtils.isEmpty(mProvince)) {
mProvince = SpUtil.getInstance().getStringValue(SpUtil.LOCATION_PROVINCE);
}
return mProvince == null ? "" : mProvince;
}
/**
* 市
*/
public String getCity() {
if (TextUtils.isEmpty(mCity)) {
mCity = SpUtil.getInstance().getStringValue(SpUtil.LOCATION_CITY);
}
return mCity == null ? "" : mCity;
}
/**
* 区
*/
public String getDistrict() {
if (TextUtils.isEmpty(mDistrict)) {
mDistrict = SpUtil.getInstance().getStringValue(SpUtil.LOCATION_DISTRICT);
}
return mDistrict == null ? "" : mDistrict;
}
public void setUserBean(UserBean bean) {
mUserBean = bean;
}
public UserBean getUserBean() {
if (mUserBean == null) {
String userBeanJson = SpUtil.getInstance().getStringValue(SpUtil.USER_INFO);
if (!TextUtils.isEmpty(userBeanJson)) {
mUserBean = JSON.parseObject(userBeanJson, UserBean.class);
}
}
return mUserBean;
}
/**
* 设置萌颜是否可用
*/
public void setTiBeautyEnable(boolean tiBeautyEnable) {
mTiBeautyEnable = tiBeautyEnable;
SpUtil.getInstance().setBooleanValue(SpUtil.TI_BEAUTY_ENABLE, tiBeautyEnable);
}
public boolean isTiBeautyEnable() {
if (mTiBeautyEnable == null) {
mTiBeautyEnable = SpUtil.getInstance().getBooleanValue(SpUtil.TI_BEAUTY_ENABLE);
}
return mTiBeautyEnable;
}
/**
* 设置登录信息
*/
public void setLoginInfo(String uid, String token, boolean save) {
L.e("登录成功", "uid------>" + uid);
L.e("登录成功", "token------>" + token);
mUid = uid;
mToken = token;
if (save) {
Map<String, String> map = new HashMap<>();
map.put(SpUtil.UID, uid);
map.put(SpUtil.TOKEN, token);
SpUtil.getInstance().setMultiStringValue(map);
}
}
/**
* 清除登录信息
*/
public void clearLoginInfo() {
mUid = null;
mToken = null;
mLoginIM = false;
SpUtil.getInstance().removeValue(SpUtil.UID, SpUtil.TOKEN, SpUtil.USER_INFO, SpUtil.IM_LOGIN);
}
/**
* 设置位置信息
*
* @param lng 经度
* @param lat 纬度
* @param province 省
* @param city 市
*/
public void setLocationInfo(double lng, double lat, String province, String city, String district) {
mLng = lng;
mLat = lat;
mProvince = province;
mCity = city;
mDistrict = district;
Map<String, String> map = new HashMap<>();
map.put(SpUtil.LOCATION_LNG, String.valueOf(lng));
map.put(SpUtil.LOCATION_LAT, String.valueOf(lat));
map.put(SpUtil.LOCATION_PROVINCE, province);
map.put(SpUtil.LOCATION_CITY, city);
map.put(SpUtil.LOCATION_DISTRICT, district);
SpUtil.getInstance().setMultiStringValue(map);
}
/**
* 清除定位信息
*/
public void clearLocationInfo() {
mLng = 0;
mLat = 0;
mProvince = null;
mCity = null;
mDistrict = null;
SpUtil.getInstance().removeValue(SpUtil.LOCATION_LNG, SpUtil.LOCATION_LAT, SpUtil.LOCATION_PROVINCE, SpUtil.LOCATION_CITY, SpUtil.LOCATION_DISTRICT);
}
public boolean isLoginIM() {
return mLoginIM;
}
public void setLoginIM(boolean loginIM) {
mLoginIM = loginIM;
}
/**
* 获取版本号
*/
public String getVersion() {
if (TextUtils.isEmpty(mVersion)) {
try {
PackageManager manager = CommonAppContext.sInstance.getPackageManager();
PackageInfo info = manager.getPackageInfo(CommonAppContext.sInstance.getPackageName(), 0);
mVersion = info.versionName;
} catch (Exception e) {
e.printStackTrace();
}
}
return mVersion;
}
/**
* 获取App名称
*/
public String getAppName() {
if (TextUtils.isEmpty(mAppName)) {
int res = CommonAppContext.sInstance.getResources().getIdentifier("app_name", "string", "myname.pdlive.shayu");
mAppName = WordUtil.getString(res);
}
return mAppName;
}
/**
* 获取App图标的资源id
*/
public int getAppIconRes() {
if (mAppIconRes == 0) {
mAppIconRes = CommonAppContext.sInstance.getResources().getIdentifier("ic_launcher", "mipmap", CommonAppContext.sInstance.getPackageName());
}
return mAppIconRes;
}
/**
* 获取MetaData中的极光AppKey
*/
public String getJPushAppKey() {
if (mJPushAppKey == null) {
mJPushAppKey = getMetaDataString("JPUSH_APPKEY");
}
return mJPushAppKey;
}
/**
* 获取MetaData中的腾讯定位地图的AppKey
*
* @return
*/
public String getTxMapAppKey() {
if (mTxMapAppKey == null) {
mTxMapAppKey = getMetaDataString("TencentMapSDK");
}
return mTxMapAppKey;
}
/**
* 获取MetaData中的腾讯定位地图的AppSecret
*
* @return
*/
public String getTxMapAppSecret() {
if (mTxMapAppSecret == null) {
mTxMapAppSecret = getMetaDataString("TencentMapAppSecret");
}
return mTxMapAppSecret;
}
private static String getMetaDataString(String key) {
String res = null;
try {
ApplicationInfo appInfo = CommonAppContext.sInstance.getPackageManager().getApplicationInfo(CommonAppContext.sInstance.getPackageName(), PackageManager.GET_META_DATA);
res = appInfo.metaData.getString(key);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Log.i("tag", "url:" + res);
return res;
}
private static boolean getMetaDataBoolean(String key) {
boolean res = false;
try {
ApplicationInfo appInfo = CommonAppContext.sInstance.getPackageManager().getApplicationInfo(CommonAppContext.sInstance.getPackageName(), PackageManager.GET_META_DATA);
res = appInfo.metaData.getBoolean(key);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return res;
}
private static int getMetaDataInt(String key) {
int res = 0;
try {
ApplicationInfo appInfo = CommonAppContext.sInstance.getPackageManager().getApplicationInfo(CommonAppContext.sInstance.getPackageName(), PackageManager.GET_META_DATA);
res = appInfo.metaData.getInt(key);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return res;
}
/**
* 个人中心功能列表
*/
public List<UserItemBean> getUserItemList() {
if (mUserItemList == null || mUserItemList.size() == 0) {
String userBeanJson = SpUtil.getInstance().getStringValue(SpUtil.USER_INFO);
if (!TextUtils.isEmpty(userBeanJson)) {
JSONObject obj = JSON.parseObject(userBeanJson);
if (obj != null) {
setUserItemList(obj.getString("list"));
}
}
}
return mUserItemList;
}
public void setUserItemList(String listString) {
UserItemBean[][] arr = JSON.parseObject(listString, UserItemBean[][].class);
if (arr != null && arr.length > 0) {
List<UserItemBean> newList = new ArrayList<>();
for (int i = 0, length1 = arr.length; i < length1; i++) {
for (int j = 0, length2 = arr[i].length; j < length2; j++) {
UserItemBean bean = arr[i][j];
if (j == length2 - 1) {
if (i < length1 - 1) {
bean.setGroupLast(true);
} else {
bean.setAllLast(true);
}
}
newList.add(bean);
}
}
mUserItemList = newList;
}
}
/**
* 保存主播粉丝徽章信息
*/
public void setAnchorFansMedal(String anchorMedalJson) {
if (TextUtils.isEmpty(anchorMedalJson)) {
return;
}
List<FansMedalBean> list = JSON.parseArray(anchorMedalJson, FansMedalBean.class);
if (list == null || list.size() == 0) {
return;
}
if (mAnchorFansMedalMap == null) {
mAnchorFansMedalMap = new SparseArray<>();
}
mAnchorFansMedalMap.clear();
for (FansMedalBean bean : list) {
mAnchorFansMedalMap.put(bean.getLevel(), bean);
}
}
/**
* 获取主播粉丝徽章信息
*/
public FansMedalBean getAnchorFansMedal(int level) {
if (mAnchorFansMedalMap == null) {
String configString = SpUtil.getInstance().getStringValue(SpUtil.CONFIG);
if (!TextUtils.isEmpty(configString)) {
JSONObject obj = JSON.parseObject(configString);
setAnchorFansMedal(obj.getString("levelfans"));
}
}
if (mAnchorFansMedalMap == null || mAnchorFansMedalMap.size() == 0) {
return null;
}
return mAnchorFansMedalMap.get(level);
}
public String getGiftListJson() {
return mGiftListJson;
}
public void setGiftListJson(String getGiftListJson) {
mGiftListJson = getGiftListJson;
}
public String getWrapListJson() {
return mWrapListJson;
}
public void setWrapListJson(String wrapListJson) {
mWrapListJson = wrapListJson;
}
/**
* 判断某APP是否安装
*/
public static boolean isAppExist(String packageName) {
if (!TextUtils.isEmpty(packageName)) {
PackageManager manager = CommonAppContext.sInstance.getPackageManager();
List<PackageInfo> list = manager.getInstalledPackages(0);
for (PackageInfo info : list) {
if (packageName.equalsIgnoreCase(info.packageName)) {
return true;
}
}
}
return false;
}
public boolean isLaunched() {
return mLaunched;
}
public void setLaunched(boolean launched) {
mLaunched = launched;
}
//app是否在前台
public boolean isFrontGround() {
return mFrontGround;
}
//app是否在前台
public void setFrontGround(boolean frontGround) {
mFrontGround = frontGround;
}
//设置美颜类型
public void setBeautySdkType(String sproutType) {
SpUtil.getInstance().setStringValue(SpUtil.BEAUTY_SDK_TYPE, sproutType);
}
public String getBeautySdkType() {
return SpUtil.getInstance().getStringValue(SpUtil.BEAUTY_SDK_TYPE);
}
//设置是否显示转盘功能
public void setTurnTableEnable(String enable) {
SpUtil.getInstance().setStringValue(SpUtil.TURNTABLE_ENABLE, enable);
}
public String getTurnTableEnable() {
return SpUtil.getInstance().getStringValue(SpUtil.TURNTABLE_ENABLE);
}
//设置360美颜贴纸下载地址
public void setBeauty360TieZhiUrl(String tieZhiUrl) {
SpUtil.getInstance().setStringValue(SpUtil.BEAUTY_360_TIEZHI_URL, tieZhiUrl);
}
public String getBeauty360TieZhiUrl() {
return SpUtil.getInstance().getStringValue(SpUtil.BEAUTY_360_TIEZHI_URL);
}
//360贴纸已经下载过
public void setBeauty360TieZhiExist(boolean isExist) {
SpUtil.getInstance().setBooleanValue(SpUtil.BEAUTY_360_TIEZHI_EXIST, isExist);
}
public boolean getBeauty360TieZhiExist() {
return SpUtil.getInstance().getBooleanValue(SpUtil.BEAUTY_360_TIEZHI_EXIST);
}
//设置主播pk时间
public void setAnchorPkTime(String pkTime) {
SpUtil.getInstance().setStringValue(SpUtil.ANCHOR_PK_TIME, pkTime);
}
public String getAnchorPkTime() {
return SpUtil.getInstance().getStringValue(SpUtil.ANCHOR_PK_TIME);
}
public static boolean isTestEnv() {
return CommonAppConfig.HOST.contains("ceshi");
}
}