DroidFish: Made the application context globally available.

This commit is contained in:
Peter Osterlund 2016-11-20 12:01:04 +01:00
parent 3292bec55c
commit 5df60c856e
13 changed files with 47 additions and 45 deletions

View File

@ -11,7 +11,8 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk android:minSdkVersion="11"
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:label="@string/app_name"
android:allowBackup="true">

View File

@ -1919,11 +1919,6 @@ public class DroidFish extends Activity
return mPonderMode;
}
@Override
public Context getContext() {
return this;
}
@Override
public String playerName() {
return playerName;

View 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;
}
}

View File

@ -24,8 +24,6 @@ import org.petero.droidfish.gamelogic.Game;
import org.petero.droidfish.gamelogic.Move;
import org.petero.droidfish.gamelogic.Position;
import android.content.Context;
/** Interface between the GUI and the ChessController. */
public interface GUIInterface {
@ -105,9 +103,6 @@ public interface GUIInterface {
/** Return true if pondering (permanent brain) is enabled. */
public boolean ponderMode();
/** Return application context. */
public Context getContext();
/** Get the default player name. */
public String playerName();

View File

@ -19,7 +19,6 @@
package org.petero.droidfish.book;
import android.annotation.SuppressLint;
import android.content.Context;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -28,6 +27,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.WeakHashMap;
import org.petero.droidfish.DroidFishApp;
import org.petero.droidfish.gamelogic.ChessParseError;
import org.petero.droidfish.gamelogic.GameTree;
import org.petero.droidfish.gamelogic.Move;
@ -42,9 +42,9 @@ public class EcoDb {
private static EcoDb instance;
/** Get singleton instance. */
public static EcoDb getInstance(Context context) {
public static EcoDb getInstance() {
if (instance == null) {
instance = new EcoDb(context);
instance = new EcoDb();
}
return instance;
}
@ -183,13 +183,13 @@ public class EcoDb {
}
/** Constructor. */
private EcoDb(Context context) {
private EcoDb() {
posHashToNodeIdx = new HashMap<Long, Short>();
posHashToNodeIdx2 = new HashMap<Long, ArrayList<Short>>();
gtNodeToIdx = new WeakLRUCache<GameTree.Node, CacheEntry>(50);
try {
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
InputStream inStream = context.getAssets().open("eco.dat");
InputStream inStream = DroidFishApp.getContext().getAssets().open("eco.dat");
if (inStream == null)
throw new IOException("Can't read ECO database");
byte[] buf = new byte[1024];

View File

@ -36,15 +36,12 @@ import org.petero.droidfish.gamelogic.UndoInfo;
import org.petero.droidfish.gamelogic.SearchListener.PvInfo;
import org.petero.droidfish.tb.Probe;
import android.content.Context;
/**
* A computer algorithm player.
* @author petero
*/
public class DroidComputerPlayer {
private UCIEngine uciEngine = null;
private final Context context;
private final SearchListener listener;
private final DroidBook book;
private EngineOptions engineOptions = new EngineOptions();
@ -234,8 +231,7 @@ public class DroidComputerPlayer {
private Thread engineMonitor;
/** Constructor. Starts engine process if not already started. */
public DroidComputerPlayer(Context context, SearchListener listener) {
this.context = context;
public DroidComputerPlayer(SearchListener listener) {
this.listener = listener;
book = DroidBook.getInstance();
}
@ -654,7 +650,7 @@ public class DroidComputerPlayer {
myAssert(searchRequest != null);
engineName = "Computer";
uciEngine = UCIEngineBase.getEngine(context, searchRequest.engine,
uciEngine = UCIEngineBase.getEngine(searchRequest.engine,
engineOptions,
new UCIEngine.Report() {
@Override

View File

@ -27,6 +27,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import org.petero.droidfish.DroidFishApp;
import org.petero.droidfish.EngineOptions;
import org.petero.droidfish.R;
import android.content.Context;
@ -46,8 +47,8 @@ public class ExternalEngine extends UCIEngineBase {
private boolean startedOk;
private boolean isRunning;
public ExternalEngine(Context context, String engine, Report report) {
this.context = context;
public ExternalEngine(String engine, Report report) {
context = DroidFishApp.getContext();
this.report = report;
engineFileName = new File(engine);
engineProc = null;

View File

@ -30,14 +30,13 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import android.content.Context;
import android.os.Environment;
/** Stockfish engine running as process, started from assets resource. */
public class InternalStockFish extends ExternalEngine {
public InternalStockFish(Context context, Report report) {
super(context, "", report);
public InternalStockFish(Report report) {
super("", report);
}
@Override

View File

@ -26,6 +26,7 @@ import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import org.petero.droidfish.DroidFishApp;
import org.petero.droidfish.EngineOptions;
import org.petero.droidfish.FileUtil;
import org.petero.droidfish.R;
@ -49,8 +50,8 @@ public class NetworkEngine extends UCIEngineBase {
private boolean isRunning;
private boolean isError;
public NetworkEngine(Context context, String engine, EngineOptions engineOptions, Report report) {
this.context = context;
public NetworkEngine(String engine, EngineOptions engineOptions, Report report) {
context = DroidFishApp.getContext();
this.report = report;
fileName = engine;
networkID = engineOptions.networkID;

View File

@ -7,13 +7,11 @@ import java.util.List;
import com.kalab.chess.enginesupport.ChessEngine;
import com.kalab.chess.enginesupport.ChessEngineResolver;
import android.content.Context;
/** Engine imported from a different android app, resolved using the open exchange format. */
public class OpenExchangeEngine extends ExternalEngine {
public OpenExchangeEngine(Context context, String engine, Report report) {
super(context, engine, report);
public OpenExchangeEngine(String engine, Report report) {
super(engine, report);
}
@Override

View File

@ -30,28 +30,26 @@ import java.util.Properties;
import org.petero.droidfish.EngineOptions;
import org.petero.droidfish.engine.cuckoochess.CuckooChessEngine;
import android.content.Context;
public abstract class UCIEngineBase implements UCIEngine {
private boolean processAlive;
private UCIOptions options;
protected boolean isUCI;
public static UCIEngine getEngine(Context context, String engine,
public static UCIEngine getEngine(String engine,
EngineOptions engineOptions, Report report) {
if ("stockfish".equals(engine) && (EngineUtil.internalStockFishName() == null))
engine = "cuckoochess";
if ("cuckoochess".equals(engine))
return new CuckooChessEngine(report);
else if ("stockfish".equals(engine))
return new InternalStockFish(context, report);
return new InternalStockFish(report);
else if (EngineUtil.isOpenExchangeEngine(engine))
return new OpenExchangeEngine(context, engine, report);
return new OpenExchangeEngine(engine, report);
else if (EngineUtil.isNetEngine(engine))
return new NetworkEngine(context, engine, engineOptions, report);
return new NetworkEngine(engine, engineOptions, report);
else
return new ExternalEngine(context, engine, report);
return new ExternalEngine(engine, report);
}
protected UCIEngineBase() {

View File

@ -88,7 +88,7 @@ public class DroidChessController {
updateGUI();
this.gameMode = gameMode;
if (computerPlayer == null) {
computerPlayer = new DroidComputerPlayer(gui.getContext(), listener);
computerPlayer = new DroidComputerPlayer(listener);
computerPlayer.setBookOptions(bookOptions);
computerPlayer.setEngineOptions(engineOptions);
}
@ -932,7 +932,7 @@ public class DroidChessController {
if (game != null) {
Pair<String, ArrayList<Move>> bi = computerPlayer.getBookHints(game.currPos(), localPt());
Pair<String, Integer> ecoData =
EcoDb.getInstance(gui.getContext()).getEco(game.tree);
EcoDb.getInstance().getEco(game.tree);
String eco = ecoData.first;
listener.notifyBookInfo(searchId, bi.first, bi.second, eco, ecoData.second);
}
@ -975,7 +975,7 @@ public class DroidChessController {
} else if (computersTurn || ponder) {
listener.clearSearchInfo(searchId);
Pair<String, Integer> ecoData =
EcoDb.getInstance(gui.getContext()).getEco(game.tree);
EcoDb.getInstance().getEco(game.tree);
String eco = ecoData.first;
listener.notifyBookInfo(searchId, "", null, eco, ecoData.second);
final Pair<Position, ArrayList<Move>> ph = game.getUCIHistory();

View File

@ -32,7 +32,7 @@ public class EcoTest extends AndroidTestCase {
}
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";
GameTree gt = readPGN(pgn);
@ -137,7 +137,7 @@ public class EcoTest extends AndroidTestCase {
}
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");
String eco = ecoDb.getEco(gt).first;
assertEquals("C01: French, exchange variation", eco);