Skip to content

Commit

Permalink
fix: Adjust push permission request to use alertDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushTNM committed Sep 3, 2023
1 parent 0437155 commit 5e4e44f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 26 deletions.
2 changes: 2 additions & 0 deletions play-services-core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

<uses-permission android:name="android.permission.FAKE_PACKAGE_SIGNATURE" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Build;
import android.os.ResultReceiver;
import android.text.Html;
import android.text.Spannable;
Expand All @@ -15,8 +16,10 @@
import android.text.SpannedString;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

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

import com.google.android.gms.R;
Expand Down Expand Up @@ -60,8 +63,7 @@ public void onCreate(Bundle savedInstanceState) {
return;
}

setContentView(R.layout.ask_gcm);

// Create and show the AlertDialog
try {
PackageManager pm = getPackageManager();
final ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
Expand All @@ -70,31 +72,34 @@ public void onCreate(Bundle savedInstanceState) {
SpannableString s = new SpannableString(raw);
s.setSpan(new StyleSpan(Typeface.BOLD), raw.indexOf(label), raw.indexOf(label) + label.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

((TextView) findViewById(R.id.permission_message)).setText(s);
findViewById(R.id.permission_allow_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answered) return;
database.noteAppKnown(packageName, true);
answered = true;
Bundle bundle = new Bundle();
bundle.putBoolean(EXTRA_EXPLICIT, true);
resultReceiver.send(Activity.RESULT_OK, bundle);
finish();
}
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setCancelable(false); // Disable canceling the dialog by tapping outside or pressing the back button
alertDialogBuilder.setMessage(s);
alertDialogBuilder.setPositiveButton(R.string.allow, (dialog, which) -> {
if (answered) return;
database.noteAppKnown(packageName, true);
answered = true;
Bundle bundle = new Bundle();
bundle.putBoolean(EXTRA_EXPLICIT, true);
resultReceiver.send(Activity.RESULT_OK, bundle);
finish();
});
findViewById(R.id.permission_deny_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answered) return;
database.noteAppKnown(packageName, false);
answered = true;
Bundle bundle = new Bundle();
bundle.putBoolean(EXTRA_EXPLICIT, true);
resultReceiver.send(Activity.RESULT_CANCELED, bundle);
finish();
}
alertDialogBuilder.setNegativeButton(R.string.deny, (dialog, which) -> {
if (answered) return;
database.noteAppKnown(packageName, false);
answered = true;
Bundle bundle = new Bundle();
bundle.putBoolean(EXTRA_EXPLICIT, true);
resultReceiver.send(Activity.RESULT_CANCELED, bundle);
finish();
});
AlertDialog alertDialog = alertDialogBuilder.create();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
} else {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
alertDialog.show();
} catch (PackageManager.NameNotFoundException e) {
finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
package org.microg.gms.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import androidx.lifecycle.lifecycleScope
import androidx.preference.ListPreference
import androidx.preference.Preference
Expand All @@ -25,6 +29,8 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() {
addPreferencesFromResource(R.xml.preferences_gcm_advanced)
}

private val OVERLAY_REQ_CODE = 123

@SuppressLint("RestrictedApi")
override fun onBindPreferences() {
confirmNewApps = preferenceScreen.findPreference(GcmPrefs.PREF_CONFIRM_NEW_APPS) ?: confirmNewApps
Expand All @@ -37,7 +43,25 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() {
val appContext = requireContext().applicationContext
lifecycleScope.launchWhenResumed {
if (newValue is Boolean) {
setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(confirmNewApps = newValue))
var confirmNewAppsBool = newValue
if (Build.VERSION.SDK_INT >= 23) {
if (confirmNewAppsBool) {
if (!Settings.canDrawOverlays(appContext)) { // Android M Or Over
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + appContext.packageName)
)
startActivityForResult(intent, OVERLAY_REQ_CODE)
}
if (!Settings.canDrawOverlays(appContext)) {
confirmNewAppsBool = false
}
}
}
setGcmServiceConfiguration(
appContext,
getGcmServiceInfo(appContext).configuration.copy(confirmNewApps = confirmNewAppsBool)
)
}
updateContent()
}
Expand Down Expand Up @@ -85,9 +109,46 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() {
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Ensure confirm new apps toggle does not get turned off after allowing overlay permission
if (Build.VERSION.SDK_INT >= 23 && requestCode == OVERLAY_REQ_CODE) {
val appContext = requireContext().applicationContext
// Check if the permission was granted
if (Settings.canDrawOverlays(appContext)) {
lifecycleScope.launchWhenResumed {
setGcmServiceConfiguration(
appContext,
getGcmServiceInfo(appContext).configuration.copy(confirmNewApps = true)
)
updateContent()
}
}
}
}

override fun onResume() {
super.onResume()
updateContent()
updateUIBasedOnOverlayPermissionStatus()
}
private fun updateUIBasedOnOverlayPermissionStatus() {
if (Build.VERSION.SDK_INT >= 23) {
val appContext = requireContext().applicationContext
val isOverlayEnabled = Settings.canDrawOverlays(appContext)

// turn off confirm new apps toggle if overlay permission was turned off
if (!isOverlayEnabled) {
lifecycleScope.launchWhenResumed {
setGcmServiceConfiguration(
appContext,
getGcmServiceInfo(appContext).configuration.copy(confirmNewApps = false)
)
updateContent()
}

}
}
}

private fun updateContent() {
Expand Down

0 comments on commit 5e4e44f

Please sign in to comment.