通过feature控制应用是否支持某功能
2014-10-29 11:07
465 查看
Android TV应用开发往往需要发布多个渠道版本,针对不同渠道版本支持的功能可能不一样,这样就引入一个问题,怎样在同一套代码中控制某个版本是否支持某个功能。项目中我们通过asserts下面的配置文件中的不同feature去实现此需求。
废话不多说,直接上代码。
asserts/AppConfig.json
{
"bsChannel": "letvrelease_0_0_1_2",//标识渠道商号
"featureSupportLive":true,//直播开关
"featureSupportAD": true//广告开关
}
com/simple/AppConfig.java
public final class AppConfig {
private static final String SEMICOLON = ";";
// Define the file name
private static final String LETV_APP_CONFIG_FILE_NAME = "AppConfig.json";
// Define the Json structure
private static final String BSCHANNEL = "bsChannel";
public static final String FEATURE_SUPPORT_AD = "featureSupportAD";
public static final String FEATURE_SUPPORT_LIVE = "featureSupportLive";
private static AppConfig sAppConfig;
// 渠道商号
private String mBsChannel = "_";
// is support AD
private boolean isSupportAD = false;
// is support live
private boolean isSupportLive = true;
/**
* This method should be invoked before any other function of this class is.
* @param context
*/
public void init() {
//获取资产管理器
AssetManager am = LetvApplication.getApplication().getAssets();
ByteArrayOutputStream baos = null;
InputStream is = null;
String configString = null;
try {
//读取配置文件信息,将字节输出流转换成字符串
is = am.open(LETV_APP_CONFIG_FILE_NAME);
baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1) {
baos.write(i);
}
configString = baos.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeStream(baos);
IOUtils.closeStream(is);
}
try {
JSONObject appConfig;
if (Utils.isStringEmpty(configString)) {
return;
}
//将字符串封装成json对象,读取相应属性值
appConfig = new JSONObject(configString);
this.initData(appConfig);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void initData(JSONObject appConfig) throws JSONException {
Logger.i("AppConfig", "Json string = " + appConfig.toString());
//读取渠道商号
String bsChannelFromAppConfig = appConfig.getString(BSCHANNEL);
this.mBsChannel = BsChannelUtils
.getAppBsChannel(bsChannelFromAppConfig);
//读取广告开关(部分渠道本版不支持广告播放,通过此开关控制)
this.isSupportAD = appConfig.getBoolean(FEATURE_SUPPORT_AD);
//读取直播开关(部分渠道本版不支持直播播放,通过此开关控制)
this.isSupportLive = appConfig.getBoolean(FEATURE_SUPPORT_LIVE);
}
private synchronized static AppConfig getInstance() {
if (sAppConfig == null) {
sAppConfig = new AppConfig();
}
return sAppConfig;
}
private AppConfig() {
this.init();
}
/**
*获取渠道商号
*@return
*/
public static String getBsChannel() {
return StringUtils.encodeStr(getInstance().mBsChannel);
}
/**
* Is support AD
* @return
*/
public static boolean isSupportAD() {
return getInstance().isSupportAD;
}
/**
* is support live
* @return
*/
public static boolean isSupportLive() {
return getInstance().isSupportLive;
}
}
废话不多说,直接上代码。
asserts/AppConfig.json
{
"bsChannel": "letvrelease_0_0_1_2",//标识渠道商号
"featureSupportLive":true,//直播开关
"featureSupportAD": true//广告开关
}
com/simple/AppConfig.java
public final class AppConfig {
private static final String SEMICOLON = ";";
// Define the file name
private static final String LETV_APP_CONFIG_FILE_NAME = "AppConfig.json";
// Define the Json structure
private static final String BSCHANNEL = "bsChannel";
public static final String FEATURE_SUPPORT_AD = "featureSupportAD";
public static final String FEATURE_SUPPORT_LIVE = "featureSupportLive";
private static AppConfig sAppConfig;
// 渠道商号
private String mBsChannel = "_";
// is support AD
private boolean isSupportAD = false;
// is support live
private boolean isSupportLive = true;
/**
* This method should be invoked before any other function of this class is.
* @param context
*/
public void init() {
//获取资产管理器
AssetManager am = LetvApplication.getApplication().getAssets();
ByteArrayOutputStream baos = null;
InputStream is = null;
String configString = null;
try {
//读取配置文件信息,将字节输出流转换成字符串
is = am.open(LETV_APP_CONFIG_FILE_NAME);
baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1) {
baos.write(i);
}
configString = baos.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeStream(baos);
IOUtils.closeStream(is);
}
try {
JSONObject appConfig;
if (Utils.isStringEmpty(configString)) {
return;
}
//将字符串封装成json对象,读取相应属性值
appConfig = new JSONObject(configString);
this.initData(appConfig);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void initData(JSONObject appConfig) throws JSONException {
Logger.i("AppConfig", "Json string = " + appConfig.toString());
//读取渠道商号
String bsChannelFromAppConfig = appConfig.getString(BSCHANNEL);
this.mBsChannel = BsChannelUtils
.getAppBsChannel(bsChannelFromAppConfig);
//读取广告开关(部分渠道本版不支持广告播放,通过此开关控制)
this.isSupportAD = appConfig.getBoolean(FEATURE_SUPPORT_AD);
//读取直播开关(部分渠道本版不支持直播播放,通过此开关控制)
this.isSupportLive = appConfig.getBoolean(FEATURE_SUPPORT_LIVE);
}
private synchronized static AppConfig getInstance() {
if (sAppConfig == null) {
sAppConfig = new AppConfig();
}
return sAppConfig;
}
private AppConfig() {
this.init();
}
/**
*获取渠道商号
*@return
*/
public static String getBsChannel() {
return StringUtils.encodeStr(getInstance().mBsChannel);
}
/**
* Is support AD
* @return
*/
public static boolean isSupportAD() {
return getInstance().isSupportAD;
}
/**
* is support live
* @return
*/
public static boolean isSupportLive() {
return getInstance().isSupportLive;
}
}
相关文章推荐
- Hadoop-2.6.0中关于控制应用是否通过CGroup限制CPU的优化
- PHP输出控制功能在简繁体转换中的应用
- PHP输出控制功能在简繁体转换中的应用
- PHP输出控制功能在简繁体转换中的应用
- WIN CE下通过注册表键值控制RIL模块的扩展功能
- 通过调用第三方应用实现彩信或邮件添加附件(Photo,Video,Audio)功能
- PHP输出控制功能在简繁体转换中的应用
- TouchPad应用更新:通过Siri控制Mac和PC
- WIN CE下通过注册表键值控制RIL模块的扩展功能
- 检测手机是否支持蓝牙功能
- 百度Map 键盘控制 右键菜单 测距 以及各个功能应用
- PHP输出控制功能在简繁体转换中的应用
- 综合应用WPF/WCF/WF/LINQ之十一:LINQ的ORM功能中对自定义的枚举的支持
- JS是否可以跨文件同时控制多个iframe页面的应用技巧
- WIN CE下通过注册表键值控制RIL模块的扩展功能
- PHP输出控制功能在简繁体转换中的应用
- PHP输出控制功能在简繁体转换中的应用
- PHP输出控制功能在简繁体转换中的应用
- 如何通过控制端口保障电脑安全(黑客技术小应用)
- TouchPad应用更新:通过Siri控制Mac和PC