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

View File

@ -30,7 +30,7 @@ public final class Util {
static {
// Using bold face causes crashes in android 4.1, see:
// 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) {
boldStart = "";
boldStop = "";

View File

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

View File

@ -28,9 +28,12 @@ import org.petero.droidfish.Util;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.app.ProgressDialog;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences.Editor;
@ -71,6 +74,41 @@ public class LoadScid extends ListActivity {
private long lastModTime = -1;
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
protected void onCreate(Bundle savedInstanceState) {
@ -91,12 +129,14 @@ public class LoadScid extends ListActivity {
Intent i = getIntent();
String action = i.getAction();
fileName = i.getStringExtra("org.petero.droidfish.pathname");
resultSentBack = false;
if (action.equals("org.petero.droidfish.loadScid")) {
showDialog(PROGRESS_DIALOG);
final LoadScid lpgn = this;
workThread = new Thread(new Runnable() {
public void run() {
if (!readFile())
startReadFile(new OnCursorReady() {
@Override
public void run(Cursor cursor) {
if (!readFile(cursor))
return;
runOnUiThread(new Runnable() {
public void run() {
@ -105,20 +145,20 @@ public class LoadScid extends ListActivity {
});
}
});
workThread.start();
} else if (action.equals("org.petero.droidfish.loadScidNextGame") ||
action.equals("org.petero.droidfish.loadScidPrevGame")) {
boolean next = action.equals("org.petero.droidfish.loadScidNextGame");
final int loadItem = defaultItem + (next ? 1 : -1);
if (loadItem < 0) {
Toast.makeText(getApplicationContext(), R.string.no_prev_game,
Toast.LENGTH_SHORT).show();
Toast.LENGTH_SHORT).show();
setResult(RESULT_CANCELED);
finish();
} else {
workThread = new Thread(new Runnable() {
public void run() {
if (!readFile())
startReadFile(new OnCursorReady() {
@Override
public void run(Cursor cursor) {
if (!readFile(cursor))
return;
runOnUiThread(new Runnable() {
public void run() {
@ -135,7 +175,6 @@ public class LoadScid extends ListActivity {
});
}
});
workThread.start();
}
} else { // Unsupported action
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))
defaultItem = 0;
long modTime = new File(fileName).lastModified();
@ -235,7 +274,6 @@ public class LoadScid extends ListActivity {
lastFileName = fileName;
gamesInFile.clear();
Cursor cursor = getListCursor();
if (cursor != null) {
int noGames = cursor.getCount();
gamesInFile.ensureCapacity(noGames);
@ -269,36 +307,6 @@ public class LoadScid extends ListActivity {
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) {
GameInfo gi = new GameInfo();
gi.gameId = cursor.getInt(idIdx);
@ -306,19 +314,42 @@ public class LoadScid extends ListActivity {
gamesInFile.add(gi);
}
private final void sendBackResult(GameInfo gi) {
if (gi.gameId >= 0) {
Cursor cursor = getOneGameCursor(gi.gameId);
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;
}
}
private final void sendBackResult(final GameInfo gi) {
if (resultSentBack)
return;
resultSentBack = true;
if (gi.gameId < 0) {
setResult(RESULT_CANCELED);
finish();
}
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;
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.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import org.petero.droidfish.EngineOptions;
import org.petero.droidfish.book.BookOptions;