Improve Android compatibility and Maven publication configuration
This commit is contained in:
1 parent
96c1d26a57
commit
e9d9281333
16 files changed
+226
-143
No files matched your search
@@ -45,6 +45,12 @@ dependencies {
|
||||
}
|
||||
|
||||
//apply from: 'bintray-release.gradle'
|
||||
android {
|
||||
publishing {
|
||||
singleVariant('release')
|
||||
}
|
||||
}
|
||||
|
||||
ext {
|
||||
PUBLISH_ARTIFACT_ID = 'public'
|
||||
}
|
||||
|
||||
@@ -59,8 +59,9 @@ public class AriaConfig {
|
||||
/**
|
||||
* 是否已经联网,true 已经联网
|
||||
*/
|
||||
private static boolean isConnectedNet = true;
|
||||
private static volatile boolean isConnectedNet = true;
|
||||
private Handler mAriaHandler;
|
||||
private volatile boolean networkCallbackRegistered;
|
||||
|
||||
private AriaConfig(Context context) {
|
||||
APP = context.getApplicationContext();
|
||||
@@ -134,56 +135,58 @@ public class AriaConfig {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
||||
NetworkRequest request = builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
|
||||
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
|
||||
.build();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
cm.registerNetworkCallback(request, new ConnectivityManager.NetworkCallback() {
|
||||
ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback() {
|
||||
private final java.util.Set<Network> networks = new java.util.HashSet<>();
|
||||
|
||||
@Override public void onLost(Network network) {
|
||||
super.onLost(network);
|
||||
isConnectedNet = isNetworkAvailable();
|
||||
ALog.d(TAG, "onLost, isConnectNet = " + isConnectedNet);
|
||||
}
|
||||
|
||||
@Override public void onAvailable(Network network) {
|
||||
super.onAvailable(network);
|
||||
isConnectedNet = true;
|
||||
ALog.d(TAG, "onAvailable, isConnectNet = true");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isNetworkAvailable() {
|
||||
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
|
||||
ConnectivityManager connectivityManager =
|
||||
(ConnectivityManager) getAPP().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
if (connectivityManager == null) {
|
||||
|
||||
return false;
|
||||
} else {
|
||||
// 获取NetworkInfo对象
|
||||
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
|
||||
|
||||
if (networkInfo != null && networkInfo.length > 0) {
|
||||
for (NetworkInfo info : networkInfo) {
|
||||
// 判断当前网络状态是否为连接状态
|
||||
if (info.getState() == NetworkInfo.State.CONNECTED) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@Override public synchronized void onAvailable(Network network) {
|
||||
networks.add(network);
|
||||
isConnectedNet = true;
|
||||
}
|
||||
|
||||
@Override public synchronized void onLost(Network network) {
|
||||
networks.remove(network);
|
||||
isConnectedNet = !networks.isEmpty();
|
||||
}
|
||||
};
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
cm.registerDefaultNetworkCallback(callback);
|
||||
} else {
|
||||
// 不限制传输类型,支持以太网和 VPN;允许局域网传输,不要求公网验证。
|
||||
NetworkRequest request = new NetworkRequest.Builder()
|
||||
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
|
||||
.build();
|
||||
cm.registerNetworkCallback(request, callback);
|
||||
}
|
||||
networkCallbackRegistered = true;
|
||||
} catch (SecurityException | IllegalArgumentException e) {
|
||||
ALog.w(TAG, "无法注册网络监听:" + e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public boolean isNetworkAvailable() {
|
||||
ConnectivityManager cm =
|
||||
(ConnectivityManager) getAPP().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (cm == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
return cm.getActiveNetwork() != null;
|
||||
}
|
||||
NetworkInfo info = cm.getActiveNetworkInfo();
|
||||
return info != null && info.isConnected();
|
||||
} catch (SecurityException e) {
|
||||
// 宿主移除 ACCESS_NETWORK_STATE 时不能因此阻止网络 I/O。
|
||||
ALog.w(TAG, "无法查询网络状态:" + e.getMessage());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnectedNet() {
|
||||
return isConnectedNet;
|
||||
// 旧系统、关闭监听或注册失败时,查询实时状态。
|
||||
return !networkCallbackRegistered ? isNetworkAvailable() : isConnectedNet;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -259,7 +259,7 @@ class DelegateFind extends AbsDelegate {
|
||||
|
||||
try {
|
||||
while (cursor.moveToNext()) {
|
||||
int pRowId = cursor.getInt(cursor.getColumnIndex(PARENT_COLUMN_ALIAS.concat("rowid")));
|
||||
int pRowId = cursor.getInt(cursor.getColumnIndexOrThrow(PARENT_COLUMN_ALIAS.concat("rowid")));
|
||||
if (childs.get(pRowId) == null) {
|
||||
childs.put(pRowId, new ArrayList<DbEntity>());
|
||||
parents.put(pRowId, createParent(pRowId, parentClazz, pColumn, cursor));
|
||||
@@ -331,7 +331,7 @@ class DelegateFind extends AbsDelegate {
|
||||
Cursor cursor)
|
||||
throws InstantiationException, IllegalAccessException {
|
||||
T child = childClazz.newInstance();
|
||||
child.rowID = cursor.getInt(cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat("rowid")));
|
||||
child.rowID = cursor.getInt(cursor.getColumnIndexOrThrow(CHILD_COLUMN_ALIAS.concat("rowid")));
|
||||
for (Field field : cColumn) {
|
||||
field.setAccessible(true);
|
||||
int columnIndex = cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat(field.getName()));
|
||||
@@ -515,7 +515,7 @@ class DelegateFind extends AbsDelegate {
|
||||
}
|
||||
//当设置了主键,而且主键的类型为integer时,查询RowID等于主键
|
||||
entity.rowID = cursor.getInt(
|
||||
cursor.getColumnIndex(TextUtils.isEmpty(primaryName) ? "rowid" : primaryName));
|
||||
cursor.getColumnIndexOrThrow(TextUtils.isEmpty(primaryName) ? "rowid" : primaryName));
|
||||
//mDataCache.put(getCacheKey(entity), entity);
|
||||
entitys.add(entity);
|
||||
}
|
||||
@@ -587,7 +587,7 @@ class DelegateFind extends AbsDelegate {
|
||||
int[] ids = new int[cursor.getCount()];
|
||||
int i = 0;
|
||||
while (cursor.moveToNext()) {
|
||||
ids[i] = cursor.getInt(cursor.getColumnIndex("rowid"));
|
||||
ids[i] = cursor.getInt(cursor.getColumnIndexOrThrow("rowid"));
|
||||
i++;
|
||||
}
|
||||
cursor.close();
|
||||
|
||||
@@ -212,7 +212,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
List<String> oldTabColumns = new ArrayList<>();
|
||||
|
||||
while (columnC.moveToNext()) {
|
||||
String columnName = columnC.getString(columnC.getColumnIndex("name"));
|
||||
String columnName = columnC.getString(columnC.getColumnIndexOrThrow("name"));
|
||||
oldTabColumns.add(columnName);
|
||||
}
|
||||
columnC.close();
|
||||
@@ -397,8 +397,8 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
c = db.rawQuery(uSql, null);
|
||||
while (c.moveToNext()) {
|
||||
int type;
|
||||
String filePath = c.getString(c.getColumnIndex("filePath"));
|
||||
String url = c.getString(c.getColumnIndex("url"));
|
||||
String filePath = c.getString(c.getColumnIndexOrThrow("filePath"));
|
||||
String url = c.getString(c.getColumnIndexOrThrow("url"));
|
||||
if (url.startsWith("ftp") || url.startsWith("sftp")) {
|
||||
type = ITaskWrapper.D_FTP;
|
||||
} else {
|
||||
|
||||
@@ -21,7 +21,6 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
@@ -344,10 +343,8 @@ public class CommonUtil {
|
||||
if (file != null) {
|
||||
dir = file.getPath() + "/";
|
||||
} else {
|
||||
dir = Environment.getExternalStorageDirectory().getPath()
|
||||
+ "/Android/data/"
|
||||
+ context.getPackageName()
|
||||
+ "/files/";
|
||||
// Android 11+ 不允许手工创建外部应用目录,外部卷不可用时使用内部目录。
|
||||
dir = context.getFilesDir().getPath() + "/";
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
@@ -647,21 +647,14 @@ public class FileUtil {
|
||||
IllegalAccessException {
|
||||
List<String> pathList = new ArrayList<>();
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
StorageManager manager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
|
||||
List<StorageVolume> volumes = manager.getStorageVolumes();
|
||||
|
||||
Class<?> volumeClass = StorageVolume.class;
|
||||
Method getPath = volumeClass.getDeclaredMethod("getPath");
|
||||
//Method isRemovable = volumeClass.getDeclaredMethod("isRemovable");
|
||||
getPath.setAccessible(true);
|
||||
//isRemovable.setAccessible(true);
|
||||
|
||||
for (StorageVolume volume : volumes) {
|
||||
String state = volume.getState();
|
||||
|
||||
if (state.equals(Environment.MEDIA_MOUNTED)) {
|
||||
pathList.add((String) getPath.invoke(volume));
|
||||
if (manager != null) {
|
||||
for (StorageVolume volume : manager.getStorageVolumes()) {
|
||||
File directory = volume.getDirectory();
|
||||
if (Environment.MEDIA_MOUNTED.equals(volume.getState()) && directory != null) {
|
||||
pathList.add(directory.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -103,10 +103,23 @@ public class NetUtils {
|
||||
return netWorkType;
|
||||
}
|
||||
|
||||
// 可选权限由宿主声明;下方已检查授权并捕获运行时撤销。
|
||||
@android.annotation.SuppressLint("MissingPermission")
|
||||
private static boolean isFastMobileNetwork(Context context) {
|
||||
TelephonyManager telephonyManager =
|
||||
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
switch (telephonyManager.getNetworkType()) {
|
||||
if (telephonyManager == null || context.checkCallingOrSelfPermission(
|
||||
android.Manifest.permission.READ_PHONE_STATE)
|
||||
!= android.content.pm.PackageManager.PERMISSION_GRANTED) {
|
||||
return false;
|
||||
}
|
||||
int networkType;
|
||||
try {
|
||||
networkType = telephonyManager.getNetworkType();
|
||||
} catch (SecurityException e) {
|
||||
return false;
|
||||
}
|
||||
switch (networkType) {
|
||||
case TelephonyManager.NETWORK_TYPE_1xRTT:
|
||||
return false; // ~ 50-100 kbps
|
||||
case TelephonyManager.NETWORK_TYPE_CDMA:
|
||||
|
||||
@@ -201,7 +201,8 @@ public class UriFileUtil {
|
||||
* 在系统 Download 目录插入一条 MediaStore 记录(供只有文件名、没有原始路径的旧任务兜底重建使用)
|
||||
*/
|
||||
private static Uri createDownloadUri(Context context, String displayName) {
|
||||
if (context == null || TextUtils.isEmpty(displayName)) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q
|
||||
|| context == null || TextUtils.isEmpty(displayName)) {
|
||||
return null;
|
||||
}
|
||||
ContentValues values = new ContentValues();
|
||||
|
||||
Reference in new issue
Block a user