├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── libraries │ ├── appcompat_v7_23_1_1.xml │ ├── auto_common_0_3.xml │ ├── auto_service_1_0_rc2.xml │ ├── butterknife_7_0_1.xml │ ├── compiler_compiler.xml │ ├── guava_18_0.xml │ ├── javapoet_1_0_0.xml │ ├── mpermission_annotation_1_0_0.xml │ ├── mpermission_api_1_0_0.xml │ ├── permission_annotation_permission_annotation.xml │ ├── support_annotations_23_1_1.xml │ └── support_v4_23_1_1.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml ├── vcs.xml └── workspace.xml ├── LICENSE ├── MPermissions.iml ├── README.md ├── build.gradle ├── compiler ├── .gitignore ├── build.gradle ├── compiler.iml └── src │ └── main │ └── java │ └── com │ └── zhy │ └── m │ └── permission │ ├── ClassValidator.java │ ├── PermissionProcessor.java │ └── ProxyInfo.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── output └── mpermissions.jar ├── permission-annotation ├── .gitignore ├── build.gradle ├── permission-annotation.iml └── src │ └── main │ └── java │ └── com │ └── zhy │ └── m │ └── permission │ ├── PermissionDenied.java │ ├── PermissionGrant.java │ └── ShowRequestPermissionRationale.java ├── permission-lib ├── .gitignore ├── build.gradle ├── permission-lib.iml ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zhy │ │ └── m │ │ └── permission │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── zhy │ │ └── m │ │ └── permission │ │ ├── MPermissions.java │ │ ├── PermissionProxy.java │ │ └── Utils.java │ └── res │ └── values │ └── strings.xml ├── permission-sample ├── .gitignore ├── build.gradle ├── libs │ └── permission-lib.jar ├── map.txt ├── permission-sample.iml ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zhy │ │ └── permission_sample │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── zhy │ │ └── permission_sample │ │ ├── MainActivity.java │ │ └── TestFragment.java │ └── res │ ├── layout │ ├── activity_main.xml │ └── fragment_test.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | 34 | # Idea 35 | .idea 36 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | MPermissions -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/libraries/appcompat_v7_23_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/libraries/auto_common_0_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/auto_service_1_0_rc2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/butterknife_7_0_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/compiler_compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/guava_18_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/javapoet_1_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/mpermission_annotation_1_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/mpermission_api_1_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/libraries/permission_annotation_permission_annotation.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/support_annotations_23_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/support_v4_23_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.7 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /MPermissions.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MPermissions 2 | 3 | 基于Annotation Processor的简单易用的处理Android M运行时权限的库。 4 | 5 | 部分代码来自[PermissionGen](https://github.com/hongyangAndroid/PermissionGen), 6 | 主要是将其基于运行时注解的实现修改为Annotation Processor的方式,即编译时注解。 7 | 8 | ## 引入 9 | 10 | project's build.gradle 11 | 12 | ``` 13 | buildscript { 14 | dependencies { 15 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' 16 | } 17 | } 18 | ``` 19 | 20 | module's buid.gradle 21 | 22 | ``` 23 | apply plugin: 'com.neenbedankt.android-apt' 24 | 25 | dependencies { 26 | apt 'com.zhy:mpermission-compiler:1.0.0' 27 | compile 'com.zhy:mpermission-api:1.0.0' 28 | } 29 | ``` 30 | 31 | ## 使用 32 | 33 | * 申请权限 34 | 35 | ```java 36 | MPermissions.requestPermissions(MainActivity.this, REQUECT_CODE_SDCARD, Manifest.permission.WRITE_EXTERNAL_STORAGE); 37 | ``` 38 | 39 | * 处理权限回调 40 | 41 | ```java 42 | @Override 43 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 44 | { 45 | MPermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults); 46 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 47 | } 48 | ``` 49 | 50 | * 是否需要弹出解释 51 | 52 | ``` 53 | if (!MPermissions.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUECT_CODE_SDCARD)) 54 | { 55 | MPermissions.requestPermissions(MainActivity.this, REQUECT_CODE_SDCARD, Manifest.permission.WRITE_EXTERNAL_STORAGE); 56 | } 57 | ``` 58 | 59 | 如果需要解释,会自动执行使用`@ShowRequestPermissionRationale`注解的方法。 60 | 61 | 授权成功以及失败调用的分支方法通过注解`@PermissionGrant`和`@PermissionDenied`进行标识,详细参考下面的例子或者sample。 62 | 63 | ## 例子 64 | 65 | * in Activity: 66 | 67 | ```java 68 | public class MainActivity extends AppCompatActivity 69 | { 70 | 71 | private Button mBtnSdcard; 72 | private static final int REQUECT_CODE_SDCARD = 2; 73 | 74 | @Override 75 | protected void onCreate(Bundle savedInstanceState) 76 | { 77 | super.onCreate(savedInstanceState); 78 | setContentView(R.layout.activity_main); 79 | 80 | mBtnSdcard = (Button) findViewById(R.id.id_btn_sdcard); 81 | mBtnSdcard.setOnClickListener(new View.OnClickListener() 82 | { 83 | @Override 84 | public void onClick(View v) 85 | { 86 | MPermissions.requestPermissions(MainActivity.this, REQUECT_CODE_SDCARD, Manifest.permission.WRITE_EXTERNAL_STORAGE); 87 | } 88 | }); 89 | } 90 | 91 | @Override 92 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 93 | { 94 | MPermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults); 95 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 96 | } 97 | 98 | 99 | @PermissionGrant(REQUECT_CODE_SDCARD) 100 | public void requestSdcardSuccess() 101 | { 102 | Toast.makeText(this, "GRANT ACCESS SDCARD!", Toast.LENGTH_SHORT).show(); 103 | } 104 | 105 | @PermissionDenied(REQUECT_CODE_SDCARD) 106 | public void requestSdcardFailed() 107 | { 108 | Toast.makeText(this, "DENY ACCESS SDCARD!", Toast.LENGTH_SHORT).show(); 109 | } 110 | } 111 | ``` 112 | 113 | * in Fragment: 114 | 115 | ```java 116 | 117 | public class TestFragment extends Fragment 118 | { 119 | @Nullable 120 | @Override 121 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 122 | { 123 | return inflater.inflate(R.layout.fragment_test, container, false); 124 | } 125 | 126 | @Override 127 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) 128 | { 129 | view.findViewById(R.id.id_btn_contact).setOnClickListener(new View.OnClickListener() 130 | { 131 | @Override 132 | public void onClick(View v) 133 | { 134 | MPermissions.requestPermissions(TestFragment.this, 4, Manifest.permission.WRITE_CONTACTS); 135 | } 136 | }); 137 | 138 | } 139 | 140 | @PermissionGrant(4) 141 | public void requestContactSuccess() 142 | { 143 | Toast.makeText(getActivity(), "GRANT ACCESS CONTACTS!", Toast.LENGTH_SHORT).show(); 144 | } 145 | 146 | @PermissionDenied(4) 147 | public void requestContactFailed() 148 | { 149 | Toast.makeText(getActivity(), "DENY ACCESS CONTACTS!", Toast.LENGTH_SHORT).show(); 150 | } 151 | 152 | @Override 153 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 154 | { 155 | MPermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults); 156 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 157 | } 158 | } 159 | 160 | ``` 161 | 162 | ## 混淆 163 | 164 | ``` 165 | -dontwarn com.zhy.m.** 166 | -keep class com.zhy.m.** {*;} 167 | -keep interface com.zhy.m.** { *; } 168 | -keep class **$$PermissionProxy { *; } 169 | ``` 170 | 171 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | google() 7 | } 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:3.4.0' 10 | classpath 'com.novoda:bintray-release:0.2.10' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | jcenter() 20 | google() 21 | } 22 | } 23 | 24 | ext { 25 | userOrg = 'hongyangandroid' 26 | groupId = 'com.zhy' 27 | uploadName = 'MPermission' 28 | publishVersion = '1.0.0' 29 | description = 'a easy API to use runtime permission for Android M ' 30 | website = 'https://github.com/hongyangAndroid/MPermissions' 31 | licences = ['Apache-2.0'] 32 | } 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /compiler/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /compiler/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'bintray-release' 3 | sourceCompatibility = JavaVersion.VERSION_1_7 4 | targetCompatibility = JavaVersion.VERSION_1_7 5 | dependencies { 6 | implementation project (':permission-annotation') 7 | annotationProcessor 'com.google.auto.service:auto-service:1.0-rc2' 8 | implementation 'com.google.auto.service:auto-service:1.0-rc2' 9 | implementation 'com.squareup:javapoet:1.0.0' 10 | } 11 | 12 | publish { 13 | artifactId = 'mpermission-compiler' 14 | userOrg = rootProject.userOrg 15 | groupId = rootProject.groupId 16 | uploadName = rootProject.uploadName 17 | publishVersion = rootProject.publishVersion 18 | description = rootProject.description 19 | website = rootProject.website 20 | licences = rootProject.licences 21 | } 22 | 23 | -------------------------------------------------------------------------------- /compiler/compiler.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /compiler/src/main/java/com/zhy/m/permission/ClassValidator.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | import javax.lang.model.element.Element; 4 | import javax.lang.model.element.TypeElement; 5 | 6 | import static javax.lang.model.element.Modifier.ABSTRACT; 7 | import static javax.lang.model.element.Modifier.PRIVATE; 8 | import static javax.lang.model.element.Modifier.PUBLIC; 9 | 10 | final class ClassValidator 11 | { 12 | static boolean isPublic(Element annotatedClass) 13 | { 14 | return annotatedClass.getModifiers().contains(PUBLIC); 15 | } 16 | 17 | static boolean isPrivate(Element annotatedClass) 18 | { 19 | return annotatedClass.getModifiers().contains(PRIVATE); 20 | } 21 | 22 | static boolean isAbstract(Element annotatedClass) 23 | { 24 | return annotatedClass.getModifiers().contains(ABSTRACT); 25 | } 26 | 27 | static String getClassName(TypeElement type, String packageName) 28 | { 29 | int packageLen = packageName.length() + 1; 30 | return type.getQualifiedName().toString().substring(packageLen) 31 | .replace('.', '$'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /compiler/src/main/java/com/zhy/m/permission/PermissionProcessor.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | import com.google.auto.service.AutoService; 4 | 5 | import java.io.IOException; 6 | import java.io.Writer; 7 | import java.lang.annotation.Annotation; 8 | import java.util.HashMap; 9 | import java.util.HashSet; 10 | import java.util.LinkedHashSet; 11 | import java.util.Map; 12 | import java.util.Set; 13 | 14 | import javax.annotation.processing.AbstractProcessor; 15 | import javax.annotation.processing.Messager; 16 | import javax.annotation.processing.ProcessingEnvironment; 17 | import javax.annotation.processing.Processor; 18 | import javax.annotation.processing.RoundEnvironment; 19 | import javax.lang.model.SourceVersion; 20 | import javax.lang.model.element.Element; 21 | import javax.lang.model.element.ElementKind; 22 | import javax.lang.model.element.ExecutableElement; 23 | import javax.lang.model.element.TypeElement; 24 | import javax.lang.model.util.Elements; 25 | import javax.tools.Diagnostic; 26 | import javax.tools.JavaFileObject; 27 | 28 | import static javax.lang.model.SourceVersion.latestSupported; 29 | 30 | @AutoService(Processor.class) 31 | public class PermissionProcessor extends AbstractProcessor 32 | { 33 | private Messager messager; 34 | private Elements elementUtils; 35 | private Map mProxyMap = new HashMap(); 36 | 37 | @Override 38 | public synchronized void init(ProcessingEnvironment processingEnv) 39 | { 40 | super.init(processingEnv); 41 | messager = processingEnv.getMessager(); 42 | elementUtils = processingEnv.getElementUtils(); 43 | } 44 | 45 | @Override 46 | public Set getSupportedAnnotationTypes() 47 | { 48 | HashSet supportTypes = new LinkedHashSet<>(); 49 | supportTypes.add(PermissionDenied.class.getCanonicalName()); 50 | supportTypes.add(PermissionGrant.class.getCanonicalName()); 51 | supportTypes.add(ShowRequestPermissionRationale.class.getCanonicalName()); 52 | return supportTypes; 53 | } 54 | 55 | @Override 56 | public SourceVersion getSupportedSourceVersion() 57 | { 58 | return latestSupported(); 59 | } 60 | 61 | private boolean processAnnotations(RoundEnvironment roundEnv, Class clazz) 62 | { 63 | for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(clazz)) 64 | { 65 | 66 | if (!checkMethodValid(annotatedElement, clazz)) return false; 67 | 68 | ExecutableElement annotatedMethod = (ExecutableElement) annotatedElement; 69 | //class type 70 | TypeElement classElement = (TypeElement) annotatedMethod.getEnclosingElement(); 71 | //full class name 72 | String fqClassName = classElement.getQualifiedName().toString(); 73 | 74 | ProxyInfo proxyInfo = mProxyMap.get(fqClassName); 75 | if (proxyInfo == null) 76 | { 77 | proxyInfo = new ProxyInfo(elementUtils, classElement); 78 | mProxyMap.put(fqClassName, proxyInfo); 79 | proxyInfo.setTypeElement(classElement); 80 | } 81 | 82 | 83 | Annotation annotation = annotatedMethod.getAnnotation(clazz); 84 | if (annotation instanceof PermissionGrant) 85 | { 86 | int requestCode = ((PermissionGrant) annotation).value(); 87 | proxyInfo.grantMethodMap.put(requestCode, annotatedMethod.getSimpleName().toString()); 88 | } else if (annotation instanceof PermissionDenied) 89 | { 90 | int requestCode = ((PermissionDenied) annotation).value(); 91 | proxyInfo.deniedMethodMap.put(requestCode, annotatedMethod.getSimpleName().toString()); 92 | } else if (annotation instanceof ShowRequestPermissionRationale) 93 | { 94 | int requestCode = ((ShowRequestPermissionRationale) annotation).value(); 95 | proxyInfo.rationaleMethodMap.put(requestCode, annotatedMethod.getSimpleName().toString()); 96 | } else 97 | { 98 | error(annotatedElement, "%s not support .", clazz.getSimpleName()); 99 | return false; 100 | } 101 | 102 | } 103 | 104 | return true; 105 | } 106 | 107 | @Override 108 | public boolean process(Set annotations, RoundEnvironment roundEnv) 109 | { 110 | mProxyMap.clear(); 111 | messager.printMessage(Diagnostic.Kind.NOTE, "process..."); 112 | 113 | if (!processAnnotations(roundEnv, PermissionGrant.class)) return false; 114 | if (!processAnnotations(roundEnv, PermissionDenied.class)) return false; 115 | if (!processAnnotations(roundEnv, ShowRequestPermissionRationale.class)) return false; 116 | 117 | 118 | for (String key : mProxyMap.keySet()) 119 | { 120 | ProxyInfo proxyInfo = mProxyMap.get(key); 121 | try 122 | { 123 | JavaFileObject jfo = processingEnv.getFiler().createSourceFile( 124 | proxyInfo.getProxyClassFullName(), 125 | proxyInfo.getTypeElement()); 126 | Writer writer = jfo.openWriter(); 127 | writer.write(proxyInfo.generateJavaCode()); 128 | writer.flush(); 129 | writer.close(); 130 | } catch (IOException e) 131 | { 132 | error(proxyInfo.getTypeElement(), 133 | "Unable to write injector for type %s: %s", 134 | proxyInfo.getTypeElement(), e.getMessage()); 135 | } 136 | 137 | } 138 | return true; 139 | } 140 | 141 | private void error(Element element, String message, Object... args) 142 | { 143 | if (args.length > 0) 144 | { 145 | message = String.format(message, args); 146 | } 147 | processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, message, element); 148 | } 149 | 150 | private boolean checkMethodValid(Element annotatedElement, Class clazz) 151 | { 152 | if (annotatedElement.getKind() != ElementKind.METHOD) 153 | { 154 | error(annotatedElement, "%s must be declared on method.", clazz.getSimpleName()); 155 | return false; 156 | } 157 | if (ClassValidator.isPrivate(annotatedElement) || ClassValidator.isAbstract(annotatedElement)) 158 | { 159 | error(annotatedElement, "%s() must can not be abstract or private.", annotatedElement.getSimpleName()); 160 | return false; 161 | } 162 | 163 | return true; 164 | } 165 | 166 | } 167 | -------------------------------------------------------------------------------- /compiler/src/main/java/com/zhy/m/permission/ProxyInfo.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import javax.lang.model.element.PackageElement; 7 | import javax.lang.model.element.TypeElement; 8 | import javax.lang.model.util.Elements; 9 | 10 | public class ProxyInfo 11 | { 12 | private String packageName; 13 | private String proxyClassName; 14 | private TypeElement typeElement; 15 | 16 | Map grantMethodMap = new HashMap<>(); 17 | Map deniedMethodMap = new HashMap<>(); 18 | Map rationaleMethodMap = new HashMap<>(); 19 | 20 | public static final String PROXY = "PermissionProxy"; 21 | 22 | public ProxyInfo(Elements elementUtils, TypeElement classElement) 23 | { 24 | PackageElement packageElement = elementUtils.getPackageOf(classElement); 25 | String packageName = packageElement.getQualifiedName().toString(); 26 | //classname 27 | String className = ClassValidator.getClassName(classElement, packageName); 28 | this.packageName = packageName; 29 | this.proxyClassName = className + "$$" + PROXY; 30 | } 31 | 32 | 33 | public String getProxyClassFullName() 34 | { 35 | return packageName + "." + proxyClassName; 36 | } 37 | 38 | public String generateJavaCode() 39 | { 40 | StringBuilder builder = new StringBuilder(); 41 | builder.append("// Generated code. Do not modify!\n"); 42 | builder.append("package ").append(packageName).append(";\n\n"); 43 | builder.append("import com.zhy.m.permission.*;\n"); 44 | builder.append('\n'); 45 | 46 | builder.append("public class ").append(proxyClassName).append(" implements " + ProxyInfo.PROXY + "<" + typeElement.getSimpleName() + ">"); 47 | builder.append(" {\n"); 48 | 49 | generateMethods(builder); 50 | builder.append('\n'); 51 | 52 | builder.append("}\n"); 53 | return builder.toString(); 54 | 55 | } 56 | 57 | 58 | private void generateMethods(StringBuilder builder) 59 | { 60 | 61 | generateGrantMethod(builder); 62 | generateDeniedMethod(builder); 63 | generateRationaleMethod(builder); 64 | 65 | 66 | } 67 | 68 | private void generateRationaleMethod(StringBuilder builder) 69 | { 70 | builder.append("@Override\n "); 71 | builder.append("public void rationale(" + typeElement.getSimpleName() + " source , int requestCode) {\n"); 72 | builder.append("switch(requestCode) {"); 73 | for (int code : rationaleMethodMap.keySet()) 74 | { 75 | builder.append("case " + code + ":"); 76 | builder.append("source." + rationaleMethodMap.get(code) + "();"); 77 | builder.append("break;"); 78 | } 79 | 80 | builder.append("}"); 81 | builder.append(" }\n"); 82 | 83 | /// 84 | 85 | builder.append("@Override\n "); 86 | builder.append("public boolean needShowRationale(int requestCode) {\n"); 87 | builder.append("switch(requestCode) {"); 88 | for (int code : rationaleMethodMap.keySet()) 89 | { 90 | builder.append("case " + code + ":"); 91 | builder.append("return true;"); 92 | } 93 | builder.append("}\n"); 94 | builder.append("return false;"); 95 | 96 | builder.append(" }\n"); 97 | } 98 | 99 | private void generateDeniedMethod(StringBuilder builder) 100 | { 101 | builder.append("@Override\n "); 102 | builder.append("public void denied(" + typeElement.getSimpleName() + " source , int requestCode) {\n"); 103 | builder.append("switch(requestCode) {"); 104 | for (int code : deniedMethodMap.keySet()) 105 | { 106 | builder.append("case " + code + ":"); 107 | builder.append("source." + deniedMethodMap.get(code) + "();"); 108 | builder.append("break;"); 109 | } 110 | 111 | builder.append("}"); 112 | builder.append(" }\n"); 113 | } 114 | 115 | private void generateGrantMethod(StringBuilder builder) 116 | { 117 | builder.append("@Override\n "); 118 | builder.append("public void grant(" + typeElement.getSimpleName() + " source , int requestCode) {\n"); 119 | builder.append("switch(requestCode) {"); 120 | for (int code : grantMethodMap.keySet()) 121 | { 122 | builder.append("case " + code + ":"); 123 | builder.append("source." + grantMethodMap.get(code) + "();"); 124 | builder.append("break;"); 125 | } 126 | 127 | builder.append("}"); 128 | builder.append(" }\n"); 129 | } 130 | 131 | public TypeElement getTypeElement() 132 | { 133 | return typeElement; 134 | } 135 | 136 | public void setTypeElement(TypeElement typeElement) 137 | { 138 | this.typeElement = typeElement; 139 | } 140 | 141 | 142 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/MPermissions/cfe39a25655d8555abe2ef94956a02517ea53706/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Feb 21 19:24:40 GMT+08:00 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /output/mpermissions.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/MPermissions/cfe39a25655d8555abe2ef94956a02517ea53706/output/mpermissions.jar -------------------------------------------------------------------------------- /permission-annotation/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /permission-annotation/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'bintray-release' 3 | sourceCompatibility = JavaVersion.VERSION_1_7 4 | targetCompatibility = JavaVersion.VERSION_1_7 5 | 6 | 7 | publish { 8 | artifactId = 'mpermission-annotation' 9 | userOrg = rootProject.userOrg 10 | groupId = rootProject.groupId 11 | uploadName = rootProject.uploadName 12 | publishVersion = rootProject.publishVersion 13 | description = rootProject.description 14 | website = rootProject.website 15 | licences = rootProject.licences 16 | } -------------------------------------------------------------------------------- /permission-annotation/permission-annotation.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /permission-annotation/src/main/java/com/zhy/m/permission/PermissionDenied.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Target; 5 | 6 | /** 7 | * Created by zhy on 16/2/19. 8 | */ 9 | @Target(ElementType.METHOD) 10 | public @interface PermissionDenied 11 | { 12 | int value(); 13 | } 14 | -------------------------------------------------------------------------------- /permission-annotation/src/main/java/com/zhy/m/permission/PermissionGrant.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Target; 5 | 6 | @Target(ElementType.METHOD) 7 | public @interface PermissionGrant 8 | { 9 | int value(); 10 | } 11 | -------------------------------------------------------------------------------- /permission-annotation/src/main/java/com/zhy/m/permission/ShowRequestPermissionRationale.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * Created by zhy on 16/3/2. 10 | */ 11 | @Target(ElementType.METHOD) 12 | @Retention(RetentionPolicy.CLASS) 13 | public @interface ShowRequestPermissionRationale 14 | { 15 | int value(); 16 | } 17 | -------------------------------------------------------------------------------- /permission-lib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /permission-lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'bintray-release' 3 | 4 | android { 5 | compileSdkVersion 23 6 | 7 | defaultConfig { 8 | minSdkVersion 10 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:23.1.1' 24 | api project(':permission-annotation') 25 | } 26 | 27 | // Jar 28 | task androidJar(type: Jar) { 29 | dependsOn assemble 30 | group 'Build' 31 | description 'blah blah' 32 | from zipTree( 33 | 'build/intermediates/bundles/release/classes.jar') 34 | from zipTree( 35 | '../compiler/build/libs/compiler.jar') 36 | from zipTree( 37 | '../permission-annotation/build/libs/permission-annotation.jar') 38 | 39 | } 40 | 41 | 42 | 43 | // javadoc tasks 44 | android.libraryVariants.all { variant -> 45 | task("javadoc${variant.name.capitalize()}", type: Javadoc) { 46 | description "Generates Javadoc for $variant.name." 47 | group 'Docs' 48 | source = variant.javaCompile.source 49 | source "../permission-annotation/src/main/java" 50 | 51 | exclude '**/BuildConfig.java' 52 | exclude '**/R.java' 53 | } 54 | } 55 | 56 | 57 | publish { 58 | artifactId = 'mpermission-api' 59 | userOrg = rootProject.userOrg 60 | groupId = rootProject.groupId 61 | uploadName = rootProject.uploadName 62 | publishVersion = rootProject.publishVersion 63 | description = rootProject.description 64 | website = rootProject.website 65 | licences = rootProject.licences 66 | } 67 | 68 | task copyJar(type: Copy) { 69 | from('build/libs/permission-lib.jar') 70 | into('../output/') 71 | rename ('permission-lib.jar', 'mpermissions.jar') 72 | } -------------------------------------------------------------------------------- /permission-lib/permission-lib.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /permission-lib/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /permission-lib/src/androidTest/java/com/zhy/m/permission/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase 10 | { 11 | public ApplicationTest() 12 | { 13 | super(Application.class); 14 | } 15 | } -------------------------------------------------------------------------------- /permission-lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /permission-lib/src/main/java/com/zhy/m/permission/MPermissions.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | import android.annotation.TargetApi; 4 | import android.app.Activity; 5 | import android.content.pm.PackageManager; 6 | import android.os.Build; 7 | import android.support.v4.app.ActivityCompat; 8 | import android.support.v4.app.Fragment; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | 14 | /** 15 | * Created by namee on 2015. 11. 17.. 16 | *
17 | * modified by hongyangAndroid 2016.02.21 18 | */ 19 | public class MPermissions 20 | { 21 | private static final String SUFFIX = "$$PermissionProxy"; 22 | 23 | public static void requestPermissions(Activity object, int requestCode, String... permissions) 24 | { 25 | _requestPermissions(object, requestCode, permissions); 26 | } 27 | 28 | public static void requestPermissions(Fragment object, int requestCode, String... permissions) 29 | { 30 | _requestPermissions(object, requestCode, permissions); 31 | } 32 | 33 | public static boolean shouldShowRequestPermissionRationale(Activity activity, String permission, int requestCode) 34 | { 35 | PermissionProxy proxy = findPermissionProxy(activity); 36 | if (!proxy.needShowRationale(requestCode)) return false; 37 | if (ActivityCompat.shouldShowRequestPermissionRationale(activity, 38 | permission)) 39 | { 40 | proxy.rationale(activity, requestCode); 41 | return true; 42 | } 43 | return false; 44 | } 45 | 46 | @TargetApi(value = Build.VERSION_CODES.M) 47 | private static void _requestPermissions(Object object, int requestCode, String... permissions) 48 | { 49 | if (!Utils.isOverMarshmallow()) 50 | { 51 | doExecuteSuccess(object, requestCode); 52 | return; 53 | } 54 | List deniedPermissions = Utils.findDeniedPermissions(Utils.getActivity(object), permissions); 55 | 56 | if (deniedPermissions.size() > 0) 57 | { 58 | if (object instanceof Activity) 59 | { 60 | ((Activity) object).requestPermissions(deniedPermissions.toArray(new String[deniedPermissions.size()]), requestCode); 61 | } else if (object instanceof Fragment) 62 | { 63 | ((Fragment) object).requestPermissions(deniedPermissions.toArray(new String[deniedPermissions.size()]), requestCode); 64 | } else 65 | { 66 | throw new IllegalArgumentException(object.getClass().getName() + " is not supported!"); 67 | } 68 | } else 69 | { 70 | doExecuteSuccess(object, requestCode); 71 | } 72 | } 73 | 74 | 75 | private static PermissionProxy findPermissionProxy(Object activity) 76 | { 77 | try 78 | { 79 | Class clazz = activity.getClass(); 80 | Class injectorClazz = Class.forName(clazz.getName() + SUFFIX); 81 | return (PermissionProxy) injectorClazz.newInstance(); 82 | } catch (ClassNotFoundException e) 83 | { 84 | e.printStackTrace(); 85 | } catch (InstantiationException e) 86 | { 87 | e.printStackTrace(); 88 | } catch (IllegalAccessException e) 89 | { 90 | e.printStackTrace(); 91 | } 92 | throw new RuntimeException(String.format("can not find %s , something when compiler.", activity.getClass().getSimpleName() + SUFFIX)); 93 | } 94 | 95 | 96 | private static void doExecuteSuccess(Object activity, int requestCode) 97 | { 98 | findPermissionProxy(activity).grant(activity, requestCode); 99 | 100 | } 101 | 102 | private static void doExecuteFail(Object activity, int requestCode) 103 | { 104 | findPermissionProxy(activity).denied(activity, requestCode); 105 | } 106 | 107 | public static void onRequestPermissionsResult(Activity activity, int requestCode, String[] permissions, 108 | int[] grantResults) 109 | { 110 | requestResult(activity, requestCode, permissions, grantResults); 111 | } 112 | 113 | public static void onRequestPermissionsResult(Fragment fragment, int requestCode, String[] permissions, 114 | int[] grantResults) 115 | { 116 | requestResult(fragment, requestCode, permissions, grantResults); 117 | } 118 | 119 | private static void requestResult(Object obj, int requestCode, String[] permissions, 120 | int[] grantResults) 121 | { 122 | List deniedPermissions = new ArrayList<>(); 123 | for (int i = 0; i < grantResults.length; i++) 124 | { 125 | if (grantResults[i] != PackageManager.PERMISSION_GRANTED) 126 | { 127 | deniedPermissions.add(permissions[i]); 128 | } 129 | } 130 | if (deniedPermissions.size() > 0) 131 | { 132 | doExecuteFail(obj, requestCode); 133 | } else 134 | { 135 | doExecuteSuccess(obj, requestCode); 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /permission-lib/src/main/java/com/zhy/m/permission/PermissionProxy.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | /** 4 | * Created by zhy on 16/2/21. 5 | */ 6 | public interface PermissionProxy 7 | { 8 | void grant(T source, int requestCode); 9 | 10 | void denied(T source, int requestCode); 11 | 12 | void rationale(T source, int requestCode); 13 | 14 | boolean needShowRationale(int requestCode); 15 | } 16 | -------------------------------------------------------------------------------- /permission-lib/src/main/java/com/zhy/m/permission/Utils.java: -------------------------------------------------------------------------------- 1 | package com.zhy.m.permission; 2 | 3 | import android.annotation.TargetApi; 4 | import android.app.Activity; 5 | import android.content.pm.PackageManager; 6 | import android.os.Build; 7 | import android.support.v4.app.Fragment; 8 | 9 | import java.lang.annotation.Annotation; 10 | import java.lang.reflect.Method; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * Created by namee on 2015. 11. 18.. 16 | */ 17 | final public class Utils { 18 | private Utils(){} 19 | 20 | public static boolean isOverMarshmallow() { 21 | return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; 22 | } 23 | 24 | @TargetApi(value = Build.VERSION_CODES.M) 25 | public static List findDeniedPermissions(Activity activity, String... permission){ 26 | List denyPermissions = new ArrayList<>(); 27 | for(String value : permission){ 28 | if(activity.checkSelfPermission(value) != PackageManager.PERMISSION_GRANTED){ 29 | denyPermissions.add(value); 30 | } 31 | } 32 | return denyPermissions; 33 | } 34 | 35 | public static List findAnnotationMethods(Class clazz, Class clazz1){ 36 | List methods = new ArrayList<>(); 37 | for(Method method : clazz.getDeclaredMethods()){ 38 | if(method.isAnnotationPresent(clazz1)){ 39 | methods.add(method); 40 | } 41 | } 42 | return methods; 43 | } 44 | 45 | 46 | 47 | public static Activity getActivity(Object object){ 48 | if(object instanceof Fragment){ 49 | return ((Fragment)object).getActivity(); 50 | } else if(object instanceof Activity){ 51 | return (Activity) object; 52 | } 53 | return null; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /permission-lib/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | permission-lib 3 | 4 | -------------------------------------------------------------------------------- /permission-sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /permission-sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | 6 | defaultConfig { 7 | applicationId "com.zhy.permission_sample" 8 | minSdkVersion 10 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | // debug{ 19 | // minifyEnabled true 20 | // proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | // } 22 | } 23 | } 24 | 25 | 26 | 27 | dependencies { 28 | // implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | implementation 'com.android.support:appcompat-v7:23.1.1' 30 | 31 | // implementation project(':permission-lib') 32 | // annotationProcessor project(':compiler') 33 | 34 | annotationProcessor 'com.zhy:mpermission-compiler:1.0.0' 35 | implementation 'com.zhy:mpermission-api:1.0.0' 36 | 37 | } 38 | -------------------------------------------------------------------------------- /permission-sample/libs/permission-lib.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/MPermissions/cfe39a25655d8555abe2ef94956a02517ea53706/permission-sample/libs/permission-lib.jar -------------------------------------------------------------------------------- /permission-sample/permission-sample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /permission-sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -dontshrink 19 | -flattenpackagehierarchy 20 | #优化时允许访问并修改有修饰符的类和类的成员 21 | -allowaccessmodification 22 | #混淆前后的映射 23 | -printmapping map.txt 24 | #不跳过(混淆) jars中的 非public classes 默认选项 25 | -dontskipnonpubliclibraryclassmembers 26 | #忽略警告 27 | -ignorewarnings 28 | #指定代码的压缩级别 29 | -optimizationpasses 5 30 | #包明不混合大小写 31 | -dontusemixedcaseclassnames 32 | #不去忽略非公共的库类 33 | -dontskipnonpubliclibraryclasses 34 | #优化 不优化输入的类文件 35 | -dontoptimize 36 | #不预校验 37 | -dontpreverify 38 | #混淆时是否记录日志 39 | -verbose 40 | #混淆时所采用的算法 41 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 42 | #保护注解 43 | -renamesourcefileattribute SourceFile 44 | -keepattributes SourceFile,LineNumberTable 45 | -keepattributes *Annotation* 46 | -keepattributes Signature 47 | -keepattributes Exceptions,InnerClasses 48 | -dontwarn org.apache.** 49 | -dontwarn android.support.** 50 | 51 | #基础配置 52 | # 保持哪些类不被混淆 53 | -keep public class * extends android.app.Fragment 54 | -keep public class * extends android.app.Activity 55 | -keep public class * extends android.app.Application 56 | -keep public class * extends android.app.Service 57 | -keep public class * extends android.content.BroadcastReceiver 58 | -keep public class * extends android.content.ContentProvider 59 | -keep public class * extends android.app.backup.BackupAgentHelper 60 | -keep public class * extends android.preference.Preference 61 | -keep public class com.android.vending.licensing.ILicensingService 62 | #如果有引用v4包可以添加下面这行 63 | -keep public class * extends android.support.v4.app.Fragment 64 | -keep public class * extends android.view.View {*;} 65 | -keep class android.support.v4.**{ *; } 66 | -keep class android.support.v7.**{ *; } 67 | -keep class android.webkit.**{*;} 68 | -keep interface android.support.v4.app.** { *; } 69 | 70 | 71 | 72 | 73 | -dontwarn com.zhy.m.** 74 | -keep class com.zhy.m.** {*;} 75 | -keep interface com.zhy.m.** { *; } 76 | -keep class **$$PermissionProxy { *; } -------------------------------------------------------------------------------- /permission-sample/src/androidTest/java/com/zhy/permission_sample/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.zhy.permission_sample; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase 10 | { 11 | public ApplicationTest() 12 | { 13 | super(Application.class); 14 | } 15 | } -------------------------------------------------------------------------------- /permission-sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 13 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /permission-sample/src/main/java/com/zhy/permission_sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhy.permission_sample; 2 | 3 | import android.Manifest; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.Toast; 9 | 10 | import com.zhy.m.permission.MPermissions; 11 | import com.zhy.m.permission.PermissionDenied; 12 | import com.zhy.m.permission.PermissionGrant; 13 | import com.zhy.m.permission.ShowRequestPermissionRationale; 14 | 15 | public class MainActivity extends AppCompatActivity 16 | { 17 | 18 | private Button mBtnSdcard, mBtnCallPhone; 19 | private static final int REQUECT_CODE_SDCARD = 2; 20 | private static final int REQUECT_CODE_CALL_PHONE = 3; 21 | 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) 25 | { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_main); 28 | 29 | mBtnSdcard = (Button) findViewById(R.id.id_btn_sdcard); 30 | mBtnCallPhone = (Button) findViewById(R.id.id_btn_callphone); 31 | 32 | mBtnSdcard.setOnClickListener(new View.OnClickListener() 33 | { 34 | @Override 35 | public void onClick(View v) 36 | { 37 | 38 | if (!MPermissions.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUECT_CODE_SDCARD)) 39 | { 40 | MPermissions.requestPermissions(MainActivity.this, REQUECT_CODE_SDCARD, Manifest.permission.WRITE_EXTERNAL_STORAGE); 41 | } 42 | } 43 | }); 44 | 45 | mBtnCallPhone.setOnClickListener(new View.OnClickListener() 46 | { 47 | @Override 48 | public void onClick(View v) 49 | { 50 | MPermissions.requestPermissions(MainActivity.this, REQUECT_CODE_CALL_PHONE, Manifest.permission.CALL_PHONE); 51 | } 52 | }); 53 | } 54 | 55 | @ShowRequestPermissionRationale(REQUECT_CODE_SDCARD) 56 | public void whyNeedSdCard() 57 | { 58 | Toast.makeText(this, "I need write news to sdcard!", Toast.LENGTH_SHORT).show(); 59 | MPermissions.requestPermissions(MainActivity.this, REQUECT_CODE_SDCARD, Manifest.permission.WRITE_EXTERNAL_STORAGE); 60 | 61 | } 62 | 63 | 64 | @Override 65 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 66 | { 67 | MPermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults); 68 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 69 | } 70 | 71 | 72 | @PermissionGrant(REQUECT_CODE_SDCARD) 73 | public void requestSdcardSuccess() 74 | { 75 | Toast.makeText(this, "GRANT ACCESS SDCARD!", Toast.LENGTH_SHORT).show(); 76 | } 77 | 78 | @PermissionDenied(REQUECT_CODE_SDCARD) 79 | public void requestSdcardFailed() 80 | { 81 | Toast.makeText(this, "DENY ACCESS SDCARD!", Toast.LENGTH_SHORT).show(); 82 | } 83 | 84 | 85 | @PermissionGrant(REQUECT_CODE_CALL_PHONE) 86 | public void requestCallPhoneSuccess() 87 | { 88 | Toast.makeText(this, "GRANT ACCESS SDCARD!", Toast.LENGTH_SHORT).show(); 89 | } 90 | 91 | @PermissionDenied(REQUECT_CODE_CALL_PHONE) 92 | public void requestCallPhoneFailed() 93 | { 94 | Toast.makeText(this, "DENY ACCESS SDCARD!", Toast.LENGTH_SHORT).show(); 95 | } 96 | 97 | 98 | } 99 | -------------------------------------------------------------------------------- /permission-sample/src/main/java/com/zhy/permission_sample/TestFragment.java: -------------------------------------------------------------------------------- 1 | package com.zhy.permission_sample; 2 | 3 | import android.Manifest; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v4.app.Fragment; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.Toast; 11 | 12 | import com.zhy.m.permission.MPermissions; 13 | import com.zhy.m.permission.PermissionDenied; 14 | import com.zhy.m.permission.PermissionGrant; 15 | 16 | /** 17 | * Created by zhy on 16/2/21. 18 | */ 19 | public class TestFragment extends Fragment 20 | { 21 | @Nullable 22 | @Override 23 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 24 | { 25 | return inflater.inflate(R.layout.fragment_test, container, false); 26 | } 27 | 28 | @Override 29 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) 30 | { 31 | view.findViewById(R.id.id_btn_contact).setOnClickListener(new View.OnClickListener() 32 | { 33 | @Override 34 | public void onClick(View v) 35 | { 36 | MPermissions.requestPermissions(TestFragment.this, 4, Manifest.permission.WRITE_CONTACTS); 37 | } 38 | }); 39 | 40 | } 41 | 42 | @PermissionGrant(4) 43 | public void requestContactSuccess() 44 | { 45 | Toast.makeText(getActivity(), "GRANT ACCESS CONTACTS!", Toast.LENGTH_SHORT).show(); 46 | } 47 | 48 | @PermissionDenied(4) 49 | public void requestContactFailed() 50 | { 51 | Toast.makeText(getActivity(), "DENY ACCESS CONTACTS!", Toast.LENGTH_SHORT).show(); 52 | } 53 | 54 | @Override 55 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 56 | { 57 | MPermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults); 58 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /permission-sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 |