Pull preferences into their own class

This commit is contained in:
Cameron Gutman 2014-11-02 13:05:17 -08:00
parent c1397e331b
commit 2f4042da8f
4 changed files with 141 additions and 63 deletions

View File

@ -14,14 +14,13 @@ import com.limelight.nvstream.StreamConfiguration;
import com.limelight.nvstream.av.video.VideoDecoderRenderer; import com.limelight.nvstream.av.video.VideoDecoderRenderer;
import com.limelight.nvstream.input.KeyboardPacket; import com.limelight.nvstream.input.KeyboardPacket;
import com.limelight.nvstream.input.MouseButtonPacket; import com.limelight.nvstream.input.MouseButtonPacket;
import com.limelight.R; import com.limelight.preferences.PreferenceConfiguration;
import com.limelight.utils.Dialog; import com.limelight.utils.Dialog;
import com.limelight.utils.SpinnerDialog; import com.limelight.utils.SpinnerDialog;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point; import android.graphics.Point;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
@ -58,8 +57,7 @@ public class Game extends Activity implements SurfaceHolder.Callback,
private ControllerHandler controllerHandler; private ControllerHandler controllerHandler;
private KeyboardTranslator keybTranslator; private KeyboardTranslator keybTranslator;
private int height; private PreferenceConfiguration prefConfig;
private int width;
private Point screenSize = new Point(0, 0); private Point screenSize = new Point(0, 0);
private NvConnection conn; private NvConnection conn;
@ -68,9 +66,6 @@ public class Game extends Activity implements SurfaceHolder.Callback,
private boolean connecting = false; private boolean connecting = false;
private boolean connected = false; private boolean connected = false;
private boolean stretchToFit;
private boolean toastsDisabled;
private EvdevWatcher evdevWatcher; private EvdevWatcher evdevWatcher;
private int modifierFlags = 0; private int modifierFlags = 0;
private boolean grabbedInput = true; private boolean grabbedInput = true;
@ -87,35 +82,6 @@ public class Game extends Activity implements SurfaceHolder.Callback,
public static final String EXTRA_UNIQUEID = "UniqueId"; public static final String EXTRA_UNIQUEID = "UniqueId";
public static final String EXTRA_STREAMING_REMOTE = "Remote"; public static final String EXTRA_STREAMING_REMOTE = "Remote";
public static final String PREFS_FILE_NAME = "gameprefs";
public static final String WIDTH_PREF_STRING = "ResH";
public static final String HEIGHT_PREF_STRING = "ResV";
public static final String REFRESH_RATE_PREF_STRING = "FPS";
public static final String DECODER_PREF_STRING = "Decoder";
public static final String BITRATE_PREF_STRING = "Bitrate";
public static final String STRETCH_PREF_STRING = "Stretch";
public static final String SOPS_PREF_STRING = "Sops";
public static final String DISABLE_TOASTS_PREF_STRING = "NoToasts";
public static final int BITRATE_DEFAULT_720_30 = 5;
public static final int BITRATE_DEFAULT_720_60 = 10;
public static final int BITRATE_DEFAULT_1080_30 = 10;
public static final int BITRATE_DEFAULT_1080_60 = 30;
public static final int DEFAULT_WIDTH = 1280;
public static final int DEFAULT_HEIGHT = 720;
public static final int DEFAULT_REFRESH_RATE = 60;
public static final int DEFAULT_DECODER = 0;
public static final int DEFAULT_BITRATE = BITRATE_DEFAULT_720_60;
public static final boolean DEFAULT_STRETCH = false;
public static final boolean DEFAULT_SOPS = true;
public static final boolean DEFAULT_DISABLE_TOASTS = false;
public static final int FORCE_HARDWARE_DECODER = -1;
public static final int AUTOSELECT_DECODER = 0;
public static final int FORCE_SOFTWARE_DECODER = 1;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -152,32 +118,22 @@ public class Game extends Activity implements SurfaceHolder.Callback,
spinner = SpinnerDialog.displayDialog(this, "Establishing Connection", "Starting connection", true); spinner = SpinnerDialog.displayDialog(this, "Establishing Connection", "Starting connection", true);
// Read the stream preferences // Read the stream preferences
SharedPreferences prefs = getSharedPreferences(PREFS_FILE_NAME, Context.MODE_MULTI_PROCESS); prefConfig = PreferenceConfiguration.readPreferences(this);
switch (prefs.getInt(Game.DECODER_PREF_STRING, Game.DEFAULT_DECODER)) { switch (prefConfig.decoder) {
case Game.FORCE_SOFTWARE_DECODER: case PreferenceConfiguration.FORCE_SOFTWARE_DECODER:
drFlags |= VideoDecoderRenderer.FLAG_FORCE_SOFTWARE_DECODING; drFlags |= VideoDecoderRenderer.FLAG_FORCE_SOFTWARE_DECODING;
break; break;
case Game.AUTOSELECT_DECODER: case PreferenceConfiguration.AUTOSELECT_DECODER:
break; break;
case Game.FORCE_HARDWARE_DECODER: case PreferenceConfiguration.FORCE_HARDWARE_DECODER:
drFlags |= VideoDecoderRenderer.FLAG_FORCE_HARDWARE_DECODING; drFlags |= VideoDecoderRenderer.FLAG_FORCE_HARDWARE_DECODING;
break; break;
} }
stretchToFit = prefs.getBoolean(STRETCH_PREF_STRING, DEFAULT_STRETCH); if (prefConfig.stretchVideo) {
if (stretchToFit) {
drFlags |= VideoDecoderRenderer.FLAG_FILL_SCREEN; drFlags |= VideoDecoderRenderer.FLAG_FILL_SCREEN;
} }
int refreshRate, bitrate;
boolean sops;
width = prefs.getInt(WIDTH_PREF_STRING, DEFAULT_WIDTH);
height = prefs.getInt(HEIGHT_PREF_STRING, DEFAULT_HEIGHT);
refreshRate = prefs.getInt(REFRESH_RATE_PREF_STRING, DEFAULT_REFRESH_RATE);
bitrate = prefs.getInt(BITRATE_PREF_STRING, DEFAULT_BITRATE);
sops = prefs.getBoolean(SOPS_PREF_STRING, DEFAULT_SOPS);
toastsDisabled = prefs.getBoolean(DISABLE_TOASTS_PREF_STRING, DEFAULT_DISABLE_TOASTS);
Display display = getWindowManager().getDefaultDisplay(); Display display = getWindowManager().getDefaultDisplay();
display.getSize(screenSize); display.getSize(screenSize);
@ -203,8 +159,8 @@ public class Game extends Activity implements SurfaceHolder.Callback,
decoderRenderer.initializeWithFlags(drFlags); decoderRenderer.initializeWithFlags(drFlags);
StreamConfiguration config = StreamConfiguration config =
new StreamConfiguration(app, width, height, new StreamConfiguration(app, prefConfig.width, prefConfig.height,
refreshRate, bitrate * 1000, sops, prefConfig.fps, prefConfig.bitrate * 1000, prefConfig.enableSops,
(decoderRenderer.getCapabilities() & (decoderRenderer.getCapabilities() &
VideoDecoderRenderer.CAPABILITY_ADAPTIVE_RESOLUTION) != 0); VideoDecoderRenderer.CAPABILITY_ADAPTIVE_RESOLUTION) != 0);
@ -214,9 +170,9 @@ public class Game extends Activity implements SurfaceHolder.Callback,
controllerHandler = new ControllerHandler(conn); controllerHandler = new ControllerHandler(conn);
SurfaceHolder sh = sv.getHolder(); SurfaceHolder sh = sv.getHolder();
if (stretchToFit || !decoderRenderer.isHardwareAccelerated()) { if (prefConfig.stretchVideo || !decoderRenderer.isHardwareAccelerated()) {
// Set the surface to the size of the video // Set the surface to the size of the video
sh.setFixedSize(width, height); sh.setFixedSize(prefConfig.width, prefConfig.height);
} }
// Initialize touch contexts // Initialize touch contexts
@ -643,8 +599,8 @@ public class Game extends Activity implements SurfaceHolder.Callback,
// Scale the deltas if the device resolution is different // Scale the deltas if the device resolution is different
// than the stream resolution // than the stream resolution
deltaX = (int)Math.round((double)deltaX * ((double)width / (double)screenSize.x)); deltaX = (int)Math.round((double)deltaX * ((double)prefConfig.width / (double)screenSize.x));
deltaY = (int)Math.round((double)deltaY * ((double)height / (double)screenSize.y)); deltaY = (int)Math.round((double)deltaY * ((double)prefConfig.height / (double)screenSize.y));
conn.sendMouseMove((short)deltaX, (short)deltaY); conn.sendMouseMove((short)deltaX, (short)deltaY);
} }
@ -739,7 +695,7 @@ public class Game extends Activity implements SurfaceHolder.Callback,
@Override @Override
public void displayTransientMessage(final String message) { public void displayTransientMessage(final String message) {
if (!toastsDisabled) { if (!prefConfig.disableWarnings) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -760,8 +716,9 @@ public class Game extends Activity implements SurfaceHolder.Callback,
// Resize the surface to match the aspect ratio of the video // Resize the surface to match the aspect ratio of the video
// This must be done after the surface is created. // This must be done after the surface is created.
if (!stretchToFit && decoderRenderer.isHardwareAccelerated()) { if (!prefConfig.stretchVideo && decoderRenderer.isHardwareAccelerated()) {
resizeSurfaceWithAspectRatio((SurfaceView) findViewById(R.id.surfaceView), width, height); resizeSurfaceWithAspectRatio((SurfaceView) findViewById(R.id.surfaceView),
prefConfig.width, prefConfig.height);
} }
conn.start(PlatformBinding.getDeviceName(), holder, drFlags, conn.start(PlatformBinding.getDeviceName(), holder, drFlags,

View File

@ -0,0 +1,122 @@
package com.limelight.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class PreferenceConfiguration {
private static final String RES_FPS_PREF_STRING = "list_resolution_fps";
private static final String DECODER_PREF_STRING = "list_decoders";
private static final String BITRATE_PREF_STRING = "seekbar_bitrate";
private static final String STRETCH_PREF_STRING = "checkbox_stretch_video";
private static final String SOPS_PREF_STRING = "checkbox_enable_sops";
private static final String DISABLE_TOASTS_PREF_STRING = "checkbox_disable_warnings";
private static final String HOST_AUDIO_PREF_STRING = "checkbox_host_audio";
private static final int BITRATE_DEFAULT_720_30 = 5;
private static final int BITRATE_DEFAULT_720_60 = 10;
private static final int BITRATE_DEFAULT_1080_30 = 10;
private static final int BITRATE_DEFAULT_1080_60 = 30;
private static final String DEFAULT_RES_FPS = "720p60";
private static final String DEFAULT_DECODER = "auto";
private static final int DEFAULT_BITRATE = BITRATE_DEFAULT_720_60;
private static final boolean DEFAULT_STRETCH = false;
private static final boolean DEFAULT_SOPS = true;
private static final boolean DEFAULT_DISABLE_TOASTS = false;
private static final boolean DEFAULT_HOST_AUDIO = false;
public static final int FORCE_HARDWARE_DECODER = -1;
public static final int AUTOSELECT_DECODER = 0;
public static final int FORCE_SOFTWARE_DECODER = 1;
public int width, height, fps;
public int bitrate;
public int decoder;
public boolean stretchVideo, enableSops, playHostAudio, disableWarnings;
public static int getDefaultBitrate(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String str = prefs.getString(RES_FPS_PREF_STRING, DEFAULT_RES_FPS);
if (str.equals("720p30")) {
return BITRATE_DEFAULT_720_30;
}
else if (str.equals("720p60")) {
return BITRATE_DEFAULT_720_60;
}
else if (str.equals("1080p30")) {
return BITRATE_DEFAULT_1080_30;
}
else if (str.equals("1080p60")) {
return BITRATE_DEFAULT_1080_60;
}
else {
// Should never get here
return DEFAULT_BITRATE;
}
}
private static int getDecoderValue(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String str = prefs.getString(DECODER_PREF_STRING, DEFAULT_DECODER);
if (str.equals("auto")) {
return AUTOSELECT_DECODER;
}
else if (str.equals("software")) {
return FORCE_SOFTWARE_DECODER;
}
else if (str.equals("hardware")) {
return FORCE_HARDWARE_DECODER;
}
else {
// Should never get here
return AUTOSELECT_DECODER;
}
}
public static PreferenceConfiguration readPreferences(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
PreferenceConfiguration config = new PreferenceConfiguration();
config.bitrate = prefs.getInt(BITRATE_PREF_STRING, getDefaultBitrate(context));
String str = prefs.getString(RES_FPS_PREF_STRING, DEFAULT_RES_FPS);
if (str.equals("720p30")) {
config.width = 1280;
config.height = 720;
config.fps = 30;
}
else if (str.equals("720p60")) {
config.width = 1280;
config.height = 720;
config.fps = 60;
}
else if (str.equals("1080p30")) {
config.width = 1920;
config.height = 1080;
config.fps = 30;
}
else if (str.equals("1080p60")) {
config.width = 1920;
config.height = 1080;
config.fps = 60;
}
else {
// Should never get here
config.width = 1280;
config.height = 720;
config.fps = 60;
}
config.decoder = getDecoderValue(context);
// Checkbox preferences
config.disableWarnings = prefs.getBoolean(DISABLE_TOASTS_PREF_STRING, DEFAULT_DISABLE_TOASTS);
config.enableSops = prefs.getBoolean(SOPS_PREF_STRING, DEFAULT_SOPS);
config.stretchVideo = prefs.getBoolean(STRETCH_PREF_STRING, DEFAULT_STRETCH);
config.playHostAudio = prefs.getBoolean(HOST_AUDIO_PREF_STRING, DEFAULT_HOST_AUDIO);
return config;
}
}

View File

@ -48,7 +48,7 @@ public class SeekBarPreference extends DialogPreference
} }
// Get default and max seekbar values // Get default and max seekbar values
defaultValue = attrs.getAttributeIntValue(SCHEMA_URL, "defaultValue", 0); defaultValue = PreferenceConfiguration.getDefaultBitrate(context);
maxValue = attrs.getAttributeIntValue(SCHEMA_URL, "max", 100); maxValue = attrs.getAttributeIntValue(SCHEMA_URL, "max", 100);
} }

View File

@ -11,7 +11,6 @@
android:defaultValue="720p60" /> android:defaultValue="720p60" />
<com.limelight.preferences.SeekBarPreference <com.limelight.preferences.SeekBarPreference
android:key="seekbar_bitrate" android:key="seekbar_bitrate"
android:defaultValue="50"
android:dialogMessage="@string/summary_seekbar_bitrate" android:dialogMessage="@string/summary_seekbar_bitrate"
android:max="100" android:max="100"
android:summary="@string/summary_seekbar_bitrate" android:summary="@string/summary_seekbar_bitrate"