PendingTaskActivity.java :  » App » andorganizer » com » google » code » andorganizer » Android Open Source

Android Open Source » App » andorganizer 
andorganizer » com » google » code » andorganizer » PendingTaskActivity.java
package com.google.code.andorganizer;

import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * Activity used for showing a task. It can modify it's completed field, but other fields cannot be modified using this activity, if you need to do that use EditTaskActivity
 * @see EditTaskActivity
 */
public class PendingTaskActivity extends Activity {
  
  private ContentValues values;
  
  private static final int MORE_TIME = 10 * 60 * 1000;//10 mins
  
  /**
   * Constructs and loads an Activity that provides shortcuts for easy task completion manipulation. 
   */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_task);
    
    final Intent intent = getIntent();
    
    int id = TaskScheduler.getIdFromPrefs(getApplicationContext());
    
    Cursor c = getContentResolver().query(ContentUris.withAppendedId(TasksProvider.CONTENT_URI, id), null, null, null, null);
    if (c.getCount() == 1) {
      c.moveToFirst();
      values = TaskHelper.createValuesFromCursor(c);
    }
    else {
      return;//TODO maybe throw an exception here
    }
    
    TextView desc = (TextView)findViewById(R.id.view_desc);
    desc.setText(values.getAsString(TaskHelper.KEY_DESCRIPTION));
    
    TextView starting = (TextView)findViewById(R.id.view_date_from);
    starting.setText(TaskHelper.DATE_FORMATTER.format(new Date(values.getAsLong(TaskHelper.KEY_FROM_DATE))));
    
    TextView duration = (TextView)findViewById(R.id.view_duration);
    int durationInMinutes = values.getAsInteger(TaskHelper.KEY_DURATION) / ( 1000 * 60 );
    duration.setText(TaskHelper.getFormatedDurationString(this, durationInMinutes / 60, durationInMinutes % 60));
    
    
    Button done = (Button)findViewById(R.id.view_button_done);
    done.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        getContentResolver().update(intent.getData(), TaskHelper.VALUES_COMPLETED_DONE, null, null);
        
        TaskScheduler.doAll(getApplicationContext());
        
        finish();
      }
    });
    
    Button moreTime = (Button)findViewById(R.id.view_button_more_time);
    moreTime.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent pi = new Intent(getApplicationContext(), NotifyReceiver.class);
        pi.putExtra(TaskScheduler.VALUES_POSITION_IN_EXTRA, values);
            PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, pi, 0 );
            
        AlarmManager am = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + MORE_TIME, sender);
      }
    });
    
    //remove notification
        if (getIntent().getBooleanExtra(TaskScheduler.FROM_NOTIFICATION, false)) {
           NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
           nm.cancel(TaskScheduler.NOTIFICATION_ID);
        }
        
      //TODO too simple but no time for more
  }

}
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.