Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2013 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.settings;
     18 
     19 import android.app.Dialog;
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.os.Bundle;
     23 import android.os.Handler;
     24 import android.view.Window;
     25 import android.view.WindowManager;
     26 import android.widget.ImageView;
     27 
     28 import com.android.systemui.R;
     29 
     30 /** A dialog that provides controls for adjusting the screen brightness. */
     31 public class BrightnessDialog extends Dialog implements
     32         BrightnessController.BrightnessStateChangeCallback {
     33 
     34     private static final String TAG = "BrightnessDialog";
     35     private static final boolean DEBUG = false;
     36 
     37     protected Handler mHandler = new Handler();
     38 
     39     private BrightnessController mBrightnessController;
     40     private final int mBrightnessDialogLongTimeout;
     41     private final int mBrightnessDialogShortTimeout;
     42 
     43     private final Runnable mDismissDialogRunnable = new Runnable() {
     44         public void run() {
     45             if (BrightnessDialog.this.isShowing()) {
     46                 BrightnessDialog.this.dismiss();
     47             }
     48         };
     49     };
     50 
     51 
     52     public BrightnessDialog(Context ctx) {
     53         super(ctx);
     54         Resources r = ctx.getResources();
     55         mBrightnessDialogLongTimeout =
     56                 r.getInteger(R.integer.quick_settings_brightness_dialog_long_timeout);
     57         mBrightnessDialogShortTimeout =
     58                 r.getInteger(R.integer.quick_settings_brightness_dialog_short_timeout);
     59     }
     60 
     61 
     62     /**
     63      * Create the brightness dialog and any resources that are used for the
     64      * entire lifetime of the dialog.
     65      */
     66     @Override
     67     public void onCreate(Bundle savedInstanceState) {
     68         super.onCreate(savedInstanceState);
     69         Window window = getWindow();
     70         window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
     71         window.getAttributes().privateFlags |=
     72                 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
     73         window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
     74         window.requestFeature(Window.FEATURE_NO_TITLE);
     75 
     76         setContentView(R.layout.quick_settings_brightness_dialog);
     77         setCanceledOnTouchOutside(true);
     78     }
     79 
     80 
     81     @Override
     82     protected void onStart() {
     83         super.onStart();
     84         mBrightnessController = new BrightnessController(getContext(),
     85                 (ImageView) findViewById(R.id.brightness_icon),
     86                 (ToggleSlider) findViewById(R.id.brightness_slider));
     87         dismissBrightnessDialog(mBrightnessDialogLongTimeout);
     88         mBrightnessController.addStateChangedCallback(this);
     89     }
     90 
     91     @Override
     92     protected void onStop() {
     93         super.onStop();
     94         mBrightnessController.unregisterCallbacks();
     95         removeAllBrightnessDialogCallbacks();
     96     }
     97 
     98     public void onBrightnessLevelChanged() {
     99         dismissBrightnessDialog(mBrightnessDialogShortTimeout);
    100     }
    101 
    102     private void dismissBrightnessDialog(int timeout) {
    103         removeAllBrightnessDialogCallbacks();
    104         mHandler.postDelayed(mDismissDialogRunnable, timeout);
    105     }
    106 
    107     private void removeAllBrightnessDialogCallbacks() {
    108         mHandler.removeCallbacks(mDismissDialogRunnable);
    109     }
    110 
    111 }
    112