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 5 package org.chromium.content.browser; 6 7 import android.content.BroadcastReceiver; 8 import android.content.Context; 9 import android.content.Intent; 10 import android.content.IntentFilter; 11 import android.util.Log; 12 13 import org.chromium.base.CalledByNative; 14 import org.chromium.base.JNINamespace; 15 16 /** 17 * Android implementation details for content::TimeZoneMonitorAndroid. 18 */ 19 @JNINamespace("content") 20 class 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 */ TimeZoneMonitor(Context context, long nativePtr)43 private TimeZoneMonitor(Context context, long nativePtr) { 44 mAppContext = context.getApplicationContext(); 45 mNativePtr = nativePtr; 46 mAppContext.registerReceiver(mBroadcastReceiver, mFilter); 47 } 48 49 @CalledByNative getInstance(Context context, long nativePtr)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 stop()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 */ nativeTimeZoneChangedFromJava(long nativeTimeZoneMonitorAndroid)67 private native void nativeTimeZoneChangedFromJava(long nativeTimeZoneMonitorAndroid); 68 } 69