Created
July 11, 2018 15:33
-
-
Save photizzo/a0c54620ea61f1e9326ee09b3eeb2511 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequiresApi(26) | |
fun requestUssdUsingTelephonyManager(ussd: String, simSlot: Int) { | |
val ussd = ussd.replace("%23", Uri.decode("#")) | |
Log.e("ussd", "requesting for ussd $ussd") | |
val manager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager | |
val handler = Handler() | |
val callback = object : TelephonyManager.UssdResponseCallback() { | |
override fun onReceiveUssdResponse(telephonyManager: TelephonyManager, request: String, response: CharSequence) { | |
super.onReceiveUssdResponse(telephonyManager, request, response) // what if i remove this line | |
EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, response.toString())) | |
textview_log.text = "Success with response : $response \n request $request" | |
Log.e("ussd", "Success with response : $response \n request $request") | |
} | |
override fun onReceiveUssdResponseFailed(telephonyManager: TelephonyManager, request: String, failureCode: Int) { | |
super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode) // what if i remove this line | |
EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, "Failed with code $failureCode")) | |
textview_log.text = "failed with code " + Integer.toString(failureCode) + "\n request " + request | |
Log.e("ussd", "failed with code " + Integer.toString(failureCode) + "\n request " + request) | |
} | |
} | |
EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, "Starting ... ")) | |
try { | |
//check for available sims on usr phone | |
val sims = SubscriptionManager.from(this).activeSubscriptionInfoList | |
if (sims == null) { | |
// no sim card is available | |
EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, "No SIM card detected")) | |
} | |
if (sims != null) { | |
for (subInfo in sims) { | |
val slotIndex = subInfo.simSlotIndex | |
// for sim 1 - slot index is 0 | |
if (simSlot == slotIndex && simSlot == 0) { | |
EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, "Requesting for SIM 1...")) | |
val subscriptionForSlot = subInfo.subscriptionId | |
Log.e("TAG", "sim subscription id $subscriptionForSlot") | |
val telephonyManager = manager.createForSubscriptionId(subscriptionForSlot) | |
textview_log.text = "Requesting ... ..." + telephonyManager.networkOperatorName | |
try { | |
// EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, "Sending USSD")) | |
telephonyManager.sendUssdRequest(ussd, callback, handler) | |
Log.e("ussd", "sending ussd successful") | |
} catch (e: SecurityException) { | |
EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, "Permission denied ")) | |
e.printStackTrace() | |
} | |
} | |
// for sim 2 slot index is 1 | |
else if (simSlot == slotIndex && simSlot == 1) { | |
// EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, "Requesting for SIM 12 ...")) | |
val subscriptionForSlot = subInfo.subscriptionId | |
Log.e("TAG", "sim subscription id $subscriptionForSlot") | |
val telephonyManager = manager.createForSubscriptionId(subscriptionForSlot) | |
textview_log.text = "Requesting ... ..." + telephonyManager.networkOperatorName | |
try { | |
EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, "Sending USSD")) | |
telephonyManager.sendUssdRequest(ussd, callback, handler) | |
Log.e("ussd", "sending ussd successful") | |
} catch (e: SecurityException) { | |
EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, "Permission denied ")) | |
e.printStackTrace() | |
} | |
} | |
} | |
} | |
} catch (e: SecurityException) { | |
EventBus.getDefault().post(IntentResult(Activity.RESULT_OK, | |
"Permission failed")) | |
Log.e("ussd", "permission failed") | |
e.printStackTrace() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment