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

Commit 1ef87076 authored by d34d's avatar d34d Committed by Gerrit Code Review
Browse files

GlobalActions: Fix setting ringer mode in global actions

It looks like we did not bring in this full feature for cm-13.0.
This patch updates SilentModeTriStateAction to match cm-12.1 with
one minor change.  We can no longer set the zen mode by writing to
Global settings directly.  The ZenModeHelper class actually checks
when a change is made and resets it.  The fix is to use
INotificationManager.setZenMode() instead.

Change-Id: Ib2423a19da3a2ca95f73cb92247e0a38bff8bbee
TICKET: CYNGNOS-1808
parent bea7701f
Loading
Loading
Loading
Loading
+56 −12
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.app.ActivityManager;
import android.app.ActivityManagerNative;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.INotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
@@ -58,6 +59,7 @@ import android.os.UserHandle;
import android.os.UserManager;
import android.os.Vibrator;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.service.dreams.DreamService;
import android.service.dreams.IDreamManager;
import android.telephony.PhoneStateListener;
@@ -84,6 +86,7 @@ import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.TextView;

import cyanogenmod.providers.CMSettings;

import java.util.ArrayList;
@@ -1116,9 +1119,9 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac
        }
    }

    private static class SilentModeTriStateAction implements Action, View.OnClickListener {
    private final class SilentModeTriStateAction implements Action, View.OnClickListener {

        private final int[] ITEM_IDS = { R.id.option1, R.id.option2, R.id.option3 };
        private final int[] ITEM_IDS = { R.id.option1, R.id.option2, R.id.option3, R.id.option4 };

        private final AudioManager mAudioManager;
        private final Handler mHandler;
@@ -1130,14 +1133,15 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac
            mContext = context;
        }

        private int ringerModeToIndex(int ringerMode) {
            // They just happen to coincide
            return ringerMode;
        }

        private int indexToRingerMode(int index) {
            // They just happen to coincide
            return index;
            if (index == 2) {
                if (mHasVibrator) {
                    return AudioManager.RINGER_MODE_VIBRATE;
                } else {
                    return AudioManager.RINGER_MODE_NORMAL;
                }
            }
            return AudioManager.RINGER_MODE_NORMAL;
        }

        @Override
@@ -1149,9 +1153,28 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac
                LayoutInflater inflater) {
            View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);

            int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
            for (int i = 0; i < 3; i++) {
            int ringerMode = mAudioManager.getRingerModeInternal();
            int zenMode = Global.getInt(mContext.getContentResolver(), Global.ZEN_MODE,
                    Global.ZEN_MODE_OFF);
            int selectedIndex = 0;
            if (zenMode != Global.ZEN_MODE_OFF) {
                if (zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) {
                    selectedIndex = 0;
                } else if (zenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) {
                    selectedIndex = 1;
                }
            } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
                selectedIndex = 2;
            } else if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {
                selectedIndex = 3;
            }

            for (int i = 0; i < ITEM_IDS.length; i++) {
                View itemView = v.findViewById(ITEM_IDS[i]);
                if (!mHasVibrator && i == 2) {
                    itemView.setVisibility(View.GONE);
                    continue;
                }
                itemView.setSelected(selectedIndex == i);
                // Set up click handler
                itemView.setTag(i);
@@ -1182,7 +1205,28 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac
            if (!(v.getTag() instanceof Integer)) return;

            int index = (Integer) v.getTag();
            mAudioManager.setRingerMode(indexToRingerMode(index));
            if (index == 0 || index == 1) {
                int zenMode = index == 0
                        ? Global.ZEN_MODE_NO_INTERRUPTIONS
                        : Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
                // ZenModeHelper will revert zen mode back to the previous value if we just
                // put the value into the Settings db, so use INotificationManager instead
                INotificationManager noMan = INotificationManager.Stub.asInterface(
                        ServiceManager.getService(Context.NOTIFICATION_SERVICE));
                try {
                    noMan.setZenMode(zenMode, null, TAG);
                } catch (RemoteException e) {
                    Log.e(TAG, "Unable to set zen mode", e);
                }
            } else {
                Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, Global.ZEN_MODE_OFF);
            }

            if (index == 2 || index == 3) {
                int ringerMode = indexToRingerMode(index);
                mAudioManager.setRingerModeInternal(ringerMode);
            }
            mAdapter.notifyDataSetChanged();
            mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
        }
    }