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 17 package com.android.systemui.volume; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.media.AudioManager; 25 import android.util.Log; 26 import android.view.KeyEvent; 27 import android.view.WindowManager; 28 29 import com.android.systemui.statusbar.phone.SystemUIDialog; 30 31 abstract 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 SafetyWarningDialog(Context context, AudioManager audioManager)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 cleanUp()61 abstract protected void cleanUp(); 62 63 @Override onKeyDown(int keyCode, KeyEvent event)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 onKeyUp(int keyCode, KeyEvent event)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 onClick(DialogInterface dialog, int which)83 public void onClick(DialogInterface dialog, int which) { 84 mAudioManager.disableSafeMediaVolume(); 85 } 86 87 @Override onStart()88 protected void onStart() { 89 super.onStart(); 90 mShowTime = System.currentTimeMillis(); 91 } 92 93 @Override onDismiss(DialogInterface unused)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 }