Home | History | Annotate | Download | only in volume
      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 
     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         setShowForAllUsers(true);
     51         setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
     52         setButton(DialogInterface.BUTTON_POSITIVE,
     53                 mContext.getString(com.android.internal.R.string.yes), this);
     54         setButton(DialogInterface.BUTTON_NEGATIVE,
     55                 mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
     56         setOnDismissListener(this);
     57 
     58         final IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
     59         context.registerReceiver(mReceiver, filter);
     60     }
     61 
     62     abstract protected void cleanUp();
     63 
     64     @Override
     65     public boolean onKeyDown(int keyCode, KeyEvent event) {
     66         if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
     67             mNewVolumeUp = true;
     68         }
     69         return super.onKeyDown(keyCode, event);
     70     }
     71 
     72     @Override
     73     public boolean onKeyUp(int keyCode, KeyEvent event) {
     74         if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp
     75                 && (System.currentTimeMillis() - mShowTime) > KEY_CONFIRM_ALLOWED_AFTER) {
     76             if (D.BUG) Log.d(TAG, "Confirmed warning via VOLUME_UP");
     77             mAudioManager.disableSafeMediaVolume();
     78             dismiss();
     79         }
     80         return super.onKeyUp(keyCode, event);
     81     }
     82 
     83     @Override
     84     public void onClick(DialogInterface dialog, int which) {
     85         mAudioManager.disableSafeMediaVolume();
     86     }
     87 
     88     @Override
     89     protected void onStart() {
     90         super.onStart();
     91         mShowTime = System.currentTimeMillis();
     92     }
     93 
     94     @Override
     95     public void onDismiss(DialogInterface unused) {
     96         mContext.unregisterReceiver(mReceiver);
     97         cleanUp();
     98     }
     99 
    100     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    101         @Override
    102         public void onReceive(Context context, Intent intent) {
    103             if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
    104                 if (D.BUG) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
    105                 cancel();
    106                 cleanUp();
    107             }
    108         }
    109     };
    110 }