package gr.mybook.mysms1;

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	EditText text1, text2;
	Button btn;
	private static final String SENT = "SMS_SENT";
	private static final String DELIVERED = "SMS_DELIVERED";
	
	SmsManager smsManager = SmsManager.getDefault();
	
	Intent i1 = new Intent(SENT);
	Intent i2 = new Intent(DELIVERED);
	IntentFilter if1 = new IntentFilter(SENT);
	IntentFilter if2 = new IntentFilter(DELIVERED);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text1 = (EditText)findViewById(R.id.EditText1);
		text2 = (EditText)findViewById(R.id.EditText2);
		btn = (Button)findViewById(R.id.Button1);
	}
	
	public void sendSMS(View v) {
		String number = text1.getText().toString();
		String text = text2.getText().toString();
		if (number.length()>0 & text.length()>0) {
		   PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, i1, 0);
           PendingIntent deliveredIntent = PendingIntent.getBroadcast(this, 0, i2, 0);
    	   MyReceiver1 mReceiver1 = new MyReceiver1();
    	   registerReceiver(mReceiver1, if1);
    	   MyReceiver2 mReceiver2 = new MyReceiver2();
    	   registerReceiver(mReceiver2, if2);
    	   try {
    		   Intent intent = new Intent(Intent.ACTION_VIEW);
    		   intent.putExtra("address", number);
    		   intent.putExtra("sms_body", text); 
    		   intent.setType("vnd.android-dir/mms-sms");
    		   startActivity(intent);

//		     smsManager.sendTextMessage(number, null, text, sentIntent, deliveredIntent);
    	   }
    	   catch (Exception e) {
    		   Toast.makeText(getApplicationContext(), "SMS failure!", Toast.LENGTH_LONG).show();
   				e.printStackTrace();
    	   }
		}
		else {
			Toast.makeText(getBaseContext(), "Please insert number & text",Toast.LENGTH_SHORT).show();
		}
	}
	
	public class MyReceiver1 extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
		}

	}
	
	public class MyReceiver2 extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
