how the setting of AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM relate to result of getResources().getConfiguration().uiMode - android-night-mode

when using
getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK
to check what mode the app is currently in,
int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're in day time
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
}
if called this with AppCompatDelegate.MODE_NIGHT_YES earlier
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
is the return of currentNightMode to be Configuration.UI_MODE_NIGHT_YES?
what it would return when the AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM was set before
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
and the device has changed from light to dark them (or from dark to light)?

context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
tells current what mode the app will be in.
when
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
if change the system theme in settings (in Android Q),
the configuration.uiMode will reflect the change.
same with the
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
or
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
note: the configuration.uiMode change will trigger a config change and may cause recreate the activity.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
themeSystem.setVisibility(View.VISIBLE);
} else {
themeSystem.setVisibility(View.GONE);
}
switch (sharePref.getTheme()) {
case THEME_LIGHT:
themeLight.setChecked(true);
break;
case THEME_DARK:
themeDark.setChecked(true);
break;
case THEME_SYSTEM:
themeSystem.setChecked(true);
break;
default:
switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
case Configuration.UI_MODE_NIGHT_NO:
themeLight.setChecked(true);
break;
case Configuration.UI_MODE_NIGHT_YES:
themeDark.setChecked(true);
break;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
themeLight.setChecked(true);
break;
}
}
themeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.themeLight:
setTheme(AppCompatDelegate.MODE_NIGHT_NO, THEME_LIGHT);
break;
case R.id.themeDark:
setTheme(AppCompatDelegate.MODE_NIGHT_YES, THEME_DARK);
break;
case R.id.themeSystem:
setTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, THEME_SYSTEM);
break;
}
}
});

Related

