1/*
2 * Copyright (C) 2007 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
17package com.android.internal.policy.impl;
18
19import com.android.internal.R;
20
21import android.app.Dialog;
22import android.app.StatusBarManager;
23import android.content.Context;
24import android.os.Bundle;
25import android.os.RemoteException;
26import android.os.LocalPowerManager;
27import android.os.ServiceManager;
28import android.os.SystemClock;
29
30import com.android.internal.app.ShutdownThread;
31import com.android.internal.telephony.ITelephony;
32import android.view.KeyEvent;
33import android.util.Log;
34import android.view.View;
35import android.view.WindowManager;
36import android.view.View.OnClickListener;
37import android.view.View.OnKeyListener;
38import android.widget.Button;
39
40/**
41 * @deprecated use {@link GlobalActions} instead.
42 */
43public class PowerDialog extends Dialog implements OnClickListener,
44        OnKeyListener {
45    private static final String TAG = "PowerDialog";
46
47    static private StatusBarManager sStatusBar;
48    private Button mKeyguard;
49    private Button mPower;
50    private Button mRadioPower;
51    private Button mSilent;
52
53    private LocalPowerManager mPowerManager;
54
55    public PowerDialog(Context context, LocalPowerManager powerManager) {
56        super(context);
57        mPowerManager = powerManager;
58    }
59
60    @Override
61    protected void onCreate(Bundle savedInstanceState) {
62        super.onCreate(savedInstanceState);
63
64        Context context = getContext();
65
66        if (sStatusBar == null) {
67            sStatusBar = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);
68        }
69
70        setContentView(com.android.internal.R.layout.power_dialog);
71
72        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
73        if (!getContext().getResources().getBoolean(
74                com.android.internal.R.bool.config_sf_slowBlur)) {
75            getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
76                    WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
77        }
78        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
79                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
80
81        setTitle(context.getText(R.string.power_dialog));
82
83        mKeyguard = (Button) findViewById(R.id.keyguard);
84        mPower = (Button) findViewById(R.id.off);
85        mRadioPower = (Button) findViewById(R.id.radio_power);
86        mSilent = (Button) findViewById(R.id.silent);
87
88        if (mKeyguard != null) {
89            mKeyguard.setOnKeyListener(this);
90            mKeyguard.setOnClickListener(this);
91        }
92        if (mPower != null) {
93            mPower.setOnClickListener(this);
94        }
95        if (mRadioPower != null) {
96            mRadioPower.setOnClickListener(this);
97        }
98        if (mSilent != null) {
99            mSilent.setOnClickListener(this);
100            // XXX: HACK for now hide the silent until we get mute support
101            mSilent.setVisibility(View.GONE);
102        }
103
104        CharSequence text;
105
106        // set the keyguard button's text
107        text = context.getText(R.string.screen_lock);
108        mKeyguard.setText(text);
109        mKeyguard.requestFocus();
110
111        try {
112            ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
113            if (phone != null) {
114                text = phone.isRadioOn() ? context
115                        .getText(R.string.turn_off_radio) : context
116                        .getText(R.string.turn_on_radio);
117            }
118        } catch (RemoteException ex) {
119            // ignore it
120        }
121
122        mRadioPower.setText(text);
123    }
124
125    public void onClick(View v) {
126        this.dismiss();
127        if (v == mPower) {
128            // shutdown by making sure radio and power are handled accordingly.
129            ShutdownThread.shutdown(getContext(), true);
130        } else if (v == mRadioPower) {
131            try {
132                ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
133                if (phone != null) {
134                    phone.toggleRadioOnOff();
135                }
136            } catch (RemoteException ex) {
137                // ignore it
138            }
139        } else if (v == mSilent) {
140            // do something
141        } else if (v == mKeyguard) {
142            if (v.isInTouchMode()) {
143                // only in touch mode for the reasons explained in onKey.
144                this.dismiss();
145                mPowerManager.goToSleep(SystemClock.uptimeMillis() + 1);
146            }
147        }
148    }
149
150    public boolean onKey(View v, int keyCode, KeyEvent event) {
151        // The activate keyguard button needs to put the device to sleep on the
152        // key up event. If we try to put it to sleep on the click or down
153        // action
154        // the the up action will cause the device to wake back up.
155
156        // Log.i(TAG, "keyCode: " + keyCode + " action: " + event.getAction());
157        if (keyCode != KeyEvent.KEYCODE_DPAD_CENTER
158                || event.getAction() != KeyEvent.ACTION_UP) {
159            // Log.i(TAG, "getting out of dodge...");
160            return false;
161        }
162
163        // Log.i(TAG, "Clicked mKeyguard! dimissing dialog");
164        this.dismiss();
165        // Log.i(TAG, "onKey: turning off the screen...");
166        // XXX: This is a hack for now
167        mPowerManager.goToSleep(event.getEventTime() + 1);
168        return true;
169    }
170
171    public void show() {
172        super.show();
173        Log.d(TAG, "show... disabling expand");
174        sStatusBar.disable(StatusBarManager.DISABLE_EXPAND);
175    }
176
177    public void dismiss() {
178        super.dismiss();
179        Log.d(TAG, "dismiss... reenabling expand");
180        sStatusBar.disable(StatusBarManager.DISABLE_NONE);
181    }
182}
183