Java Code Examples for android.content.res.Configuration#UI_MODE_NIGHT_NO

The following examples show how to use android.content.res.Configuration#UI_MODE_NIGHT_NO . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RNCAppearanceModule.java    From react-native-appearance with MIT License 6 votes vote down vote up
protected String getColorScheme(Configuration config) {
    String colorScheme = "no-preference";

    // Only Android 10+ support dark mode
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
        int currentNightMode = config.uiMode & Configuration.UI_MODE_NIGHT_MASK;
        switch (currentNightMode) {
            case Configuration.UI_MODE_NIGHT_NO:
            case Configuration.UI_MODE_NIGHT_UNDEFINED:
                colorScheme = "light";
                break;
            case Configuration.UI_MODE_NIGHT_YES:
                colorScheme = "dark";
                break;

        }
    }

    return colorScheme;
}
 
Example 2
Source File: MainActivity.java    From Markdown with MIT License 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() != 0x1
            && item.getItemId() != 0x2) {
        setText(item.getItemId());
        return true;
    } else if (item.getItemId() == 0x1) {
        getResources().getConfiguration().uiMode |= Configuration.UI_MODE_NIGHT_YES;
        getResources().getConfiguration().uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
        getResources().updateConfiguration(getResources().getConfiguration(), getResources().getDisplayMetrics());
        recreate();
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }
}
 
Example 3
Source File: MainActivity.java    From SkinSprite with MIT License 6 votes vote down vote up
@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
        }
    }
}
 
Example 4
Source File: U.java    From Taskbar with Apache License 2.0 6 votes vote down vote up
private static String getCurrentTheme(Context context) {
    String defaultTheme = context.getString(R.string.tb_pref_theme_default);

    SharedPreferences pref = getSharedPreferences(context);
    String themePref = pref.getString(PREF_THEME, defaultTheme);

    if(themePref.equals("system")) {
        Configuration configuration = context.getResources().getConfiguration();
        int currentNightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
        switch(currentNightMode) {
            case Configuration.UI_MODE_NIGHT_NO:
                // Night mode is not active, we're using the light theme
                return "light";
            case Configuration.UI_MODE_NIGHT_YES:
                // Night mode is active, we're using dark theme
                return "dark";
        }
    } else
        return themePref;

    return defaultTheme;
}
 
Example 5
Source File: MainActivity.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    getDelegate().onConfigurationChanged(newConfig);
    int currentNightMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
    switch (currentNightMode) {
        case Configuration.UI_MODE_NIGHT_NO:
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            break;
        case Configuration.UI_MODE_NIGHT_YES:
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            break;
    }

    finish();
    overridePendingTransition(0, 0);
    startActivity(getIntent());
}
 
Example 6
Source File: ThemeableActivity.java    From linphone-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();

    int nightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    switch (nightMode) {
        case Configuration.UI_MODE_NIGHT_NO:
        case Configuration.UI_MODE_NIGHT_UNDEFINED:
            if (LinphonePreferences.instance().isDarkModeEnabled()) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            }
        case Configuration.UI_MODE_NIGHT_YES:
            if (!LinphonePreferences.instance().isDarkModeEnabled()) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
    }
}
 
Example 7
Source File: ThemeUtils.java    From call_manage with MIT License 5 votes vote down vote up
/**
 * Check if night mode is on by system
 * @param context
 * @return is on / not
 */
public static boolean isNightModeOn(Context context) {
    int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    switch (currentNightMode) {
        case Configuration.UI_MODE_NIGHT_NO:
            // Night mode is not active, we're using the light theme
            return false;
        case Configuration.UI_MODE_NIGHT_YES:
            // Night mode is active, we're using dark theme
            return true;
        default:
            return false;
    }
}
 
