Home | History | Annotate | Download | only in am
      1 /*
      2  * Copyright (C) 2006-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 
     17 package com.android.server.am;
     18 
     19 import android.bluetooth.BluetoothHeadset;
     20 import android.content.Context;
     21 import android.os.Binder;
     22 import android.os.IBinder;
     23 import android.os.Parcel;
     24 import android.os.Process;
     25 import android.os.ServiceManager;
     26 import android.telephony.SignalStrength;
     27 import android.util.Slog;
     28 
     29 import com.android.internal.app.IBatteryStats;
     30 import com.android.internal.os.BatteryStatsImpl;
     31 import com.android.internal.os.PowerProfile;
     32 
     33 import java.io.FileDescriptor;
     34 import java.io.PrintWriter;
     35 
     36 /**
     37  * All information we are collecting about things that can happen that impact
     38  * battery life.
     39  */
     40 public final class BatteryStatsService extends IBatteryStats.Stub {
     41     static IBatteryStats sService;
     42 
     43     final BatteryStatsImpl mStats;
     44     Context mContext;
     45 
     46     BatteryStatsService(String filename) {
     47         mStats = new BatteryStatsImpl(filename);
     48     }
     49 
     50     public void publish(Context context) {
     51         mContext = context;
     52         ServiceManager.addService("batteryinfo", asBinder());
     53         mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps());
     54         mStats.setRadioScanningTimeout(mContext.getResources().getInteger(
     55                 com.android.internal.R.integer.config_radioScanningTimeout)
     56                 * 1000L);
     57     }
     58 
     59     public void shutdown() {
     60         Slog.w("BatteryStats", "Writing battery stats before shutdown...");
     61         synchronized (mStats) {
     62             mStats.writeLocked();
     63         }
     64     }
     65 
     66     public static IBatteryStats getService() {
     67         if (sService != null) {
     68             return sService;
     69         }
     70         IBinder b = ServiceManager.getService("batteryinfo");
     71         sService = asInterface(b);
     72         return sService;
     73     }
     74 
     75     /**
     76      * @return the current statistics object, which may be modified
     77      * to reflect events that affect battery usage.  You must lock the
     78      * stats object before doing anything with it.
     79      */
     80     public BatteryStatsImpl getActiveStatistics() {
     81         return mStats;
     82     }
     83 
     84     public byte[] getStatistics() {
     85         mContext.enforceCallingPermission(
     86                 android.Manifest.permission.BATTERY_STATS, null);
     87         //Slog.i("foo", "SENDING BATTERY INFO:");
     88         //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
     89         Parcel out = Parcel.obtain();
     90         mStats.writeToParcel(out, 0);
     91         byte[] data = out.marshall();
     92         out.recycle();
     93         return data;
     94     }
     95 
     96     public void noteStartWakelock(int uid, String name, int type) {
     97         enforceCallingPermission();
     98         synchronized (mStats) {
     99             mStats.getUidStatsLocked(uid).noteStartWakeLocked(name, type);
    100         }
    101     }
    102 
    103     public void noteStopWakelock(int uid, String name, int type) {
    104         enforceCallingPermission();
    105         synchronized (mStats) {
    106             mStats.getUidStatsLocked(uid).noteStopWakeLocked(name, type);
    107         }
    108     }
    109 
    110     public void noteStartSensor(int uid, int sensor) {
    111         enforceCallingPermission();
    112         synchronized (mStats) {
    113             mStats.getUidStatsLocked(uid).noteStartSensor(sensor);
    114         }
    115     }
    116 
    117     public void noteStopSensor(int uid, int sensor) {
    118         enforceCallingPermission();
    119         synchronized (mStats) {
    120             mStats.getUidStatsLocked(uid).noteStopSensor(sensor);
    121         }
    122     }
    123 
    124     public void noteStartGps(int uid) {
    125         enforceCallingPermission();
    126         synchronized (mStats) {
    127             mStats.noteStartGps(uid);
    128         }
    129     }
    130 
    131     public void noteStopGps(int uid) {
    132         enforceCallingPermission();
    133         synchronized (mStats) {
    134             mStats.noteStopGps(uid);
    135         }
    136     }
    137 
    138     public void noteScreenOn() {
    139         enforceCallingPermission();
    140         synchronized (mStats) {
    141             mStats.noteScreenOnLocked();
    142         }
    143     }
    144 
    145     public void noteScreenBrightness(int brightness) {
    146         enforceCallingPermission();
    147         synchronized (mStats) {
    148             mStats.noteScreenBrightnessLocked(brightness);
    149         }
    150     }
    151 
    152     public void noteScreenOff() {
    153         enforceCallingPermission();
    154         synchronized (mStats) {
    155             mStats.noteScreenOffLocked();
    156         }
    157     }
    158 
    159     public void noteInputEvent() {
    160         enforceCallingPermission();
    161         mStats.noteInputEventAtomic();
    162     }
    163 
    164     public void noteUserActivity(int uid, int event) {
    165         enforceCallingPermission();
    166         synchronized (mStats) {
    167             mStats.noteUserActivityLocked(uid, event);
    168         }
    169     }
    170 
    171     public void notePhoneOn() {
    172         enforceCallingPermission();
    173         synchronized (mStats) {
    174             mStats.notePhoneOnLocked();
    175         }
    176     }
    177 
    178     public void notePhoneOff() {
    179         enforceCallingPermission();
    180         synchronized (mStats) {
    181             mStats.notePhoneOffLocked();
    182         }
    183     }
    184 
    185     public void notePhoneSignalStrength(SignalStrength signalStrength) {
    186         enforceCallingPermission();
    187         synchronized (mStats) {
    188             mStats.notePhoneSignalStrengthLocked(signalStrength);
    189         }
    190     }
    191 
    192     public void notePhoneDataConnectionState(int dataType, boolean hasData) {
    193         enforceCallingPermission();
    194         synchronized (mStats) {
    195             mStats.notePhoneDataConnectionStateLocked(dataType, hasData);
    196         }
    197     }
    198 
    199     public void notePhoneState(int state) {
    200         enforceCallingPermission();
    201         synchronized (mStats) {
    202             mStats.notePhoneStateLocked(state);
    203         }
    204     }
    205 
    206     public void noteWifiOn(int uid) {
    207         enforceCallingPermission();
    208         synchronized (mStats) {
    209             mStats.noteWifiOnLocked(uid);
    210         }
    211     }
    212 
    213     public void noteWifiOff(int uid) {
    214         enforceCallingPermission();
    215         synchronized (mStats) {
    216             mStats.noteWifiOffLocked(uid);
    217         }
    218     }
    219 
    220     public void noteStartAudio(int uid) {
    221         enforceCallingPermission();
    222         synchronized (mStats) {
    223             mStats.noteAudioOnLocked(uid);
    224         }
    225     }
    226 
    227     public void noteStopAudio(int uid) {
    228         enforceCallingPermission();
    229         synchronized (mStats) {
    230             mStats.noteAudioOffLocked(uid);
    231         }
    232     }
    233 
    234     public void noteStartVideo(int uid) {
    235         enforceCallingPermission();
    236         synchronized (mStats) {
    237             mStats.noteVideoOnLocked(uid);
    238         }
    239     }
    240 
    241     public void noteStopVideo(int uid) {
    242         enforceCallingPermission();
    243         synchronized (mStats) {
    244             mStats.noteVideoOffLocked(uid);
    245         }
    246     }
    247 
    248     public void noteWifiRunning() {
    249         enforceCallingPermission();
    250         synchronized (mStats) {
    251             mStats.noteWifiRunningLocked();
    252         }
    253     }
    254 
    255     public void noteWifiStopped() {
    256         enforceCallingPermission();
    257         synchronized (mStats) {
    258             mStats.noteWifiStoppedLocked();
    259         }
    260     }
    261 
    262     public void noteBluetoothOn() {
    263         enforceCallingPermission();
    264         BluetoothHeadset headset = new BluetoothHeadset(mContext, null);
    265         synchronized (mStats) {
    266             mStats.noteBluetoothOnLocked();
    267             mStats.setBtHeadset(headset);
    268         }
    269     }
    270 
    271     public void noteBluetoothOff() {
    272         enforceCallingPermission();
    273         synchronized (mStats) {
    274             mStats.noteBluetoothOffLocked();
    275         }
    276     }
    277 
    278     public void noteFullWifiLockAcquired(int uid) {
    279         enforceCallingPermission();
    280         synchronized (mStats) {
    281             mStats.noteFullWifiLockAcquiredLocked(uid);
    282         }
    283     }
    284 
    285     public void noteFullWifiLockReleased(int uid) {
    286         enforceCallingPermission();
    287         synchronized (mStats) {
    288             mStats.noteFullWifiLockReleasedLocked(uid);
    289         }
    290     }
    291 
    292     public void noteScanWifiLockAcquired(int uid) {
    293         enforceCallingPermission();
    294         synchronized (mStats) {
    295             mStats.noteScanWifiLockAcquiredLocked(uid);
    296         }
    297     }
    298 
    299     public void noteScanWifiLockReleased(int uid) {
    300         enforceCallingPermission();
    301         synchronized (mStats) {
    302             mStats.noteScanWifiLockReleasedLocked(uid);
    303         }
    304     }
    305 
    306     public void noteWifiMulticastEnabled(int uid) {
    307         enforceCallingPermission();
    308         synchronized (mStats) {
    309             mStats.noteWifiMulticastEnabledLocked(uid);
    310         }
    311     }
    312 
    313     public void noteWifiMulticastDisabled(int uid) {
    314         enforceCallingPermission();
    315         synchronized (mStats) {
    316             mStats.noteWifiMulticastDisabledLocked(uid);
    317         }
    318     }
    319 
    320     public boolean isOnBattery() {
    321         return mStats.isOnBattery();
    322     }
    323 
    324     public void setOnBattery(boolean onBattery, int level) {
    325         enforceCallingPermission();
    326         mStats.setOnBattery(onBattery, level);
    327     }
    328 
    329     public void recordCurrentLevel(int level) {
    330         enforceCallingPermission();
    331         mStats.recordCurrentLevel(level);
    332     }
    333 
    334     public long getAwakeTimeBattery() {
    335         mContext.enforceCallingOrSelfPermission(
    336                 android.Manifest.permission.BATTERY_STATS, null);
    337         return mStats.getAwakeTimeBattery();
    338     }
    339 
    340     public long getAwakeTimePlugged() {
    341         mContext.enforceCallingOrSelfPermission(
    342                 android.Manifest.permission.BATTERY_STATS, null);
    343         return mStats.getAwakeTimePlugged();
    344     }
    345 
    346     public void enforceCallingPermission() {
    347         if (Binder.getCallingPid() == Process.myPid()) {
    348             return;
    349         }
    350         mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
    351                 Binder.getCallingPid(), Binder.getCallingUid(), null);
    352     }
    353 
    354     @Override
    355     protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
    356         synchronized (mStats) {
    357             boolean isCheckin = false;
    358             if (args != null) {
    359                 for (String arg : args) {
    360                     if ("--checkin".equals(arg)) {
    361                         isCheckin = true;
    362                         break;
    363                     }
    364                 }
    365             }
    366             if (isCheckin) mStats.dumpCheckinLocked(pw, args);
    367             else mStats.dumpLocked(pw);
    368         }
    369     }
    370 }
    371