Skip to content

Commit

Permalink
【头像信息修改完善(相机、选图、剪裁模块完成)】
Browse files Browse the repository at this point in the history
core/util 下新建一个 callback包,下新建CallbackManager、IGlobalCallback、CallbackType;
更新PermissionCheckerDelegate、CameraHandler(注释)、AndroidManifest、UserProfileClickListener;
  • Loading branch information
aaLiweipeng committed Mar 4, 2020
1 parent 5ed0d2d commit 0dc55e7
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 6 deletions.
Binary file modified xiaoyun_annotations/build/libs/xiaoyun_annotations.jar
Binary file not shown.
Binary file modified xiaoyun_compiler/build/libs/xiaoyun_compiler.jar
Binary file not shown.
11 changes: 10 additions & 1 deletion xiaoyun_core/src/main/AndroidManifest.xml
Expand Up @@ -27,6 +27,15 @@

<application
android:allowBackup="true"
android:supportsRtl="true"/>
android:supportsRtl="true">

<!--图片剪裁Activity-->
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
</application>



</manifest>
Expand Up @@ -2,11 +2,19 @@

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.lwp.xiaoyun_core.ui.camera.CameraImageBean;
import com.lwp.xiaoyun_core.ui.camera.RequestCodes;
import com.lwp.xiaoyun_core.ui.camera.XiaoYunCamera;
import com.lwp.xiaoyun_core.util.callback.CallbackManager;
import com.lwp.xiaoyun_core.util.callback.CallbackType;
import com.lwp.xiaoyun_core.util.callback.IGlobalCallback;
import com.yalantis.ucrop.UCrop;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
Expand Down Expand Up @@ -41,7 +49,9 @@ void startCamera() {
XiaoYunCamera.start(this);
}

//开始请求权限
//开始请求权限,
//在进行 需要权限的操作时!!! 调用;
// 如项目中 点击个人具体信息页的头像时!
public void startCameraWithCheck() {
//开始请求权限(相机、读写)
PermissionCheckerDelegatePermissionsDispatcher.startCameraWithPermissionCheck(this);
Expand Down Expand Up @@ -93,4 +103,61 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
//PermissionCheckerDelegatePermissionsDispatcher 是注解自动生成的!!
PermissionCheckerDelegatePermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}

