Skip to content

Commit

Permalink
解决GPermission的内存泄漏问题
Browse files Browse the repository at this point in the history
  • Loading branch information
王志昱 committed Nov 12, 2019
1 parent e11fed8 commit dc54b2a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.CAMERA" />

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/test/itgungnir/permission/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test.itgungnir.permission

import android.app.Application
import com.squareup.leakcanary.LeakCanary

/**
* Description:
*
* Created by ITGungnir on 2019-11-12
*/
class App : Application() {

override fun onCreate() {
super.onCreate()
if (LeakCanary.isInAnalyzerProcess(this)) {
return
}
LeakCanary.install(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

grantResult.text = when (GPermission.allGranted(
grantResult.text = when (GPermission.with(this).allGranted(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE
)) {
Expand Down
17 changes: 8 additions & 9 deletions permission/src/main/java/my/itgungnir/permission/GPermission.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import androidx.lifecycle.ViewModelStoreOwner

class GPermission private constructor() {

private lateinit var context: FragmentActivity

private lateinit var permissionUtil: GPermissionProxy

private var showDialogAtPermissionRejection: Boolean = false
private var grantedCallback: (() -> Unit)? = null
private var deniedCallback: (() -> Unit)? = null

companion object {

private lateinit var context: FragmentActivity

fun with(component: ViewModelStoreOwner) = GPermission().apply {
permissionUtil = GPermissionProxy.with(component)
context = when (component) {
Expand All @@ -33,12 +32,6 @@ class GPermission private constructor() {
throw IllegalArgumentException("GPermission requested from wrong component.")
}
}

fun allGranted(vararg permissions: String) = permissions.all { granted(it) }

private fun granted(permission: String): Boolean =
ContextCompat.checkSelfPermission(context.applicationContext, permission) ==
PackageManager.PERMISSION_GRANTED
}

/**
Expand Down Expand Up @@ -73,11 +66,17 @@ class GPermission private constructor() {
}
}

private fun granted(permission: String): Boolean =
ContextCompat.checkSelfPermission(context.applicationContext, permission) ==
PackageManager.PERMISSION_GRANTED

private fun lackedOnes(vararg permissions: Pair<String, String>): List<String> {
return permissions.filter { !granted(permission = it.first) }
.map { it.second }
}

fun allGranted(vararg permissions: String) = permissions.all { granted(it) }

/**
* Repeatedly pop the permission-request dialogs.
* When the user denied a permission without checking the "Don't show again" checkbox,
Expand Down

0 comments on commit dc54b2a

Please sign in to comment.