Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.90 KB | None | 0 0
  1. package com.bitcallproject.RNPlivo;
  2.  
  3. import com.bitcallproject.MainActivity;
  4. import com.bitcallproject.RNPlivo.ServiceCallbacks;
  5.  
  6.  
  7. import java.lang.Exception;
  8. import java.lang.Error;
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.HashMap;
  14.  
  15. import android.os.SystemClock;
  16. import android.app.Service;
  17. import android.os.IBinder;
  18. import android.os.Binder;
  19. import android.content.Intent;
  20. import android.content.IntentFilter;
  21. import android.content.ServiceConnection;
  22. import android.content.ComponentName;
  23. import android.content.BroadcastReceiver;
  24. import android.content.Context;
  25. import android.util.Log;
  26. import android.view.Window;
  27. import android.view.WindowManager;
  28. import android.app.ActivityManager;
  29. import android.app.ActivityManager.*;
  30. import android.telephony.TelephonyManager;
  31. import android.app.KeyguardManager;
  32. import android.app.KeyguardManager.KeyguardLock;
  33. import android.app.PendingIntent;
  34. import android.app.AlarmManager;
  35.  
  36. import com.plivo.endpoint.Endpoint;
  37. import com.plivo.endpoint.EventListener;
  38. import com.plivo.endpoint.Incoming;
  39. import com.plivo.endpoint.Outgoing;
  40.  
  41. import java.lang.*;
  42.  
  43.  
  44. public class PlivoService extends Service implements EventListener{
  45. KeyguardLock keyguardLock;
  46. private BroadcastReceiver receiver;
  47. private boolean isAppBinded;
  48. private Endpoint endpoint;
  49. private Outgoing outCall;
  50. private Incoming inCall;
  51. public final static String PLIVO_PASSWORD = "sip_user";
  52. private ServiceCallbacks serviceCallbacks;
  53. private String username;
  54. private static final String ACTION_WAKEUP = "com.bitcallproject.WAKEUP";
  55. private static final String STATUS = "com.bitcallproject.STATUS";
  56. private static final String SLEEP = "com.bitcallproject.SLEEP";
  57. private static final String READY = "com.bitcallproject.READY";
  58. private static final String RELEASE_LOCK = "com.bitcallproject.RELEASE_LOCK";
  59. private static final String KEEP_ALIVE = "com.bitcallproject.KEEP_ALIVE";
  60. private static final String ACTION_LOGIN = "com.bitcallproject.ACTION_LOGIN";
  61. private static final String ACTION_INCOMING_CALL = "com.bitcallproject.ACTION_INCOMING_CALL";
  62. private static final String ACTION_BIND_APP = "com.bitcallproject.ACTION_BIND_APP";
  63. private static final String EVENT_SERVICE_STARTED = "com.bitcallproject.EVENT_SERVICE_STARTED";
  64. private static final String EVENT_END_CALL = "com.bitcallproject.EVENT_END_CALL";
  65. private static final String ACTION_REJECT_CALL = "com.bitcallproject.ACTION_REJECT_CALL";
  66. private static final String EVENT_REJECT_CALL = "com.bitcallproject.EVENT_REJECT_CALL";
  67. private static final String EVENT_HANG_UP = "com.bitcallproject.EVENT_HANG_UP";
  68. private static final String ACTION_HANG_UP = "com.bitcallproject.ACTION_HANG_UP";
  69. private Intent currIntent;
  70.  
  71.  
  72. @Override
  73. public int onStartCommand(Intent intent, int flags, int startId) {
  74. currIntent = intent;
  75. initService();
  76. username = intent.getStringExtra("username");
  77. Log.v("ReactNativeJS", "Starting service with username = " + username);
  78. if (endpoint != null){
  79. Log.v("ReactNativeJS", "Resettimng endpoint");
  80. endpoint.resetEndpoint();
  81. endpoint = null;
  82. endpoint = Endpoint.newInstance(true, this);
  83. }
  84. login(username);
  85. return START_NOT_STICKY;
  86. //return START_REDELIVER_INTENT;
  87. }
  88.  
  89. @Override
  90. public IBinder onBind(Intent intent) {
  91. return null;
  92. }
  93.  
  94. public void call(final String number, final String user, final String fromNumber){
  95. TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  96. String countryCode = tm.getNetworkCountryIso().toUpperCase();
  97. String formatedNumber = formatOutboundNumber(number, getCountryCode(countryCode));
  98. Log.v("ReactNativeJS", "Country code: " + getCountryCode(countryCode));
  99. Log.v("ReactNativeJS", "Formated number: " + formatedNumber);
  100. Map extraArgs = new HashMap();
  101. extraArgs.put("X-PH-User", user);
  102. extraArgs.put("X-PH-Number", fromNumber);
  103. extraArgs.put("X-PH-RawNumber", number.replace("+", ""));
  104. if (endpoint == null){
  105. Log.v("ReactNativeJS", "Endpoint is null");
  106. return;
  107. }
  108. outCall = new Outgoing(endpoint);
  109. outCall = endpoint.createOutgoingCall();
  110. if (outCall == null){
  111. Log.v("ReactNativeJS", "Outgoing call is null");
  112. return;
  113. }
  114. outCall.callH(formatedNumber, extraArgs);
  115. }
  116.  
  117. private void handleIncomingCall() {
  118. Log.v("ReactNativeJS", "Handling incoming call!!!");
  119. broadcastEvent(KEEP_ALIVE);
  120. int sipLen = "sip:".length();
  121. Intent payload = new Intent();
  122. payload.setAction(ACTION_INCOMING_CALL);
  123. String fromContact = inCall.getFromSip();
  124. Map<String, String> header = inCall.getHeaderDict();
  125. HashMap<String, String> hashHeader = new HashMap<String, String>(header);
  126.  
  127. payload.putExtra("header", hashHeader);
  128. payload.putExtra("fromContact", fromContact.substring(sipLen, fromContact.length()));
  129. sendBroadcast(payload);
  130. }
  131.  
  132. /*@Override
  133. public void onCreate()*/public void initService(){
  134. //super.onCreate();
  135. IntentFilter filter = new IntentFilter();
  136. filter.addAction(READY);
  137. filter.addAction(SLEEP);
  138. filter.addAction(ACTION_LOGIN);
  139. filter.addAction(ACTION_BIND_APP);
  140. filter.addAction(ACTION_REJECT_CALL);
  141. filter.addAction(ACTION_HANG_UP);
  142. receiver = new BroadcastReceiver() {
  143. @Override
  144. public void onReceive(Context context, Intent intent) {
  145. //set true to avoid start app, just wake up
  146. Log.v("ReactNativeJS", "Service intent received: " + intent.getAction() + " incCall = " + Boolean.toString(inCall != null));
  147. if (intent.getAction().equals(ACTION_BIND_APP)) {
  148. isAppBinded = true;
  149. } else if (intent.getAction().equals(READY) && inCall != null) {
  150. handleIncomingCall();
  151. } else if (intent.getAction().equals(SLEEP)) {
  152. broadcastEvent(ACTION_WAKEUP);
  153. //don't login if username from broadcast equals to username from startservice
  154. } /*else if (intent.getAction().equals(ACTION_LOGIN)) {
  155. String currUsername = intent.getStringExtra("username");
  156. if (username != currUsername) {
  157. username = currUsername;
  158. login(username);
  159. }
  160. } */else if (intent.getAction().equals(ACTION_REJECT_CALL)) {
  161. reject();
  162. } else if (intent.getAction().equals(ACTION_HANG_UP)) {
  163. hangup();
  164. }
  165. }
  166. };
  167. registerReceiver(receiver, filter);
  168. broadcastEvent(EVENT_SERVICE_STARTED);
  169. }
  170.  
  171. @Override
  172. public void onDestroy(){
  173. if (receiver != null) {
  174. unregisterReceiver(receiver);
  175. receiver = null;
  176. }
  177. super.onDestroy();
  178. }
  179.  
  180. private void launchActivity() {
  181. KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
  182. keyguardLock = keyguardManager.newKeyguardLock("TAG");
  183. keyguardLock.disableKeyguard();
  184. Intent intent = new Intent(this, MainActivity.class);
  185. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  186. this.startActivity(intent);
  187. }
  188.  
  189. public void login(final String username) {
  190. if (endpoint == null) {
  191. Log.v("ReactNativeJS", "Creating endpoint instance!!!");
  192. try {
  193. endpoint = Endpoint.newInstance(true, this);
  194. }
  195. catch (Exception e) {
  196. e.printStackTrace();
  197. Log.v("ReactNativeJS", "exception" + e.getMessage());
  198. }
  199. }
  200. Log.v("ReactNativeJS", "After reset before login");
  201. try {
  202. endpoint.login(username, PLIVO_PASSWORD);
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. Log.v("ReactNativeJS", "exception" + e.getMessage());
  206. }
  207. }
  208.  
  209. public void reject(){
  210. inCall.reject();
  211. broadcastEvent(EVENT_REJECT_CALL);
  212. }
  213.  
  214. public void hangup(){
  215. outCall.hangup();
  216. broadcastEvent(EVENT_HANG_UP);
  217. }
  218.  
  219. public void onLogin(){
  220. Log.v("ReactNativeJS", "Logged in");
  221. broadcastEvent(RELEASE_LOCK);
  222. }
  223.  
  224.  
  225. public void onLogout() {
  226. }
  227.  
  228. public void onLoginFailed() {
  229. Log.v("ReactNativeJS", "Failed to login");
  230. }
  231.  
  232. @Override
  233. public void onTaskRemoved(Intent rootIntent) {
  234. super.onTaskRemoved(rootIntent);
  235. endpoint.resetEndpoint();
  236. Log.v("ReactNativeJS", "Application has gone, and never return with rootusername = " + currIntent.getStringExtra("username"));
  237. PendingIntent pintent = PendingIntent.getService(PlivoService.this, 0, currIntent, 0);
  238. AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  239. alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 6000, pintent);
  240. }
  241.  
  242. public void onIncomingCall(Incoming incoming) {
  243. Log.v("ReactNativeJS", "Incoming call has come. isAppBinded = " + Boolean.toString(isAppBinded));
  244. inCall = incoming;
  245. if (isAppBinded == true){
  246. broadcastEvent(STATUS);
  247. } else {
  248. Log.v("ReactNativeJS", "Launching new activity");
  249. launchActivity();
  250. }
  251. }
  252.  
  253. private void broadcastEvent(final String eventName){
  254. Intent i = new Intent();
  255. i.setAction(eventName);
  256. sendBroadcast(i);
  257. Log.v("ReactNativeJS", "Service intent sent: " + i.getAction());
  258. }
  259.  
  260. public void onIncomingCallHangup(Incoming incoming) {
  261. broadcastEvent(EVENT_HANG_UP);
  262. }
  263.  
  264. public void onIncomingCallRejected(Incoming incoming) {
  265. broadcastEvent(EVENT_REJECT_CALL);
  266. }
  267.  
  268. public void onOutgoingCall(Outgoing outgoing) {
  269. broadcastEvent(KEEP_ALIVE);
  270. }
  271.  
  272. public void onOutgoingCallAnswered(Outgoing outgoing) {
  273. }
  274.  
  275. public void onOutgoingCallHangup(Outgoing outgoing) {
  276. Log.v("ReactNativeJS", "Trigger onOutgoingCallHangup");
  277. broadcastEvent(RELEASE_LOCK);
  278. }
  279.  
  280. public void onOutgoingCallRejected(Outgoing outgoing) {
  281. Log.v("ReactNativeJS", "Trigger onOutgoingCallRejected");
  282. broadcastEvent(RELEASE_LOCK);
  283. }
  284.  
  285. public void onOutgoingCallInvalid(Outgoing outgoing) {
  286. Log.v("ReactNativeJS", "Trigger onOutgoingCallInvalid");
  287. broadcastEvent(RELEASE_LOCK);
  288. }
  289.  
  290. private String getCountryCode(String countryCode){
  291. Map<String, String> countryCodes = new HashMap<String, String>() {{
  292. put("IL", "972"); put("AF", "93"); put("AL", "355"); put("DZ", "213"); put("AS", "1");
  293. put("AD", "376"); put("AO", "244"); put("AI", "1"); put("AG", "1"); put("AR", "54");
  294. put("AM", "374"); put("AW", "297"); put("AU", "61"); put("AT", "43"); put("AZ", "994");
  295. put("BS", "1"); put("BH", "973"); put("BD", "880"); put("BB", "1"); put("BY", "375");
  296. put("BE", "32"); put("BZ", "501"); put("BJ", "229"); put("BM", "1"); put("BT", "975");
  297. put("BA", "387"); put("BW", "267"); put("BR", "55"); put("IO", "246"); put("BG", "359");
  298. put("BF", "226"); put("BI", "257"); put("KH", "855"); put("CM", "237"); put("CA", "1");
  299. put("CV", "238"); put("KY", "345"); put("CF", "236"); put("TD", "235"); put("CL", "56");
  300. put("CN", "86"); put("CX", "61"); put("CO", "57"); put("KM", "269"); put("CG", "242");
  301. put("CK", "682"); put("CR", "506"); put("HR", "385"); put("CU", "53"); put("CY", "537");
  302. put("CZ", "420"); put("DK", "45"); put("DJ", "253"); put("DM", "1"); put("DO", "1");
  303. put("EC", "593"); put("EG", "20"); put("SV", "503"); put("GQ", "240"); put("ER", "291");
  304. put("EE", "372"); put("ET", "251"); put("FO", "298"); put("FJ", "679"); put("FI", "358");
  305. put("FR", "33"); put("GF", "594"); put("PF", "689"); put("GA", "241"); put("GM", "220");
  306. put("GE", "995"); put("DE", "49"); put("GH", "233"); put("GI", "350"); put("GR", "30");
  307. put("GL", "299"); put("GD", "1"); put("GP", "590"); put("GU", "1"); put("GT", "502");
  308. put("GN", "224"); put("GW", "245"); put("GY", "595"); put("HT", "509"); put("HN", "504");
  309. put("HU", "36"); put("IS", "354"); put("IN", "91"); put("ID", "62"); put("IQ", "964");
  310. put("IE", "353"); put("IL", "972"); put("IT", "39"); put("JM", "1"); put("JP", "81");
  311. put("JO", "962"); put("KZ", "77"); put("KE", "254"); put("KI", "686"); put("KW", "965");
  312. put("KG", "996"); put("LV", "371"); put("LB", "961"); put("LS", "266"); put("LR", "231");
  313. put("LI", "423"); put("LT", "370"); put("LU", "352"); put("MG", "261"); put("MW", "265");
  314. put("MY", "60"); put("MV", "960"); put("ML", "223"); put("MT", "356"); put("MH", "692");
  315. put("MQ", "596"); put("MR", "222"); put("MU", "230"); put("YT", "262"); put("MX", "52");
  316. put("MC", "377"); put("MN", "976"); put("ME", "382"); put("MS", "1"); put("MA", "212");
  317. put("MM", "95"); put("NA", "264"); put("NR", "674"); put("NP", "977"); put("NL", "31");
  318. put("AN", "599"); put("NC", "687"); put("NZ", "64"); put("NI", "505"); put("NE", "227");
  319. put("NG", "234"); put("NU", "683"); put("NF", "672"); put("MP", "1"); put("NO", "47");
  320. put("OM", "968"); put("PK", "92"); put("PW", "680"); put("PA", "507"); put("PG", "675");
  321. put("PY", "595"); put("PE", "51"); put("PH", "63"); put("PL", "48"); put("PT", "351");
  322. put("PR", "1"); put("QA", "974"); put("RO", "40"); put("RW", "250"); put("WS", "685");
  323. put("SM", "378"); put("SA", "966"); put("SN", "221"); put("RS", "381"); put("SC", "248");
  324. put("SL", "232"); put("SG", "65"); put("SK", "421"); put("SI", "386"); put("SB", "677");
  325. put("ZA", "27"); put("GS", "500"); put("ES", "34"); put("LK", "94"); put("SD", "249");
  326. put("SR", "597"); put("SZ", "268"); put("SE", "46"); put("CH", "41"); put("TJ", "992");
  327. put("TH", "66"); put("TG", "228"); put("TK", "690"); put("TO", "676"); put("TT", "1");
  328. put("TN", "216"); put("TR", "90"); put("TM", "993"); put("TC", "1"); put("TV", "688");
  329. put("UG", "256"); put("UA", "380"); put("AE", "971"); put("GB", "44"); put("US", "1");
  330. put("UY", "598"); put("UZ", "998"); put("VU", "678"); put("WF", "681"); put("YE", "967");
  331. put("ZM", "260"); put("ZW", "263"); put("BO", "591"); put("BN", "673"); put("CC", "61");
  332. put("CD", "243"); put("CI", "225"); put("FK", "500"); put("GG", "44"); put("VA", "379");
  333. put("HK", "852"); put("IR", "98"); put("IM", "44"); put("JE", "44"); put("KP", "850");
  334. put("KR", "82"); put("LA", "856"); put("LY", "218"); put("MO", "853"); put("MK", "389");
  335. put("FM", "691"); put("MD", "373"); put("MZ", "258"); put("PS", "970"); put("PN", "872");
  336. put("RE", "262"); put("RU", "7"); put("BL", "590"); put("SH", "290"); put("KN", "1");
  337. put("LC", "1"); put("MF", "590"); put("PM", "508"); put("VC", "1"); put("ST", "239");
  338. put("SO", "252"); put("SJ", "47"); put("SY", "963"); put("TW", "886"); put("TZ", "255");
  339. put("TL", "670"); put("VE", "58"); put("VN", "84"); put("VG", "1"); put("VI", "1");
  340. }};
  341. return countryCodes.get(countryCode);
  342. }
  343.  
  344. private String formatOutboundNumber(String number, String countryCode){
  345. String cleanNumber = number.replaceAll("[^A-Za-z0-9]", "");
  346. int len = cleanNumber.length();
  347. if (cleanNumber.startsWith("00")){
  348. return "+" + countryCode + cleanNumber.substring(2, len);
  349. }
  350. if (cleanNumber.startsWith("0")){
  351. return "+" + countryCode + cleanNumber.substring(1, len);
  352. }
  353. return "+" + cleanNumber;
  354. }
  355.  
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement