Skip to content

Commit

Permalink
适配MANAGE_EXTERNAL_STORAGE分区存储
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Jul 6, 2021
1 parent a976990 commit a320779
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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<String> permissions, boolean all) {
List<DebugAccount> accounts = MyDbUtil.getAll(hostType, countryCode);
@Override
public void onGranted(List<String> permissions, boolean all) {
List<DebugAccount> accounts = MyDbUtil.getAll(hostType, countryCode);

showSelectAccountDialog(hostType, activity, countryCode, accounts, callback);
}
showSelectAccountDialog(hostType, activity, countryCode, accounts, callback);
}

@Override
public void onDenied(List<String> permissions, boolean never) {
Toast.makeText(activity.getApplicationContext(), "需要存储权限", Toast.LENGTH_LONG).show();
}
});
@Override
public void onDenied(List<String> permissions, boolean never) {
Toast.makeText(activity.getApplicationContext(), "需要存储权限", Toast.LENGTH_LONG).show();
}
});
}

private static boolean isNotDevOrTest(int hostType) {
Expand All @@ -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()];
Expand Down Expand Up @@ -181,8 +200,8 @@ public void onGranted(List<String> permissions, boolean all) {

List<DebugAccount> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hss01248.accountcacherdemo">

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AccountCacher.init("",true);
}

public void select(View view) {
Expand Down

0 comments on commit a320779

Please sign in to comment.