diff --git a/app-debug.apk b/app-debug.apk index 0749e87..36bb19f 100644 Binary files a/app-debug.apk and b/app-debug.apk differ diff --git a/app/build.gradle b/app/build.gradle index df94408..55a4e72 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 @@ -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' diff --git a/app/src/main/java/com/db/policylibdemo/MainActivity.java b/app/src/main/java/com/db/policylibdemo/MainActivity.java index f808283..0d704e6 100644 --- a/app/src/main/java/com/db/policylibdemo/MainActivity.java +++ b/app/src/main/java/com/db/policylibdemo/MainActivity.java @@ -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" + @@ -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("必要权限已经可以使用"); } @@ -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); } } @@ -139,7 +147,7 @@ public void onPermissionsGranted(int requestCode, @NonNull List perms) { @Override public void onPermissionsDenied(int requestCode, @NonNull List 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(); } @@ -160,7 +168,7 @@ private void showToast(String text) { } @Override - public void policyCancelClick() { - + public void policyCancelClick(int reqeustCode) { + showToast("必要的授权权限被禁止,无法正常使用"); } } diff --git a/app/src/main/java/com/db/policylibdemo/MainFragment.java b/app/src/main/java/com/db/policylibdemo/MainFragment.java new file mode 100644 index 0000000..25c097f --- /dev/null +++ b/app/src/main/java/com/db/policylibdemo/MainFragment.java @@ -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 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 perms) { + + } + + @Override + public void onPermissionsDenied(int requestCode, @NonNull List 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(); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 23e650b..8d506b5 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -14,4 +14,10 @@ android:padding="10dp" android:text="Hello World!" /> + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml new file mode 100644 index 0000000..538682f --- /dev/null +++ b/app/src/main/res/layout/fragment_main.xml @@ -0,0 +1,14 @@ + + + +