~singpolyma/cheogram-android

9253c1c21f5fe28992210b5c25ecf00c4afcbdd0 — Stephen Paul Weber 11 months ago ce49fa2
Per-account quiet hours
M src/main/java/eu/siacs/conversations/services/NotificationService.java => src/main/java/eu/siacs/conversations/services/NotificationService.java +19 -17
@@ 331,19 331,21 @@ public class NotificationService {
                "notifications_from_strangers", R.bool.notifications_from_strangers);
    }

    private boolean isQuietHours() {
    private boolean isQuietHours(Account account) {
        if (mXmppConnectionService.getAccounts().size() < 2) account = null;
        final String suffix = account == null ? "" : ":" + account.getUuid();
        if (!mXmppConnectionService.getBooleanPreference(
                "enable_quiet_hours", R.bool.enable_quiet_hours)) {
                "enable_quiet_hours" + suffix, R.bool.enable_quiet_hours)) {
            return false;
        }
        final SharedPreferences preferences =
                PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
        final long startTime =
                TimePreference.minutesToTimestamp(
                        preferences.getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE));
                        preferences.getLong("quiet_hours_start" + suffix, TimePreference.DEFAULT_VALUE));
        final long endTime =
                TimePreference.minutesToTimestamp(
                        preferences.getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE));
                        preferences.getLong("quiet_hours_end" + suffix, TimePreference.DEFAULT_VALUE));
        final long nowTime = Calendar.getInstance().getTimeInMillis();

        if (endTime < startTime) {


@@ 562,7 564,7 @@ public class NotificationService {
    }

    public synchronized void startRinging(final AbstractJingleConnection.Id id, final Set<Media> media) {
        if (isQuietHours()) return;
        if (isQuietHours(id.getContact().getAccount())) return;

        if (tryRingingWithDialerUI(id, media)) {
            return;


@@ 912,8 914,6 @@ public class NotificationService {
        final SharedPreferences preferences =
                PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);

        final boolean quiteHours = isQuietHours();

        final boolean notifyOnlyOneChild =
                notify
                        && conversations != null


@@ 930,27 930,29 @@ public class NotificationService {
            final Builder mBuilder;
            if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                ArrayList<Message> messages = notifications.values().iterator().next();
                mBuilder = buildSingleConversations(messages, notify, quiteHours);
                modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences, messages.isEmpty() ? null : messages.get(0).getConversation().getAccount());
                final Account account = messages.isEmpty() ? null : messages.get(0).getConversation().getAccount();
                mBuilder = buildSingleConversations(messages, notify, isQuietHours(account));
                modifyForSoundVibrationAndLight(mBuilder, notify, preferences, account);
                notify(NOTIFICATION_ID, mBuilder.build());
            } else {
                mBuilder = buildMultipleConversation(notify, quiteHours);
                mBuilder = buildMultipleConversation(notify, isQuietHours(null));
                if (notifyOnlyOneChild) {
                    mBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
                }
                modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences, null);
                modifyForSoundVibrationAndLight(mBuilder, notify, preferences, null);
                if (!summaryOnly) {
                    for (Map.Entry<String, ArrayList<Message>> entry : notifications.entrySet()) {
                        String uuid = entry.getKey();
                        final boolean notifyThis =
                                notifyOnlyOneChild ? conversations.contains(uuid) : notify;
                        final Account account = entry.getValue().isEmpty() ? null : entry.getValue().get(0).getConversation().getAccount();
                        Builder singleBuilder =
                                buildSingleConversations(entry.getValue(), notifyThis, quiteHours);
                                buildSingleConversations(entry.getValue(), notifyThis, isQuietHours(account));
                        if (!notifyOnlyOneChild) {
                            singleBuilder.setGroupAlertBehavior(
                                    NotificationCompat.GROUP_ALERT_SUMMARY);
                        }
                        modifyForSoundVibrationAndLight(singleBuilder, notifyThis, quiteHours, preferences, entry.getValue().isEmpty() ? null : entry.getValue().get(0).getConversation().getAccount());
                        modifyForSoundVibrationAndLight(singleBuilder, notifyThis, preferences, account);
                        singleBuilder.setGroup(MESSAGES_GROUP);
                        notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build());
                    }


@@ 986,7 988,7 @@ public class NotificationService {
    }

    private void modifyForSoundVibrationAndLight(
            Builder mBuilder, boolean notify, boolean quietHours, SharedPreferences preferences, Account account) {
            Builder mBuilder, boolean notify, SharedPreferences preferences, Account account) {
        final Resources resources = mXmppConnectionService.getResources();
        final String ringtone =
                preferences.getString(


@@ 1000,7 1002,7 @@ public class NotificationService {
        final boolean headsup =
                preferences.getBoolean(
                        "notification_headsup", resources.getBoolean(R.bool.headsup_notifications));
        if (notify && !quietHours) {
        if (notify && !isQuietHours(account)) {
            if (vibrate) {
                final int dat = 70;
                final long[] pattern = {0, 3 * dat, dat, dat};


@@ 1113,7 1115,7 @@ public class NotificationService {
    private Builder buildMissedCall(
            final Conversational conversation, final MissedCallsInfo info, boolean publicVersion) {
        final Builder builder =
                new NotificationCompat.Builder(mXmppConnectionService, isQuietHours() ? "quiet_hours" : "missed_calls");
                new NotificationCompat.Builder(mXmppConnectionService, isQuietHours(conversation.getAccount()) ? "quiet_hours" : "missed_calls");
        final String title =
                (info.getNumberOfCalls() == 1)
                        ? mXmppConnectionService.getString(R.string.missed_call)


@@ 1167,7 1169,7 @@ public class NotificationService {
        if (led) {
            builder.setLights(LED_COLOR, 2000, 3000);
        }
        builder.setPriority(isQuietHours() ? NotificationCompat.PRIORITY_LOW : NotificationCompat.PRIORITY_HIGH);
        builder.setPriority(isQuietHours(account) ? NotificationCompat.PRIORITY_LOW : NotificationCompat.PRIORITY_HIGH);
        builder.setSound(null);
        setNotificationColor(builder, account);
    }

M src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java => src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +8 -0
@@ 646,6 646,12 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
        binding.accountColorBox.setOnClickListener((v) -> {
            showColorDialog();
        });
        binding.quietHoursBox.setOnClickListener((v) -> {
            Intent intent = new Intent(Intent.ACTION_VIEW, null, EditAccountActivity.this, SettingsActivity.class);
            intent.putExtra("page", "quiet_hours");
            intent.putExtra("suffix", ":" + mAccount.getUuid());
            startActivity(intent);
        });
    }

    private void onEditYourNameClicked(View view) {


@@ 1072,8 1078,10 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
        if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() > 1) {
            binding.accountColorBox.setVisibility(View.VISIBLE);
            binding.colorPreview.setBackgroundColor(mAccount.getColor(isDarkTheme()));
            binding.quietHoursBox.setVisibility(View.VISIBLE);
        } else {
            binding.accountColorBox.setVisibility(View.GONE);
            binding.quietHoursBox.setVisibility(View.GONE);
        }

        final boolean togglePassword = mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) || !mAccount.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY);

M src/main/java/eu/siacs/conversations/ui/SettingsActivity.java => src/main/java/eu/siacs/conversations/ui/SettingsActivity.java +5 -0
@@ 126,6 126,11 @@ public class SettingsActivity extends XmppActivity implements OnSharedPreference
            Preference pref = mSettingsFragment.findPreference("dialler_integration_incoming");
            if (cat != null && pref != null) cat.removePreference(pref);
        }
        if (xmppConnectionService.getAccounts().size() > 1) {
            PreferenceCategory cat = (PreferenceCategory) mSettingsFragment.findPreference("notification_category");
            Preference pref = mSettingsFragment.findPreference("quiet_hours");
            if (cat != null && pref != null) cat.removePreference(pref);
        }
        final Preference accountPreference =
                mSettingsFragment.findPreference(UnifiedPushDistributor.PREFERENCE_ACCOUNT);
        reconfigureUpAccountPreference(accountPreference);

M src/main/java/eu/siacs/conversations/ui/SettingsFragment.java => src/main/java/eu/siacs/conversations/ui/SettingsFragment.java +27 -0
@@ 9,6 9,8 @@ import android.preference.PreferenceScreen;
import android.text.TextUtils;
import android.widget.ListView;

import java.lang.reflect.Method;

import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.utils.Compatibility;


@@ 16,6 18,7 @@ import eu.siacs.conversations.utils.Compatibility;
public class SettingsFragment extends PreferenceFragment {

	private String page = null;
	private String suffix = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {


@@ 57,6 60,7 @@ public class SettingsFragment extends PreferenceFragment {
			if (Intent.ACTION_VIEW.equals(intent.getAction())) {
				if (intent.getExtras() != null) {
					this.page = intent.getExtras().getString("page");
					this.suffix = intent.getExtras().getString("suffix");
					if (wasEmpty) {
						openPreferenceScreen(page);
					}


@@ 71,7 75,30 @@ public class SettingsFragment extends PreferenceFragment {
			final PreferenceScreen preferenceScreen = (PreferenceScreen) pref;
			getActivity().setTitle(preferenceScreen.getTitle());
			preferenceScreen.setDependency("");
			if (this.suffix != null) {
				for (int i = 0; i < preferenceScreen.getPreferenceCount(); i++) {
					final Preference p = preferenceScreen.getPreference(i);
					if (!p.hasKey()) continue;
					p.setKey(p.getKey() + this.suffix);
					if (p.getDependency() != null && !"".equals(p.getDependency())) {
						p.setDependency(p.getDependency() + this.suffix);
					}
					reloadPref(p);
				}
			}
			setPreferenceScreen((PreferenceScreen) pref);
		}
	}

	static void reloadPref(final Preference pref) {
		Class iterClass = pref.getClass();
		while(iterClass != Object.class) {
			try {
				Method m = iterClass.getDeclaredMethod("onSetInitialValue", boolean.class, Object.class);
				m.setAccessible(true);
				m.invoke(pref, true, null);
			} catch (Exception e) { }
			iterClass = iterClass.getSuperclass();
		}
	}
}

M src/main/res/layout/activity_edit_account.xml => src/main/res/layout/activity_edit_account.xml +40 -0
@@ 582,6 582,46 @@
                        </RelativeLayout>

                        <RelativeLayout
                            android:id="@+id/quiet_hours_box"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_marginTop="12dp">

                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentLeft="true"
                                android:layout_centerVertical="true"
                                android:layout_toLeftOf="@+id/action_edit_quiet_hours"
                                android:orientation="vertical">

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="@string/title_pref_quiet_hours"
                                    android:textAppearance="@style/TextAppearance.Conversations.Body1"/>

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="@string/pref_quiet_hours_summary"
                                    android:textAppearance="@style/TextAppearance.Conversations.Caption"/>
                            </LinearLayout>

                            <ImageView
                                android:id="@+id/action_edit_quiet_hours"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentRight="true"
                                android:layout_centerVertical="true"
                                android:alpha="?attr/icon_alpha"
                                android:background="?attr/selectableItemBackgroundBorderless"
                                android:padding="@dimen/image_button_padding"
                                android:src="?attr/icon_edit_body"
                                android:visibility="visible"/>
                        </RelativeLayout>

                        <RelativeLayout
                            android:id="@+id/pgp_fingerprint_box"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"