PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/actionslib/src/com/google/android/marvin/commands/impls/SavingPhoneStateListener.java

http://eyes-free.googlecode.com/
Java | 162 lines | 119 code | 21 blank | 22 comment | 62 complexity | 26ee5d345c503189fa29b0ace7150091 MD5 | raw file
  1// Copyright 2011 Google Inc. All Rights Reserved.
  2
  3package com.google.android.marvin.commands.impls;
  4
  5import android.content.Context;
  6import android.telephony.PhoneStateListener;
  7import android.telephony.ServiceState;
  8import android.telephony.SignalStrength;
  9import android.telephony.TelephonyManager;
 10
 11/**
 12 * A phone state listener that saves the latest value to shared preferences.
 13 *
 14 * @author clsimon@google.com (Cheryl Simon)
 15 */
 16public class SavingPhoneStateListener extends PhoneStateListener {
 17    /** The shared signal strength in bars. */
 18    private static int sVoiceLevel = -1;
 19
 20    private final TelephonyManager mTelephonyManager;
 21
 22    /** Whether the phone has service. */
 23    private boolean mInService = true;
 24
 25    public SavingPhoneStateListener(Context context) {
 26        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
 27    }
 28
 29    /**
 30     * @return the most recently set signal strength in bars
 31     */
 32    public static int getVoiceLevel() {
 33        return sVoiceLevel;
 34    }
 35
 36    /**
 37     * Sets the signal strength in bars.
 38     *
 39     * @param level The signal strength in bars.
 40     */
 41    private static void setVoiceLevel(int level) {
 42        sVoiceLevel = level;
 43    }
 44
 45    @Override
 46    public void onServiceStateChanged(ServiceState service) {
 47        if (service.getState() != ServiceState.STATE_IN_SERVICE) {
 48            mInService = false;
 49        } else {
 50            mInService = true;
 51        }
 52    }
 53
 54    @Override
 55    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
 56        final int voiceSignalStrength = signalStrengthToBars(signalStrength);
 57
 58        setVoiceLevel(voiceSignalStrength);
 59    }
 60
 61    /**
 62     * Computes a signal strength in bars from a {@link SignalStrength} object.
 63     *
 64     * @param signalStrength The detected signal strength.
 65     * @return the signal strength in bars
 66     */
 67    private int signalStrengthToBars(SignalStrength signalStrength) {
 68        if (signalStrength.isGsm()) {
 69            return gsmSignalStrengthToBars(signalStrength.getGsmSignalStrength());
 70        } else if (mTelephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_CDMA) {
 71            return cdmaStrengthToBars(signalStrength.getCdmaDbm(), signalStrength.getCdmaEcio());
 72        } else {
 73            return evdoStrengthToBars(signalStrength.getEvdoDbm(), signalStrength.getEvdoSnr());
 74        }
 75    }
 76
 77    private int gsmSignalStrengthToBars(int strength) {
 78        if ((strength == -1) || !mInService) {
 79            return -1;
 80        } else if (strength <= 0 || strength == 99) {
 81            return 0;
 82        } else if (strength >= 16) {
 83            return 4;
 84        } else if (strength >= 8) {
 85            return 3;
 86        } else if (strength >= 4) {
 87            return 2;
 88        } else {
 89            return 1;
 90        }
 91    }
 92
 93    private int cdmaStrengthToBars(int dbm, int ecio) {
 94        int dbmLevel;
 95        if (!mInService) {
 96            dbmLevel = -1;
 97        } else if (dbm >= -75) {
 98            dbmLevel = 4;
 99        } else if (dbm >= -85) {
100            dbmLevel = 3;
101        } else if (dbm >= -95) {
102            dbmLevel = 2;
103        } else if (dbm >= -100) {
104            dbmLevel = 1;
105        } else {
106            dbmLevel = -1;
107        }
108
109        int ecioLevel;
110
111        if (!mInService) {
112            ecioLevel = -1;
113        } else if (ecio >= -90) {
114            ecioLevel = 4;
115        } else if (ecio >= -110) {
116            ecioLevel = 3;
117        } else if (ecio >= -130) {
118            ecioLevel = 2;
119        } else if (ecio >= -150) {
120            ecioLevel = 1;
121        } else {
122            ecioLevel = -1;
123        }
124
125        return dbmLevel < ecioLevel ? dbmLevel : ecioLevel;
126    }
127
128    private int evdoStrengthToBars(int dbm, int snr) {
129        int dbmLevel;
130        if (!mInService) {
131            dbmLevel = -1;
132        } else if (dbm >= -65) {
133            dbmLevel = 4;
134        } else if (dbm >= -75) {
135            dbmLevel = 3;
136        } else if (dbm >= -90) {
137            dbmLevel = 2;
138        } else if (dbm >= -105) {
139            dbmLevel = 1;
140        } else {
141            dbmLevel = -1;
142        }
143
144        int snrLevel;
145        if (!mInService) {
146            snrLevel = -1;
147        } else if (snr >= 7) {
148            snrLevel = 4;
149        } else if (snr >= 5) {
150            snrLevel = 3;
151        } else if (snr >= 3) {
152            snrLevel = 2;
153        } else if (snr >= 1) {
154            snrLevel = 1;
155        } else {
156            snrLevel = -1;
157        }
158
159        return dbmLevel < snrLevel ? dbmLevel : snrLevel;
160    }
161
162}