Skip to content

Commit 0633c54

Browse files
committed
add daynight mode
1 parent 94d2be0 commit 0633c54

26 files changed

+229
-92
lines changed

app/build.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,18 @@ android {
135135
}
136136
}
137137

138+
repositories {
139+
flatDir {
140+
dirs 'libs' //this way we can find the .aar file in libs folder
141+
}
142+
}
143+
138144
dependencies {
139145
compile fileTree(dir: 'libs', include: ['*.jar'])
140146
testCompile 'junit:junit:4.12'
141147
compile 'org.greenrobot:eventbus:3.0.0'
142148
compile 'com.google.code.gson:gson:2.4'
143149
compile 'com.tencent.bugly:crashreport:latest.release'
144-
compile 'me.imid.swipebacklayout.lib:library:1.0.0'
145150
compile 'com.umeng.analytics:analytics:latest.integration'
146151
compile 'com.squareup.okhttp3:okhttp:3.4.1'
147152
compile 'com.squareup.picasso:picasso:2.5.2'
@@ -152,4 +157,6 @@ dependencies {
152157
compile 'com.android.support:design:24.2.1'
153158
compile 'com.android.support:support-v4:24.2.1'
154159
compile 'pub.devrel:easypermissions:0.2.0'
160+
161+
compile(name:'swipebacklayout', ext:'aar')
155162
}

app/libs/swipebacklayout.aar

49.4 KB
Binary file not shown.

app/src/main/java/com/brian/codeblog/App.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Context;
66
import android.os.StrictMode;
77

8+
import com.brian.common.tools.DayNightHelper;
89
import com.brian.common.utils.AppInfoUtil;
910
import com.brian.codeblog.manager.ConfigHelper;
1011
import com.brian.common.utils.LogUtil;
@@ -31,6 +32,7 @@ public void onCreate() {
3132
ConfigHelper.init(this);
3233
LogUtil.w("Config.DEBUG_ENABLE=" + Config.DEBUG_ENABLE);
3334
setStrictModeEnable(Config.DEBUG_ENABLE);
35+
DayNightHelper.getInstance().initDayNightMode();
3436
}
3537
handleCrash();
3638
}

app/src/main/java/com/brian/codeblog/activity/BaseActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void initStatusBar() {
124124
return;
125125
}
126126
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
127-
getWindow().setStatusBarColor(ResourceUtil.getColor(R.color.light_blue));
127+
getWindow().setStatusBarColor(ResourceUtil.getColor(R.color.common_titlebar_bg));
128128
}
129129
}
130130

app/src/main/java/com/brian/codeblog/activity/MainTabActivity.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.widget.FrameLayout;
1313
import android.widget.ImageView;
1414

15+
import com.brian.codeblog.App;
1516
import com.brian.codeblog.Config;
1617
import com.brian.codeblog.activity.adapter.MainTabAdapter;
1718
import com.brian.codeblog.datacenter.preference.CommonPreference;
@@ -211,9 +212,16 @@ protected void onDestroy() {
211212
int count = CommonPreference.getInstance().getBlogReadCount();
212213
String time = TimeUtil.convCountTime(CommonPreference.getInstance().getBlogReadTime());
213214
ToastUtil.showMsg(String.format(Locale.CHINA, "累计学习 %d篇博文,用时 %s", count, time));
214-
super.onDestroy();
215215
OnlineConfigAgent.getInstance().removeOnlineConfigListener();
216216
SpotManager.getInstance(this).onDestroy();
217+
super.onDestroy();
218+
219+
getUIHandler().postDelayed(new Runnable() {
220+
@Override
221+
public void run() {
222+
App.exit();
223+
}
224+
}, 500);
217225
}
218226

219227
// 当启动模式为singletask,重新被启动时调用

app/src/main/java/com/brian/codeblog/activity/SettingActivity.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.brian.codeblog.manager.ShareManager;
1717
import com.brian.codeblog.update.UpdateManager;
1818
import com.brian.codeblog.manager.UsageStatsManager;
19+
import com.brian.common.tools.DayNightHelper;
1920
import com.brian.common.utils.FileUtil;
2021
import com.brian.common.utils.MarketUtils;
2122
import com.brian.common.utils.ToastUtil;
@@ -30,6 +31,7 @@ public class SettingActivity extends BaseActivity {
3031

3132
@BindView(R.id.title_bar) TitleBar mTitleBar;
3233

34+
@BindView(R.id.switch_night_mode) TextView mNightModeText;
3335
@BindView(R.id.switch_show_ad_text) TextView mSwitchAdText;
3436
@BindView(R.id.switch_vertical_text) TextView mSwitchVerticalText;
3537
@BindView(R.id.switch_picinfwifi_text) TextView mSwitchPicWifiText;
@@ -133,6 +135,23 @@ public void onClick(View v) {
133135
}
134136
});
135137

