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