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