You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ADBReceiver.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.rob57530.smsserver;
  2. import android.app.Activity;
  3. import android.app.PendingIntent;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.content.SharedPreferences;
  9. import android.os.AsyncTask;
  10. import android.preference.PreferenceManager;
  11. import android.telephony.SmsManager;
  12. import android.util.Base64;
  13. import org.apache.http.HttpResponse;
  14. import org.apache.http.HttpStatus;
  15. import org.apache.http.StatusLine;
  16. import org.apache.http.client.ClientProtocolException;
  17. import org.apache.http.client.HttpClient;
  18. import org.apache.http.client.methods.HttpGet;
  19. import org.apache.http.impl.client.DefaultHttpClient;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. public class ADBReceiver extends BroadcastReceiver {
  23. private String m_http;
  24. class RequestTask extends AsyncTask<String, String, String>
  25. {
  26. @Override
  27. protected String doInBackground(String... uri)
  28. {
  29. HttpClient httpclient = new DefaultHttpClient();
  30. HttpResponse response;
  31. String responseString = null;
  32. try
  33. {
  34. HttpGet get = new HttpGet(uri[0]);
  35. get.setHeader("Host", m_http);
  36. get.setHeader("Authorization", "Basic " + Base64.encodeToString("__gates__:gatespasswd".getBytes(), Base64.NO_WRAP));
  37. response = httpclient.execute(get);
  38. StatusLine statusLine = response.getStatusLine();
  39. if (statusLine.getStatusCode() == HttpStatus.SC_OK)
  40. {
  41. ByteArrayOutputStream out = new ByteArrayOutputStream();
  42. response.getEntity().writeTo(out);
  43. out.close();
  44. responseString = out.toString();
  45. }
  46. else
  47. {
  48. response.getEntity().getContent().close();
  49. throw new IOException(statusLine.getReasonPhrase());
  50. }
  51. }
  52. catch (ClientProtocolException e)
  53. {
  54. e.printStackTrace();
  55. }
  56. catch (IOException e)
  57. {
  58. e.printStackTrace();
  59. }
  60. return responseString;
  61. }
  62. @Override
  63. protected void onPostExecute(String result)
  64. {
  65. super.onPostExecute(result);
  66. }
  67. }
  68. public ADBReceiver() {
  69. }
  70. @Override
  71. public void onReceive(Context context, Intent intent)
  72. {
  73. SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
  74. if (!p.getBoolean("running", false))
  75. return;
  76. m_http = p.getString("http", "");
  77. String number = intent.getStringExtra("number");
  78. String text = intent.getStringExtra("text");
  79. final int id = intent.getIntExtra("id", -1);
  80. if (id == -1)
  81. return;
  82. else if (number == null || number.length() == 0
  83. || text == null || text.length() == 0)
  84. new RequestTask().execute("http://" + m_http + "/sent.php?id=" + id + "&status=7");
  85. else
  86. {
  87. try
  88. {
  89. String SENT = "SMS_SENT";
  90. PendingIntent sent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, new Intent(SENT), 0);
  91. context.getApplicationContext().registerReceiver(new BroadcastReceiver() {
  92. @Override
  93. public void onReceive(Context arg0, Intent arg1) {
  94. int r = getResultCode();
  95. if (r == Activity.RESULT_OK)
  96. r = 1;
  97. else if (r == SmsManager.RESULT_ERROR_GENERIC_FAILURE)
  98. r = 3;
  99. else if (r == SmsManager.RESULT_ERROR_NO_SERVICE)
  100. r = 4;
  101. else if (r == SmsManager.RESULT_ERROR_NULL_PDU)
  102. r = 5;
  103. else if (r == SmsManager.RESULT_ERROR_RADIO_OFF)
  104. r = 6;
  105. else
  106. r = 7;
  107. new RequestTask().execute("http://" + m_http + "/sent.php?id="
  108. + id + "&status=" + r);
  109. }
  110. }, new IntentFilter(SENT));
  111. SmsManager.getDefault().sendTextMessage(number, null, text, sent, null);
  112. }
  113. catch (Exception e)
  114. {
  115. e.printStackTrace();
  116. }
  117. }
  118. }
  119. }