From a3207790ab9a9c03bee764363be0ceeb3cf21b96 Mon Sep 17 00:00:00 2001 From: hss01248 Date: Tue, 6 Jul 2021 11:42:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8DMANAGE=5FEXTERNAL=5FSTORAGE?= =?UTF-8?q?=E5=88=86=E5=8C=BA=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hss01248/accountcache/AccountCacher.java | 75 ++++++++++++------- .../com/hss01248/accountcache/MyDbUtil.java | 4 +- app/build.gradle | 6 +- app/src/main/AndroidManifest.xml | 2 + .../accountcacherdemo/MainActivity.java | 1 + 5 files changed, 55 insertions(+), 33 deletions(-) diff --git a/accountcache/src/main/java/com/hss01248/accountcache/AccountCacher.java b/accountcache/src/main/java/com/hss01248/accountcache/AccountCacher.java index 113e358..35b47cd 100644 --- a/accountcache/src/main/java/com/hss01248/accountcache/AccountCacher.java +++ b/accountcache/src/main/java/com/hss01248/accountcache/AccountCacher.java @@ -8,6 +8,7 @@ import android.util.Base64; import android.widget.Toast; +import androidx.annotation.Nullable; import androidx.fragment.app.FragmentActivity; import com.hjq.permissions.OnPermissionCallback; @@ -31,15 +32,29 @@ public class AccountCacher { public static int TYPE_TEST = 3; public static int TYPE_DEV = 1; - public static void configHostType(int dev,int test,int release){ + static boolean hasAdaptScopedStorage; + static String dbName = ""; + + public static void configHostType(int dev, int test, int release) { TYPE_RELEASE = release; TYPE_DEV = dev; TYPE_TEST = test; } - static void init(Application application) { + /** + * 非必须 + * + * @param dbName 可以为空. 为空则存储于默认数据库 + * @param hasAdaptScopedStorage 是否已经适配Android11的分区存储 + */ + public static void init(@Nullable String dbName, boolean hasAdaptScopedStorage) { + AccountCacher.dbName = dbName; + AccountCacher.hasAdaptScopedStorage = hasAdaptScopedStorage; + } + + static void init(Application application) { app = application; - XXPermissions.setScopedStorage(false); + XXPermissions.setScopedStorage(hasAdaptScopedStorage); } /** @@ -61,26 +76,30 @@ public static void selectAccount(int hostType, FragmentActivity activity, String }*/ + XXPermissions permissions = XXPermissions.with(activity); + if (hasAdaptScopedStorage) { + permissions.permission(Permission.MANAGE_EXTERNAL_STORAGE); + } else { + permissions.permission(Permission.Group.STORAGE); + } + // .permission(hasAdaptScopedStorage ? Permission.Group.STORAGE : Permission.MANAGE_EXTERNAL_STORAGE) + //.permission(Permission.READ_EXTERNAL_STORAGE) + // 申请多个权限 + // .permission(Permission.Group.CALENDAR) + permissions.request(new OnPermissionCallback() { - XXPermissions.with(activity) - .permission(Permission.Group.STORAGE) - //.permission(Permission.READ_EXTERNAL_STORAGE) - // 申请多个权限 - // .permission(Permission.Group.CALENDAR) - .request(new OnPermissionCallback() { - - @Override - public void onGranted(List permissions, boolean all) { - List accounts = MyDbUtil.getAll(hostType, countryCode); + @Override + public void onGranted(List permissions, boolean all) { + List accounts = MyDbUtil.getAll(hostType, countryCode); - showSelectAccountDialog(hostType, activity, countryCode, accounts, callback); - } + showSelectAccountDialog(hostType, activity, countryCode, accounts, callback); + } - @Override - public void onDenied(List permissions, boolean never) { - Toast.makeText(activity.getApplicationContext(), "需要存储权限", Toast.LENGTH_LONG).show(); - } - }); + @Override + public void onDenied(List permissions, boolean never) { + Toast.makeText(activity.getApplicationContext(), "需要存储权限", Toast.LENGTH_LONG).show(); + } + }); } private static boolean isNotDevOrTest(int hostType) { @@ -93,24 +112,24 @@ private static void showSelectAccountDialog(int hostType, Activity activity, Str DebugAccount accountLast = null; int idx = 0; - if(accounts != null && !accounts.isEmpty()){ + if (accounts != null && !accounts.isEmpty()) { for (int i = 0; i < accounts.size(); i++) { DebugAccount account = accounts.get(i); - if(accountLast == null){ + if (accountLast == null) { idx = i; accountLast = account; - }else { + } else { - if(accountLast.updateTime < account.updateTime){ + if (accountLast.updateTime < account.updateTime) { accountLast = account; idx = i; } } } } - if(idx != 0){ + if (idx != 0) { accounts.remove(idx); - accounts.add(0,accountLast); + accounts.add(0, accountLast); } String[] strs = new String[accounts.size()]; @@ -181,8 +200,8 @@ public void onGranted(List permissions, boolean all) { List list = MyDbUtil.getDaoSession().getDebugAccountDao() .queryBuilder().where(DebugAccountDao.Properties.Account.eq(account) - ,DebugAccountDao.Properties.CountryCode.eq(countryCode) - ,DebugAccountDao.Properties.HostType.eq(currentHostType)).list(); + , DebugAccountDao.Properties.CountryCode.eq(countryCode) + , DebugAccountDao.Properties.HostType.eq(currentHostType)).list(); if (list == null || list.isEmpty()) { DebugAccount debugAccount = new DebugAccount(); debugAccount.account = account; diff --git a/accountcache/src/main/java/com/hss01248/accountcache/MyDbUtil.java b/accountcache/src/main/java/com/hss01248/accountcache/MyDbUtil.java index 013b419..76a3bf8 100644 --- a/accountcache/src/main/java/com/hss01248/accountcache/MyDbUtil.java +++ b/accountcache/src/main/java/com/hss01248/accountcache/MyDbUtil.java @@ -19,8 +19,8 @@ public class MyDbUtil { */ private static void initGreenDao(Application context) { Context context2 = new MyDBContext(context); - DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context2, context2.getPackageName()+"testaccount2.db"); - Database db = helper.getEncryptedReadableDb("856yuv98"); + DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context2, AccountCacher.dbName+"testaccount2.db"); + Database db = helper.getEncryptedReadableDb(AccountCacher.dbName+"856yuv98"); DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); } diff --git a/app/build.gradle b/app/build.gradle index 09dfb7d..b2f90b4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,13 +6,13 @@ plugins { } android { - compileSdkVersion 29 - buildToolsVersion "29.0.0" + compileSdkVersion 30 + buildToolsVersion "30.0.0" defaultConfig { applicationId "com.hss01248.accountcacherdemo" minSdkVersion 16 - targetSdkVersion 29 + targetSdkVersion 30 versionCode 1 versionName "1.0" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 292e6eb..250cbcf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + +