public class App extends Application {
    private static Context context;
//    private RefWatcher watcher;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init();

/*        if (LeakCanary.isInAnalyzerProcess(this)) {
            return;
        }
        watcher = LeakCanary.install(this);*/
    }

    public static Context getContext() {
        return context;
    }

/*    public static RefWatcher getWatcher(Context context) {
        App app = (App) context.getApplicationContext();
        return app.watcher;
    }*/
} public interface IDataSource {
    void queryWeather(String woeid, RequestWeatherCallback callback);

    void queryWeather(RequestWeatherCallback callback);


    void saveWeather(List<Forecast> forecastList, ForecastInfo info);

    void loadAllCity(RequestCityCallback cityCallback);

    void saveAllCity(List<String> cityList);

    void queryCity(RequestCityResultCallback callback, String cityName);

} public interface RequestCityCallback {
    void success(List<String> cityList);

    void error();
} public interface RequestCityResultCallback {
    void success(List<City> cityList);

    void error(String message);
} public interface RequestWeatherCallback {
    void success(List<Forecast> forecasts, ForecastInfo forecastInfo);

    void error(String message);
} public class AllCityResult {
    private String id;
    private String name;
    private String en;
    private String parent1;
    private String parent2;
    private String parent3;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEn() {
        return en;
    }

    public String getParent1() {
        return parent1;
    }

    public String getParent2() {
        return parent2;
    }

    public String getParent3() {
        return parent3;
    }
} public class City implements Serializable{
    private String city_name;
    private String woeid;
    private String fullNmae;

    public City(String city_name, String woeid, String fullNmae) {
        this.city_name = city_name;
        this.woeid = woeid;
        this.fullNmae = fullNmae;
    }

    public String getCity_name() {
        return city_name;
    }

    public String getWoeid() {
        return woeid;
    }

    public String getFullNmae() {
        return fullNmae;
    }
} public class CitySearchResult {
    private Query query;

    public Query getQuery() {
        return query;
    }

    public static class Query{
        private String count;
        private String created;
        private String landg;
        private Results results;

        public Results getResults() {
            return results;
        }
    }


    public static class Results{
        private Place place;

        public Place getPlace() {
            return place;
        }
    }
    public static class Place{
        private String name;
        private String country;
        private String admin1;
        private String admin2;
        private String admin3;
        private String woeid;

        public String getName() {
            return name;
        }

        public String getCountry() {
            return country;
        }

        public String getAdmin1() {
            return admin1;
        }

        public String getAdmin2() {
            return admin2;
        }

        public String getAdmin3() {
            return admin3;
        }

        public String getWoeid() {
            return woeid;
        }
    }
} public class Forecast implements Parcelable{
    public static final Parcelable.Creator<Forecast> CREATOR = new Creator<Forecast>() {
        @Override
        public Forecast createFromParcel(Parcel source) {
            Forecast forecast = new Forecast(source.readString(), source.readString(), source.readString(),
                    source.readString(), source.readString());

            return null;
        }

        @Override
        public Forecast[] newArray(int size) {
            return new Forecast[size];
        }
    };
    private String date;
    private String high;
    private String low;
    private String text;
    private String woeid;

    public Forecast(String date, String high, String low, String text, String woeid) {
        this.date = date;
        this.high = high;
        this.low = low;
        this.text = text;
        this.woeid = woeid;
    }



    public String getDate() {
        return date;
    }

    public String getHigh() {
        return high;
    }

    public String getLow() {
        return low;
    }

    public String getText() {
        return text;
    }

    public String getWoeid() {
        return woeid;
    }

    public void setWoeid(String woeid) {
        this.woeid = woeid;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(date);
        dest.writeString(high);
        dest.writeString(low);
        dest.writeString(text);
        dest.writeString(woeid);
    }
} public class ForecastInfo implements Serializable {
    private String link;
    private String lastBuildDate;
    private String windDirection;
    private String windSpeed;
    private String date;
    private String temp;
    private String text;
    private String sunrise;
    private String sunset;
    private String woeid;

    public ForecastInfo(String link, String lastBuildDate, String windDirection, String windSpeed,
                        String date, String temp, String text, String woeid, String sunrise, String sunset) {

        this.link = link;
        this.lastBuildDate = lastBuildDate;
        this.windDirection = windDirection;
        this.windSpeed = windSpeed;
        this.date = date;
        this.temp = temp;
        this.text = text;
        this.woeid = woeid;
        this.sunrise = sunrise;
        this.sunset = sunset;
    }


    public String getLink() {
        return link;
    }

    public String getLastBuildDate() {
        return lastBuildDate;
    }

    public String getWindDirection() {
        return windDirection;
    }

    public String getWindSpeed() {
        return windSpeed;
    }

    public String getDate() {
        return date;
    }

    public String getTemp() {
        return temp;
    }

    public String getText() {

        return text;
    }

    public String getWoeid() {
        return woeid;
    }

    public String getSunset() {
        return sunset;
    }

    public String getSunrise() {
        return sunrise;
    }
} public class WeatherResult {
    Query query;

    public Query getQuery() {
        return query;
    }


    public static class Query {
        Result results;

        public Result getResults() {
            return results;
        }
    }


    public static class Result {
        Channel channel;

        public Channel getChannel() {
            return channel;
        }
    }

    public static class Channel {
        String link;
        Wind wind;
        Astronomy astronomy;
        Item item;
        String lastBuildDate;

        public String getLink() {
            return link;
        }

        public Wind getWind() {
            return wind;
        }

        public Astronomy getAstronomy() {
            return astronomy;
        }

        public Item getItem() {

            return item;
        }

        public String getLastBuildDate() {
            return lastBuildDate;
        }
    }


    public static class Wind {
        String chill;
        String direction;
        String speed;

        public String getChill() {
            return chill;
        }

        public String getDirection() {
            return direction;
        }

        public String getSpeed() {
            return speed;
        }
    }

    public static class Astronomy {
        String sunrise;
        String sunset;

        public String getSunrise() {
            return sunrise;
        }

        public String getSunset() {
            return sunset;
        }
    }

    public static class Item {
        String link;
        String pubDate;
        Condition condition;
        Forecast[] forecast;

        public String getLink() {
            return link;
        }

        public String getPubDate() {
            return pubDate;
        }

        public Condition getCondition() {
            return condition;
        }

        public Forecast[] getForecast() {
            return forecast;
        }
    }

    public static class Condition {
        String date;
        String temp;
        String text;

        public String getDate() {
            return date;
        }

        public String getTemp() {
            return temp;
        }

        public String getText() {
            return text;
        }
    }
    /*public static class Forecast{
        String date;
        String day;
        String high;
        String low;
        String text;

        public String getDate() {
            return date;
        }

        public String getDay() {
            return day;
        }

        public String getHigh() {
            return high;
        }

        public String getLow() {
            return low;
        }

        public String getText() {
            return text;
        }
    }*/

} public class PreferenceUtil {
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    public static final String PREF_HAS_LOAD_ALL_CITY = "hasLoadAllCity";
    public static final String PREF_SELECTED_CITY = "selectCity";
    public static final String PREF_WOEID = "woeid";
    public static final String PREF_CITY_SIMPLE_NAME = "selectCitySimpleName";
    public static final String PREF_AUTO_UPDATE = "auto_update";

    private PreferenceUtil() {

    }

    public static PreferenceUtil getInstance() {
        return InstanceHolder.INSTANCE;
    }


    public SharedPreferences getSharedPreferences() {
        if (sharedPreferences == null)
            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.getContext());
        return sharedPreferences;
    }

    public SharedPreferences.Editor getSharedPreferencesEditor() {
        if (editor == null) {
            editor = getSharedPreferences().edit();
        }

        return editor;
    }

    public static String getWoeid() {
        return InstanceHolder.INSTANCE.getSharedPreferences().getString(PREF_WOEID, null);
    }

    public static String getCitySimpleName() {
        return InstanceHolder.INSTANCE.getSharedPreferences().getString(PREF_CITY_SIMPLE_NAME, null);
    }

    public static boolean getCanAutoUpdate() {
        return InstanceHolder.INSTANCE.getSharedPreferences().getBoolean(PREF_AUTO_UPDATE, false);
    }


    private static final class InstanceHolder {
        static final PreferenceUtil INSTANCE = new PreferenceUtil();
    }
}
import android.util.Log;
import android.widget.Toast;
import com.kuahusg.weather.App;
import com.kuahusg.weather.R;
import com.kuahusg.weather.data.IDataSource;
import com.kuahusg.weather.data.callback.RequestCityCallback;
import com.kuahusg.weather.data.callback.RequestCityResultCallback;
import com.kuahusg.weather.data.callback.RequestWeatherCallback;
import com.kuahusg.weather.model.bean.AllCityResult;
import com.kuahusg.weather.model.bean.City;
import com.kuahusg.weather.model.bean.CitySearchResult;
import com.kuahusg.weather.model.bean.Forecast;
import com.kuahusg.weather.model.bean.ForecastInfo;
import com.kuahusg.weather.model.bean.WeatherResult;
import com.kuahusg.weather.util.PreferenceUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
package com.kuahusg.weather.data.remote;