138+
mNightModeText.setSelected(SettingPreference.getInstance().getNightModeEnable());
139+
mNightModeText.setOnClickListener(new OnClickListener() {
140+
141+
@Override
142+
public void onClick(View v) {
143+
UsageStatsManager.sendUsageData(UsageStatsManager.SETTING_NIGHT, "night_mode");
144+
boolean selected = mNightModeText.isSelected();
145+
selected = !selected;
146+
mNightModeText.setSelected(selected);
147+
148+
// 保存preference
149+
SettingPreference.getInstance().setNightModeEnable(selected);
150+
151+
DayNightHelper.getInstance().setDayNightMode(selected);
152+
}
153+
});
154+
136155
mMarketText.setOnClickListener(new OnClickListener() {
137156
@Override
138157
public void onClick(View v) {

app/src/main/java/com/brian/codeblog/datacenter/preference/SettingPreference.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
public class SettingPreference extends BasePreference {
77

8+
private static final String KEY_NIGHT_MODE = "setting_night_mode";
89
private static final String KEY_LOAD_IMAGE_WIFI = "setting_load_Img";
910
private static final String KEY_RUN_IN_BACK = "setting_run_in_back";
1011
private static final String KEY_SHOW_ADS = "setting_show_ads";
@@ -23,12 +24,16 @@ public static final SettingPreference getInstance() {
2324
return sInstance;
2425
}
2526

26-
public void setRunInBackEnable(boolean isInBackEnable) {
27-
putBoolean(KEY_RUN_IN_BACK, isInBackEnable);
27+
public void setRunInBackEnable(boolean enable) {
28+
putBoolean(KEY_RUN_IN_BACK, enable);
2829
}
2930

30-
public void setLoadImgOnlyInWifiEnable(boolean isWifiOnly) {
31-
putBoolean(KEY_LOAD_IMAGE_WIFI, isWifiOnly);
31+
public void setLoadImgOnlyInWifiEnable(boolean enable) {
32+
putBoolean(KEY_LOAD_IMAGE_WIFI, enable);
33+
}
34+
35+
public void setNightModeEnable(boolean enable) {
36+
putBoolean(KEY_NIGHT_MODE, enable);
3237
}
3338

3439
public void setAdsEnable(boolean isAdsEnable) {
@@ -46,4 +51,8 @@ public boolean getLoadImgOnlyInWifiEnable() {
4651
public boolean getAdsEnable() {
4752
return getBoolean(KEY_SHOW_ADS, true);
4853
}
54+
55+
public boolean getNightModeEnable() {
56+
return getBoolean(KEY_NIGHT_MODE, false);
57+
}
4958
}

app/src/main/java/com/brian/codeblog/manager/UsageStatsManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class UsageStatsManager {
4040

4141
public static final String SETTING_LIST = "setting_list";
4242
public static final String SETTING_AD = "setting_ad";
43+
public static final String SETTING_NIGHT= "setting_night";
4344

4445
// private static ArrayList<HashMap<String, Object>> mUsageDatas = null;
4546
// private static final int MAX_CACHE_SIZE = 10;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.brian.common.tools;
2+
3+
import android.support.v7.app.AppCompatDelegate;
4+
5+
import com.brian.codeblog.activity.BaseActivity;
6+
import com.brian.codeblog.datacenter.preference.SettingPreference;
7+
8+
/**
9+
* 夜间模式辅助类
10+
* Created by huamm on 2016/10/31 0031.
11+
*/
12+
13+
public class DayNightHelper {
14+
15+
private boolean mIsDayNightEnabled = false;
16+
17+
private static class SingletonHolder {
18+
private static final DayNightHelper INSTANCE = new DayNightHelper();
19+
}
20+
private DayNightHelper() {
21+
}
22+
public static final DayNightHelper getInstance() {
23+
return SingletonHolder.INSTANCE;
24+
}
25+
26+
public void initDayNightMode() {
27+
mIsDayNightEnabled = SettingPreference.getInstance().getNightModeEnable();
28+
AppCompatDelegate.setDefaultNightMode(mIsDayNightEnabled ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
29+
}
30+
31+
public void setDayNightMode(boolean enable) {
32+
if (mIsDayNightEnabled == enable) {
33+
return;
34+
}
35+
mIsDayNightEnabled = enable;
36+
BaseActivity activity = BaseActivity.getTopActivity();
37+
activity.getDelegate().setLocalNightMode(enable ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
38+
activity.recreate();
39+
}
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.brian.common.tools;
2+
3+
import android.view.View;
4+
5+
import java.util.Calendar;
6+
7+
/**
8+
* 防止连续点击
9+
* Created by huamm on 2016/10/27 0027.
10+
*/
11+
public abstract class NoDoubleClickListener implements View.OnClickListener {
12+
13+
public static final int MIN_CLICK_DELAY_TIME = 500;
14+
private long lastClickTime = 0;
15+
16+
@Override
17+
public void onClick(View v) {
18+
long currentTime = Calendar.getInstance().getTimeInMillis();
19+
if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
20+
lastClickTime = currentTime;
21+
onNoDoubleClick(v);
22+
}
23+
}
24+
25+
public abstract void onNoDoubleClick(View view);
26+
}

0 commit comments

Comments
 (0)