Android Open Source - AmbientAlarmClock Ambient Alarm Activity






From Project

Back to project page AmbientAlarmClock.

License

The source code is released under:

<p align="center"><a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"> <img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/4.0...

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

package de.lukeslog.alarmclock.ui;
//w  ww.ja  va2 s  .  co m
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import de.lukeslog.alarmclock.ambientalarm.AmbientAlarm;
import de.lukeslog.alarmclock.ambientalarm.AmbientAlarmManager;
import de.lukeslog.alarmclock.R;
import de.lukeslog.alarmclock.support.AlarmClockConstants;
import de.lukeslog.alarmclock.support.Logger;

/**
 * Created by lukas on 03.04.14.
 */
public class AmbientAlarmActivity extends Activity
{

    public static final String PREFS_NAME = AlarmClockConstants.PREFS_NAME;
    public static String TAG = AlarmClockConstants.TAG;
    public static boolean running=false;

    PowerManager.WakeLock wakeLock;
    AmbientAlarmActivity alarmActivity;

    AmbientAlarm alarm;

    private UIUpdater updater;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ambient_alarm_base_activity);
        Logger.d(TAG, "AmbientAlarmActivity: onCreate");
        String alarmID = getIntent().getStringExtra("ambientAlarmID");
        alarm = AmbientAlarmManager.getAlarmById(alarmID);

        alarmActivity = this;

        running=true;

        turnScreenOnAndBright();

        configureSnoozeButton();
        configureAwakeButton();

        startUIUpdater();

    }

    private void startUIUpdater()
    {
        updater= new UIUpdater();
        updater.run();
    }

    @Override
    public void onPause()
    {
        super.onPause();
        running=false;
        closeActivity();

    }

    @Override
    public void  onDestroy()
    {
        Logger.d(TAG, "on Destroy alarm activity...");
        running=false;
        super.onDestroy();
    }

    private void configureAwakeButton()
    {
        final Button button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                awakeButtonPressed();
                closeActivity();
            }
        });
    }

    private void closeActivity()
    {
        Logger.d(TAG, "close activity....");
        try
        {
            if(updater!=null)
            {
                updater.onPause();
                Logger.d(TAG, "on Pause worked...");
            }
            if(wakeLock!=null)
            {
                Logger.d(TAG, "wakelog release...");
                wakeLock.release();
                Logger.d(TAG, "...worked");
            }
        }
        catch(Exception e)
        {
            Logger.e(TAG, "Exception closing the Alarm Activity because of on pause stuff...");
        }
        AmbientAlarmActivity.this.finish();
    }

    private void configureSnoozeButton()
    {
        final Button button = (Button) findViewById(R.id.button1);
        boolean showSnooze=alarm.isSnoozing();
        if (!showSnooze)
        {
            button.setVisibility(View.GONE);
        }
        else
        {

        }
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                snoozeButtonPressed();
                closeActivity();
            }
        });
    }

    private void turnScreenOnAndBright()
    {
        Logger.d(TAG, "turnscreenonandbright");
        try
        {
            Logger.e(TAG, "1");
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            Logger.e(TAG, "2");
            Logger.e(TAG, "3");

        }
        catch(Exception e)
        {
            Logger.e(TAG, "The Problem seems to be to turn on or unlock the screen");
        }
        Logger.e(TAG, "a");
        final Window win = getWindow();
        Logger.e(TAG, "b");
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        try
        {
            Logger.e(TAG, "c");
            // API 8+
            win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
            Logger.e(TAG, "d");
            }
            catch (final Throwable whocares)
            {
                // API 7+
                Logger.e(TAG, "c2");
                win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
                Logger.e(TAG, "d2");
            }
        try
        {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            Logger.e(TAG, "4");
            wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
            Logger.e(TAG, "5");
            wakeLock.acquire();
        }
        catch(Exception e)
        {
            Logger.d(TAG, "The power manager stuff does not work...");
            Logger.e(TAG, e.getLocalizedMessage());
        }
        try
        {
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.screenBrightness = 1.0F;
            getWindow().setAttributes(params);

        }
        catch(Exception e)
        {

        }
        Logger.e(TAG, ""+7);
    }

    private class  UIUpdater implements Runnable
    {
        private Handler handler = new Handler();
        public static final int delay= 500;

        @Override
        public void run()
        {
           if(running)
           {
               Logger.d(TAG, "Alarm Activity is running...");
               alarm.updateAlarmUI(alarmActivity);

               handler.removeCallbacks(this); // remove the old callback
               handler.postDelayed(this, delay); // register a new one
           }
        }

        public void onPause()
        {
            running=false;
            Logger.d(TAG, "Alarm Activity update on Pause ");
            handler.removeCallbacks(this); // stop updating
        }

        public void onResume()
        {
            running=true;
            handler.removeCallbacks(this); // remove the old callback
            handler.postDelayed(this, delay); // register a new one
        }

    }

    public void awakeButtonPressedRemotely()
    {
            AmbientAlarmActivity.this.awakeButtonPressed();
            AmbientAlarmActivity.this.closeActivity();
    }

    private void awakeButtonPressed()
    {
        try
        {
            alarm.awakeButtonPressed();
            if(updater!=null)
            {
                updater.onPause();
            }
            if(wakeLock!=null)
            {
                wakeLock.release();
            }
        }
        catch(Exception e)
        {
            Logger.e(TAG, "Exception an pressing awake button");
        }
    }

    private void snoozeButtonPressed()
    {
        try
        {
            alarm.snoozeButtonPressed();
            if(updater!=null)
            {
                updater.onPause();
            }
            if(wakeLock!=null)
            {
                wakeLock.release();
            }
        }
        catch(Exception e)
        {
            Logger.e(TAG, "Exception an pressing snooze button");
        }
    }
}




