1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package com.android.deskclock.timer;
16
17import android.content.Intent;
18import android.content.res.Configuration;
19import android.os.Bundle;
20import android.util.Log;
21import android.view.KeyEvent;
22import android.view.View;
23import android.view.ViewGroup;
24import android.view.Window;
25import android.view.WindowManager;
26
27import com.android.deskclock.BaseActivity;
28import com.android.deskclock.R;
29import com.android.deskclock.Utils;
30import com.android.deskclock.timer.TimerFullScreenFragment.OnEmptyListListener;
31
32/**
33 * Timer alarm alert: pops visible indicator. This activity is the version which
34 * shows over the lock screen.
35 * This activity re-uses TimerFullScreenFragment GUI
36 */
37public class TimerAlertFullScreen extends BaseActivity implements OnEmptyListListener {
38
39    private static final String TAG = "TimerAlertFullScreen";
40    private static final String FRAGMENT = "timer";
41
42    @Override
43    protected void onCreate(Bundle savedInstanceState) {
44        super.onCreate(savedInstanceState);
45
46        setContentView(R.layout.timer_alert_full_screen);
47        final View view = findViewById(R.id.fragment_container);
48        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
49
50        final Window win = getWindow();
51        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
52                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
53        // Turn on the screen unless we are being launched from the AlarmAlert
54        // subclass as a result of the screen turning off.
55        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
56                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
57                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
58
59        // Don't create overlapping fragments.
60        if (getFragment() == null) {
61            TimerFullScreenFragment timerFragment = new TimerFullScreenFragment();
62
63            // Create fragment and give it an argument to only show
64            // timers in STATE_TIMESUP state
65            Bundle args = new Bundle();
66            args.putBoolean(Timers.TIMESUP_MODE, true);
67
68            timerFragment.setArguments(args);
69
70            // Add the fragment to the 'fragment_container' FrameLayout
71            getFragmentManager().beginTransaction()
72                    .add(R.id.fragment_container, timerFragment, FRAGMENT).commit();
73        }
74    }
75
76    @Override
77    protected void onResume() {
78        super.onResume();
79
80        // Only show notifications for times-up when this activity closed.
81        Utils.cancelTimesUpNotifications(this);
82    }
83
84    @Override
85    public void onPause() {
86        Utils.showTimesUpNotifications(this);
87
88        super.onPause();
89    }
90
91    @Override
92    public boolean dispatchKeyEvent(KeyEvent event) {
93        // Handle key down and key up on a few of the system keys.
94        boolean up = event.getAction() == KeyEvent.ACTION_UP;
95        switch (event.getKeyCode()) {
96        // Volume keys and camera keys stop all the timers
97        case KeyEvent.KEYCODE_VOLUME_UP:
98        case KeyEvent.KEYCODE_VOLUME_DOWN:
99        case KeyEvent.KEYCODE_VOLUME_MUTE:
100        case KeyEvent.KEYCODE_CAMERA:
101        case KeyEvent.KEYCODE_FOCUS:
102            if (up) {
103                stopAllTimesUpTimers();
104            }
105            return true;
106
107        default:
108            break;
109        }
110        return super.dispatchKeyEvent(event);
111    }
112
113    /**
114     * this is called when a second timer is triggered while a previous alert
115     * window is still active.
116     */
117    @Override
118    protected void onNewIntent(Intent intent) {
119        TimerFullScreenFragment timerFragment = getFragment();
120        if (timerFragment != null) {
121            timerFragment.restartAdapter();
122        }
123        super.onNewIntent(intent);
124    }
125
126    @Override
127    public void onConfigurationChanged(Configuration newConfig) {
128        ViewGroup viewContainer = (ViewGroup)findViewById(R.id.fragment_container);
129        viewContainer.requestLayout();
130        super.onConfigurationChanged(newConfig);
131    }
132
133    protected void stopAllTimesUpTimers() {
134        TimerFullScreenFragment timerFragment = getFragment();
135        if (timerFragment != null) {
136            timerFragment.updateAllTimesUpTimers();
137        }
138    }
139
140    @Override
141    public void onEmptyList() {
142        if (Timers.LOGGING) {
143            Log.v(TAG, "onEmptyList");
144        }
145        onListChanged();
146        finish();
147    }
148
149    @Override
150    public void onListChanged() {
151        Utils.showInUseNotifications(this);
152    }
153
154    private TimerFullScreenFragment getFragment() {
155        return (TimerFullScreenFragment) getFragmentManager().findFragmentByTag(FRAGMENT);
156    }
157}
158