68 | * Before 在原方法执行之前执行要插入的代码
69 | * After 在原方法执行之后执行要插入的代码
70 | * AfterReturning 在原方法执行后,返回一个结果再执行,如果没结果,用此修辞符修辞是不会执行的
71 | * AfterThrowing 在原方法执行过程中抛出异常后执行,也就是方法执行过程中,如果抛出异常后,才会执行此切面方法。
72 | * Around 在原方法执行前后和抛出异常时执行(前面几种通知的综合)
73 | */
74 | @Around("(method() || constructor()) && @annotation(trace)") // 在连接点进行方法替换
75 | public Object aroundJoinPoint(final ProceedingJoinPoint joinPoint, JTrace trace) throws Throwable {
76 | // 实例化计时器
77 | StopWatch stopWatch = new StopWatch();
78 | // 开始计时
79 | stopWatch.start();
80 | // 执行原方法
81 | Object result = joinPoint.proceed();
82 | // 结束计时
83 | stopWatch.stop();
84 |
85 | // 获取方法信息对象
86 | MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
87 | // 获取当前对象
88 | String className = methodSignature.getDeclaringType().getSimpleName();
89 | String methodName = methodSignature.getName();
90 | // 打印日志
91 | DebugLog.d(className, buildLogMessage(methodName, stopWatch.getTotalTimeMillis()));
92 | return result;
93 | }
94 |
95 | /**
96 | * @return 消息
97 | * @description 拼接 log 消息
98 | * @date: 2019/8/8 2:29
99 | * @author: 陈白衣
100 | */
101 | private String buildLogMessage(String methodName, long totalTimeMillis) {
102 | StringBuilder message = new StringBuilder();
103 | message.append("JTrace --> ")
104 | .append(methodName)
105 | .append(" [")
106 | .append(totalTimeMillis)
107 | .append("ms]");
108 | return message.toString();
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
62 | * Before 在原方法执行之前执行要插入的代码
63 | * After 在原方法执行之后执行要插入的代码
64 | * AfterReturning 在原方法执行后,返回一个结果再执行,如果没结果,用此修辞符修辞是不会执行的
65 | * AfterThrowing 在原方法执行过程中抛出异常后执行,也就是方法执行过程中,如果抛出异常后,才会执行此切面方法。
66 | * Around 在原方法执行前后和抛出异常时执行(前面几种通知的综合)
67 | */
68 | @Around("(method() || constructor()) && @annotation(permission)") // 在连接点进行方法替换
69 | public void aroundJoinPoint(final ProceedingJoinPoint joinPoint, JPermission permission) {
70 | FragmentManager fragmentManager = null;
71 | // 根据当前上下文对象是,获取对应的 FragmentManager 对象
72 | if (joinPoint.getThis() instanceof Fragment) {
73 | // fragment
74 | Fragment fragment = (Fragment) joinPoint.getThis();
75 | fragmentManager = fragment.getChildFragmentManager();
76 | } else if (joinPoint.getThis() instanceof FragmentActivity) {
77 | // activity
78 | FragmentActivity activity = (FragmentActivity) joinPoint.getThis();
79 | fragmentManager = activity.getSupportFragmentManager();
80 | }
81 | // 请求权限
82 | PermissionUtils.permission(permission.value())
83 | .callback(new PermissionUtils.FullCallback() {
84 | @Override
85 | public void onGranted(List
129 | * 2.前者是/sdcard/Android/data/
130 | * 3.后者获取到的是 /data/data/
131 | *
132 | * @param uniqueName 缓存目录
133 | */
134 | public static File getDiskCacheDir(Context context, String uniqueName) {
135 | String cachePath;
136 | if (isSDCardEnable() && context.getExternalCacheDir() != null) {
137 | cachePath = context.getExternalCacheDir().getPath();
138 | } else {
139 | cachePath = context.getCacheDir().getPath();
140 | }
141 | return new File(cachePath + File.separator + uniqueName);
142 | }
143 |
144 | private static boolean isSDCardEnable() {
145 | return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
146 | || !Environment.isExternalStorageRemovable();
147 | }
148 |
149 | /**
150 | * 获取 App 版本码
151 | *
152 | * @param context
153 | * @return App 版本码
154 | */
155 | public static int getAppVersionCode(Context context) {
156 | if (context != null) {
157 | PackageManager pm = context.getPackageManager();
158 | if (pm != null) {
159 | PackageInfo pi;
160 | try {
161 | pi = pm.getPackageInfo(context.getPackageName(), 0);
162 | if (pi != null) {
163 | return pi.versionCode;
164 | }
165 | } catch (PackageManager.NameNotFoundException e) {
166 | e.printStackTrace();
167 | }
168 | }
169 | }
170 | return -1;
171 | }
172 |
173 | /**
174 | * 方法是否有返回值
175 | *
176 | * @param signature
177 | * @return
178 | */
179 | public static boolean isHasReturnType(Signature signature) {
180 | return signature instanceof MethodSignature
181 | && ((MethodSignature) signature).getReturnType() != void.class;
182 | }
183 |
184 | }
185 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
{@code false}: 否
91 | */
92 | public static boolean isGranted(final String... permissions) {
93 | for (String permission : permissions) {
94 | if (!isGranted(permission)) {
95 | return false;
96 | }
97 | }
98 | return true;
99 | }
100 |
101 | private static boolean isGranted(final String permission) {
102 | return Build.VERSION.SDK_INT < Build.VERSION_CODES.M
103 | || PackageManager.PERMISSION_GRANTED
104 | == ContextCompat.checkSelfPermission(Aop.getContext(), permission);
105 | }
106 |
107 |
108 | private PermissionUtils(final String... permissions) {
109 | mPermissions = new LinkedHashSet<>();
110 | // 遍历动态请求的权限集
111 | for (String permission : permissions) {
112 | // 遍历该权限是否属于需要动态申请的权限组
113 | for (String aPermission : PermissionConsts.getPermissions(permission)) {
114 | // 判断清单文件中的权限列表是否包含该权限,存在,则添加到集合中
115 | if (PERMISSIONS.contains(aPermission)) {
116 | mPermissions.add(aPermission);
117 | }
118 | }
119 | }
120 | sInstance = this;
121 | }
122 |
123 | /**
124 | * 设置请求权限
125 | *
126 | * @param permissions 要请求的权限
127 | * @return {@link PermissionUtils}
128 | */
129 | public static PermissionUtils permission(@PermissionConsts.Permissions() final String... permissions) {
130 | return new PermissionUtils(permissions);
131 | }
132 |
133 | /**
134 | * 设置拒绝权限后再次请求的回调接口
135 | *
136 | * @param listener 拒绝权限后再次请求的回调接口
137 | * @return {@link PermissionUtils}
138 | */
139 | PermissionUtils rationale(final OnRationaleListener listener) {
140 | mOnRationaleListener = listener;
141 | return this;
142 | }
143 |
144 | /**
145 | * 设置回调
146 | *
147 | * @param callback 简单回调接口
148 | * @return {@link PermissionUtils}
149 | */
150 | public PermissionUtils callback(final SimpleCallback callback) {
151 | mSimpleCallback = callback;
152 | return this;
153 | }
154 |
155 | /**
156 | * 设置回调
157 | *
158 | * @param callback 完整回调接口
159 | * @return {@link PermissionUtils}
160 | */
161 | public PermissionUtils callback(final FullCallback callback) {
162 | mFullCallback = callback;
163 | return this;
164 | }
165 |
166 | /**
167 | * 开始请求
168 | */
169 | public void request(FragmentManager fragmentManager) {
170 | if (fragmentManager == null) {
171 | throw new RuntimeException("The context must to be an activity or a fragment.");
172 | }
173 | mPermissionsGranted = new ArrayList<>();
174 | mPermissionsRequest = new ArrayList<>();
175 | // 判断 SDK 版本 【23(Android 6.0 系统)】 ==> 小于 23:不需要动态权限申请;大于 23:要动态权限申请
176 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
177 | mPermissionsGranted.addAll(mPermissions);
178 | requestCallback();
179 | } else {
180 | // 遍历权限列表,检测是否已授权 ==> 已授权:添加到已授权权限列表;未授权:添加到需要申请的权限列表
181 | for (String permission : mPermissions) {
182 | if (isGranted(permission)) {
183 | mPermissionsGranted.add(permission);
184 | } else {
185 | mPermissionsRequest.add(permission);
186 | }
187 | }
188 | // 判断需要申请的权限列表是否为空,如果不为空,前往申请权限
189 | if (mPermissionsRequest.isEmpty()) {
190 | requestCallback();
191 | } else {
192 | startPermissionFragment(fragmentManager);
193 | }
194 | }
195 | }
196 |
197 | @RequiresApi(api = Build.VERSION_CODES.M)
198 | private void startPermissionFragment(FragmentManager fragmentManager) {
199 | this.fragmentManager = fragmentManager;
200 | // 初始化拒绝、永久拒绝的权限集合
201 | mPermissionsDenied = new ArrayList<>();
202 | mPermissionsDeniedForever = new ArrayList<>();
203 | if (sInstance.mPermissionsRequest != null) {
204 | int size = sInstance.mPermissionsRequest.size();
205 |
206 | mPermissionsFragment = getPermissionsFragment(fragmentManager);
207 | mPermissionsFragment.requestPermissions(sInstance.mPermissionsRequest.toArray(new String[size]));
208 | }
209 | }
210 |
211 | private PermissionFragment getPermissionsFragment(FragmentManager fragmentManager) {
212 | PermissionFragment fragment = (PermissionFragment) findFragment(fragmentManager);
213 | if (fragment == null) {
214 | fragment = new PermissionFragment();
215 | fragmentManager
216 | .beginTransaction()
217 | .add(fragment, FRAGMENT_TAG)
218 | .commitNow();
219 | }
220 | return fragment;
221 | }
222 |
223 | private Fragment findFragment(FragmentManager fragmentManager) {
224 | return fragmentManager.findFragmentByTag(FRAGMENT_TAG);
225 | }
226 |
227 | @RequiresApi(api = Build.VERSION_CODES.M)
228 | public boolean rationale(final Activity activity) {
229 | boolean isRationale = false;
230 | if (mOnRationaleListener != null) {
231 | for (String permission : mPermissionsRequest) {
232 | if (activity.shouldShowRequestPermissionRationale(permission)) {
233 | getPermissionsStatus(activity);
234 | mOnRationaleListener.rationale(again -> {
235 | if (again) {
236 | startPermissionFragment(fragmentManager);
237 | } else {
238 | requestCallback();
239 | }
240 | });
241 | isRationale = true;
242 | break;
243 | }
244 | }
245 | mOnRationaleListener = null;
246 | }
247 | return isRationale;
248 | }
249 |
250 | private void getPermissionsStatus(final Activity activity) {
251 | for (String permission : mPermissionsRequest) {
252 | if (isGranted(permission)) {
253 | mPermissionsGranted.add(permission);
254 | } else {
255 | mPermissionsDenied.add(permission);
256 | if (!activity.shouldShowRequestPermissionRationale(permission)) {
257 | mPermissionsDeniedForever.add(permission);
258 | }
259 | }
260 | }
261 | }
262 |
263 | private void requestCallback() {
264 | if (mSimpleCallback != null) {
265 | if (mPermissionsRequest.size() == 0
266 | || mPermissions.size() == mPermissionsGranted.size()) {
267 | mSimpleCallback.onGranted();
268 | } else {
269 | if (!mPermissionsDenied.isEmpty()) {
270 | mSimpleCallback.onDenied();
271 | }
272 | }
273 | mSimpleCallback = null;
274 | }
275 | if (mFullCallback != null) {
276 | if (mPermissionsRequest.size() == 0
277 | || mPermissions.size() == mPermissionsGranted.size()) {
278 | mFullCallback.onGranted(mPermissionsGranted);
279 | } else {
280 | if (!mPermissionsDenied.isEmpty()) {
281 | mFullCallback.onDenied(mPermissionsDeniedForever, mPermissionsDenied);
282 | }
283 | }
284 | mFullCallback = null;
285 | }
286 | mOnRationaleListener = null;
287 | }
288 |
289 | private void onRequestPermissionsResult(final Activity activity) {
290 | getPermissionsStatus(activity);
291 | requestCallback();
292 | }
293 |
294 | public interface OnRationaleListener {
295 |
296 | void rationale(ShouldRequest shouldRequest);
297 |
298 | interface ShouldRequest {
299 | /**
300 | * 是否需要重新请求权限
301 | *
302 | * @param again true 要,false 不要
303 | */
304 | void again(boolean again);
305 | }
306 | }
307 |
308 | /**
309 | * 简单的权限申请回调
310 | */
311 | public interface SimpleCallback {
312 | /**
313 | * 权限申请成功
314 | */
315 | void onGranted();
316 |
317 | /**
318 | * 权限申请被拒绝
319 | */
320 | void onDenied();
321 | }
322 |
323 | public interface FullCallback {
324 | void onGranted(List