Java Source Code List

de.lukeslog.alarmclock.MediaPlayer.MediaPlayerService.java
de.lukeslog.alarmclock.actions.ActionActivity.java
de.lukeslog.alarmclock.actions.ActionConfigBundle.java
de.lukeslog.alarmclock.actions.ActionManager.java
de.lukeslog.alarmclock.actions.AmbientAction.java
de.lukeslog.alarmclock.actions.CountdownActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.CountdownAction.java
de.lukeslog.alarmclock.actions.EZControlHeatActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.EZControlHeatAction.java
de.lukeslog.alarmclock.actions.EZControlPlugActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.EZControlPlugAction.java
de.lukeslog.alarmclock.actions.MusicActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.MusicAction.java
de.lukeslog.alarmclock.actions.PhilipsHueActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.PhilipsHueAction.java
de.lukeslog.alarmclock.actions.SendMailActionFragment.java
de.lukeslog.alarmclock.actions.SendMailAction.java
de.lukeslog.alarmclock.actions.WebsiteActionConfigurationFragment.java
de.lukeslog.alarmclock.actions.WebsiteAction.java
de.lukeslog.alarmclock.ambientService.dropbox.DropBox.java
de.lukeslog.alarmclock.ambientService.lastfm.Scrobbler.java
de.lukeslog.alarmclock.ambientService.mail.BackgroundMail.java
de.lukeslog.alarmclock.ambientService.mail.Mail.java
de.lukeslog.alarmclock.ambientalarm.AmbientAlarmManager.java
de.lukeslog.alarmclock.ambientalarm.AmbientAlarm.java
de.lukeslog.alarmclock.datatabse.AmbientAlarmDatabase.java
de.lukeslog.alarmclock.datatabse.DatabaseConstants.java
de.lukeslog.alarmclock.main.ClockWorkService.java
de.lukeslog.alarmclock.main.NotificationManagement.java
de.lukeslog.alarmclock.main.Timable.java
de.lukeslog.alarmclock.startup.NotificationService.java
de.lukeslog.alarmclock.startup.ServiceStarter.java
de.lukeslog.alarmclock.startup.StartUp.java
de.lukeslog.alarmclock.support.AlarmClockConstants.java
de.lukeslog.alarmclock.support.AlarmState.java
de.lukeslog.alarmclock.support.Day.java
de.lukeslog.alarmclock.support.Logger.java
de.lukeslog.alarmclock.support.Radiostations.java
de.lukeslog.alarmclock.support.Settings.java
de.lukeslog.alarmclock.support.UISupport.java
de.lukeslog.alarmclock.teardown.Teardown.java
de.lukeslog.alarmclock.ui.ActionListAdapter.java
de.lukeslog.alarmclock.ui.AlarmClockMainActivity.java
de.lukeslog.alarmclock.ui.AlarmListAdapter.java
de.lukeslog.alarmclock.ui.AmbientAlarmActivity.java
de.lukeslog.alarmclock.ui.AmbientAlarmConfigurationActivity.java
de.lukeslog.alarmclock.ui.NewAmbientAction.java
de.lukeslog.alarmclock.ui.NewSettingsFragment.java
de.lukeslog.alarmclock.ui.SettingsActivity.java
org.json2.JSONArray.java
org.json2.JSONException.java
org.json2.JSONObject.java
org.json2.JSONString.java
org.json2.JSONStringer.java
org.json2.JSONTokener.java
org.json2.JSONWriter.java