Skip to content

Commit

Permalink
Android: Overhaul the orientation lock setting
Browse files Browse the repository at this point in the history
When using motion controls, it's useful to be able to lock the screen
to a certain orientation so that Android won't interpret game motions
as an intent to change the screen orientation. To this end, I've
changed the existing orientation lock setting in the following ways:

- A portrait lock mode has been added in addition to the existing
  landscape lock mode and unlocked mode.
- The landscape lock mode now locks to regular landscape rather than
  letting you change between the two possible landscape orientations.
- The setting is now accessed during emulation rather than outside.
  • Loading branch information
JosJuice committed Nov 20, 2019
1 parent a548489 commit 4d83821
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 24 deletions.
Expand Up @@ -24,6 +24,7 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
Expand Down Expand Up @@ -98,7 +99,8 @@ public final class EmulationActivity extends AppCompatActivity
MENU_ACTION_SAVE_SLOT6, MENU_ACTION_LOAD_SLOT1, MENU_ACTION_LOAD_SLOT2,
MENU_ACTION_LOAD_SLOT3, MENU_ACTION_LOAD_SLOT4, MENU_ACTION_LOAD_SLOT5,
MENU_ACTION_LOAD_SLOT6, MENU_ACTION_EXIT, MENU_ACTION_CHANGE_DISC,
MENU_ACTION_RESET_OVERLAY, MENU_SET_IR_SENSITIVITY, MENU_ACTION_CHOOSE_DOUBLETAP})
MENU_ACTION_RESET_OVERLAY, MENU_SET_IR_SENSITIVITY, MENU_ACTION_CHOOSE_DOUBLETAP,
MENU_ACTION_SCREEN_ORIENTATION})
public @interface MenuAction
{
}
Expand Down Expand Up @@ -132,6 +134,7 @@ public final class EmulationActivity extends AppCompatActivity
public static final int MENU_ACTION_RESET_OVERLAY = 26;
public static final int MENU_SET_IR_SENSITIVITY = 27;
public static final int MENU_ACTION_CHOOSE_DOUBLETAP = 28;
public static final int MENU_ACTION_SCREEN_ORIENTATION = 29;


private static SparseIntArray buttonsActionsMap = new SparseIntArray();
Expand Down Expand Up @@ -178,6 +181,8 @@ public final class EmulationActivity extends AppCompatActivity
EmulationActivity.MENU_SET_IR_SENSITIVITY);
buttonsActionsMap.append(R.id.menu_emulation_choose_doubletap,
EmulationActivity.MENU_ACTION_CHOOSE_DOUBLETAP);
buttonsActionsMap.append(R.id.menu_screen_orientation,
EmulationActivity.MENU_ACTION_SCREEN_ORIENTATION);
}

private static String[] scanForSecondDisc(GameFile gameFile)
Expand Down Expand Up @@ -253,9 +258,13 @@ protected void onCreate(Bundle savedInstanceState)
restoreState(savedInstanceState);
}

mPreferences = PreferenceManager.getDefaultSharedPreferences(this);

mSettings = new Settings();
mSettings.loadSettings(null);

updateOrientation();

// TODO: The accurate way to find out which console we're emulating is to
// first launch emulation and then ask the core which console we're emulating
sIsGameCubeGame = Platform.fromNativeInt(mPlatform) == Platform.GAMECUBE;
Expand Down Expand Up @@ -297,17 +306,6 @@ protected void onCreate(Bundle savedInstanceState)

setContentView(R.layout.activity_emulation);


BooleanSetting lockLandscapeSetting =
(BooleanSetting) mSettings.getSection(Settings.SECTION_INI_CORE)
.getSetting(SettingsFile.KEY_LOCK_LANDSCAPE);
boolean lockLandscape = lockLandscapeSetting == null || lockLandscapeSetting.getValue();
// Force landscape if set
if (mDeviceHasTouchScreen && lockLandscape)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}

// Find or create the EmulationFragment
mEmulationFragment = (EmulationFragment) getSupportFragmentManager()
.findFragmentById(R.id.frame_emulation_fragment);
Expand All @@ -323,9 +321,6 @@ protected void onCreate(Bundle savedInstanceState)
{
setTitle(mSelectedTitle);
}

mPreferences = PreferenceManager.getDefaultSharedPreferences(this);

}

@Override
Expand Down Expand Up @@ -431,6 +426,12 @@ private void enableFullscreenImmersive()
View.SYSTEM_UI_FLAG_IMMERSIVE);
}

private void updateOrientation()
{
setRequestedOrientation(mPreferences.getInt("emulationActivityOrientation",
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE));
}

