您的位置:首页 > Web前端

通过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;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: