Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
Sync with latest common lib (implemented UserPrefs wrapper)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-- committed Jan 16, 2013
1 parent 75783bd commit b342009
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 53 deletions.
14 changes: 7 additions & 7 deletions res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
android:title="@string/auto_frame_skip"
android:defaultValue="true"/>

<com.androidemu.SeekBarPreference
<com.androidemu.persistent.SeekBarPreference
android:key="maxFrameSkips"
android:title="@string/max_frame_skips"
android:summary="@string/max_frame_skips_summary"
Expand All @@ -36,11 +36,11 @@
android:title="@string/game_key_bindings"/>

<PreferenceCategory android:title="@string/shortcut_keys">
<com.androidemu.GameKeyPreference
<com.androidemu.persistent.GameKeyPreference
android:key="quickLoad"
android:title="@string/quick_load"
android:defaultValue="0"/>
<com.androidemu.GameKeyPreference
<com.androidemu.persistent.GameKeyPreference
android:key="quickSave"
android:title="@string/quick_save"
android:defaultValue="0"/>
Expand All @@ -65,7 +65,7 @@
<CheckBoxPreference android:key="dpad4Way"
android:title="@string/dpad_4way"/>

<com.androidemu.SeekBarPreference
<com.androidemu.persistent.SeekBarPreference
android:key="dpadDeadZone"
android:title="@string/dpad_deadzone"
androidemu:maxValue="4"
Expand All @@ -80,7 +80,7 @@
android:title="@string/point_size_press"
android:summary="@string/point_size_press_summary"/>

<com.androidemu.SeekBarPreference
<com.androidemu.persistent.SeekBarPreference
android:key="pointSizePressThreshold"
android:title="@string/point_size_press_threshold"
android:dependency="pointSizePress"
Expand All @@ -95,7 +95,7 @@
android:entryValues="@array/vkeypad_size_entryvalues"
android:defaultValue="medium"/>

<com.androidemu.SeekBarPreference
<com.androidemu.persistent.SeekBarPreference
android:key="vkeypadTransparency"
android:title="@string/vkeypad_transparency"
android:defaultValue="50"
Expand All @@ -109,7 +109,7 @@
android:entryValues="@array/vkeypad_layout_entryvalues"
android:defaultValue="top_bottom"/>

<com.androidemu.SeekBarPreference
<com.androidemu.persistent.SeekBarPreference
android:key="layoutMargin"
android:title="@string/layout_margin"
androidemu:maxValue="5"
Expand Down
70 changes: 28 additions & 42 deletions src/com/androidemu/EmulatorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@
import java.io.File;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;

import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

import android.widget.Toast;

import com.androidemu.FileChooser;
Expand All @@ -27,6 +21,7 @@
import com.androidemu.gba.Emulator;
import com.androidemu.gba.EmulatorView;
import com.androidemu.gba.GamePreferences;
import com.androidemu.gba.UserPrefs;
import com.androidemu.gba.input.GameKeyListener;
import com.androidemu.gba.input.Keyboard;
import com.androidemu.gba.input.Keycodes;
Expand Down Expand Up @@ -65,19 +60,16 @@ public class EmulatorActivity extends GameActivity implements GameKeyListener,
private boolean isMenuShowing;
private int quickLoadKey;
private int quickSaveKey;

private UserPrefs cfg;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

if (Wrapper.SDK_INT < 11)
{
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}
cfg = new UserPrefs(getApplication().getApplicationContext());

setVolumeControlStream(AudioManager.STREAM_MUSIC);

