townley.stuart.app.android.trekkinly.Notification_picker_class.java Source code

Java tutorial

Introduction

Here is the source code for townley.stuart.app.android.trekkinly.Notification_picker_class.java

Source

package townley.stuart.app.android.trekkinly;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

import itemclass_all.NotificationItemClass;

/**
* Copyright 2015 Stuart Lachlan Townley
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
    http://www.apache.org/licenses/LICENSE-2.0
    
    *Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */

public class Notification_picker_class extends Fragment {

    static Calendar cal = Calendar.getInstance();
    public static long timeDateNotification;

    public static Notification_picker_class newInstance() {
        Notification_picker_class notification_picker_class = new Notification_picker_class();
        return notification_picker_class;
    }

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

        final Button button = (Button) rootView.findViewById(R.id.Button);
        final Button button2 = (Button) rootView.findViewById(R.id.Button2);
        final Button button3 = (Button) rootView.findViewById(R.id.Button3);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePickerDialog(v);
                AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
                animation1.setDuration(500);
                button.startAnimation(animation1);

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePickerDialog(v);
                AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
                animation1.setDuration(500);
                button2.startAnimation(animation1);
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setNotification();
                AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
                animation1.setDuration(500);
                button3.startAnimation(animation1);
            }
        });

        button3.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ComponentName receiver = new ComponentName(getActivity(), AlertClass.class);
                PackageManager pm = getActivity().getPackageManager();
                pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP);

                Intent intent = new Intent(getActivity(), AlertClass.class);
                AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                alarmManager.cancel(PendingIntent.getBroadcast(getActivity().getApplicationContext(), 1, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT));
                Toast.makeText(getActivity(), "Notification reset!", Toast.LENGTH_SHORT).show();

                AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
                animation1.setDuration(500);
                button3.startAnimation(animation1);

                //It would not let me assign void so I
                return true;
            }
        });

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        ((MainActivityTrekkly) activity).onSectionAttached(9);
        MainActivityTrekkly mA = ((MainActivityTrekkly) getActivity());
        mA.restoreActionBar(Color.parseColor("#1A8B55"));
    }

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getActivity().getFragmentManager(), "timePicker");
    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getActivity().getFragmentManager(), "datePicker");
    }

    public void setNotification() {

        NotificationItemClass notificationItemClass = new NotificationItemClass();
        DataBaseHelper db = new DataBaseHelper(getActivity());

        db.getWritableDatabase();
        db.addNotification(notificationItemClass);
        db.upDateDataBaseNotification(notificationItemClass);
        db.getNotification(notificationItemClass);

        if (db.notification <= 0) {
            Toast.makeText(getActivity(), "Please set a time for the notification", Toast.LENGTH_SHORT).show();
        }

        else {

            ComponentName receiver = new ComponentName(getActivity(), AlertClass.class);
            PackageManager pm = getActivity().getPackageManager();
            pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);

            Intent intent = new Intent(getActivity(), AlertClass.class);
            AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, db.notification, PendingIntent.getBroadcast(
                    getActivity().getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            Toast.makeText(getActivity(), "Notification issued", Toast.LENGTH_SHORT).show();
            Log.d("Trekkinly", "AlarmNotification Called");

        }
    }

    public Notification_picker_class() {

    }

    public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            int minute = cal.get(Calendar.MINUTE);

            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            cal.set(Calendar.MINUTE, view.getCurrentMinute());
            cal.set(Calendar.HOUR_OF_DAY, view.getCurrentHour());
            timeDateNotification = cal.getTimeInMillis();

        }
    }

    public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);

            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            cal.set(Calendar.YEAR, view.getYear());
            cal.set(Calendar.MONTH, view.getMonth());
            cal.set(Calendar.DAY_OF_MONTH, view.getDayOfMonth());
            timeDateNotification = cal.getTimeInMillis();

            Log.d("DatePickerFragment", " " + timeDateNotification);
        }
    }
}