PowerUI.java revision c597ba8e9ad08ac628271b4498c27eda74c6c86c
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 125if (false) { // getRingtone ANRs 126 final ContentResolver cr = mContext.getContentResolver(); 127 if (Settings.System.getInt(cr, Settings.System.POWER_SOUNDS_ENABLED, 1) == 1) { 128 final String soundPath = Settings.System.getString(cr, 129 Settings.System.LOW_BATTERY_SOUND); 130 if (soundPath != null) { 131 final Uri soundUri = Uri.parse("file://" + soundPath); 132 if (soundUri != null) { 133 final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri); 134 if (sfx != null) { 135 sfx.setStreamType(AudioManager.STREAM_SYSTEM); 136 sfx.play(); 137 } 138 } 139 } 140 } 141} 142 } 143 144 private DialogInterface.OnDismissListener mLowBatteryListener 145 = new DialogInterface.OnDismissListener() { 146 public void onDismiss(DialogInterface dialog) { 147 mLowBatteryDialog = null; 148 mBatteryLevelTextView = null; 149 } 150 }; 151 152 153 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 154 if (false) { 155 pw.println("args=" + Arrays.toString(args)); 156 } 157 if (args == null || args.length == 0) { 158 pw.print("mLowBatteryDialog="); 159 pw.println(mLowBatteryDialog == null ? "null" : mLowBatteryDialog.toString()); 160 pw.print("mBatteryLevel="); 161 pw.println(Integer.toString(mBatteryLevel)); 162 } 163 164 // DO NOT SUBMIT with this turned on. 165 if (false) { 166 if (args.length == 3 && "level".equals(args[1])) { 167 try { 168 final int level = Integer.parseInt(args[2]); 169 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 170 intent.putExtra("level", level); 171 mIntentReceiver.onReceive(mContext, intent); 172 } catch (NumberFormatException ex) { 173 pw.println(ex); 174 } 175 } else if (args.length == 2 && "low".equals(args[1])) { 176 Intent intent = new Intent(Intent.ACTION_BATTERY_LOW); 177 mIntentReceiver.onReceive(mContext, intent); 178 } else if (args.length == 2 && "ok".equals(args[1])) { 179 Intent intent = new Intent(Intent.ACTION_BATTERY_OKAY); 180 mIntentReceiver.onReceive(mContext, intent); 181 } 182 } 183 } 184} 185 186