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

Commit ffb4535d authored by Jason Monk's avatar Jason Monk
Browse files

Add dnd conditional

Change-Id: I81eabf274bde05358c60bb63e1ed6d1bd447ce55
parent db4ed191
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -2673,5 +2673,13 @@
            </intent-filter>
       </receiver>

        <receiver
            android:name=".dashboard.conditional.DndCondition$Receiver"
            android:enabled="false">
            <intent-filter>
                 <action android:name="android.app.action.INTERRUPTION_FILTER_CHANGED_INTERNAL" />
            </intent-filter>
       </receiver>

    </application>
</manifest>
+26 −0
Original line number Diff line number Diff line
<!--
     Copyright (C) 2015 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="24dp"
    android:viewportHeight="48.0"
    android:viewportWidth="48.0"
    android:width="24dp" >

    <path
        android:fillColor="#FFFFFFFF"
        android:pathData="M24.0,4.0C12.95,4.0 4.0,12.95 4.0,24.0s8.95,20.0 20.0,20.0 20.0,-8.95 20.0,-20.0S35.05,4.0 24.0,4.0zm10.0,22.0L14.0,26.0l0.0,-4.0l20.0,0.0l0.0,4.0z" />

</vector>
+3 −0
Original line number Diff line number Diff line
@@ -6707,4 +6707,7 @@
    <!-- Summary of condition that airplane mode is on [CHAR LIMIT=NONE] -->
    <string name="condition_airplane_summary">Wi-Fi, Bluetooth, and cellular network are turned off. You can\'t make phone calls or connect to the Internet.</string>

    <!-- Title of condition that do not disturb is on [CHAR LIMIT=30] -->
    <string name="condition_zen_title">Do not disturb is on (<xliff:g name="zen_mode_type" example="Alarms only">%1$s</xliff:g>)</string>

</resources>
+3 −0
Original line number Diff line number Diff line
@@ -130,6 +130,7 @@ public class ConditionManager {
    private void addMissingConditions() {
        addIfMissing(AirplaneModeCondition.class);
        addIfMissing(HotspotCondition.class);
        addIfMissing(DndCondition.class);
    }

    private void addIfMissing(Class<? extends Condition> clz) {
@@ -144,6 +145,8 @@ public class ConditionManager {
            return new AirplaneModeCondition(this);
        } else if (HotspotCondition.class == clz) {
            return new HotspotCondition(this);
        } else if (DndCondition.class == clz) {
            return new DndCondition(this);
        }
        try {
            Constructor<?> constructor = clz.getConstructor(ConditionManager.class);
+133 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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.dashboard.conditional;

import android.app.ActivityManager;
import android.app.NotificationManager;
import android.app.StatusBarManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon;
import android.provider.Settings;

import android.service.notification.ZenModeConfig;
import com.android.settings.R;

public class DndCondition extends Condition {

    private static final String TAG = "DndCondition";

    private int mZen;
    private ZenModeConfig mConfig;

    public DndCondition(ConditionManager manager) {
        super(manager);
    }

    @Override
    public void refreshState() {
        NotificationManager notificationManager =
                mManager.getContext().getSystemService(NotificationManager.class);
        mZen = notificationManager.getZenMode();
        boolean zenModeEnabled = mZen != Settings.Global.ZEN_MODE_OFF;
        if (zenModeEnabled) {
            mConfig = notificationManager.getZenModeConfig();
        } else {
            mConfig = null;
        }
        setActive(zenModeEnabled);
    }

    @Override
    protected void onSilenceChanged(boolean silenced) {
        // Only need to listen for dnd mode changes when its been silenced.
        PackageManager pm = mManager.getContext().getPackageManager();
        pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), Receiver.class),
                silenced ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                        : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

    private CharSequence getZenState() {
        switch (mZen) {
            case Settings.Global.ZEN_MODE_ALARMS:
                return mManager.getContext().getString(R.string.zen_mode_option_alarms);
            case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
                return mManager.getContext().getString(
                        R.string.zen_mode_option_important_interruptions);
            case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
                return mManager.getContext().getString(R.string.zen_mode_option_no_interruptions);
        }
        return null;
    }

    @Override
    public Icon getIcon() {
        return Icon.createWithResource(mManager.getContext(), R.drawable.ic_zen);
    }

    @Override
    public CharSequence getTitle() {
        return mManager.getContext().getString(R.string.condition_zen_title, getZenState());
    }

    @Override
    public CharSequence getSummary() {
        final boolean isForever = mConfig != null && mConfig.manualRule != null
                && mConfig.manualRule.conditionId == null;
        return isForever ? mManager.getContext().getString(com.android.internal.R.string.zen_mode_forever_dnd)
                : ZenModeConfig.getConditionSummary(mManager.getContext(), mConfig,
                ActivityManager.getCurrentUser(),
                false);
    }

    @Override
    public CharSequence[] getActions() {
        return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_off) };
    }

    @Override
    public void onPrimaryClick() {
        StatusBarManager statusBar = mManager.getContext().getSystemService(StatusBarManager.class);
        statusBar.expandSettingsPanel("dnd");
    }

    @Override
    public void onActionClick(int index) {
        if (index == 0) {
            NotificationManager notificationManager = mManager.getContext().getSystemService(
                    NotificationManager.class);
            notificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG);
            setActive(false);
        } else {
            throw new IllegalArgumentException("Unexpected index " + index);
        }
    }

    public static class Receiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL
                    .equals(intent.getAction())) {
                ConditionManager.get(context).getCondition(DndCondition.class)
                        .refreshState();
            }
        }
    }
}