Android Open Source - AmbientAlarmClock Action 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.actions;
/*from  w  w w . j  av a 2 s  .co  m*/
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

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

/**
 * Created by lukas on 03.04.14.
 */
public class ActionActivity extends Activity
{
    public static String TAG = AlarmClockConstants.TAG;
    Context ctx;
    AmbientAlarm alarm;
    AmbientAction action;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.action_configuration_base_activity);

        ctx = this;

        String alarmID = getIntent().getStringExtra("ambientAlarmID");
        alarm = AmbientAlarmManager.getAlarmById(alarmID);

        String actionID = getIntent().getStringExtra("ambientActionID");
        action = ActionManager.getActionByID(actionID);

        Logger.d(TAG, "ACTIONACTIVITE_>_>_>_>_>_>_ " + action.getActionName());
        setAlarmHeader();

        setJoinSnoozingCheckBox();

        setActionName();

        setTiming();

        insertActionConfigrationFRagment();
    }

    @Override
    public void onPause()
    {
        super.onPause();
        ActionActivity.this.finish();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        AmbientAlarmManager.updateDataBaseEntry(alarm);
    }

    private void setJoinSnoozingCheckBox()
    {
        final CheckBox joinSnoozing = (CheckBox) findViewById(R.id.joinsnooze);
        joinSnoozing.setChecked(action.isJoinSnoozing());
        joinSnoozing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                action.setJoinSnoozing(isChecked);
            }
        });
    }

    private void setActionName()
    {
        final TextView actionname = (TextView) findViewById(R.id.actionname);
        actionname.setText(action.getActionName());
        actionname.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
            {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
            {

            }

            @Override
            public void afterTextChanged(Editable editable)
            {
                action.setActionName(actionname.getEditableText().toString());
            }
        });
    }

    public AmbientAction getAction()
    {
        return action;
    }

    private void insertActionConfigrationFRagment()
    {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        try
        {
            Fragment configFragment = (Fragment) action.getConfigActivity().newInstance();
            fragmentTransaction.add(R.id.configspace, configFragment);
            fragmentTransaction.commit();
        }
        catch (InstantiationException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }

    private void setTiming()
    {

        String timing = getTimingValue();
        Logger.d(TAG, "TIMING VALUE-------------->"+timing);
        timing = timing.replace("-", "");
        timing = timing.replace("+", "");
        timing = timing.replace(" ", "");
        final TextView timingText = (TextView) findViewById(R.id.timingtext);

        timingText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
            {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
            {

            }

            @Override
            public void afterTextChanged(Editable editable)
            {
                reregisteraction();
            }
        });
        int t = Integer.parseInt(timing);

        final Spinner timeunits = (Spinner) findViewById(R.id.timeunits);
        List<String> list2 = new ArrayList<String>();
        String minutes = getResources().getString(R.string.minutes);
        String hours = getResources().getString(R.string.hours);
        list2.add(minutes);
        list2.add(hours);
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list2);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        timeunits.setAdapter(dataAdapter2);
        timeunits.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                reregisteraction();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {

            }
        });
        if(minutesCanBeExpressedAsHours(t))
        {
            t=minutesAsHours(t) ;
            timeunits.setSelection(1);
        }


        Spinner beforeafter = (Spinner) findViewById(R.id.beforeafter);
        List<String> list = new ArrayList<String>();
        String before = getResources().getString(R.string.before);
        String after = getResources().getString(R.string.after);
        list.add(before);
        list.add(after);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        beforeafter.setAdapter(dataAdapter);
        beforeafter.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                reregisteraction();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {

            }
        });
        if(getTimingValue().startsWith("+"))
        {
            beforeafter.setSelection(1);
        }

        timing=""+t;
        timingText.setText(timing);
    }

    public boolean minutesCanBeExpressedAsHours(int timevalue)
    {
        return timevalue>60 && timevalue%60==0;
    }

    public int minutesAsHours(int m)
    {
        return m/60;
    }

    private void reregisteraction()
    {
        Logger.d(TAG, ">>>>> reregisteraction");
        Spinner beforeafter = (Spinner) findViewById(R.id.beforeafter);
        Spinner timeunits = (Spinner) findViewById(R.id.timeunits);
        TextView timingText = (TextView) findViewById(R.id.timingtext);
        try
        {
            Logger.d(TAG, ">>>>> TRY to get Timing");
            int timing = Integer.parseInt(timingText.getEditableText().toString());
            Logger.d(TAG, "timing="+timing);
            if(timeunits.getSelectedItemPosition() == 1)
            {
                timing=timing*60;
            }
            Logger.d(TAG, "reclaculated timing="+timing);
            String s = ""+timing;
            if(timing!=0)
            {
                if (beforeafter.getSelectedItemPosition() == 0)
                {
                    s = "-" + s;
                }
                else
                {
                    s = "+" + s;
                }
            }
            Logger.d(TAG, "s = "+s);
            alarm.registerAction(s, action);
        }
        catch (Exception e)
        {

        }
    }

    private void setAlarmHeader()
    {

        String alarmTime = UISupport.getTimeAsString(alarm.getAlarmTime());

        TextView alarmInformation = (TextView) findViewById(R.id.alarminformation);
        alarmInformation.setText(alarmTime);

        ImageView alarmicon = (ImageView) findViewById(R.id.alarmicon);
        if(alarm.isActive())
        {
            if(alarm.isSnoozing())
            {
                alarmicon.setImageResource(R.drawable.alarmicon_a_s);
                if(alarm.isCurrentlyLocked())
                {
                    alarmicon.setImageResource(R.drawable.alarmicon_a_s_l);
                }
            }
            else
            {
                alarmicon.setImageResource(R.drawable.alarmicon_a);
                if(alarm.isCurrentlyLocked())
                {
                    alarmicon.setImageResource(R.drawable.alarmicon_a_l);
                }
            }
        }
        else
        {
            alarmicon.setImageResource(R.drawable.alarmicon_b);
        }

    }

    private String getTimingValue()
    {
        return calculateTimingValue();
    }

    private String calculateTimingValue()
    {
        HashMap<String, ArrayList<AmbientAction>> registeredActions = alarm.getRegisteredActions();
        Set<String> keys = registeredActions.keySet();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext())
        {
            String actiontime = iterator.next();
            ArrayList<AmbientAction> actions = registeredActions.get(actiontime);
            for (AmbientAction action : actions)
            {
                if(action.getActionID().equals(this.action.getActionID()))
                {
                    return actiontime;
                }
            }
        }
        return "0";
    }

    public AmbientAlarm getAlarm()
    {
        return alarm;
    }
}




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