Skip to content

Commit

Permalink
Improve notification support
Browse files Browse the repository at this point in the history
  • Loading branch information
sarsamurmu committed Jul 2, 2024
1 parent 31bd571 commit 15610bf
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import androidx.appcompat.graphics.drawable.DrawerArrowDrawable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.core.view.ViewCompat;
Expand Down Expand Up @@ -161,6 +162,8 @@ public abstract class CandyBarMainActivity extends AppCompatActivity implements
private Handler mTimesVisitedHandler;
private Runnable mTimesVisitedRunnable;

private static final int NOTIFICATION_PERMISSION_CODE = 10;

@NonNull
public abstract ActivityConfiguration onInit();

Expand Down Expand Up @@ -279,8 +282,34 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
Preferences.get(this).setFirstRun(true);
}

final Runnable askNotificationPermission = () -> {
final Runnable showToast = () -> {
Toast.makeText(this, getResources().getString(R.string.permission_notification_denied_1), Toast.LENGTH_LONG).show();
Toast.makeText(this, getResources().getString(R.string.permission_notification_denied_2), Toast.LENGTH_LONG).show();
};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && CandyBarApplication.getConfiguration().isNotificationEnabled()) {
if (NotificationManagerCompat.from(this).areNotificationsEnabled()) {
CandyBarApplication.getConfiguration().getNotificationHandler().setMode(Preferences.get(this).isNotificationsEnabled());
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
int permissionState = ContextCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS);
if (permissionState != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(android.Manifest.permission.POST_NOTIFICATIONS)) {
showToast.run();
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.POST_NOTIFICATIONS}, NOTIFICATION_PERMISSION_CODE);
}
}
} else {
showToast.run();
}
}
}
};

final Runnable onNewVersion = () -> {
ChangelogFragment.showChangelog(mFragManager);
ChangelogFragment.showChangelog(mFragManager, askNotificationPermission);
File cache = getCacheDir();
FileHelper.clearDirectory(cache);
};
Expand Down Expand Up @@ -336,12 +365,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mTimesVisitedHandler.postDelayed(mTimesVisitedRunnable, getResources().getInteger(R.integer.in_app_review_visit_time) * 1000L);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && CandyBarApplication.getConfiguration().isNotificationEnabled()) {
int permissionState = ContextCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS);
if (permissionState == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.POST_NOTIFICATIONS}, 10);
}
}
askNotificationPermission.run();
}

@Override
Expand Down Expand Up @@ -442,6 +466,15 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
Toast.makeText(this, R.string.permission_storage_denied, Toast.LENGTH_LONG).show();
}
if (requestCode == NOTIFICATION_PERMISSION_CODE) {
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
CandyBarApplication.getConfiguration().getNotificationHandler().setMode(Preferences.get(this).isNotificationsEnabled());
} else {
Toast.makeText(this, getResources().getString(R.string.permission_notification_denied_1), Toast.LENGTH_LONG).show();
Toast.makeText(this, getResources().getString(R.string.permission_notification_denied_2), Toast.LENGTH_LONG).show();
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
int finalPosition = position - 1;

int color = ColorHelper.getAttributeColor(mContext, android.R.attr.textColorPrimary);
LogUtil.d("COLOR IS: " + Integer.toHexString(color));
if (mHomes.get(finalPosition).getIcon() != -1) {
if (mHomes.get(finalPosition).getType() == Home.Type.DIMENSION) {
if (CandyBarGlideModule.isValidContextForGlide(mContext)) {
Expand Down
19 changes: 16 additions & 3 deletions library/src/main/java/candybar/lib/adapters/SettingsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -15,6 +18,7 @@

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
Expand Down Expand Up @@ -148,7 +152,8 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}

if (setting.getType() == NOTIFICATIONS) {
contentViewHolder.materialSwitch.setChecked(Preferences.get(mContext).isNotificationsEnabled());
boolean isPermissionGranted = Build.VERSION.SDK_INT < Build.VERSION_CODES.O || NotificationManagerCompat.from(mContext).areNotificationsEnabled();
contentViewHolder.materialSwitch.setChecked(Preferences.get(mContext).isNotificationsEnabled() && isPermissionGranted);
int pad = contentViewHolder.container.getPaddingLeft();
contentViewHolder.container.setPadding(pad, pad, pad, 0);
}
Expand Down Expand Up @@ -197,14 +202,22 @@ private class ContentViewHolder extends RecyclerView.ViewHolder implements View.
}
break;
case NOTIFICATIONS:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !NotificationManagerCompat.from(mContext).areNotificationsEnabled()) {
materialSwitch.setChecked(false);
Intent settingsIntent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Settings.EXTRA_APP_PACKAGE, mContext.getPackageName());
mContext.startActivity(settingsIntent);
break;
}
if (isChecked != Preferences.get(mContext).isNotificationsEnabled()) {
Preferences.get(mContext).setNotificationsEnabled(isChecked);
// TODO: Method to do stuff
CandyBarApplication.Configuration.NotificationHandler handler = CandyBarApplication.getConfiguration().getNotificationHandler();
if (handler != null) {
handler.setMode(isChecked);
}
}
break;
}
});
}
Expand Down Expand Up @@ -390,7 +403,7 @@ public void onClick(View view) {
put("action", "open_dialog");
}}
);
ChangelogFragment.showChangelog(((AppCompatActivity) mContext).getSupportFragmentManager());
ChangelogFragment.showChangelog(((AppCompatActivity) mContext).getSupportFragmentManager(), () -> {});
break;
case RESET_TUTORIAL:
CandyBarApplication.getConfiguration().getAnalyticsHandler().logEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private void initSettings() {
"", "", Setting.Type.THEME));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
settings.add(new Setting(-1, "", "Material You", "", "", Setting.Type.MATERIAL_YOU));
settings.add(new Setting(-1, "", resources.getString(R.string.theme_name_material_you), "", "", Setting.Type.MATERIAL_YOU));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,26 @@
public class ChangelogFragment extends DialogFragment {

private static final String TAG = "candybar.dialog.changelog";
private Runnable onPositive;

private static ChangelogFragment newInstance() {
return new ChangelogFragment();
private static ChangelogFragment newInstance(Runnable onPositive) {
return new ChangelogFragment(onPositive);
}

public static void showChangelog(FragmentManager fm) {
ChangelogFragment(Runnable onPositive) {
super();
this.onPositive = onPositive;
}

public static void showChangelog(FragmentManager fm, Runnable onPositive) {
FragmentTransaction ft = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag(TAG);
if (prev != null) {
ft.remove(prev);
}

try {
DialogFragment dialog = ChangelogFragment.newInstance();
DialogFragment dialog = ChangelogFragment.newInstance(onPositive);
dialog.show(ft, TAG);
} catch (IllegalArgumentException | IllegalStateException ignored) {
}
Expand All @@ -68,6 +74,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
.typeface(TypefaceHelper.getMedium(requireActivity()), TypefaceHelper.getRegular(requireActivity()))
.customView(R.layout.fragment_changelog, false)
.positiveText(R.string.close)
.onPositive((d, which) -> this.onPositive.run())
.build();
dialog.show();

Expand Down
2 changes: 2 additions & 0 deletions library/src/main/res/values/dashboard_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@

<!-- Permission -->
<string name="permission_storage_denied">Storage permission needed to save wallpaper</string>
<string name="permission_notification_denied_1">Notification permission is needed to receive notifications from the developer</string>
<string name="permission_notification_denied_2">Enable notifications from the app settings</string>

<!-- Intent -->
<string name="app_client">Choose App</string>
Expand Down

0 comments on commit 15610bf

Please sign in to comment.