Skip to content

Commit

Permalink
Added storage API incompatibility mitigation using MANAGE_EXTERNAL_ST…
Browse files Browse the repository at this point in the history
…ORAGE
  • Loading branch information
MewX committed Jul 16, 2023
1 parent d62bdea commit 233b3be
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- TODO: Remove this permission after migration. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.mewx.wenku8.activity;

import static android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

Expand All @@ -15,10 +20,13 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.Theme;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import org.mewx.wenku8.BuildConfig;
import org.mewx.wenku8.MyApp;
import org.mewx.wenku8.R;
import org.mewx.wenku8.async.CheckAppNewVersion;
Expand All @@ -37,8 +45,10 @@


public class MainActivity extends BaseMaterialActivity {
// Below request codes can be any value.
private static final int REQUEST_WRITE_EXTERNAL = 100;
private static final int REQUEST_READ_EXTERNAL = 101;
private static final int APP_STORAGE_ACCESS_REQUEST_CODE = 501;

private static final AtomicBoolean NEW_VERSION_CHECKED = new AtomicBoolean(false);

Expand Down Expand Up @@ -77,19 +87,30 @@ private void initialApp() {
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

// request write permission (112 write permission)
boolean hasPermission = (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL);
}

// request read permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
hasPermission = (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
// Requests storage RW permissions (112 write permission).
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
// Shows a dialog to confirm requesting for the permission.
new MaterialDialog.Builder(MainActivity.this)
.theme(Theme.LIGHT)
.onPositive((ignored1, ignored2) -> {
Intent intent = new Intent(ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, Uri.parse("package:" + BuildConfig.APPLICATION_ID));
startActivityForResult(intent, APP_STORAGE_ACCESS_REQUEST_CODE);
})
.onNegative((ignored1, ignored2) -> {
Toast.makeText(this, getResources().getText(R.string.missing_permission), Toast.LENGTH_LONG).show();
})
.content(R.string.dialog_content_ask_for_storage_permission)
.positiveText(R.string.dialog_positive_ok)
.negativeText(R.string.dialog_negative_preferno)
.show();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// Write permission.
if (missingPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL);
}
// Read permission.
if (missingPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL);
}
Expand Down Expand Up @@ -233,25 +254,35 @@ protected void onResume() {
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && requestCode == APP_STORAGE_ACCESS_REQUEST_CODE) {
// Note that the result Code could either be OK or CANCELLED.
if (Environment.isExternalStorageManager()) {
// Permission granted.
reloadApp();
} else {
Toast.makeText(this, getResources().getText(R.string.missing_permission), Toast.LENGTH_LONG).show();
}
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_WRITE_EXTERNAL:
case REQUEST_READ_EXTERNAL: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//reload my activity with permission granted or use the features what required the permission
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
if (i != null) {
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
case REQUEST_READ_EXTERNAL:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
reloadApp();
} else {
Toast.makeText(this, getResources().getText(R.string.missing_permission), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, getResources().getText(R.string.missing_permission), Toast.LENGTH_LONG).show();
}
}
break;
}

}

@Override
Expand Down Expand Up @@ -283,4 +314,17 @@ public void run() {
}
}

private boolean missingPermission(String permissionName) {
return ContextCompat.checkSelfPermission(this, permissionName) != PackageManager.PERMISSION_GRANTED;
}

private void reloadApp() {
//reload my activity with permission granted or use the features what required the permission
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
if (i != null) {
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,6 @@
<string name="system_warning">註意</string>
<string name="relay_warning">您正在使用MewX提供的第三方中繼服務,點擊查看詳情。</string>
<string name="relay_warning_full">已啟用第三方Wenku8中繼服務,由於以下的可能原因:\n登錄過期,請重新登錄;\n手機網絡異常,試試瀏覽器訪問;\n運營商劫持(部分地區的10086);\nWIFI切換到數據需等待域名解析緩存刷新(約10min);\n服務器暫時無法響應請求;\n此時段請求服務器次數過多。\n\n該中繼服務列表服務由mewx.org提供,與wenku8無關。另外,此列表數據僅供海外用戶使用,並可能滯後網站24小時以上。請勿在中國大陸使用,否則後果自負,望悉知。</string>
<string name="about_qqq">QQ交流群: 427577610</string>
<string name="about_qqq">QQ交流群: 181789084</string>
<string name="dialog_content_ask_for_storage_permission">由於曆史原因,app需要冩入存儲目錄下的wenku8文件夾,是否授予冩入權限?</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,6 @@
<string name="system_warning">註意</string>
<string name="relay_warning">您正在使用MewX提供的第三方中繼服務,點擊查看詳情。</string>
<string name="relay_warning_full">已啟用第三方Wenku8中繼服務,由於以下的可能原因:\n登錄過期,請重新登錄;\n手機網絡異常,試試瀏覽器訪問;\n運營商劫持(部分地區的10086);\nWIFI切換到數據需等待域名解析緩存刷新(約10min);\n服務器暫時無法響應請求;\n此時段請求服務器次數過多。\n\n該中繼服務列表服務由mewx.org提供,與wenku8無關。另外,此列表數據僅供海外用戶使用,並可能滯後網站24小時以上。請勿在中國大陸使用,否則後果自負,望悉知。</string>
<string name="about_qqq">QQ交流群: 427577610</string>
<string name="about_qqq">QQ交流群: 181789084</string>
<string name="dialog_content_ask_for_storage_permission">由於曆史原因,app需要冩入存儲目錄下的wenku8文件夾,是否授予冩入權限?</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<string name="task_stop">停止加载</string>
<string name="task_retry">点击重新加载</string>
<string name="task_null">没有结果</string>
<string name="dialog_content_ask_for_storage_permission">由于历史原因,app需要写入存储目录下的wenku8文件夹,是否授予写入权限?</string>
<string name="dialog_title_choose_download_option">选择执行的下载操作:</string>
<string name="dialog_title_choose_delete_option">选择执行的删除操作:</string>
<string name="dialog_title_save_file_name">输入保存的文件名(不要加.jpg)</string>
Expand Down Expand Up @@ -240,5 +241,5 @@
<string name="system_post_locked">帖子太久远,无法回复</string>
<string name="relay_warning">您正在使用MewX提供的第三方中继服务,点击查看详情。</string>
<string name="relay_warning_full">已启用第三方Wenku8中继服务,由于以下的可能原因:\n登录过期,请重新登录;\n手机网络异常,试试浏览器访问;\n运营商劫持(部分地区的10086);\nWIFI切换到数据需等待域名解析缓存刷新(约10min);\n服务器暂时无法响应请求;\n此时段请求服务器次数过多。\n\n该中继服务列表服务由mewx.org提供,与wenku8无关。另外,此列表数据仅供海外用户使用,并可能滞后网站24小时以上。请勿在中国大陆使用,否则后果自负,望悉知。</string>
<string name="about_qqq">QQ交流群: 427577610</string>
<string name="about_qqq">QQ交流群: 181789084</string>
</resources>

0 comments on commit 233b3be

Please sign in to comment.