ChannelUtil.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.czt.util;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.content.SharedPreferences.Editor;
  5. import android.content.pm.ApplicationInfo;
  6. import android.content.pm.PackageManager.NameNotFoundException;
  7. import android.preference.PreferenceManager;
  8. import android.text.TextUtils;
  9. import java.io.IOException;
  10. import java.util.Enumeration;
  11. import java.util.zip.ZipEntry;
  12. import java.util.zip.ZipFile;
  13. public class ChannelUtil {
  14. private static final String CHANNEL_KEY = "cztchannel";
  15. private static final String CHANNEL_VERSION_KEY = "cztchannel_version";
  16. private static String mChannel;
  17. /**
  18. * 返回市场。 如果获取失败返回""
  19. * @param context
  20. * @return
  21. */
  22. public static String getChannel(Context context){
  23. return getChannel(context, "");
  24. }
  25. /**
  26. * 返回市场。 如果获取失败返回defaultChannel
  27. * @param context
  28. * @param defaultChannel
  29. * @return
  30. */
  31. public static String getChannel(Context context, String defaultChannel) {
  32. //内存中获取
  33. if(!TextUtils.isEmpty(mChannel)){
  34. return mChannel;
  35. }
  36. //sp中获取
  37. mChannel = getChannelBySharedPreferences(context);
  38. if(!TextUtils.isEmpty(mChannel)){
  39. return mChannel;
  40. }
  41. //从apk中获取
  42. mChannel = getChannelFromApk(context, CHANNEL_KEY);
  43. if(!TextUtils.isEmpty(mChannel)){
  44. //保存sp中备用
  45. saveChannelBySharedPreferences(context, mChannel);
  46. return mChannel;
  47. }
  48. //全部获取失败
  49. return defaultChannel;
  50. }
  51. /**
  52. * 从apk中获取版本信息
  53. * @param context
  54. * @param channelKey
  55. * @return
  56. */
  57. private static String getChannelFromApk(Context context, String channelKey) {
  58. //从apk包中获取
  59. ApplicationInfo appinfo = context.getApplicationInfo();
  60. String sourceDir = appinfo.sourceDir;
  61. //默认放在meta-inf/里, 所以需要再拼接一下
  62. String key = "META-INF/" + channelKey;
  63. String ret = "";
  64. ZipFile zipfile = null;
  65. try {
  66. zipfile = new ZipFile(sourceDir);
  67. Enumeration<?> entries = zipfile.entries();
  68. while (entries.hasMoreElements()) {
  69. ZipEntry entry = ((ZipEntry) entries.nextElement());
  70. String entryName = entry.getName();
  71. if (entryName.startsWith(key)) {
  72. ret = entryName;
  73. break;
  74. }
  75. }
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. } finally {
  79. if (zipfile != null) {
  80. try {
  81. zipfile.close();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. }
  87. String[] split = ret.split("_");
  88. String channel = "";
  89. if (split != null && split.length >= 2) {
  90. channel = ret.substring(split[0].length() + 1);
  91. }
  92. return channel;
  93. }
  94. /**
  95. * 本地保存channel & 对应版本号
  96. * @param context
  97. * @param channel
  98. */
  99. private static void saveChannelBySharedPreferences(Context context, String channel){
  100. SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
  101. Editor editor = sp.edit();
  102. editor.putString(CHANNEL_KEY, channel);
  103. editor.putInt(CHANNEL_VERSION_KEY, getVersionCode(context));
  104. editor.commit();
  105. }
  106. /**
  107. * 从sp中获取channel
  108. * @param context
  109. * @return 为空表示获取异常、sp中的值已经失效、sp中没有此值
  110. */
  111. private static String getChannelBySharedPreferences(Context context){
  112. SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
  113. int currentVersionCode = getVersionCode(context);
  114. if(currentVersionCode == -1){
  115. //获取错误
  116. return "";
  117. }
  118. int versionCodeSaved = sp.getInt(CHANNEL_VERSION_KEY, -1);
  119. if(versionCodeSaved == -1){
  120. //本地没有存储的channel对应的版本号
  121. //第一次使用 或者 原先存储版本号异常
  122. return "";
  123. }
  124. if(currentVersionCode != versionCodeSaved){
  125. return "";
  126. }
  127. return sp.getString(CHANNEL_KEY, "");
  128. }
  129. /**
  130. * 从包信息中获取版本号
  131. * @param context
  132. * @return
  133. */
  134. private static int getVersionCode(Context context){
  135. try{
  136. return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
  137. }catch(NameNotFoundException e) {
  138. e.printStackTrace();
  139. }
  140. return -1;
  141. }
  142. }