Skip to content

Commit

Permalink
Merge pull request #1138 from crankycoder/features/1137-no-telephony
Browse files Browse the repository at this point in the history
1137 - fix crash on tablets
  • Loading branch information
crankycoder committed Oct 28, 2014
2 parents 6bc0530 + 1863f0e commit 989f203
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.location.Location;
import android.os.BatteryManager;
import android.support.v4.content.LocalBroadcastManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import org.mozilla.mozstumbler.service.AppGlobals;
Expand Down Expand Up @@ -45,7 +46,7 @@ private boolean isBatteryLow() {
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING);
int level = Math.round(rawLevel * scale/100.0f);
int level = Math.round(rawLevel * scale / 100.0f);

final int kMinBatteryPct = 15;
return !isCharging && level < kMinBatteryPct;
Expand All @@ -61,7 +62,10 @@ public void newPassiveGpsLocation() {
}

mWifiScanner.start(ActiveOrPassiveStumbling.PASSIVE_STUMBLING);
mCellScanner.start(ActiveOrPassiveStumbling.PASSIVE_STUMBLING);

if (mCellScanner != null) {
mCellScanner.start(ActiveOrPassiveStumbling.PASSIVE_STUMBLING);
}

// how often to flush a leftover bundle to the reports table
// If there is a bundle, and nothing happens for 10sec, then flush it
Expand All @@ -75,23 +79,23 @@ public void newPassiveGpsLocation() {
when.setTime(when.getTime() + flushRate_ms);
mPassiveModeFlushTimer = new Timer();
mPassiveModeFlushTimer.schedule(new TimerTask() {
@Override
public void run() {
Intent flush = new Intent(Reporter.ACTION_FLUSH_TO_BUNDLE);
LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(flush);
}
@Override
public void run() {
Intent flush = new Intent(Reporter.ACTION_FLUSH_TO_BUNDLE);
LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(flush);
}
}, when);
}

public synchronized void setPassiveMode(boolean on) {
mStumblingMode = (on)? ActiveOrPassiveStumbling.PASSIVE_STUMBLING :
ActiveOrPassiveStumbling.ACTIVE_STUMBLING;
}

public synchronized boolean isPassiveMode() {
return ActiveOrPassiveStumbling.PASSIVE_STUMBLING == mStumblingMode;
}

public synchronized void setPassiveMode(boolean on) {
mStumblingMode = (on) ? ActiveOrPassiveStumbling.PASSIVE_STUMBLING :
ActiveOrPassiveStumbling.ACTIVE_STUMBLING;
}

public synchronized void startScanning(Context context) {
if (this.isScanning()) {
return;
Expand All @@ -101,7 +105,13 @@ public synchronized void startScanning(Context context) {
if (mGPSScanner == null) {
mGPSScanner = new GPSScanner(context, this);
mWifiScanner = new WifiScanner(context);
mCellScanner = new CellScanner(context);

TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null &&
(telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA ||
telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM)) {
mCellScanner = new CellScanner(context);
}
}

if (AppGlobals.isDebug) {
Expand All @@ -111,7 +121,11 @@ public synchronized void startScanning(Context context) {
mGPSScanner.start(mStumblingMode);
if (mStumblingMode == ActiveOrPassiveStumbling.ACTIVE_STUMBLING) {
mWifiScanner.start(mStumblingMode);
mCellScanner.start(mStumblingMode);

if (mCellScanner != null) {
mCellScanner.start(mStumblingMode);
}

// in passive mode, these scans are started by passive gps notifications
}
mIsScanning = true;
Expand All @@ -128,7 +142,9 @@ public synchronized boolean stopScanning() {

mGPSScanner.stop();
mWifiScanner.stop();
mCellScanner.stop();
if (mCellScanner != null) {
mCellScanner.stop();
}

mIsScanning = false;
return true;
Expand All @@ -143,27 +159,27 @@ public synchronized boolean isScanning() {
}

public int getAPCount() {
return (mWifiScanner == null)? 0 : mWifiScanner.getAPCount();
return (mWifiScanner == null) ? 0 : mWifiScanner.getAPCount();
}

public int getVisibleAPCount() {
return (mWifiScanner == null)? 0 :mWifiScanner.getVisibleAPCount();
return (mWifiScanner == null) ? 0 : mWifiScanner.getVisibleAPCount();
}

public int getWifiStatus() {
return (mWifiScanner == null)? 0 : mWifiScanner.getStatus();
return (mWifiScanner == null) ? 0 : mWifiScanner.getStatus();
}

public int getCellInfoCount() {
return (mCellScanner == null)? 0 :mCellScanner.getCellInfoCount();
return (mCellScanner == null) ? 0 : mCellScanner.getCellInfoCount();
}

public int getLocationCount() {
return (mGPSScanner == null)? 0 : mGPSScanner.getLocationCount();
return (mGPSScanner == null) ? 0 : mGPSScanner.getLocationCount();
}

public Location getLocation() {
return (mGPSScanner == null)? new Location("null") : mGPSScanner.getLocation();
return (mGPSScanner == null) ? new Location("null") : mGPSScanner.getLocation();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public class CellScannerImplementation implements CellScanner.CellScannerImpl {
protected boolean mIsStarted;
protected int mPhoneType;
protected final Context mContext;
protected volatile int mSignalStrength;
protected volatile int mCdmaDbm;

protected volatile int mSignalStrength = CellInfo.UNKNOWN_SIGNAL;
protected volatile int mCdmaDbm = CellInfo.UNKNOWN_SIGNAL;


private PhoneStateListener mPhoneStateListener;

Expand Down Expand Up @@ -86,17 +88,17 @@ public synchronized void start() {
}

mPhoneType = mTelephonyManager.getPhoneType();
if (mPhoneType == TelephonyManager.PHONE_TYPE_NONE) {
// This is almost certainly a tablet or some other wifi only device.
return;
}

if (mPhoneType != TelephonyManager.PHONE_TYPE_GSM
&& mPhoneType != TelephonyManager.PHONE_TYPE_CDMA) {
throw new UnsupportedOperationException("Unexpected Phone Type: " + mPhoneType);
}
mSignalStrength = CellInfo.UNKNOWN_SIGNAL;
mCdmaDbm = CellInfo.UNKNOWN_SIGNAL;
}

mSignalStrength = CellInfo.UNKNOWN_SIGNAL;
mCdmaDbm = CellInfo.UNKNOWN_SIGNAL;
}

mPhoneStateListener = new PhoneStateListener() {
@Override
Expand All @@ -114,7 +116,10 @@ public void onSignalStrengthsChanged(SignalStrength ss) {
@Override
public synchronized void stop() {
mIsStarted = false;
if (mTelephonyManager != null) {
if (mTelephonyManager != null &&
(mPhoneType == TelephonyManager.PHONE_TYPE_GSM ||
mPhoneType == TelephonyManager.PHONE_TYPE_CDMA) &&
mPhoneStateListener != null) {
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
mSignalStrength = CellInfo.UNKNOWN_SIGNAL;
Expand Down

0 comments on commit 989f203

Please sign in to comment.