PowerUI.java revision 10523b4d0c99cec86647130426d470a1e02a44f6
1/*
2 * Copyright (C) 2008 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.systemui.power;
18
19import java.io.FileDescriptor;
20import java.io.PrintWriter;
21import java.util.Arrays;
22
23import android.app.AlertDialog;
24import android.content.BroadcastReceiver;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.DialogInterface;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.net.Uri;
31import android.os.Handler;
32import android.media.AudioManager;
33import android.media.Ringtone;
34import android.media.RingtoneManager;
35import android.provider.Settings;
36import android.util.Slog;
37import android.view.View;
38import android.view.WindowManager;
39import android.widget.TextView;
40
41import com.android.systemui.R;
42import com.android.systemui.SystemUI;
43
44public class PowerUI extends SystemUI {
45    static final String TAG = "PowerUI";
46
47    Handler mHandler = new Handler();
48
49    AlertDialog mLowBatteryDialog;
50    int mBatteryLevel;
51    TextView mBatteryLevelTextView;
52
53    public void start() {
54        // Register for Intent broadcasts for...
55        IntentFilter filter = new IntentFilter();
56        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
57        filter.addAction(Intent.ACTION_BATTERY_LOW);
58        filter.addAction(Intent.ACTION_BATTERY_OKAY);
59        filter.addAction(Intent.ACTION_POWER_CONNECTED);
60        mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
61    }
62
63    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
64        @Override
65        public void onReceive(Context context, Intent intent) {
66            String action = intent.getAction();
67            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
68                mBatteryLevel = intent.getIntExtra("level", -1);
69            } else if (action.equals(Intent.ACTION_BATTERY_LOW)) {
70                showLowBatteryWarning();
71            } else if (action.equals(Intent.ACTION_BATTERY_OKAY)
72                    || action.equals(Intent.ACTION_POWER_CONNECTED)) {
73                if (mLowBatteryDialog != null) {
74                    mLowBatteryDialog.dismiss();
75                }
76            } else {
77                Slog.w(TAG, "unknown intent: " + intent);
78            }
79        }
80    };
81
82    void showLowBatteryWarning() {
83        CharSequence levelText = mContext.getString(
84                R.string.battery_low_percent_format, mBatteryLevel);
85
86        if (mBatteryLevelTextView != null) {
87            mBatteryLevelTextView.setText(levelText);
88        } else {
89            View v = View.inflate(mContext, R.layout.battery_low, null);
90            mBatteryLevelTextView = (TextView)v.findViewById(R.id.level_percent);
91
92            mBatteryLevelTextView.setText(levelText);
93
94            AlertDialog.Builder b = new AlertDialog.Builder(mContext);
95                b.setCancelable(true);
96                b.setTitle(R.string.battery_low_title);
97                b.setView(v);
98                b.setIcon(android.R.drawable.ic_dialog_alert);
99                b.setPositiveButton(android.R.string.ok, null);
100
101                final Intent intent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
102                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
103                        | Intent.FLAG_ACTIVITY_MULTIPLE_TASK
104                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
105                        | Intent.FLAG_ACTIVITY_NO_HISTORY);
106                if (intent.resolveActivity(mContext.getPackageManager()) != null) {
107                    b.setNegativeButton(R.string.battery_low_why,
108                            new DialogInterface.OnClickListener() {
109                        public void onClick(DialogInterface dialog, int which) {
110                            mContext.startActivity(intent);
111                            if (mLowBatteryDialog != null) {
112                                mLowBatteryDialog.dismiss();
113                            }
114                        }
115                    });
116                }
117
118            AlertDialog d = b.create();
119            d.setOnDismissListener(mLowBatteryListener);
120            d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
121            d.show();
122            mLowBatteryDialog = d;
123        }
124
125        final ContentResolver cr = mContext.getContentResolver();
126        if (Settings.System.getInt(cr, Settings.System.POWER_SOUNDS_ENABLED, 1) == 1) {
127            final String soundPath = Settings.System.getString(cr,
128                    Settings.System.LOW_BATTERY_SOUND);
129            if (soundPath != null) {
130                final Uri soundUri = Uri.parse("file://" + soundPath);
131                if (soundUri != null) {
132                    final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
133                    if (sfx != null) {
134                        sfx.setStreamType(AudioManager.STREAM_SYSTEM);
135                        sfx.play();
136                    }
137                }
138            }
139        }
140    }
141
142    private DialogInterface.OnDismissListener mLowBatteryListener
143            = new DialogInterface.OnDismissListener() {
144        public void onDismiss(DialogInterface dialog) {
145            mLowBatteryDialog = null;
146            mBatteryLevelTextView = null;
147        }
148    };
149
150
151    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
152        if (false) {
153            pw.println("args=" + Arrays.toString(args));
154        }
155        if (args == null || args.length == 0) {
156            pw.print("mLowBatteryDialog=");
157            pw.println(mLowBatteryDialog == null ? "null" : mLowBatteryDialog.toString());
158            pw.print("mBatteryLevel=");
159            pw.println(Integer.toString(mBatteryLevel));
160        }
161
162        // DO NOT SUBMIT with this turned on.
163        if (false) {
164            if (args.length == 3 && "level".equals(args[1])) {
165                try {
166                    final int level = Integer.parseInt(args[2]);
167                    Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
168                    intent.putExtra("level", level);
169                    mIntentReceiver.onReceive(mContext, intent);
170                } catch (NumberFormatException ex) {
171                    pw.println(ex);
172                }
173            } else if (args.length == 2 && "low".equals(args[1])) {
174                Intent intent = new Intent(Intent.ACTION_BATTERY_LOW);
175                mIntentReceiver.onReceive(mContext, intent);
176            } else if (args.length == 2 && "ok".equals(args[1])) {
177                Intent intent = new Intent(Intent.ACTION_BATTERY_OKAY);
178                mIntentReceiver.onReceive(mContext, intent);
179            }
180        }
181    }
182}
183
184