public class UrlCity {

    private String status;
    /**
     * city : 南子岛
     * cnty : 中国
     * id : CN101310230
     * lat : 11.26
     * lon : 114.20
     * prov : 海南
     */

    private List<CityInfoBean> city_info;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<CityInfoBean> getCity_info() {
        return city_info;
    }

    public void setCity_info(List<CityInfoBean> city_info) {
        this.city_info = city_info;
    }

    public static class CityInfoBean {
        private String city;
        private String cnty;
        private String id;
        private String lat;
        private String lon;
        private String prov;

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCnty() {
            return cnty;
        }

        public void setCnty(String cnty) {
            this.cnty = cnty;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getLat() {
            return lat;
        }

        public void setLat(String lat) {
            this.lat = lat;
        }

        public String getLon() {
            return lon;
        }

        public void setLon(String lon) {
            this.lon = lon;
        }

        public String getProv() {
            return prov;
        }

        public void setProv(String prov) {
            this.prov = prov;
        }
    }
} public class MyApplication extends Application{

    private static MyApplication mcontext;
    public static DaoMaster daoMaster;
    public static DaoSession daoSession;
    public static LocationClient mLocationClient;


    private static final String cityUrl = "https://free-api.heweather.com/";
    private static final String heweatherKey = "93d476b872724a9681a642dce28c6523";

    public static final String shareFilename1 ="nightMode";
    public static final String shareFilename2 ="nightMode2";
    public static final String logFilename = "log";
    public static final String notificationname = "notification";
    public static final String widgetname = "widget";
    public static final String cachename = "cache";
//    public static RefWatcher mRefWatcher;
    public static boolean isNewDay = false;
    public static final boolean ENCRYPTED = false;

    public static DaoSession getDaoSession() {
        if (daoMaster == null) {
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getAppContext(), ENCRYPTED ?
                    "weather-db-encrypted" : "weather-db");
            Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
            daoMaster = new DaoMaster(db);
            daoSession = new DaoMaster(db).newSession();
        }
        return daoMaster.newSession();
    }



    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        TimeMonitorManager.getInstance().resetTimeMonitor(TimeMonitorConfig.TIME_MONITOR_ID_APPLICATION_START);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mcontext = this;
        new Thread(new Runnable() {
            @Override
            public void run() {
                NetService.getInstance().init(cityUrl);
                Logger.addLogAdapter(new AndroidLogAdapter());
            }
        }).start();

//        Stetho.initializeWithDefaults(this);
//        mRefWatcher = LeakCanary.install(mcontext);
        TimeMonitorManager.getInstance().getTimeMonitor(TimeMonitorConfig
                .TIME_MONITOR_ID_APPLICATION_START).recordingTimeTag("ApplicationCreated");

//        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getAppContext(),
//                "weather-db", null);
//        daoMaster = new DaoMaster(helper.getWritableDatabase());


//        CrashHandler crashHandler = CrashHandler.getInstance();
//        crashHandler.init(this);
//        crashHandler.sendPreviousReportsToServer();
    }

    public static Context getAppContext() {
        return mcontext;
    }

    public static String getCityUrl() {
        return cityUrl;
    }

    public static String getHeweatherKey() {
        return heweatherKey;
    }


    public static boolean nightMode(){
        SharedPreferences share = mcontext.getSharedPreferences(shareFilename1, MODE_PRIVATE);
        boolean ischecked=share.getBoolean("ischecked",false);
        return ischecked;
    }

    public static boolean nightMode2(){
        SharedPreferences share = mcontext.getSharedPreferences(shareFilename2, MODE_PRIVATE);
        boolean ischecked = share.getBoolean("ischecked",false);
        return ischecked;
    }


    public static boolean log(){
        SharedPreferences share = mcontext.getSharedPreferences(logFilename, MODE_PRIVATE);
        boolean ischecked = share.getBoolean("ischecked",true);
        return ischecked;
    }


    public static boolean notification(){
        SharedPreferences share = mcontext.getSharedPreferences(notificationname, MODE_PRIVATE);
        boolean ischecked = share.getBoolean("ischecked",true);
        return ischecked;
    }

    public static boolean widget(){
        SharedPreferences share = mcontext.getSharedPreferences(widgetname, MODE_PRIVATE);
        boolean ischecked = share.getBoolean("ischecked",true);
        return ischecked;
    }

    public static boolean cache(){
        SharedPreferences share = mcontext.getSharedPreferences(cachename, MODE_PRIVATE);
        boolean ischecked = share.getBoolean("ischecked",true);
        return ischecked;
    }

    public static LocationClient getmLocationClient() {
        if (mLocationClient == null) {
            mLocationClient = new LocationClient(mcontext);

        }
        return mLocationClient;
    }

    public static boolean isNewDay() {
        return isNewDay;
    }
} public class HWeather implements Parcelable,Serializable {


    private List<Weather> HeWeather5;

    public List<Weather> getHeWeather5() {
        return HeWeather5;
    }

    public void setHeWeather5(List<Weather> HeWeather5) {
        this.HeWeather5 = HeWeather5;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(this.HeWeather5);
    }

    public HWeather() {
    }

    protected HWeather(Parcel in) {
        this.HeWeather5 = new ArrayList<Weather>();
        in.readList(this.HeWeather5, Weather.class.getClassLoader());
    }

    public static final Creator<HWeather> CREATOR = new Creator<HWeather>() {
        @Override
        public HWeather createFromParcel(Parcel source) {
            return new HWeather(source);
        }

        @Override
        public HWeather[] newArray(int size) {
            return new HWeather[size];
        }
    };
} public interface CityDataSource {

    /******************************************************************************************
     * 对CityDao进行的操作
     *******************************************************************************************/
    void addCities(UrlCity city);

    void addCity(CityEntity city);

    boolean isExistInCity();

    void getCityFromProvince(String provinceName, GetCityCallBack callBack);

    void getAllCity(GetCityCallBack callBack);

    void getCities(String name, GetCityCallBack callBack);

    boolean isExistInCity(String name);

    List<CityEntity> getAllCities();

    List<CityEntity> getCities(String name);


    /******************************************************************************************
     * 对ProvinceDao进行的操作
     *******************************************************************************************/

    List<ProvinceEntity> getProvinces(String name);

    void getAllProvince(GetProvinceCallBack callBack);

    void addProvince();

    void addProvince(ProvinceEntity province);

    boolean isExistInProvince();

    void getProvince(String name, GetProvinceCallBack callBack);

    List<ProvinceEntity> getAllProvince();


    /******************************************************************************************
     *  对LoveCityDao进行的操作
     *******************************************************************************************/


    List<LoveCityEntity> getAllLoveCities();

    void getLoveCity(String cityName, GetLoveCityCallBack callBack);

    void getLoveCity(int order, GetLoveCityCallBack callBack);

    LoveCityEntity getLoveCity(int order);

    void getLoveCity(GetLoveCityCallBack callBack);

    void addLoveCity(LoveCityEntity loveCity);

    boolean isExistInLoveCity(String cityName);

    void updateCityOrder(String cityName, int order);

    void updateLocaitonCityOrder(String cityName,int order);

    void deleteCity(String cityName);

    interface GetLoveCityCallBack {
        void onSuccess(List<LoveCityEntity> loveCities);

        void onFailure(String failureMessage);

    }

    interface GetProvinceCallBack {
        void onSuccess(List<ProvinceEntity> provinces);

        void onFailure(String failureMessage);
    }


    interface GetCityCallBack {
        void onSuccess(List<CityEntity> cities);

        void onFailure(String failureMessage);
    }


} public interface WeatherDataSource {

    interface GetWeatherCallBack{

        void onSuccess(HWeather weather);

        void onFailure(String failureMessage);
    }

    interface GetWeatherEntityCallBack{

        void onSuccess(WeatherEntity entity);

        void onFailure(String failureMessage);
    }


    String getShowCity();

    void addWeather(WeatherEntity entity);

    boolean isExistInCityWeather(String cityName);

    void getWeatherDataFromCity(String cityName, GetWeatherCallBack callBack) throws Exception;

    void getWeatherEntity(String cityName,GetWeatherEntityCallBack callBack);


    WeatherEntity getWeatherEntity(String cityName);

    void updateCityWeather(String cityName) throws Exception;

    HWeather getLocalWeather(String cityName);

    void saveWeather(HWeather weather);

    HWeather getWeatherDataFromCity(String cityName) throws Exception;
} public class CityEntityDao extends AbstractDao<CityEntity, Long> {

    public static final String TABLENAME = "CITY_ENTITY";

    /**
     * Properties of entity CityEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property CityName = new Property(1, String.class, "cityName", false, "CITY_NAME");
        public final static Property ProvinceName = new Property(2, String.class, "provinceName", false, "PROVINCE_NAME");
    }


    public CityEntityDao(DaoConfig config) {
        super(config);
    }
    
    public CityEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"CITY_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"CITY_NAME\" TEXT," + // 1: cityName
                "\"PROVINCE_NAME\" TEXT);"); // 2: provinceName
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CITY_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, CityEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String cityName = entity.getCityName();
        if (cityName != null) {
            stmt.bindString(2, cityName);
        }
 
        String provinceName = entity.getProvinceName();
        if (provinceName != null) {
            stmt.bindString(3, provinceName);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, CityEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String cityName = entity.getCityName();
        if (cityName != null) {
            stmt.bindString(2, cityName);
        }
 
        String provinceName = entity.getProvinceName();
        if (provinceName != null) {
            stmt.bindString(3, provinceName);
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public CityEntity readEntity(Cursor cursor, int offset) {
        CityEntity entity = new CityEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // cityName
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) // provinceName
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, CityEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setCityName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setProvinceName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(CityEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(CityEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(CityEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
} public class LoveCityEntityDao extends AbstractDao<LoveCityEntity, Long> {

    public static final String TABLENAME = "LOVE_CITY_ENTITY";

    /**
     * Properties of entity LoveCityEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property CityName = new Property(1, String.class, "cityName", false, "CITY_NAME");
        public final static Property Order = new Property(2, int.class, "order", false, "ORDER");
    }


    public LoveCityEntityDao(DaoConfig config) {
        super(config);
    }
    
    public LoveCityEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"LOVE_CITY_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"CITY_NAME\" TEXT," + // 1: cityName
                "\"ORDER\" INTEGER NOT NULL );"); // 2: order
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"LOVE_CITY_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, LoveCityEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String cityName = entity.getCityName();
        if (cityName != null) {
            stmt.bindString(2, cityName);
        }
        stmt.bindLong(3, entity.getOrder());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, LoveCityEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String cityName = entity.getCityName();
        if (cityName != null) {
            stmt.bindString(2, cityName);
        }
        stmt.bindLong(3, entity.getOrder());
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public LoveCityEntity readEntity(Cursor cursor, int offset) {
        LoveCityEntity entity = new LoveCityEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // cityName
            cursor.getInt(offset + 2) // order
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, LoveCityEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setCityName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setOrder(cursor.getInt(offset + 2));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(LoveCityEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(LoveCityEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(LoveCityEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
} public class ProvinceEntityDao extends AbstractDao<ProvinceEntity, Long> {

    public static final String TABLENAME = "PROVINCE_ENTITY";

    /**
     * Properties of entity ProvinceEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property ProvinceName = new Property(1, String.class, "provinceName", false, "PROVINCE_NAME");
    }


    public ProvinceEntityDao(DaoConfig config) {
        super(config);
    }
    
    public ProvinceEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"PROVINCE_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"PROVINCE_NAME\" TEXT);"); // 1: provinceName
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"PROVINCE_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, ProvinceEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String provinceName = entity.getProvinceName();
        if (provinceName != null) {
            stmt.bindString(2, provinceName);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, ProvinceEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String provinceName = entity.getProvinceName();
        if (provinceName != null) {
            stmt.bindString(2, provinceName);
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public ProvinceEntity readEntity(Cursor cursor, int offset) {
        ProvinceEntity entity = new ProvinceEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1) // provinceName
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, ProvinceEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setProvinceName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(ProvinceEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(ProvinceEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(ProvinceEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
} public class WeatherEntityDao extends AbstractDao<WeatherEntity, Long> {

    public static final String TABLENAME = "WEATHER_ENTITY";

    /**
     * Properties of entity WeatherEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property CityName = new Property(1, String.class, "cityName", false, "CITY_NAME");
        public final static Property Weather = new Property(2, byte[].class, "weather", false, "WEATHER");
        public final static Property UpdateTime = new Property(3, java.util.Date.class, "updateTime", false, "UPDATE_TIME");
    }


    public WeatherEntityDao(DaoConfig config) {
        super(config);
    }
    
    public WeatherEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"WEATHER_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"CITY_NAME\" TEXT," + // 1: cityName
                "\"WEATHER\" BLOB," + // 2: weather
                "\"UPDATE_TIME\" INTEGER);"); // 3: updateTime
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"WEATHER_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, WeatherEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String cityName = entity.getCityName();
        if (cityName != null) {
            stmt.bindString(2, cityName);
        }
 
        byte[] weather = entity.getWeather();
        if (weather != null) {
            stmt.bindBlob(3, weather);
        }
 
        java.util.Date updateTime = entity.getUpdateTime();
        if (updateTime != null) {
            stmt.bindLong(4, updateTime.getTime());
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, WeatherEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String cityName = entity.getCityName();
        if (cityName != null) {
            stmt.bindString(2, cityName);
        }
 
        byte[] weather = entity.getWeather();
        if (weather != null) {
            stmt.bindBlob(3, weather);
        }
 
        java.util.Date updateTime = entity.getUpdateTime();
        if (updateTime != null) {
            stmt.bindLong(4, updateTime.getTime());
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public WeatherEntity readEntity(Cursor cursor, int offset) {
        WeatherEntity entity = new WeatherEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // cityName
            cursor.isNull(offset + 2) ? null : cursor.getBlob(offset + 2), // weather
            cursor.isNull(offset + 3) ? null : new java.util.Date(cursor.getLong(offset + 3)) // updateTime
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, WeatherEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setCityName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setWeather(cursor.isNull(offset + 2) ? null : cursor.getBlob(offset + 2));
        entity.setUpdateTime(cursor.isNull(offset + 3) ? null : new java.util.Date(cursor.getLong(offset + 3)));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(WeatherEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(WeatherEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(WeatherEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
} @Entity
public class CityEntity{

    @Id(autoincrement = true)
    private Long id;

    private String cityName;

    private String provinceName;

    @Generated(hash = 1398242605)
    public CityEntity(Long id, String cityName, String provinceName) {
        this.id = id;
        this.cityName = cityName;
        this.provinceName = provinceName;
    }

    @Generated(hash = 2001321047)
    public CityEntity() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCityName() {
        return this.cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getProvinceName() {
        return this.provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

} @Entity
public class LoveCityEntity {

    @Id(autoincrement = true)
    private Long id;
    private String cityName;
    private int order;

    @Generated(hash = 1968781902)
    public LoveCityEntity(Long id, String cityName, int order) {
        this.id = id;
        this.cityName = cityName;
        this.order = order;
    }

    @Generated(hash = 2010273138)
    public LoveCityEntity() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCityName() {
        return this.cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public int getOrder() {
        return this.order;
    }

    public void setOrder(int order) {
        this.order = order;
    }


} @Entity
public class ProvinceEntity {

    @Id(autoincrement = true)
    private Long id;

    private String provinceName;

    @Generated(hash = 1618707584)
    public ProvinceEntity(Long id, String provinceName) {
        this.id = id;
        this.provinceName = provinceName;
    }

    @Generated(hash = 1419486807)
    public ProvinceEntity() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getProvinceName() {
        return this.provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }


} @Entity
public class WeatherEntity {
    @Id(autoincrement = true)
    private Long id;

    private String cityName;
    /**
     * HWeather对象转成二进制流
     */
    private byte[] weather;
    private Date updateTime;

    @Generated(hash = 1331303326)
    public WeatherEntity(Long id, String cityName, byte[] weather,
                         Date updateTime) {
        this.id = id;
        this.cityName = cityName;
        this.weather = weather;
        this.updateTime = updateTime;
    }

    @Generated(hash = 1598697471)
    public WeatherEntity() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCityName() {
        return this.cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public byte[] getWeather() {
        return this.weather;
    }

    public void setWeather(byte[] weather) {
        this.weather = weather;
    }

    public Date getUpdateTime() {
        return this.updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
} public class WeatherJsonConverter {

    public static Weather getWeather(HWeather hWeather){
        return hWeather.getHeWeather5().get(0);
    }
} public static <T> T checkNotNull(T ref) {
    if (ref == null) {
        throw new NullPointerException();
    }else{
        return ref;
    }
} public static Date convertDate(Date date){
    try {
        return simpleDateFormat.parse(simpleDateFormat.format(date));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
} public static <T>  T convertObject(byte[] bytes, Class<T> clz) {
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        Object object = new ObjectInputStream(bais).readObject();
        T t = clz.cast(object);
        return t;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
} public static boolean isListEmpty(List<?> list) {
    if (list == null) {
        return true;
    }
    if (list.size() == 0) {
        return true;
    }

    return false;
}
import android.util.Log;
import com.orhanobut.logger.Logger;
import com.weather.byhieg.easyweather.data.bean.UrlCity;
import com.weather.byhieg.easyweather.MyApplication;
import com.weather.byhieg.easyweather.data.bean.HWeather;
import com.weather.byhieg.easyweather.data.source.CityDataSource;
import com.weather.byhieg.easyweather.data.source.WeatherDataSource;
import com.weather.byhieg.easyweather.data.source.local.dao.CityEntityDao;
import com.weather.byhieg.easyweather.data.source.local.dao.LoveCityEntityDao;
import com.weather.byhieg.easyweather.data.source.local.dao.ProvinceEntityDao;
import com.weather.byhieg.easyweather.data.source.local.dao.WeatherEntityDao;
import com.weather.byhieg.easyweather.data.source.local.entity.CityEntity;
import com.weather.byhieg.easyweather.data.source.local.entity.LoveCityEntity;
import com.weather.byhieg.easyweather.data.source.local.entity.ProvinceEntity;
import com.weather.byhieg.easyweather.data.source.local.entity.WeatherEntity;
import com.weather.byhieg.easyweather.tools.WeatherJsonConverter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import static com.weather.byhieg.easyweather.tools.Knife.checkNotNull;
import static com.weather.byhieg.easyweather.tools.Knife.convertDate;
import static com.weather.byhieg.easyweather.tools.Knife.convertObject;
import static com.weather.byhieg.easyweather.tools.Knife.isListEmpty;
package com.weather.byhieg.easyweather.data.source.local;






/**
 * Created by byhieg on 17/5/21.
 * Contact with byhieg@gmail.com
 */

public class WeatherLocalDataSource implements WeatherDataSource, CityDataSource {

    private final static String TAG = "WeatherLocalDataSource";
    private ProvinceEntityDao mProvinceDao;
    private CityEntityDao mCityDao;
    private LoveCityEntityDao mLoveCityDao;
    private WeatherEntityDao mWeatherDao;

    public WeatherLocalDataSource() {
        mProvinceDao = MyApplication.getDaoSession().getProvinceEntityDao();
        mCityDao = MyApplication.getDaoSession().getCityEntityDao();
        mLoveCityDao = MyApplication.getDaoSession().getLoveCityEntityDao();
        mWeatherDao = MyApplication.getDaoSession().getWeatherEntityDao();
    }


    @Override
    public String getShowCity() {
        List<LoveCityEntity> entity = mLoveCityDao.queryBuilder().
                where(LoveCityEntityDao.Properties.Order.eq(1)).
                list();
        return entity.get(0).getCityName();
    }

    @Override
    public void addWeather(WeatherEntity entity) {
        mWeatherDao.insert(entity);
    }

    @Override
    public boolean isExistInCityWeather(String cityName) {
        List<WeatherEntity> res = mWeatherDao.queryBuilder().
                where(WeatherEntityDao.Properties.CityName.eq(cityName)).
                limit(1).list();
        return !isListEmpty(res);
    }

    @Override
    public void getWeatherDataFromCity(String cityName, GetWeatherCallBack callBack) throws Exception {
        List<WeatherEntity> res = mWeatherDao.
                queryBuilder().
                where(WeatherEntityDao.Properties.CityName.eq(cityName)).list();
        if (isListEmpty(res)) {
            callBack.onFailure("没有该城市的天气");
        } else {
            byte[] bytes = res.get(res.size() - 1).getWeather();
            callBack.onSuccess(convertObject(bytes, HWeather.class));
        }
    }

    @Override
    public void getWeatherEntity(String cityName, GetWeatherEntityCallBack callBack) {
        List<WeatherEntity> res = mWeatherDao.queryBuilder().
                where(WeatherEntityDao.Properties.CityName.eq(cityName)).list();
        if (isListEmpty(res)) {
            callBack.onFailure("没有该城市的天气数据");
        } else {
            callBack.onSuccess(res.get(res.size() - 1));
        }
    }

    @Override
    public WeatherEntity getWeatherEntity(String cityName) {
        List<WeatherEntity> res = mWeatherDao.queryBuilder().
                where(WeatherEntityDao.Properties.CityName.eq(cityName)).list();
        if (!isListEmpty(res)) {
            return res.get(res.size() - 1);
        } else {
            return null;
        }
    }

    /**
     * 该方法由repository实现
     *
     * @param cityName
     * @throws Exception
     */
    @Override
    public void updateCityWeather(String cityName) throws Exception {

    }

    @Override
    public HWeather getLocalWeather(String cityName) {
        WeatherEntity entity = getWeatherEntity(cityName);
        if (entity == null) {
            return null;
        } else {
            return convertObject(entity.getWeather(), HWeather.class);
        }
    }


    @Override
    public void saveWeather(HWeather weather) {
        WeatherEntity entity = new WeatherEntity();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            Date date = simpleDateFormat.parse(WeatherJsonConverter.getWeather(weather).getBasic().getUpdate().getLoc());
            entity.setCityName(WeatherJsonConverter.getWeather(weather).getBasic().getCity());
            entity.setUpdateTime(date);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(weather);
            objectOutputStream.flush();
            byte[] bytes = byteArrayOutputStream.toByteArray();
            entity.setWeather(bytes);
            addWeather(entity);
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 该方法由remoteDataSource实现
     *
     * @param cityName
     * @return
     * @throws Exception
     */
    @Override
    public HWeather getWeatherDataFromCity(String cityName) {
        return null;
    }

    @Override
    public void addCities(UrlCity city) {