1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.volume;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.media.AudioManager;
25import android.util.Log;
26import android.view.KeyEvent;
27import android.view.WindowManager;
28
29import com.android.systemui.statusbar.phone.SystemUIDialog;
30
31abstract public class SafetyWarningDialog extends SystemUIDialog
32        implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
33
34    private static final String TAG = Util.logTag(SafetyWarningDialog.class);
35
36    private static final int KEY_CONFIRM_ALLOWED_AFTER = 1000; // milliseconds
37
38    private final Context mContext;
39    private final AudioManager mAudioManager;
40
41    private long mShowTime;
42    private boolean mNewVolumeUp;
43
44    public SafetyWarningDialog(Context context, AudioManager audioManager) {
45        super(context);
46        mContext = context;
47        mAudioManager = audioManager;
48
49        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
50        setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
51        setButton(DialogInterface.BUTTON_POSITIVE,
52                mContext.getString(com.android.internal.R.string.yes), this);
53        setButton(DialogInterface.BUTTON_NEGATIVE,
54                mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
55        setOnDismissListener(this);
56
57        final IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
58        context.registerReceiver(mReceiver, filter);
59    }
60
61    abstract protected void cleanUp();
62
63    @Override
64    public boolean onKeyDown(int keyCode, KeyEvent event) {
65        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
66            mNewVolumeUp = true;
67        }
68        return super.onKeyDown(keyCode, event);
69    }
70
71    @Override
72    public boolean onKeyUp(int keyCode, KeyEvent event) {
73        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp
74                && (System.currentTimeMillis() - mShowTime) > KEY_CONFIRM_ALLOWED_AFTER) {
75            if (D.BUG) Log.d(TAG, "Confirmed warning via VOLUME_UP");
76            mAudioManager.disableSafeMediaVolume();
77            dismiss();
78        }
79        return super.onKeyUp(keyCode, event);
80    }
81
82    @Override
83    public void onClick(DialogInterface dialog, int which) {
84        mAudioManager.disableSafeMediaVolume();
85    }
86
87    @Override
88    protected void onStart() {
89        super.onStart();
90        mShowTime = System.currentTimeMillis();
91    }
92
93    @Override
94    public void onDismiss(DialogInterface unused) {
95        mContext.unregisterReceiver(mReceiver);
96        cleanUp();
97    }
98
99    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
100        @Override
101        public void onReceive(Context context, Intent intent) {
102            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
103                if (D.BUG) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
104                cancel();
105                cleanUp();
106            }
107        }
108    };
109}