Android Open Source - sleep-timer Main Activity






From Project

Back to project page sleep-timer.

License

The source code is released under:

MIT License

If you think the Android project sleep-timer 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

/*
Copyright (c) 2013 Joel Andrews/*from   w w w.j  av a 2  s  .  c om*/
Distributed under the MIT License: http://opensource.org/licenses/MIT
 */

package com.oldsneerjaw.sleeptimer;

import android.content.Intent;
import android.app.Activity;

import java.util.Date;

/**
 * The launching point for the sleep timer.
 *
 * @author Joel Andrews
 */
public class MainActivity extends Activity {

    @Override
    protected void onResume() {
        super.onResume();

        launchActivity();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case RESULT_OK:
                break;
            case RESULT_CANCELED:
                finish();

                return;
            default:
                throw new IllegalArgumentException("Argument resultCode must be be either RESULT_OK or RESULT_CANCELED");
        }
    }

    /**
     * Launches the appropriate activity depending on whether the sleep timer is currently running
     * ({@link CountdownActivity}) or not ({@link SetTimerActivity}).
     */
    private void launchActivity() {
        Date scheduledTime = TimerManager.get(this).getScheduledTime();
        Intent intent = getActivityIntent(new Date(), scheduledTime);

        startActivityForResult(intent, 0);
    }

    /**
     * Returns an intent that can be used to launch an activity to either schedule a timer or display the timer
     * countdown depending on whether a timer is currently scheduled for the future.
     *
     * @param now The current date and time
     * @param scheduledTime The date and time at which the timer should expire
     *
     * @return An {@link Intent} to launch either {@link SetTimerActivity} or {@link CountdownActivity}.
     */
    Intent getActivityIntent(Date now, Date scheduledTime) {
        Class activityClass;
        if (scheduledTime == null || scheduledTime.getTime() <= now.getTime()) {
            // If the scheduled time occurs in the past, we treat it as if the timer is no longer running
            activityClass = SetTimerActivity.class;
        } else {
            activityClass = CountdownActivity.class;
        }

        return new Intent(this, activityClass)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }
}




Java Source Code List

com.oldsneerjaw.sleeptimer.CountdownActivity.java
com.oldsneerjaw.sleeptimer.CountdownNotifierTest.java
com.oldsneerjaw.sleeptimer.CountdownNotifier.java
com.oldsneerjaw.sleeptimer.MainActivityTest.java
com.oldsneerjaw.sleeptimer.MainActivity.java
com.oldsneerjaw.sleeptimer.PauseMusicNotifierTest.java
com.oldsneerjaw.sleeptimer.PauseMusicNotifier.java
com.oldsneerjaw.sleeptimer.PauseMusicReceiverTest.java
com.oldsneerjaw.sleeptimer.PauseMusicReceiver.java
com.oldsneerjaw.sleeptimer.PauseMusicServiceTest.java
com.oldsneerjaw.sleeptimer.PauseMusicService.java
com.oldsneerjaw.sleeptimer.SetTimerActivity.java
com.oldsneerjaw.sleeptimer.TimerManagerTest.java
com.oldsneerjaw.sleeptimer.TimerManager.java