-
Notifications
You must be signed in to change notification settings - Fork 1
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
wangzhiyu1
committed
Jun 26, 2019
1 parent
83883b8
commit 2a90d29
Showing
14 changed files
with
297 additions
and
89 deletions.
There are no files selected for viewing
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
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
10 changes: 10 additions & 0 deletions
10
app/src/main/java/test/itgungnir/permission/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 |
---|---|---|
@@ -1,12 +1,22 @@ | ||
package test.itgungnir.permission | ||
|
||
import android.Manifest | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import kotlinx.android.synthetic.main.activity_main.* | ||
import my.itgungnir.permission.GPermission | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
grantResult.text = GPermission.with(this).allGranted( | ||
Manifest.permission.WRITE_EXTERNAL_STORAGE, | ||
Manifest.permission.READ_PHONE_STATE, | ||
Manifest.permission.CAMERA, | ||
Manifest.permission.RECORD_AUDIO | ||
).toString() | ||
} | ||
} |
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,17 +1,14 @@ | ||
<?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" | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
android:layout_height="match_parent" | ||
android:gravity="center" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/grantResult" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Hello World!" | ||
android:textColor="@android:color/black" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
android:textColor="@android:color/black" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</LinearLayout> |
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
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
69 changes: 69 additions & 0 deletions
69
permission/src/main/java/my/itgungnir/permission/GPermissionFragment.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,69 @@ | ||
package my.itgungnir.permission | ||
|
||
import android.annotation.TargetApi | ||
import android.content.pm.PackageManager | ||
import android.os.Build | ||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentManager | ||
import io.reactivex.subjects.PublishSubject | ||
|
||
class GPermissionFragment private constructor() : Fragment() { | ||
|
||
private val subjects = mutableMapOf<String, PublishSubject<Permission>>() | ||
|
||
companion object { | ||
const val PERMISSION_REQUEST_CODE = 0x1F | ||
|
||
fun getInstance(manager: FragmentManager): GPermissionFragment = | ||
(manager.findFragmentByTag(GPermissionFragment::class.java.name) ?: GPermissionFragment().apply { | ||
manager.beginTransaction().add(this, GPermissionFragment::class.java.name).commitNow() | ||
}) as GPermissionFragment | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
retainInstance = true | ||
} | ||
|
||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | ||
if (PERMISSION_REQUEST_CODE != requestCode) { | ||
return | ||
} | ||
for (i in 0 until permissions.size) { | ||
val subject = subjects[permissions[i]] ?: return | ||
subjects.remove(permissions[i]) | ||
val granted = grantResults[i] == PackageManager.PERMISSION_GRANTED | ||
subject.onNext( | ||
Permission( | ||
name = permissions[i], | ||
granted = granted, | ||
shouldShowRequestPermissionRationale = shouldShowRequestPermissionRationale(permissions[i]) | ||
) | ||
) | ||
subject.onComplete() | ||
} | ||
} | ||
|
||
fun requestPermissions(permissions: Array<String>) = | ||
requestPermissions(permissions, PERMISSION_REQUEST_CODE) | ||
|
||
fun getPermissionSubject(permission: String) = | ||
subjects[permission] | ||
|
||
fun setPermissionSubject(permission: String, subject: PublishSubject<Permission>) { | ||
subjects[permission] = subject | ||
} | ||
|
||
fun isPermissionSubjectExist(permission: String) = | ||
subjects.containsKey(permission) | ||
|
||
@TargetApi(Build.VERSION_CODES.M) | ||
fun isPermissionGranted(permission: String): Boolean = | ||
activity?.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED | ||
|
||
@TargetApi(Build.VERSION_CODES.M) | ||
fun isPermissionRevoked(permission: String): Boolean = | ||
activity?.packageManager?.isPermissionRevokedByPolicy(permission, activity!!.packageName) ?: false | ||
} |
Oops, something went wrong.