Example 8
Source File: ThemeUtils.java    From timecat with Apache License 2.0 5 votes vote down vote up
public static Resources updateNightMode(Resources resource, boolean on) {
    DisplayMetrics dm = resource.getDisplayMetrics();
    Configuration config = resource.getConfiguration();
    final int uiModeNightMaskOrigin = config.uiMode &= ~Configuration.UI_MODE_TYPE_MASK;
    final int uiModeNightMaskNew = on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
    if (uiModeNightMaskOrigin != uiModeNightMaskNew) {
        config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        config.uiMode |= uiModeNightMaskNew;
        resource.updateConfiguration(config, dm);
    }
    return resource;
}
 
Example 9
Source File: SettingsFragment.java    From MaoWanAndoidClient with Apache License 2.0 5 votes vote down vote up
private void ChangeThemeMode() {
    int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    if(mode == Configuration.UI_MODE_NIGHT_YES) {
        RxBus.getDefault().post(new ThemeModeEvent(AppCompatDelegate.MODE_NIGHT_NO));
    } else if(mode == Configuration.UI_MODE_NIGHT_NO) {
        RxBus.getDefault().post(new ThemeModeEvent(AppCompatDelegate.MODE_NIGHT_YES));
    }else {
        //否则保持亮色主题
        RxBus.getDefault().post(new ThemeModeEvent(AppCompatDelegate.MODE_NIGHT_NO));
    }
}
 
Example 10
Source File: AuthActivity.java    From Aegis with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void setPreferredTheme(Theme theme) {
    if (theme == Theme.SYSTEM || theme == Theme.SYSTEM_AMOLED) {
        // set the theme based on the system theme
        int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        switch (currentNightMode) {
            case Configuration.UI_MODE_NIGHT_NO:
                theme = Theme.LIGHT;
                break;
            case Configuration.UI_MODE_NIGHT_YES:
                theme = theme == Theme.SYSTEM_AMOLED ? Theme.AMOLED : Theme.DARK;
                break;
        }
    }

    switch (theme) {
        case LIGHT:
            setTheme(R.style.AppTheme_Light_NoActionBar);
            break;
        case DARK:
            setTheme(R.style.AppTheme_Dark_NoActionBar);
            break;
        case AMOLED:
            setTheme(R.style.AppTheme_TrueBlack_NoActionBar);
            break;
    }
}
 
Example 11
Source File: AegisActivity.java    From Aegis with GNU General Public License v3.0 5 votes vote down vote up
protected void setPreferredTheme(Theme theme) {
    if (theme == Theme.SYSTEM || theme == Theme.SYSTEM_AMOLED) {
        // set the theme based on the system theme
        int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        switch (currentNightMode) {
            case Configuration.UI_MODE_NIGHT_NO:
                theme = Theme.LIGHT;
                break;
            case Configuration.UI_MODE_NIGHT_YES:
                theme = theme == Theme.SYSTEM_AMOLED ? Theme.AMOLED : Theme.DARK;
                break;
        }
    }

    _currentTheme = theme;

    switch (theme) {
        case LIGHT:
            setTheme(R.style.AppTheme);
            break;
        case DARK:
            setTheme(R.style.AppTheme_Dark);
            break;
        case AMOLED:
            setTheme(R.style.AppTheme_TrueBlack);
            break;
    }
}
 
Example 12
Source File: CommonUtils.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
public static boolean isNightModeOn(@NonNull Context context, boolean fallback) {
    int mode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    switch (mode) {
        case Configuration.UI_MODE_NIGHT_YES:
            return true;
        case Configuration.UI_MODE_NIGHT_NO:
            return false;
        default:
        case Configuration.UI_MODE_NIGHT_UNDEFINED:
            return fallback;
    }
}
 
Example 13
Source File: ThemeManager.java    From zephyr with MIT License 5 votes vote down vote up
@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;
    }
}
 
