/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga
Java source code (C) 2009-2011 Sergii Pylypenko
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
There is a license exception for this particular file (Settings.java) -
you may modify it as you wish without releasing source code,
as long as the libraries in the binary package you're distributing
(typically libapplication.so and other closed-source libraries in your .apk file)
can be linked and executed without error against the SDL compiled
from the original source code. This implies that you may not modify Java-to-C
interface, otherwise you have to publish the source code with your changes.
This exception is here to permit you to replace a rather awkward SDL startup config dialog
with your own user-friendly version, possibly with lesser options and some help on them.
Also you may put your own logo in the startup screen instead of the "Powered by SDL" logo.
You may modify other Java files if needed to implement your custom startup config dialog
without publishing the source code, to a reasonable extent of course - if your modification
will make SDL work better, faster or more stable beyond the startup config dialog -
you still have to publish the source code with such change (and I don't care about
changes which will make SDL work worse, slower or less stable, you may keep them).
*/
package net.sourceforge.clonekeenplus;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.util.Log;
import java.io.*;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Environment;
import android.os.StatFs;
import java.util.Locale;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.Collections;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import java.lang.String;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.FrameLayout;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.widget.TextView;
import android.widget.EditText;
import android.text.Editable;
import android.text.SpannedString;
// TODO: too much code here, split into multiple files, possibly auto-generated menus?
class Settings
{
static String SettingsFileName = "libsdl-settings.cfg";
static boolean settingsLoaded = false;
static boolean settingsChanged = false;
static final int SETTINGS_FILE_VERSION = 5;
static void Save(final MainActivity p)
{
try {
ObjectOutputStream out = new ObjectOutputStream(p.openFileOutput( SettingsFileName, p.MODE_WORLD_READABLE ));
out.writeInt(SETTINGS_FILE_VERSION);
out.writeBoolean(Globals.DownloadToSdcard);
out.writeBoolean(Globals.PhoneHasArrowKeys);
out.writeBoolean(Globals.PhoneHasTrackball);
out.writeBoolean(Globals.UseAccelerometerAsArrowKeys);
out.writeBoolean(Globals.UseTouchscreenKeyboard);
out.writeInt(Globals.TouchscreenKeyboardSize);
out.writeInt(Globals.AccelerometerSensitivity);
out.writeInt(Globals.AccelerometerCenterPos);
out.writeInt(Globals.TrackballDampening);
out.writeInt(Globals.AudioBufferConfig);
out.writeInt(Globals.TouchscreenKeyboardTheme);
out.writeInt(Globals.RightClickMethod);
out.writeBoolean(Globals.ShowScreenUnderFinger);
out.writeInt(Globals.LeftClickMethod);
out.writeBoolean(Globals.MoveMouseWithJoystick);
out.writeBoolean(Globals.ClickMouseWithDpad);
out.writeInt(Globals.ClickScreenPressure);
out.writeInt(Globals.ClickScreenTouchspotSize);
out.writeBoolean(Globals.KeepAspectRatio);
out.writeInt(Globals.MoveMouseWithJoystickSpeed);
out.writeInt(Globals.MoveMouseWithJoystickAccel);
out.writeInt(SDL_Keys.JAVA_KEYCODE_LAST);
for( int i = 0; i < SDL_Keys.JAVA_KEYCODE_LAST; i++ )
{
out.writeInt(Globals.RemapHwKeycode[i]);
}
out.writeInt(Globals.RemapScreenKbKeycode.length);
for( int i = 0; i < Globals.RemapScreenKbKeycode.length; i++ )
{
out.writeInt(Globals.RemapScreenKbKeycode[i]);
}
out.writeInt(Globals.ScreenKbControlsShown.length);
for( int i = 0; i < Globals.ScreenKbControlsShown.length; i++ )
{
out.writeBoolean(Globals.ScreenKbControlsShown[i]);
}
out.writeInt(Globals.TouchscreenKeyboardTransparency);
out.writeInt(Globals.RemapMultitouchGestureKeycode.length);
for( int i = 0; i < Globals.RemapMultitouchGestureKeycode.length; i++ )
{
out.writeInt(Globals.RemapMultitouchGestureKeycode[i]);
out.writeBoolean(Globals.MultitouchGesturesUsed[i]);
}
out.writeInt(Globals.MultitouchGestureSensitivity);
for( int i = 0; i < Globals.TouchscreenCalibration.length; i++ )
out.writeInt(Globals.TouchscreenCalibration[i]);
out.writeInt(Globals.DataDir.length());
for( int i = 0; i < Globals.DataDir.length(); i++ )
out.writeChar(Globals.DataDir.charAt(i));
out.writeInt(Globals.CommandLine.length());
for( int i = 0; i < Globals.CommandLine.length(); i++ )
out.writeChar(Globals.CommandLine.charAt(i));
out.writeInt(Globals.ScreenKbControlsLayout.length);
for( int i = 0; i < Globals.ScreenKbControlsLayout.length; i++ )
for( int ii = 0; ii < 4; ii++ )
out.writeInt(Globals.ScreenKbControlsLayout[i][ii]);
out.writeInt(Globals.LeftClickKey);
out.writeInt(Globals.RightClickKey);
out.writeBoolean(Globals.SmoothVideo);
out.writeInt(Globals.LeftClickTimeout);
out.writeInt(Globals.RightClickTimeout);
out.writeBoolean(Globals.RelativeMouseMovement);
out.writeInt(Globals.RelativeMouseMovementSpeed);
out.writeInt(Globals.RelativeMouseMovementAccel);
out.writeBoolean(Globals.MultiThreadedVideo);
out.writeInt(Globals.OptionalDataDownload.length);
for(int i = 0; i < Globals.OptionalDataDownload.length; i++)
out.writeBoolean(Globals.OptionalDataDownload[i]);
out.close();
settingsLoaded = true;
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) {
} catch ( IOException e ) {};
}
static void Load( final MainActivity p )
{
if(settingsLoaded) // Prevent starting twice
{
return;
}
System.out.println("libSDL: Settings.Load(): enter");
nativeInitKeymap();
for( int i = 0; i < SDL_Keys.JAVA_KEYCODE_LAST; i++ )
{
int sdlKey = nativeGetKeymapKey(i);
int idx = 0;
for(int ii = 0; ii < SDL_Keys.values.length; ii++)
if(SDL_Keys.values[ii] == sdlKey)
idx = ii;
Globals.RemapHwKeycode[i] = idx;
}
for( int i = 0; i < Globals.RemapScreenKbKeycode.length; i++ )
{
int sdlKey = nativeGetKeymapKeyScreenKb(i);
int idx = 0;
for(int ii = 0; ii < SDL_Keys.values.length; ii++)
if(SDL_Keys.values[ii] == sdlKey)
idx = ii;
Globals.RemapScreenKbKeycode[i] = idx;
}
Globals.ScreenKbControlsShown[0] = Globals.AppNeedsArrowKeys;
Globals.ScreenKbControlsShown[1] = Globals.AppNeedsTextInput;
for( int i = 2; i < Globals.ScreenKbControlsShown.length; i++ )
Globals.ScreenKbControlsShown[i] = ( i - 2 < Globals.AppTouchscreenKeyboardKeysAmount );
for( int i = 0; i < Globals.RemapMultitouchGestureKeycode.length; i++ )
{
int sdlKey = nativeGetKeymapKeyMultitouchGesture(i);
int idx = 0;
for(int ii = 0; ii < SDL_Keys.values.length; ii++)
if(SDL_Keys.values[ii] == sdlKey)
idx = ii;
Globals.RemapMultitouchGestureKeycode[i] = idx;
}
for( int i = 0; i < Globals.MultitouchGesturesUsed.length; i++ )
Globals.MultitouchGesturesUsed[i] = true;
try {
ObjectInputStream settingsFile = new ObjectInputStream(new FileInputStream( p.getFilesDir().getAbsolutePath() + "/" + SettingsFileName ));
if( settingsFile.readInt() != SETTINGS_FILE_VERSION )
throw new IOException();
Globals.DownloadToSdcard = settingsFile.readBoolean();
Globals.PhoneHasArrowKeys = settingsFile.readBoolean();
Globals.PhoneHasTrackball = settingsFile.readBoolean();
Globals.UseAccelerometerAsArrowKeys = settingsFile.readBoolean();
Globals.UseTouchscreenKeyboard = settingsFile.readBoolean();
Globals.TouchscreenKeyboardSize = settingsFile.readInt();
Globals.AccelerometerSensitivity = settingsFile.readInt();
Globals.AccelerometerCenterPos = settingsFile.readInt();
Globals.TrackballDampening = settingsFile.readInt();
Globals.AudioBufferConfig = settingsFile.readInt();
Globals.TouchscreenKeyboardTheme = settingsFile.readInt();
Globals.RightClickMethod = settingsFile.readInt();
Globals.ShowScreenUnderFinger = settingsFile.readBoolean();
Globals.LeftClickMethod = settingsFile.readInt();
Globals.MoveMouseWithJoystick = settingsFile.readBoolean();
Globals.ClickMouseWithDpad = settingsFile.readBoolean();
Globals.ClickScreenPressure = settingsFile.readInt();
Globals.ClickScreenTouchspotSize = settingsFile.readInt();
Globals.KeepAspectRatio = settingsFile.readBoolean();
Globals.MoveMouseWithJoystickSpeed = settingsFile.readInt();
Globals.MoveMouseWithJoystickAccel = settingsFile.readInt();
int readKeys = settingsFile.readInt();
for( int i = 0; i < readKeys; i++ )
{
Globals.RemapHwKeycode[i] = settingsFile.readInt();
}
if( settingsFile.readInt() != Globals.RemapScreenKbKeycode.length )
throw new IOException();
for( int i = 0; i < Globals.RemapScreenKbKeycode.length; i++ )
{
Globals.RemapScreenKbKeycode[i] = settingsFile.readInt();
}
if( settingsFile.readInt() != Globals.ScreenKbControlsShown.length )
throw new IOException();
for( int i = 0; i < Globals.ScreenKbControlsShown.length; i++ )
{
Globals.ScreenKbControlsShown[i] = settingsFile.readBoolean();
}
Globals.TouchscreenKeyboardTransparency = settingsFile.readInt();
if( settingsFile.readInt() != Globals.RemapMultitouchGestureKeycode.length )
throw new IOException();
for( int i = 0; i < Globals.RemapMultitouchGestureKeycode.length; i++ )
{
Globals.RemapMultitouchGestureKeycode[i] = settingsFile.readInt();
Globals.MultitouchGesturesUsed[i] = settingsFile.readBoolean();
}
Globals.MultitouchGestureSensitivity = settingsFile.readInt();
for( int i = 0; i < Globals.TouchscreenCalibration.length; i++ )
Globals.TouchscreenCalibration[i] = settingsFile.readInt();
StringBuilder b = new StringBuilder();
int len = settingsFile.readInt();
for( int i = 0; i < len; i++ )
b.append( settingsFile.readChar() );
Globals.DataDir = b.toString();
b = new StringBuilder();
len = settingsFile.readInt();
for( int i = 0; i < len; i++ )
b.append( settingsFile.readChar() );
Globals.CommandLine = b.toString();
if( settingsFile.readInt() != Globals.ScreenKbControlsLayout.length )
throw new IOException();
for( int i = 0; i < Globals.ScreenKbControlsLayout.length; i++ )
for( int ii = 0; ii < 4; ii++ )
Globals.ScreenKbControlsLayout[i][ii] = settingsFile.readInt();
Globals.LeftClickKey = settingsFile.readInt();
Globals.RightClickKey = settingsFile.readInt();
Globals.SmoothVideo = settingsFile.readBoolean();
Globals.LeftClickTimeout = settingsFile.readInt();
Globals.RightClickTimeout = settingsFile.readInt();
Globals.RelativeMouseMovement = settingsFile.readBoolean();
Globals.RelativeMouseMovementSpeed = settingsFile.readInt();
Globals.RelativeMouseMovementAccel = settingsFile.readInt();
Globals.MultiThreadedVideo = settingsFile.readBoolean();
Globals.OptionalDataDownload = new boolean[settingsFile.readInt()];
for(int i = 0; i < Globals.OptionalDataDownload.length; i++)
Globals.OptionalDataDownload[i] = settingsFile.readBoolean();
settingsLoaded = true;
System.out.println("libSDL: Settings.Load(): loaded settings successfully");
return;
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) {
} catch ( IOException e ) {};
if( Globals.DataDir.length() == 0 )
Globals.DataDir = Globals.DownloadToSdcard ?
Environment.getExternalStorageDirectory().getAbsolutePath() + "/app-data/" + Globals.class.getPackage().getName() :
p.getFilesDir().getAbsolutePath();
// This code fails for both of my phones!
/*
Configuration c = new Configuration();
c.setToDefaults();
if( c.navigation == Configuration.NAVIGATION_TRACKBALL ||
c.navigation == Configuration.NAVIGATION_DPAD ||
c.navigation == Configuration.NAVIGATION_WHEEL )
{
Globals.AppNeedsArrowKeys = false;
}
System.out.println( "libSDL: Phone keypad type: " +
(
c.navigation == Configuration.NAVIGATION_TRACKBALL ? "Trackball" :
c.navigation == Configuration.NAVIGATION_DPAD ? "Dpad" :
c.navigation == Configuration.NAVIGATION_WHEEL ? "Wheel" :
c.navigation == Configuration.NAVIGATION_NONAV ? "None" :
"Unknown" ) );
*/
System.out.println("libSDL: Settings.Load(): loading settings failed, running config dialog");
p.setUpStatusLabel();
showConfig(p, true);
}
// ===============================================================================================
public static abstract class Menu
{
abstract void run(final MainActivity p);
abstract String title(final MainActivity p);
boolean enabled()
{
return true;
}
}
static ArrayList