Skip to content

Commit

Permalink
Add Helper for some UnifiedPush functions
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Jan 14, 2025
1 parent 66824aa commit cc45584
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.joinmastodon.android.model.LegacyFilter;
import org.joinmastodon.android.model.Instance;
import org.joinmastodon.android.model.Token;
import org.joinmastodon.android.utils.UnifiedPushHelper;
import org.unifiedpush.android.connector.UnifiedPush;

import java.io.File;
Expand Down Expand Up @@ -127,7 +128,7 @@ public void addAccount(Instance instance, Token token, Account self, Application
MastodonAPIController.runInBackground(()->writeInstanceInfoFile(wrapper, instance.uri));

updateMoreInstanceInfo(instance, instance.uri);
if (UnifiedPush.getAckDistributor(context) != null) {
if (UnifiedPushHelper.isUnifiedPushEnabled(context)) {
UnifiedPush.register(
context,
session.getID(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.joinmastodon.android.ui.M3AlertDialogBuilder;
import org.joinmastodon.android.ui.utils.HideableSingleViewRecyclerAdapter;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.utils.UnifiedPushHelper;
import org.unifiedpush.android.connector.UnifiedPush;

import java.time.Instant;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment<Void>{

// MEGALODON
private boolean useUnifiedPush = false;
private boolean hasAnyUnifiedPushDistrib = false;
private CheckableListItem<Void> uniformIconItem, deleteItem, onlyLatestItem, unifiedPushItem;
private CheckableListItem<Void> postsItem, updateItem;

Expand All @@ -72,7 +74,8 @@ public void onCreate(Bundle savedInstanceState){
lp=AccountSessionManager.get(accountID).getLocalPreferences();

getPushSubscription();
useUnifiedPush=UnifiedPush.getAckDistributor(getContext()) != null;
useUnifiedPush=UnifiedPushHelper.isUnifiedPushEnabled(getContext());
hasAnyUnifiedPushDistrib=UnifiedPushHelper.hasAnyDistributorInstalled(getContext());

onDataLoaded(List.of(
pauseItem=new CheckableListItem<>(getString(R.string.pause_all_notifications), getPauseItemSubtitle(), CheckableListItem.Style.SWITCH, false, R.drawable.ic_fluent_alert_snooze_24_regular, i->onPauseNotificationsClick(false)),
Expand All @@ -94,7 +97,7 @@ public void onCreate(Bundle savedInstanceState){
));

//only enable when distributors, who can receive notifications, are available
unifiedPushItem.isEnabled=!UnifiedPush.getDistributors(getContext()).isEmpty();
unifiedPushItem.isEnabled=hasAnyUnifiedPushDistrib;
if (!unifiedPushItem.isEnabled) {
unifiedPushItem.subtitleRes=R.string.sk_settings_unifiedpush_no_distributor_body;
}
Expand Down Expand Up @@ -316,12 +319,12 @@ private void updateBanner(){
bannerText.setText(R.string.notifications_disabled_in_system);
bannerButton.setText(R.string.open_system_notification_settings);
bannerButton.setOnClickListener(v->openSystemNotificationSettings());
}else if(BuildConfig.BUILD_TYPE.equals("fdroidRelease") && UnifiedPush.getAckDistributor(getContext()) != null){
}else if(BuildConfig.BUILD_TYPE.equals("fdroidRelease") && useUnifiedPush){
bannerAdapter.setVisible(true);
bannerIcon.setImageResource(R.drawable.ic_fluent_warning_24_filled);
bannerTitle.setVisibility(View.VISIBLE);
bannerTitle.setText(R.string.mo_settings_unifiedpush_warning);
if(UnifiedPush.getDistributors(getContext()).isEmpty()) {
if(!hasAnyUnifiedPushDistrib) {
bannerText.setText(R.string.mo_settings_unifiedpush_warning_no_distributors);
bannerButton.setText(R.string.info);
bannerButton.setOnClickListener(v->UiUtils.launchWebBrowser(getContext(), "https://unifiedpush.org/"));
Expand All @@ -342,40 +345,26 @@ private void updateBanner(){
}

private void onUnifiedPushClick(){
if(UnifiedPush.getAckDistributor(getContext()) == null){
if(!useUnifiedPush){
List<String> distributors = UnifiedPush.getDistributors(getContext());
showUnifiedPushRegisterDialog(distributors);
return;
}

for (AccountSession accountSession : AccountSessionManager.getInstance().getLoggedInAccounts()) {
UnifiedPush.unregister(
getContext(),
accountSession.getID()
);

//re-register to fcm
accountSession.getPushSubscriptionManager().registerAccountForPush(getPushSubscription());
}
UnifiedPushHelper.unregisterAllAccounts(getContext());
unifiedPushItem.toggle();
rebindItem(unifiedPushItem);
useUnifiedPush = false;
}

private void showUnifiedPushRegisterDialog(List<String> distributors){
new M3AlertDialogBuilder(getContext()).setTitle(R.string.sk_settings_unifiedpush_choose).setItems(distributors.toArray(String[]::new),
(dialog, which)->{
String userDistrib = distributors.get(which);
UnifiedPush.saveDistributor(getContext(), userDistrib);
for (AccountSession accountSession : AccountSessionManager.getInstance().getLoggedInAccounts()){
UnifiedPush.register(
getContext(),
accountSession.getID(),
null,
null
);
}
UnifiedPushHelper.registerAllAccounts(getContext());
unifiedPushItem.toggle();
rebindItem(unifiedPushItem);
useUnifiedPush = true;
}).setOnCancelListener(d->rebindItem(unifiedPushItem)).show();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.joinmastodon.android.utils;

import android.content.Context;

import androidx.annotation.NonNull;

import org.joinmastodon.android.api.session.AccountSession;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.unifiedpush.android.connector.UnifiedPush;

public class UnifiedPushHelper {

/**
* @param context
* @return `true` if UnifiedPush is used
*/
public static boolean isUnifiedPushEnabled(@NonNull Context context) {
return UnifiedPush.getAckDistributor(context) != null;
}

/**
* If any distributor is installed on the device
* @param context
* @return `true` if at least one is installed
*/
public static boolean hasAnyDistributorInstalled(@NonNull Context context) {
return !UnifiedPush.getDistributors(context).isEmpty();
}

public static void registerAllAccounts(@NonNull Context context) {
for (AccountSession accountSession : AccountSessionManager.getInstance().getLoggedInAccounts()){
UnifiedPush.register(
context,
accountSession.getID(),
null,
null
);
}
}

public static void unregisterAllAccounts(@NonNull Context context) {
for (AccountSession accountSession : AccountSessionManager.getInstance().getLoggedInAccounts()){
UnifiedPush.unregister(
context,
accountSession.getID()
);
// use FCM again
accountSession.getPushSubscriptionManager().registerAccountForPush(null);
}
}
}

0 comments on commit cc45584

Please sign in to comment.