use preferred SDK-8 API to look for downloads directory, and wrap it

in an interface loaded only when SDK>=8 to avoid load-time crash on
older devices.
This commit is contained in:
Eric House 2012-12-05 07:32:11 -08:00
parent a035ef5623
commit af853098f2
3 changed files with 45 additions and 4 deletions

View file

@ -26,7 +26,7 @@
android:versionName="@string/app_version"
>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

View file

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

View file

@ -47,6 +47,20 @@ import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
public class DictUtils {
// Standard hack for using APIs from an SDK in code to ship on
// older devices that don't support it: prevent class loader from
// seeing something it'll barf on by loading it manually
private static interface SafeDirGetter {
public File getDownloadDir();
}
private static SafeDirGetter s_dirGetter = null;
static {
int sdkVersion = Integer.valueOf( android.os.Build.VERSION.SDK );
if ( 8 <= sdkVersion ) {
s_dirGetter = new DirGetter();
}
}
// keep in sync with loc_names string-array
public enum DictLoc { UNKNOWN, BUILT_IN, INTERNAL, EXTERNAL, DOWNLOAD };
public static final String INVITED = "invited";
@ -566,10 +580,21 @@ public class DictUtils {
return null != getDownloadDir( context );
}
// The approved way to get this is to call Environment.
// getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
// but at least on my Samsung Galaxy Blaze 4G it returns a
// directory that does not exist! (FU, Samsung...) So in that
// case fall back to using getExternalStorageDirectory and
// appending "download"
public static File getDownloadDir( Context context )
{
File result = null;
if ( haveWriteableSD() ) {
if ( null != s_dirGetter ) {
result = s_dirGetter.getDownloadDir();
}
if ( null != result ) {
// we're done
} else if ( haveWriteableSD() ) {
File file = null;
String myPath = XWPrefs.getMyDownloadDir( context );
if ( null != myPath && 0 < myPath.length() ) {
@ -577,7 +602,11 @@ public class DictUtils {
} else {
file = Environment.getExternalStorageDirectory();
if ( null != file ) {
file = new File( file, "download/" );
File child = new File( file, "download/" );
if ( child.exists() && child.isDirectory()
&& child.canWrite() ) {
file = child;
}
}
}
if ( null != file && file.exists() && file.isDirectory() ) {
@ -596,4 +625,16 @@ public class DictUtils {
}
return result;
}
private static class DirGetter implements SafeDirGetter {
public File getDownloadDir()
{
File path = Environment.
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if ( null != path && !path.canWrite() ) {
path = null;
}
return path;
}
}
}