Skip to content

Commit 989f203

Browse files
committed
Merge pull request #1138 from crankycoder/features/1137-no-telephony
1137 - fix crash on tablets
2 parents 6bc0530 + 1863f0e commit 989f203

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

android/src/main/java/org/mozilla/mozstumbler/service/stumblerthread/scanners/ScanManager.java

+37-21
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.location.Location;
1111
import android.os.BatteryManager;
1212
import android.support.v4.content.LocalBroadcastManager;
13+
import android.telephony.TelephonyManager;
1314
import android.util.Log;
1415

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

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

6364
mWifiScanner.start(ActiveOrPassiveStumbling.PASSIVE_STUMBLING);
64-
mCellScanner.start(ActiveOrPassiveStumbling.PASSIVE_STUMBLING);
65+
66+
if (mCellScanner != null) {
67+
mCellScanner.start(ActiveOrPassiveStumbling.PASSIVE_STUMBLING);
68+
}
6569

6670
// how often to flush a leftover bundle to the reports table
6771
// If there is a bundle, and nothing happens for 10sec, then flush it
@@ -75,23 +79,23 @@ public void newPassiveGpsLocation() {
7579
when.setTime(when.getTime() + flushRate_ms);
7680
mPassiveModeFlushTimer = new Timer();
7781
mPassiveModeFlushTimer.schedule(new TimerTask() {
78-
@Override
79-
public void run() {
80-
Intent flush = new Intent(Reporter.ACTION_FLUSH_TO_BUNDLE);
81-
LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(flush);
82-
}
82+
@Override
83+
public void run() {
84+
Intent flush = new Intent(Reporter.ACTION_FLUSH_TO_BUNDLE);
85+
LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(flush);
86+
}
8387
}, when);
8488
}
8589

86-
public synchronized void setPassiveMode(boolean on) {
87-
mStumblingMode = (on)? ActiveOrPassiveStumbling.PASSIVE_STUMBLING :
88-
ActiveOrPassiveStumbling.ACTIVE_STUMBLING;
89-
}
90-
9190
public synchronized boolean isPassiveMode() {
9291
return ActiveOrPassiveStumbling.PASSIVE_STUMBLING == mStumblingMode;
9392
}
9493

