parent
01e74fcc55
commit
7d6b1d47ac
@ -0,0 +1,52 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
import android.graphics.Bitmap; |
||||
import android.graphics.BitmapFactory; |
||||
import android.util.Log; |
||||
|
||||
import androidx.lifecycle.MutableLiveData; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import okhttp3.Call; |
||||
import okhttp3.Callback; |
||||
import okhttp3.Response; |
||||
import okhttp3.ResponseBody; |
||||
|
||||
public class HttpFileResponse implements Callback { |
||||
|
||||
private static final String header = "Content-Type"; |
||||
|
||||
private final MutableLiveData<Bitmap> image; |
||||
|
||||
public HttpFileResponse(MutableLiveData<Bitmap> image) { |
||||
this.image = image; |
||||
} |
||||
|
||||
@Override |
||||
public void onFailure(@NotNull Call call, @NotNull IOException e) { |
||||
e.printStackTrace(); |
||||
Log.e(HttpJSONResponse.class.getName(), e.toString()); |
||||
} |
||||
|
||||
@Override |
||||
public void onResponse(@NotNull Call call, @NotNull Response response) { |
||||
String contentType = response.header(header); |
||||
if ("image/png".equals(contentType)) { |
||||
ResponseBody responseBody = response.body(); |
||||
if (responseBody != null) { |
||||
Bitmap bitmap = BitmapFactory.decodeStream(responseBody.byteStream()); |
||||
image.postValue(bitmap); |
||||
} |
||||
} else { |
||||
onParseError(call, response, "接口不是响应图片数据,非法响应头" + header + "=" + contentType); |
||||
} |
||||
} |
||||
|
||||
private void onParseError(@NotNull Call call, @NotNull Response response, String err) { |
||||
Log.e(HttpJSONResponse.class.getName(), err); |
||||
throw new RuntimeException(err); |
||||
} |
||||
} |
@ -0,0 +1,139 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
import android.app.Activity; |
||||
import android.content.Intent; |
||||
import android.content.pm.PackageManager; |
||||
import android.net.Uri; |
||||
import android.os.Build; |
||||
import android.provider.Settings; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.appcompat.app.AlertDialog; |
||||
import androidx.core.app.ActivityCompat; |
||||
import androidx.core.content.ContextCompat; |
||||
|
||||
import com.community.pocket.R; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class PermissionsUtils<T> { |
||||
|
||||
private final int mRequestCode = 100;//权限请求码
|
||||
|
||||
private PermissionsUtils() { |
||||
} |
||||
|
||||
private static PermissionsUtils permissionsUtils; |
||||
private IPermissionsResult mPermissionsResult; |
||||
|
||||
public static PermissionsUtils getInstance() { |
||||
if (permissionsUtils == null) { |
||||
permissionsUtils = new PermissionsUtils(); |
||||
} |
||||
return permissionsUtils; |
||||
} |
||||
|
||||
public void checkPermissions(Activity context, String[] permissions, @NonNull IPermissionsResult permissionsResult) { |
||||
mPermissionsResult = permissionsResult; |
||||
|
||||
if (Build.VERSION.SDK_INT < 23) {//6.0才用动态权限
|
||||
permissionsResult.passPermissons(); |
||||
return; |
||||
} |
||||
|
||||
//创建一个mPermissionList,逐个判断哪些权限未授予,未授予的权限存储到mPerrrmissionList中
|
||||
List<String> mPermissionList = new ArrayList<>(); |
||||
//逐个判断你要的权限是否已经通过
|
||||
for (String permission : permissions) { |
||||
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { |
||||
mPermissionList.add(permission);//添加还未授予的权限
|
||||
} |
||||
} |
||||
|
||||
//申请权限
|
||||
if (mPermissionList.size() > 0) {//有权限没有通过,需要申请
|
||||
ActivityCompat.requestPermissions(context, permissions, mRequestCode); |
||||
} else { |
||||
//说明权限都已经通过,可以做你想做的事情去
|
||||
permissionsResult.passPermissons(); |
||||
} |
||||
} |
||||
|
||||
//请求权限后回调的方法
|
||||
//参数: requestCode 是我们自己定义的权限请求码
|
||||
//参数: permissions 是我们请求的权限名称数组
|
||||
//参数: grantResults 是我们在弹出页面后是否允许权限的标识数组,数组的长度对应的是权限名称数组的长度,数组的数据0表示允许权限,-1表示我们点击了禁止权限
|
||||
|
||||
public void onRequestPermissionsResult(Activity context, int requestCode, @NonNull String[] permissions, |
||||
@NonNull int[] grantResults) { |
||||
boolean hasPermissionDismiss = false;//有权限没有通过
|
||||
if (mRequestCode == requestCode) { |
||||
for (int grantResult : grantResults) { |
||||
if (grantResult == -1) { |
||||
hasPermissionDismiss = true; |
||||
break; |
||||
} |
||||
} |
||||
//如果有权限没有被允许
|
||||
if (hasPermissionDismiss) { |
||||
if (context.getResources().getBoolean(R.bool.showSystemSetting)) { |
||||
showSystemPermissionsSettingDialog(context);//跳转到系统设置权限页面,或者直接关闭页面,不让他继续访问
|
||||
} else { |
||||
mPermissionsResult.forbitPermissons(); |
||||
} |
||||
} else { |
||||
//全部权限通过,可以进行下一步操作。。。
|
||||
mPermissionsResult.passPermissons(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 不再提示权限时的展示对话框 |
||||
*/ |
||||
private AlertDialog mPermissionDialog; |
||||
|
||||
private void showSystemPermissionsSettingDialog(final Activity context) { |
||||
final String mPackName = context.getPackageName(); |
||||
if (mPermissionDialog == null) { |
||||
mPermissionDialog = new AlertDialog.Builder(context) |
||||
.setMessage("已禁用权限,请手动授予") |
||||
.setPositiveButton("设置", (dialog, which) -> { |
||||
cancelPermissionDialog(); |
||||
|
||||
Uri packageURI = Uri.parse("package:" + mPackName); |
||||
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI); |
||||
context.startActivity(intent); |
||||
context.finish(); |
||||
}) |
||||
.setNegativeButton("取消", (dialog, which) -> { |
||||
//关闭页面或者做其他操作
|
||||
cancelPermissionDialog(); |
||||
//mContext.finish();
|
||||
mPermissionsResult.forbitPermissons(); |
||||
}) |
||||
.create(); |
||||
} |
||||
mPermissionDialog.show(); |
||||
} |
||||
|
||||
//关闭对话框
|
||||
private void cancelPermissionDialog() { |
||||
if (mPermissionDialog != null) { |
||||
mPermissionDialog.cancel(); |
||||
mPermissionDialog = null; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
public interface IPermissionsResult { |
||||
void passPermissons(); |
||||
|
||||
void forbitPermissons(); |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,4 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<resources> |
||||
<bool name="showSystemSetting">true</bool> |
||||
</resources> |
Loading…
Reference in new issue