1/*
2 * Copyright (C) 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
17package com.android.settings.datetime;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23
24import com.android.settingslib.core.lifecycle.LifecycleObserver;
25import com.android.settingslib.core.lifecycle.events.OnPause;
26import com.android.settingslib.core.lifecycle.events.OnResume;
27
28public class TimeChangeListenerMixin extends BroadcastReceiver
29        implements LifecycleObserver, OnPause, OnResume {
30
31    private final Context mContext;
32    private final UpdateTimeAndDateCallback mCallback;
33
34    public TimeChangeListenerMixin(Context context, UpdateTimeAndDateCallback callback) {
35        mContext = context;
36        mCallback = callback;
37    }
38
39    @Override
40    public void onResume() {
41        // Register for time ticks and other reasons for time change
42        final IntentFilter filter = new IntentFilter();
43        filter.addAction(Intent.ACTION_TIME_TICK);
44        filter.addAction(Intent.ACTION_TIME_CHANGED);
45        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
46
47        mContext.registerReceiver(this, filter, null, null);
48    }
49
50    @Override
51    public void onPause() {
52        mContext.unregisterReceiver(this);
53    }
54
55    @Override
56    public void onReceive(Context context, Intent intent) {
57        if (mCallback != null) {
58            mCallback.updateTimeAndDateDisplay(mContext);
59        }
60    }
61}
62