Guest User

Untitled

a guest
Oct 22nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.74 KB | None | 0 0
  1. package call.sip.sipandroidcall
  2.  
  3. import android.Manifest
  4. import android.app.AlertDialog
  5. import android.app.Dialog
  6. import android.support.v7.app.AppCompatActivity
  7. import android.os.Bundle
  8. import android.net.sip.SipAudioCall
  9. import android.net.sip.SipManager
  10. import android.content.IntentFilter
  11. import android.content.Intent
  12. import android.content.DialogInterface
  13. import android.preference.PreferenceManager
  14. import android.content.pm.PackageManager
  15. import android.support.v4.app.ActivityCompat
  16. import android.support.v4.content.ContextCompat
  17. import android.view.*
  18. import android.widget.EditText
  19. import android.widget.TextView
  20. import android.view.LayoutInflater
  21. import kotlinx.android.synthetic.main.activity_main.*
  22. import call.sip.secondmindsiplib.SIPManager;
  23. import call.sip.secondmindsiplib.SIPManagerListener;
  24. //import sip.secondmind.secondmindsiplibrary.SIPManagerImpl
  25. //import sip.secondmind.secondmindsiplibrary.SipManagerImplListener
  26.  
  27.  
  28. //override fun onRequestPermissionsResult(requestCode: Int,
  29. // permissions: Array<String>, grantResults: IntArray) {
  30. // super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  31. //
  32. // when (requestCode) {
  33. // PERMISSION_REQ_CODE -> {
  34. //
  35. // if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  36. //
  37. // val pushToTalkButton = pushToTalk
  38. // pushToTalkButton.setOnTouchListener(this)
  39. //
  40. // // Set up the intent filter. This will be used to fire an
  41. // // IncomingCallReceiver when someone calls the SIP address used by this
  42. // // application.
  43. // val filter = IntentFilter()
  44. // filter.addAction("android.SipDemo.INCOMING_CALL")
  45. // callReceiver = IncomingCallReceiver()
  46. // this.registerReceiver(callReceiver, filter)
  47. //
  48. // // "Push to talk" can be a serious pain when the screen keeps turning off.
  49. // // Let's prevent that.
  50. // window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
  51. //
  52. // initializeManager()
  53. //
  54. // }
  55. // }
  56. // }
  57. //}
  58.  
  59. //val PERMISSION_REQ_CODE = 1234
  60. //val PERMISSIONS = arrayOf(
  61. // Manifest.permission.USE_SIP,
  62. // Manifest.permission.INTERNET,
  63. // Manifest.permission.ACCESS_WIFI_STATE,
  64. // Manifest.permission.WAKE_LOCK,
  65. // Manifest.permission.RECORD_AUDIO,
  66. // Manifest.permission.VIBRATE)
  67.  
  68. class MainActivity : AppCompatActivity(), View.OnClickListener {
  69.  
  70. var sipAddress: String? = "sip1-dev.secondmind.ai"
  71.  
  72. var manager: SipManager? = null
  73. // var me: SipProfile? = null
  74. var call: SipAudioCall? = null
  75. var callReceiver: IncomingCallReceiver? = null
  76. lateinit var sipManagerImpl: SIPManager
  77.  
  78. private val CALL_ADDRESS = 1
  79. private val SET_AUTH_INFO = 2
  80. private val UPDATE_SETTINGS_DIALOG = 3
  81. private val HANG_UP = 4
  82.  
  83. private var inCall = false
  84.  
  85. val PERMISSION_REQ_CODE = 1234
  86. val PERMISSIONS = arrayOf(
  87. Manifest.permission.USE_SIP,
  88. Manifest.permission.INTERNET,
  89. Manifest.permission.ACCESS_WIFI_STATE,
  90. Manifest.permission.WAKE_LOCK,
  91. Manifest.permission.RECORD_AUDIO,
  92. Manifest.permission.VIBRATE)
  93.  
  94.  
  95. public override fun onCreate(savedInstanceState: Bundle?) {
  96.  
  97. super.onCreate(savedInstanceState)
  98. setContentView(R.layout.activity_main)
  99. //
  100. // val pushToTalkButton = pushToTalk
  101. // pushToTalkButton.setOnTouchListener(this)
  102. //
  103. // // Set up the intent filter. This will be used to fire an
  104. // // IncomingCallReceiver when someone calls the SIP address used by this
  105. // // application.
  106. // val filter = IntentFilter()
  107. // filter.addAction("android.SipDemo.INCOMING_CALL")
  108. // callReceiver = IncomingCallReceiver()
  109. // this.registerReceiver(callReceiver, filter)
  110. //
  111. // // "Push to talk" can be a serious pain when the screen keeps turning off.
  112. // // Let's prevent that.
  113. // window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
  114. //
  115. // initializeManager()
  116.  
  117. sipManagerImpl = SIPManager(this)
  118.  
  119. sipManagerImpl.setSIPManagerListener(object : SIPManagerListener {
  120. override fun onCallEstablished(call: SipAudioCall) {
  121. updateStatus(call)
  122. }
  123.  
  124. override fun onCallEnded(call: SipAudioCall) {
  125. updateStatus("Ready")
  126. }
  127.  
  128. override fun onRegistering(localProfileUri: String) {
  129. updateStatus("Registering with SIP Server...")
  130. }
  131.  
  132. override fun onRegistrationDone(localProfileUri: String, expiryTime: Long) {
  133. updateStatus("Ready")
  134. }
  135.  
  136. override fun onRegistrationFailed(localProfileUri: String, errorCode: Int, errorMessage: String) {
  137. updateStatus("Registration failed. Please check settings.")
  138. }
  139.  
  140. override fun statusUpdate(message: String) {
  141. updateStatus(message)
  142. }
  143. })
  144.  
  145. askPermission()
  146. }
  147.  
  148. private fun askPermission() {
  149. if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.USE_SIP)
  150. != PackageManager.PERMISSION_GRANTED) {
  151. ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_REQ_CODE)
  152. } else {
  153.  
  154. val pushToTalkButton = pushToTalk
  155. pushToTalkButton.setOnClickListener(this)
  156.  
  157. // Set up the intent filter. This will be used to fire an
  158. // IncomingCallReceiver when someone calls the SIP address used by this
  159. // application.
  160. val filter = IntentFilter()
  161. filter.addAction("android.SipDemo.INCOMING_CALL")
  162. callReceiver = IncomingCallReceiver()
  163. this.registerReceiver(callReceiver, filter)
  164.  
  165. // "Push to talk" can be a serious pain when the screen keeps turning off.
  166. // Let's prevent that.
  167. window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
  168.  
  169. initializeManager()
  170. }
  171. }
  172.  
  173. override fun onRequestPermissionsResult(requestCode: Int,
  174. permissions: Array<String>, grantResults: IntArray) {
  175. super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  176.  
  177. when (requestCode) {
  178. PERMISSION_REQ_CODE -> {
  179.  
  180. if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED
  181. && grantResults[1] == PackageManager.PERMISSION_GRANTED
  182. ) {
  183.  
  184. val pushToTalkButton = pushToTalk
  185. pushToTalkButton.setOnClickListener(this)
  186.  
  187. // Set up the intent filter. This will be used to fire an
  188. // IncomingCallReceiver when someone calls the SIP address used by this
  189. // application.
  190. val filter = IntentFilter()
  191. filter.addAction("android.SipDemo.INCOMING_CALL")
  192. callReceiver = IncomingCallReceiver()
  193. this.registerReceiver(callReceiver, filter)
  194.  
  195. // "Push to talk" can be a serious pain when the screen keeps turning off.
  196. // Let's prevent that.
  197. window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
  198.  
  199.  
  200. // initializeManager()
  201.  
  202. }
  203. }
  204. }
  205. }
  206.  
  207. public override fun onStart() {
  208. super.onStart()
  209. // When we get back from the preference setting Activity, assume
  210. // settings have changed, and re-login with new auth info.
  211. initializeManager()
  212. }
  213.  
  214. public override fun onDestroy() {
  215. super.onDestroy()
  216. // if (call != null) {
  217. // call!!.close()
  218. // }
  219. //
  220. // closeLocalProfile()
  221.  
  222. if (callReceiver != null) {
  223. this.unregisterReceiver(callReceiver);
  224. }
  225. }
  226.  
  227.  
  228. public fun initializeManager() {
  229. if (manager == null) {
  230. manager = SipManager.newInstance(this);
  231. }
  232. //
  233. initializeLocalProfile();
  234. }
  235.  
  236. /**
  237. * Logs you into your SIP provider, registering this device as the location to
  238. * send SIP calls to for your SIP address.
  239. */
  240. fun initializeLocalProfile() {
  241. // if (manager == null) {
  242. // return
  243. // }
  244. //
  245. // if (me != null) {
  246. // closeLocalProfile()
  247. // }
  248.  
  249. val prefs = PreferenceManager.getDefaultSharedPreferences(baseContext)
  250. val username = prefs.getString("namePref", "")
  251. val domain = prefs.getString("domainPref", "")
  252. val password = prefs.getString("passPref", "")
  253.  
  254. if (username!!.length == 0 || domain!!.length == 0 || password!!.length == 0) {
  255. showDialog(UPDATE_SETTINGS_DIALOG)
  256. return
  257. }
  258.  
  259. // sipManagerImpl = SIPManagerImpl(this)
  260. //
  261. // sipManagerImpl.setSIPManagerListener(object : SipManagerImplListener {
  262. // override fun onCallEstablished(call: SipAudioCall) {
  263. // updateStatus(call)
  264. // }
  265. //
  266. // override fun onCallEnded(call: SipAudioCall) {
  267. // updateStatus("Ready")
  268. // }
  269. //
  270. // override fun onRegistering(localProfileUri: String) {
  271. // updateStatus("Registering with SIP Server...")
  272. // }
  273. //
  274. // override fun onRegistrationDone(localProfileUri: String, expiryTime: Long) {
  275. // updateStatus("Ready")
  276. // }
  277. //
  278. // override fun onRegistrationFailed(localProfileUri: String, errorCode: Int, errorMessage: String) {
  279. // updateStatus("Registration failed. Please check settings.")
  280. // }
  281. //
  282. // override fun statusUpdate(message: String) {
  283. // updateStatus(message)
  284. // }
  285. // })
  286.  
  287. sipManagerImpl.setProfile(username, password, domain)
  288.  
  289. //N/9pmE6PtfPjHUb2
  290. // try {
  291. // val builder = SipProfile.Builder(username, domain)
  292. // builder.setPassword(password)
  293. // me = builder.build()
  294. //
  295. // val i = Intent()
  296. // i.action = "android.SipDemo.INCOMING_CALL"
  297. // val pi = PendingIntent.getBroadcast(this, 0, i, Intent.FILL_IN_DATA)
  298. // manager!!.open(me, pi, null)
  299. //
  300. //
  301. // // This listener must be added AFTER manager.open is called,
  302. // // Otherwise the methods aren't guaranteed to fire.
  303. // val test = me!!.getUriString()
  304. // manager!!.setRegistrationListener(me!!.getUriString(), object : SipRegistrationListener {
  305. // override fun onRegistering(localProfileUri: String) {
  306. // updateStatus("Registering with SIP Server...")
  307. // }
  308. //
  309. // override fun onRegistrationDone(localProfileUri: String, expiryTime: Long) {
  310. // updateStatus("Ready")
  311. // }
  312. //
  313. // override fun onRegistrationFailed(localProfileUri: String, errorCode: Int, errorMessage: String) {
  314. // updateStatus("Registration failed. Please check settings.")
  315. // }
  316. // })
  317. // } catch (pe: ParseException) {
  318. // updateStatus("Connection Error.")
  319. // } catch (se: SipException) {
  320. // updateStatus("Connection error.")
  321. // }
  322.  
  323. }
  324.  
  325. /**
  326. * Closes out your local profile, freeing associated objects into memory
  327. * and unregistering your device from the server.
  328. */
  329. fun closeLocalProfile() {
  330. // if (manager == null) {
  331. // return
  332. // }
  333. // try {
  334. // if (me != null) {
  335. // manager!!.close(me!!.getUriString())
  336. // }
  337. // } catch (ee: Exception) {
  338. // Log.d("MainActivity/onDestroy", "Failed to close local profile.", ee)
  339. // }
  340.  
  341. }
  342.  
  343. /**
  344. * Make an outgoing call.
  345. */
  346. fun initiateCall() {
  347.  
  348. // sipManagerImpl.startSIPCall()
  349.  
  350. // updateStatus(sipAddress!!)
  351. //
  352. // try {
  353. // val listener = object : SipAudioCall.Listener() {
  354. // // Much of the client's interaction with the SIP Stack will
  355. // // happen via listeners. Even making an outgoing call, don't
  356. // // forget to set up a listener to set things up once the call is established.
  357. // override fun onCallEstablished(call: SipAudioCall) {
  358. // call.startAudio()
  359. // call.setSpeakerMode(true)
  360. // call.toggleMute()
  361. // updateStatus(call)
  362. // }
  363. //
  364. // override fun onCallEnded(call: SipAudioCall) {
  365. // updateStatus("Ready.")
  366. // }
  367. // }
  368. //
  369. // call = manager!!.makeAudioCall(me!!.getUriString(), sipAddress + "@sip1-dev.secondmind.ai", listener, 30)
  370. //// manager.
  371. //
  372. // } catch (e: Exception) {
  373. // Log.i("Main/InitiateCall", "Error when trying to close manager.", e)
  374. // if (me != null) {
  375. // try {
  376. // manager!!.close(me!!.getUriString())
  377. // } catch (ee: Exception) {
  378. // Log.i("Main/InitiateCall",
  379. // "Error when trying to close manager.", ee)
  380. // ee.printStackTrace()
  381. // }
  382. //
  383. // }
  384. // if (call != null) {
  385. // call!!.close()
  386. // }
  387. // }
  388.  
  389. }
  390.  
  391. /**
  392. * Updates the status box at the top of the UI with a messege of your choice.
  393. * @param status The String to display in the status box.
  394. */
  395. fun updateStatus(status: String) {
  396. // Be a good citizen. Make sure UI changes fire on the UI thread.
  397. this.runOnUiThread {
  398. val labelView = findViewById(R.id.sipLabel) as TextView
  399. labelView.text = status
  400. }
  401. }
  402.  
  403. /**
  404. * Updates the status box with the SIP address of the current call.
  405. * @param call The current, active call.
  406. */
  407. fun updateStatus(call: SipAudioCall) {
  408. var useName: String? = call.peerProfile.displayName
  409. if (useName == null) {
  410. useName = call.peerProfile.userName
  411. }
  412. updateStatus(useName + "@" + call.peerProfile.sipDomain)
  413. }
  414.  
  415. /**
  416. * Updates whether or not the user's voice is muted, depending on whether the button is pressed.
  417. * @param v The View where the touch event is being fired.
  418. * @param event The motion to act on.
  419. * @return boolean Returns false to indicate that the parent view should handle the touch event
  420. * as it normally would.
  421. */
  422. // override fun onTouch(v: View, event: MotionEvent): Boolean {
  423. //
  424. // if (event.action == MotionEvent.ACTION_DOWN && call != null) {
  425. // try {
  426. // call!!.endCall()
  427. //
  428. // } catch (se: SipException) {
  429. // Log.d("Main/onOptionsItem",
  430. // "Error ending call.", se)
  431. // }
  432. //
  433. // call!!.close()
  434. // call = null
  435. // } else if(event.action == MotionEvent.ACTION_UP && call == null)
  436. // showDialog(CALL_ADDRESS)
  437. //
  438. //// if (call == null) {
  439. //// return false
  440. //// } else if (event.action == MotionEvent.ACTION_DOWN && call != null && call!!.isMuted()) {
  441. //// call!!.toggleMute()
  442. //// } else if (event.action == MotionEvent.ACTION_UP && !call!!.isMuted()) {
  443. //// call!!.toggleMute()
  444. //// }
  445. //// return false
  446. // return false
  447. // }
  448.  
  449.  
  450. override fun onClick(v: View?) {
  451.  
  452. if (inCall) {
  453. sipManagerImpl.endSIPCall()
  454. inCall = false
  455.  
  456. } else {
  457. showDialog(CALL_ADDRESS)
  458. }
  459.  
  460. // if (call != null) {
  461. // try {
  462. // call!!.endCall()
  463. //
  464. // } catch (se: SipException) {
  465. // Log.d("Main/onOptionsItem",
  466. // "Error ending call.", se)
  467. // }
  468. //
  469. // call!!.close()
  470. // call = null
  471. //// initializeLocalProfile()
  472. // } else //if(event.action == MotionEvent.ACTION_UP && call == null)
  473. // showDialog(CALL_ADDRESS)
  474.  
  475. }
  476.  
  477. override fun onCreateOptionsMenu(menu: Menu): Boolean {
  478. // menu.add(0, CALL_ADDRESS, 0, "Call someone")
  479. menu.add(0, SET_AUTH_INFO, 0, "Edit your SIP Info.")
  480. // menu.add(0, HANG_UP, 0, "End Current Call.")
  481.  
  482. return true
  483. }
  484.  
  485. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  486. when (item.getItemId()) {
  487. // CALL_ADDRESS -> showDialog(CALL_ADDRESS)
  488. SET_AUTH_INFO -> updatePreferences()
  489. // HANG_UP -> if (call != null) {
  490. // try {
  491. // call!!.endCall()
  492. // } catch (se: SipException) {
  493. // Log.d("Main/onOptionsItem",
  494. // "Error ending call.", se)
  495. // }
  496. //
  497. // call!!.close()
  498. // }
  499. }
  500. return true
  501. }
  502.  
  503. override fun onCreateDialog(id: Int): Dialog? {
  504. when (id) {
  505. CALL_ADDRESS -> {
  506.  
  507. val factory = LayoutInflater.from(this)
  508. val textBoxView = factory.inflate(R.layout.call_address_dialog, null)
  509. return AlertDialog.Builder(this)
  510. .setTitle("Call Someone.")
  511. .setView(textBoxView)
  512. .setPositiveButton(
  513. android.R.string.ok, DialogInterface.OnClickListener { dialog, whichButton ->
  514. val textField = textBoxView.findViewById(R.id.calladdress_edit) as EditText
  515. sipAddress = textField.text.toString()
  516. // initiateCall()
  517. sipManagerImpl.startSIPCall(sipAddress!!)
  518. inCall = true
  519. })
  520. .setNegativeButton(
  521. android.R.string.cancel, DialogInterface.OnClickListener { dialog, whichButton ->
  522. // Noop.
  523. })
  524. .create()
  525. }
  526.  
  527. UPDATE_SETTINGS_DIALOG -> return AlertDialog.Builder(this)
  528. .setMessage("Please update your SIP Account Settings.")
  529. .setPositiveButton(android.R.string.ok, DialogInterface.OnClickListener { dialog, whichButton -> updatePreferences() })
  530. .setNegativeButton(
  531. android.R.string.cancel, DialogInterface.OnClickListener { dialog, whichButton ->
  532. // Noop.
  533. })
  534. .create()
  535. }
  536. return null
  537. }
  538.  
  539. fun updatePreferences() {
  540. val settingsActivity = Intent(baseContext,
  541. SipSettings::class.java)
  542. startActivity(settingsActivity)
  543. }
  544.  
  545.  
  546. }
Add Comment
Please, Sign In to add comment