下面列出了android.content.res.Configuration#UI_MODE_NIGHT_MASK 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean isDarkModeEnabled() {
if (getConfig() == null) return false;
if (!mContext.getResources().getBoolean(R.bool.allow_dark_mode)) return false;
boolean useNightModeDefault =
AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES;
if (mContext != null) {
int nightMode =
mContext.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
if (nightMode == Configuration.UI_MODE_NIGHT_YES) {
useNightModeDefault = true;
}
}
return getConfig().getBool("app", "dark_mode", useNightModeDefault);
}
@Override
public void onClick(View view) {
int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO: {
setDayNightMode(AppCompatDelegate.MODE_NIGHT_YES);
// Night mode is not active, we're in day time
break;
}
case Configuration.UI_MODE_NIGHT_YES:{
setDayNightMode(AppCompatDelegate.MODE_NIGHT_NO);
// Night mode is active, we're at night!
break;
}
case Configuration.UI_MODE_NIGHT_UNDEFINED: {
// We don't know what mode we're in, assume notnight
}
}
}
public Observable<List<Displayable>> parseDisplayables(GetStoreWidgets getStoreWidgets) {
int currentNightMode = getContext().getResources()
.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
boolean isDarkTheme = currentNightMode == Configuration.UI_MODE_NIGHT_YES;
return Observable.from(getStoreWidgets.getDataList()
.getList())
.concatMapEager(wsWidget -> {
AptoideApplication application =
(AptoideApplication) getContext().getApplicationContext();
return DisplayablesFactory.parse(marketName, wsWidget, storeTheme, storeRepository,
storeCredentialsProvider, storeContext, getContext(), accountManager, storeUtilsProxy,
(WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE),
getContext().getResources(), installedRepository, storeAnalytics, storeTabNavigator,
navigationTracker, new BadgeDialogFactory(getActivity(), themeManager),
((ActivityResultNavigator) getContext()).getFragmentNavigator(),
application.getBodyInterceptorPoolV7(), application.getDefaultClient(),
WebService.getDefaultConverter(), application.getTokenInvalidator(),
application.getDefaultSharedPreferences(), themeManager);
})
.toList()
.first();
}
@Theme
@Override
public int getCurrentTheme() {
int currentNightMode = mContext.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return Theme.LIGHT;
case Configuration.UI_MODE_NIGHT_YES:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
default:
return Theme.DARK;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search_post_sort_type_bottom_sheet, container, false);
ButterKnife.bind(this, rootView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) {
rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
relevanceTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(SortType.Type.RELEVANCE.name());
dismiss();
});
hotTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(SortType.Type.HOT.name());
dismiss();
});
topTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(SortType.Type.TOP.name());
dismiss();
});
newTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.NEW));
dismiss();
});
commentsTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(SortType.Type.COMMENTS.name());
dismiss();
});
return rootView;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_post_type_bottom_sheet, container, false);
ButterKnife.bind(this, rootView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) {
rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
textTypeTextView.setOnClickListener(view -> {
((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_TEXT);
dismiss();
});
linkTypeTextView.setOnClickListener(view -> {
((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_LINK);
dismiss();
});
imageTypeTextView.setOnClickListener(view -> {
((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_IMAGE);
dismiss();
});
videoTypeTextView.setOnClickListener(view -> {
((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_VIDEO);
dismiss();
});
return rootView;
}
static String applicationTheme(Context context, boolean useNightMode) {
synchronized (PPApplication.applicationPreferencesMutex) {
if (applicationTheme == null)
applicationTheme(context);
String _applicationTheme = applicationTheme;
if (_applicationTheme.equals("light") ||
_applicationTheme.equals("material") ||
_applicationTheme.equals("color") ||
_applicationTheme.equals("dlight")) {
String defaultValue = "white";
if (Build.VERSION.SDK_INT >= 28)
defaultValue = "night_mode";
_applicationTheme = defaultValue;
SharedPreferences.Editor editor = ApplicationPreferences.getSharedPreferences(context).edit();
editor.putString(ApplicationPreferences.PREF_APPLICATION_THEME, _applicationTheme);
editor.apply();
applicationTheme = _applicationTheme;
}
if (_applicationTheme.equals("night_mode") && useNightMode) {
int nightModeFlags =
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
case Configuration.UI_MODE_NIGHT_YES:
_applicationTheme = "dark";
break;
case Configuration.UI_MODE_NIGHT_NO:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
_applicationTheme = "white";
break;
}
}
return _applicationTheme;
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
// case R.id.nav_account:
// drawer_layout.closeDrawers();
// return false;
case R.id.nav_switch_night_mode:
int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if (mode == Configuration.UI_MODE_NIGHT_YES) {
SettingUtil.getInstance().setIsNightMode(false);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else {
SettingUtil.getInstance().setIsNightMode(true);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
getWindow().setWindowAnimations(R.style.WindowAnimationFadeInOut);
recreate();
return false;
case R.id.nav_setting:
startActivity(new Intent(this, SettingActivity.class));
drawer_layout.closeDrawers();
return false;
case R.id.nav_share:
Intent shareIntent = new Intent()
.setAction(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_app_text) + getString(R.string.source_code_url));
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_to)));
drawer_layout.closeDrawers();
return false;
}
return false;
}
@Override
protected void onResume() {
super.onResume();
int uiMode = getResources().getConfiguration().uiMode;
int dayNightUiMode = uiMode & Configuration.UI_MODE_NIGHT_MASK;
if (dayNightUiMode == Configuration.UI_MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if (dayNightUiMode == Configuration.UI_MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences("setting", MODE_PRIVATE);
editor = sharedPreferences.edit();
//Q导航栏沉浸
rootview = findViewById(android.R.id.content);
/*ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView(), new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
v.setPadding(0,0,0,insets.getSystemWindowInsetBottom());
return insets;
}
});*/
//状态栏icon黑色
int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if (mode == Configuration.UI_MODE_NIGHT_NO) {
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
rootview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
ViewCompat.setOnApplyWindowInsetsListener(rootview, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
rootview.setPadding(insets.getSystemWindowInsetLeft(), 0, insets.getSystemWindowInsetRight(), 0);
return insets;
}
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
//rootView.setFitsSystemWindows(true);
//rootView.setPadding(0,0,0,getNavigationBarHeight());
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
//rootView.setFitsSystemWindows(true);
/*if ((Build.VERSION.SDK_INT<Build.VERSION_CODES.Q)|(getNavigationBarHeight()>dp2px(16))) {
//rootView.setPadding(0,0,0,getNavigationBarHeight());
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
//rootView.setFitsSystemWindows(true);
rootView.setPadding(0,0,0,getNavigationBarHeight());
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}else {
//rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//rootView.setFitsSystemWindows(true);
}
}else {
rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}*/
//设置为miui主题
//setMiuiTheme(BaseActivity.this,0,mode);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//全局自定义字体
ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath(sharedPreferences.getString("font_path", null))
.setFontAttrId(R.attr.fontPath)
.build()))
.build());
}
public static boolean isDarkMode(Context context) {
return (context.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
@Override
public boolean isSystemNightMode() {
return (getContext().getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
private void loadImage() {
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
Glide.with(mContext)
.load(GlideUtil.getSquare(illust))
.apply(bitmapTransform(new BlurTransformation(25, 3)))
.transition(withCrossFade())
.into(baseBind.bgImage);
break;
case Configuration.UI_MODE_NIGHT_YES:
baseBind.bgImage.setImageResource(R.color.black);
break;
}
mDetailAdapter = new IllustDetailAdapter(illust, mActivity);
mDetailAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View v, int position, int viewType) {
if (viewType == 0) {
Intent intent = new Intent(mContext, ImageDetailActivity.class);
intent.putExtra("illust", illust);
intent.putExtra("dataType", "二级详情");
intent.putExtra("index", position);
if (Shaft.sSettings.isFirstImageSize()) {
mActivity.startActivity(intent);
} else {
if (mDetailAdapter.getHasLoad().get(position)) {
Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(mActivity,
v, "big_image_" + position).toBundle();
startActivity(intent, bundle);
} else {
mActivity.startActivity(intent);
}
}
} else if (viewType == 1) {
}
}
});
baseBind.recyclerView.setAdapter(mDetailAdapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/bold.ttf")
.setFontAttrId(R.attr.fontPath)
.build()))
.build());
setContentView(R.layout.activity_post_comments);
mFirestore = FirebaseFirestore.getInstance();
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("Comments");
int nightModeFlags=getResources().getConfiguration().uiMode& Configuration.UI_MODE_NIGHT_MASK;
if(nightModeFlags==UI_MODE_NIGHT_NO){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags=getWindow().getDecorView().getSystemUiVisibility();
flags|=View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
getWindow().getDecorView().setSystemUiVisibility(flags);
}
toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDarkk));
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Comments");
user_image=findViewById(R.id.comment_admin);
post_desc=findViewById(R.id.comment_post_desc);
Cursor rs = new UserHelper(this).getData(1);
rs.moveToFirst();
String image = rs.getString(rs.getColumnIndex(UserHelper.CONTACTS_COLUMN_IMAGE));
if (!rs.isClosed()) {
rs.close();
}
Glide.with(this)
.setDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.default_profile_picture))
.load(image)
.into(user_image);
setupCommentView();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/bold.ttf")
.setFontAttrId(R.attr.fontPath)
.build()))
.build());
setContentView(R.layout.activity_post_image);
imagesList=getIntent().getParcelableArrayListExtra("imagesList");
if(imagesList.isEmpty()){
finish();
}
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("New Image Post");
int nightModeFlags=getResources().getConfiguration().uiMode& Configuration.UI_MODE_NIGHT_MASK;
if(nightModeFlags==UI_MODE_NIGHT_NO){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags=getWindow().getDecorView().getSystemUiVisibility();
flags|=View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
getWindow().getDecorView().setSystemUiVisibility(flags);
}
toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDarkk));
}
try {
getSupportActionBar().setTitle("New Image Post");
} catch (Exception e) {
e.printStackTrace();
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
postMap = new HashMap<>();
pager=findViewById(R.id.pager);
indicator=findViewById(R.id.indicator);
indicator_holder=findViewById(R.id.indicator_holder);
indicator.setDotsClickable(true);
adapter=new PagerPhotosAdapter(this,imagesList);
pager.setAdapter(adapter);
if(imagesList.size()>1){
indicator_holder.setVisibility(View.VISIBLE);
indicator.setViewPager(pager);
}else{
indicator_holder.setVisibility(GONE);
}
mFirestore = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
sharedPreferences=getSharedPreferences("uploadservice",MODE_PRIVATE);
serviceCount=sharedPreferences.getInt("count",0);
mEditText = findViewById(R.id.text);
compressor=new Compressor(this)
.setQuality(85)
.setCompressFormat(Bitmap.CompressFormat.PNG);
mDialog = new ProgressDialog(this);
mStorage=FirebaseStorage.getInstance().getReference();
}
public static boolean isDarkThemeActive(Context context) {
int uiMode = context.getResources().getConfiguration().uiMode;
return (uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sort_type_bottom_sheet, container, false);
ButterKnife.bind(this, rootView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) {
rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
if (getArguments() == null || getArguments().getBoolean(EXTRA_NO_BEST_TYPE)) {
bestTypeTextView.setVisibility(View.GONE);
} else {
bestTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.BEST));
dismiss();
});
}
hotTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.HOT));
dismiss();
});
newTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.NEW));
dismiss();
});
if (getArguments() == null || (getArguments().containsKey(EXTRA_NO_RANDOM_TYPE) && getArguments().getBoolean(EXTRA_NO_RANDOM_TYPE))) {
randomTypeTextView.setVisibility(View.GONE);
} else {
randomTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.RANDOM));
dismiss();
});
}
risingTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.RISING));
dismiss();
});
topTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(SortType.Type.TOP.name());
dismiss();
});
controversialTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(SortType.Type.CONTROVERSIAL.name());
dismiss();
});
return rootView;
}
private boolean shouldUseDarkThemeForAutoMode() {
return (mContext.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
private int getCurrentNightMode() {
return getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
}
/**
* Determine if the current UI Mode is Night Mode.
*
* @param context Context to get the configuration.
* @return true if the night mode is enabled, otherwise false.
*/
protected static boolean isNightMode(@NonNull Context context) {
int currentNightMode = context.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
}