com.licenta.android.licenseapp.alarm.AlarmFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.licenta.android.licenseapp.alarm.AlarmFragment.java

Source

package com.licenta.android.licenseapp.alarm;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.licenta.android.licenseapp.FacebookUtils;
import com.licenta.android.licenseapp.R;
import com.licenta.android.licenseapp.activity.MainTabActivity;
import com.licenta.android.licenseapp.fragment.TabFragment;
import com.licenta.android.licenseapp.fragment.UpdatableFragment;
import com.licenta.android.licenseapp.util.Constants;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by oana.ilovan on 28.12.2015.
 */
public class AlarmFragment extends Fragment implements TabFragment, UpdatableFragment {

    private Context mContext;
    private FloatingActionButton mAlarmFab;
    private TextView mTimerText;
    private CountDownTimer mCountDownTimer;
    private SharedPreferences mPrefs;

    //    public static AlarmFragment newInstance(boolean isAlarmOn) {
    //        AlarmFragment fragment = new AlarmFragment();
    //        Bundle args = new Bundle();
    //        args.putBoolean(Constants.PREF_KEY_IS_ALARM_ON, isAlarmOn);
    //        fragment.setArguments(args);
    //
    //        return fragment;
    //    }
    //
    //    @Override
    //    public void onAttach(Context context) {
    //        super.onAttach(context);
    //
    //        mContext = context;
    //    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_alarm, container, false);

        mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

        mAlarmFab = (FloatingActionButton) rootView.findViewById(R.id.alarm_fab);
        mAlarmFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleAlarm();

            }
        });
        setUpAlarmFab();

        FloatingActionButton safeFab = (FloatingActionButton) rootView.findViewById(R.id.safe_fab);
        safeFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCountDownTimer.onFinish();
                FacebookUtils.printKeyHash(getActivity());
            }
        });

        mTimerText = (TextView) rootView.findViewById(R.id.timer);
        final long futureTime = Long.parseLong(mPrefs.getString("repeat_interval", "0"));
        mCountDownTimer = new CountDownTimer(futureTime * DateUtils.MINUTE_IN_MILLIS, DateUtils.SECOND_IN_MILLIS) {

            public void onTick(long millisUntilFinished) {
                mTimerText.setText(new SimpleDateFormat("mm:ss").format(new Date(millisUntilFinished)));
            }

            public void onFinish() {
                mTimerText.setText(R.string.timer_default);
            }
        };

        return rootView;
    }

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

    }

    @Override
    public String getTitle() {
        return "ALARM";
    }

    @Override
    public void update() {
        setUpAlarmFab();
        mCountDownTimer.cancel();
        mCountDownTimer.onFinish();
    }

    private void setUpAlarmFab() {
        //        SpannableStringBuilder builder = new SpannableStringBuilder();
        //        builder.append(" ");
        //        builder.setSpan(new ImageSpan(getContext(), R.drawable.ic_action_action_alarm_add),
        //                builder.length() - 1, builder.length(), 0);
        //        builder.append(getString(R.string.alarm_started));

        boolean isAlarmOn = mPrefs.getBoolean(Constants.PREF_KEY_IS_ALARM_ON, false);
        if (isAlarmOn) {
            mAlarmFab.setImageResource(R.drawable.ic_stat_action_alarm_off);
        } else {
            mAlarmFab.setImageResource(R.drawable.ic_action_action_alarm_add);
        }
    }

    private void handleAlarm() {

        boolean isAlarmOn = mPrefs.getBoolean(Constants.PREF_KEY_IS_ALARM_ON, false);

        if (isAlarmOn) {
            ((MainTabActivity) getActivity()).showPinDialog(true);
        } else {
            AlarmReceiver.setAlarm(getActivity());
            mAlarmFab.setImageResource(R.drawable.ic_stat_action_alarm_off);
            mCountDownTimer.start();
            Toast.makeText(getActivity(), R.string.alarm_started, Toast.LENGTH_LONG).show();
        }
    }

}