-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
17 changed files
with
292 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,11 +1,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.simplation.xpermission"> | ||
|
||
<uses-permission android:name="android.permission.CALL_PHONE" /> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.CAMERA" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" /> | ||
</manifest> | ||
android:theme="@style/AppTheme"> | ||
|
||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
</application> | ||
|
||
</manifest> |
41 changes: 41 additions & 0 deletions
41
app/src/main/java/com/simplation/xpermission/MainActivity.kt
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,41 @@ | ||
package com.simplation.xpermission | ||
|
||
import android.Manifest | ||
import android.annotation.SuppressLint | ||
import android.content.Intent | ||
import android.net.Uri | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import com.simplation.library.XPermission | ||
import kotlinx.android.synthetic.main.activity_main.* | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
makeCallBtn.setOnClickListener { | ||
XPermission.request( | ||
this, | ||
Manifest.permission.CALL_PHONE, | ||
Manifest.permission.CAMERA, | ||
Manifest.permission.WRITE_EXTERNAL_STORAGE | ||
) { allGranted, deniedList -> | ||
if (allGranted) { | ||
call() | ||
} else { | ||
Toast.makeText(this, "You defined $deniedList", Toast.LENGTH_SHORT).show() | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
private fun call() { | ||
val intent = Intent(Intent.ACTION_CALL) | ||
intent.data = Uri.parse("tel:10086") | ||
startActivity(intent) | ||
} | ||
} |
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,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".MainActivity"> | ||
|
||
<Button | ||
android:id="@+id/makeCallBtn" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="makeCallBtn" | ||
android:textAllCaps="false" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
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 @@ | ||
/build |
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,41 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'kotlin-android-extensions' | ||
|
||
// 添加下面两行 | ||
apply plugin: 'com.github.dcendents.android-maven' | ||
// 这里 LoveLifeEveryday 改为你的 github 账号名 | ||
group = 'com.github.Simplation' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
buildToolsVersion "29.0.3" | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles 'consumer-rules.pro' | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
implementation 'androidx.appcompat:appcompat:1.1.0' | ||
implementation 'androidx.core:core-ktx:1.2.0' | ||
testImplementation 'junit:junit:4.13' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
} |
Empty file.
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
library/src/androidTest/java/com/simplation/library/ExampleInstrumentedTest.kt
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,24 @@ | ||
package com.simplation.library | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.simplation.mylibrary.test", appContext.packageName) | ||
} | ||
} |
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,2 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.simplation.library" /> |
56 changes: 56 additions & 0 deletions
56
library/src/main/java/com/simplation/library/InvisibleFragment.kt
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,56 @@ | ||
package com.simplation.library | ||
|
||
import android.content.pm.PackageManager | ||
import androidx.fragment.app.Fragment | ||
|
||
/** | ||
* @作者: W ◕‿-。 Z | ||
* @日期: 2020/5/25 19:38 | ||
* @描述: 运行时权限的实现思路,有以下三种: | ||
* 1. 将运行时权限的操作封装到 BaseActivity 中 | ||
* 2. 提供一个透明的 Activity 来处理 | ||
* 3. 提供一个隐藏的 Fragment 来处理 (使用) | ||
* @更新: | ||
*/ | ||
|
||
typealias PermissionCallback = (Boolean, List<String>) -> Unit | ||
|
||
class InvisibleFragment : Fragment() { | ||
|
||
private var callback: PermissionCallback? = null | ||
|
||
fun requestNow(cb: PermissionCallback, vararg permission: String) { | ||
callback = cb | ||
|
||
requestPermissions(permission, 1) | ||
} | ||
|
||
/** | ||
* 请求返回结果 | ||
* @param requestCode Int 请求码 | ||
* @param permissions Array<String> 权限 | ||
* @param grantResults IntArray 请求结果 | ||
*/ | ||
override fun onRequestPermissionsResult( | ||
requestCode: Int, | ||
permissions: Array<out String>, | ||
grantResults: IntArray | ||
) { | ||
if (requestCode == 1) { | ||
val deniedList = ArrayList<String>() | ||
|
||
for ((index, result) in grantResults.withIndex()) { | ||
if (result != PackageManager.PERMISSION_GRANTED) { | ||
deniedList.add(permissions[index]) | ||
} | ||
} | ||
|
||
val allGranted = deniedList.isEmpty() | ||
|
||
// 对申请权限的结果进行回调 | ||
callback?.let { | ||
it(allGranted, deniedList) | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
library/src/main/java/com/simplation/library/XPermission.kt
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,33 @@ | ||
package com.simplation.library | ||
|
||
import androidx.fragment.app.FragmentActivity | ||
|
||
/** | ||
* @作者: W ◕‿-。 Z | ||
* @日期: 2020/5/25 19:50 | ||
* @描述: | ||
* @更新: | ||
*/ | ||
object XPermission { | ||
private const val TAG = "InvisibleFragment" | ||
|
||
fun request( | ||
activity: FragmentActivity, | ||
vararg permission: String, | ||
callback: PermissionCallback | ||
) { | ||
val fragmentManager = activity.supportFragmentManager | ||
val exitFragment = fragmentManager.findFragmentByTag(TAG) | ||
|
||
val fragment = if (exitFragment != null) { | ||
exitFragment as InvisibleFragment | ||
} else { | ||
val invisibleFragment = InvisibleFragment() | ||
fragmentManager.beginTransaction().add(invisibleFragment, TAG).commitNow() | ||
invisibleFragment | ||
} | ||
|
||
// 这里在 permission 前面加个星号的意思是:将数组转化为可变长度参数传递过去 | ||
fragment.requestNow(callback, *permission) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
library/src/test/java/com/simplation/library/ExampleUnitTest.kt
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,17 @@ | ||
package com.simplation.library | ||
|
||
import org.junit.Test | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Example local unit test, which will execute on the development machine (host). | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
class ExampleUnitTest { | ||
@Test | ||
fun addition_isCorrect() { | ||
assertEquals(4, 2 + 2) | ||
} | ||
} |
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,2 +1,3 @@ | ||
rootProject.name='XPermission' | ||
include ':app' | ||
include ':library' |