aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/lockclock/WidgetApplication.java
blob: c805f5cd6420cb966b768f2094423fe85f140260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2013 The CyanogenMod Project (DvTonder)
 *
 * 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.cyanogenmod.lockclock;

import com.cyanogenmod.lockclock.misc.Constants;
import com.cyanogenmod.lockclock.ClockWidgetProvider;
import com.cyanogenmod.lockclock.ClockWidgetService;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class WidgetApplication extends Application {
    private static final String TAG = "WidgetApplication";
    private static boolean D = Constants.DEBUG;
    private static final long INTERVAL_ONE_MINUTE = 60000L;

    private BroadcastReceiver mTickReceiver = null;

    /**
     * BroadReceiver and supporting functions used for handling clock ticks
     * for the TextView clock support (API 16) by scheduling a repeating
     * alarm event every 60 seconds and triggering a refresh of the widget
     */
    public class TickReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_TIME_TICK.equals(action)){
                if (D) Log.d(TAG, "Clock tick event received");

                // Schedule the clock refresh alarm event
                scheduleClockRefresh(context);

                // Refresh the widget
                Intent refreshIntent = new Intent(context, ClockWidgetProvider.class);
                refreshIntent.setAction(ClockWidgetService.ACTION_REFRESH);
                context.sendBroadcast(refreshIntent);

                // We no longer need the tick receiver, its done its job, stop it
                stopTickReceiver();
            }
        }
    }

    public void startTickReceiver() {
        // Clean up first, just in case
        stopTickReceiver();

        // Start the new receiver
        if (D) Log.d(TAG, "Registering clock tick receiver");
        mTickReceiver = new TickReceiver();
        registerReceiver(mTickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
    }

    public void stopTickReceiver() {
        if (mTickReceiver != null) {
            if (D) Log.d(TAG, "Cleaning up: Unregistering clock tick receiver");
            unregisterReceiver(mTickReceiver);
            mTickReceiver = null;
        }
    }

    private static void scheduleClockRefresh(Context context) {
        if (D) Log.d(TAG, "Starting clock refresh alarm");
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long due = System.currentTimeMillis() + INTERVAL_ONE_MINUTE;
        am.setRepeating(AlarmManager.RTC, due, INTERVAL_ONE_MINUTE, getClockRefreshIntent(context));
    }

    public static void cancelClockRefresh(Context context) {
        if (D) Log.d(TAG, "Cleaning up: Stopping clock refresh alarm");
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(getClockRefreshIntent(context));
    }

    private static PendingIntent getClockRefreshIntent(Context context) {
        Intent i = new Intent(context, ClockWidgetService.class);
        i.setAction(ClockWidgetService.ACTION_REFRESH);
        return PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    }
}