Switch Case within a Switch Case - Is using fall through cases followed by a new switch case of the same fall through a bad design philosophy? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am designing a system where some different states will have the same initial actions, but then depending on what the inital state is, the next state will be different.
I was thinking of using a switch case fallthrough and then using another switch case to determine the end state as follows :
switch (State)
{
case State1:
case State2:
case State3:
//Same actions for all 3 states
UART_Transmit(&WakeUp);
UART_Receive(&WakeUpResponse);
if (WakeUpResponse != WAKEUP_RESPONSE)
{
//We retry the ACk -> Ack Response
UART_Transmit(&WakeUp);
UART_Receive(&WakeUpResponse);
if (WakeUpResponse != WAKEUP_RESPONSE)
{
UART.State = NewState1;
}
}
//at this point, if WakeUpResponse == WAKEUP_RESPONSE
//then depending on the initial state, a new state is selected
if (WakeUpResponse == WAKEUP_RESPONSE)
{
//New switch case with the same States as above
switch(State)
{
case State1:
UART.State = NewState2;
break;
case State2:
UART.State = NewState3;
break;
case State3:
UART.State = NewState4;
break;
default:
break;
}
State4:
//Actions
break;
//other cases
}
Is this a bad way to go about this situation? I know it's doable strictly coding wise, but I am looking for inputs about maintainability of the code or any other quirks that could come out of that situation.
Thank you for your help.
You can rewrite this as:
static void StateHelper(SomeType NewState)
{
UART_Transmit(&WakeUp);
UART_Receive(&WakeUpResponse);
if (WakeUpResponse != WAKEUP_RESPONSE)
{
//We retry the ACk -> Ack Response
UART_Transmit(&WakeUp);
UART_Receive(&WakeUpResponse);
if (WakeUpResponse != WAKEUP_RESPONSE)
{
UART.State = NewState1;
}
}
//at this point, if WakeUpResponse == WAKEUP_RESPONSE
//then depending on the initial state, a new state is selected
if (WakeUpResponse == WAKEUP_RESPONSE)
UART.State = NewState;
}
…
switch (State)
{
case State1:
StateHelper(NewState2);
break;
case State2:
StateHelper(NewState3);
break;
case State3:
StateHelper(NewState4);
break;
case State4:
//Actions
break;
//other cases
}
Further, if the state values are small or consecutive or nearly so, it may be desirable to create an array NewStateLookup so that it can be written something like:
switch (State)
{
case State1:
case State2:
case State3:
StateHelper(NewStateLookup[State]);
break;
case State4:
//Actions
break;
//other cases
}

GTK: "key-press-event" handling while Shift is pressed

I'm writing a small program: it's a lone drop-down menu with lowercase letters as menuitem labels:
If you hold Shift, the labels become capitalized (I wrote a "key-press-event" and "key-release-event" handler for this). The problem is that while Shift is pressed, I still want to navigate the menu and choose items with Enter press. The default handlers aren't get triggered if some modifier is pressed, so I handle it in the following way:
static gboolean menu_key_event(GtkWidget *menu, GdkEvent *event, gpointer data) {
(void)data;
GdkEventKey *key_event = (GdkEventKey*)event;
switch (key_event->keyval) {
case GDK_KEY_Shift_L:
case GDK_KEY_Shift_R: ;
bool b = (key_event->type == GDK_KEY_PRESS) ? true : false;
gtk_container_foreach(GTK_CONTAINER(menu), menuitem_capitalize_label, &b);
return TRUE;
break;
case GDK_KEY_Return:
if ((key_event->type == GDK_KEY_PRESS) &&
(key_event->state & GDK_SHIFT_MASK)) {
// I want default callback to handle this
g_signal_emit_by_name(menu, "activate-current");
return TRUE;
}
break;
case GDK_KEY_Up:
case GDK_KEY_Down:
if ((key_event->type == GDK_KEY_PRESS) &&
(key_event->state & GDK_SHIFT_MASK)) {
// Some function I wrote to fiddle with menu items,
// simulating default selection behavior
menu_rotate_selection(GTK_MENU_SHELL(menu), key_event->keyval);
return TRUE;
}
break;
}
return FALSE;
}
Could this be done in a more elegant fashion? In short, I want my application handle Enter, arrow keys and Shift+Enter, Shift+ arrow keys the same way without needing to manually process it.
I've finally found needed signal to select menuitems ("move-current"), so my own menu_rotate_selection function is no longer needed. That signal name is confusing though, I'd rather think its purpose to actually move the menuitem itself whithin the menu (first I thought the other obscure-named signal "cycle-focus" is for changing selection). Now it can be rewriten as following:
...
case GDK_KEY_Up:
case GDK_KEY_Down:
if ((key_event->type == GDK_KEY_PRESS) &&
(key_event->state & GDK_SHIFT_MASK)) {
GtkMenuDirectionType dir = (key_event->keyval == GDK_KEY_Up) ?
GTK_MENU_DIR_PREV : GTK_MENU_DIR_NEXT;
g_signal_emit_by_name(menu, "move-current", dir);
return TRUE;
}
break;
...
This pretty much answers my question.

Is there a way to change local variables partway through switch case without exiting the switch statement?

I'm dealing with a rather large enumeration, which can conceptually be divided as representing four different categories. Currently I'm using this enumeration in a switch case statement in order to map values into four separate arrays (in line with the four different categories).
What I'm curious about is if it is possible to change a locally defined variable after arbitrary cases in the switch case statement. This would allow for the ability to break the switch statement into these four different sections, and value assignments that would occur for each case -- if equivalent -- can occur at these sections.
A simplified example what I'm going for is as follows:
Setup
enum incidental_indexes {
arr1_0, arr1_2, arr2_0, arr1_1, arr2_1
} indexes;
struct foobar{
int arr1[3];
int arr2[2];
}
enum indexes unknown_index = ???; // In my code there are two separate indexes being mapped
// from one another, so for the sake of example imagine that
// this index is unknown
enum indexes curr_index = arr1_1; //Value here does not matter
struct foobar my_struc;
int * curr_arr;
int mapped_index;
Brute force approach
switch(unknown_index){
case(arr1_0):
curr_arr = my_struc.arr_1; //First category array
curr_index = arr1_0;
break;
case(arr1_1):
curr_arr = my_struc.arr_1; //First category array, again
curr_index = arr1_1;
break;
case(arr1_2):
curr_arr = my_struc.arr_1; //First category array, again, again
curr_index = arr1_2;
break;
case(arr2_0):
curr_index = arr2_0;
curr_arr = my_struc.arr_2; //Second category array
break;
case(arr2_1):
curr_index = arr2_1;
curr_arr = my_struc.arr_2; //....
break;
}
Ideal Approach
switch(unknown_index){
default: //Notice no break.
curr_arr = my_struc.arr_1; //First category array
case(arr1_0):
curr_index = arr1_0;
break;
case(arr1_1):
curr_index = arr1_1;
break;
case(arr1_2):
curr_index = arr1_2;
break;
default: //Something like a second default, however disallowed
curr_arr = my_struc.arr_2; //Second category array
case(arr2_0):
curr_index = arr2_0;
break;
case(arr2_1):
curr_index = arr2_1;
break;
}
The functional benefits are obviously nill, however I'm curious if this functionality even exists in C, or if there is perhaps a better method for executing this.
Thanks!
Switch statements only perform a single branch, so you can't jump around inside of the switch like that. What you can do however is group certain cases together without a break in between:
switch(curr_index){
case arr1_0:
case arr1_1:
case arr1_2:
curr_arr = my_struc.arr_1;
break;
case arr2_0:
case arr2_1:
curr_arr = my_struc.arr_2;
break;
}
EDIT:
For the index assignment part, you could do a second switch like this:
switch(unknown_index){
case arr1_0:
curr_index = arr1_0;
break;
case arr1_1:
curr_index = arr1_1;
break;
case arr1_2:
curr_index = arr1_2;
break;
case arr2_0:
curr_index = arr2_0;
break;
case arr2_1:
curr_index = arr2_1;
break;
}
But since you're always assigning whatever the value of unknown_index is, the above is the same as this:
curr_index = unknown_index;
One, no.
Two, just use ifs and elses. As the saying goes, when you have a hammer, everything looks like a nail. switch is a really weird "hammer" to try applying to everything.
Three, um, I guess you could use goto everywhere, but we decided this was a bad idea and creates horribly messes of code in the 80s or something.
Within a switch you have access to all the locally defined variables. I'm not quite sure I understand your question... it seems like what you're trying to do is best accomplished by 2 switches:
switch(unknown_index){
case(arr1_0):
case(arr1_1):
case(arr1_2):
curr_arr = my_struc.arr_1; //First category array, again, again
break;
case(arr2_0):
case(arr2_1):
curr_arr = my_struc.arr_2; //....
break;
}
switch(unknown_index){
case(arr1_0):
curr_index = arr1_0;
break;
case(arr1_1):
curr_index = arr1_1;
break;
case(arr1_2):
curr_index = arr1_2;
break;
case(arr2_0):
curr_index = arr2_0;
break;
case(arr2_1):
curr_index = arr2_1;
break;
}

Any way to programmatically get the FPS of a video?

I am currently working in this problem for hours now. I have to create a program that when user gets a video from a child window which accesses your hard disk drives, I have to get the frame rate and other properties from that video.
Here's a sample code of how I'm getting the videos and some of their properties.
SelectDirectoryWindow selectDirectoryWindow = (sender as SelectDirectoryWindow);
if (selectDirectoryWindow.DialogResult.GetValueOrDefault(false))
{
foreach (System.IO.FileInfo fileInfo in selectDirectoryWindow.VideoFiles)
{
VideoFileInfo videoFileInfo = new VideoFileInfo();
videoFileInfo.FileName = fileInfo.Name;
videoFileInfo.Path = fileInfo.FullName;
videoFileInfo.Extension = fileInfo.Extension;
videoFileInfo.FileSize = fileInfo.Length;
switch (videoFileInfo.Extension.ToUpper())
{
case ".WMV":
videoFileInfo.VideoFileType = Constants.VideoFileType.Wmv;
break;
case ".MOV":
videoFileInfo.VideoFileType = Constants.VideoFileType.ProResHq;
break;
case ".MPG":
videoFileInfo.VideoFileType = Constants.VideoFileType.Mpeg2;
break;
case ".ISM":
videoFileInfo.VideoFileType = Constants.VideoFileType.SmoothStreaming;
break;
case ".MP4":
videoFileInfo.VideoFileType = Constants.VideoFileType.iPad;
break;
default:
break;
}
Is there any way I can also get the frame rate, video duration and bit rate from this? What can I do to get the frame rate and bit rate? Thanks in advance.
I have found the answer. There is a ShellFile class on the Microsoft.WindowsAPICodePack.Shell. In there you can get the properties of the video, just give it the source of the file(filepath). And you can get anything from there.
Here's how I got the Frame Rate.
ShellFile shellFile = ShellFile.FromFilePath(sourceFile);
return (shellFile.Properties.System.Video.FrameRate.Value / 1000).ToString();

c ideas on shrinking this function?

I have this huge switch case with nested switch case statements in it that I was wondering if anyone had any ideas on how to clean up?
switch (datatype) {
case STR:
{
switch(operation)
{
case EQUALS:
{
/* perform str compare */
}
case NOTEQUALS:
{
}
case LT:
{
}
case GT:
{
}
case GTE:
{
}
case LTE:
{
}
default:
break;
}
break;
}
case VER:
{
switch(operation)
{
case EQUALS:
{
/* perform version compare */
}
case NOTEQUALS:
{
}
case LT:
{
}
case GT:
{
}
case GTE:
{
}
case LTE:
{
}
default:
break;
}
break;
}
case INT:
{
/* same */
}
case FLOAT:
{
/* same */
}
/* ... more types ... */
/* ... .......... ... */
default:
break;
}
If the values for the operation are contiguous, you could make a table of function pointers. In fact you could make a 2D table of function pointers with a separate function to handle each operation/type combination. e.g
// do some range checking on input params
// invoke the handler
handlers[datatype][operation]();
Create some tables (arrays) with pointers to functions in it. You can then look up func[STR][EQUALS] to make the appropriate call. The call would end up looking like this...
Func[datatype][operation]();
You could try the command pattern.
The NOTEQUALS case can always be written in terms of the EQUALS case; similarly GTE in terms of LT and LTE in terms of GE. So make the outer switch in terms of the operation, and only three of those six cases will need to switch on the datatype.
You could use a big array of function pointers and then call the relevant function based upon indexing to the correct function pointer in the array.
have you considered creatively using a series of function pointers and storing them in a struct?
do it right and you can mimic objects and do something this like:
bool someArbitraryFunction (dataType aDatatype, operations anOperation)
{
someUnknownStruct.datatype = aDatatype;
someUnknownStruct.operation = anOperation;
return someUnknownStruct->doMath(1,2);
}
and then you can put all the needed math functions, enums, and structs in a header file somewhere.
cleans up the "meat" of the code, and makes the math portable (just import it wherever you need it).
Assuming your cases can all return a simple boolean value, all six logic cases can be rewritten in terms of LT and EQUALS, like follows:
switch(operation) {
case EQUALS:
return isEquals();
case NOTEQUALS:
return !isEquals();
case LT:
return isLT();
case GT:
return !isLT() && !isEquals();
case GTE:
return !isLT();
case LTE:
reutrn isLT() || isEquals();
default:
break;
}
This would only require you to write the logic for isLT() and isEquals(), which would do the switching on datatype when necessary. This eliminates a significant amount of unnecessary code duplication, yet doesn't sacrifice a lot of legibility.
This can be combined with function pointers as Stephen Doyle and rikh already suggested, which would eliminate the switch() statement entirely.

Resources