Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

targetSdkVersion 为安卓14时,优化安卓14上媒体重选逻辑(根据谷歌官方推荐写法) #2837

Open
wants to merge 3 commits into
base: version_component
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions app/src/main/res/layout/ps_custom_fragment_selector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
app:layout_constraintBottom_toTopOf="@+id/bottom_nar_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_bar" />
app:layout_constraintTop_toBottomOf="@+id/media_reselection_tip_view" />

<TextView
android:id="@+id/tv_current_data_time"
Expand All @@ -31,7 +31,7 @@
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_bar" />
app:layout_constraintTop_toBottomOf="@+id/media_reselection_tip_view" />

<com.luck.pictureselector.CustomTitleBar
android:id="@+id/title_bar"
Expand All @@ -42,6 +42,16 @@
app:layout_constraintTop_toTopOf="parent"
tools:layout_height="48dp" />

<com.luck.picture.lib.widget.MediaReselectionTipView
android:id="@+id/media_reselection_tip_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_bar"
tools:layout_height="48dp" />

<com.luck.pictureselector.CustomBottomNavBar
android:id="@+id/bottom_nar_bar"
android:layout_width="0dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.luck.picture.lib.utils.ValueOf;
import com.luck.picture.lib.widget.BottomNavBar;
import com.luck.picture.lib.widget.CompleteSelectView;
import com.luck.picture.lib.widget.MediaReselectionTipView;
import com.luck.picture.lib.widget.RecyclerPreloadView;
import com.luck.picture.lib.widget.SlideSelectTouchListener;
import com.luck.picture.lib.widget.SlideSelectionHandler;
Expand Down Expand Up @@ -92,6 +93,7 @@ public class PictureSelectorFragment extends PictureCommonFragment
private RecyclerPreloadView mRecycler;
private TextView tvDataEmpty;
private TitleBar titleBar;
private MediaReselectionTipView mediaReselectionTipView;
private BottomNavBar bottomNarBar;
private CompleteSelectView completeSelectView;
private TextView tvCurrentDataTime;
Expand Down Expand Up @@ -240,6 +242,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
tvDataEmpty = view.findViewById(R.id.tv_data_empty);
completeSelectView = view.findViewById(R.id.ps_complete_select);
titleBar = view.findViewById(R.id.title_bar);
mediaReselectionTipView = view.findViewById(R.id.media_reselection_tip_view);
bottomNarBar = view.findViewById(R.id.bottom_nar_bar);
tvCurrentDataTime = view.findViewById(R.id.tv_current_data_time);
onCreateLoader();
Expand Down Expand Up @@ -388,7 +391,7 @@ public void onDismissPopupWindow() {
addAlbumPopWindowAction();
}

