Skip to content

Commit

Permalink
Add the protection feature to make sure checkSelfPermission always ex…
Browse files Browse the repository at this point in the history
…ists.
  • Loading branch information
sinyu890807 committed Jul 10, 2017
1 parent 5995171 commit f88195f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class DatabaseGenerateException extends RuntimeException {
/**
* Don't have permission to create database on sdcard.
*/
public static final String EXTERNAL_STORAGE_PERMISSION_DENIED = "You don't have permission to create database at %1$s. Make sure you handled WRITE_EXTERNAL_STORAGE runtime permission correctly.";
public static final String EXTERNAL_STORAGE_PERMISSION_DENIED = "You don't have permission to access database at %1$s. Make sure you handled WRITE_EXTERNAL_STORAGE runtime permission correctly.";

/**
* Constructor of DatabaseGenerateException.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.litepal.LitePalApplication;
import org.litepal.exceptions.DatabaseGenerateException;
import org.litepal.parser.LitePalAttr;
import org.litepal.util.BaseUtility;

import java.io.File;

Expand Down Expand Up @@ -110,7 +111,8 @@ private static LitePalOpenHelper buildConnection() {
// internal or empty means internal storage, neither or them means sdcard storage
String dbPath = Environment.getExternalStorageDirectory().getPath() + "/" + litePalAttr.getStorage();
dbPath = dbPath.replace("//", "/");
if (ContextCompat.checkSelfPermission(LitePalApplication.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (BaseUtility.isClassAndMethodExist("android.support.v4.content.ContextCompat", "checkSelfPermission") &&
ContextCompat.checkSelfPermission(LitePalApplication.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
throw new DatabaseGenerateException(String.format(DatabaseGenerateException.EXTERNAL_STORAGE_PERMISSION_DENIED, dbPath));
}
File path = new File(dbPath);
Expand Down
25 changes: 25 additions & 0 deletions litepal/src/main/java/org/litepal/util/BaseUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.litepal.parser.LitePalAttr;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Locale;

Expand Down Expand Up @@ -245,4 +246,28 @@ public static boolean isLitePalXMLExists() {
return false;
}

/**
* Check the existence of the specific class and method.
*
* @param className
* Class name with full package name.
* @param methodName
* Method name.
* @return Return true if both of class and method are exist. Otherwise return false.
*/
public static boolean isClassAndMethodExist(String className, String methodName) {
try {
Class<?> clazz = Class.forName(className);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

}

0 comments on commit f88195f

Please sign in to comment.