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

Commit e86de4c0 authored by John Spurlock's avatar John Spurlock
Browse files

Zen mode tweaks.

 - Fix regression with alarms.
 - Run all condition provider callbacks on the main thread.
 - Exit zen mode if the current condition is disabled / uninstalled.

Bug:14402762
Change-Id: I0746670c1910047a9dc9b7e29aa1a6c3899fd9fe
parent 1de6b101
Loading
Loading
Loading
Loading
+39 −19
Original line number Diff line number Diff line
@@ -22,7 +22,9 @@ import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.ServiceManager;
import android.util.Log;

@@ -46,6 +48,8 @@ public abstract class ConditionProviderService extends Service {
    private final String TAG = ConditionProviderService.class.getSimpleName()
            + "[" + getClass().getSimpleName() + "]";

    private final H mHandler = new H();

    private Provider mProvider;
    private INotificationManager mNoMan;

@@ -100,41 +104,57 @@ public abstract class ConditionProviderService extends Service {
    }

    private final class Provider extends IConditionProvider.Stub {
        private final ConditionProviderService mService = ConditionProviderService.this;

        @Override
        public void onConnected() {
            try {
                mService.onConnected();
            } catch (Throwable t) {
                Log.w(TAG, "Error running onConnected", t);
            }
            mHandler.obtainMessage(H.ON_CONNECTED).sendToTarget();
        }

        @Override
        public void onRequestConditions(int relevance) {
            try {
                mService.onRequestConditions(relevance);
            } catch (Throwable t) {
                Log.w(TAG, "Error running onRequestConditions", t);
            }
            mHandler.obtainMessage(H.ON_REQUEST_CONDITIONS, relevance, 0).sendToTarget();
        }

        @Override
        public void onSubscribe(Uri conditionId) {
            try {
                mService.onSubscribe(conditionId);
            } catch (Throwable t) {
                Log.w(TAG, "Error running onSubscribe", t);
            }
            mHandler.obtainMessage(H.ON_SUBSCRIBE, conditionId).sendToTarget();
        }

        @Override
        public void onUnsubscribe(Uri conditionId) {
            mHandler.obtainMessage(H.ON_UNSUBSCRIBE, conditionId).sendToTarget();
        }
    }

    private final class H extends Handler {
        private static final int ON_CONNECTED = 1;
        private static final int ON_REQUEST_CONDITIONS = 2;
        private static final int ON_SUBSCRIBE = 3;
        private static final int ON_UNSUBSCRIBE = 4;

        @Override
        public void handleMessage(Message msg) {
            String name = null;
            try {
                mService.onUnsubscribe(conditionId);
                switch(msg.what) {
                    case ON_CONNECTED:
                        name = "onConnected";
                        onConnected();
                        break;
                    case ON_REQUEST_CONDITIONS:
                        name = "onRequestConditions";
                        onRequestConditions(msg.arg1);
                        break;
                    case ON_SUBSCRIBE:
                        name = "onSubscribe";
                        onSubscribe((Uri)msg.obj);
                        break;
                    case ON_UNSUBSCRIBE:
                        name = "onUnsubscribe";
                        onUnsubscribe((Uri)msg.obj);
                        break;
                }
            } catch (Throwable t) {
                Log.w(TAG, "Error running onUnsubscribe", t);
                Log.w(TAG, "Error running " + name, t);
            }
        }
    }
+6 −0
Original line number Diff line number Diff line
@@ -108,6 +108,12 @@ public class ConditionProviders extends ManagedServices {
    @Override
    protected void onServiceRemovedLocked(ManagedServiceInfo removed) {
        if (removed == null) return;
        if (mCurrentConditionId != null) {
            if (removed.equals(mConditions.get(mCurrentConditionId))) {
                mCurrentConditionId = null;
                mZenModeHelper.setZenMode(Global.ZEN_MODE_OFF);
            }
        }
        for (int i = mConditions.size() - 1; i >= 0; i--) {
            if (removed.equals(mConditions.valueAt(i))) {
                mConditions.removeAt(i);
+10 −0
Original line number Diff line number Diff line
@@ -84,6 +84,9 @@ public class ZenModeHelper {
            "com.google.android.talk",
            "com.android.mms"
            ));
    private static final Set<String> ALARM_PACKAGES = new HashSet<String>(Arrays.asList(
            "com.google.android.deskclock"
            ));

    public ZenModeHelper(Context context, Handler handler) {
        mContext = context;
@@ -122,6 +125,9 @@ public class ZenModeHelper {

    public boolean shouldIntercept(String pkg, Notification n) {
        if (mZenMode != Global.ZEN_MODE_OFF) {
            if (isAlarm(pkg, n)) {
                return false;
            }
            if (isCall(pkg, n)) {
                return !mConfig.allowCalls;
            }
@@ -223,6 +229,10 @@ public class ZenModeHelper {
        }
    }

    private boolean isAlarm(String pkg, Notification n) {
        return ALARM_PACKAGES.contains(pkg);
    }

    private boolean isCall(String pkg, Notification n) {
        return CALL_PACKAGES.contains(pkg);
    }