package com.xunao.effectdemo;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.uc.crashsdk.export.CrashApi;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.xunao.effectdemo.umeng.UmInitConfig;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

import cn.jpush.android.api.JPushInterface;

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/315:31
 * desc :
 * version: 1.0
 */
public class App extends Application {

	public static App app;

	public static App getInstance() {
		return app;
	}
	@Override
	public void onCreate() {
		super.onCreate();
		initUmengSDK();
		app = this;
		JPushInterface.setDebugMode(true);
		JPushInterface.init(this);
//		getSHA1Signature(this);

	}

	void initLebo(){
//		LelinkSourceSDK.getInstance()
//				//FIXME WARN: 这里替换为您申请的AppID & AppSecret，build.gradle替换为您的应用包名
//				.setSdkInitInfo(this, getString(R.string.app_id), getString(R.string.app_secret))
//				.easyPush(mBrowseContainer);
	}

	/**
	 * 初始化友盟SDK
	 */
	private void initUmengSDK() {
//		sharedPreferencesHelper=new SharedPreferencesHelper(this,"umeng");

		//设置LOG开关，默认为false
		UMConfigure.setLogEnabled(true);

		//友盟预初始化
		UMConfigure.preInit(getApplicationContext(),"6304343088ccdf4b7e0d67bb","Umeng");

		/**
		 * 打开app首次隐私协议授权，以及sdk初始化，判断逻辑请查看SplashTestActivity
		 */
		//判断是否同意隐私协议，uminit为1时为已经同意，直接初始化umsdk
		//友盟正式初始化
		UmInitConfig umInitConfig=new UmInitConfig();
		umInitConfig.UMinit(getApplicationContext());

		final Bundle customInfo = new Bundle();
		customInfo.putBoolean("mCallNativeDefaultHandler",true);
		CrashApi.getInstance().updateCustomInfo(customInfo);

		// 自动采集选择
		MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);

		// 支持在子进程中统计自定义事件
		UMConfigure.setProcessEvent(true);
	}

	public void getSHA1Signature(Context context) {
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);

			byte[] cert = info.signatures[0].toByteArray();

			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] publicKey = md.digest(cert);
			StringBuilder hexString = new StringBuilder();
			for (int i = 0; i < publicKey.length; i++) {
				String appendString = Integer.toHexString(0xFF & publicKey[i])
						.toUpperCase(Locale.US);
				if (appendString.length() == 1)
					hexString.append("0");
				appendString.replaceAll(":","");
				hexString.append(appendString);
				hexString.append(":");
			}
			Log.e("MyTag","签名:"+hexString.toString());
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}


	/**
	 * 将给定字符串中给定的区域的字符转换成小写
	 *
	 * @param str 给定字符串中
	 * @param beginIndex 开始索引（包括）
	 * @param endIndex 结束索引（不包括）
	 * @return 新的字符串
	 */
	public static String toLowerCase(String str, int beginIndex, int endIndex) {
		return str.replaceFirst(str.substring(beginIndex, endIndex),
				str.substring(beginIndex, endIndex)
						.toLowerCase(Locale.getDefault()));
	}
}
