forked from GavinCT/AndroidMultiChannelBuildTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannelUtil.java
More file actions
227 lines (220 loc) · 6.6 KB
/
Copy pathChannelUtil.java
File metadata and controls
227 lines (220 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.czt.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.preference.PreferenceManager;
import android.text.TextUtils;
public class ChannelUtil {
//在多个地方用到时提供标识
private static final String PACKAGE_ID = "czt";//"myapp_"
//保存的安装包渠道
private static final String CHANNEL_LAST_KEY = PACKAGE_ID + "channel";
//保存的安装包版本
private static final String CHANNEL_LAST_VERSION_KEY = PACKAGE_ID + "channel_version";
//保存的安装包更新时间(SDK >= 9)
private static final String CHANNEL_LAST_UPDATE_TIME = PACKAGE_ID + "channel_last_update_time";
//初次安装渠道
private static final String CHANNEL_INSTALL_KEY = PACKAGE_ID + "channel_install";
//缓存安装包渠道
private static String sChannel;
//缓存初次安装渠道
private static String sInstallChannel;
/**
* 返回渠道号。 如果获取失败返回""
* @param context
* @return
*/
public static String getChannel(Context context){
return getChannel(context, "");
}
/**
* 返回渠道号。 如果获取失败返回defaultChannel
* @param context
* @param defaultChannel
* @return
*/
public static String getChannel(Context context, String defaultChannel) {
//内存中获取
if(!TextUtils.isEmpty(sChannel)){
return sChannel;
}
//sp中获取
sChannel = getChannelBySharedPreferences(context);
if(!TextUtils.isEmpty(sChannel)){
return sChannel;
}
//从apk中获取
sChannel = getChannelFromApk(context, CHANNEL_LAST_KEY);
if(!TextUtils.isEmpty(sChannel)){
//保存sp中备用
saveChannelBySharedPreferences(context, sChannel);
return sChannel;
}
//全部获取失败
return defaultChannel;
}
/**
* 返回初装渠道号。 如果获取失败返回""
* @param context
* @return
*/
public static String getInstallChannel(Context context){
return getInstallChannel(context, "");
}
/**
* 返回初装渠道号。 如果获取失败返回defaultChannel
* @param context
* @param defaultChannel
* @return
*/
public static String getInstallChannel(Context context, String defaultChannel) {
//内存中获取
if(!TextUtils.isEmpty(sInstallChannel)){
return sInstallChannel;
}
//sp中获取
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sInstallChannel = sp.getString(CHANNEL_INSTALL_KEY, "");
if(!TextUtils.isEmpty(sInstallChannel)){
return sInstallChannel;
}
//全部获取失败
sInstallChannel = getChannel(context, defaultChannel);
return sInstallChannel;
}
/**
* 从apk中获取渠道号
* @param context
* @param channelKey
* @return
*/
private static String getChannelFromApk(Context context, String channelKey) {
//从apk包中获取
ApplicationInfo appinfo = context.getApplicationInfo();
String sourceDir = appinfo.sourceDir;
//默认放在META-INF/里, 所以需要再拼接一下
String key = "META-INF/" + channelKey;
String channel = "";
ZipFile zipfile = null;
BufferedReader br = null;
try {
zipfile = new ZipFile(sourceDir);
ZipEntry entry = zipfile.getEntry(key);
if (entry != null) {
br = new BufferedReader(new InputStreamReader(zipfile.getInputStream(entry), "UTF-8"));
channel = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return channel;
}
/**
* 本地保存channel & 对应版本号
* @param context
* @param channel
*/
private static void saveChannelBySharedPreferences(Context context, String channel){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sp.edit();
editor.putString(CHANNEL_LAST_KEY, channel);
editor.putInt(CHANNEL_LAST_VERSION_KEY, getVersionCode(context));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
editor.putLong(CHANNEL_LAST_UPDATE_TIME, getLastUpdateTime(context));
if (TextUtils.isEmpty(sp.getString(CHANNEL_INSTALL_KEY, "")))
editor.putString(CHANNEL_INSTALL_KEY, channel);
editor.commit();
}
/**
* 从sp中获取channel(NOTE: 同版本升级渠道号会缓存, 不会被识别)
* @param context
* @return 为空表示获取异常、sp中的值已经失效、sp中没有此值
*/
private static String getChannelBySharedPreferences(Context context){
if (canUseSP(context)) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
return sp.getString(CHANNEL_LAST_KEY, "");
}
return "";
}
private static boolean canUseSP(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
//GINGERBREAD及更高版本尝试检测同VersionCode, 不同渠道间的升级
long lastUpdateTime = getLastUpdateTime(context);
if (lastUpdateTime == -1) {
//不匹配或之前没有正确的时间
return false;
}
long lastUpdateTimeSaved = sp.getLong(CHANNEL_LAST_UPDATE_TIME, -1);
if(lastUpdateTime != lastUpdateTimeSaved) {
//已
return false;
}
}
{
int currentVersionCode = getVersionCode(context);
if (currentVersionCode == -1) {
//获取错误
return false;
}
int versionCodeSaved = sp.getInt(CHANNEL_LAST_VERSION_KEY, -1);
if (currentVersionCode != versionCodeSaved) {
//不匹配或之前没有正确的版本号
return false;
}
}
return true;
}
/**
* 从包信息中获取版本号
* @param context
* @return
*/
private static int getVersionCode(Context context){
try{
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
}catch(NameNotFoundException e) {
e.printStackTrace();
}
return -1;
}
/**
* 从包信息中获取最后更新时间
* @param context
* @return
*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static long getLastUpdateTime(Context context){
try{
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).lastUpdateTime;
}catch(NameNotFoundException e) {
e.printStackTrace();
}
return -1;
}
}