TimeZoneMonitor.java revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.content.browser;
6
7import android.content.BroadcastReceiver;
8import android.content.Context;
9import android.content.Intent;
10import android.content.IntentFilter;
11import android.util.Log;
12
13import org.chromium.base.CalledByNative;
14import org.chromium.base.JNINamespace;
15
16/**
17 * Android implementation details for content::TimeZoneMonitorAndroid.
18 */
19@JNINamespace("content")
20class TimeZoneMonitor {
21    private static final String TAG = "TimeZoneMonitor";
22
23    private final Context mAppContext;
24    private final IntentFilter mFilter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
25    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
26        @Override
27        public void onReceive(Context context, Intent intent) {
28           if (!intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
29               Log.e(TAG, "unexpected intent");
30               return;
31           }
32
33           nativeTimeZoneChangedFromJava(mNativePtr);
34        }
35    };
36
37    private long mNativePtr;
38
39    /**
40     * Start listening for intents.
41     * @param nativePtr The native content::TimeZoneMonitorAndroid to notify of time zone changes.
42     */
43    private TimeZoneMonitor(Context context, long nativePtr) {
44        mAppContext = context.getApplicationContext();
45        mNativePtr = nativePtr;
46        mAppContext.registerReceiver(mBroadcastReceiver, mFilter);
47    }
48
49    @CalledByNative
50    static TimeZoneMonitor getInstance(Context context, long nativePtr) {
51        return new TimeZoneMonitor(context, nativePtr);
52    }
53
54    /**
55     * Stop listening for intents.
56     */
57    @CalledByNative
58    void stop() {
59        mAppContext.unregisterReceiver(mBroadcastReceiver);
60        mNativePtr = 0;
61    }
62
63    /**
64     * Native JNI call to content::TimeZoneMonitorAndroid::TimeZoneChanged.
65     * See content/browser/time_zone_monitor_android.cc.
66     */
67    private native void nativeTimeZoneChangedFromJava(long nativeTimeZoneMonitorAndroid);
68}
69