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