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.content.res.Resources.NotFoundException; 25 import android.media.AudioManager; 26 import android.util.Log; 27 import android.view.KeyEvent; 28 import android.view.WindowManager; 29 30 import com.android.systemui.statusbar.phone.SystemUIDialog; 31 32 abstract public class SafetyWarningDialog extends SystemUIDialog 33 implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener { 34 35 private static final String TAG = Util.logTag(SafetyWarningDialog.class); 36 37 private static final int KEY_CONFIRM_ALLOWED_AFTER = 1000; // milliseconds 38 39 private final Context mContext; 40 private final AudioManager mAudioManager; 41 42 private long mShowTime; 43 private boolean mNewVolumeUp; 44 private boolean mDisableOnVolumeUp; 45 SafetyWarningDialog(Context context, AudioManager audioManager)46 public SafetyWarningDialog(Context context, AudioManager audioManager) { 47 super(context); 48 mContext = context; 49 mAudioManager = audioManager; 50 try { 51 mDisableOnVolumeUp = mContext.getResources().getBoolean( 52 com.android.internal.R.bool.config_safe_media_disable_on_volume_up); 53 } catch (NotFoundException e) { 54 mDisableOnVolumeUp = true; 55 } 56 getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); 57 setShowForAllUsers(true); 58 setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning)); 59 setButton(DialogInterface.BUTTON_POSITIVE, 60 mContext.getString(com.android.internal.R.string.yes), this); 61 setButton(DialogInterface.BUTTON_NEGATIVE, 62 mContext.getString(com.android.internal.R.string.no), (OnClickListener) null); 63 setOnDismissListener(this); 64 65 final IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 66 context.registerReceiver(mReceiver, filter); 67 } 68 cleanUp()69 abstract protected void cleanUp(); 70 71 @Override onKeyDown(int keyCode, KeyEvent event)72 public boolean onKeyDown(int keyCode, KeyEvent event) { 73 if (mDisableOnVolumeUp && keyCode == KeyEvent.KEYCODE_VOLUME_UP 74 && event.getRepeatCount() == 0) { 75 mNewVolumeUp = true; 76 } 77 return super.onKeyDown(keyCode, event); 78 } 79 80 @Override onKeyUp(int keyCode, KeyEvent event)81 public boolean onKeyUp(int keyCode, KeyEvent event) { 82 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp 83 && (System.currentTimeMillis() - mShowTime) > KEY_CONFIRM_ALLOWED_AFTER) { 84 if (D.BUG) Log.d(TAG, "Confirmed warning via VOLUME_UP"); 85 mAudioManager.disableSafeMediaVolume(); 86 dismiss(); 87 } 88 return super.onKeyUp(keyCode, event); 89 } 90 91 @Override onClick(DialogInterface dialog, int which)92 public void onClick(DialogInterface dialog, int which) { 93 mAudioManager.disableSafeMediaVolume(); 94 } 95 96 @Override onStart()97 protected void onStart() { 98 super.onStart(); 99 mShowTime = System.currentTimeMillis(); 100 } 101 102 @Override onDismiss(DialogInterface unused)103 public void onDismiss(DialogInterface unused) { 104 try { 105 mContext.unregisterReceiver(mReceiver); 106 } catch (IllegalArgumentException e) { 107 // Don't crash if the receiver has already been unregistered. 108 e.printStackTrace(); 109 } 110 cleanUp(); 111 } 112 113 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 114 @Override 115 public void onReceive(Context context, Intent intent) { 116 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) { 117 if (D.BUG) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS"); 118 cancel(); 119 cleanUp(); 120 } 121 } 122 }; 123 } 124