-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
weiqiangliu
committed
May 13, 2022
1 parent
f1fc80e
commit b07d0c9
Showing
10 changed files
with
314 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
plugin/src/main/groovy/com/sensorsdata/analytics/android/plugin/utils/TextUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.sensorsdata.analytics.android.plugin.utils; | ||
|
||
public class TextUtil { | ||
public static boolean isEmpty(CharSequence str) { | ||
return str == null || str.length() == 0; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...com/sensorsdata/analytics/android/plugin/version/SensorsAnalyticsVersionFieldVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.sensorsdata.analytics.android.plugin.version; | ||
|
||
import com.sensorsdata.analytics.android.plugin.ClassNameAnalytics; | ||
import com.sensorsdata.analytics.android.plugin.Logger; | ||
import com.sensorsdata.analytics.android.plugin.SensorsAnalyticsTransform; | ||
import com.sensorsdata.analytics.android.plugin.SensorsAnalyticsUtil; | ||
import com.sensorsdata.analytics.android.plugin.utils.TextUtil; | ||
import com.sensorsdata.analytics.android.plugin.version.SensorsDataSDKVersionHelper; | ||
|
||
import org.objectweb.asm.FieldVisitor; | ||
|
||
public class SensorsAnalyticsVersionFieldVisitor extends FieldVisitor { | ||
private String mName, mClassName; | ||
private Object mValue; | ||
private SensorsDataSDKVersionHelper mSdkVersionHelper; | ||
private ClassNameAnalytics mClassNameAnalytics; | ||
|
||
public SensorsAnalyticsVersionFieldVisitor(int api, FieldVisitor fieldVisitor, String name, Object value, SensorsDataSDKVersionHelper sdkVersionHelper, String className, ClassNameAnalytics classNameAnalytics) { | ||
super(api, fieldVisitor); | ||
this.mName = name; | ||
this.mValue = value; | ||
this.mSdkVersionHelper = sdkVersionHelper; | ||
this.mClassName = className; | ||
this.mClassNameAnalytics = classNameAnalytics; | ||
} | ||
|
||
@Override | ||
public void visitEnd() { | ||
if (mClassNameAnalytics.getIsSensorsDataAPI()) { | ||
if ("VERSION".equals(mName)) { | ||
String version = (String) mValue; | ||
if (SensorsAnalyticsUtil.compareVersion(SensorsAnalyticsTransform.MIN_SDK_VERSION, version) > 0) { | ||
String errMessage = String.format("你目前集成的神策埋点 SDK 版本号为 v%s,请升级到 v%s 及以上的版本。详情请参考:https://github.com/sensorsdata/sa-sdk-android", version, SensorsAnalyticsTransform.MIN_SDK_VERSION); | ||
Logger.error(errMessage); | ||
throw new Error(errMessage); | ||
} | ||
String message = mSdkVersionHelper.getMessageBySDKCurrentVersion(mClassName, version); | ||
if (!TextUtil.isEmpty(message)) { | ||
throw new Error(message); | ||
} | ||
} else if ("MIN_PLUGIN_VERSION".equals(mName)) { | ||
String minPluginVersion = (String) mValue; | ||
if (!TextUtil.isEmpty(minPluginVersion)) { | ||
if (SensorsAnalyticsUtil.compareVersion(SensorsAnalyticsTransform.VERSION, minPluginVersion) < 0) { | ||
String errMessage = String.format("你目前集成的神策插件版本号为 v%s,请升级到 v%s 及以上的版本。详情请参考:https://github.com/sensorsdata/sa-sdk-android-plugin2", SensorsAnalyticsTransform.VERSION, minPluginVersion); | ||
Logger.error(errMessage); | ||
throw new Error(errMessage); | ||
} | ||
} | ||
} | ||
} else if (mClassNameAnalytics.getIsSensorsDataVersion()) { | ||
if (SensorsDataSDKVersionHelper.VERSION_KEY_CURRENT_VERSION.equals(mName)) { | ||
String version = (String) mValue; | ||
String message = mSdkVersionHelper.getMessageBySDKCurrentVersion(mClassName, version); | ||
if (!TextUtil.isEmpty(message)) { | ||
throw new Error(message); | ||
} | ||
} else if (SensorsDataSDKVersionHelper.VERSION_KEY_DEPENDENT_SDK_VERSION.equals(mName)) { | ||
String relatedOtherSDK = (String) mValue; | ||
String message = mSdkVersionHelper.getMessageBySDKRelyVersion(mClassName, relatedOtherSDK); | ||
if (!TextUtil.isEmpty(message)) { | ||
throw new Error(message); | ||
} | ||
} | ||
} | ||
super.visitEnd(); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...in/groovy/com/sensorsdata/analytics/android/plugin/version/SensorsDataSDKVersionBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.sensorsdata.analytics.android.plugin.version; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.sensorsdata.analytics.android.plugin.utils.TextUtil; | ||
|
||
|
||
public class SensorsDataSDKVersionBean { | ||
/** | ||
* 当前 SDK 要求的最小版本(必须) | ||
*/ | ||
private String mMinSensorsDataSDKVersion; | ||
/** | ||
* 其他业务 SDK 的版本信息路径,也就是 SensorDataVersionOptions 类的路径 | ||
* 通过 SensorDataVersionOptions 类的路径匹配,当前依赖 SDK 的版本和已有的 SDK 的关系(每一个 SDK 唯一,用于匹配) | ||
* (必须) | ||
*/ | ||
private String mSensorsDataSDKPath; | ||
/** | ||
* 版本不匹配时,提醒信息,可为 null | ||
*/ | ||
private String mMessage; | ||
|
||
public SensorsDataSDKVersionBean(String sensorsSDKPath, String minVersion, String message) { | ||
this.mSensorsDataSDKPath = sensorsSDKPath; | ||
this.mMinSensorsDataSDKVersion = minVersion; | ||
this.mMessage = message; | ||
} | ||
|
||
public String getSensorsDataSDKPath() { | ||
return mSensorsDataSDKPath; | ||
} | ||
|
||
public String getSensorsDataSDKVersionMessage(String version) { | ||
return !isVersionValid(version, mMinSensorsDataSDKVersion) ? String.format(TextUtil.isEmpty(mMessage) ? SensorsDataSDKVersionHelper.DEFAULT_MESSAGE : mMessage, version, mMinSensorsDataSDKVersion) : ""; | ||
} | ||
|
||
private boolean isVersionValid(String saVersion, String requiredVersion) { | ||
try { | ||
if (saVersion.equals(requiredVersion)) { | ||
return true; | ||
} else { | ||
String[] saVersions = saVersion.split("\\."); | ||
String[] requiredVersions = requiredVersion.split("\\."); | ||
for (int index = 0; index < requiredVersions.length; index++) { | ||
int saVersionsNum = Integer.parseInt(saVersions[index]); | ||
int requiredVersionsNum = Integer.parseInt(requiredVersions[index]); | ||
if (saVersionsNum != requiredVersionsNum) { | ||
return saVersionsNum > requiredVersionsNum; | ||
} | ||
} | ||
return false; | ||
} | ||
} catch (Exception ex) { | ||
// ignore | ||
return false; | ||
} | ||
} | ||
|
||
public static SensorsDataSDKVersionBean createSensorDataSDKBean(JsonObject jsonObject) { | ||
if (null != jsonObject) { | ||
try { | ||
String path = jsonObject.get("SDK_VERSION_PATH").getAsString(); | ||
String minVersion = jsonObject.get("DEPENDENT_MIN_SDK_VERSIONS").getAsString(); | ||
if (!TextUtil.isEmpty(path) && !TextUtil.isEmpty(minVersion)) { | ||
if (path.contains(".")) { | ||
path = path.replaceAll("\\.", "/"); | ||
} | ||
String message = jsonObject.get("ERROR_MESSAGE").getAsString(); | ||
return new SensorsDataSDKVersionBean(path, minVersion, message); | ||
} | ||
} catch (Exception e) { | ||
// ignore | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public String toString() { | ||
return "\tminSensorsDataSDKVersion=" + mMinSensorsDataSDKVersion + "\n" + | ||
"\tsensorsDataSDKPath=" + mSensorsDataSDKPath + "\n" + | ||
"\tmessage=" + mMessage; | ||
} | ||
|
||
} |
Oops, something went wrong.