Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ee414b4c authored by Matías Hernández's avatar Matías Hernández
Browse files

Extract the not-strictly-modes-related parts of ZenModesBackend

So that when it's moved to SettingsLib, it doesn't need to carry that baggage.

Bug: 346519570
Test: atest com.android.settings.notification.modes
Flag: android.app.modes_ui
Change-Id: I7911a521d96f5dbac2c2395171d324b7b54b8b07
parent a3894e67
Loading
Loading
Loading
Loading
+0 −13
Original line number Original line Diff line number Diff line
@@ -357,19 +357,6 @@ public class NotificationBackend {
        }
        }
    }
    }


    /**
     * Returns all of a user's packages that have at least one channel that will bypass DND
     */
    public List<String> getPackagesBypassingDnd(int userId,
            boolean includeConversationChannels) {
        try {
            return sINM.getPackagesBypassingDnd(userId, includeConversationChannels);
        } catch (Exception e) {
            Log.w(TAG, "Error calling NoMan", e);
            return new ArrayList<>();
        }
    }

    public void updateChannel(String pkg, int uid, NotificationChannel channel) {
    public void updateChannel(String pkg, int uid, NotificationChannel channel) {
        try {
        try {
            sINM.updateNotificationChannelForPackage(pkg, uid, channel);
            sINM.updateNotificationChannelForPackage(pkg, uid, channel);
+119 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.notification.modes;

import android.annotation.Nullable;
import android.app.INotificationManager;
import android.content.Context;
import android.content.pm.ParceledListSlice;
import android.database.Cursor;
import android.os.ServiceManager;
import android.provider.ContactsContract;
import android.service.notification.ConversationChannelWrapper;
import android.util.Log;

import androidx.annotation.VisibleForTesting;

import com.android.settings.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Class used for Settings-system_server interactions that are not <em>directly</em> related to
 * Mode management, but still used in the UI of its Settings pages (such as listing priority
 * conversations, contacts, etc).
 */
class ZenHelperBackend {

    private static final String TAG = "ZenHelperBackend";

    @Nullable // Until first usage
    private static ZenHelperBackend sInstance;

    private final Context mContext;
    private final INotificationManager mInm;

    static ZenHelperBackend getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new ZenHelperBackend(context.getApplicationContext());
        }
        return sInstance;
    }

    ZenHelperBackend(Context context) {
        mContext = context;
        mInm = INotificationManager.Stub.asInterface(
                ServiceManager.getService(Context.NOTIFICATION_SERVICE));
    }

    /**
     * Returns all of a user's packages that have at least one channel that will bypass DND
     */
    List<String> getPackagesBypassingDnd(int userId,
            boolean includeConversationChannels) {
        try {
            return mInm.getPackagesBypassingDnd(userId, includeConversationChannels);
        } catch (Exception e) {
            Log.w(TAG, "Error calling NoMan", e);
            return new ArrayList<>();
        }
    }

    @SuppressWarnings("unchecked")
    ParceledListSlice<ConversationChannelWrapper> getConversations(boolean onlyImportant) {
        try {
            return mInm.getConversations(onlyImportant);
        } catch (Exception e) {
            Log.w(TAG, "Error calling NoMan", e);
            return ParceledListSlice.emptyList();
        }
    }

    List<String> getStarredContacts() {
        try (Cursor cursor = queryStarredContactsData()) {
            return getStarredContacts(cursor);
        }
    }

    @VisibleForTesting
    List<String> getStarredContacts(Cursor cursor) {
        List<String> starredContacts = new ArrayList<>();
        if (cursor != null && cursor.moveToFirst()) {
            do {
                String contact = cursor.getString(0);
                starredContacts.add(contact != null ? contact :
                        mContext.getString(R.string.zen_mode_starred_contacts_empty_name));

            } while (cursor.moveToNext());
        }
        return starredContacts;
    }

    private Cursor queryStarredContactsData() {
        return mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                new String[]{ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
                ContactsContract.Data.STARRED + "=1", null,
                ContactsContract.Data.TIMES_CONTACTED);
    }

    Cursor queryAllContactsData() {
        return mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                new String[]{ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
                null, null, null);
    }
}
+6 −5
Original line number Original line Diff line number Diff line
@@ -29,7 +29,6 @@ import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.Preference;


import com.android.settings.core.SubSettingLauncher;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.notification.NotificationBackend;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.applications.ApplicationsState;


import java.util.ArrayList;
import java.util.ArrayList;
@@ -47,14 +46,16 @@ class ZenModeAppsLinkPreferenceController extends AbstractZenModePreferenceContr


    private final ZenModeSummaryHelper mSummaryHelper;
    private final ZenModeSummaryHelper mSummaryHelper;
    private ApplicationsState.Session mAppSession;
    private ApplicationsState.Session mAppSession;
    private NotificationBackend mNotificationBackend = new NotificationBackend();
    private final ZenHelperBackend mHelperBackend;
    private ZenMode mZenMode;
    private ZenMode mZenMode;
    private Preference mPreference;
    private Preference mPreference;


    ZenModeAppsLinkPreferenceController(Context context, String key, Fragment host,
    ZenModeAppsLinkPreferenceController(Context context, String key, Fragment host,
            ApplicationsState applicationsState, ZenModesBackend backend) {
            ApplicationsState applicationsState, ZenModesBackend backend,
            ZenHelperBackend helperBackend) {
        super(context, key, backend);
        super(context, key, backend);
        mSummaryHelper = new ZenModeSummaryHelper(mContext, mBackend);
        mSummaryHelper = new ZenModeSummaryHelper(mContext, helperBackend);
        mHelperBackend = helperBackend;
        if (applicationsState != null && host != null) {
        if (applicationsState != null && host != null) {
            mAppSession = applicationsState.newSession(mAppSessionCallbacks, host.getLifecycle());
            mAppSession = applicationsState.newSession(mAppSessionCallbacks, host.getLifecycle());
        }
        }
@@ -105,7 +106,7 @@ class ZenModeAppsLinkPreferenceController extends AbstractZenModePreferenceContr
                pkgLabelMap.put(entry.info.packageName, entry.label);
                pkgLabelMap.put(entry.info.packageName, entry.label);
            }
            }
        }
        }
        for (String pkg : mNotificationBackend.getPackagesBypassingDnd(mContext.getUserId(),
        for (String pkg : mHelperBackend.getPackagesBypassingDnd(mContext.getUserId(),
                /* includeConversationChannels= */ false)) {
                /* includeConversationChannels= */ false)) {
            // Settings may hide some packages from the user, so if they're not present here
            // Settings may hide some packages from the user, so if they're not present here
            // we skip displaying them, even if they bypass dnd.
            // we skip displaying them, even if they bypass dnd.
+2 −1
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.notification.modes;


import android.app.settings.SettingsEnums;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Context;

import com.android.settings.R;
import com.android.settings.R;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.AbstractPreferenceController;


@@ -34,7 +35,7 @@ public class ZenModeCallsFragment extends ZenModeFragmentBase {
    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
        List<AbstractPreferenceController> controllers = new ArrayList<>();
        List<AbstractPreferenceController> controllers = new ArrayList<>();
        controllers.add(new ZenModePrioritySendersPreferenceController(context,
        controllers.add(new ZenModePrioritySendersPreferenceController(context,
                "zen_mode_settings_category_calls", false, mBackend));
                "zen_mode_settings_category_calls", false, mBackend, mHelperBackend));
        controllers.add(new ZenModeRepeatCallersPreferenceController(context,
        controllers.add(new ZenModeRepeatCallersPreferenceController(context,
                "zen_mode_repeat_callers", mBackend,
                "zen_mode_repeat_callers", mBackend,
                context.getResources().getInteger(com.android.internal.R.integer
                context.getResources().getInteger(com.android.internal.R.integer
+2 −2
Original line number Original line Diff line number Diff line
@@ -31,9 +31,9 @@ class ZenModeCallsLinkPreferenceController extends AbstractZenModePreferenceCont
    private final ZenModeSummaryHelper mSummaryHelper;
    private final ZenModeSummaryHelper mSummaryHelper;


    public ZenModeCallsLinkPreferenceController(Context context, String key,
    public ZenModeCallsLinkPreferenceController(Context context, String key,
            ZenModesBackend backend) {
            ZenModesBackend backend, ZenHelperBackend helperBackend) {
        super(context, key, backend);
        super(context, key, backend);
        mSummaryHelper = new ZenModeSummaryHelper(context, backend);
        mSummaryHelper = new ZenModeSummaryHelper(context, helperBackend);
    }
    }


    @Override
    @Override
Loading