diff --git a/app/app.iml b/app/app.iml index 7f0efb4..dc954d6 100644 --- a/app/app.iml +++ b/app/app.iml @@ -65,14 +65,6 @@ - - - - - - - - @@ -81,9 +73,16 @@ + + + + + + + + - @@ -107,7 +106,8 @@ - + + @@ -140,8 +140,9 @@ + - + diff --git a/app/build.gradle b/app/build.gradle index 44bd2f4..7c6e523 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,6 +43,7 @@ android { exclude 'META-INF/services/javax.annotation.processing.Processor' } + } retrolambda { @@ -72,7 +73,8 @@ dependencies { compile 'com.jakewharton:butterknife:7.0.1' compile 'jp.wasabeef:recyclerview-animators:2.2.3' - compile 'com.tencent.bugly:crashreport:latest.release' + compile 'com.tencent.bugly:crashreport:latest.release' //其中latest.release指代最新版本号,也可以指定明确的版本号,例如2.1.5 + compile 'com.tencent.bugly:nativecrashreport:latest.release' //其中latest.release指代最新版本号,也可以指定明确的版本号,例如2.2.0 debugCompile 'com.github.moduth:blockcanary-android:1.2.1' releaseCompile 'com.github.moduth:blockcanary-no-op:1.2.1' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1f8b3ad..521f3c6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -65,6 +65,8 @@ android:screenOrientation="portrait" android:theme="@style/AppTheme.transpant"/> + + diff --git a/app/src/main/java/com/xiecc/seeWeather/base/BaseActivity.java b/app/src/main/java/com/xiecc/seeWeather/base/BaseActivity.java index 8e0103c..0ec6cf5 100644 --- a/app/src/main/java/com/xiecc/seeWeather/base/BaseActivity.java +++ b/app/src/main/java/com/xiecc/seeWeather/base/BaseActivity.java @@ -10,6 +10,7 @@ import android.view.WindowManager; import com.readystatesoftware.systembartint.SystemBarTintManager; import com.xiecc.seeWeather.common.ACache; import com.xiecc.seeWeather.modules.setting.Setting; +import rx.subscriptions.CompositeSubscription; /** * Created by hugo on 2015/12/16. @@ -19,6 +20,8 @@ public class BaseActivity extends AppCompatActivity { public ACache aCache; public Setting mSetting = null; + protected CompositeSubscription compositeSubscription = new CompositeSubscription(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -80,4 +83,16 @@ public class BaseActivity extends AppCompatActivity { public void showSnackbar(View view, String s, boolean ture) { Snackbar.make(view, s, Snackbar.LENGTH_LONG).show(); } + + @Override + protected void onStop() { + super.onStop(); + //compositeSubscription.clear(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + compositeSubscription.clear(); + } } diff --git a/app/src/main/java/com/xiecc/seeWeather/base/BaseApplication.java b/app/src/main/java/com/xiecc/seeWeather/base/BaseApplication.java index 515133b..b59dd3f 100644 --- a/app/src/main/java/com/xiecc/seeWeather/base/BaseApplication.java +++ b/app/src/main/java/com/xiecc/seeWeather/base/BaseApplication.java @@ -24,12 +24,11 @@ public class BaseApplication extends Application { // 初始化 retrofit RetrofitSingleton.init(); CrashHandler.init(new CrashHandler(getApplicationContext())); - CrashReport.initCrashReport(getApplicationContext(), "900030389", false); + CrashReport.initCrashReport(getApplicationContext(), "900028220", false); BlockCanary.install(this, new AppBlockCanaryContext()).start(); LeakCanary.install(this); RxUtils.unifiedErrorHandler(); //Thread.setDefaultUncaughtExceptionHandler(new MyUnCaughtExceptionHandler()); - /** * 如果存在SD卡则将缓存写入SD卡,否则写入手机内存 */ diff --git a/app/src/main/java/com/xiecc/seeWeather/base/RxBus.java b/app/src/main/java/com/xiecc/seeWeather/base/RxBus.java new file mode 100644 index 0000000..1a8b7b5 --- /dev/null +++ b/app/src/main/java/com/xiecc/seeWeather/base/RxBus.java @@ -0,0 +1,51 @@ +package com.xiecc.seeWeather.base; + +import rx.Observable; +import rx.subjects.PublishSubject; +import rx.subjects.SerializedSubject; +import rx.subjects.Subject; + +/** + * RxBus + * Created by YoKeyword on 2015/6/17. + */ +public class RxBus { + + private static volatile RxBus instance; + // 主题 + private final Subject bus; + + // PublishSubject只会把在订阅发生的时间点之后来自原始Observable的数据发射给观察者 + public RxBus() { + bus = new SerializedSubject<>(PublishSubject.create()); + } + + // 单例RxBus + public static RxBus getDefault() { + if (instance == null) { + synchronized (RxBus.class) { + if (instance == null) { + instance = new RxBus(); + } + } + } + return instance; + } + + // 提供了一个新的事件 + public void post(Object o) { + bus.onNext(o); + } + + // 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者 + public Observable toObserverable(Class eventType) { + return bus.ofType(eventType); + // 这里感谢小鄧子的提醒: ofType = filter + cast + // return bus.filter(new Func1() { + // @Override + // public Boolean call(Object o) { + // return eventType.isInstance(o); + // } + // }) .cast(eventType); + } +} diff --git a/app/src/main/java/com/xiecc/seeWeather/common/CrashHandler.java b/app/src/main/java/com/xiecc/seeWeather/common/CrashHandler.java index 21652f4..b26af95 100644 --- a/app/src/main/java/com/xiecc/seeWeather/common/CrashHandler.java +++ b/app/src/main/java/com/xiecc/seeWeather/common/CrashHandler.java @@ -2,6 +2,7 @@ package com.xiecc.seeWeather.common; import android.content.Context; import com.xiecc.seeWeather.common.utils.Util; +import com.xiecc.seeWeather.modules.setting.Setting; import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; @@ -36,16 +37,15 @@ public class CrashHandler implements Thread.UncaughtExceptionHandler { PLog.e(TAG, ex.toString()); PLog.e(TAG, collectCrashDeviceInfo()); PLog.e(TAG, getCrashInfo(ex)); + + Setting mSetting = Setting.getInstance(); + mSetting.setCityName("北京"); // 调用系统错误机制 defaultHandler.uncaughtException(thread, ex); } - /** * 得到程序崩溃的详细信息 - * - * @param ex - * @return */ public String getCrashInfo(Throwable ex) { Writer result = new StringWriter(); @@ -55,11 +55,8 @@ public class CrashHandler implements Thread.UncaughtExceptionHandler { return result.toString(); } - /** * 收集程序崩溃的设备信息 - * - * @return */ public String collectCrashDeviceInfo() { @@ -69,8 +66,5 @@ public class CrashHandler implements Thread.UncaughtExceptionHandler { String manufacturer = android.os.Build.MANUFACTURER; return versionName + " " + model + " " + androidVersion + " " + manufacturer; - } - - } diff --git a/app/src/main/java/com/xiecc/seeWeather/common/utils/Util.java b/app/src/main/java/com/xiecc/seeWeather/common/utils/Util.java index 968e59b..d773792 100644 --- a/app/src/main/java/com/xiecc/seeWeather/common/utils/Util.java +++ b/app/src/main/java/com/xiecc/seeWeather/common/utils/Util.java @@ -7,6 +7,8 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.text.TextUtils; import com.xiecc.seeWeather.R; +import java.io.Closeable; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -106,7 +108,7 @@ public class Util { * 安全的 String 返回 * * @param prefix 默认字段 - * @param obj 拼接字段 + * @param obj 拼接字段 (需检查) */ public static String safeText(String prefix, String obj) { if (TextUtils.isEmpty(obj)) return ""; @@ -114,7 +116,7 @@ public class Util { } public static String safeText(String msg) { - return safeText(msg, ""); + return safeText("", msg); } /** @@ -143,4 +145,17 @@ public class Util { city = safeText(city).replaceAll("(?:省|市|自治区|特别行政区|地区|盟)", ""); return city; } + + /** + * Java 中有一个 Closeable 接口,标识了一个可关闭的对象,它只有一个 close 方法. + */ + public static void closeQuietly(Closeable closeable) { + if (null != closeable) { + try { + closeable.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/xiecc/seeWeather/component/RetrofitSingleton.java b/app/src/main/java/com/xiecc/seeWeather/component/RetrofitSingleton.java index 6a8f22d..ed3b804 100644 --- a/app/src/main/java/com/xiecc/seeWeather/component/RetrofitSingleton.java +++ b/app/src/main/java/com/xiecc/seeWeather/component/RetrofitSingleton.java @@ -28,9 +28,6 @@ public class RetrofitSingleton { private static OkHttpClient okHttpClient = null; private static final String TAG = RetrofitSingleton.class.getSimpleName(); - /** - * 初始化 - */ public static void init() { initOkHttp(); initRetrofit(); @@ -48,7 +45,7 @@ public class RetrofitSingleton { private static void initOkHttp() { // https://drakeet.me/retrofit-2-0-okhttp-3-0-config HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); - interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); + interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); okHttpClient = new OkHttpClient.Builder() .addInterceptor(interceptor) .retryOnConnectionFailure(true) @@ -77,7 +74,13 @@ public class RetrofitSingleton { public Observable fetchWeather(String city) { return apiService.mWeatherAPI(city, C.KEY) - .filter(weatherAPI -> weatherAPI.mHeWeatherDataService30s.get(0).status.equals("ok")) + //.filter(weatherAPI -> weatherAPI.mHeWeatherDataService30s.get(0).status.equals("ok")) + .flatMap(weatherAPI -> { + if (weatherAPI.mHeWeatherDataService30s.get(0).status.equals("no more requests")) { + return Observable.error(new RuntimeException("/(ㄒoㄒ)/~~,API免费次数已用完")); + } + return Observable.just(weatherAPI); + }) .map(weatherAPI -> weatherAPI.mHeWeatherDataService30s.get(0)) .compose(RxUtils.rxSchedulerHelper()); } diff --git a/app/src/main/java/com/xiecc/seeWeather/modules/city/db/DBManager.java b/app/src/main/java/com/xiecc/seeWeather/modules/city/db/DBManager.java index 647c477..bac9c82 100644 --- a/app/src/main/java/com/xiecc/seeWeather/modules/city/db/DBManager.java +++ b/app/src/main/java/com/xiecc/seeWeather/modules/city/db/DBManager.java @@ -44,7 +44,7 @@ public class DBManager { public void openDatabase() { - PLog.e(TAG, DB_PATH + "/" + DB_NAME); + //PLog.e(TAG, DB_PATH + "/" + DB_NAME); this.database = this.openDatabase(DB_PATH + "/" + DB_NAME); } diff --git a/app/src/main/java/com/xiecc/seeWeather/modules/city/db/WeatherDB.java b/app/src/main/java/com/xiecc/seeWeather/modules/city/db/WeatherDB.java index ea23bac..cb8a885 100644 --- a/app/src/main/java/com/xiecc/seeWeather/modules/city/db/WeatherDB.java +++ b/app/src/main/java/com/xiecc/seeWeather/modules/city/db/WeatherDB.java @@ -2,6 +2,7 @@ package com.xiecc.seeWeather.modules.city.db; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import com.xiecc.seeWeather.common.utils.Util; import com.xiecc.seeWeather.modules.city.domain.City; import com.xiecc.seeWeather.modules.city.domain.Province; import java.util.ArrayList; @@ -13,7 +14,6 @@ import java.util.List; */ public class WeatherDB { - public WeatherDB() { } @@ -32,7 +32,7 @@ public class WeatherDB { list.add(province); } while (cursor.moveToNext()); } - cursor.close(); + Util.closeQuietly(cursor); return list; } @@ -48,8 +48,7 @@ public class WeatherDB { list.add(city); } while (cursor.moveToNext()); } - cursor.close(); - + Util.closeQuietly(cursor); return list; } } diff --git a/app/src/main/java/com/xiecc/seeWeather/modules/city/ui/ChoiceCityActivity.java b/app/src/main/java/com/xiecc/seeWeather/modules/city/ui/ChoiceCityActivity.java index 2529b3d..489075d 100644 --- a/app/src/main/java/com/xiecc/seeWeather/modules/city/ui/ChoiceCityActivity.java +++ b/app/src/main/java/com/xiecc/seeWeather/modules/city/ui/ChoiceCityActivity.java @@ -16,6 +16,7 @@ import butterknife.Bind; import butterknife.ButterKnife; import com.xiecc.seeWeather.R; import com.xiecc.seeWeather.base.BaseActivity; +import com.xiecc.seeWeather.base.RxBus; import com.xiecc.seeWeather.common.PLog; import com.xiecc.seeWeather.common.utils.RxUtils; import com.xiecc.seeWeather.component.ImageLoader; @@ -24,7 +25,7 @@ import com.xiecc.seeWeather.modules.city.db.DBManager; import com.xiecc.seeWeather.modules.city.db.WeatherDB; import com.xiecc.seeWeather.modules.city.domain.City; import com.xiecc.seeWeather.modules.city.domain.Province; -import com.xiecc.seeWeather.modules.setting.Setting; +import com.xiecc.seeWeather.modules.main.domain.ChangeCityEvent; import java.util.ArrayList; import java.util.List; import jp.wasabeef.recyclerview.animators.FadeInUpAnimator; @@ -112,8 +113,11 @@ public class ChoiceCityActivity extends BaseActivity { selectedCity = cityList.get(pos); Intent intent = new Intent(); String cityName = selectedCity.CityName; - intent.putExtra(Setting.CITY_NAME, cityName); - setResult(2, intent); + //intent.putExtra(Setting.CITY_NAME, cityName); + //setResult(2, intent); + mSetting.setCityName(cityName); + RxBus.getDefault().post(new ChangeCityEvent()); + finish(); } }); diff --git a/app/src/main/java/com/xiecc/seeWeather/modules/main/domain/ChangeCityEvent.java b/app/src/main/java/com/xiecc/seeWeather/modules/main/domain/ChangeCityEvent.java new file mode 100644 index 0000000..e77694c --- /dev/null +++ b/app/src/main/java/com/xiecc/seeWeather/modules/main/domain/ChangeCityEvent.java @@ -0,0 +1,20 @@ +package com.xiecc.seeWeather.modules.main.domain; + +/** + * Created by HugoXie on 16/6/2. + * + * Email: Hugo3641@gamil.com + * GitHub: https://github.com/xcc3641 + * Info: + */ +public class ChangeCityEvent { + + String city; + + public ChangeCityEvent() { + } + + public ChangeCityEvent(String city) { + this.city = city; + } +} diff --git a/app/src/main/java/com/xiecc/seeWeather/modules/main/ui/MainActivity.java b/app/src/main/java/com/xiecc/seeWeather/modules/main/ui/MainActivity.java index f3f4e55..9890dcc 100644 --- a/app/src/main/java/com/xiecc/seeWeather/modules/main/ui/MainActivity.java +++ b/app/src/main/java/com/xiecc/seeWeather/modules/main/ui/MainActivity.java @@ -46,14 +46,17 @@ import com.tbruyelle.rxpermissions.RxPermissions; import com.xiecc.seeWeather.R; import com.xiecc.seeWeather.base.BaseActivity; import com.xiecc.seeWeather.base.C; +import com.xiecc.seeWeather.base.RxBus; import com.xiecc.seeWeather.common.PLog; import com.xiecc.seeWeather.common.utils.CheckVersion; +import com.xiecc.seeWeather.common.utils.ToastUtil; import com.xiecc.seeWeather.common.utils.Util; import com.xiecc.seeWeather.component.ImageLoader; import com.xiecc.seeWeather.component.RetrofitSingleton; import com.xiecc.seeWeather.modules.about.ui.AboutActivity; import com.xiecc.seeWeather.modules.city.ui.ChoiceCityActivity; import com.xiecc.seeWeather.modules.main.adapter.WeatherAdapter; +import com.xiecc.seeWeather.modules.main.domain.ChangeCityEvent; import com.xiecc.seeWeather.modules.main.domain.Weather; import com.xiecc.seeWeather.modules.main.listener.HidingScrollListener; import com.xiecc.seeWeather.modules.service.AutoUpdateService; @@ -62,6 +65,7 @@ import com.xiecc.seeWeather.modules.setting.ui.SettingActivity; import java.util.Calendar; import rx.Observable; import rx.Observer; +import rx.android.schedulers.AndroidSchedulers; // TODO: 16/5/18 config.gradle 未提示 整合 retrofit 的使用 public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener, @@ -114,13 +118,35 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig @Override protected void onStart() { super.onStart(); + PLog.i("onStart"); showEggs(); + compositeSubscription.add( + RxBus.getDefault().toObserverable(ChangeCityEvent.class).observeOn(AndroidSchedulers.mainThread()).subscribe( + changeCityEvent -> { + mRefreshLayout.setRefreshing(true); + load(); + }, throwable -> { + PLog.e(throwable.getMessage()) + ; + })); + } + + @Override + protected void onRestart() { + super.onRestart(); + PLog.i("onRestart"); //为了实现 Intent 重启使图标生效 initIcon(); // 修改 adapter 的初始化 mAdapter.notifyDataSetChanged(); } + @Override + protected void onResume() { + super.onResume(); + PLog.i("onResume"); + } + @Override protected void onPause() { super.onPause(); @@ -131,6 +157,7 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig protected void onStop() { super.onStop(); PLog.i("onStop"); + } /** @@ -391,10 +418,12 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig */ private Observable fetchDataByNetWork() { String cityName = Util.replaceCity(mSetting.getCityName()); + PLog.i("网络请求"); return RetrofitSingleton.getInstance() .fetchWeather(cityName) .onErrorReturn(throwable -> { PLog.e(throwable.getMessage()); + ToastUtil.showLong(throwable.getMessage()); return null; }); } @@ -504,16 +533,16 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig } } - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - //requestCode标示请求的标示 resultCode表示有数据 - if (requestCode == 1 && resultCode == 2) { - mRefreshLayout.setRefreshing(true); - mSetting.setCityName(data.getStringExtra(Setting.CITY_NAME)); - load(); - } - } + //@Override + //protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // super.onActivityResult(requestCode, resultCode, data); + // //requestCode标示请求的标示 resultCode表示有数据 + // if (requestCode == 1 && resultCode == 2) { + // mRefreshLayout.setRefreshing(true); + // mSetting.setCityName(data.getStringExtra(Setting.CITY_NAME)); + // load(); + // } + //} private void normalStyleNotification(Weather weather) { Intent intent = new Intent(MainActivity.this, MainActivity.class); diff --git a/gradle.properties b/gradle.properties index 4930285..4dc1174 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,5 +12,7 @@ # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -#Wed Apr 13 13:42:18 CST 2016 +#Sat May 28 20:20:10 CST 2016 +systemProp.http.proxyHost=127.0.0.1 +systemProp.http.proxyPort=1080 android.useDeprecatedNdk=true