|
1 | 1 | package com.lcl.lclmeasurementtool.Managers;
|
2 | 2 | import android.content.Context;
|
| 3 | +import android.os.Looper; |
| 4 | +import android.telephony.CellSignalStrength; |
| 5 | +import android.telephony.CellSignalStrengthLte; |
| 6 | +import android.telephony.PhoneStateListener; |
3 | 7 | import android.telephony.SignalStrength;
|
4 | 8 | import android.telephony.TelephonyManager;
|
| 9 | +import android.util.Log; |
| 10 | +import android.view.View; |
| 11 | +import android.widget.TextView; |
| 12 | + |
| 13 | +import androidx.annotation.NonNull; |
5 | 14 |
|
6 | 15 | import com.lcl.lclmeasurementtool.Utils.SignalStrengthLevel;
|
7 | 16 |
|
| 17 | +import java.util.List; |
| 18 | + |
8 | 19 | /**
|
9 | 20 | * CellularManager monitors changes in device's signal strength and
|
10 | 21 | * report changes(callback) to front-end UI
|
| 22 | + * @see <a href="https://developer.android.com/reference/android/telephony/CellSignalStrengthLte">CellSignalStrengthLte</a> |
11 | 23 | */
|
12 |
| - |
13 | 24 | public class CellularManager {
|
14 | 25 |
|
15 |
| - private TelephonyManager telephonyManager; |
| 26 | + // LOG TAG constant |
| 27 | + static final String TAG = "CELLULAR_MANAGER_TAG"; |
| 28 | + |
| 29 | + private static CellularManager cellularManager = null; |
| 30 | + |
| 31 | + // the telephony manager that manages all access related to cellular information. |
| 32 | + private final TelephonyManager telephonyManager; |
| 33 | + |
| 34 | + // the signal strength object that stores the information |
| 35 | + // retrieved from the system by the time the object is accessed. |
| 36 | + private final SignalStrength signalStrength; |
16 | 37 |
|
17 |
| - public CellularManager(Context context) { |
| 38 | + // the LTE signal strength report that consists of all cellular signal strength information. |
| 39 | + private final CellSignalStrengthLte report; |
| 40 | + |
| 41 | + // the flag that controls when to stop listening to signal strength change. |
| 42 | + private boolean stopListening; |
| 43 | + |
| 44 | + /** |
| 45 | + * Construct a new CellularManager object based on current context. |
| 46 | + * @param context the context of the application. |
| 47 | + */ |
| 48 | + private CellularManager(@NonNull Context context) { |
18 | 49 | this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
| 50 | + this.signalStrength = this.telephonyManager.getSignalStrength(); |
| 51 | + if (this.signalStrength.getCellSignalStrengths(CellSignalStrengthLte.class).size() > 0) { |
| 52 | + this.report = this.signalStrength.getCellSignalStrengths(CellSignalStrengthLte.class).get(0); |
| 53 | + } else { |
| 54 | + this.report = null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Retrieve the cellular manager object from current context. |
| 60 | + * @return a cellular manager |
| 61 | + */ |
| 62 | + public static CellularManager getManager(@NonNull Context context) { |
| 63 | + return cellularManager == null ? new CellularManager(context) : cellularManager; |
19 | 64 | }
|
20 | 65 |
|
| 66 | + /** |
| 67 | + * Retrieve the signal strength object from current context. |
| 68 | + * @return a signal strength object. |
| 69 | + */ |
21 | 70 | public SignalStrength getSignalStrength() {
|
22 |
| - return this.telephonyManager.getSignalStrength(); |
| 71 | + return this.signalStrength; |
23 | 72 | }
|
24 | 73 |
|
| 74 | + /** |
| 75 | + * Retrieve the signalStrengthLevel Enum from current context. |
| 76 | + * @return a corresponding signal strength level from the current context. |
| 77 | + */ |
25 | 78 | public SignalStrengthLevel getSignalStrengthLevel() {
|
26 |
| - int level = getSignalStrength().getLevel(); |
27 |
| - return SignalStrengthLevel.init(level); |
| 79 | + if (report != null) { |
| 80 | + int level = report.getLevel(); |
| 81 | + return SignalStrengthLevel.init(level); |
| 82 | + } |
| 83 | + |
| 84 | + return SignalStrengthLevel.NONE; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Retrieve the CellSignalStrength report. |
| 89 | + * @return a CellSignalStrength object that |
| 90 | + * contains all information related to cellular signal strength. |
| 91 | + * report might be null if no cellular connection. |
| 92 | + */ |
| 93 | + public CellSignalStrength getCellSignalStrength() { |
| 94 | + return report; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Retrieve the signal Strength in dBm. |
| 99 | + * @return an integer of signal strength in dBm. |
| 100 | + * If no cellular connection, 0. |
| 101 | + */ |
| 102 | + public int getDBM() { |
| 103 | + return report != null ? report.getDbm() : 0; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Start listen to signal strength change and display onto the corresponding TextView. |
| 108 | + * |
| 109 | + * @param textView the text view that will be used to display the signal strength data. |
| 110 | + */ |
| 111 | + public void listenToSignalStrengthChange(TextView textView) { |
| 112 | + new Thread(new Runnable() { |
| 113 | + @Override |
| 114 | + public void run() { |
| 115 | + stopListening = false; |
| 116 | + Looper.prepare(); |
| 117 | + |
| 118 | + telephonyManager.listen(new PhoneStateListener() { |
| 119 | + @Override |
| 120 | + public void onSignalStrengthsChanged(SignalStrength signalStrength) { |
| 121 | + super.onSignalStrengthsChanged(signalStrength); |
| 122 | + List<CellSignalStrengthLte> reports = signalStrength |
| 123 | + .getCellSignalStrengths(CellSignalStrengthLte.class); |
| 124 | + |
| 125 | + String text; |
| 126 | + if (reports.size() > 0) { |
| 127 | + CellSignalStrengthLte report = reports.get(0); |
| 128 | + text = report.getDbm() + " " + report.getLevel(); |
| 129 | + } else { |
| 130 | + text = SignalStrengthLevel.NONE.getName(); |
| 131 | + } |
| 132 | + textView.setText(text); |
| 133 | + |
| 134 | + if (stopListening) { |
| 135 | + Looper.myLooper().quit(); |
| 136 | + } |
| 137 | + } |
| 138 | + }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); |
| 139 | + |
| 140 | + Looper.loop(); |
| 141 | + } |
| 142 | + }).start(); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Stop listening the changes on signal strength. |
| 147 | + */ |
| 148 | + public void stopListening() { |
| 149 | + this.stopListening = true; |
28 | 150 | }
|
29 | 151 | }
|
0 commit comments