riku
2020-10-10 5771916ffb24807dd57c1969baa6371611c334da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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/";
    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_RELEASE = "http://47.100.191.150:9005/";
    public final String ROOT_URL_RELEASE_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(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;
    }
 
    /**
     * 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(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
     * @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;
    }
 
}