Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
更新
  • Loading branch information
tandongjay@126.com committed Dec 23, 2019
1 parent cbd8204 commit 459cef4
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 47 deletions.
Binary file modified app-debug.apk
Binary file not shown.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand All @@ -29,6 +29,7 @@ dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation project(':policylib')
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
20 changes: 14 additions & 6 deletions app/src/main/java/com/db/policylibdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public class MainActivity extends AppCompatActivity implements Policy.RuleListen
private static final String[] STORAGE_AND_PHONE =
{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE};
private static final int RC_STORAGE_PHONE_PERM = 125;
private MainFragment mainFragment;
private String text = "欢迎使用XX应用!我们将通过XXXXXX《用户协议》和《隐私政策》帮助您了解我们收集、使用、存储和共享个人信息的情况,以及您所享有的相关权利。\n\n" +
"• 为了向您提供XX音频文件生成存储、语音转文字、头像上传、用户注册等功能服务,我们需要使用您的一些存储权限、音视频录制权限、相机权限、获取设备信息等权限及信息。\n" +
"• 为了向您提供XX音频文件生成存储、头像上传、用户注册等功能服务,我们需要使用您的一些存储权限、音视频录制权限、相机权限、获取设备信息等权限及信息。\n" +
"• 您可以在个人中心修改、更正您的信息,也可以自己注销账户。\n" +
"• 我们会采用业界领先的安全技术保护好您的个人信息。\n\n" +
"您可以通过阅读完整版用户隐私政策,了解个人信息类型与用途的对应关系等更加详尽的个人信息处理规则。\n" +
Expand Down Expand Up @@ -72,6 +73,7 @@ public void twoClick() {

private void initView() {
tv_text = findViewById(R.id.tv_text);
mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
tv_text.setText("必要权限已经可以使用");
}

Expand Down Expand Up @@ -120,14 +122,20 @@ public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
if (requestCode == RC_STORAGE_PHONE_PERM) {
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
} else if (requestCode == MainFragment.RC_RECORD_AUDIO_PERM) {
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, mainFragment);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
if (requestCode == RC_STORAGE_PHONE_PERM) {
getPermission();
} else if (requestCode == MainFragment.RC_RECORD_AUDIO_PERM) {
mainFragment.onActivityResult(requestCode, resultCode, data);
}
}

Expand All @@ -139,7 +147,7 @@ public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this).build().show(list, this);
new AppSettingsDialog.Builder(this).build().show(requestCode, list, this);
} else {
getPermission();
}
Expand All @@ -160,7 +168,7 @@ private void showToast(String text) {
}

@Override
public void policyCancelClick() {

public void policyCancelClick(int reqeustCode) {
showToast("必要的授权权限被禁止,无法正常使用");
}
}
131 changes: 131 additions & 0 deletions app/src/main/java/com/db/policylibdemo/MainFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.db.policylibdemo;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.db.policylib.PermissionPolicy;
import com.db.policylib.Policy;

import java.util.ArrayList;
import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


public class MainFragment extends Fragment implements View.OnClickListener, EasyPermissions.PermissionCallbacks,
EasyPermissions.RationaleCallbacks, Policy.PolicyClick {
public static final int RC_RECORD_AUDIO_PERM = 122;
private static final String[] RECORD_AUDIO =
{Manifest.permission.RECORD_AUDIO};
private List<PermissionPolicy> list;
private Button btn_audio;

public MainFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_main, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btn_audio = getView().findViewById(R.id.btn_audio);
btn_audio.setOnClickListener(this);
list = new ArrayList<>();
PermissionPolicy permissionPolicy = new PermissionPolicy();
permissionPolicy.setPermission(Manifest.permission.RECORD_AUDIO);
permissionPolicy.setTitle("录音权限");
permissionPolicy.setDes("用于录制音频功能。");
permissionPolicy.setIcon(R.mipmap.icon_record_audio);
permissionPolicy.setRequest(true);
list.add(permissionPolicy);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_RECORD_AUDIO_PERM) {
showToast("设置回调");
getPermissions();
}
}

@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// EasyPermissions handles the request result.
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

@AfterPermissionGranted(RC_RECORD_AUDIO_PERM)
private void getPermissions() {
if (EasyPermissions.hasPermissions(getContext(), RECORD_AUDIO)) {
// Have permission, do the thing!
showToast("已获取权限");
} else {
// Request one permission
EasyPermissions.requestPermissions(this, "权限",
RC_RECORD_AUDIO_PERM, list, RECORD_AUDIO);
}
}

@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {

}

@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this).build().show(requestCode, list, this);
} else {
getPermissions();
}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_audio:
getPermissions();
break;
}
}

@Override
public void onRationaleAccepted(int requestCode) {

}

@Override
public void onRationaleDenied(int requestCode) {
showToast("取消授权");
}