// 本方法是对应 startActivityForResult()的回调
// 前两个case 与 CameraHandler中的 DELEGATE.startActivityForResult() 相对应
// .
// 后两个case 与 前两个case中的 UCrop.start()相对应,
// 实际上 UCrop.start()源码中,封装了startActivityForResult()
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {

switch (requestCode) {
case RequestCodes.TAKE_PHOTO:
//获取到 存储照片的 临时文件路径
final Uri resultUri = CameraImageBean.getInstance().getPath();
UCrop.of(resultUri, resultUri)//一参为 欲剪裁图片的路径,二参为 放置剪切完图片的路径
.withMaxResultSize(400, 400)
.start(getContext(), this);//start()用意看源码
break;

case RequestCodes.PICK_PHOTO:

if (data != null) {
final Uri pickPath = data.getData();//拿到用户选择的图片的路径

//从相册选择后 需要有个路径 来存放 剪裁过的图片
final String pickCropPath = XiaoYunCamera.createCropFile().getPath();

UCrop.of(pickPath, Uri.parse(pickCropPath))
.withMaxResultSize(400, 400)
.start(getContext(), this);
}
break;

case RequestCodes.CROP_PHOTO:

final Uri cropUri = UCrop.getOutput(data);

//拿到剪裁后的数据进行处理
@SuppressWarnings("unchecked")
final IGlobalCallback<Uri> callback = CallbackManager
.getInstance()
.getCallback(CallbackType.ON_CROP);//拿到回调接口
if (callback != null) {
callback.executeCallback(cropUri);//执行回调接口方法
}
break;

case RequestCodes.CROP_ERROR:
Toast.makeText(getContext(), "剪裁出错", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
}
Expand Up @@ -30,6 +30,9 @@
public class CameraHandler implements View.OnClickListener {

private final AlertDialog DIALOG;
//这个Delegate乃是 个人具体信息页 UserProfileDelegate
// (哪个继承自XiaoYunDelegate的 delegate 调用了 PermissionCheckerDelegate.startCameraWithCheck(),
// 这个Delegate就是 哪个,UserProfileDelegate 中的点击事件中,调用了 startCameraWithCheck())
private final PermissionCheckerDelegate DELEGATE;

public CameraHandler(PermissionCheckerDelegate delegate) {
Expand Down Expand Up @@ -69,16 +72,20 @@ private String getPhotoName() {
//拍照取图
public void takePhoto() {
final String currentPhotoName = getPhotoName();

//拍照意图
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//File 临时文件句柄 临时文件:这里是系统相册目录下的当前文件名的文件临时句柄
//CAMERA_PHOTO_DIR 系统相册目录
//CAMERA_PHOTO_DIR 系统相册目录!!!
final File tempFile = new File(FileUtil.CAMERA_PHOTO_DIR, currentPhotoName);

//兼容7.0及以上的写法
//下面是创建临时文件,存储拍的照片
// 兼容7.0及以上的写法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

final ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, tempFile.getPath());

//使用 ContentProvider 的方式
final Uri uri = DELEGATE.getContext().getContentResolver().
insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
Expand All @@ -87,8 +94,10 @@ public void takePhoto() {
final File realFile =
FileUtils.getFileByPath(FileUtil.getRealFilePath(DELEGATE.getContext(), uri));

//把临时照片文件路径存起来,方便后面使用(如PermissionCheckerDelegate.onActivityResult())
final Uri realUri = Uri.fromFile(realFile);
CameraImageBean.getInstance().setPath(realUri);
//把临时文件Uri路径设置给照片的输出端(EXTRA_OUTPUT)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
} else {

Expand Down
@@ -0,0 +1,33 @@
package com.lwp.xiaoyun_core.util.callback;

import java.util.WeakHashMap;

/**
* <pre>
* author : 李蔚蓬(简书_凌川江雪)
* time : 2020/3/5 6:22
* desc : 【回调机制】
* 集成本工具包,可以在任何位置设置回调!!!
* </pre>
*/
public class CallbackManager {

//用来存储所有IGlobalCallback
private static final WeakHashMap<Object, IGlobalCallback> CALLBACKS = new WeakHashMap<>();

//静态内部类型单例模式
private static class Holder {
private static final CallbackManager INSTANCE = new CallbackManager();
}
public static CallbackManager getInstance() {
return Holder.INSTANCE;
}

public CallbackManager addCallback(Object tag, IGlobalCallback callback) {
CALLBACKS.put(tag, callback);
return this;
}
public IGlobalCallback getCallback(Object tag) {
return CALLBACKS.get(tag);
}
}
@@ -0,0 +1,16 @@
package com.lwp.xiaoyun_core.util.callback;

/**
* <pre>
* author : 李蔚蓬(简书_凌川江雪)
* time : 2020/3/5 6:23
* desc : 为callback本包 提供枚举
* </pre>
*/
public enum CallbackType {

ON_CROP,
TAG_OPEN_PUSH,
TAG_STOP_PUSH,
ON_SCAN,
}
@@ -0,0 +1,16 @@
package com.lwp.xiaoyun_core.util.callback;

/**
* <pre>
* author : 李蔚蓬(简书_凌川江雪)
* time : 2020/3/5 6:22
* desc : 全局回调
* </pre>
*/
public interface IGlobalCallback<T> {

//泛型接口,一般回调的时候需要给回调方法传递一些值,
// 关于值的类型,这里 指为泛型 比 传入Object好很多,
// 性能、可拓展性、健壮性提高很多
void executeCallback(T args);
}
Expand Up @@ -2,15 +2,26 @@

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.SimpleClickListener;
import com.lwp.xiaoyun.ec.R;
import com.lwp.xiaoyun.ec.main.personal.list.ListBean;
import com.lwp.xiaoyun.ui.date.DateDialogUtil;
import com.lwp.xiaoyun_core.delegates.XiaoYunDelegate;
import com.lwp.xiaoyun_core.net.RestClient;
import com.lwp.xiaoyun_core.net.callback.ISuccess;
import com.lwp.xiaoyun_core.util.callback.CallbackManager;
import com.lwp.xiaoyun_core.util.callback.CallbackType;
import com.lwp.xiaoyun_core.util.callback.IGlobalCallback;
import com.lwp.xiaoyun_core.util.log.XiaoYunLogger;

import retrofit2.http.DELETE;

Expand Down Expand Up @@ -41,8 +52,51 @@ public void onItemClick(BaseQuickAdapter adapter, final View view, int position)
final int id = bean.getId();//id 来自 UserProfileDelegate!!!
switch (id) {
case 1:
//个人具体信息页面头像点击,启动照相机或选择图片,
// 开始请求权限,
//个人具体信息页面头像点击,启动照相机或选择图片
CallbackManager.getInstance()
.addCallback(CallbackType.ON_CROP, new IGlobalCallback<Uri>() {
@Override
public void executeCallback(Uri args) {
XiaoYunLogger.d("ON_CROP", args);
Toast.makeText(DELEGATE.getContext(), args+"", Toast.LENGTH_SHORT).show();
final ImageView avatar = (ImageView) view.findViewById(R.id.img_arrow_avatar);
Glide.with(DELEGATE)
.load(args)
.into(avatar);

// RestClient.builder()
// .url(UploadConfig.UPLOAD_IMG)
// .loader(DELEGATE.getContext())
// .file(args.getPath())
// .success(new ISuccess() {
// @Override
// public void onSuccess(String response) {
// LatteLogger.d("ON_CROP_UPLOAD", response);
// final String path = JSON.parseObject(response).getJSONObject("result")
// .getString("path");
//
// //通知服务器更新信息
// RestClient.builder()
// .url("user_profile.php")
// .params("avatar", path)
// .loader(DELEGATE.getContext())
// .success(new ISuccess() {
// @Override
// public void onSuccess(String response) {
// //获取更新后的用户信息,然后更新本地数据库
// //没有本地数据的APP,每次打开APP都请求API,获取信息
// }
// })
// .build()
// .post();
// }
// })
// .build()
// .upload();
}
});

// 开始请求权限!!!
// 随后进行权限结果处理——
// 成功则开始选图或拍照,失败则。。详见 PermissionCheckerDelegate
DELEGATE.startCameraWithCheck();
Expand Down

0 comments on commit 0dc55e7

Please sign in to comment.