This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
/
BaseActivity.java
127 lines (112 loc) · 4.12 KB
/
BaseActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.xiecc.seeWeather.base;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.WindowManager;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.xiecc.seeWeather.common.ACache;
import com.xiecc.seeWeather.modules.setting.Setting;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;
/**
* Created by hugo on 2015/12/16.
*/
public class BaseActivity extends AppCompatActivity {
private static String TAG = BaseActivity.class.getSimpleName();
public ACache aCache;
public Setting mSetting = null;
private CompositeSubscription mCompositeSubscription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//overridePendingTransition(R.anim.zoom_enter,R.anim.zoom_exit);
if (savedInstanceState==null){
setTheme(this);
}
aCache = ACache.get(getApplication());
mSetting = Setting.getInstance();
}
public CompositeSubscription getCompositeSubscription() {
if (this.mCompositeSubscription == null) {
this.mCompositeSubscription = new CompositeSubscription();
}
return this.mCompositeSubscription;
}
public void addSubscription(Subscription s) {
if (this.mCompositeSubscription == null) {
this.mCompositeSubscription = new CompositeSubscription();
}
this.mCompositeSubscription.add(s);
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
}
/**
* 设置状态栏颜色
* 也就是所谓沉浸式状态栏
*/
@Deprecated
public void setStatusBarColor(int color) {
/**
* Android4.4以上 但是抽屉有点冲突,目前就重写一个方法暂时解决4.4的问题
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(color);
}
}
public void setStatusBarColorForKitkat(int color) {
/**
* Android4.4
*/
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(color);
}
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (this.mCompositeSubscription != null) {
this.mCompositeSubscription.unsubscribe();
}
}
public static void setDayTheme(AppCompatActivity activity) {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_NO);
activity.getDelegate().setLocalNightMode(
AppCompatDelegate.MODE_NIGHT_NO);
// 调用 recreate() 使设置生效
activity.recreate();
}
public static void setNightTheme(AppCompatActivity activity) {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
activity.getDelegate().setLocalNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
// 调用 recreate() 使设置生效
activity.recreate();
}
public void setTheme(boolean isNights, AppCompatActivity activity) {
if (isNights) {
setNightTheme(activity);
} else {
setDayTheme(activity);
}
}
public void setTheme(AppCompatActivity activity) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
activity.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
activity.recreate();
}
}