feat(settings): Add microphone permission prompt

This commit is contained in:
alexytomi 2025-09-02 00:27:41 +08:00
parent 126884cf49
commit 53b4310804
4 changed files with 37 additions and 0 deletions

View File

@ -166,7 +166,9 @@ public class LauncherActivity extends BaseActivity {
};
private ActivityResultLauncher<String> mRequestNotificationPermissionLauncher;
private ActivityResultLauncher<String> mRequestMicrophonePermissionLauncher;
private WeakReference<Runnable> mRequestNotificationPermissionRunnable;
private WeakReference<Runnable> mRequestMicrophonePermissionRunnable;
@Override
protected boolean shouldIgnoreNotch() {
@ -206,6 +208,16 @@ public class LauncherActivity extends BaseActivity {
}
}
);
mRequestMicrophonePermissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
isAllowed -> {
if(!isAllowed) handleNoNotificationPermission();
else {
Runnable runnable = Tools.getWeakReference(mRequestMicrophonePermissionRunnable);
if(runnable != null) runnable.run();
}
}
);
getWindow().setBackgroundDrawable(null);
bindViews();
checkNotificationPermission();
@ -343,6 +355,11 @@ public class LauncherActivity extends BaseActivity {
this,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_DENIED;
}
public boolean checkForMicrophonePermission() {
return ContextCompat.checkSelfPermission(
this,
Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_DENIED;
}
public void askForNotificationPermission(Runnable onSuccessRunnable) {
if(Build.VERSION.SDK_INT < 33) return;
@ -352,6 +369,13 @@ public class LauncherActivity extends BaseActivity {
mRequestNotificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
}
public void askForMicrophonePermission(Runnable onSuccessRunnable) {
if(onSuccessRunnable != null) {
mRequestMicrophonePermissionRunnable = new WeakReference<>(onSuccessRunnable);
}
mRequestMicrophonePermissionLauncher.launch(Manifest.permission.RECORD_AUDIO);
}
/** Stuff all the view boilerplate here */
private void bindViews(){
mFragmentView = findViewById(R.id.container_fragment);

View File

@ -35,6 +35,7 @@ public class LauncherPreferenceFragment extends PreferenceFragmentCompat impleme
private void setupNotificationRequestPreference() {
Preference mRequestNotificationPermissionPreference = requirePreference("notification_permission_request");
Preference mMicrophonePermissionPreference = requirePreference("microphone_permission_request");
Activity activity = getActivity();
if(activity instanceof LauncherActivity) {
LauncherActivity launcherActivity = (LauncherActivity)activity;
@ -43,6 +44,11 @@ public class LauncherPreferenceFragment extends PreferenceFragmentCompat impleme
launcherActivity.askForNotificationPermission(()->mRequestNotificationPermissionPreference.setVisible(false));
return true;
});
mMicrophonePermissionPreference.setVisible(!launcherActivity.checkForMicrophonePermission());
mMicrophonePermissionPreference.setOnPreferenceClickListener(preference -> {
launcherActivity.askForMicrophonePermission(()->mMicrophonePermissionPreference.setVisible(false));
return true;
});
}else{
mRequestNotificationPermissionPreference.setVisible(false);
}

View File

@ -472,4 +472,6 @@
<string name="preference_touchcontroller_vibrate_length_title">TouchController vibrate length</string>
<string name="preference_touchcontroller_vibrate_length_description">Set the length of the vibration when using TouchController.</string>
<string name="error_vanilla_json_corrupt">Vanilla json seems to be corrupt. Please send a bug report on github or discord.</string>
<string name="preference_ask_for_microphone_title">Allow microphone</string>
<string name="preference_ask_for_microphone_description">Click to re-request the notification permission, required for voice chat mods like Simple Voice Chat.</string>
</resources>

View File

@ -66,6 +66,11 @@
android:summary="@string/preference_ask_for_notification_description"
android:key="notification_permission_request"
/>
<Preference
android:title="@string/preference_ask_for_microphone_title"
android:summary="@string/preference_ask_for_microphone_description"
android:key="microphone_permission_request"
/>