-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
更新
- Loading branch information
tandongjay@126.com
committed
Dec 23, 2019
1 parent
cbd8204
commit 459cef4
Showing
13 changed files
with
227 additions
and
47 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
app/src/main/java/com/db/policylibdemo/MainFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.