Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nix.safcom
- import android.Manifest
- import android.content.Context
- import android.content.Intent
- import android.content.pm.PackageManager
- import android.graphics.Color
- import android.net.Uri
- import android.os.Bundle
- import android.provider.ContactsContract
- import android.telephony.TelephonyManager
- import android.view.Menu
- import android.view.MenuItem
- import android.view.View
- import androidx.appcompat.app.AppCompatActivity
- import androidx.core.app.ActivityCompat
- import androidx.core.content.ContextCompat
- import com.google.android.material.snackbar.Snackbar
- import kotlinx.android.synthetic.main.activity_main.*
- import android.widget.Toast
- import android.content.DialogInterface
- import android.text.InputType
- import android.widget.EditText
- import androidx.core.app.ComponentActivity.ExtraData
- import androidx.core.content.ContextCompat.getSystemService
- import android.icu.lang.UCharacter.GraphemeClusterBreak.T
- import androidx.appcompat.app.AlertDialog
- class MainActivity : AppCompatActivity() {
- var Hash = Uri.encode("#")
- var phoneNo: String?=null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- setSupportActionBar(toolbar)
- }
- //fun
- fun checkBalance(view:View){
- USSDRUN("*144");
- //Toast.makeText(this,"Checking Airtime Balance "+Name,Toast.LENGTH_SHORT).show();
- display_snackbar("Checking Airtime Balance ");
- }
- fun rechargeAirtime(view:View){
- val taskEditText = EditText(this)
- taskEditText.inputType = InputType.TYPE_CLASS_PHONE
- val dialog = AlertDialog.Builder(this)
- .setTitle("Recharge Airtime")
- .setMessage("Please Enter the 16 digit Voucher Pin of the Scratch Card")
- .setView(taskEditText)
- .setPositiveButton("Recharge", DialogInterface.OnClickListener { dialog, which ->
- val voucherpin = taskEditText.text.toString()
- if (voucherpin.length != 16) {
- // Toast.makeText(Main.this,"The Voucher Pin is Invalid.Make Sure it is 16 digits",Toast.LENGTH_SHORT).show();
- display_snackbar("The Voucher Pin is Invalid.Make Sure it is 16 digits")
- } else {
- USSDRUN("*141*$voucherpin")
- //toast to tell the user what has just happen
- Toast.makeText(
- this@MainActivity,
- "Please Wait...as Safaricom recharges your account",
- Toast.LENGTH_LONG
- ).show()
- display_snackbar("Please Wait...as Safaricom recharges your account")
- }
- })
- .setNegativeButton("Cancel", null)
- .create()
- dialog.show()
- }
- fun buyBundles(view:View){
- USSDRUN("*544");
- display_snackbar("Accessing Data Bundles");
- }
- fun checkBundlesBalance(view:View){
- USSDRUN("*450");
- display_snackbar("Checking Data Bundles Balance ");
- }
- fun mpesaMiniStatement(view:View){
- }
- fun callCustomerCare(view:View){
- USSDRUN("*100");
- //Toast.makeText(this,"Opening CustomerCare Services",Toast.LENGTH_SHORT).show();
- display_snackbar("Opening CustomerCare Services");
- }
- fun sendPleaseCallMe(view: View){
- val AD = android.app.AlertDialog.Builder(this@MainActivity)
- AD.setCancelable(false)
- AD.setMessage("Do you want sent Please Call Me to one who is in your contacts? ")
- AD.setPositiveButton("YES") { arg0, arg1 ->
- //first pick contact
- val intent = Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI)
- startActivityForResult(intent, 1)
- }
- //if no not in contacts then let user input the number
- AD.setNegativeButton("NO") { arg0, arg1 ->
- val taskEditText = EditText(this@MainActivity)
- taskEditText.inputType = InputType.TYPE_CLASS_PHONE
- val dialog = AlertDialog.Builder(this@MainActivity)
- .setTitle("Sending Please Call Me")
- .setMessage("Please Enter the Phone number of who to send Please Call Me to.:")
- .setView(taskEditText)
- .setPositiveButton("Send", DialogInterface.OnClickListener { dialog, which ->
- val phoneNo = taskEditText.text.toString()
- if (phoneNo.length < 0) {
- display_snackbar(" The Number You Have Entered is Invalid.\n" + "Please Check and Try Again ")
- } else {
- USSDRUN("*130*$phoneNo")
- //toast to tell the user what has just happen
- Toast.makeText(
- this@MainActivity,
- "Sending a FlashBack Message to $phoneNo", Toast.LENGTH_LONG
- ).show()
- display_snackbar("Sending a FlashBack Message to $phoneNo")
- }
- })
- .setNegativeButton("Cancel", null)
- .create()
- dialog.show()
- }
- AD.show()
- }
- override fun onCreateOptionsMenu(menu: Menu): Boolean {
- // Inflate the menu; this adds items to the action bar if it is present.
- menuInflater.inflate(R.menu.menu_main, menu)
- return true
- }
- override fun onOptionsItemSelected(item: MenuItem): Boolean {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- return when (item.itemId) {
- R.id.action_settings -> true
- else -> super.onOptionsItemSelected(item)
- }
- }
- fun USSDRUN(tel: String) {
- val permissionCheck =
- ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.CALL_PHONE)
- if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(
- this,
- arrayOf(Manifest.permission.CALL_PHONE), 123
- )
- } else {
- val Name = OperatorName()
- if (Name === "Safaricom") {
- } else {
- val intent = Intent(Intent.ACTION_CALL)
- intent.data = Uri.parse("tel:$tel$Hash")
- intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
- startActivity(intent)
- }
- }
- }
- fun OperatorName(): String {
- val OperatorName: String
- val mTeleManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
- mTeleManager.simOperatorName
- mTeleManager.networkOperatorName
- mTeleManager.simOperator
- OperatorName = mTeleManager.simOperatorName
- return OperatorName
- }
- //handles the results returned i.e from the contacts
- public override fun onActivityResult(reqCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(reqCode, resultCode, data)
- phoneNo = null
- val uri = data!!.data
- val cursor = contentResolver.query(uri!!, null, null, null, null)
- cursor!!.moveToFirst()
- val phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
- phoneNo = cursor.getString(phoneIndex)
- val name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
- val AD = android.app.AlertDialog.Builder(this)
- AD.setCancelable(false)
- AD.setMessage("Do you want to sent please call me to $name")
- AD.setPositiveButton("YES") { arg0, arg1 ->
- USSDRUN("*130*$phoneNo")
- //Toast.makeText(Main.this,"Sending FlashBack To "+name+"..............",Toast.LENGTH_SHORT).show();
- display_snackbar("Sending FlashBack To $name..............")
- }
- AD.setNegativeButton("NO") { arg0, arg1 ->
- phoneNo = null
- arg0.cancel()
- }
- AD.show()
- }
- //method to display snackbar
- fun display_snackbar(Info: String) {
- val snackbar =
- Snackbar.make(findViewById<View>(android.R.id.content), Info, Snackbar.LENGTH_LONG)
- // Change the Snackbar default text color
- val snackbarView = snackbar.view
- // Change the Snackbar default background color
- snackbarView.setBackgroundColor(Color.parseColor("#11A73E"))
- // Display the Snackbar
- snackbar.show()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement