Skip to content

Commit

Permalink
commit code
Browse files Browse the repository at this point in the history
  • Loading branch information
Wang99012 committed May 26, 2020
1 parent 7e949b7 commit a0938a1
Show file tree
Hide file tree
Showing 17 changed files with 292 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .idea/dictionaries/sunnyit.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ dependencies {
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.12'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation project(":library")
}
20 changes: 18 additions & 2 deletions app/src/main/AndroidManifest.xml
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 app/src/main/java/com/simplation/xpermission/MainActivity.kt
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)
}
}
20 changes: 20 additions & 0 deletions app/src/main/res/layout/activity_main.xml
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>
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ buildscript {

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

// 添加 maven 插件
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}

Expand Down
1 change: 1 addition & 0 deletions library/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
41 changes: 41 additions & 0 deletions library/build.gradle
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 added library/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions library/proguard-rules.pro
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
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)
}
}
2 changes: 2 additions & 0 deletions library/src/main/AndroidManifest.xml
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 library/src/main/java/com/simplation/library/InvisibleFragment.kt
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 library/src/main/java/com/simplation/library/XPermission.kt
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 library/src/test/java/com/simplation/library/ExampleUnitTest.kt
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)
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rootProject.name='XPermission'
include ':app'
include ':library'

0 comments on commit a0938a1

Please sign in to comment.