private void toggleMenu()
{
boolean result = getSupportFragmentManager().popBackStackImmediate(
Expand Down Expand Up @@ -646,6 +647,10 @@ public void handleMenuAction(@MenuAction int menuAction)
chooseDoubleTapButton();
return;

case MENU_ACTION_SCREEN_ORIENTATION:
chooseOrientation();
return;

case MENU_ACTION_EXIT:
// ATV menu is built using a fragment, this will pop that fragment before emulation ends.
if (TvUtil.isLeanback(getApplicationContext()))
Expand Down Expand Up @@ -883,6 +888,37 @@ private void chooseController()
alertDialog.show();
}

private void chooseOrientation()
{
final int[] orientationValues = getResources().getIntArray(R.array.orientationValues);
int initialChoice = mPreferences.getInt("emulationActivityOrientation",
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int initialIndex = -1;
for (int i = 0; i < orientationValues.length; i++)
{
if (orientationValues[i] == initialChoice)
initialIndex = i;
}

final SharedPreferences.Editor editor = mPreferences.edit();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.emulation_screen_orientation);
builder.setSingleChoiceItems(R.array.orientationEntries, initialIndex,
(dialog, indexSelected) ->
{
int orientation = orientationValues[indexSelected];
editor.putInt("emulationActivityOrientation", orientation);
});
builder.setPositiveButton(getString(R.string.ok), (dialogInterface, i) ->
{
editor.apply();
updateOrientation();
});

AlertDialog alertDialog = builder.create();
alertDialog.show();
}

private void setIRSensitivity()
{
int ir_pitch = Integer.valueOf(
Expand Down
Expand Up @@ -224,7 +224,6 @@ private void addGeneralSettings(ArrayList<SettingsItem> sl)
Setting autoDiscChange = null;
Setting analytics = null;
Setting enableSaveState;
Setting lockToLandscape;

SettingSection coreSection = mSettings.getSection(Settings.SECTION_INI_CORE);
SettingSection analyticsSection = mSettings.getSection(Settings.SECTION_ANALYTICS);
Expand All @@ -238,7 +237,6 @@ private void addGeneralSettings(ArrayList<SettingsItem> sl)
autoDiscChange = coreSection.getSetting(SettingsFile.KEY_AUTO_DISC_CHANGE);
analytics = analyticsSection.getSetting(SettingsFile.KEY_ANALYTICS_ENABLED);
enableSaveState = coreSection.getSetting(SettingsFile.KEY_ENABLE_SAVE_STATES);
lockToLandscape = coreSection.getSetting(SettingsFile.KEY_LOCK_LANDSCAPE);

// TODO: Having different emuCoresEntries/emuCoresValues for each architecture is annoying.
// The proper solution would be to have one emuCoresEntries and one emuCoresValues
Expand Down Expand Up @@ -283,12 +281,6 @@ else if (defaultCpuCore == 4) // AArch64
sl.add(new CheckBoxSetting(SettingsFile.KEY_ENABLE_SAVE_STATES, Settings.SECTION_INI_CORE,
R.string.enable_save_states, R.string.enable_save_states_description, false,
enableSaveState));
if (!TvUtil.isLeanback(DolphinApplication.getAppContext()))
{
sl.add(new CheckBoxSetting(SettingsFile.KEY_LOCK_LANDSCAPE, Settings.SECTION_INI_CORE,
R.string.lock_emulation_landscape, R.string.lock_emulation_landscape_desc, true,
lockToLandscape));
}
sl.add(new CheckBoxSetting(SettingsFile.KEY_ANALYTICS_ENABLED, Settings.SECTION_ANALYTICS,
R.string.analytics, 0, false, analytics));
}
Expand Down
Expand Up @@ -52,7 +52,6 @@ public final class SettingsFile
public static final String KEY_SLOT_A_DEVICE = "SlotA";
public static final String KEY_SLOT_B_DEVICE = "SlotB";
public static final String KEY_ENABLE_SAVE_STATES = "EnableSaveStates";
public static final String KEY_LOCK_LANDSCAPE = "LockLandscape";

public static final String KEY_ANALYTICS_ENABLED = "Enabled";
public static final String KEY_ANALYTICS_PERMISSION_ASKED = "PermissionAsked";
Expand Down
5 changes: 5 additions & 0 deletions Source/Android/app/src/main/res/menu/menu_emulation.xml
Expand Up @@ -113,6 +113,11 @@
</menu>
</item>

<item
android:id="@+id/menu_screen_orientation"
app:showAsAction="never"
android:title="@string/emulation_screen_orientation"/>

<item
android:id="@+id/menu_change_disc"
app:showAsAction="never"
Expand Down
5 changes: 5 additions & 0 deletions Source/Android/app/src/main/res/menu/menu_emulation_wii.xml
Expand Up @@ -133,6 +133,11 @@
</menu>
</item>

<item
android:id="@+id/menu_screen_orientation"
app:showAsAction="never"
android:title="@string/emulation_screen_orientation"/>

<item
android:id="@+id/menu_change_disc"
app:showAsAction="never"
Expand Down
11 changes: 11 additions & 0 deletions Source/Android/app/src/main/res/values/arrays.xml
Expand Up @@ -348,4 +348,15 @@
<item>Wii Controller Settings</item>
<item>Clear Game Settings</item>
</string-array>

<string-array name="orientationEntries">
<item>Landscape</item>
<item>Portrait</item>
<item>Auto</item>
</string-array>
<integer-array name="orientationValues">
<item>0</item>
<item>1</item>
<item>-1</item>
</integer-array>
</resources>
1 change: 1 addition & 0 deletions Source/Android/app/src/main/res/values/strings.xml
Expand Up @@ -313,6 +313,7 @@
<string name="emulation_ir_group">Touch IR Pointer</string>
<string name="emulation_ir_sensitivity">IR Sensitivity</string>
<string name="emulation_choose_doubletap">Double tap button</string>
<string name="emulation_screen_orientation">Screen Orientation</string>

<!-- GC Adapter Menu-->
<string name="gc_adapter_rumble">Enable Vibration</string>
Expand Down

0 comments on commit 4d83821

Please sign in to comment.