WMSApplication.java 3.42 KB
package com.huaheng.wms;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;

import com.huaheng.wms.https.HttpInterface;
import com.huaheng.wms.https.Subscribers.ProgressSubscriber;
import com.huaheng.wms.https.Subscribers.SubscriberOnNextListener;
import com.huaheng.wms.util.CrashHandler;
import com.huaheng.wms.util.SoundUtils;
import com.huaheng.wms.util.SpeechUtil;
import com.huaheng.wms.util.WMSUtils;
import com.huaheng.wms.wmsgreendao.DaoMaster;
import com.huaheng.wms.wmsgreendao.DaoSession;
import com.huaheng.wms.work.collectgoods.TaskBean;

import org.xutils.x;

import java.util.List;

import rx.Subscriber;


public class WMSApplication extends Application {

    private static String cookie;
    //  greenDao
    private  DaoMaster.DevOpenHelper mHelper;
    private  SQLiteDatabase db;
    private  DaoMaster mDaoMaster;
    private  DaoSession mDaoSession;
    private static Context mContext;
    private static WMSApplication instance;
    private MyHandler myHandler = new MyHandler();
    private boolean send = true;

    @Override
    public void onCreate() {
        super.onCreate();
        initData();
    }

    public static WMSApplication getInstance() {
        if(instance == null) {
            instance = new WMSApplication();
        }
        return instance;
    }

    public  void initData() {
        mContext = this;
        SoundUtils.getInstance(this);
        setupDataBase();
        CrashHandler.getInstance().init(this);

        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
        SpeechUtil.getInstance(mContext).initSpeech();
        myHandler.sendEmptyMessageDelayed(0 , 30 * 1000);
    }

    public static String getOkhttpCookie() {
        return cookie;
    }

    public static void setOkhttpCookie(String result) {
        cookie = result;
    }

    /**
     * 初始化数据库
     */
    private void setupDataBase() {
        mHelper = new DaoMaster.DevOpenHelper(this, "WMSApplication", null);
        db = mHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
        WMSLog.d("setupDataBase");
    }


    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    public static Context getContext() {
        return mContext;
    }

    private void selectTaskOutList() {
        if(send) {
            send = false;
            myHandler.sendEmptyMessageDelayed(0 , 30 * 1000);
        }
        HttpInterface.getInsstance().selectTaskOutList(new Subscriber<List<TaskBean>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                send = true;
            }

            @Override
            public void onNext(List<TaskBean> taskBeans) {
                if(taskBeans != null && taskBeans.size() > 0) {
                    SoundUtils.getInstance(mContext).dingSound();
                }
                send = true;
            }
        });
    }

    private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            selectTaskOutList();
            super.handleMessage(msg);
        }
    }
}