Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 8.69 KB | None | 0 0
  1. package com.nix.safcom
  2.  
  3. import android.Manifest
  4. import android.content.Context
  5. import android.content.Intent
  6. import android.content.pm.PackageManager
  7. import android.graphics.Color
  8. import android.net.Uri
  9. import android.os.Bundle
  10. import android.provider.ContactsContract
  11. import android.telephony.TelephonyManager
  12. import android.view.Menu
  13. import android.view.MenuItem
  14. import android.view.View
  15. import androidx.appcompat.app.AppCompatActivity
  16. import androidx.core.app.ActivityCompat
  17. import androidx.core.content.ContextCompat
  18. import com.google.android.material.snackbar.Snackbar
  19. import kotlinx.android.synthetic.main.activity_main.*
  20. import android.widget.Toast
  21. import android.content.DialogInterface
  22. import android.text.InputType
  23. import android.widget.EditText
  24. import androidx.core.app.ComponentActivity.ExtraData
  25. import androidx.core.content.ContextCompat.getSystemService
  26. import android.icu.lang.UCharacter.GraphemeClusterBreak.T
  27. import androidx.appcompat.app.AlertDialog
  28.  
  29.  
  30. class MainActivity : AppCompatActivity() {
  31.  
  32.     var Hash = Uri.encode("#")
  33.     var phoneNo: String?=null
  34.     override fun onCreate(savedInstanceState: Bundle?) {
  35.         super.onCreate(savedInstanceState)
  36.         setContentView(R.layout.activity_main)
  37.         setSupportActionBar(toolbar)
  38.  
  39.  
  40.     }
  41.  
  42.     //fun
  43.     fun checkBalance(view:View){
  44.         USSDRUN("*144");
  45.         //Toast.makeText(this,"Checking Airtime Balance "+Name,Toast.LENGTH_SHORT).show();
  46.         display_snackbar("Checking Airtime Balance ");
  47.     }
  48.  
  49.     fun rechargeAirtime(view:View){
  50.         val taskEditText = EditText(this)
  51.         taskEditText.inputType = InputType.TYPE_CLASS_PHONE
  52.         val dialog = AlertDialog.Builder(this)
  53.             .setTitle("Recharge Airtime")
  54.             .setMessage("Please Enter the 16 digit Voucher Pin of the Scratch Card")
  55.             .setView(taskEditText)
  56.             .setPositiveButton("Recharge", DialogInterface.OnClickListener { dialog, which ->
  57.                 val voucherpin = taskEditText.text.toString()
  58.                 if (voucherpin.length != 16) {
  59.                     // Toast.makeText(Main.this,"The Voucher Pin is Invalid.Make Sure it is 16 digits",Toast.LENGTH_SHORT).show();
  60.                     display_snackbar("The Voucher Pin is Invalid.Make Sure it is 16 digits")
  61.                 } else {
  62.                     USSDRUN("*141*$voucherpin")
  63.                     //toast to tell the user what has just happen
  64.                     Toast.makeText(
  65.                         this@MainActivity,
  66.                         "Please Wait...as Safaricom recharges your account",
  67.                         Toast.LENGTH_LONG
  68.                     ).show()
  69.                     display_snackbar("Please Wait...as Safaricom recharges your account")
  70.                 }
  71.             })
  72.             .setNegativeButton("Cancel", null)
  73.             .create()
  74.         dialog.show()
  75.     }
  76.  
  77.     fun buyBundles(view:View){
  78.         USSDRUN("*544");
  79.         display_snackbar("Accessing Data Bundles");
  80.     }
  81.  
  82.     fun checkBundlesBalance(view:View){
  83.         USSDRUN("*450");
  84.         display_snackbar("Checking Data Bundles Balance ");
  85.     }
  86.     fun mpesaMiniStatement(view:View){
  87.  
  88.     }
  89.  
  90.     fun callCustomerCare(view:View){
  91.         USSDRUN("*100");
  92.         //Toast.makeText(this,"Opening CustomerCare Services",Toast.LENGTH_SHORT).show();
  93.         display_snackbar("Opening CustomerCare Services");
  94.     }
  95.  
  96.     fun sendPleaseCallMe(view: View){
  97.         val AD = android.app.AlertDialog.Builder(this@MainActivity)
  98.         AD.setCancelable(false)
  99.         AD.setMessage("Do you want sent Please Call Me to one who is in your contacts? ")
  100.         AD.setPositiveButton("YES") { arg0, arg1 ->
  101.             //first pick contact
  102.             val intent = Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI)
  103.             startActivityForResult(intent, 1)
  104.         }
  105.         //if no not in contacts then let user input the number
  106.         AD.setNegativeButton("NO") { arg0, arg1 ->
  107.             val taskEditText = EditText(this@MainActivity)
  108.             taskEditText.inputType = InputType.TYPE_CLASS_PHONE
  109.             val dialog = AlertDialog.Builder(this@MainActivity)
  110.                 .setTitle("Sending Please Call Me")
  111.                 .setMessage("Please Enter the Phone number of who to send Please Call Me to.:")
  112.                 .setView(taskEditText)
  113.                 .setPositiveButton("Send", DialogInterface.OnClickListener { dialog, which ->
  114.                     val phoneNo = taskEditText.text.toString()
  115.                     if (phoneNo.length < 0) {
  116.                         display_snackbar(" The Number You Have Entered is Invalid.\n" + "Please Check and Try Again ")
  117.                     } else {
  118.                         USSDRUN("*130*$phoneNo")
  119.                         //toast to tell the user what has just happen
  120.                         Toast.makeText(
  121.                             this@MainActivity,
  122.                             "Sending a FlashBack Message to $phoneNo", Toast.LENGTH_LONG
  123.                         ).show()
  124.                         display_snackbar("Sending a FlashBack Message to $phoneNo")
  125.                     }
  126.                 })
  127.                 .setNegativeButton("Cancel", null)
  128.                 .create()
  129.             dialog.show()
  130.         }
  131.         AD.show()
  132.  
  133.     }
  134.  
  135.  
  136.  
  137.     override fun onCreateOptionsMenu(menu: Menu): Boolean {
  138.         // Inflate the menu; this adds items to the action bar if it is present.
  139.         menuInflater.inflate(R.menu.menu_main, menu)
  140.         return true
  141.     }
  142.  
  143.     override fun onOptionsItemSelected(item: MenuItem): Boolean {
  144.         // Handle action bar item clicks here. The action bar will
  145.         // automatically handle clicks on the Home/Up button, so long
  146.         // as you specify a parent activity in AndroidManifest.xml.
  147.         return when (item.itemId) {
  148.             R.id.action_settings -> true
  149.             else -> super.onOptionsItemSelected(item)
  150.         }
  151.     }
  152.  
  153.  
  154.     fun USSDRUN(tel: String) {
  155.         val permissionCheck =
  156.             ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.CALL_PHONE)
  157.         if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
  158.             ActivityCompat.requestPermissions(
  159.                 this,
  160.                 arrayOf(Manifest.permission.CALL_PHONE), 123
  161.             )
  162.         } else {
  163.             val Name = OperatorName()
  164.             if (Name === "Safaricom") {
  165.  
  166.  
  167.             } else {
  168.                 val intent = Intent(Intent.ACTION_CALL)
  169.                 intent.data = Uri.parse("tel:$tel$Hash")
  170.                 intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
  171.                 startActivity(intent)
  172.             }
  173.  
  174.         }
  175.  
  176.     }
  177.  
  178.     fun OperatorName(): String {
  179.         val OperatorName: String
  180.         val mTeleManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
  181.         mTeleManager.simOperatorName
  182.         mTeleManager.networkOperatorName
  183.         mTeleManager.simOperator
  184.         OperatorName = mTeleManager.simOperatorName
  185.         return OperatorName
  186.     }
  187.  
  188.     //handles the results returned i.e from the contacts
  189.     public override fun onActivityResult(reqCode: Int, resultCode: Int, data: Intent?) {
  190.  
  191.         super.onActivityResult(reqCode, resultCode, data)
  192.         phoneNo = null
  193.         val uri = data!!.data
  194.         val cursor = contentResolver.query(uri!!, null, null, null, null)
  195.         cursor!!.moveToFirst()
  196.         val phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
  197.         phoneNo = cursor.getString(phoneIndex)
  198.         val name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
  199.        
  200.         val AD = android.app.AlertDialog.Builder(this)
  201.         AD.setCancelable(false)
  202.         AD.setMessage("Do you want to sent please call me to $name")
  203.         AD.setPositiveButton("YES") { arg0, arg1 ->
  204.             USSDRUN("*130*$phoneNo")
  205.             //Toast.makeText(Main.this,"Sending FlashBack To "+name+"..............",Toast.LENGTH_SHORT).show();
  206.             display_snackbar("Sending FlashBack To $name..............")
  207.         }
  208.         AD.setNegativeButton("NO") { arg0, arg1 ->
  209.             phoneNo = null
  210.             arg0.cancel()
  211.         }
  212.         AD.show()
  213.        
  214.     }
  215.     //method to display snackbar
  216.     fun display_snackbar(Info: String) {
  217.         val snackbar =
  218.             Snackbar.make(findViewById<View>(android.R.id.content), Info, Snackbar.LENGTH_LONG)
  219.         // Change the Snackbar default text color
  220.         val snackbarView = snackbar.view
  221.         // Change the Snackbar default background color
  222.         snackbarView.setBackgroundColor(Color.parseColor("#11A73E"))
  223.         // Display the Snackbar
  224.         snackbar.show()
  225.  
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement