Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在不改动原有代码结构基础上,增加一点扩展,使其用任意可启动activity的context申请权限,不必强求activity继承自FragmentActivity。 #286

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
Expand All @@ -53,9 +57,9 @@
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"/>
<uses-permission android:name="android.permission.BODY_SENSORS_BACKGROUND"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" />
<uses-permission android:name="android.permission.BODY_SENSORS_BACKGROUND" />

<application
android:allowBackup="true"
Expand All @@ -64,20 +68,25 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- <activity android:name=".MainJavaActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<activity
android:name=".MainJavaActivity"
android:exported="true">
<!-- <intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>-->
<activity android:name=".MainActivity" android:exported="true">
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>-->
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.permissionx.app.ExampleComponentActivity" />
</application>

</manifest>
81 changes: 81 additions & 0 deletions app/src/main/java/com/permissionx/app/ExampleComponentActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.permissionx.app

import android.Manifest
import android.graphics.Color
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import com.permissionx.app.databinding.ActivityExampleComponentBinding
import com.permissionx.guolindev.PermissionX
import com.permissionx.guolindev.patch.PermissionDelegateHolder

class ExampleComponentActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val layout = ActivityExampleComponentBinding.inflate(layoutInflater)
layout.makeRequestBtn.setOnClickListener {

PermissionX.init(this) {
it.permissions(Manifest.permission.CAMERA)
.request { allGranted, grantedList, deniedList ->
if (allGranted) {
Toast.makeText(this, "All permissions are granted", Toast.LENGTH_SHORT)
.show()
} else {
Toast.makeText(
this,
"The following permissions are denied:$deniedList",
Toast.LENGTH_SHORT
).show()
}
}
}
// or directly use PermissionDelegateHolder
// requestPerms()
}
setContentView(layout.root)
}

fun requestPerms() {
PermissionDelegateHolder.delegate(this) {
PermissionX.init(it)
.permissions(
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.RECORD_AUDIO,
// Manifest.permission.READ_CALENDAR,
// Manifest.permission.READ_CALL_LOG,
// Manifest.permission.READ_CONTACTS,
// Manifest.permission.READ_PHONE_STATE,
// Manifest.permission.BODY_SENSORS,
// Manifest.permission.ACTIVITY_RECOGNITION,
// Manifest.permission.SEND_SMS,
// Manifest.permission.READ_EXTERNAL_STORAGE
)
.setDialogTintColor(Color.parseColor("#1972e8"), Color.parseColor("#8ab6f5"))
.onExplainRequestReason { scope, deniedList, beforeRequest ->
val message = "PermissionX needs following permissions to continue"
scope.showRequestReasonDialog(deniedList, message, "Allow", "Deny")
// val message = "Please allow the following permissions in settings"
// val dialog = CustomDialogFragment(message, deniedList)
// scope.showRequestReasonDialog(dialog)
}
.onForwardToSettings { scope, deniedList ->
val message = "Please allow following permissions in settings"
scope.showForwardToSettingsDialog(deniedList, message, "Allow", "Deny")
}
.request { allGranted, grantedList, deniedList ->
if (allGranted) {
Toast.makeText(it, "All permissions are granted", Toast.LENGTH_SHORT)
.show()
} else {
Toast.makeText(
it,
"The following permissions are denied:$deniedList",
Toast.LENGTH_SHORT
).show()
}
}
}
}
}
34 changes: 30 additions & 4 deletions app/src/main/java/com/permissionx/app/MainFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.permissionx.app

