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 Context.RECEIVER_EXPORTED_UNAUDITED); 68 } 69 cleanUp()70 abstract protected void cleanUp(); 71 72 @Override onKeyDown(int keyCode, KeyEvent event)73 public boolean onKeyDown(int keyCode, KeyEvent event) { 74 if (mDisableOnVolumeUp && keyCode == KeyEvent.KEYCODE_VOLUME_UP 75 && event.getRepeatCount() == 0) { 76 mNewVolumeUp = true; 77 } 78 return super.onKeyDown(keyCode, event); 79 } 80 81 @Override onKeyUp(int keyCode, KeyEvent event)82 public boolean onKeyUp(int keyCode, KeyEvent event) { 83 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp 84 && (System.currentTimeMillis() - mShowTime) > KEY_CONFIRM_ALLOWED_AFTER) { 85 if (D.BUG) Log.d(TAG, "Confirmed warning via VOLUME_UP"); 86 mAudioManager.disableSafeMediaVolume(); 87 dismiss(); 88 } 89 return super.onKeyUp(keyCode, event); 90 } 91 92 @Override onClick(DialogInterface dialog, int which)93 public void onClick(DialogInterface dialog, int which) { 94 mAudioManager.disableSafeMediaVolume(); 95 } 96 97 @Override start()98 protected void start() { 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