DroidFish: Require at least API level 15 (4.0.3). Removed use of some

deprecated APIs.
This commit is contained in:
Peter Osterlund
2015-12-19 22:36:32 +01:00
parent 8630c450df
commit 394a6c0e85
6 changed files with 122 additions and 91 deletions

View File

@@ -10,4 +10,4 @@
# Indicates whether an apk should be generated for each density. # Indicates whether an apk should be generated for each density.
split.density=false split.density=false
# Project target. # Project target.
target=android-10 target=android-15

View File

@@ -73,6 +73,9 @@ import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
@@ -96,12 +99,9 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.view.MotionEventCompat; import android.support.v4.view.MotionEventCompat;
import android.text.ClipboardManager;
import android.text.Html; import android.text.Html;
import android.text.Layout; import android.text.Layout;
import android.text.Spannable; import android.text.Spannable;
@@ -128,6 +128,7 @@ import android.view.View.OnClickListener;
import android.view.View.OnKeyListener; import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener; import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener; import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
@@ -230,7 +231,6 @@ public class DroidFish extends Activity implements GUIInterface {
PgnScreenText gameTextListener; PgnScreenText gameTextListener;
private WakeLock wakeLock = null;
private boolean useWakeLock = false; private boolean useWakeLock = false;
private Typeface figNotation; private Typeface figNotation;
@@ -407,10 +407,7 @@ public class DroidFish extends Activity implements GUIInterface {
} }
}); });
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
setWakeLock(false); setWakeLock(false);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "droidfish");
wakeLock.setReferenceCounted(false);
custom1ButtonActions = new ButtonActions("custom1", CUSTOM1_BUTTON_DIALOG, custom1ButtonActions = new ButtonActions("custom1", CUSTOM1_BUTTON_DIALOG,
R.string.select_action); R.string.select_action);
@@ -929,7 +926,6 @@ public class DroidFish extends Activity implements GUIInterface {
ctrl.setGuiPaused(false); ctrl.setGuiPaused(false);
notificationActive = true; notificationActive = true;
updateNotification(); updateNotification();
setWakeLock(useWakeLock);
super.onResume(); super.onResume();
} }
@@ -947,7 +943,6 @@ public class DroidFish extends Activity implements GUIInterface {
} }
lastVisibleMillis = System.currentTimeMillis(); lastVisibleMillis = System.currentTimeMillis();
updateNotification(); updateNotification();
setWakeLock(false);
super.onPause(); super.onPause();
} }
@@ -1173,13 +1168,10 @@ public class DroidFish extends Activity implements GUIInterface {
@SuppressLint("Wakelock") @SuppressLint("Wakelock")
private synchronized final void setWakeLock(boolean enableLock) { private synchronized final void setWakeLock(boolean enableLock) {
WakeLock wl = wakeLock; if (enableLock)
if (wl != null) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (wl.isHeld()) else
wl.release(); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (enableLock)
wl.acquire();
}
} }
private final void setEngineStrength(String engine, int strength) { private final void setEngineStrength(String engine, int strength) {
@@ -1851,19 +1843,19 @@ public class DroidFish extends Activity implements GUIInterface {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.option_new_game); builder.setTitle(R.string.option_new_game);
builder.setMessage(R.string.start_new_game); builder.setMessage(R.string.start_new_game);
builder.setPositiveButton(R.string.yes, new Dialog.OnClickListener() { builder.setNeutralButton(R.string.yes, new Dialog.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
startNewGame(2); startNewGame(2);
} }
}); });
builder.setNeutralButton(R.string.white, new Dialog.OnClickListener() { builder.setNegativeButton(R.string.white, new Dialog.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
startNewGame(0); startNewGame(0);
} }
}); });
builder.setNegativeButton(R.string.black, new Dialog.OnClickListener() { builder.setPositiveButton(R.string.black, new Dialog.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
startNewGame(1); startNewGame(1);
@@ -1926,21 +1918,28 @@ public class DroidFish extends Activity implements GUIInterface {
case COPY_GAME: { case COPY_GAME: {
String pgn = ctrl.getPGN(); String pgn = ctrl.getPGN();
ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(pgn); clipboard.setPrimaryClip(new ClipData("DroidFish game",
new String[]{ "application/x-chess-pgn", ClipDescription.MIMETYPE_TEXT_PLAIN },
new ClipData.Item(pgn)));
break; break;
} }
case COPY_POSITION: { case COPY_POSITION: {
String fen = ctrl.getFEN() + "\n"; String fen = ctrl.getFEN() + "\n";
ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(fen); clipboard.setPrimaryClip(new ClipData(fen,
new String[]{ "application/x-chess-fen", ClipDescription.MIMETYPE_TEXT_PLAIN },
new ClipData.Item(fen)));
break; break;
} }
case PASTE: { case PASTE: {
ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
if (clipboard.hasText()) { if (clipboard.hasPrimaryClip()) {
String fenPgn = clipboard.getText().toString(); ClipData clip = clipboard.getPrimaryClip();
StringBuilder fenPgn = new StringBuilder();
for (int i = 0; i < clip.getItemCount(); i++)
fenPgn.append(clip.getItemAt(i).coerceToText(getApplicationContext()));
try { try {
ctrl.setFENOrPGN(fenPgn); ctrl.setFENOrPGN(fenPgn.toString());
setBoardFlip(true); setBoardFlip(true);
} catch (ChessParseError e) { } catch (ChessParseError e) {
Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();

View File

@@ -30,7 +30,7 @@ public final class Util {
static { static {
// Using bold face causes crashes in android 4.1, see: // Using bold face causes crashes in android 4.1, see:
// http://code.google.com/p/android/issues/detail?id=34872 // http://code.google.com/p/android/issues/detail?id=34872
final int sdkVersion = Integer.parseInt(Build.VERSION.SDK); final int sdkVersion = Build.VERSION.SDK_INT;
if (sdkVersion == 16) { if (sdkVersion == 16) {
boldStart = ""; boldStart = "";
boldStop = ""; boldStop = "";

View File

@@ -42,6 +42,9 @@ import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@@ -53,7 +56,6 @@ import android.os.Handler;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.view.MotionEventCompat; import android.support.v4.view.MotionEventCompat;
import android.text.ClipboardManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
@@ -450,15 +452,20 @@ public class EditBoard extends Activity {
setPosFields(); setPosFields();
String fen = TextIO.toFEN(cb.pos) + "\n"; String fen = TextIO.toFEN(cb.pos) + "\n";
ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(fen); clipboard.setPrimaryClip(new ClipData(fen,
new String[]{ "application/x-chess-fen", ClipDescription.MIMETYPE_TEXT_PLAIN },
new ClipData.Item(fen)));
setSelection(-1); setSelection(-1);
break; break;
} }
case PASTE_POSITION: { case PASTE_POSITION: {
ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
if (clipboard.hasText()) { if (clipboard.hasPrimaryClip()) {
String fen = clipboard.getText().toString(); ClipData clip = clipboard.getPrimaryClip();
setFEN(fen); if (clip.getItemCount() > 0) {
String fen = clip.getItemAt(0).coerceToText(getApplicationContext()).toString();
setFEN(fen);
}
} }
break; break;
} }

View File

@@ -28,9 +28,12 @@ import org.petero.droidfish.Util;
import android.app.Dialog; import android.app.Dialog;
import android.app.ListActivity; import android.app.ListActivity;
import android.app.LoaderManager;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.CursorLoader;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
@@ -71,6 +74,41 @@ public class LoadScid extends ListActivity {
private long lastModTime = -1; private long lastModTime = -1;
Thread workThread = null; Thread workThread = null;
private int idIdx;
private int summaryIdx;
private boolean resultSentBack = false;
private interface OnCursorReady {
void run(Cursor cursor);
}
private void startReadFile(final OnCursorReady r) {
getLoaderManager().restartLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String scidFileName = fileName.substring(0, fileName.indexOf("."));
String[] proj = new String[]{"_id", "summary"};
return new CursorLoader(getApplicationContext(),
Uri.parse("content://org.scid.database.scidprovider/games"),
proj, scidFileName, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, final Cursor cursor) {
idIdx = cursor.getColumnIndex("_id");
summaryIdx = cursor.getColumnIndex("summary");
workThread = new Thread(new Runnable() {
public void run() {
r.run(cursor);
}
});
workThread.start();
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
});
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -91,12 +129,14 @@ public class LoadScid extends ListActivity {
Intent i = getIntent(); Intent i = getIntent();
String action = i.getAction(); String action = i.getAction();
fileName = i.getStringExtra("org.petero.droidfish.pathname"); fileName = i.getStringExtra("org.petero.droidfish.pathname");
resultSentBack = false;
if (action.equals("org.petero.droidfish.loadScid")) { if (action.equals("org.petero.droidfish.loadScid")) {
showDialog(PROGRESS_DIALOG); showDialog(PROGRESS_DIALOG);
final LoadScid lpgn = this; final LoadScid lpgn = this;
workThread = new Thread(new Runnable() { startReadFile(new OnCursorReady() {
public void run() { @Override
if (!readFile()) public void run(Cursor cursor) {
if (!readFile(cursor))
return; return;
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
public void run() { public void run() {
@@ -105,20 +145,20 @@ public class LoadScid extends ListActivity {
}); });
} }
}); });
workThread.start();
} else if (action.equals("org.petero.droidfish.loadScidNextGame") || } else if (action.equals("org.petero.droidfish.loadScidNextGame") ||
action.equals("org.petero.droidfish.loadScidPrevGame")) { action.equals("org.petero.droidfish.loadScidPrevGame")) {
boolean next = action.equals("org.petero.droidfish.loadScidNextGame"); boolean next = action.equals("org.petero.droidfish.loadScidNextGame");
final int loadItem = defaultItem + (next ? 1 : -1); final int loadItem = defaultItem + (next ? 1 : -1);
if (loadItem < 0) { if (loadItem < 0) {
Toast.makeText(getApplicationContext(), R.string.no_prev_game, Toast.makeText(getApplicationContext(), R.string.no_prev_game,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
finish(); finish();
} else { } else {
workThread = new Thread(new Runnable() { startReadFile(new OnCursorReady() {
public void run() { @Override
if (!readFile()) public void run(Cursor cursor) {
if (!readFile(cursor))
return; return;
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
public void run() { public void run() {
@@ -135,7 +175,6 @@ public class LoadScid extends ListActivity {
}); });
} }
}); });
workThread.start();
} }
} else { // Unsupported action } else { // Unsupported action
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
@@ -225,7 +264,7 @@ public class LoadScid extends ListActivity {
} }
} }
private final boolean readFile() { private final boolean readFile(Cursor cursor) {
if (!fileName.equals(lastFileName)) if (!fileName.equals(lastFileName))
defaultItem = 0; defaultItem = 0;
long modTime = new File(fileName).lastModified(); long modTime = new File(fileName).lastModified();
@@ -235,7 +274,6 @@ public class LoadScid extends ListActivity {
lastFileName = fileName; lastFileName = fileName;
gamesInFile.clear(); gamesInFile.clear();
Cursor cursor = getListCursor();
if (cursor != null) { if (cursor != null) {
int noGames = cursor.getCount(); int noGames = cursor.getCount();
gamesInFile.ensureCapacity(noGames); gamesInFile.ensureCapacity(noGames);
@@ -269,36 +307,6 @@ public class LoadScid extends ListActivity {
return true; return true;
} }
private int idIdx;
private int summaryIdx;
private Cursor getListCursor() {
String scidFileName = fileName.substring(0, fileName.indexOf("."));
String[] proj = new String[]{"_id", "summary"};
try {
Cursor cursor = managedQuery(Uri.parse("content://org.scid.database.scidprovider/games"),
proj, scidFileName, null, null);
idIdx = cursor.getColumnIndex("_id");
summaryIdx = cursor.getColumnIndex("summary");
return cursor;
} catch (Throwable t) {
return null;
}
}
private Cursor getOneGameCursor(int gameId) {
String scidFileName = fileName.substring(0, fileName.indexOf("."));
String[] proj = new String[]{"pgn"};
try {
String uri = String.format(Locale.US, "content://org.scid.database.scidprovider/games/%d", gameId);
Cursor cursor = managedQuery(Uri.parse(uri),
proj, scidFileName, null, null);
return cursor;
} catch (Throwable t) {
return null;
}
}
private void addGameInfo(Cursor cursor) { private void addGameInfo(Cursor cursor) {
GameInfo gi = new GameInfo(); GameInfo gi = new GameInfo();
gi.gameId = cursor.getInt(idIdx); gi.gameId = cursor.getInt(idIdx);
@@ -306,19 +314,42 @@ public class LoadScid extends ListActivity {
gamesInFile.add(gi); gamesInFile.add(gi);
} }
private final void sendBackResult(GameInfo gi) { private final void sendBackResult(final GameInfo gi) {
if (gi.gameId >= 0) { if (resultSentBack)
Cursor cursor = getOneGameCursor(gi.gameId); return;
if (cursor != null && cursor.moveToFirst()) { resultSentBack = true;
String pgn = cursor.getString(cursor.getColumnIndex("pgn")); if (gi.gameId < 0) {
if (pgn != null && pgn.length() > 0) { setResult(RESULT_CANCELED);
setResult(RESULT_OK, (new Intent()).setAction(pgn)); finish();
finish();
return;
}
}
} }
setResult(RESULT_CANCELED);
finish(); getLoaderManager().restartLoader(1, null, new LoaderManager.LoaderCallbacks<Cursor>() {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String scidFileName = fileName.substring(0, fileName.indexOf("."));
String[] proj = new String[]{"pgn"};
String uri = String.format(Locale.US, "content://org.scid.database.scidprovider/games/%d",
gi.gameId);
return new CursorLoader(getApplicationContext(),
Uri.parse(uri),
proj, scidFileName, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, final Cursor cursor) {
if (cursor != null && cursor.moveToFirst()) {
String pgn = cursor.getString(cursor.getColumnIndex("pgn"));
if (pgn != null && pgn.length() > 0) {
setResult(RESULT_OK, (new Intent()).setAction(pgn));
finish();
return;
}
}
setResult(RESULT_CANCELED);
finish();
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
});
} }
} }

View File

@@ -18,15 +18,9 @@
package org.petero.droidfish.engine; package org.petero.droidfish.engine;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
import org.petero.droidfish.EngineOptions; import org.petero.droidfish.EngineOptions;
import org.petero.droidfish.book.BookOptions; import org.petero.droidfish.book.BookOptions;