@Override
public void policyCancelClick(int reqeustCode) {
showToast("必要的授权权限被禁止,无法正常使用");
}

private void showToast(String text) {
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
android:padding="10dp"
android:text="Hello World!" />

<fragment
android:id="@+id/fragment"
android:name="com.db.policylibdemo.MainFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_main" />
</LinearLayout>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainFragment">

<Button
android:id="@+id/btn_audio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="录音权限" />

</FrameLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<resources>
<string name="app_name">PolicyLibDemo</string>

<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>
2 changes: 1 addition & 1 deletion policylib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand Down
54 changes: 25 additions & 29 deletions policylib/src/main/java/com/db/policylib/Policy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

Expand All @@ -34,6 +33,8 @@
public class Policy {

private static volatile Policy instance = null;
private String title = "应用需要下列权限才可以正常使用";
public static final String TITLE = "应用需要下列权限才可以正常使用";

private Policy() {
}
Expand All @@ -49,11 +50,19 @@ public static Policy getInstance() {
return instance;
}

public void showPermissionDesDialog(Context context, List<PermissionPolicy> list, boolean before,
public void setTitle(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

public void showPermissionDesDialog(Context context, int requestCode, List<PermissionPolicy> list, boolean before,
final PolicyClick policyClick) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
if (policyClick != null) {
policyClick.policyCancelClick();
policyClick.policyCancelClick(requestCode);
}
return;
}
Expand All @@ -65,13 +74,13 @@ public void showPermissionDesDialog(Context context, List<PermissionPolicy> list
}
if (listRequest.size() == 0) {
if (policyClick != null) {
policyClick.policyCancelClick();
policyClick.policyCancelClick(requestCode);
}
return;
}
if (!before) {
if (policyClick != null) {
policyClick.policyCancelClick();
policyClick.policyCancelClick(requestCode);
}
return;
}
Expand All @@ -90,14 +99,14 @@ public void showPermissionDesDialog(Context context, List<PermissionPolicy> list
public void onClick(View v) {
dialog.dismiss();
if (policyClick != null) {
policyClick.policyCancelClick();
policyClick.policyCancelClick(requestCode);
}
}
});
}

public interface PolicyClick {
void policyCancelClick();
void policyCancelClick(int reqeustCode);
}

public boolean hasPermission(Context context, String permission) {
Expand Down Expand Up @@ -128,24 +137,14 @@ public interface RequestPermission {
void request(boolean showRequest);
}

public void showSettingDesDialog(final Context context, List<PermissionPolicy> list,
public void showSettingDesDialog(final Context context, int requestCode, List<PermissionPolicy> list,
final PolicyClick policyClick) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
if (policyClick != null) {
policyClick.policyCancelClick();
policyClick.policyCancelClick(requestCode);
}
return;
}
boolean request = false;
for (int i = 0; i < list.size(); i++) {
if (hasPermission(context, list.get(i).getPermission())) {
list.remove(i);
} else {
if (list.get(i).isRequest()) {
request = true;
}
}
}
final Dialog dialog = new Dialog(context, R.style.POLICY_DIALOG);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
Expand All @@ -158,13 +157,8 @@ public void showSettingDesDialog(final Context context, List<PermissionPolicy> l
TextView tv_request = dialog.findViewById(R.id.tv_request);
TextView tv_title = dialog.findViewById(R.id.tv_title);
TextView tv_tips = dialog.findViewById(R.id.tv_tips);
if (request) {
ll_bottom.setVisibility(View.GONE);
tv_request.setVisibility(View.VISIBLE);
} else {
ll_bottom.setVisibility(View.VISIBLE);
tv_request.setVisibility(View.GONE);
}
ll_bottom.setVisibility(View.VISIBLE);
tv_request.setVisibility(View.GONE);
tv_title.setText("应用选择了不再提示,请手动授权");
tv_tips.setVisibility(View.VISIBLE);
tv_tips.setText("您已经选择了不再提示,请去应用详情设置->权限里手动授权。");
Expand All @@ -177,7 +171,8 @@ public void onClick(View v) {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setData(Uri.fromParts("package", context.getPackageName(), null));
((Activity) context).startActivityForResult(intent, AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE);
((Activity) context).startActivityForResult(intent, requestCode > 0 ? requestCode :
AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE);
}
});
tv_request.setOnClickListener(new View.OnClickListener() {
Expand All @@ -186,15 +181,16 @@ public void onClick(View v) {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setData(Uri.fromParts("package", context.getPackageName(), null));
((Activity) context).startActivityForResult(intent, AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE);
((Activity) context).startActivityForResult(intent, requestCode > 0 ? requestCode :
AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE);
}
});
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if (policyClick != null) {
policyClick.policyCancelClick();
policyClick.policyCancelClick(requestCode);
}
}
});
Expand Down
Loading

0 comments on commit 459cef4

Please sign in to comment.