File datadir = getDir("data", MODE_PRIVATE);
if (!initEmulator(datadir))
{
Expand All @@ -102,21 +94,21 @@ protected void onCreate(Bundle savedInstanceState)
extractAsset(new File(datadir, "game_config.txt"));

// load settings
lastPickedGame = cfg.getString("lastPickedGame", null);
lastPickedGame = cfg.lastPickedGame;
loadGlobalSettings();

// restore state if any
if (savedInstanceState != null)
currentGame = savedInstanceState.getString("currentGame");

// load BIOS
if (loadBIOS(cfg.getString("bios", null)))
if (loadBIOS(cfg.bios))
{
// restore last running game
String last = cfg.getString("lastRunningGame", null);
String last = cfg.lastRunningGame;
if (last != null)
{
saveLastRunningGame(null);
cfg.setLastRunningGame(null);
if (new File(getGameStateFile(last, 0)).exists() && loadROM(last, false))
quickLoad();
}
Expand All @@ -125,6 +117,13 @@ protected void onCreate(Bundle savedInstanceState)
showPlaceholder();
}

protected void initResources()
{
super.initResources();

cfg = new UserPrefs(getApplicationContext());
}

@Override
protected void onDestroy()
{
Expand Down Expand Up @@ -157,9 +156,7 @@ protected void onStop()
{
super.onStop();

SharedPreferences.Editor editor = cfg.edit();
editor.putString("lastPickedGame", lastPickedGame);
editor.commit();
cfg.setLastPickedGame(lastPickedGame);
}

@Override
Expand Down Expand Up @@ -442,42 +439,33 @@ private static int getScalingMode(String mode)
return EmulatorView.SCALING_STRETCH;
}

private void saveLastRunningGame(String game)
{
SharedPreferences.Editor editor = cfg.edit();
editor.putString("lastRunningGame", game);
editor.commit();
}

private void loadGlobalSettings()
{
pauseEmulator();

emulator.setOption("autoFrameSkip", cfg.getBoolean("autoFrameSkip", true));
emulator.setOption("maxFrameSkips", Integer.toString(cfg.getInt("maxFrameSkips", 2)));
emulator.setOption("autoFrameSkip", cfg.autoFrameSkip);
emulator.setOption("maxFrameSkips", cfg.maxFrameSkips);

emulator.setOption("soundEnabled", cfg.getBoolean("soundEnabled", res.getBoolean(R.bool.def_soundEnabled)));
emulator.setOption("soundEnabled", cfg.soundEnabled);

trackball.setEnabled(cfg.getBoolean("enableTrackball", res.getBoolean(R.bool.def_hasTrackball)));
trackball.setEnabled(cfg.enableTrackball);

keypad.setVisibility(cfg.getBoolean("enableVirtualKeypad", res.getBoolean(R.bool.def_useTouch)) ? View.VISIBLE : View.GONE);
keypad.setVisibility(cfg.enableVirtualKeypad ? View.VISIBLE : View.GONE);

emulatorView.setScalingMode(getScalingMode(cfg.getString("scalingMode", res.getString(R.string.def_scalingMode))));
emulatorView.setScalingMode(getScalingMode(cfg.scalingMode));

// key bindings
final int[] gameKeys = GamePreferences.gameKeys;
final String[] prefKeys = GamePreferences.keyPrefKeys;
final int[] defaultKeys = GamePreferences.getDefaultKeys(this);

keyboard.clearKeyMap();
for (int i = 0; i < prefKeys.length; i++)
for (int i = 0; i < gameKeys.length; i++)
{
keyboard.mapKey(gameKeys[i], cfg.getInt(prefKeys[i], defaultKeys[i]));
keyboard.mapKey(gameKeys[i], cfg.keysMap[i]);
}

// shortcut keys
quickLoadKey = cfg.getInt("quickLoad", 0);
quickSaveKey = cfg.getInt("quickSave", 0);
quickLoadKey = cfg.quickLoad;
quickSaveKey = cfg.quickSave;

resumeEmulator();
}
Expand Down Expand Up @@ -538,7 +526,7 @@ public void onClick(DialogInterface dialog, int which)
break;
case 1:
quickSave();
saveLastRunningGame(currentGame);
cfg.setLastRunningGame(currentGame);
// fall through
case 2:
finish();
Expand Down Expand Up @@ -566,9 +554,7 @@ private boolean loadBIOS(String name)
{
if (name != null && emulator.loadBIOS(name))
{
SharedPreferences.Editor editor = cfg.edit();
editor.putString("bios", name);
editor.commit();
cfg.setBIOS(name);
return true;
}

Expand Down
6 changes: 2 additions & 4 deletions src/com/androidemu/gba/GamePreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import android.preference.PreferenceManager;
import android.view.KeyEvent;

import com.androidemu.CommonPreferences;
import com.androidemu.GameKeyPreference;
import com.androidemu.gba.input.Keycodes;

public class GamePreferences extends CommonPreferences implements
public class GamePreferences extends com.androidemu.persistent.CommonPreferences implements
Preference.OnPreferenceChangeListener
{
public static final int[] gameKeys = { Keycodes.GAMEPAD_UP, Keycodes.GAMEPAD_DOWN,
Expand Down Expand Up @@ -85,7 +83,7 @@ protected void onCreate(Bundle savedInstanceState)
int[] defaultKeys = getDefaultKeys(this);
for (int i = 0; i < keyPrefKeys.length; i++)
{
GameKeyPreference pref = new GameKeyPreference(this);
com.androidemu.persistent.GameKeyPreference pref = new com.androidemu.persistent.GameKeyPreference(this);
pref.setKey(keyPrefKeys[i]);
pref.setTitle(keyDisplayNames[i]);
pref.setDefaultValue(defaultKeys[i]);
Expand Down
76 changes: 76 additions & 0 deletions src/com/androidemu/gba/UserPrefs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.androidemu.gba;

import android.content.Context;
import android.content.SharedPreferences;

public class UserPrefs extends com.androidemu.persistent.UserPrefs
{
public final String bios;
public final String lastRunningGame;
public final String lastPickedGame;
public final boolean autoFrameSkip;
public final String maxFrameSkips;
public final boolean soundEnabled;
public final boolean enableTrackball;
public final boolean enableVirtualKeypad;
public final String scalingMode;
public final int quickLoad;
public final int quickSave;
public int[] keysMap = new int[128];

public UserPrefs(Context context)
{
super(context);

bios = prefsData.getString("bios", null);

lastRunningGame = prefsData.getString("lastRunningGame", null);

lastPickedGame = prefsData.getString("lastPickedGame", null);

autoFrameSkip = prefsData.getBoolean("autoFrameSkip", true);

maxFrameSkips = Integer.toString(prefsData.getInt("maxFrameSkips", 2));

soundEnabled = prefsData.getBoolean("soundEnabled", res.getBoolean(R.bool.def_soundEnabled));

enableTrackball = prefsData.getBoolean("enableTrackball", res.getBoolean(R.bool.def_hasTrackball));

enableVirtualKeypad = prefsData.getBoolean("enableVirtualKeypad", res.getBoolean(R.bool.def_useTouch));

scalingMode = prefsData.getString("scalingMode", res.getString(R.string.def_scalingMode));

quickLoad = prefsData.getInt("quickLoad", 0);

quickSave = prefsData.getInt("quickSave", 0);

final String[] prefKeys = GamePreferences.keyPrefKeys;
final int[] defaultKeys = GamePreferences.getDefaultKeys(context);

for (int i = 0; i < prefKeys.length; i++)
{
keysMap[i] = prefsData.getInt(prefKeys[i], defaultKeys[i]);
}
}

public void setLastPickedGame(String lastPickedGame)
{
SharedPreferences.Editor editor = prefsData.edit();
editor.putString("lastPickedGame", lastPickedGame);
editor.commit();
}

public void setLastRunningGame(String lastRunningGame)
{
SharedPreferences.Editor editor = prefsData.edit();
editor.putString("lastRunningGame", lastRunningGame);
editor.commit();
}

public void setBIOS(String bios)
{
SharedPreferences.Editor editor = prefsData.edit();
editor.putString("bios", bios);
editor.commit();
}
}

0 comments on commit b342009

Please sign in to comment.