daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest May 7th, 2016 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class WalkieTalkieActivity extends Activity implements View.OnTouchListener {
  2.  
  3. public String sipAddress = null;
  4.  
  5. public SipManager manager = null;
  6. public SipProfile me = null;
  7. public SipAudioCall call = null;
  8. public IncomingCallReceiver callReceiver;
  9.  
  10. private static final int CALL_ADDRESS = 1;
  11. private static final int SET_AUTH_INFO = 2;
  12. private static final int UPDATE_SETTINGS_DIALOG = 3;
  13. private static final int HANG_UP = 4;
  14.  
  15. EditText callerAddressTxt;
  16. Button contactsBtn;
  17.  
  18.  
  19.  
  20.  
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23.  
  24.     super.onCreate(savedInstanceState);
  25.     setContentView(R.layout.walkietalkie);
  26.  
  27.     callerAddressTxt = (EditText) findViewById(R.id.txtSipAddress);
  28.  
  29.     contactsBtn = (Button) findViewById(R.id.btnContacts);
  30.     contactsBtn.setOnClickListener(new View.OnClickListener() {
  31.         @Override
  32.         public void onClick(View view) {
  33.             Intent i = new Intent(WalkieTalkieActivity.this,ContactsActivity.class);
  34.             startActivityForResult(i, 2);
  35.  
  36.         }
  37.     });
  38.  
  39.     ImageButton callBtn = (ImageButton) findViewById(R.id.idCallButton);
  40.     callBtn.setOnClickListener(new View.OnClickListener() {
  41.         @Override
  42.         public void onClick(View view) {
  43.             initiateCall();
  44.         }
  45.     });
  46.  
  47.     // Set up the intent filter.  This will be used to fire an
  48.     // IncomingCallReceiver when someone calls the SIP address used by this
  49.     // application.
  50.     IntentFilter filter = new IntentFilter();
  51.     filter.addAction("android.SipDemo.INCOMING_CALL");
  52.     callReceiver = new IncomingCallReceiver();
  53.     this.registerReceiver(callReceiver, filter);
  54.  
  55.     // "Push to talk" can be a serious pain when the screen keeps turning off.
  56.     // Let's prevent that.
  57.     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  58.  
  59.     initializeManager();
  60. }
  61.  
  62. @Override
  63. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  64.     super.onActivityResult(requestCode, resultCode, data);
  65.     if(requestCode==2)
  66.     {
  67.         sipAddress=data.getStringExtra("sipAddress");
  68.         callerAddressTxt.setText(sipAddress);
  69.     }
  70. }
  71.  
  72. @Override
  73. public void onStart() {
  74.     super.onStart();
  75.     // When we get back from the preference setting Activity, assume
  76.     // settings have changed, and re-login with new auth info.
  77.     initializeManager();
  78. }
  79.  
  80. @Override
  81. public void onDestroy() {
  82.     super.onDestroy();
  83.     if (call != null) {
  84.         call.close();
  85.     }
  86.  
  87.     closeLocalProfile();
  88.  
  89.     if (callReceiver != null) {
  90.         this.unregisterReceiver(callReceiver);
  91.     }
  92. }
  93.  
  94. public void initializeManager() {
  95.     if(manager == null) {
  96.       manager = SipManager.newInstance(this);
  97.     }
  98.  
  99.     initializeLocalProfile();
  100. }
  101.  
  102. /**
  103.  * Logs you into your SIP provider, registering this device as the location to
  104.  * send SIP calls to for your SIP address.
  105.  */
  106. public void initializeLocalProfile() {
  107.     if (manager == null) {
  108.         return;
  109.     }
  110.  
  111.     if (me != null) {
  112.         closeLocalProfile();
  113.     }
  114.  
  115.     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
  116.     String username = prefs.getString("namePref", "");
  117.     String domain = prefs.getString("domainPref", "");
  118.     String password = prefs.getString("passPref", "");
  119.  
  120.     if (username.length() == 0 || domain.length() == 0 || password.length() == 0) {
  121.         showDialog(UPDATE_SETTINGS_DIALOG);
  122.         return;
  123.     }
  124.  
  125.     try {
  126.         SipProfile.Builder builder = new SipProfile.Builder(username, domain);
  127.         builder.setPassword(password);
  128.         me = builder.build();
  129.  
  130.         Intent i = new Intent();
  131.         i.setAction("android.SipDemo.INCOMING_CALL");
  132.         PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, Intent.FILL_IN_DATA);
  133.         manager.open(me, pi, null);
  134.  
  135.  
  136.         // This listener must be added AFTER manager.open is called,
  137.         // Otherwise the methods aren't guaranteed to fire.
  138.  
  139.         manager.setRegistrationListener(me.getUriString(), new SipRegistrationListener() {
  140.                 public void onRegistering(String localProfileUri) {
  141.                     updateStatus("Registering with SIP Server...");
  142.                 }
  143.  
  144.                 public void onRegistrationDone(String localProfileUri, long expiryTime) {
  145.                     updateStatus("Ready");
  146.                 }
  147.  
  148.                 public void onRegistrationFailed(String localProfileUri, int errorCode,
  149.                         String errorMessage) {
  150.                     updateStatus("Registration failed.  Please check settings.");
  151.                 }
  152.             });
  153.     } catch (ParseException pe) {
  154.         updateStatus("Connection Error.");
  155.     } catch (SipException se) {
  156.         updateStatus("Connection error.");
  157.     }
  158. }
  159.  
  160. /**
  161.  * Closes out your local profile, freeing associated objects into memory
  162.  * and unregistering your device from the server.
  163.  */
  164. public void closeLocalProfile() {
  165.     if (manager == null) {
  166.         return;
  167.     }
  168.     try {
  169.         if (me != null) {
  170.             manager.close(me.getUriString());
  171.         }
  172.     } catch (Exception ee) {
  173.         Log.d("WalkieTalkieActivity/onDestroy", "Failed", ee);
  174.     }
  175. }
  176.  
  177. /**
  178.  * Make an outgoing call.
  179.  */
  180. public void initiateCall() {
  181.  
  182.     updateStatus(sipAddress);
  183.  
  184.     try {
  185.         SipAudioCall.Listener listener = new SipAudioCall.Listener() {
  186.             // Much of the client's interaction with the SIP Stack will
  187.             // happen via listeners.  Even making an outgoing call, don't
  188.             // forget to set up a listener to set things up once the call is established.
  189.             @Override
  190.             public void onCallEstablished(SipAudioCall call) {
  191.                 call.startAudio();
  192.                 call.setSpeakerMode(true);
  193.                 call.toggleMute();
  194.                 updateStatus(call);
  195.             }
  196.  
  197.             @Override
  198.             public void onCallEnded(SipAudioCall call) {
  199.                 updateStatus("Ready.");
  200.             }
  201.         };
  202.  
  203.         call = manager.makeAudioCall(me.getUriString(), sipAddress, listener, 30);
  204.  
  205.     }
  206.     catch (Exception e) {
  207.         Log.i("WalkieTalkieActivity/InitiateCall", "Error", e);
  208.         if (me != null) {
  209.             try {
  210.                 manager.close(me.getUriString());
  211.             } catch (Exception ee) {
  212.                 Log.i("WalkieTalkieActivity/InitiateCall", "Error", ee);
  213.                 ee.printStackTrace();
  214.             }
  215.         }
  216.         if (call != null) {
  217.             call.close();
  218.         }
  219.     }
  220. }
  221.  
  222. /**
  223.  * Updates the status box at the top of the UI with a messege of your choice.
  224.  * @param status The String to display in the status box.
  225.  */
  226. public void updateStatus(final String status) {
  227.     // Be a good citizen.  Make sure UI changes fire on the UI thread.
  228.     this.runOnUiThread(new Runnable() {
  229.         public void run() {
  230.             TextView labelView = (TextView) findViewById(R.id.sipLabel);
  231.             labelView.setText(status);
  232.         }
  233.     });
  234. }
  235.  
  236. /**
  237.  * Updates the status box with the SIP address of the current call.
  238.  * @param call The current, active call.
  239.  */
  240. public void updateStatus(SipAudioCall call) {
  241.     String useName = call.getPeerProfile().getDisplayName();
  242.     if(useName == null) {
  243.       useName = call.getPeerProfile().getUserName();
  244.     }
  245.     updateStatus(useName + "@" + call.getPeerProfile().getSipDomain());
  246. }
  247.  
  248. /**
  249.  * Updates whether or not the user's voice is muted, depending on whether the button is pressed.
  250.  * @param v The View where the touch event is being fired.
  251.  * @param event The motion to act on.
  252.  * @return boolean Returns false to indicate that the parent view should handle the touch event
  253.  * as it normally would.
  254.  */
  255. public boolean onTouch(View v, MotionEvent event) {
  256.     if (call == null) {
  257.         return false;
  258.     } else if (event.getAction() == MotionEvent.ACTION_DOWN && call != null && call.isMuted()) {
  259.         call.toggleMute();
  260.     } else if (event.getAction() == MotionEvent.ACTION_UP && !call.isMuted()) {
  261.         call.toggleMute();
  262.     }
  263.     return false;
  264. }
  265.  
  266. public boolean onCreateOptionsMenu(Menu menu) {
  267.     menu.add(0, SET_AUTH_INFO, 0, "Edit your SIP Info.");
  268.     menu.add(0, HANG_UP, 0, "End Current Call.");
  269.  
  270.     return true;
  271. }
  272.  
  273. public boolean onOptionsItemSelected(MenuItem item) {
  274.     switch (item.getItemId()) {
  275.         case SET_AUTH_INFO:
  276.             updatePreferences();
  277.             break;
  278.         case HANG_UP:
  279.             if(call != null) {
  280.                 try {
  281.                   call.endCall();
  282.                 } catch (SipException se) {
  283.                     Log.d("WalkieTalkieActivity/onOptionsItemSelected", "Error", se);
  284.                 }
  285.                 call.close();
  286.                 updateStatus("Ready");
  287.             }
  288.             break;
  289.     }
  290.     return true;
  291. }
  292.  
  293. @Override
  294. protected Dialog onCreateDialog(int id) {
  295.     switch (id) {
  296.         case UPDATE_SETTINGS_DIALOG:
  297.             return new AlertDialog.Builder(this)
  298.                     .setMessage("Please update your SIP Account Settings.")
  299.                     .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  300.                         public void onClick(DialogInterface dialog, int whichButton) {
  301.                             updatePreferences();
  302.                         }
  303.                     })
  304.                     .setNegativeButton(
  305.                             android.R.string.cancel, new DialogInterface.OnClickListener() {
  306.                                 public void onClick(DialogInterface dialog, int whichButton) {
  307.                                     // Noop.
  308.                                 }
  309.                     })
  310.                     .create();
  311.     }
  312.     return null;
  313. }
  314.  
  315. public void updatePreferences() {
  316.     Intent settingsActivity = new Intent(getBaseContext(),
  317.             SipSettings.class);
  318.     startActivity(settingsActivity);
  319. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top