package cn.flightfeather.thirdappmodule; import android.app.Application; import android.database.sqlite.SQLiteDatabase; import android.os.Environment; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.ping.greendao.gen.DaoMaster; import com.ping.greendao.gen.DaoSession; import com.ping.greendao.gen.UserinfoDao; import org.greenrobot.greendao.query.QueryBuilder; import java.io.File; import cn.flightfeather.thirdappmodule.bean.entity.Userinfo; import cn.flightfeather.thirdappmodule.common.net.RetrofitFactory; import cn.flightfeather.thirdappmodule.util.GlobalConfig; import cn.flightfeather.thirdappmodule.util.crashreport.MyCrashHandler; import cn.flightfeather.thirdappmodule.util.notification.MyNotificationChannel; import cn.flightfeather.thirdappmodule.util.push.PushService; import cn.flightfeather.thirdappmodule.util.tbs.Tbs; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by note_ff_1602 on 2018/1/10. */ public class CommonApplication extends Application { // public final String ROOT_URL ="http://192.168.0.200:8080/"; // public final String ROOT_URL_IMAGE ="http://192.168.0.200:8080/images/"; public final String ROOT_URL ="http://192.168.0.146:8080/"; public final String ROOT_URL_IMAGE ="http://192.168.0.146:8080/images/"; public String ROOT_URL_RELEASE = "http://47.100.191.150:9005/"; public String ROOT_URL_RELEASE_IMAGE = ROOT_URL_RELEASE + "images/"; private Retrofit retrofit; private Retrofit retrofitImage; private DaoSession daoSession; private Userinfo currentUser; protected boolean released = true; //在程序打开时联网获取预置数据 private boolean loadPreDataWhenOpen = true; private static CommonApplication instance; @Override public void onCreate() { super.onCreate(); new MyCrashHandler(this).init(); //初始化网络请求 RetrofitFactory.init(this); //x5内核初始化 Tbs.Companion.init(this); //Android8.0后注册通知通道 MyNotificationChannel.Companion.init(this); //初始化移动推送 PushService.Companion.init(this); if (instance == null) { instance = this; } } public static CommonApplication getInstance() { return instance; } /** * 获取数据传输接口调用的根地址 */ public Retrofit getRetrofit(){ if (retrofit ==null){ Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") .create(); if (released){ retrofit = new Retrofit.Builder() .baseUrl(ROOT_URL_RELEASE) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); }else { retrofit = new Retrofit.Builder() .baseUrl(ROOT_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } } return retrofit; } /** * 获取图片传输接口调用的根地址 */ public Retrofit getRetrofitImage(){ if (retrofitImage ==null){ Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") .create(); if (released){ retrofitImage = new Retrofit.Builder() .baseUrl(ROOT_URL_RELEASE_IMAGE) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); }else { retrofitImage = new Retrofit.Builder() .baseUrl(ROOT_URL_IMAGE) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); } } return retrofitImage; } /** * 获取数据库Session */ public DaoSession getDaoSession() { if (daoSession ==null) { File path = new File(Environment.getExternalStorageDirectory(), "FlightFeather/third_new.db"); if(!path.exists()) path.getParentFile().mkdirs(); //创建数据库shop.db" DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getApplicationContext(), path.getAbsolutePath(), null); //获取可写数据库 SQLiteDatabase db = helper.getWritableDatabase(); //获取数据库对象 DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); QueryBuilder.LOG_SQL = true; QueryBuilder.LOG_VALUES = true; } return daoSession; } /** * 获取当前用户对象 */ public Userinfo getCurrentUser() { if (currentUser ==null){ UserinfoDao userinfoDao = this.getDaoSession().getUserinfoDao(); String userId = GlobalConfig.getInstance().getUserId(); if (userId.equals(GlobalConfig.DEFAULT_USER)) { currentUser = userinfoDao.queryBuilder().unique(); } else { currentUser = userinfoDao.queryBuilder().where(UserinfoDao.Properties.Guid.eq(userId)).unique(); } } return currentUser; } public void setCurrentUser(Userinfo currentUser) { GlobalConfig.getInstance().setUserId(currentUser.getGuid()); this.currentUser = currentUser; } /** * app启动时判断是否加载初始化数据完成 */ public boolean isLoadPreDataWhenOpen() { return loadPreDataWhenOpen; } public void setLoadPreDataWhenOpen(boolean loadPreDataWhenOpen) { this.loadPreDataWhenOpen = loadPreDataWhenOpen; } }