Android Open Source - datapulse Schedule






From Project

Back to project page datapulse.

License

The source code is released under:

GNU General Public License

If you think the Android project datapulse listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * //from   ww  w.j  a  v  a2 s .  c o  m
 */
package ar.idack.datapulse.activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import ar.idack.datapulse.R;

/**
 * @author Ivan Dackiewicz
 * 
 */
public class Schedule extends FragmentActivity {

  private static final String TAG = "DataPulse-Scheduler";

  private CheckBox mWifiCheckbox;
  private CheckBox mDataCheckbox;
  private Spinner mFrequencySpinner;
  private Spinner mDurationSpinner;
  private Button mAcceptButton;

  private AlarmManager mAlarmManager;
  private ConnectivityManager mConnectivityManager;

  private long mFrequency;
  private long mDuration;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_schedule);

    mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    mWifiCheckbox = (CheckBox) findViewById(R.id.schedule_wifi_checkbox);
    mDataCheckbox = (CheckBox) findViewById(R.id.schedule_data_checkbox);

    // If device has not mobile data, removes the checkbox.
    if (!mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isAvailable()) {
      mDataCheckbox.setVisibility(View.GONE);
    }

    // If device has not wifi, removes the checkbox.
    if (!mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        .isAvailable()) {
      mWifiCheckbox.setVisibility(View.GONE);
    }

    mFrequencySpinner = (Spinner) findViewById(R.id.schedule_frequency_spinner);
    ArrayAdapter<CharSequence> frequencyAdapter = ArrayAdapter.createFromResource(
        this, R.array.frequency_array, R.layout.dropdown_item);
    mFrequencySpinner.setAdapter(frequencyAdapter);
    mFrequencySpinner.setOnItemSelectedListener(new FrequecyListener());

    mDurationSpinner = (Spinner) findViewById(R.id.schedule_duration_spinner);
    ArrayAdapter<CharSequence> durationAdapter = ArrayAdapter.createFromResource(
        this, R.array.duration_array, R.layout.dropdown_item);
    mDurationSpinner.setAdapter(durationAdapter);
    mDurationSpinner.setOnItemSelectedListener(new DurationListener());

    mAcceptButton = (Button) findViewById(R.id.schedule_accept_button);
    mAcceptButton.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        setSchedule();
        finish();
      }
    });
  }

  private void setSchedule() {
    boolean wifi = mWifiCheckbox.isChecked();
    boolean data = mDataCheckbox.isChecked();

    // Creates beginning intent.
    Intent beginIntent = new Intent(Schedule.this, ScheduleAlarmReceiver.class);
    beginIntent.putExtra(ScheduleAlarmReceiver.WIFI, wifi);
    beginIntent.putExtra(ScheduleAlarmReceiver.DATA, data);
    beginIntent.putExtra(ScheduleAlarmReceiver.DURATION, mDuration);

    // Create each pending intent.
    PendingIntent beginConnectionIntent = PendingIntent.getBroadcast(
        Schedule.this, ScheduleAlarmReceiver.REQUEST_ON, beginIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);

    if (!wifi && !data) {
      mAlarmManager.cancel(beginConnectionIntent);
      Log.i(TAG, "Schedule canceled.");
    } else {
      mAlarmManager.setInexactRepeating(
          AlarmManager.ELAPSED_REALTIME_WAKEUP,
          SystemClock.elapsedRealtime(), mFrequency,
          beginConnectionIntent);
      Log.i(TAG, "Schedule setted.");
    }
  }

  /**
   * OnItemSelectedListener for the frequency spinner.
   * 
   * @author Ivan Dackiewicz<ivandack@gmail.com>
   */
  private class FrequecyListener implements OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
        int position, long id) {
      switch (position) {
      case 0:
        mFrequency = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
        break;
      case 1:
        mFrequency = AlarmManager.INTERVAL_HALF_HOUR;
        break;
      case 2:
        mFrequency = AlarmManager.INTERVAL_HOUR;
        break;
      case 3:
        mFrequency = AlarmManager.INTERVAL_HALF_DAY;
        break;
      case 4:
        mFrequency = AlarmManager.INTERVAL_DAY;
        break;
      default:
        mFrequency = -1;
        break;
      }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
      mFrequency = -1;
    }
  }

  /**
   * OnItemSelectedListener for the duration spinner.
   * 
   * @author Ivan Dackiewicz<ivandack@gmail.com>
   */
  private class DurationListener implements OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
        int position, long id) {
      switch (position) {
      case 0:
        mDuration = 5;
        break;
      case 1:
        mDuration = 10;
        break;
      default:
        mDuration = -1;
        break;
      }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
      mDuration = -1;
    }
  }
}




Java Source Code List

ar.idack.datapulse.activity.ScheduleAlarmReceiver.java
ar.idack.datapulse.activity.ScheduleList.java
ar.idack.datapulse.activity.Schedule.java
ar.idack.datapulse.model.Pulse.java