mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2025-04-01 09:51:00 +02:00
DroidFish: Request external storage permission in android 6.
This commit is contained in:
parent
46fa159cc1
commit
ad9c97a3ef
Binary file not shown.
@ -10,5 +10,5 @@
|
|||||||
# 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-16
|
target=android-23
|
||||||
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
||||||
|
@ -65,6 +65,7 @@ import com.kalab.chess.enginesupport.ChessEngineResolver;
|
|||||||
import com.larvalabs.svgandroid.SVG;
|
import com.larvalabs.svgandroid.SVG;
|
||||||
import com.larvalabs.svgandroid.SVGParser;
|
import com.larvalabs.svgandroid.SVGParser;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
@ -101,6 +102,8 @@ import android.os.Environment;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Vibrator;
|
import android.os.Vibrator;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.support.v4.app.ActivityCompat;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.support.v4.view.MotionEventCompat;
|
import android.support.v4.view.MotionEventCompat;
|
||||||
import android.support.v4.widget.DrawerLayout;
|
import android.support.v4.widget.DrawerLayout;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
@ -138,7 +141,9 @@ import android.widget.TextView;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
@SuppressLint("ClickableViewAccessibility")
|
@SuppressLint("ClickableViewAccessibility")
|
||||||
public class DroidFish extends Activity implements GUIInterface {
|
public class DroidFish extends Activity
|
||||||
|
implements GUIInterface,
|
||||||
|
ActivityCompat.OnRequestPermissionsResultCallback {
|
||||||
// FIXME!!! PGN view option: game continuation (for training)
|
// FIXME!!! PGN view option: game continuation (for training)
|
||||||
// FIXME!!! Remove invalid playerActions in PGN import (should be done in verifyChildren)
|
// FIXME!!! Remove invalid playerActions in PGN import (should be done in verifyChildren)
|
||||||
// FIXME!!! Implement bookmark mechanism for positions in pgn files
|
// FIXME!!! Implement bookmark mechanism for positions in pgn files
|
||||||
@ -219,6 +224,16 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
}
|
}
|
||||||
private AutoMode autoMode = AutoMode.OFF;
|
private AutoMode autoMode = AutoMode.OFF;
|
||||||
|
|
||||||
|
/** State of requested permissions. */
|
||||||
|
private static enum PermissionState {
|
||||||
|
UNKNOWN,
|
||||||
|
REQUESTED,
|
||||||
|
GRANTED,
|
||||||
|
DENIED
|
||||||
|
}
|
||||||
|
/** State of WRITE_EXTERNAL_STORAGE permission. */
|
||||||
|
private PermissionState storagePermission = PermissionState.UNKNOWN;
|
||||||
|
|
||||||
private final static String bookDir = "DroidFish/book";
|
private final static String bookDir = "DroidFish/book";
|
||||||
private final static String pgnDir = "DroidFish/pgn";
|
private final static String pgnDir = "DroidFish/pgn";
|
||||||
private final static String fenDir = "DroidFish/epd";
|
private final static String fenDir = "DroidFish/epd";
|
||||||
@ -357,7 +372,7 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
public String getId() { return "loadLastFile"; }
|
public String getId() { return "loadLastFile"; }
|
||||||
public int getName() { return R.string.load_last_file; }
|
public int getName() { return R.string.load_last_file; }
|
||||||
public int getIcon() { return R.raw.open_last_file; }
|
public int getIcon() { return R.raw.open_last_file; }
|
||||||
public boolean enabled() { return currFileType() != FT_NONE; }
|
public boolean enabled() { return currFileType() != FT_NONE && storageAvailable(); }
|
||||||
public void run() {
|
public void run() {
|
||||||
loadLastFile();
|
loadLastFile();
|
||||||
}
|
}
|
||||||
@ -491,6 +506,19 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
|
|
||||||
/** Create directory structure on SD card. */
|
/** Create directory structure on SD card. */
|
||||||
private final void createDirectories() {
|
private final void createDirectories() {
|
||||||
|
if (storagePermission == PermissionState.UNKNOWN) {
|
||||||
|
String extStorage = Manifest.permission.WRITE_EXTERNAL_STORAGE;
|
||||||
|
if (ContextCompat.checkSelfPermission(this, extStorage) ==
|
||||||
|
PackageManager.PERMISSION_GRANTED) {
|
||||||
|
storagePermission = PermissionState.GRANTED;
|
||||||
|
} else {
|
||||||
|
ActivityCompat.requestPermissions(this, new String[]{extStorage}, 0);
|
||||||
|
storagePermission = PermissionState.REQUESTED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (storagePermission != PermissionState.GRANTED)
|
||||||
|
return;
|
||||||
|
|
||||||
File extDir = Environment.getExternalStorageDirectory();
|
File extDir = Environment.getExternalStorageDirectory();
|
||||||
String sep = File.separator;
|
String sep = File.separator;
|
||||||
new File(extDir + sep + bookDir).mkdirs();
|
new File(extDir + sep + bookDir).mkdirs();
|
||||||
@ -502,6 +530,22 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
new File(extDir + sep + rtbDefaultDir).mkdirs();
|
new File(extDir + sep + rtbDefaultDir).mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int code, String[] permissions, int[] results) {
|
||||||
|
if (storagePermission == PermissionState.REQUESTED) {
|
||||||
|
if ((results.length > 0) && (results[0] == PackageManager.PERMISSION_GRANTED))
|
||||||
|
storagePermission = PermissionState.GRANTED;
|
||||||
|
else
|
||||||
|
storagePermission = PermissionState.DENIED;
|
||||||
|
}
|
||||||
|
createDirectories();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return true if the WRITE_EXTERNAL_STORAGE permission has been granted. */
|
||||||
|
private boolean storageAvailable() {
|
||||||
|
return storagePermission == PermissionState.GRANTED;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return PGN/FEN data or filename from the Intent. Both can not be non-null.
|
* Return PGN/FEN data or filename from the Intent. Both can not be non-null.
|
||||||
* @return Pair of PGN/FEN data and filename.
|
* @return Pair of PGN/FEN data and filename.
|
||||||
@ -1184,6 +1228,10 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final void setEngineStrength(String engine, int strength) {
|
private final void setEngineStrength(String engine, int strength) {
|
||||||
|
if (!storageAvailable()) {
|
||||||
|
if (!"stockfish".equals(engine) && !"cuckoochess".equals(engine))
|
||||||
|
engine = "stockfish";
|
||||||
|
}
|
||||||
ctrl.setEngineStrength(engine, strength);
|
ctrl.setEngineStrength(engine, strength);
|
||||||
setEngineTitle(engine, strength);
|
setEngineTitle(engine, strength);
|
||||||
}
|
}
|
||||||
@ -1402,8 +1450,10 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ITEM_FILE_MENU:
|
case ITEM_FILE_MENU:
|
||||||
removeDialog(FILE_MENU_DIALOG);
|
if (storageAvailable()) {
|
||||||
showDialog(FILE_MENU_DIALOG);
|
removeDialog(FILE_MENU_DIALOG);
|
||||||
|
showDialog(FILE_MENU_DIALOG);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ITEM_RESIGN:
|
case ITEM_RESIGN:
|
||||||
if (ctrl.humansTurn())
|
if (ctrl.humansTurn())
|
||||||
@ -1421,12 +1471,19 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ITEM_SELECT_BOOK:
|
case ITEM_SELECT_BOOK:
|
||||||
removeDialog(SELECT_BOOK_DIALOG);
|
if (storageAvailable()) {
|
||||||
showDialog(SELECT_BOOK_DIALOG);
|
removeDialog(SELECT_BOOK_DIALOG);
|
||||||
|
showDialog(SELECT_BOOK_DIALOG);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ITEM_MANAGE_ENGINES:
|
case ITEM_MANAGE_ENGINES:
|
||||||
removeDialog(MANAGE_ENGINES_DIALOG);
|
if (storageAvailable()) {
|
||||||
showDialog(MANAGE_ENGINES_DIALOG);
|
removeDialog(MANAGE_ENGINES_DIALOG);
|
||||||
|
showDialog(MANAGE_ENGINES_DIALOG);
|
||||||
|
} else {
|
||||||
|
removeDialog(SELECT_ENGINE_DIALOG_NOMANAGE);
|
||||||
|
showDialog(SELECT_ENGINE_DIALOG_NOMANAGE);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ITEM_SET_COLOR_THEME:
|
case ITEM_SET_COLOR_THEME:
|
||||||
showDialog(SET_COLOR_THEME_DIALOG);
|
showDialog(SET_COLOR_THEME_DIALOG);
|
||||||
@ -2029,7 +2086,9 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
List<CharSequence> lst = new ArrayList<CharSequence>();
|
List<CharSequence> lst = new ArrayList<CharSequence>();
|
||||||
List<Integer> actions = new ArrayList<Integer>();
|
List<Integer> actions = new ArrayList<Integer>();
|
||||||
lst.add(getString(R.string.clipboard)); actions.add(CLIPBOARD);
|
lst.add(getString(R.string.clipboard)); actions.add(CLIPBOARD);
|
||||||
lst.add(getString(R.string.option_file)); actions.add(FILEMENU);
|
if (storageAvailable()) {
|
||||||
|
lst.add(getString(R.string.option_file)); actions.add(FILEMENU);
|
||||||
|
}
|
||||||
lst.add(getString(R.string.share)); actions.add(SHARE);
|
lst.add(getString(R.string.share)); actions.add(SHARE);
|
||||||
if (hasFenProvider(getPackageManager())) {
|
if (hasFenProvider(getPackageManager())) {
|
||||||
lst.add(getString(R.string.get_fen)); actions.add(GET_FEN);
|
lst.add(getString(R.string.get_fen)); actions.add(GET_FEN);
|
||||||
@ -2222,42 +2281,44 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
ids.add("stockfish"); items.add(getString(R.string.stockfish_engine));
|
ids.add("stockfish"); items.add(getString(R.string.stockfish_engine));
|
||||||
ids.add("cuckoochess"); items.add(getString(R.string.cuckoochess_engine));
|
ids.add("cuckoochess"); items.add(getString(R.string.cuckoochess_engine));
|
||||||
|
|
||||||
final String sep = File.separator;
|
if (storageAvailable()) {
|
||||||
final String base = Environment.getExternalStorageDirectory() + sep + engineDir + sep;
|
final String sep = File.separator;
|
||||||
{
|
final String base = Environment.getExternalStorageDirectory() + sep + engineDir + sep;
|
||||||
ChessEngineResolver resolver = new ChessEngineResolver(this);
|
{
|
||||||
List<ChessEngine> engines = resolver.resolveEngines();
|
ChessEngineResolver resolver = new ChessEngineResolver(this);
|
||||||
ArrayList<Pair<String,String>> oexEngines = new ArrayList<Pair<String,String>>();
|
List<ChessEngine> engines = resolver.resolveEngines();
|
||||||
for (ChessEngine engine : engines) {
|
ArrayList<Pair<String,String>> oexEngines = new ArrayList<Pair<String,String>>();
|
||||||
if ((engine.getName() != null) && (engine.getFileName() != null) &&
|
for (ChessEngine engine : engines) {
|
||||||
(engine.getPackageName() != null)) {
|
if ((engine.getName() != null) && (engine.getFileName() != null) &&
|
||||||
oexEngines.add(new Pair<String,String>(EngineUtil.openExchangeFileName(engine),
|
(engine.getPackageName() != null)) {
|
||||||
engine.getName()));
|
oexEngines.add(new Pair<String,String>(EngineUtil.openExchangeFileName(engine),
|
||||||
|
engine.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Collections.sort(oexEngines, new Comparator<Pair<String,String>>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Pair<String, String> lhs, Pair<String, String> rhs) {
|
||||||
|
return lhs.second.compareTo(rhs.second);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for (Pair<String,String> eng : oexEngines) {
|
||||||
|
ids.add(base + EngineUtil.openExchangeDir + sep + eng.first);
|
||||||
|
items.add(eng.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Collections.sort(oexEngines, new Comparator<Pair<String,String>>() {
|
|
||||||
|
String[] fileNames = findFilesInDirectory(engineDir, new FileNameFilter() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(Pair<String, String> lhs, Pair<String, String> rhs) {
|
public boolean accept(String filename) {
|
||||||
return lhs.second.compareTo(rhs.second);
|
return !reservedEngineName(filename);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (Pair<String,String> eng : oexEngines) {
|
for (String file : fileNames) {
|
||||||
ids.add(base + EngineUtil.openExchangeDir + sep + eng.first);
|
ids.add(base + file);
|
||||||
items.add(eng.second);
|
items.add(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] fileNames = findFilesInDirectory(engineDir, new FileNameFilter() {
|
|
||||||
@Override
|
|
||||||
public boolean accept(String filename) {
|
|
||||||
return !reservedEngineName(filename);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
for (String file : fileNames) {
|
|
||||||
ids.add(base + file);
|
|
||||||
items.add(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
String currEngine = ctrl.getEngine();
|
String currEngine = ctrl.getEngine();
|
||||||
int defaultItem = 0;
|
int defaultItem = 0;
|
||||||
final int nEngines = items.size();
|
final int nEngines = items.size();
|
||||||
@ -2900,6 +2961,8 @@ public class DroidFish extends Activity implements GUIInterface {
|
|||||||
|
|
||||||
/** Return true if engine UCI options can be set now. */
|
/** Return true if engine UCI options can be set now. */
|
||||||
private final boolean canSetEngineOptions() {
|
private final boolean canSetEngineOptions() {
|
||||||
|
if (!storageAvailable())
|
||||||
|
return false;
|
||||||
UCIOptions uciOpts = ctrl.getUCIOptions();
|
UCIOptions uciOpts = ctrl.getUCIOptions();
|
||||||
if (uciOpts == null)
|
if (uciOpts == null)
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user