mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2025-12-12 17:12:40 +01:00
DroidFish: Made the application context globally available.
This commit is contained in:
@@ -11,7 +11,8 @@
|
|||||||
<uses-permission android:name="android.permission.VIBRATE" />
|
<uses-permission android:name="android.permission.VIBRATE" />
|
||||||
<uses-sdk android:minSdkVersion="11"
|
<uses-sdk android:minSdkVersion="11"
|
||||||
android:targetSdkVersion="23"/>
|
android:targetSdkVersion="23"/>
|
||||||
<application android:icon="@mipmap/icon"
|
<application android:name="org.petero.droidfish.DroidFishApp"
|
||||||
|
android:icon="@mipmap/icon"
|
||||||
android:theme="@android:style/Theme.Holo"
|
android:theme="@android:style/Theme.Holo"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:allowBackup="true">
|
android:allowBackup="true">
|
||||||
|
|||||||
@@ -1919,11 +1919,6 @@ public class DroidFish extends Activity
|
|||||||
return mPonderMode;
|
return mPonderMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Context getContext() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String playerName() {
|
public String playerName() {
|
||||||
return playerName;
|
return playerName;
|
||||||
|
|||||||
18
DroidFish/src/org/petero/droidfish/DroidFishApp.java
Normal file
18
DroidFish/src/org/petero/droidfish/DroidFishApp.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package org.petero.droidfish;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
public class DroidFishApp extends Application {
|
||||||
|
private static Context appContext;
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
appContext = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the application context. */
|
||||||
|
public static Context getContext() {
|
||||||
|
return appContext;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,8 +24,6 @@ import org.petero.droidfish.gamelogic.Game;
|
|||||||
import org.petero.droidfish.gamelogic.Move;
|
import org.petero.droidfish.gamelogic.Move;
|
||||||
import org.petero.droidfish.gamelogic.Position;
|
import org.petero.droidfish.gamelogic.Position;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
/** Interface between the GUI and the ChessController. */
|
/** Interface between the GUI and the ChessController. */
|
||||||
public interface GUIInterface {
|
public interface GUIInterface {
|
||||||
|
|
||||||
@@ -105,9 +103,6 @@ public interface GUIInterface {
|
|||||||
/** Return true if pondering (permanent brain) is enabled. */
|
/** Return true if pondering (permanent brain) is enabled. */
|
||||||
public boolean ponderMode();
|
public boolean ponderMode();
|
||||||
|
|
||||||
/** Return application context. */
|
|
||||||
public Context getContext();
|
|
||||||
|
|
||||||
/** Get the default player name. */
|
/** Get the default player name. */
|
||||||
public String playerName();
|
public String playerName();
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
package org.petero.droidfish.book;
|
package org.petero.droidfish.book;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -28,6 +27,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
|
import org.petero.droidfish.DroidFishApp;
|
||||||
import org.petero.droidfish.gamelogic.ChessParseError;
|
import org.petero.droidfish.gamelogic.ChessParseError;
|
||||||
import org.petero.droidfish.gamelogic.GameTree;
|
import org.petero.droidfish.gamelogic.GameTree;
|
||||||
import org.petero.droidfish.gamelogic.Move;
|
import org.petero.droidfish.gamelogic.Move;
|
||||||
@@ -42,9 +42,9 @@ public class EcoDb {
|
|||||||
private static EcoDb instance;
|
private static EcoDb instance;
|
||||||
|
|
||||||
/** Get singleton instance. */
|
/** Get singleton instance. */
|
||||||
public static EcoDb getInstance(Context context) {
|
public static EcoDb getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = new EcoDb(context);
|
instance = new EcoDb();
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
@@ -183,13 +183,13 @@ public class EcoDb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Constructor. */
|
/** Constructor. */
|
||||||
private EcoDb(Context context) {
|
private EcoDb() {
|
||||||
posHashToNodeIdx = new HashMap<Long, Short>();
|
posHashToNodeIdx = new HashMap<Long, Short>();
|
||||||
posHashToNodeIdx2 = new HashMap<Long, ArrayList<Short>>();
|
posHashToNodeIdx2 = new HashMap<Long, ArrayList<Short>>();
|
||||||
gtNodeToIdx = new WeakLRUCache<GameTree.Node, CacheEntry>(50);
|
gtNodeToIdx = new WeakLRUCache<GameTree.Node, CacheEntry>(50);
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
|
||||||
InputStream inStream = context.getAssets().open("eco.dat");
|
InputStream inStream = DroidFishApp.getContext().getAssets().open("eco.dat");
|
||||||
if (inStream == null)
|
if (inStream == null)
|
||||||
throw new IOException("Can't read ECO database");
|
throw new IOException("Can't read ECO database");
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
|
|||||||
@@ -36,15 +36,12 @@ import org.petero.droidfish.gamelogic.UndoInfo;
|
|||||||
import org.petero.droidfish.gamelogic.SearchListener.PvInfo;
|
import org.petero.droidfish.gamelogic.SearchListener.PvInfo;
|
||||||
import org.petero.droidfish.tb.Probe;
|
import org.petero.droidfish.tb.Probe;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A computer algorithm player.
|
* A computer algorithm player.
|
||||||
* @author petero
|
* @author petero
|
||||||
*/
|
*/
|
||||||
public class DroidComputerPlayer {
|
public class DroidComputerPlayer {
|
||||||
private UCIEngine uciEngine = null;
|
private UCIEngine uciEngine = null;
|
||||||
private final Context context;
|
|
||||||
private final SearchListener listener;
|
private final SearchListener listener;
|
||||||
private final DroidBook book;
|
private final DroidBook book;
|
||||||
private EngineOptions engineOptions = new EngineOptions();
|
private EngineOptions engineOptions = new EngineOptions();
|
||||||
@@ -234,8 +231,7 @@ public class DroidComputerPlayer {
|
|||||||
private Thread engineMonitor;
|
private Thread engineMonitor;
|
||||||
|
|
||||||
/** Constructor. Starts engine process if not already started. */
|
/** Constructor. Starts engine process if not already started. */
|
||||||
public DroidComputerPlayer(Context context, SearchListener listener) {
|
public DroidComputerPlayer(SearchListener listener) {
|
||||||
this.context = context;
|
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
book = DroidBook.getInstance();
|
book = DroidBook.getInstance();
|
||||||
}
|
}
|
||||||
@@ -654,7 +650,7 @@ public class DroidComputerPlayer {
|
|||||||
myAssert(searchRequest != null);
|
myAssert(searchRequest != null);
|
||||||
|
|
||||||
engineName = "Computer";
|
engineName = "Computer";
|
||||||
uciEngine = UCIEngineBase.getEngine(context, searchRequest.engine,
|
uciEngine = UCIEngineBase.getEngine(searchRequest.engine,
|
||||||
engineOptions,
|
engineOptions,
|
||||||
new UCIEngine.Report() {
|
new UCIEngine.Report() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
|
|
||||||
|
import org.petero.droidfish.DroidFishApp;
|
||||||
import org.petero.droidfish.EngineOptions;
|
import org.petero.droidfish.EngineOptions;
|
||||||
import org.petero.droidfish.R;
|
import org.petero.droidfish.R;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -46,8 +47,8 @@ public class ExternalEngine extends UCIEngineBase {
|
|||||||
private boolean startedOk;
|
private boolean startedOk;
|
||||||
private boolean isRunning;
|
private boolean isRunning;
|
||||||
|
|
||||||
public ExternalEngine(Context context, String engine, Report report) {
|
public ExternalEngine(String engine, Report report) {
|
||||||
this.context = context;
|
context = DroidFishApp.getContext();
|
||||||
this.report = report;
|
this.report = report;
|
||||||
engineFileName = new File(engine);
|
engineFileName = new File(engine);
|
||||||
engineProc = null;
|
engineProc = null;
|
||||||
|
|||||||
@@ -30,14 +30,13 @@ import java.security.MessageDigest;
|
|||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
|
|
||||||
/** Stockfish engine running as process, started from assets resource. */
|
/** Stockfish engine running as process, started from assets resource. */
|
||||||
public class InternalStockFish extends ExternalEngine {
|
public class InternalStockFish extends ExternalEngine {
|
||||||
|
|
||||||
public InternalStockFish(Context context, Report report) {
|
public InternalStockFish(Report report) {
|
||||||
super(context, "", report);
|
super("", report);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import java.io.InputStreamReader;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
import org.petero.droidfish.DroidFishApp;
|
||||||
import org.petero.droidfish.EngineOptions;
|
import org.petero.droidfish.EngineOptions;
|
||||||
import org.petero.droidfish.FileUtil;
|
import org.petero.droidfish.FileUtil;
|
||||||
import org.petero.droidfish.R;
|
import org.petero.droidfish.R;
|
||||||
@@ -49,8 +50,8 @@ public class NetworkEngine extends UCIEngineBase {
|
|||||||
private boolean isRunning;
|
private boolean isRunning;
|
||||||
private boolean isError;
|
private boolean isError;
|
||||||
|
|
||||||
public NetworkEngine(Context context, String engine, EngineOptions engineOptions, Report report) {
|
public NetworkEngine(String engine, EngineOptions engineOptions, Report report) {
|
||||||
this.context = context;
|
context = DroidFishApp.getContext();
|
||||||
this.report = report;
|
this.report = report;
|
||||||
fileName = engine;
|
fileName = engine;
|
||||||
networkID = engineOptions.networkID;
|
networkID = engineOptions.networkID;
|
||||||
|
|||||||
@@ -7,13 +7,11 @@ import java.util.List;
|
|||||||
import com.kalab.chess.enginesupport.ChessEngine;
|
import com.kalab.chess.enginesupport.ChessEngine;
|
||||||
import com.kalab.chess.enginesupport.ChessEngineResolver;
|
import com.kalab.chess.enginesupport.ChessEngineResolver;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
/** Engine imported from a different android app, resolved using the open exchange format. */
|
/** Engine imported from a different android app, resolved using the open exchange format. */
|
||||||
public class OpenExchangeEngine extends ExternalEngine {
|
public class OpenExchangeEngine extends ExternalEngine {
|
||||||
|
|
||||||
public OpenExchangeEngine(Context context, String engine, Report report) {
|
public OpenExchangeEngine(String engine, Report report) {
|
||||||
super(context, engine, report);
|
super(engine, report);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -30,28 +30,26 @@ import java.util.Properties;
|
|||||||
import org.petero.droidfish.EngineOptions;
|
import org.petero.droidfish.EngineOptions;
|
||||||
import org.petero.droidfish.engine.cuckoochess.CuckooChessEngine;
|
import org.petero.droidfish.engine.cuckoochess.CuckooChessEngine;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
public abstract class UCIEngineBase implements UCIEngine {
|
public abstract class UCIEngineBase implements UCIEngine {
|
||||||
|
|
||||||
private boolean processAlive;
|
private boolean processAlive;
|
||||||
private UCIOptions options;
|
private UCIOptions options;
|
||||||
protected boolean isUCI;
|
protected boolean isUCI;
|
||||||
|
|
||||||
public static UCIEngine getEngine(Context context, String engine,
|
public static UCIEngine getEngine(String engine,
|
||||||
EngineOptions engineOptions, Report report) {
|
EngineOptions engineOptions, Report report) {
|
||||||
if ("stockfish".equals(engine) && (EngineUtil.internalStockFishName() == null))
|
if ("stockfish".equals(engine) && (EngineUtil.internalStockFishName() == null))
|
||||||
engine = "cuckoochess";
|
engine = "cuckoochess";
|
||||||
if ("cuckoochess".equals(engine))
|
if ("cuckoochess".equals(engine))
|
||||||
return new CuckooChessEngine(report);
|
return new CuckooChessEngine(report);
|
||||||
else if ("stockfish".equals(engine))
|
else if ("stockfish".equals(engine))
|
||||||
return new InternalStockFish(context, report);
|
return new InternalStockFish(report);
|
||||||
else if (EngineUtil.isOpenExchangeEngine(engine))
|
else if (EngineUtil.isOpenExchangeEngine(engine))
|
||||||
return new OpenExchangeEngine(context, engine, report);
|
return new OpenExchangeEngine(engine, report);
|
||||||
else if (EngineUtil.isNetEngine(engine))
|
else if (EngineUtil.isNetEngine(engine))
|
||||||
return new NetworkEngine(context, engine, engineOptions, report);
|
return new NetworkEngine(engine, engineOptions, report);
|
||||||
else
|
else
|
||||||
return new ExternalEngine(context, engine, report);
|
return new ExternalEngine(engine, report);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected UCIEngineBase() {
|
protected UCIEngineBase() {
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ public class DroidChessController {
|
|||||||
updateGUI();
|
updateGUI();
|
||||||
this.gameMode = gameMode;
|
this.gameMode = gameMode;
|
||||||
if (computerPlayer == null) {
|
if (computerPlayer == null) {
|
||||||
computerPlayer = new DroidComputerPlayer(gui.getContext(), listener);
|
computerPlayer = new DroidComputerPlayer(listener);
|
||||||
computerPlayer.setBookOptions(bookOptions);
|
computerPlayer.setBookOptions(bookOptions);
|
||||||
computerPlayer.setEngineOptions(engineOptions);
|
computerPlayer.setEngineOptions(engineOptions);
|
||||||
}
|
}
|
||||||
@@ -932,7 +932,7 @@ public class DroidChessController {
|
|||||||
if (game != null) {
|
if (game != null) {
|
||||||
Pair<String, ArrayList<Move>> bi = computerPlayer.getBookHints(game.currPos(), localPt());
|
Pair<String, ArrayList<Move>> bi = computerPlayer.getBookHints(game.currPos(), localPt());
|
||||||
Pair<String, Integer> ecoData =
|
Pair<String, Integer> ecoData =
|
||||||
EcoDb.getInstance(gui.getContext()).getEco(game.tree);
|
EcoDb.getInstance().getEco(game.tree);
|
||||||
String eco = ecoData.first;
|
String eco = ecoData.first;
|
||||||
listener.notifyBookInfo(searchId, bi.first, bi.second, eco, ecoData.second);
|
listener.notifyBookInfo(searchId, bi.first, bi.second, eco, ecoData.second);
|
||||||
}
|
}
|
||||||
@@ -975,7 +975,7 @@ public class DroidChessController {
|
|||||||
} else if (computersTurn || ponder) {
|
} else if (computersTurn || ponder) {
|
||||||
listener.clearSearchInfo(searchId);
|
listener.clearSearchInfo(searchId);
|
||||||
Pair<String, Integer> ecoData =
|
Pair<String, Integer> ecoData =
|
||||||
EcoDb.getInstance(gui.getContext()).getEco(game.tree);
|
EcoDb.getInstance().getEco(game.tree);
|
||||||
String eco = ecoData.first;
|
String eco = ecoData.first;
|
||||||
listener.notifyBookInfo(searchId, "", null, eco, ecoData.second);
|
listener.notifyBookInfo(searchId, "", null, eco, ecoData.second);
|
||||||
final Pair<Position, ArrayList<Move>> ph = game.getUCIHistory();
|
final Pair<Position, ArrayList<Move>> ph = game.getUCIHistory();
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class EcoTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testEco() throws Throwable {
|
public void testEco() throws Throwable {
|
||||||
EcoDb ecoDb = EcoDb.getInstance(getContext());
|
EcoDb ecoDb = EcoDb.getInstance();
|
||||||
{
|
{
|
||||||
String pgn = "e4 e5 Nf3 Nc6 Bb5 a6 Ba4 Nf6 O-O Be7 Re1";
|
String pgn = "e4 e5 Nf3 Nc6 Bb5 a6 Ba4 Nf6 O-O Be7 Re1";
|
||||||
GameTree gt = readPGN(pgn);
|
GameTree gt = readPGN(pgn);
|
||||||
@@ -137,7 +137,7 @@ public class EcoTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testEcoFromFEN() throws Throwable {
|
public void testEcoFromFEN() throws Throwable {
|
||||||
EcoDb ecoDb = EcoDb.getInstance(getContext());
|
EcoDb ecoDb = EcoDb.getInstance();
|
||||||
GameTree gt = gtFromFEN("rnbqkbnr/ppp2ppp/4p3/3P4/3P4/8/PPP2PPP/RNBQKBNR b KQkq - 0 3");
|
GameTree gt = gtFromFEN("rnbqkbnr/ppp2ppp/4p3/3P4/3P4/8/PPP2PPP/RNBQKBNR b KQkq - 0 3");
|
||||||
String eco = ecoDb.getEco(gt).first;
|
String eco = ecoDb.getEco(gt).first;
|
||||||
assertEquals("C01: French, exchange variation", eco);
|
assertEquals("C01: French, exchange variation", eco);
|
||||||
|
|||||||
Reference in New Issue
Block a user