Files
tysdk-intergration/sdk-intergration/Packages/tysdk/Plugins/Android/ChannelAdapterBase.java

84 lines
2.6 KiB
Java

package com.unity3d.player;
import android.app.Activity;
import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class ChannelAdapterBase implements SDKManager.IChannelSDKAdapter {
private static final String TAG = "ChannelAdapterBase";
protected Activity activity;
protected String channel = "GooglePlay";
final ChannelAdapterBase setup(Activity activity, String channel) {
this.activity = activity;
this.channel = channel;
return this;
}
@Override
public final SDKManager.IChannelSDKAdapter loadConfig() {
applyConfig();
return this;
}
protected void applyConfig() {
applyConfigObject(createConfig());
}
@Override
public String getChannel() {
return channel;
}
@Override
public void init() {
SDKManager.initDefault(activity);
}
protected void applyConfigObject(Object source) {
if (source == null) {
return;
}
for (Field sourceField : source.getClass().getDeclaredFields()) {
sourceField.setAccessible(true);
try {
Object value = sourceField.get(source);
if (value == null) {
continue;
}
Field targetField = ConfigManager.class.getDeclaredField(sourceField.getName());
targetField.setAccessible(true);
targetField.set(null, value);
Log.i(TAG, "apply config: " + sourceField.getName() + "=" + value);
} catch (NoSuchFieldException ignored) {
try {
Object value = sourceField.get(source);
Log.w(TAG, "ignore extra config: " + sourceField.getName() + "=" + value);
} catch (Exception e) {
Log.w(TAG, "ignore extra config: " + sourceField.getName());
}
} catch (Exception e) {
Log.e(TAG, "apply config failed: " + sourceField.getName(), e);
}
}
}
private Object createConfig() {
try {
Class<?> configClass = Class.forName(this.getClass().getName() + "$Config");
Constructor<?> constructor = configClass.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (ClassNotFoundException ignored) {
return null;
} catch (Exception e) {
Log.e(TAG, "create config failed: " + this.getClass().getName(), e);
return null;
}
}
}