Skip to content

Commit

Permalink
feat(agentweb): Compatible with Android 14
Browse files Browse the repository at this point in the history
  • Loading branch information
Justson committed Dec 9, 2023
1 parent 62b346b commit 04b716c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,47 @@
* @since 1.0.0
*/
public class AgentWebPermissions {
public static final String[] CAMERA;
public static final String[] LOCATION;
public static final String[] STORAGE;
public static String[] CAMERA;
public static String[] LOCATION;
public static String[] MEDIA;
public static final String ACTION_CAMERA = "Camera";
public static final String ACTION_LOCATION = "Location";
public static final String ACTION_STORAGE = "Storage";
public static final String ACTION_MEDIA = "Media";

static {
CAMERA = new String[]{
Manifest.permission.CAMERA};

LOCATION = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION};

if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
STORAGE = new String[]{
Manifest.permission.READ_MEDIA_IMAGES,
MEDIA = new String[]{
Manifest.permission.READ_MEDIA_VIDEO,
Manifest.permission.READ_MEDIA_AUDIO,
Manifest.permission.READ_MEDIA_IMAGES,
};
} else {
STORAGE = new String[]{
MEDIA = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
}
}

private static void emptyMediaPermission() {
MEDIA = new String[]{};
}

private static void emptyCameraPermission() {
CAMERA = new String[]{};
}

public static void dontAskUnnecessaryPermissions() {
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
emptyMediaPermission();
emptyCameraPermission();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -767,7 +768,7 @@ public static boolean hasPermission(@NonNull Context context, @NonNull List<Stri

public static List<String> getDeniedPermissions(Activity activity, String[] permissions) {
if (permissions == null || permissions.length == 0) {
return null;
return Collections.EMPTY_LIST;
}
List<String> deniedPermissions = new ArrayList<>(permissions.length);
for (int i = 0; i < permissions.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
Expand All @@ -32,9 +30,6 @@
import com.download.library.ResourceRequest;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -108,7 +103,7 @@ protected void onDownloadStartInternal(String url, String userAgent, String cont
return;
}
if (null != this.mPermissionListener) {
if (this.mPermissionListener.intercept(url, AgentWebPermissions.STORAGE, "download")) {
if (this.mPermissionListener.intercept(url, new String[]{}, "download")) {
return;
}
}
Expand Down
9 changes: 8 additions & 1 deletion agentweb-filechooser/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- To handle the reselection within the app on Android 14 (API level 34) -->
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED" />
<!-- Devices running Android 13 (API level 33) or higher -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ public void run() {
private void fileChooser() {

List<String> permission = null;
if (AgentWebUtils.getDeniedPermissions(mActivity, AgentWebPermissions.STORAGE).isEmpty()) {
touchOffFileChooserAction();
if (AgentWebUtils.getDeniedPermissions(mActivity, AgentWebPermissions.MEDIA).isEmpty()) {
chooserAction();
} else {
Action mAction = Action.createPermissionsAction(AgentWebPermissions.STORAGE);
Action mAction = Action.createPermissionsAction(AgentWebPermissions.MEDIA);
mAction.setFromIntention(FROM_INTENTION_CODE >> 2);
mAction.setPermissionListener(mPermissionListener);
AgentActionFragment.start(mActivity, mAction);
Expand All @@ -188,7 +188,7 @@ private void fileChooser() {

}

private void touchOffFileChooserAction() {
private void chooserAction() {
Action mAction = new Action();
mAction.setAction(Action.ACTION_FILE);
mAction.setChooserListener(getChooserListener());
Expand Down Expand Up @@ -270,12 +270,12 @@ private void openFileChooserInternal() {
}
}
if (!needCamera && !needVideo) {
touchOffFileChooserAction();
chooserAction();
return;
}
}
if (!TextUtils.isEmpty(this.mAcceptType) && !this.mAcceptType.contains("*/") && !this.mAcceptType.contains("image/")) {
touchOffFileChooserAction();
chooserAction();
return;
}

Expand Down Expand Up @@ -314,7 +314,6 @@ public boolean handleMessage(Message msg) {


private void onCameraAction() {

if (mActivity == null) {
return;
}
Expand All @@ -324,9 +323,7 @@ private void onCameraAction() {
cancel();
return;
}

}

Action mAction = new Action();
List<String> deniedPermissions = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !(deniedPermissions = checkNeedPermission()).isEmpty()) {
Expand All @@ -348,8 +345,8 @@ private List<String> checkNeedPermission() {
if (!AgentWebUtils.hasPermission(mActivity, AgentWebPermissions.CAMERA)) {
deniedPermissions.add(AgentWebPermissions.CAMERA[0]);
}
if (!AgentWebUtils.hasPermission(mActivity, AgentWebPermissions.STORAGE)) {
deniedPermissions.addAll(Arrays.asList(AgentWebPermissions.STORAGE));
if (!AgentWebUtils.hasPermission(mActivity, AgentWebPermissions.MEDIA)) {
deniedPermissions.addAll(Arrays.asList(AgentWebPermissions.MEDIA));
}
return deniedPermissions;
}
Expand Down Expand Up @@ -380,16 +377,16 @@ public void onRequestPermissionsResult(@NonNull String[] permissions, @NonNull i
private void permissionResult(boolean grant, int fromIntention) {
if (fromIntention == FROM_INTENTION_CODE >> 2) {
if (grant) {
touchOffFileChooserAction();
chooserAction();
} else {
cancel();

if (null != mAgentWebUIController.get()) {
mAgentWebUIController
.get()
.onPermissionsDeny(
AgentWebPermissions.STORAGE,
AgentWebPermissions.ACTION_STORAGE,
AgentWebPermissions.MEDIA,
AgentWebPermissions.ACTION_MEDIA,
"Open file chooser");
}
}
Expand Down
14 changes: 10 additions & 4 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.just.agentweb.sample">

<uses-feature
android:name="android.hardware.camera"
android:required="false" />


<!-- Devices running Android 12L (API level 32) or lower -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,12 @@ private void openFragment(int key) {

/*Fragment 使用AgenWeb*/
case FLAG_GUIDE_DICTIONARY_USE_IN_FRAGMENT: //项目中请使用常量代替0 , 代码可读性更高
ft.add(R.id.container_framelayout, mAgentWebFragment = AgentWebFragment.getInstance(mBundle = new Bundle()), AgentWebFragment.class.getName());
mBundle.putString(AgentWebFragment.URL_KEY, "http://cw.gzyunjuchuang.com/");
break;
/*下载文件*/
/*下载文件*/
case FLAG_GUIDE_DICTIONARY_FILE_DOWNLOAD:
ft.add(R.id.container_framelayout, mAgentWebFragment = AgentWebFragment.getInstance(mBundle = new Bundle()), AgentWebFragment.class.getName());
mBundle.putString(AgentWebFragment.URL_KEY, "http://android.myapp.com/");
break;
/*input标签上传文件*/
/*input标签上传文件*/
case FLAG_GUIDE_DICTIONARY_INPUT_TAG_PROBLEM:
ft.add(R.id.container_framelayout, mAgentWebFragment = AgentWebFragment.getInstance(mBundle = new Bundle()), AgentWebFragment.class.getName());
mBundle.putString(AgentWebFragment.URL_KEY, "file:///android_asset/upload_file/uploadfile.html");
Expand Down Expand Up @@ -118,7 +115,7 @@ private void openFragment(int key) {
/*豌豆荚*/
case FLAG_GUIDE_DICTIONARY_CUSTOM_WEBVIEW_SETTINGS:
ft.add(R.id.container_framelayout, mAgentWebFragment = CustomSettingsFragment.getInstance(mBundle = new Bundle()), CustomSettingsFragment.class.getName());
mBundle.putString(AgentWebFragment.URL_KEY, "http://www.wandoujia.com/apps");
mBundle.putString(AgentWebFragment.URL_KEY, "https://m.wandoujia.com/");
break;

/*短信*/
Expand Down
1 change: 0 additions & 1 deletion sample/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" overridePins="true" /> <!--信任系统证书-->
<certificates src="user" overridePins="true" /> <!--信任用户证书-->
</trust-anchors>
</base-config>
</network-security-config>

0 comments on commit 04b716c

Please sign in to comment.