private void recoverSaveInstanceData(){
private void recoverSaveInstanceData() {
mAdapter.setDisplayCamera(isDisplayCamera);
setEnterAnimationDuration(0);
if (selectorConfig.isOnlySandboxDir) {
Expand Down Expand Up @@ -428,24 +431,32 @@ private void requestLoadData() {
mAdapter.setDisplayCamera(isDisplayCamera);
if (PermissionChecker.isCheckReadStorage(selectorConfig.chooseMode, getContext())) {
beginLoadData();
} else if (PermissionChecker.isCheckUserSelected(selectorConfig.chooseMode, getContext())) {
//媒体权限未授予,但是授予了 READ_MEDIA_VISUAL_USER_SELECTED 权限
//正常加载数据
beginLoadData();
} else {
String[] readPermissionArray = PermissionConfig.getReadPermissionArray(getAppContext(), selectorConfig.chooseMode);
onPermissionExplainEvent(true, readPermissionArray);
if (selectorConfig.onPermissionsEventListener != null) {
onApplyPermissionsEvent(PermissionEvent.EVENT_SOURCE_DATA, readPermissionArray);
} else {
PermissionChecker.getInstance().requestPermissions(this, readPermissionArray, new PermissionResultCallback() {
@Override
public void onGranted() {
beginLoadData();
}
requestPermissionsAndLoadData();
}
}

@Override
public void onDenied() {
handlePermissionDenied(readPermissionArray);
}
});
}
private void requestPermissionsAndLoadData() {
String[] readPermissionArray = PermissionConfig.getReadPermissionArray(getAppContext(), selectorConfig.chooseMode);
onPermissionExplainEvent(true, readPermissionArray);
if (selectorConfig.onPermissionsEventListener != null) {
onApplyPermissionsEvent(PermissionEvent.EVENT_SOURCE_DATA, readPermissionArray);
} else {
PermissionChecker.getInstance().requestPermissions(this, readPermissionArray, new PermissionResultCallback() {
@Override
public void onGranted() {
beginLoadData();
}

@Override
public void onDenied() {
handlePermissionDenied(readPermissionArray);
}
});
}
}

Expand All @@ -471,6 +482,8 @@ public void onCall(String[] permissionArray, boolean isResult) {
* 开始获取数据
*/
private void beginLoadData() {
//加载数据同时,初始化媒体重选 View
initMediaReselectionTipView();
onPermissionExplainEvent(false, null);
if (selectorConfig.isOnlySandboxDir) {
loadOnlyInAppDirectoryAllMediaData();
Expand All @@ -479,9 +492,29 @@ private void beginLoadData() {
}
}

private void initMediaReselectionTipView() {
//未授予读写权限 且 授予 READ_MEDIA_VISUAL_USER_SELECTED 权限,需要显示媒体重选提示
boolean shouldShowMediaReselectionTip = !PermissionChecker.isCheckReadStorage(selectorConfig.chooseMode, getContext())
&& PermissionChecker.isCheckUserSelected(selectorConfig.chooseMode, getContext());
if (shouldShowMediaReselectionTip) {
mediaReselectionTipView.setVisibility(View.VISIBLE);
mediaReselectionTipView.setMediaReselectionTipViewStyle();
mediaReselectionTipView.setOnMediaReselectionListener(
new MediaReselectionTipView.OnMediaReselectionListener() {
@Override
public void onManageClick() {
requestPermissionsAndLoadData();
}
}
);
} else {
mediaReselectionTipView.setVisibility(View.GONE);
}
}

@Override
public void handlePermissionSettingResult(String[] permissions) {
if (permissions == null){
if (permissions == null) {
return;
}
onPermissionExplainEvent(false, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void requestPermissions(Fragment fragment, List<String[]> permissionGrou
}
}

public void onRequestPermissionsResult(Context context,String[] permissions,int[] grantResults, PermissionResultCallback action) {
public void onRequestPermissionsResult(Context context, String[] permissions, int[] grantResults, PermissionResultCallback action) {
Activity activity = (Activity) context;
for (String permission : permissions) {
boolean should = ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
Expand Down Expand Up @@ -143,6 +143,19 @@ public static boolean isCheckReadStorage(int chooseMode, Context context) {
}
}

/**
* 检查 READ_MEDIA_VISUAL_USER_SELECTED 权限是否存在
*/
public static boolean isCheckUserSelected(int chooseMode, Context context) {
if (SdkVersionUtils.isUPSIDE_DOWN_CAKE()) {
if (chooseMode != SelectMimeType.ofAudio()) {
return PermissionChecker.checkSelfPermission(context,
new String[]{PermissionConfig.READ_MEDIA_VISUAL_USER_SELECTED});
} else return false;
} else {
return false;
}
}

/**
* 检查读取图片权限是否存在
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.luck.picture.lib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.luck.picture.lib.R;
import com.luck.picture.lib.config.SelectorConfig;
import com.luck.picture.lib.config.SelectorProviders;
import com.luck.picture.lib.style.BottomNavBarStyle;
import com.luck.picture.lib.style.PictureSelectorStyle;
import com.luck.picture.lib.utils.StyleUtils;

/**
* Created on 2024/1/17.
*
* @author [email protected]
* @description 安卓34+,READ_MEDIA_VISUAL_USER_SELECTED 授权后,提示用户重选媒体
*/
public class MediaReselectionTipView extends FrameLayout implements View.OnClickListener {

protected TextView tvTip;
protected TextView tvManage;
protected SelectorConfig config;
protected ConstraintLayout mediaReselectionTipLayout;

public TextView getTitleManageView() {
return tvManage;
}

public MediaReselectionTipView(Context context) {
super(context);
init();
}

public MediaReselectionTipView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MediaReselectionTipView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

protected void init() {
inflateLayout();
setClickable(true);
setFocusable(true);
config = SelectorProviders.getInstance().getSelectorConfig();
mediaReselectionTipLayout = findViewById(R.id.cl_media_reselection_tip);
tvTip = findViewById(R.id.ps_tv_title);
tvManage = findViewById(R.id.ps_tv_manage);
tvManage.setOnClickListener(this);
mediaReselectionTipLayout.setOnClickListener(this);
setBackgroundColor(ContextCompat.getColor(getContext(), R.color.ps_color_grey));
handleLayoutUI();
}

protected void inflateLayout() {
LayoutInflater.from(getContext()).inflate(R.layout.ps_media_reselection_tip_view, this);
}

protected void handleLayoutUI() {

}

public void setMediaReselectionTipViewStyle() {
PictureSelectorStyle selectorStyle = config.selectorStyle;
BottomNavBarStyle bottomBarStyle = selectorStyle.getBottomBarStyle();

int backgroundColor = bottomBarStyle.getBottomNarBarBackgroundColor();
if (StyleUtils.checkStyleValidity(backgroundColor)) {
setBackgroundColor(backgroundColor);
}

int previewNormalTextColor = bottomBarStyle.getBottomPreviewNormalTextColor();
if (StyleUtils.checkSizeValidity(previewNormalTextColor)) {
tvTip.setTextColor(previewNormalTextColor);
}

int previewSelectTextColor = bottomBarStyle.getBottomPreviewSelectTextColor();
if (StyleUtils.checkStyleValidity(previewSelectTextColor)) {
tvManage.setTextColor(previewSelectTextColor);
}
int previewNormalTextSize = bottomBarStyle.getBottomPreviewNormalTextSize();
if (StyleUtils.checkSizeValidity(previewNormalTextSize)) {
tvManage.setTextSize(previewNormalTextSize);
}
}

@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.ps_tv_manage) {
if (mediaReselectionListener != null) {
mediaReselectionListener.onManageClick();
}
}
}

protected OnMediaReselectionListener mediaReselectionListener;

/**
* 功能事件回调
*
* @param listener
*/
public void setOnMediaReselectionListener(OnMediaReselectionListener listener) {
this.mediaReselectionListener = listener;
}

public static class OnMediaReselectionListener {
public void onManageClick() {

}
}
}
16 changes: 13 additions & 3 deletions selector/src/main/res/layout/ps_fragment_selector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
app:layout_constraintBottom_toTopOf="@+id/bottom_nar_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_bar" />
app:layout_constraintTop_toBottomOf="@+id/media_reselection_tip_view" />

<com.luck.picture.lib.widget.MediumBoldTextView
android:id="@+id/tv_current_data_time"
Expand All @@ -29,7 +29,7 @@
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_bar" />
app:layout_constraintTop_toBottomOf="@+id/media_reselection_tip_view" />

<com.luck.picture.lib.widget.TitleBar
android:id="@+id/title_bar"
Expand All @@ -40,6 +40,16 @@
app:layout_constraintTop_toTopOf="parent"
tools:layout_height="48dp" />

<com.luck.picture.lib.widget.MediaReselectionTipView
android:id="@+id/media_reselection_tip_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_bar"
tools:layout_height="48dp" />

<com.luck.picture.lib.widget.BottomNavBar
android:id="@+id/bottom_nar_bar"
android:layout_width="0dp"
Expand All @@ -54,9 +64,9 @@
android:id="@+id/ps_complete_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:layout_marginEnd="15dp"
android:background="@drawable/ps_transparent_space"
android:enabled="false"
app:layout_constraintBottom_toBottomOf="@id/bottom_nar_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/bottom_nar_bar" />
Expand Down
Loading