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

Commit 76b52b3f authored by John Spurlock's avatar John Spurlock
Browse files

Volume: Port safety warning over to new dialog.

And remove obsolete "muted by <x>" disablement.

Bug: 19260237
Change-Id: I17dc6d5761aad9ce70cc5aad054489549113e15e
parent ad80b437
Loading
Loading
Loading
Loading
+109 −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.systemui.volume;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.android.systemui.statusbar.phone.SystemUIDialog;

abstract public class SafetyWarningDialog extends SystemUIDialog
        implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {

    private static final String TAG = Util.logTag(SafetyWarningDialog.class);

    private static final int KEY_CONFIRM_ALLOWED_AFTER = 1000; // milliseconds

    private final Context mContext;
    private final AudioManager mAudioManager;

    private long mShowTime;
    private boolean mNewVolumeUp;

    public SafetyWarningDialog(Context context, AudioManager audioManager) {
        super(context);
        mContext = context;
        mAudioManager = audioManager;

        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
        setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
        setButton(DialogInterface.BUTTON_POSITIVE,
                mContext.getString(com.android.internal.R.string.yes), this);
        setButton(DialogInterface.BUTTON_NEGATIVE,
                mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
        setOnDismissListener(this);

        final IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.registerReceiver(mReceiver, filter);
    }

    abstract protected void cleanUp();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
            mNewVolumeUp = true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp
                && (System.currentTimeMillis() - mShowTime) > KEY_CONFIRM_ALLOWED_AFTER) {
            if (D.BUG) Log.d(TAG, "Confirmed warning via VOLUME_UP");
            mAudioManager.disableSafeMediaVolume();
            dismiss();
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        mAudioManager.disableSafeMediaVolume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mShowTime = System.currentTimeMillis();
    }

    @Override
    public void onDismiss(DialogInterface unused) {
        mContext.unregisterReceiver(mReceiver);
        cleanUp();
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
                if (D.BUG) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
                cancel();
                cleanUp();
            }
        }
    };
}
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ class Util {
        AudioManager.FLAG_SHOW_VIBRATE_HINT,
        AudioManager.FLAG_SHOW_SILENT_HINT,
        AudioManager.FLAG_FROM_KEY,
        AudioManager.FLAG_SHOW_UI_WARNINGS,
    };

    private static String[] AUDIO_MANAGER_FLAG_NAMES = new String[] {
@@ -58,6 +59,7 @@ class Util {
        "SHOW_VIBRATE_HINT",
        "SHOW_SILENT_HINT",
        "FROM_KEY",
        "SHOW_UI_WARNINGS",
    };

    public static String logTag(Class<?> c) {
+49 −19
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ public class VolumeDialog {
    private final View mTextFooter;
    private final ZenFooter mZenFooter;
    private final LayoutTransition mLayoutTransition;
    private final Object mSafetyWarningLock = new Object();

    private boolean mShowing;
    private boolean mExpanded;
@@ -113,8 +114,9 @@ public class VolumeDialog {
    private boolean mAutomute = Prefs.DEFAULT_ENABLE_AUTOMUTE;
    private boolean mSilentMode = Prefs.DEFAULT_ENABLE_SILENT_MODE;
    private State mState;
    private int mExpandAnimRes;
    private int mExpandButtonRes;
    private boolean mExpanding;
    private SafetyWarningDialog mSafetyWarning;

    public VolumeDialog(Context context, VolumeDialogController controller,
            ZenModeController zenModeController) {
@@ -138,7 +140,7 @@ public class VolumeDialog {
        mDialog.setCanceledOnTouchOutside(true);
        final Resources res = mContext.getResources();
        final WindowManager.LayoutParams lp = window.getAttributes();
        lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        lp.type = WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY;
        lp.format = PixelFormat.TRANSLUCENT;
        lp.setTitle(VolumeDialog.class.getSimpleName());
        lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
@@ -450,6 +452,7 @@ public class VolumeDialog {

    private int computeTimeoutH() {
        if (mZenFooter != null && mZenFooter.isFooterExpanded()) return 10000;
        if (mSafetyWarning != null) return 5000;
        if (mExpanded || mExpanding) return 5000;
        if (mActiveStream == AudioManager.STREAM_MUSIC) return 1500;
        return 3000;
@@ -464,6 +467,12 @@ public class VolumeDialog {
        Events.writeEvent(Events.EVENT_DISMISS_DIALOG, reason);
        setExpandedH(false);
        mController.notifyVisible(false);
        synchronized (mSafetyWarningLock) {
            if (mSafetyWarning != null) {
                if (D.BUG) Log.d(TAG, "SafetyWarning dismissed");
                mSafetyWarning.dismiss();
            }
        }
    }

    private void setExpandedH(boolean expanded) {
@@ -498,8 +507,8 @@ public class VolumeDialog {
        if (mExpanding && isAttached()) return;
        final int res = mExpanded ? R.drawable.ic_volume_collapse_animation
                : R.drawable.ic_volume_expand_animation;
        if (res == mExpandAnimRes) return;
        mExpandAnimRes = res;
        if (res == mExpandButtonRes) return;
        mExpandButtonRes = res;
        mExpandButton.setImageResource(res);
    }

@@ -654,7 +663,6 @@ public class VolumeDialog {
                && mState.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS;
        final boolean isLimited = isRingStream
                && mState.zenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
        final boolean isRingAndSuppressed = isRingStream && mState.effectsSuppressor != null;

        // update slider max
        final int max = ss.levelMax * 100;
@@ -670,10 +678,7 @@ public class VolumeDialog {

        // update header text
        final String text;
        if (isRingAndSuppressed) {
            text = mContext.getString(R.string.volume_stream_suppressed, ss.name,
                    mState.effectsSuppressorName);
        } else if (isNoned) {
        if (isNoned) {
            text = mContext.getString(R.string.volume_stream_muted_dnd, ss.name);
        } else if (isRingVibrate && isLimited) {
            text = mContext.getString(R.string.volume_stream_vibrate_dnd, ss.name);
@@ -689,14 +694,14 @@ public class VolumeDialog {
        Util.setText(row.header, text);

        // update icon
        final boolean iconEnabled = !isRingAndSuppressed && (mAutomute || ss.muteSupported);
        final boolean iconEnabled = mAutomute || ss.muteSupported;
        row.icon.setEnabled(iconEnabled);
        row.icon.setAlpha(iconEnabled ? 1 : 0.5f);
        final int iconRes =
                !isRingAndSuppressed && isRingVibrate ? R.drawable.ic_volume_ringer_vibrate
                isRingVibrate ? R.drawable.ic_volume_ringer_vibrate
                : ss.routedToBluetooth ?
                        (ss.muted ? R.drawable.ic_volume_bt_mute : R.drawable.ic_volume_bt)
                : isRingAndSuppressed || (mAutomute && ss.level == 0) ? row.iconMuteRes
                : mAutomute && ss.level == 0 ? row.iconMuteRes
                : (ss.muted ? row.iconMuteRes : row.iconRes);
        if (iconRes != row.cachedIconRes) {
            if (row.cachedIconRes != 0 && isRingVibrate) {
@@ -714,18 +719,13 @@ public class VolumeDialog {
                : Events.ICON_STATE_UNKNOWN;

        // update slider
        updateVolumeRowSliderH(row, isRingAndSuppressed);
        updateVolumeRowSliderH(row);
    }

    private void updateVolumeRowSliderH(VolumeRow row, boolean isRingAndSuppressed) {
        row.slider.setEnabled(!isRingAndSuppressed);
    private void updateVolumeRowSliderH(VolumeRow row) {
        if (row.tracking) {
            return;  // don't update if user is sliding
        }
        if (isRingAndSuppressed) {
            row.slider.setProgress(0);
            return;
        }
        final int progress = row.slider.getProgress();
        final int level = getImpliedLevel(row.slider, progress);
        final boolean rowVisible = row.view.getVisibility() == View.VISIBLE;
@@ -800,6 +800,29 @@ public class VolumeDialog {
        }
    }

    private void showSafetyWarningH(int flags) {
        if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
                || mShowing) {
            synchronized (mSafetyWarningLock) {
                if (mSafetyWarning != null) {
                    return;
                }
                mSafetyWarning = new SafetyWarningDialog(mContext, mController.getAudioManager()) {
                    @Override
                    protected void cleanUp() {
                        synchronized (mSafetyWarningLock) {
                            mSafetyWarning = null;
                        }
                        recheckH(null);
                    }
                };
                mSafetyWarning.show();
            }
            recheckH(null);
        }
        rescheduleTimeoutH();
    }

    private final VolumeDialogController.Callbacks mControllerCallbackH
            = new VolumeDialogController.Callbacks() {
        @Override
@@ -812,6 +835,7 @@ public class VolumeDialog {
            dismissH(reason);
        }

        @Override
        public void onScreenOff() {
            dismissH(Events.DISMISS_REASON_SCREEN_OFF);
        }
@@ -839,11 +863,17 @@ public class VolumeDialog {
            }
        }

        @Override
        public void onShowSilentHint() {
            if (mSilentMode) {
                mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
            }
        }

        @Override
        public void onShowSafetyWarning(int flags) {
            showSafetyWarningH(flags);
        }
    };

    private final OnClickListener mClickExpand = new OnClickListener() {
+29 −3
Original line number Diff line number Diff line
@@ -119,6 +119,10 @@ public class VolumeDialogController {
        mHasVibrator = mVibrator != null && mVibrator.hasVibrator();
    }

    public AudioManager getAudioManager() {
        return mAudio;
    }

    public void dismiss() {
        mCallbacks.onDismissRequested(Events.DISMISS_REASON_VOLUME_CONTROLLER);
    }
@@ -256,6 +260,10 @@ public class VolumeDialogController {
        // hook for subclasses
    }

    private void onShowSafetyWarningW(int flags) {
        mCallbacks.onShowSafetyWarning(flags);
    }

    private boolean checkRoutedToBluetoothW(int stream) {
        boolean changed = false;
        if (stream == AudioManager.STREAM_MUSIC) {
@@ -490,7 +498,10 @@ public class VolumeDialogController {

        @Override
        public void displaySafeVolumeWarning(int flags) throws RemoteException {
            // noop
            if (D.BUG) Log.d(TAG, "displaySafeVolumeWarning "
                    + Util.audioManagerFlagsToString(flags));
            if (mDestroyed) return;
            mWorker.obtainMessage(W.SHOW_SAFETY_WARNING, flags, 0).sendToTarget();
        }

        @Override
@@ -537,6 +548,7 @@ public class VolumeDialogController {
        private static final int SET_ACTIVE_STREAM = 11;
        private static final int NOTIFY_VISIBLE = 12;
        private static final int USER_ACTIVITY = 13;
        private static final int SHOW_SAFETY_WARNING = 14;

        W(Looper looper) {
            super(looper);
@@ -556,8 +568,9 @@ public class VolumeDialogController {
                case CONFIGURATION_CHANGED: mCallbacks.onConfigurationChanged(); break;
                case SET_STREAM_VOLUME: onSetStreamVolumeW(msg.arg1, msg.arg2); break;
                case SET_ACTIVE_STREAM: onSetActiveStreamW(msg.arg1); break;
                case NOTIFY_VISIBLE: onNotifyVisibleW(msg.arg1 != 0);
                case USER_ACTIVITY: onUserActivityW();
                case NOTIFY_VISIBLE: onNotifyVisibleW(msg.arg1 != 0); break;
                case USER_ACTIVITY: onUserActivityW(); break;
                case SHOW_SAFETY_WARNING: onShowSafetyWarningW(msg.arg1); break;
            }
        }
    }
@@ -672,6 +685,18 @@ public class VolumeDialogController {
                });
            }
        }

        @Override
        public void onShowSafetyWarning(final int flags) {
            for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) {
                entry.getValue().post(new Runnable() {
                    @Override
                    public void run() {
                        entry.getKey().onShowSafetyWarning(flags);
                    }
                });
            }
        }
    }


@@ -958,5 +983,6 @@ public class VolumeDialogController {
        void onShowVibrateHint();
        void onShowSilentHint();
        void onScreenOff();
        void onShowSafetyWarning(int flags);
    }
}
+10 −77
Original line number Diff line number Diff line
@@ -52,7 +52,6 @@ import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
@@ -72,7 +71,6 @@ import android.widget.TextView;

import com.android.internal.R;
import com.android.systemui.DemoMode;
import com.android.systemui.statusbar.phone.SystemUIDialog;
import com.android.systemui.statusbar.policy.ZenModeController;

import java.io.FileDescriptor;
@@ -264,80 +262,6 @@ public class VolumePanel extends Handler implements DemoMode {
    private static AlertDialog sSafetyWarning;
    private static Object sSafetyWarningLock = new Object();

    private static class SafetyWarning extends SystemUIDialog
            implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
        private final Context mContext;
        private final VolumePanel mVolumePanel;
        private final AudioManager mAudioManager;

        private boolean mNewVolumeUp;

        SafetyWarning(Context context, VolumePanel volumePanel, AudioManager audioManager) {
            super(context);
            mContext = context;
            mVolumePanel = volumePanel;
            mAudioManager = audioManager;

            setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
            setButton(DialogInterface.BUTTON_POSITIVE,
                    mContext.getString(com.android.internal.R.string.yes), this);
            setButton(DialogInterface.BUTTON_NEGATIVE,
                    mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
            setOnDismissListener(this);

            IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            context.registerReceiver(mReceiver, filter);
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
                mNewVolumeUp = true;
            }
            return super.onKeyDown(keyCode, event);
        }

        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp) {
                if (LOGD) Log.d(TAG, "Confirmed warning via VOLUME_UP");
                mAudioManager.disableSafeMediaVolume();
                dismiss();
            }
            return super.onKeyUp(keyCode, event);
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            mAudioManager.disableSafeMediaVolume();
        }

        @Override
        public void onDismiss(DialogInterface unused) {
            mContext.unregisterReceiver(mReceiver);
            cleanUp();
        }

        private void cleanUp() {
            synchronized (sSafetyWarningLock) {
                sSafetyWarning = null;
            }
            mVolumePanel.forceTimeout(0);
            mVolumePanel.updateStates();
        }

        private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
                    if (LOGD) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
                    cancel();
                    cleanUp();
                }
            }
        };
    }

    protected LayoutParams getDialogLayoutParams(Window window, Resources res) {
        final LayoutParams lp = window.getAttributes();
        lp.token = null;
@@ -1283,7 +1207,16 @@ public class VolumePanel extends Handler implements DemoMode {
                if (sSafetyWarning != null) {
                    return;
                }
                sSafetyWarning = new SafetyWarning(mContext, this, mAudioManager);
                sSafetyWarning = new SafetyWarningDialog(mContext, mAudioManager) {
                    @Override
                    protected void cleanUp() {
                        synchronized (sSafetyWarningLock) {
                            sSafetyWarning = null;
                        }
                        forceTimeout(0);
                        updateStates();
                    }
                };
                sSafetyWarning.show();
            }
            updateStates();