import android.Manifest
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -17,7 +18,11 @@ class MainFragment : Fragment() {

private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentMainBinding.inflate(inflater, container, false)
return binding.root
}
Expand All @@ -28,7 +33,7 @@ class MainFragment : Fragment() {
binding.makeRequestBtn.setOnClickListener {
PermissionX.init(this)
.permissions(
Manifest.permission.CAMERA,
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.RECORD_AUDIO,
// Manifest.permission.READ_CALENDAR,
Expand All @@ -54,12 +59,33 @@ class MainFragment : Fragment() {
}
.request { allGranted, grantedList, deniedList ->
if (allGranted) {
Toast.makeText(activity, "All permissions are granted", Toast.LENGTH_SHORT).show()
Toast.makeText(activity, "All permissions are granted", Toast.LENGTH_SHORT)
.show()
} else {
Toast.makeText(activity, "The following permissions are denied:$deniedList", Toast.LENGTH_SHORT).show()
Toast.makeText(
activity,
"The following permissions are denied:$deniedList",
Toast.LENGTH_SHORT
).show()
}
}
}
binding.makeRequestBtn2.setOnClickListener {
requireActivity().startActivity(
Intent(
requireActivity(),
ExampleComponentActivity::class.java
)
)
}
binding.makeRequestBtn3.setOnClickListener {
requireActivity().startActivity(
Intent(
requireActivity(),
MainJavaActivity::class.java
)
)
}
}

override fun onDestroyView() {
Expand Down
41 changes: 38 additions & 3 deletions app/src/main/java/com/permissionx/app/MainJavaActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import com.permissionx.app.databinding.ActivityMainJavaBinding;
import com.permissionx.guolindev.PermissionMediator;
import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.patch.PermissionDelegateHolder;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class MainJavaActivity extends AppCompatActivity {

Expand All @@ -16,11 +22,40 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainJavaBinding binding = ActivityMainJavaBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.makeRequestBtn.setOnClickListener(view -> PermissionX.init(MainJavaActivity.this)

binding.makeRequestBtn.setOnClickListener(view -> {
requestCameraPermission(MainJavaActivity.this);
});

binding.makeRequestBtn2.setOnClickListener(view -> {
PermissionX.init(this, permissionMediator -> {
permissionMediator
.permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.explainReasonBeforeRequest()
.request((allGranted, grantedList, deniedList) -> {
if (allGranted) {
Toast.makeText(MainJavaActivity.this, "All permissions are granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainJavaActivity.this, "The following permissions are denied:" + deniedList, Toast.LENGTH_SHORT).show();
}
});
return null;
});

// or directly use PermissionDelegateHolder
// PermissionDelegateHolder.delegate(this, fragmentActivity -> {
// requestCameraPermission(fragmentActivity);// use the activity argument, do not use MainJavaActivity.this
// return null;
// });
});
}

private void requestCameraPermission(FragmentActivity activity) {
PermissionX.init(activity)
.permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.explainReasonBeforeRequest()
.onExplainRequestReason((scope, deniedList, beforeRequest) -> {
// CustomDialog customDialog = new CustomDialog(MainJavaActivity.this, "PermissionX needs following permissions to continue", deniedList);
// CustomDialog customDialog = new CustomDialog(activity, "PermissionX needs following permissions to continue", deniedList);
// scope.showRequestReasonDialog(customDialog);
scope.showRequestReasonDialog(deniedList, "PermissionX needs following permissions to continue", "Allow");
})
Expand All @@ -33,6 +68,6 @@ protected void onCreate(Bundle savedInstanceState) {
} else {
Toast.makeText(MainJavaActivity.this, "The following permissions are denied:" + deniedList, Toast.LENGTH_SHORT).show();
}
}));
});
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_example_component.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">

<Button
android:id="@+id/makeRequestBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Request Permissions" />
</LinearLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main_java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?android:attr/colorBackground">

<Button
Expand All @@ -11,4 +12,11 @@
android:textAllCaps="false"
android:text="Make Request" />

<Button
android:id="@+id/makeRequestBtn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Make Request2 use PermissionDelegateHolder" />

</LinearLayout>
15 changes: 15 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?android:attr/colorBackground">

<Button
Expand All @@ -11,4 +12,18 @@
android:textAllCaps="false"
android:text="Make Request" />

<Button
android:id="@+id/makeRequestBtn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="open ComponentActivity" />

<Button
android:id="@+id/makeRequestBtn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="MainJavaActivity" />

</LinearLayout>
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
agp = "8.4.1"
agp = "8.5.1"
kotlin = "1.9.0"
coreKtx = "1.13.1"
junit = "4.13.2"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed May 29 21:48:04 CST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
5 changes: 5 additions & 0 deletions permissionx/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<activity android:name="com.permissionx.guolindev.patch.DelegateActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
/>
</application>
</manifest>
Loading