1/*
2 * Copyright (C) 2014 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
17package com.android.deskclock.timer;
18
19import android.app.Activity;
20import android.app.Fragment;
21import android.app.FragmentTransaction;
22import android.content.Context;
23import android.os.Bundle;
24import android.text.TextUtils;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.View.OnClickListener;
28import android.view.ViewGroup;
29import android.widget.FrameLayout;
30import android.widget.TextView;
31
32import com.android.deskclock.CircleButtonsLayout;
33import com.android.deskclock.DeskClock;
34import com.android.deskclock.DeskClock.OnTapListener;
35import com.android.deskclock.LabelDialogFragment;
36import com.android.deskclock.R;
37
38public class TimerItemFragment extends Fragment {
39    private static final String TAG = "TimerItemFragment_tag";
40    private TimerObj mTimerObj;
41
42    public TimerItemFragment() {
43    }
44
45    public static TimerItemFragment newInstance(TimerObj timerObj) {
46        final TimerItemFragment fragment = new TimerItemFragment();
47        final Bundle args = new Bundle();
48        args.putParcelable(TAG, timerObj);
49        fragment.setArguments(args);
50        return fragment;
51    }
52
53    @Override
54    public void onCreate(Bundle savedInstanceState) {
55        super.onCreate(savedInstanceState);
56        final Bundle bundle = getArguments();
57        if (bundle != null) {
58            mTimerObj = (TimerObj) bundle.getParcelable(TAG);
59        }
60    }
61
62    @Override
63    public View onCreateView(LayoutInflater inflater, ViewGroup container,
64            Bundle savedInstanceState) {
65        final TimerListItem v = (TimerListItem) inflater.inflate(R.layout.timer_list_item,
66                null);
67        mTimerObj.mView = v;
68        final long timeLeft = mTimerObj.updateTimeLeft(false);
69        final boolean drawWithColor = mTimerObj.mState != TimerObj.STATE_RESTART;
70        v.set(mTimerObj.mOriginalLength, timeLeft, drawWithColor);
71        v.setTime(timeLeft, true);
72        v.setResetAddButton(mTimerObj.mState == TimerObj.STATE_RUNNING ||
73                mTimerObj.mState == TimerObj.STATE_TIMESUP, new OnClickListener() {
74            @Override
75            public void onClick(View view) {
76                final Fragment parent = getParentFragment();
77                if (parent instanceof TimerFragment) {
78                    ((TimerFragment) parent).onPlusOneButtonPressed(mTimerObj);
79                }
80            }
81        });
82        switch (mTimerObj.mState) {
83            case TimerObj.STATE_RUNNING:
84                v.start();
85                break;
86            case TimerObj.STATE_TIMESUP:
87                v.timesUp();
88                break;
89            case TimerObj.STATE_DONE:
90                v.done();
91                break;
92            default:
93                break;
94        }
95
96        final CircleButtonsLayout circleLayout =
97                (CircleButtonsLayout) v.findViewById(R.id.timer_circle);
98        circleLayout.setCircleTimerViewIds(R.id.timer_time, R.id.reset_add, R.id.timer_label,
99                R.id.timer_label_text);
100
101        return v;
102    }
103
104    @Override
105    public void onActivityCreated(Bundle savedInstanceState) {
106        super.onActivityCreated(savedInstanceState);
107        final View v = mTimerObj.mView;
108        if (v == null) {
109            return;
110        }
111        final FrameLayout labelLayout = (FrameLayout) v.findViewById(R.id.timer_label);
112        final TextView labelPlaceholder = (TextView) v.findViewById(R.id.timer_label_placeholder);
113        final TextView labelText = (TextView) v.findViewById(R.id.timer_label_text);
114        if (TextUtils.isEmpty(mTimerObj.mLabel)) {
115            labelText.setVisibility(View.GONE);
116            labelPlaceholder.setVisibility(View.VISIBLE);
117        } else {
118            labelText.setText(mTimerObj.mLabel);
119            labelText.setVisibility(View.VISIBLE);
120            labelPlaceholder.setVisibility(View.GONE);
121        }
122        final Activity activity = getActivity();
123        if (activity instanceof DeskClock) {
124            labelLayout.setOnClickListener(new OnClickListener() {
125                @Override
126                public void onClick(View view) {
127                    onLabelPressed(mTimerObj);
128                }
129            });
130        } else {
131            labelPlaceholder.setVisibility(View.INVISIBLE);
132        }
133    }
134
135    private void onLabelPressed(TimerObj t) {
136        final String dialogTag = "label_dialog";
137        final FragmentTransaction ft = getFragmentManager().beginTransaction();
138        final Fragment prev = getFragmentManager().findFragmentByTag(dialogTag);
139        if (prev != null) {
140            ft.remove(prev);
141        }
142        ft.addToBackStack(null);
143        final LabelDialogFragment newFragment =
144                LabelDialogFragment.newInstance(t, t.mLabel, getParentFragment().getTag());
145        newFragment.show(ft, dialogTag);
146    }
147}
148