94+
public synchronized void setPassiveMode(boolean on) {
95+
mStumblingMode = (on) ? ActiveOrPassiveStumbling.PASSIVE_STUMBLING :
96+
ActiveOrPassiveStumbling.ACTIVE_STUMBLING;
97+
}
98+
9599
public synchronized void startScanning(Context context) {
96100
if (this.isScanning()) {
97101
return;
@@ -101,7 +105,13 @@ public synchronized void startScanning(Context context) {
101105
if (mGPSScanner == null) {
102106
mGPSScanner = new GPSScanner(context, this);
103107
mWifiScanner = new WifiScanner(context);
104-
mCellScanner = new CellScanner(context);
108+
109+
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
110+
if (telephonyManager != null &&
111+
(telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA ||
112+
telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM)) {
113+
mCellScanner = new CellScanner(context);
114+
}
105115
}
106116

107117
if (AppGlobals.isDebug) {
@@ -111,7 +121,11 @@ public synchronized void startScanning(Context context) {
111121
mGPSScanner.start(mStumblingMode);
112122
if (mStumblingMode == ActiveOrPassiveStumbling.ACTIVE_STUMBLING) {
113123
mWifiScanner.start(mStumblingMode);
114-
mCellScanner.start(mStumblingMode);
124+
125+
if (mCellScanner != null) {
126+
mCellScanner.start(mStumblingMode);
127+
}
128+
115129
// in passive mode, these scans are started by passive gps notifications
116130
}
117131
mIsScanning = true;
@@ -128,7 +142,9 @@ public synchronized boolean stopScanning() {
128142

129143
mGPSScanner.stop();
130144
mWifiScanner.stop();
131-
mCellScanner.stop();
145+
if (mCellScanner != null) {
146+
mCellScanner.stop();
147+
}
132148

133149
mIsScanning = false;
134150
return true;
@@ -143,27 +159,27 @@ public synchronized boolean isScanning() {
143159
}
144160

145161
public int getAPCount() {
146-
return (mWifiScanner == null)? 0 : mWifiScanner.getAPCount();
162+
return (mWifiScanner == null) ? 0 : mWifiScanner.getAPCount();
147163
}
148164

149165
public int getVisibleAPCount() {
150-
return (mWifiScanner == null)? 0 :mWifiScanner.getVisibleAPCount();
166+
return (mWifiScanner == null) ? 0 : mWifiScanner.getVisibleAPCount();
151167
}
152168

153169
public int getWifiStatus() {
154-
return (mWifiScanner == null)? 0 : mWifiScanner.getStatus();
170+
return (mWifiScanner == null) ? 0 : mWifiScanner.getStatus();
155171
}
156172

157173
public int getCellInfoCount() {
158-
return (mCellScanner == null)? 0 :mCellScanner.getCellInfoCount();
174+
return (mCellScanner == null) ? 0 : mCellScanner.getCellInfoCount();
159175
}
160176

161177
public int getLocationCount() {
162-
return (mGPSScanner == null)? 0 : mGPSScanner.getLocationCount();
178+
return (mGPSScanner == null) ? 0 : mGPSScanner.getLocationCount();
163179
}
164180

165181
public Location getLocation() {
166-
return (mGPSScanner == null)? new Location("null") : mGPSScanner.getLocation();
182+
return (mGPSScanner == null) ? new Location("null") : mGPSScanner.getLocation();
167183
}
168184

169185
}

android/src/main/java/org/mozilla/mozstumbler/service/stumblerthread/scanners/cellscanner/CellScannerImplementation.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ public class CellScannerImplementation implements CellScanner.CellScannerImpl {
4141
protected boolean mIsStarted;
4242
protected int mPhoneType;
4343
protected final Context mContext;
44-
protected volatile int mSignalStrength;
45-
protected volatile int mCdmaDbm;
44+
45+
protected volatile int mSignalStrength = CellInfo.UNKNOWN_SIGNAL;
46+
protected volatile int mCdmaDbm = CellInfo.UNKNOWN_SIGNAL;
47+
4648

4749
private PhoneStateListener mPhoneStateListener;
4850

@@ -86,17 +88,17 @@ public synchronized void start() {
8688
}
8789

8890
mPhoneType = mTelephonyManager.getPhoneType();
91+
if (mPhoneType == TelephonyManager.PHONE_TYPE_NONE) {
92+
// This is almost certainly a tablet or some other wifi only device.
93+
return;
94+
}
8995

9096
if (mPhoneType != TelephonyManager.PHONE_TYPE_GSM
9197
&& mPhoneType != TelephonyManager.PHONE_TYPE_CDMA) {
9298
throw new UnsupportedOperationException("Unexpected Phone Type: " + mPhoneType);
9399
}
94-
mSignalStrength = CellInfo.UNKNOWN_SIGNAL;
95-
mCdmaDbm = CellInfo.UNKNOWN_SIGNAL;
96-
}
97100

98-
mSignalStrength = CellInfo.UNKNOWN_SIGNAL;
99-
mCdmaDbm = CellInfo.UNKNOWN_SIGNAL;
101+
}
100102

101103
mPhoneStateListener = new PhoneStateListener() {
102104
@Override
@@ -114,7 +116,10 @@ public void onSignalStrengthsChanged(SignalStrength ss) {
114116
@Override
115117
public synchronized void stop() {
116118
mIsStarted = false;
117-
if (mTelephonyManager != null) {
119+
if (mTelephonyManager != null &&
120+
(mPhoneType == TelephonyManager.PHONE_TYPE_GSM ||
121+
mPhoneType == TelephonyManager.PHONE_TYPE_CDMA) &&
122+
mPhoneStateListener != null) {
118123
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
119124
}
120125
mSignalStrength = CellInfo.UNKNOWN_SIGNAL;

0 commit comments

Comments
 (0)