SMSPost.java :  » SMS » smspost » com » myprogram » smspost » Android Open Source

Android Open Source » SMS » smspost 
smspost » com » myprogram » smspost » SMSPost.java
package com.myprogram.smspost;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.myprogram.util.Const;
import com.myprogram.util.SMSPostUtility;



public class SMSPost extends Activity implements OnClickListener {
  TextView tvRunStatus, tvScheduleStatus, tvSetScheduleStatus, tvStatusBar;
  ImageButton buttonSMSPost;
  CheckBox checkBoxSchedule;
  
  SharedPreferences preferences;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Log.d(Const.TAG, "main screen loaded");
        
        // get handle to the shared preferences
        preferences = getSharedPreferences(Const.PREFS_NAME, MODE_PRIVATE);
        
        buttonSMSPost = (ImageButton) findViewById(R.id.buttonSMSPost);
        buttonSMSPost.setOnClickListener(this);
        
        checkBoxSchedule = (CheckBox) findViewById(R.id.checkBoxSchedule);
        checkBoxSchedule.setOnClickListener(this);
        
        tvRunStatus = (TextView) findViewById(R.id.tvRunStatus);
        tvRunStatus.setText(R.string.stringTVRunStatus);
        
        tvScheduleStatus = (TextView) findViewById(R.id.tvScheduleStatus);
        tvScheduleStatus.setText(R.string.stringTVScheduleStatus);
        
        tvStatusBar = (TextView) findViewById(R.id.tvStatusBar);
        tvStatusBar.setText(R.string.stringTVStatusBar);
      
        //perform system checks before the user starts using the application
        if (performSystemChecks()) {
          //enable all actions and show a message
          setClickableStatus(true);                    
        } else {
          //disable all actions and show a message
          setClickableStatus(false);
          tvStatusBar.setText("Use device menu to specify the URL for posting messages");
        }
    }
  
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    Log.d(Const.TAG, "button click event");
  
    switch (v.getId()) {
      case R.id.buttonSMSPost:
        Log.d(Const.TAG, "button PostSMS");
        //create a new SMSPostUtility Object
        SMSPostUtility smsPostUtility = new SMSPostUtility((Context)this);
        String msg = smsPostUtility.postMessage();
        tvRunStatus.setText(msg);
        break;
      case R.id.checkBoxSchedule:
        if (preferences.getString(Const.PUBLIC_STATIC_TIMEPICKER_IDENTIFIER, "") == "") {
          tvStatusBar.setTextColor(Color.RED);
          tvStatusBar.setText("Use device menu to specify the frequency of scheduled run");
          return;
        }
          
        if (((CheckBox)v).isChecked()) {
          setAlarm();
          tvScheduleStatus.setText("Schedule Run Enabled\n(Runs every one minute)");
        } else {
          cancelAlarm();
          tvScheduleStatus.setText("Enable Schedule Run\n(Runs every one minute)");
        }                   
        break;
    }
    
  }
  
  public void setAlarm(){
    try {
            
      Long firstTime = SystemClock.elapsedRealtime();
      
      Intent intent  = new Intent(this, AlarmReceiver.class);
      intent.putExtra("alarm_message", "Alarm Scheduled!");
      
      PendingIntent sender = PendingIntent.getBroadcast(this, Const.url_REQUEST_CODE, intent, 0);
      
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 60*1000, sender); // to be alerted 1 min from now
      
      } catch (Exception e) {
            Log.e(Const.TAG, "ERROR IN CODE:"+e.toString());
          }
  }
  
  public void cancelAlarm(){
    try {      
      Intent intent1        = new Intent(this, AlarmReceiver.class);
      PendingIntent sender1 = PendingIntent.getBroadcast(this, Const.url_REQUEST_CODE, intent1, 0);
      AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
      am1.cancel(sender1);

      Toast.makeText(this, "Schedule Cancelled",
                    Toast.LENGTH_LONG).show();

      
          } catch (Exception e) {
            Log.e(Const.TAG, "ERROR IN CODE:"+e.toString());
          }
  }
  
  //Override the function call when Options menu is triggered
  @Override
  public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;    
  }
  
  // define action when Options are selected
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.menuSetSchedule:     
            Intent timePickerDialogIntent = new Intent();
            timePickerDialogIntent.setClassName("com.myprogram.smspost", "com.myprogram.smspost.TimePicker");
            startActivityForResult(timePickerDialogIntent, Const.schedule_REQUEST_CODE);
            Log.d(Const.TAG, "timePickerDialogIntent loaded");
            break;
          case R.id.menuSetUrl:     
            //Creates an intent class
            Intent urlDialogIntent = new Intent();
            urlDialogIntent.setClassName("com.myprogram.smspost","com.myprogram.smspost.urlInputDialog");
            //startActivity(urlDialogIntent);
            startActivityForResult(urlDialogIntent, Const.url_REQUEST_CODE);
            
              break;
          }
      return true;
  }
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    super.onActivityResult(requestCode, resultCode, resultData);
    
    switch(requestCode){
    case(Const.url_REQUEST_CODE): {
      if(resultCode==Activity.RESULT_OK){
        //read the data passed from resultData intent
        //Log.d(Const.TAG, "Result_OK");
        String resultUrl = resultData.getStringExtra(Const.PUBLIC_STATIC_URL_IDENTIFIER);
        //Store the value in the preferences
        SharedPreferences.Editor editor = preferences.edit();
        //reusing the static identifier to store the key value pair in the shared preferences
        editor.putString(Const.PUBLIC_STATIC_URL_IDENTIFIER, resultUrl);
        editor.commit();
        
        //enable action buttons
        setClickableStatus(true);
        }
      }
    case(Const.schedule_REQUEST_CODE): {
      if(resultCode==Activity.RESULT_OK){
        //read the data passed from resultData intent
        //Log.d(Const.TAG, "Result_OK");
        String resultFrequency = resultData.getStringExtra(Const.PUBLIC_STATIC_TIMEPICKER_IDENTIFIER);
        
        SharedPreferences.Editor editor = preferences.edit();
        //reusing the static identifier to store the key value pair in the shared preferences
        editor.putString(Const.PUBLIC_STATIC_TIMEPICKER_IDENTIFIER, resultFrequency);
        editor.commit();
        
        //enable action buttons
        setClickableStatus(true);
        }
      }
    }
  }
  
  // System checks
  private boolean performSystemChecks(){
    if (preferences.getString(Const.PUBLIC_STATIC_URL_IDENTIFIER, "") == "")
      return false;
    else 
      return true;    
  }
  
  //enable & disable of activity buttons
  private void setClickableStatus(boolean status){
    buttonSMSPost.setClickable(status);
      checkBoxSchedule.setClickable(status);
      
      if (status) {
        tvStatusBar.setTextColor(Color.WHITE);
        tvStatusBar.setText("Use device menu to access more options");
      }
      else {
        tvStatusBar.setTextColor(Color.RED);
      }
      
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.