1 /*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 package com.example.android.directboot;
18 
19 import com.example.android.directboot.alarms.Alarm;
20 import com.example.android.directboot.alarms.AlarmAdapter;
21 import com.example.android.directboot.alarms.AlarmIntentService;
22 import com.example.android.directboot.alarms.AlarmStorage;
23 import com.example.android.directboot.alarms.AlarmUtil;
24 
25 import android.app.Activity;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.os.Bundle;
31 import android.support.annotation.Nullable;
32 import android.support.design.widget.FloatingActionButton;
33 import android.support.v4.app.Fragment;
34 import android.support.v4.content.LocalBroadcastManager;
35 import android.support.v7.widget.LinearLayoutManager;
36 import android.support.v7.widget.RecyclerView;
37 import android.view.LayoutInflater;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.widget.TextView;
41 
42 /**
43  * Fragment that registers scheduled alarms.
44  */
45 public class SchedulerFragment extends Fragment {
46 
47     private static final String FRAGMENT_TIME_PICKER_TAG = "fragment_time_picker";
48 
49     private AlarmAdapter mAlarmAdapter;
50     private AlarmUtil mAlarmUtil;
51     private TextView mTextViewIntroMessage;
52     private BroadcastReceiver mAlarmWentOffBroadcastReceiver;
53 
newInstance()54     public static SchedulerFragment newInstance() {
55         SchedulerFragment fragment = new SchedulerFragment();
56         return fragment;
57     }
58 
SchedulerFragment()59     public SchedulerFragment() {
60         // Required empty public constructor
61     }
62 
63     @Override
onActivityCreated(@ullable Bundle savedInstanceState)64     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
65         super.onActivityCreated(savedInstanceState);
66         mAlarmWentOffBroadcastReceiver = new AlarmWentOffReceiver();
67         LocalBroadcastManager.getInstance(getActivity())
68                 .registerReceiver(mAlarmWentOffBroadcastReceiver,
69                         new IntentFilter(AlarmIntentService.ALARM_WENT_OFF_ACTION));
70     }
71 
72     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)73     public View onCreateView(LayoutInflater inflater, ViewGroup container,
74             Bundle savedInstanceState) {
75         return inflater.inflate(R.layout.fragment_alarm_scheduler, container, false);
76     }
77 
78     @Override
onViewCreated(final View rootView, Bundle savedInstanceState)79     public void onViewCreated(final View rootView, Bundle savedInstanceState) {
80         super.onViewCreated(rootView, savedInstanceState);
81         FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab_add_alarm);
82         fab.setOnClickListener(new View.OnClickListener() {
83             @Override
84             public void onClick(View view) {
85                 TimePickerFragment fragment = TimePickerFragment.newInstance();
86                 fragment.setAlarmAddListener(new AlarmAddListenerImpl());
87                 fragment.show(getFragmentManager(), FRAGMENT_TIME_PICKER_TAG);
88             }
89         });
90         mTextViewIntroMessage = (TextView) rootView.findViewById(R.id.text_intro_message);
91         Activity activity = getActivity();
92         AlarmStorage alarmStorage = new AlarmStorage(activity);
93         mAlarmAdapter = new AlarmAdapter(activity, alarmStorage.getAlarms());
94         if (mAlarmAdapter.getItemCount() == 0) {
95             mTextViewIntroMessage.setVisibility(View.VISIBLE);
96         }
97         RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_alarms);
98         recyclerView.setLayoutManager(new LinearLayoutManager(activity));
99         recyclerView.setAdapter(mAlarmAdapter);
100         recyclerView.addItemDecoration(new AlarmAdapter.DividerItemDecoration(activity));
101         mAlarmUtil = new AlarmUtil(activity);
102     }
103 
104     @Override
onDestroy()105     public void onDestroy() {
106         LocalBroadcastManager.getInstance(getActivity())
107                 .unregisterReceiver(mAlarmWentOffBroadcastReceiver);
108         super.onDestroy();
109     }
110 
111     /**
112      * {@link TimePickerFragment.AlarmAddListener} to do actions after an alarm is added.
113      */
114     private class AlarmAddListenerImpl implements TimePickerFragment.AlarmAddListener {
115 
116         @Override
onAlarmAdded(Alarm alarm)117         public void onAlarmAdded(Alarm alarm) {
118             mAlarmAdapter.addAlarm(alarm);
119             mAlarmUtil.scheduleAlarm(alarm);
120             mTextViewIntroMessage.setVisibility(View.GONE);
121         }
122     }
123 
124     /**
125      * A {@link BroadcastReceiver} that receives an intent when an alarm goes off.
126      * This receiver removes the corresponding alarm from the RecyclerView.
127      */
128     private class AlarmWentOffReceiver extends BroadcastReceiver {
129 
130         @Override
onReceive(Context context, Intent intent)131         public void onReceive(Context context, Intent intent) {
132             Alarm alarm = AlarmUtil.readAlarm(intent.getExtras());
133             mAlarmAdapter.deleteAlarm(alarm);
134         }
135     }
136 }
137