Permalink
Browse files

beta sms service fixed, using intentservice now, broadcast to refresh

saldo
  • Loading branch information...
1 parent 52254a1 commit 5d9122853101eeec39e9ef87e239551a9157d080 @arminc committed Sep 29, 2011
View
@@ -27,12 +27,10 @@
android:resource="@xml/betasms_widget_provider" />
</receiver>
<service android:name=".widget.WidgetUpdateService" />
- <service android:enabled="true" android:name="utils.BetaSMSService" />
+ <service android:enabled="true" android:name=".utils.BetaSMSService" />
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
- <uses-permission android:name="android.permission.READ_SMS" />
- <uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
@@ -32,6 +32,7 @@
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
+import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@@ -101,20 +102,20 @@ public void onCreate(Bundle savedInstanceState)
// Set the view
Log.d(Const.TAG_MAIN, "Creating the view and the rest of the GUI.");
- //allow custom title
+ // allow custom title
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.betasms);
- //set custom title
+ // set custom title
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
txtTitleSaldoValue = (TextView) findViewById(R.id.txtTitleSaldoValue);
- txtTitleSaldo = (TextView) findViewById(R.id.txtTitleSaldo);
-
- //show providers
+ txtTitleSaldo = (TextView) findViewById(R.id.txtTitleSaldo);
+
+ // show providers
providers = getResources().getStringArray(R.array.providers);
- //get the rest of the ui components
+ // get the rest of the ui components
to = (AutoCompleteTextView) findViewById(R.id.txtTo);
txtTextCount = (TextView) findViewById(R.id.txtTextCount);
txtSmsText = (EditText) findViewById(R.id.txtSmsText);
@@ -138,7 +139,7 @@ public void onCreate(Bundle savedInstanceState)
// Set the intent for selecting the contact
intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
- // When taped it will fire up an intent for showing Contacts,
+ // When tapped it will fire up an intent for showing Contacts,
// when a contact is selected it will return and fire up
// onActivityResult function
contact.setOnClickListener(new View.OnClickListener()
@@ -291,24 +292,23 @@ public boolean onOptionsItemSelected(MenuItem item)
*/
private void onSend()
{
- if (!checkIfFieldAreEmpty())
- {
- return;
- }
- Intent in = new Intent(this, BetaSMSService.class);
- in.putExtra("number", to.getText().toString());
- in.putExtra("sms", txtSmsText.getText().toString());
- startService(in);
- Toast.makeText(Beta_SMS.this, getText(R.string.SMS_SENDING), Toast.LENGTH_SHORT).show();
- if (properties.getBoolean("DeleteTextKey", false))
+ if (fieldsNotEmpty())
{
- // reset everything to empty
- txtSmsText.setText("");
- to.setText("");
+ Intent in = new Intent(this, BetaSMSService.class);
+ in.putExtra(BetaSMSService.TO, to.getText().toString());
+ in.putExtra(BetaSMSService.SMS, txtSmsText.getText().toString());
+ startService(in);
+ Toast.makeText(Beta_SMS.this, getText(R.string.SMS_SENDING), Toast.LENGTH_SHORT).show();
+ if (properties.getBoolean("DeleteTextKey", false))
+ {
+ // reset everything to empty
+ txtSmsText.setText("");
+ to.setText("");
+ }
}
}
- private boolean checkIfFieldAreEmpty()
+ private boolean fieldsNotEmpty()
{
Log.d(Const.TAG_MAIN, "Checking to see if all fields are filled.");
if (to.getText().toString().length() < 1)
@@ -355,14 +355,14 @@ protected void onPostExecute(String anwser)
@Override
protected void onDestroy()
{
- stopService(new Intent(this, BetaSMSService.class));
+ //stopService(new Intent(this, BetaSMSService.class));
super.onDestroy();
}
private void checkForIntent(Intent receivedIntent)
{
Bundle extras = receivedIntent.getExtras();
- //if it contains extras then it's our own bundle
+ // if it contains extras then it's our own bundle
if (extras != null)
{
Iterator<String> i = extras.keySet().iterator();
@@ -372,16 +372,26 @@ private void checkForIntent(Intent receivedIntent)
Log.d(Const.TAG_MAIN, "Bundle key: " + tmp + " value: " + extras.getString(tmp));
}
Intent in = new Intent(this, BetaSMSService.class);
- in.putExtra("number", extras.getString("number"));
- in.putExtra("sms", extras.getString("sms"));
+ in.putExtra(BetaSMSService.TO, extras.getString(BetaSMSService.TO));
+ in.putExtra(BetaSMSService.SMS, extras.getString(BetaSMSService.SMS));
startService(in);
this.finish();
}
- //if it only contains data then it has the number to sms to
+ // if it only contains data then it has the number to sms to
else if (receivedIntent.getData() != null)
{
Log.d(Const.TAG_MAIN, "Got an non empty Intent.");
checkDataIncomingIntent(receivedIntent);
}
}
+
+ public class ResponseReceiver extends BroadcastReceiver {
+ public static final String ACTION_RESP = "nl.coralic.beta.sms.REFRESH_SALDO";
+ @Override
+ public void onReceive(Context context, Intent intent) {
+
+ showBalance();
+ }
+
+ }
}
@@ -18,101 +18,77 @@
package nl.coralic.beta.sms.utils;
import nl.coralic.beta.sms.Beta_SMS;
+import nl.coralic.beta.sms.Beta_SMS.ResponseReceiver;
import nl.coralic.beta.sms.betamax.BetamaxHandler;
-import nl.coralic.beta.sms.utils.objects.Const;
import nl.coralic.beta.sms.utils.objects.Response;
import android.R;
+import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
-import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
-import android.os.IBinder;
import android.preference.PreferenceManager;
+import android.util.Log;
-public class BetaSMSService extends Service
+public class BetaSMSService extends IntentService
{
+ public static final String TO = "to";
+ public static final String SMS = "sms";
+
private SharedPreferences properties;
- @Override
- public IBinder onBind(Intent arg0)
+ public BetaSMSService()
{
- return null;
+ super("BetaSMSService");
}
@Override
- public void onCreate()
+ protected void onHandleIntent(Intent intent)
{
properties = PreferenceManager.getDefaultSharedPreferences(BetaSMSService.this);
- }
-
- @Override
- public void onStart(Intent intent, int startId)
- {
-
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId)
- {
// TODO: if the sms is bigger than 160charts it will fail, fix!
- String to = intent.getExtras().getString("number");
- String sms = intent.getExtras().getString("sms");
-
- AsyncTask<String, Void, Response> task = new AsyncTask<String, Void, Response>()
+ String to = intent.getExtras().getString(TO);
+ String sms = intent.getExtras().getString(SMS);
+ Response response = BetamaxHandler.sendSMS(properties.getString("ServiceKey", ""), properties.getString("UsernameKey", ""), properties.getString("PasswordKey", ""),
+ properties.getString("PhoneKey", ""), to, sms);
+ if (response.isResponseOke() == true)
{
-
- private String to;
- private String sms;
- private int startId;
-
- @Override
- protected Response doInBackground(String... s)
+ //send broadcast
+ Intent broadcastIntent = new Intent();
+ broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
+ broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
+ sendBroadcast(broadcastIntent);
+
+
+ SMSHelper smsHelper = new SMSHelper();
+ if (properties.getBoolean("SaveSMSKey", false))
{
- to = s[0];
- sms = s[1];
- startId = Integer.valueOf(s[2]);
- return BetamaxHandler.sendSMS(properties.getString("ServiceKey", ""), properties.getString("UsernameKey", ""), properties.getString("PasswordKey", ""),
- properties.getString("PhoneKey", ""), to, sms);
+ smsHelper.addSMS(getContentResolver(), sms, to);
}
+
+ }
+ else
+ {
+ NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- @Override
- protected void onPostExecute(Response anwser)
- {
- if (anwser.isResponseOke() == true)
- {
- SMSHelper smsHelper = new SMSHelper();
- if (properties.getBoolean("SaveSMSKey", false))
- {
- smsHelper.addSMS(getContentResolver(), sms, to);
- }
- }
- else
- {
- NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
-
- int icon = R.drawable.ic_dialog_alert;
- CharSequence text = "Beta-SMS failed to send SMS";
- CharSequence contentTitle = "Can't send to: " + to;
- CharSequence contentText = anwser.getErrorMessage();
- long when = System.currentTimeMillis();
+ int icon = R.drawable.ic_dialog_alert;
+ CharSequence text = "Beta-SMS failed to send SMS";
+ CharSequence contentTitle = "Can't send to: " + to;
+ CharSequence contentText = response.getErrorMessage();
+ long when = System.currentTimeMillis();
- Intent i = new Intent(getApplicationContext(), Beta_SMS.class);
- i.putExtra("number", to);
- i.putExtra("sms", sms);
- PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
- Notification notification = new Notification(icon, text, when);
- notification.flags |= Notification.FLAG_AUTO_CANCEL;
- notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
- notificationManager.notify(startId, notification);
- }
- }
- };
- task.execute(to, sms, String.valueOf(startId));
- return 1;
+ Intent i = new Intent(getApplicationContext(), Beta_SMS.class);
+ i.putExtra(TO, to);
+ i.putExtra(SMS, sms);
+ PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
+ Notification notification = new Notification(icon, text, when);
+ notification.flags |= Notification.FLAG_AUTO_CANCEL;
+ notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
+ notificationManager.notify(1, notification);
+ }
}
}

0 comments on commit 5d91228

Please sign in to comment.