Skip to content

Instantly share code, notes, and snippets.

@xxnjdlys
Created March 9, 2017 10:58
Show Gist options
  • Save xxnjdlys/c08865f23c82b335956a6904f7133f24 to your computer and use it in GitHub Desktop.
Save xxnjdlys/c08865f23c82b335956a6904f7133f24 to your computer and use it in GitHub Desktop.
package com.wukongtv.wkremote.client;
import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Build;
import android.support.multidex.MultiDex;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.tinker.loader.app.DefaultApplicationLike;
import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.wukongtv.http.WKAsyncHttpClient;
import com.wukongtv.remotelogger.RL;
import com.wukongtv.wkremote.client.NetWork.UdpManager;
import com.wukongtv.wkremote.client.Util.FeedbackUtil;
import com.wukongtv.wkremote.client.Util.Miscellaneous;
import com.wukongtv.wkremote.client.Util.OmniStorage;
import com.wukongtv.wkremote.client.Util.RedPointUtils;
import com.wukongtv.wkremote.client.Util.WkFIFOLimitedMemoryCache;
import com.wukongtv.wkremote.client.ad.ADConfigManager;
import com.wukongtv.wkremote.client.ad.ADUtil;
import com.wukongtv.wkremote.client.device.DeviceManager;
import com.wukongtv.wkremote.client.http.AppConfigManager;
import com.wukongtv.wkremote.client.http.HostManager;
import com.wukongtv.wkremote.client.http.SmartImageDownloader;
import com.wukongtv.wkremote.client.skin.DelegateResources;
import com.wukongtv.wkremote.client.skin.SkinManager;
import com.wukongtv.wkremote.client.skin.control.ThemeUiInterface;
import com.wukongtv.wkremote.client.update.UpdateManager;
import com.wukongtv.wkremote.client.video.control.VideoHeartbeatHelper;
import com.wukongtv.wkremote.client.video.pay.CibnPayHelper;
import com.wukongtv.wkutil.ChannelUtil;
import com.xiaomi.mipush.sdk.MiPushClient;
import java.io.File;
import timber.log.Timber;
/**
* MyApplication
* Created by zhangge on 14-1-10.
*/
public class WKSugarAppLike extends DefaultApplicationLike {
public static final String BUGLY_APP_ID = UpdateManager.BUGLY_APP_ID;
public OmniStorage mStorage;
private static final String IFLYTEK_ID = "544f5aac";
private SkinManager mSkinManager;
private static WKSugarAppLike sInstance;
public WKSugarAppLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag,
long applicationStartElapsedTime, long applicationStartMillisTime,
Intent tinkerResultIntent, Resources[] resources, ClassLoader[] classLoader,
AssetManager[] assetManager) {
super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime,
applicationStartMillisTime, tinkerResultIntent, resources, classLoader,
assetManager);
}
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
String processName = Miscellaneous.getProcessName(getApplication(), android.os.Process.myPid());
if (!TextUtils.isEmpty(processName) && processName.equals(getApplication().getPackageName())) {
initChannel();
preloadClass();
VideoHeartbeatHelper.getInstance().init(getApplication());
RedPointUtils.init(getApplication());
mStorage = new OmniStorage(getApplicationContext());
mStorage.init();
SpeechUtility.createUtility(getApplication(), SpeechConstant.APPID + "=" + IFLYTEK_ID + "," + SpeechConstant.ENGINE_MODE + "=" + "msc");
/**
* issues from v1.2.8.2: Caused by:java.lang.IllegalArgumentException:
* cacheDir argument mustbe not null
*
* https://github.com/nostra13/Android-Universal-Image-Loader/issues/392
* https://github.com/nostra13/Android-Universal-Image-Loader/issues/255
*/
File cacheDir = mStorage.getCacheDir();
DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.resetViewBeforeLoading(true)
.build();
ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(getApplicationContext())
.threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY - 1)
.defaultDisplayImageOptions(displayOptions)
.diskCacheSize(1024 * 1024 * 100)
.memoryCache(new WkFIFOLimitedMemoryCache(4 * 1024 * 1024))
.imageDownloader(new SmartImageDownloader(getApplicationContext()));
ImageLoaderConfiguration config = builder.build();
ImageLoader.getInstance().init(config);
WKAsyncHttpClient.getInstance().init(cacheDir);
HostManager.getInstance().init(getApplication());
// make log system ready
if (BuildConfig.DEBUG || BuildConfig.RELEASE_TIMBER_LOG) {
Timber.plant(new Timber.DebugTree());
} else {
Timber.plant(new Timber.HollowTree());
}
mSkinManager = new SkinManager(getApplication());
if (!mSkinManager.isUsingDefaultSkin()) {
setResources( mSkinManager.getResources(getApplication()));
}
//instance UdpManager
UdpManager.getInstance();
DeviceManager.getInstance().init(getApplication());
RL.init(getApplication(), "http://wk199.wukongtv.com/application/debuglog", false);
AppConfigManager.getInstance().init(getApplication());
ADConfigManager.init(getApplication());
WKProtocolClassLoader.getInstance().init(getApplication());
MobclickAgent.setCatchUncaughtExceptions(true);
ADUtil.initWkADImpl();
ADUtil.init(getApplication());
FeedbackUtil.getsInstance(getApplication()).init();
CibnPayHelper.getInstance().init(getApplication());
MiPushClient.registerPush(getApplication(), getApplication().getResources().getString(R.string.xiaomi_app_id), getApplication().getResources().getString(R.string.xiaomi_app_key));
}
/**
* 上边括号内的逻辑防重入!
* 确定不写在上边的括号内?
*/
}
public Context getApplicationContext() {
return getApplication().getApplicationContext();
}
public static WKSugarAppLike getInstance() {
return sInstance;
}
public boolean isUsingDefaultSkin() {
return mSkinManager == null || mSkinManager.isUsingDefaultSkin();
}
public void setTheme(Context context) {
if (mSkinManager != null) {
injectResources();
mSkinManager.setTheme(context);
}
}
/**
* @return 返回值为是否需要换肤
*/
public boolean changeTheme(Context context, String destTheme) {
if (mSkinManager != null) {
if (equalsTheme(context, destTheme)) {
return false;
} else {
mSkinManager.changeTheme(getApplication(), destTheme);
return true;
}
}
return false;
}
public void changeTheme(View rootView) {
if (rootView == null) {
return;
}
//這裡邏輯很簡單 就是递归调用changeTheme-----递归调用setTheme了。
//这里为了简单 我就不写那么复杂了,就这一个逻辑:先set自己的theme 然后遍历自己的子控件 逐一set
if (rootView instanceof ThemeUiInterface) {
((ThemeUiInterface) rootView).changeTheme(getApplication());
if (rootView instanceof ViewGroup) {
int count = ((ViewGroup) rootView).getChildCount();
for (int i = 0; i < count; i++) {
changeTheme(((ViewGroup) rootView).getChildAt(i));
}
}
} else if (rootView instanceof ViewGroup) {
int count = ((ViewGroup) rootView).getChildCount();
for (int i = 0; i < count; i++) {
changeTheme(((ViewGroup) rootView).getChildAt(i));
}
}
}
public String getCurrentTheme(Context context) {
if (mSkinManager != null) {
return mSkinManager.getCurrentTheme(context);
}
return Preferences.THEME_DEFAULT;
}
public String getCurrentThemeName() {
switch (getCurrentTheme(getApplication())) {
case Preferences.THEME_BLUE:
return getApplication().getString(R.string.theme_blue);
case Preferences.THEME_GREEN:
return getApplication().getString(R.string.theme_green);
case Preferences.THEME_PURPLE:
return getApplication().getString(R.string.theme_purple);
case Preferences.THEME_RED:
return getApplication().getString(R.string.theme_red);
case Preferences.THEME_ORANGE:
return getApplication().getString(R.string.theme_orange);
case Preferences.THEME_PORCELAIN:
return getApplication().getString(R.string.theme_porcelain);
case Preferences.THEME_LANDSCAPE:
return getApplication().getString(R.string.theme_landscape);
case Preferences.THEME_BLACK:
return getApplication().getString(R.string.theme_black);
default:
return getApplication().getString(R.string.theme_default);
}
}
public void injectResources(){
if (!(getApplication().getResources() instanceof DelegateResources) && mSkinManager != null){
mSkinManager.init(getApplication() ,true);
}
}
public boolean equalsTheme(Context context, String value) {
if (TextUtils.isEmpty(value)) {
return false;
}
return mSkinManager != null && mSkinManager.getCurrentTheme(context).equals(value);
}
@Override
public void onBaseContextAttached(Context base) {
super.onBaseContextAttached(base);
// you must install multiDex whatever tinker is installed!
MultiDex.install(base);
// TODO: 安装tinker
// if (Build.VERSION.SDK_INT >= 14) {
// FIXME: 12/19/16 这里先尝试在以下使用tinker,找机器测试下结果.
Beta.installTinker(this);
Log.v("SADIEYU", "Beta.installTinker");
// }
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) {
getApplication().registerActivityLifecycleCallbacks(callbacks);
}
private void preloadClass() {
try {
// 解决 java.lang.NoClassDefFoundError: android/os/AsyncTask
// 在ConfigClient中会用到
// 参考http://stackoverflow.com/questions/6968744/getting-noclassdeffounderror-android-os-asynctask
Class.forName("android.os.AsyncTask");
} catch (Throwable ignore) {
}
}
private void initChannel() {
String channel = ChannelUtil.getChannel(getApplication());
AnalyticsConfig.setChannel(channel);
UmengUpdateAgent.setChannel(channel);
if (BuildConfig.DEBUG) {
Timber.v("Pid == " + channel);
}
CrashReport.UserStrategy us = new CrashReport.UserStrategy(getApplication().getApplicationContext());
us.setAppChannel(channel);
UpdateManager.getInstance().initBuglyUpdateStrategy();
//标识为开发设备
Bugly.setIsDevelopmentDevice(getApplicationContext(), true);
// 这里实现SDK初始化,appId替换成你的在Bugly平台申请的appId
Bugly.init(getApplicationContext(), BUGLY_APP_ID, true, us);
}
public boolean isDebugMode() {
return Preferences.isDebugMode(getApplication());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment