通过feature控制应用是否支持某功能

      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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值