package cn.flightfeather.thirdapp; 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.thirdapp.bean.Userinfo; import cn.flightfeather.thirdapp.common.net.RetrofitFactory; import cn.flightfeather.thirdapp.util.crashreport.MyCrashHandler; 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 { // private final String ROOTURL="http://106.14.187.51:8801/supervision/"; private final String ROOTURL="http://192.168.0.200:8080/"; private final String ROOTURL_IMAGE="http://192.168.0.200:8080/images/"; private final String ROOTURL_REALEASE = "http://47.100.191.150:9005/"; public final String ROOTURL_REALEASE_IMAGE = "http://47.100.191.150:9005/images/"; private Retrofit retrofit; private Retrofit retrofitImage; private DaoSession daoSession; private Userinfo currentUser; private boolean debugMode = false; private boolean released = true; private UserinfoDao userinfoDao; //在程序打开时联网获取预置数据 private boolean loadPreDataWhenOpen = true; private static CommonApplication instance; @Override public void onCreate() { super.onCreate(); new MyCrashHandler(this).init(); //初始化网络请求 RetrofitFactory.init(this); if (instance == null) { instance = this; } } public static CommonApplication getInstance() { return instance; } /** * 2018.11.07 by riku 获取数据传输接口调用的根地址 * @return */ 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(ROOTURL_REALEASE) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); }else { retrofit = new Retrofit.Builder() .baseUrl(ROOTURL) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } } return retrofit; } /** * 2018.11.07 by riku 获取图片传输接口调用的根地址 * @return */ 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(ROOTURL_REALEASE_IMAGE) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); }else { retrofitImage = new Retrofit.Builder() .baseUrl(ROOTURL_IMAGE) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); } } return retrofitImage; } /** * 获取数据库Session * @return */ 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 boolean isDebugMode() { return debugMode; } /** * 2018.11.07 by riku 获取当前用户对象 * @return */ public Userinfo getCurrentUser() { if (currentUser ==null){ userinfoDao = this.getDaoSession().getUserinfoDao(); currentUser = userinfoDao.queryBuilder().unique(); } return currentUser; } public void setCurrentUser(Userinfo currentUser) { this.currentUser = currentUser; } /** * 2018.11.07 by riku app启动时判断是否加载初始化数据完成 * @return */ public boolean isLoadPreDataWhenOpen() { return loadPreDataWhenOpen; } public void setLoadPreDataWhenOpen(boolean loadPreDataWhenOpen) { this.loadPreDataWhenOpen = loadPreDataWhenOpen; } }