Java tutorial
/* * Copyright (C) 2015 brayanarias.com ing.brayanarias@gmail.com * * 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. */ package com.brayanarias.alarmproject.fragment; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.android.datetimepicker.time.RadialPickerLayout; import com.android.datetimepicker.time.TimePickerDialog; import com.brayanarias.alarmproject.R; import com.brayanarias.alarmproject.activity.AddAlarmActivity; import com.brayanarias.alarmproject.activity.MainActivity; import com.brayanarias.alarmproject.adapter.AlarmListAdapter; import com.brayanarias.alarmproject.database.AlarmDataBase; import com.brayanarias.alarmproject.database.DataBaseManager; import com.brayanarias.alarmproject.decorator.DividerItemDecoration; import com.brayanarias.alarmproject.model.Alarm; import com.brayanarias.alarmproject.util.AlarmUtilities; import com.brayanarias.alarmproject.util.Constant; import java.io.File; import java.util.ArrayList; import java.util.Calendar; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class AlarmListFragment extends Fragment implements View.OnClickListener, TimePickerDialog.OnTimeSetListener { public AlarmListFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View layout = inflater.inflate(R.layout.fragment_alarm_list, container, false); RecyclerView recyclerView = (RecyclerView) layout.findViewById(R.id.alarmList); AlarmListAdapter alarmListAdapter = new AlarmListAdapter(getData(), (MainActivity) getActivity()); recyclerView.setAdapter(alarmListAdapter); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); linearLayoutManager.scrollToPosition(0); recyclerView.setLayoutManager(linearLayoutManager); if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) { GridLayoutManager gridLayoutManager; if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { gridLayoutManager = new GridLayoutManager(getActivity().getBaseContext(), 3); recyclerView.setLayoutManager(gridLayoutManager); } else { gridLayoutManager = new GridLayoutManager(getActivity().getBaseContext(), 2); recyclerView.setLayoutManager(gridLayoutManager); } recyclerView.setLayoutManager(gridLayoutManager); } FloatingActionButton fab = (FloatingActionButton) layout.findViewById(R.id.fab); fab.setOnClickListener(this); return layout; } public List<Alarm> getData() { DataBaseManager dataBaseManager = DataBaseManager.getInstance(getActivity()); List<Alarm> alarmList = AlarmDataBase.getAlarmList(dataBaseManager); return alarmList; } @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(this, hour, minute, false); timePickerDialog.show(getFragmentManager(), "tag"); } @Override public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) { Alarm alarm = AlarmUtilities.createDefaultAlarm(); String min = minute < 10 ? "0" + minute : "" + minute; if (hourOfDay == 12) { alarm.setAmPm("PM"); alarm.setHour(hourOfDay); alarm.setHourFormatted((hourOfDay) + ":" + min); } else if (hourOfDay > 12) { alarm.setAmPm("PM"); alarm.setHour(hourOfDay - 12); alarm.setHourFormatted((hourOfDay - 12) + ":" + min); } else { if (hourOfDay == 0) { hourOfDay = 12; } alarm.setAmPm("AM"); alarm.setHour(hourOfDay); alarm.setHourFormatted(hourOfDay + ":" + min); } alarm.setMinute(minute); Intent intent = new Intent(getActivity(), AddAlarmActivity.class); intent.putExtra(Constant.actionAlarmKey, Constant.addAlarm); intent.putExtra(Constant.alarmSerializableKey, alarm); startActivity(intent); } }