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

Commit eb728517 authored by Yuri Lin's avatar Yuri Lin
Browse files

Add "delete mode" option on mode configuration page

Flag: android.app.modes_ui
Bug: 346575126
Test: manual
Change-Id: I0d085eb54ef5fa07f312bdddf0219855bf1c2ef4
parent 3b62c233
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -7958,6 +7958,12 @@
    <!-- Do not disturb: Delete text button presented in a dialog to confirm the user would like to delete the selected DND rules. [CHAR LIMIT=30] -->
    <string name="zen_mode_schedule_delete">Delete</string>
    <!-- Do not disturb: Menu option for deleting a mode on its configuration page [CHAR LIMIT=40] -->
    <string name="zen_mode_menu_delete_mode">Delete mode</string>
    <!-- Do not disturb: Confirmation dialog asking the user whether they would like to delete the named mode [CHAR LIMIT: 40] -->
    <string name="zen_mode_delete_mode_confirmation">Delete \"<xliff:g id="mode" example="My Schedule">%1$s</xliff:g>\" mode?</string>
    <!--  Do not disturb: Edit label for button that allows user to edit the dnd schedule name. [CHAR LIMIT=30] -->
    <string name="zen_mode_rule_name_edit">Edit</string>
+46 −0
Original line number Diff line number Diff line
@@ -16,10 +16,14 @@

package com.android.settings.notification.modes;

import android.app.AlertDialog;
import android.app.Application;
import android.app.AutomaticZenRule;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.android.settings.R;
import com.android.settingslib.applications.ApplicationsState;
@@ -30,6 +34,9 @@ import java.util.List;

public class ZenModeFragment extends ZenModeFragmentBase {

    // for mode deletion menu
    private static final int DELETE_MODE = 1;

    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.modes_rule_settings;
@@ -76,4 +83,43 @@ public class ZenModeFragment extends ZenModeFragmentBase {
        // TODO: b/332937635 - make this the correct metrics category
        return SettingsEnums.NOTIFICATION_ZEN_MODE_AUTOMATION;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.add(Menu.NONE, DELETE_MODE, Menu.NONE, R.string.zen_mode_menu_delete_mode);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    protected boolean onOptionsItemSelected(MenuItem item, ZenMode zenMode) {
        switch (item.getItemId()) {
            case DELETE_MODE:
                new AlertDialog.Builder(mContext)
                        .setTitle(mContext.getString(R.string.zen_mode_delete_mode_confirmation,
                                zenMode.getRule().getName()))
                        .setPositiveButton(R.string.zen_mode_schedule_delete,
                                (dialog, which) -> {
                                    // start finishing before calling removeMode() so that we don't
                                    // try to update this activity with a nonexistent mode when the
                                    // zen mode config is updated
                                    finish();
                                    mBackend.removeMode(zenMode);
                                })
                        .setNegativeButton(R.string.cancel, null)
                        .show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void updateZenModeState() {
        // Because this fragment may be asked to finish by the delete menu but not be done doing
        // so yet, ignore any attempts to update info in that case.
        if (getActivity() != null && getActivity().isFinishing()) {
            return;
        }
        super.updateZenModeState();
    }
}
+13 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.app.AutomaticZenRule;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
@@ -108,6 +109,18 @@ abstract class ZenModeFragmentBase extends ZenModesFragmentBase {
        updateControllers();
    }

    @Override
    public final boolean onOptionsItemSelected(MenuItem item) {
        if (mZenMode != null) {
            return onOptionsItemSelected(item, mZenMode);
        }
        return super.onOptionsItemSelected(item);
    }

    protected boolean onOptionsItemSelected(MenuItem item, @NonNull ZenMode zenMode) {
        return true;
    }

    private void updateControllers() {
        if (getPreferenceControllers() == null || mZenMode == null) {
            return;