Example 14
Source File: ThemeUtils.java    From MagicaSakura with Apache License 2.0 5 votes vote down vote up
public static Resources updateNightMode(Resources resource, boolean on) {
    DisplayMetrics dm = resource.getDisplayMetrics();
    Configuration config = resource.getConfiguration();
    final int uiModeNightMaskOrigin = config.uiMode &= ~Configuration.UI_MODE_TYPE_MASK;
    final int uiModeNightMaskNew = on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
    if (uiModeNightMaskOrigin != uiModeNightMaskNew) {
        config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        config.uiMode |= uiModeNightMaskNew;
        resource.updateConfiguration(config, dm);
    }
    return resource;
}
 
Example 15
Source File: MainActivity.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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);
    }
}
 
Example 16
Source File: ThemeHelper.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private static boolean nightMode(Context context) {
    int nightModeFlags = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    switch (nightModeFlags) {
        case Configuration.UI_MODE_NIGHT_YES:
            return true;
        case Configuration.UI_MODE_NIGHT_NO:
            return false;
        case Configuration.UI_MODE_NIGHT_UNDEFINED:
            return false;
        default:
            return false;
    }
}
 
Example 17
Source File: FragmentTracklist.java    From GPSLogger with GNU General Public License v3.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_tracklist, container, false);

    TVTracklistEmpty    = view.findViewById(R.id.id_textView_TracklistEmpty);
    recyclerView        = view.findViewById(R.id.my_recycler_view);

    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.getItemAnimator().setChangeDuration(0);
    adapter = new TrackAdapter(data);

    switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
        case Configuration.UI_MODE_NIGHT_NO:
            // Night mode is not active, we're in day time
            adapter.isLightTheme = true;
            break;
        case Configuration.UI_MODE_NIGHT_YES:
            // Night mode is active, we're at night!
        case Configuration.UI_MODE_NIGHT_UNDEFINED:
            // We don't know what mode we're in, assume notnight
            adapter.isLightTheme = false;
            break;
    }

    recyclerView.setAdapter(adapter);

    return view;
}
 
Example 18
Source File: FragmentSingleIllust.java    From Pixiv-Shaft with MIT License 4 votes vote down vote up
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);
}
 
Example 19
Source File: BaseActivity.java    From OneText_For_Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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());
}
 
Example 20
Source File: ProfileActivity.java    From mage-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onMapReady(GoogleMap map) {
	SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
	map.setMapType(preferences.getInt(getString(R.string.baseLayerKey), getResources().getInteger(R.integer.baseLayerDefaultValue)));

	int dayNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
	if (dayNightMode == Configuration.UI_MODE_NIGHT_NO) {
		map.setMapStyle(null);
	} else {
		map.setMapStyle(MapStyleOptions.loadRawResourceStyle(getApplicationContext(), R.raw.map_theme_night));
	}


	if (latLng != null && icon != null) {
		map.addMarker(new MarkerOptions()
			.position(latLng)
			.icon(icon));

		LocationProperty accuracyProperty = location.getPropertiesMap().get("accuracy");
		if (accuracyProperty != null) {
			float accuracy = Float.parseFloat(accuracyProperty.getValue().toString());

			int color = LocationBitmapFactory.locationColor(getApplicationContext(), location);
			map.addCircle(new CircleOptions()
				.center(latLng)
				.radius(accuracy)
				.fillColor(ColorUtils.setAlphaComponent(color, (int) (256 * .20)))
				.strokeColor(ColorUtils.setAlphaComponent(color, (int) (256 * .87)))
				.strokeWidth(2.0f));

			double latitudePadding = (accuracy / 111325);
			LatLngBounds bounds = new LatLngBounds(
					new LatLng(latLng.latitude - latitudePadding, latLng.longitude),
					new LatLng(latLng.latitude + latitudePadding, latLng.longitude));

			int minDimension = Math.min(mapView.getWidth(), mapView.getHeight());
			int padding = (int) Math.floor(minDimension / 5f);
			map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
		} else {
			map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));
		}
	}
}