/**
 * Created by kuahusg on 16-9-28.
 */

public class RemoteForecastDataSource implements IDataSource {
    private String queryWeatherString = "select * from weather.forecast where woeid = %s and u=\"c\"";
    private String formatMethod = "json";
    private String getAllMainCityUrl = "https://raw.githubusercontent.com/Kuanghusing/City_list/master/city-list";

    private String queryCityString = "select woeid, name, country.content," +
            "admin1.content, admin2.content, admin3.content from geo.places(1) " +
            "where text=\"%s\" and lang = \"zh-CN\"";

    @Override
    public void saveWeather(List<Forecast> forecastList, ForecastInfo info) {
        //do nothing
    }

    @Override
    public void loadAllCity(final RequestCityCallback cityCallback) {
        RetrofitManager.getWeatherService().queryAllMainCity(getAllMainCityUrl)
                .enqueue(new Callback<List<AllCityResult>>() {
                    @Override
                    public void onResponse(Call<List<AllCityResult>> call, Response<List<AllCityResult>> response) {
                        List<AllCityResult> cityResults = response.body();
                        List<String> cityList = new ArrayList<>();
                        for (AllCityResult cityResult : cityResults) {
                            cityList.add(formatCityName(cityResult.getName(), cityResult.getParent1(), cityResult.getParent2(), cityResult.getParent3()));

                        }

                        if (cityList.size() > 0) {
                            cityCallback.success(cityList);
                        } else
                            cityCallback.error();
                    }

                    @Override
                    public void onFailure(Call<List<AllCityResult>> call, Throwable t) {
                        cityCallback.error();
                    }
                });

    }

    @Override
    public void queryWeather(RequestWeatherCallback callback) {
        queryWeather(PreferenceUtil.getWoeid(), callback);
    }

    @Override
    public void queryWeather(String woeid, final RequestWeatherCallback callback) {
        final String woeid_copy;
        if (woeid == null)
            woeid_copy = PreferenceUtil.getWoeid();
        else
            woeid_copy = woeid;

        Call<WeatherResult> call = RetrofitManager.getWeatherService()