Skip to content
Browse files

add battery percentage widget

  • Loading branch information...
1 parent 377222f commit b2f6d797d2d7685bfbf8a4dc62ab763962868063 @jordancraig jordancraig committed Apr 21, 2012
View
8 core/java/android/provider/Settings.java
@@ -2785,6 +2785,14 @@ public static void setShowGTalkServiceStatus(ContentResolver cr, boolean flag) {
*/
public static final String STATUSBAR_BATTERY_ICON = "statusbar_battery_icon";
+ /**
+ * Battery text
+ *
+ * @hide
+ */
+ public static final String STATUSBAR_BATTERY_TEXT = "statusbar_battery_text";
+
+
/**
* @hide
*/
View
44 packages/SystemUI/res/layout/battery_percent_widget.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- /* apps/common/assets/default/default/skins/StatusBar.xml ** ** Copyright
+ 2011, The Android Open Source Project ** ** Licensed under the Apache License,
+ Version 2.0 (the "License"); ** you may not use this file except in compliance
+ with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0
+ ** ** Unless required by applicable law or agreed to in writing, software
+ ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT
+ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the
+ License for the specific language governing permissions and ** limitations
+ under the License. */ -->
+
+<com.android.systemui.statusbar.policy.BatteryController
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_height="wrap_content"
+ android:layout_width="wrap_content"
+ android:orientation="horizontal">
+ <FrameLayout
+ android:id="@+id/battery_combo"
+ android:layout_height="wrap_content"
+ android:layout_width="wrap_content"
+ android:paddingLeft="4dp">
+
+ <ImageView
+ android:id="@+id/battery"
+ android:layout_height="wrap_content"
+ android:layout_width="wrap_content"
+ android:layout_gravity="right" />
+ <TextView
+ android:id="@+id/battery_text"
+ android:layout_marginRight="4dp"
+ android:paddingBottom="1dp"
+ android:layout_height="wrap_content"
+ android:layout_width="wrap_content"
+ android:layout_gravity="left|bottom"
+ android:textSize="11dp"
+ android:textStyle="bold"
+ android:text="@string/test"
+ android:shadowColor="#FF000000"
+ android:shadowDx="0"
+ android:shadowDy="0"
+ android:shadowRadius="2.5"
+ android:visibility="gone" />
+ </FrameLayout>
+</com.android.systemui.statusbar.policy.BatteryController>
View
16 packages/SystemUI/res/layout/status_bar.xml
@@ -69,19 +69,17 @@
android:layout_height="match_parent"
android:paddingLeft="2dp"
android:orientation="horizontal"
- android:gravity="center"
- >
+ android:gravity="center">
+
<include layout="@layout/signal_cluster_view"
android:id="@+id/signal_cluster"
android:layout_width="wrap_content"
+ android:layout_height="wrap_content"/>
+
+ <include layout="@layout/battery_percent_widget"
+ android:id="@+id/battery_cluster"
android:layout_height="wrap_content"
- />
- <ImageView
- android:id="@+id/battery"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:paddingLeft="4dip"
- />
+ android:layout_width="wrap_content"/>
</LinearLayout>
<com.android.systemui.statusbar.policy.Clock
View
1 packages/SystemUI/res/values/strings.xml
@@ -19,6 +19,7 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- Name of the status bar as seen in the applications info settings page. [CHAR LIMIT=12] -->
<string name="app_label">System UI</string>
+ <string name="test">98</string>
<!-- The text for the button in the notification window-shade that clears
all of the currently visible notifications. [CHAR LIMIT=10]-->
View
7 packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -85,7 +85,6 @@
import com.android.systemui.statusbar.StatusBarIconView;
import com.android.systemui.statusbar.SignalClusterView;
import com.android.systemui.statusbar.policy.DateView;
-import com.android.systemui.statusbar.policy.BatteryController;
import com.android.systemui.statusbar.policy.LocationController;
import com.android.systemui.statusbar.policy.NetworkController;
import com.android.systemui.statusbar.policy.NotificationRowLayout;
@@ -134,7 +133,7 @@
PhoneStatusBarPolicy mIconPolicy;
// These are no longer handled by the policy, because we need custom strategies for them
- BatteryController mBatteryController;
+ //BatteryController mBatteryController;
LocationController mLocationController;
NetworkController mNetworkController;
@@ -352,8 +351,8 @@ protected View makeStatusBarView() {
// Other icons
mLocationController = new LocationController(mContext); // will post a notification
- mBatteryController = new BatteryController(mContext);
- mBatteryController.addIconView((ImageView)sb.findViewById(R.id.battery));
+ //mBatteryController = new BatteryController(mContext);
+ //mBatteryController.addIconView((ImageView)sb.findViewById(R.id.battery));
mNetworkController = new NetworkController(mContext);
final SignalClusterView signalCluster =
(SignalClusterView)sb.findViewById(R.id.signal_cluster);
View
123 packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java
@@ -19,29 +19,60 @@
import java.util.ArrayList;
import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.database.ContentObserver;
import android.os.BatteryManager;
-import android.util.Slog;
+import android.os.Handler;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
import android.widget.ImageView;
+import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.systemui.R;
-public class BatteryController extends BroadcastReceiver {
+public class BatteryController extends LinearLayout {
private static final String TAG = "StatusBar.BatteryController";
private Context mContext;
private ArrayList<ImageView> mIconViews = new ArrayList<ImageView>();
private ArrayList<TextView> mLabelViews = new ArrayList<TextView>();
- public BatteryController(Context context) {
+ private ImageView mBatteryIcon;
+ private TextView mBatteryText;
+ private ViewGroup mBatteryGroup;
+
+ private boolean mShowBatteryText;
+
+ public BatteryController(Context context, AttributeSet attrs) {
+ super(context, attrs);
mContext = context;
+ }
+
+ @Override
+ protected void onAttachedToWindow() {
+ super.onAttachedToWindow();
+ init();
+ mBatteryGroup = (ViewGroup) findViewById(R.id.battery_combo);
+ mBatteryIcon = (ImageView) findViewById(R.id.battery);
+ mBatteryText = (TextView) findViewById(R.id.battery_text);
+ addIconView(mBatteryIcon);
+
+ SettingsObserver settingsObserver = new SettingsObserver(new Handler());
+ settingsObserver.observe();
+ updateSettings(); // to initialize values
+ }
+
+ private void init() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
- context.registerReceiver(this, filter);
+ mContext.registerReceiver(mBatteryBroadcastReceiver, filter);
}
public void addIconView(ImageView v) {
@@ -52,27 +83,71 @@ public void addLabelView(TextView v) {
mLabelViews.add(v);
}
- public void onReceive(Context context, Intent intent) {
- final String action = intent.getAction();
- if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
- final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
- final boolean plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
- final int icon = plugged ? R.drawable.stat_sys_battery_charge
- : R.drawable.stat_sys_battery;
- int N = mIconViews.size();
- for (int i=0; i<N; i++) {
- ImageView v = mIconViews.get(i);
- v.setImageResource(icon);
- v.setImageLevel(level);
- v.setContentDescription(mContext.getString(R.string.accessibility_battery_level,
- level));
- }
- N = mLabelViews.size();
- for (int i=0; i<N; i++) {
- TextView v = mLabelViews.get(i);
- v.setText(mContext.getString(R.string.status_bar_settings_battery_meter_format,
- level));
+ private BroadcastReceiver mBatteryBroadcastReceiver = new BroadcastReceiver() {
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ final String action = intent.getAction();
+ if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
+ final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
+ final boolean plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
+ setBatteryIcon(level, plugged);
}
}
+ };
+
+ private void setBatteryIcon(int level, boolean plugged) {
+ final int icon = plugged ? R.drawable.stat_sys_battery_charge
+ : R.drawable.stat_sys_battery;
+ int N = mIconViews.size();
+ for (int i = 0; i < N; i++) {
+ ImageView v = mIconViews.get(i);
+ v.setImageResource(icon);
+ v.setImageLevel(level);
+ v.setContentDescription(mContext.getString(
+ R.string.accessibility_battery_level,
+ level));
+ }
+ N = mLabelViews.size();
+ for (int i = 0; i < N; i++) {
+ TextView v = mLabelViews.get(i);
+ v.setText(mContext.getString(R.string.status_bar_settings_battery_meter_format,
+ level));
+ }
+
+ // do my stuff here
+ if (mBatteryGroup != null)
+ mBatteryText.setText(Integer.toString(level));
+ }
+
+ class SettingsObserver extends ContentObserver {
+ SettingsObserver(Handler handler) {
+ super(handler);
+ }
+
+ void observe() {
+ ContentResolver resolver = mContext.getContentResolver();
+ resolver.registerContentObserver(Settings.System.getUriFor(
+ Settings.System.STATUSBAR_BATTERY_TEXT), false, this);
+ }
+
+ @Override
+ public void onChange(boolean selfChange) {
+ updateSettings();
+ }
+ }
+
+ private void updateSettings() {
+ // Slog.i(TAG, "updated settings values");
+ ContentResolver cr = mContext.getContentResolver();
+ mShowBatteryText = Settings.System
+ .getInt(cr, Settings.System.STATUSBAR_BATTERY_TEXT, 0) == 1;
+
+ if (mShowBatteryText) {
+ mBatteryText.setVisibility(View.VISIBLE);
+ } else {
+ mBatteryText.setVisibility(View.GONE);
+ }
+
}
}
View
11 packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java
@@ -162,7 +162,7 @@
ViewGroup mPile;
HeightReceiver mHeightReceiver;
- BatteryController mBatteryController;
+ //BatteryController mBatteryController;
BluetoothController mBluetoothController;
LocationController mLocationController;
NetworkController mNetworkController;
@@ -208,9 +208,8 @@ protected void addPanelWindows() {
new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PANEL, mNotificationPanel));
// the battery icon
- mBatteryController.addIconView((ImageView)mNotificationPanel.findViewById(R.id.battery));
- mBatteryController.addLabelView(
- (TextView)mNotificationPanel.findViewById(R.id.battery_text));
+ //mBatteryController.addIconView((ImageView)mNotificationPanel.findViewById(R.id.battery));
+ //mBatteryController.addLabelView((TextView)mNotificationPanel.findViewById(R.id.battery_text));
// Bt
mBluetoothController.addIconView(
@@ -486,8 +485,8 @@ protected View makeStatusBarView() {
// The icons
mLocationController = new LocationController(mContext); // will post a notification
- mBatteryController = new BatteryController(mContext);
- mBatteryController.addIconView((ImageView)sb.findViewById(R.id.battery));
+ //mBatteryController = new BatteryController(mContext);
+ //mBatteryController.addIconView((ImageView)sb.findViewById(R.id.battery));
mBluetoothController = new BluetoothController(mContext);
mBluetoothController.addIconView((ImageView)sb.findViewById(R.id.bluetooth));

0 comments on commit b2f6d79

Please sign in to comment.
Something went wrong with that request. Please try again.