Skip to content

Commit

Permalink
Request permission to post notifications on Android 13 & above
Browse files Browse the repository at this point in the history
It will hopefully fixed crashes of Apply on boot

Signed-off-by: sunilpaulmathew <[email protected]>
  • Loading branch information
sunilpaulmathew committed Mar 27, 2023
1 parent ce85349 commit ae7fc68
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.smartpack.kernelmanager.activities;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
Expand All @@ -31,6 +32,7 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
Expand All @@ -41,6 +43,8 @@
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.checkbox.MaterialCheckBox;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.navigation.NavigationView;
import com.smartpack.kernelmanager.R;
import com.smartpack.kernelmanager.fragments.BaseFragment;
Expand Down Expand Up @@ -540,6 +544,32 @@ public NavigationFragment[] newArray(int size) {
public void onStart() {
super.onStart();

// Request permission to post notification on Android 13 & above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && Utils.isPermissionDenied(Manifest.permission.POST_NOTIFICATIONS, this) &&
!Prefs.getBoolean("permission_notification", false, this)) {
View checkBoxView = View.inflate(this, R.layout.rv_checkbox, null);
MaterialCheckBox checkBox = checkBoxView.findViewById(R.id.checkbox);
checkBox.setChecked(!Prefs.getBoolean("permission_notification", false, this));
checkBox.setText(getString(R.string.always_show));
checkBox.setOnCheckedChangeListener((buttonView, isChecked)
-> Prefs.saveBoolean("permission_notification", !isChecked, this)
);

new MaterialAlertDialogBuilder(this)
.setView(checkBoxView)
.setIcon(R.mipmap.ic_launcher)
.setTitle(R.string.warning)
.setMessage(getString(R.string.permission_notification_message))
.setCancelable(false)
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
})
.setPositiveButton(R.string.permission_request, (dialogInterface, i) -> Utils.requestPermission(
new String[] {
Manifest.permission.POST_NOTIFICATIONS
}, this)
).show();
}

// Initialize auto app update check
if (Common.isUpdateCheckEnabled()) {
new UpdateCheck().initialize(1, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.smartpack.kernelmanager.services.boot;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
Expand Down Expand Up @@ -66,6 +67,9 @@ public void onCreate() {
super.onCreate();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && Utils.isPermissionDenied(Manifest.permission.POST_NOTIFICATIONS, this)) {
return;
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;
import androidx.core.hardware.fingerprint.FingerprintManagerCompat;
import androidx.core.view.ViewCompat;
Expand Down Expand Up @@ -103,6 +104,14 @@ public static boolean isPackageInstalled(String packageName) {
return RootUtils.runAndGetOutput("pm list packages " + packageName).equals("package:" + packageName);
}

public static boolean isPermissionDenied(String permission, Context context) {
return (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED);
}

public static void requestPermission(String[] permissions, Activity activity) {
ActivityCompat.requestPermissions(activity, permissions, 0);
}

public static void initializeAppTheme(Context context) {
if (Prefs.getBoolean("darktheme", true, context)) {
AppCompatDelegate.setDefaultNightMode(
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,8 @@
<string name="disable_question">Disable %s?</string>
<string name="swipe_message">Sorry! You can\'t swipe out a %s.</string>
<string name="drag_message">Sorry! You can\'t drag a %s into different position.</string>
<string name="permission_notification_message">Inorder to work \'Apply on boot notifications\' on devices running android TIRAMISU and newer versions, it is necessary to have permission to post notifications granted by the user. If you like to have this feature working, please go ahead with requesting (and granting) the permission by clicking the button provided below.</string>
<string name="permission_request">Request Permission</string>

<!-- SmartPack -->
<string name="acquiring">Acquiring update info</string>
Expand Down

0 comments on commit ae7fc68

Please sign in to comment.