frameworks/base
Revision | cf97b6b32a6edafd6c88f4b1ef8c443118eb5e5e (tree) |
---|---|
Zeit | 2016-10-09 19:47:07 |
Autor | Luis Vidal <lvidal@cyng...> |
Commiter | Steve Kondik |
Add Weather Content Provider [5/5]
Use the Weather Content Provider in the cmsdk to pull the
weather data.
However, SystemUI will rely on LockClock to force weather
updates via the new Weather Service, which in turn will
send a broadcast when new weather data is available.
Change-Id: I3c65ea5f4cc297a7944fcdef33f496cdf2d68d0a
SystemUI: Expose weather text colors
Weather text colors in the expanded status bar are hardcoded to
white. Expose these via cm_colors so that themes can color them
however they'd like.
A martini. Shaken, not stirred.
Change-Id: I5e5a738c8b97f556562114a66d102fcb924a0493
SystemUI: Update cached weather data when temperature unit changes
Register an observer to keep track of the temperature unit
selected in settings to update the cached data accordingly
TICKET: CYNGNOS-2694
Change-Id: I92adec20835bcd6d6476d793564300611bcba4a1
@@ -165,9 +165,6 @@ | ||
165 | 165 | <!-- the ability to rename notifications posted by other apps --> |
166 | 166 | <uses-permission android:name="android.permission.SUBSTITUTE_NOTIFICATION_APP_NAME" /> |
167 | 167 | |
168 | - <!-- Weather --> | |
169 | - <uses-permission android:name="com.cyanogenmod.lockclock.permission.READ_WEATHER" /> | |
170 | - | |
171 | 168 | <!-- manage cmsettings --> |
172 | 169 | <uses-permission android:name="cyanogenmod.permission.WRITE_SETTINGS" /> |
173 | 170 | <uses-permission android:name="cyanogenmod.permission.WRITE_SECURE_SETTINGS" /> |
@@ -26,6 +26,9 @@ | ||
26 | 26 | <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> |
27 | 27 | <uses-permission android:name="android.permission.RECORD_AUDIO" /> |
28 | 28 | |
29 | + <!-- Weather Provider --> | |
30 | + <uses-permission android:name="cyanogenmod.permission.READ_WEATHER" /> | |
31 | + | |
29 | 32 | <application> |
30 | 33 | |
31 | 34 | <activity-alias |
@@ -183,7 +183,7 @@ | ||
183 | 183 | android:layout_height="wrap_content" |
184 | 184 | android:paddingEnd="16dp" |
185 | 185 | android:gravity="right" |
186 | - android:textColor="#ffffff" | |
186 | + android:textColor="@color/status_bar_temperature_text_color" | |
187 | 187 | android:textSize="@dimen/weather_text_size" |
188 | 188 | android:importantForAccessibility="noHideDescendants"/> |
189 | 189 | <TextView |
@@ -192,7 +192,7 @@ | ||
192 | 192 | android:layout_height="wrap_content" |
193 | 193 | android:paddingEnd="16dp" |
194 | 194 | android:gravity="right" |
195 | - android:textColor="#ffffff" | |
195 | + android:textColor="@color/status_bar_temperature_location_text_color" | |
196 | 196 | android:textSize="@dimen/weather_text_size" |
197 | 197 | android:importantForAccessibility="noHideDescendants"/> |
198 | 198 | </LinearLayout> |
@@ -0,0 +1,23 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<!-- | |
3 | + Copyright (C) 2016 The CyanogenMod Project | |
4 | + | |
5 | + Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | + you may not use this file except in compliance with the License. | |
7 | + You may obtain a copy of the License at | |
8 | + | |
9 | + http://www.apache.org/licenses/LICENSE-2.0 | |
10 | + | |
11 | + Unless required by applicable law or agreed to in writing, software | |
12 | + distributed under the License is distributed on an "AS IS" BASIS, | |
13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | + See the License for the specific language governing permissions and | |
15 | + limitations under the License. | |
16 | +--> | |
17 | +<resources> | |
18 | + | |
19 | + <!-- Expanded Status Bar Weather Text Colors --> | |
20 | + <color name="status_bar_temperature_text_color">#FFFFFFFF</color> | |
21 | + <color name="status_bar_temperature_location_text_color">#FFFFFFFF</color> | |
22 | + | |
23 | +</resources> |
@@ -16,34 +16,22 @@ | ||
16 | 16 | |
17 | 17 | package com.android.systemui.statusbar.phone; |
18 | 18 | |
19 | -import android.app.ActivityManager; | |
20 | -import android.app.ActivityManagerNative; | |
21 | 19 | import android.app.AlarmManager; |
22 | -import android.app.IUserSwitchObserver; | |
23 | 20 | import android.app.PendingIntent; |
24 | 21 | import android.content.ContentUris; |
25 | -import android.content.ContentResolver; | |
26 | 22 | import android.content.Context; |
27 | 23 | import android.content.Intent; |
28 | 24 | import android.content.res.Configuration; |
29 | 25 | import android.content.res.Resources; |
30 | -import android.database.ContentObserver; | |
31 | 26 | import android.graphics.Outline; |
32 | 27 | import android.graphics.Rect; |
33 | 28 | import android.graphics.drawable.Animatable; |
34 | 29 | import android.graphics.drawable.Drawable; |
35 | 30 | import android.graphics.drawable.RippleDrawable; |
36 | 31 | import android.net.Uri; |
37 | -import android.os.Handler; | |
38 | -import android.os.IRemoteCallback; | |
39 | -import android.os.RemoteException; | |
40 | -import android.os.UserHandle; | |
41 | 32 | import android.provider.AlarmClock; |
42 | 33 | import android.provider.CalendarContract; |
43 | -import android.provider.Settings; | |
44 | -import android.net.Uri; | |
45 | 34 | import android.util.AttributeSet; |
46 | -import android.util.Log; | |
47 | 35 | import android.util.MathUtils; |
48 | 36 | import android.util.TypedValue; |
49 | 37 | import android.view.View; |
@@ -55,11 +43,11 @@ import android.widget.RelativeLayout; | ||
55 | 43 | import android.widget.Switch; |
56 | 44 | import android.widget.TextView; |
57 | 45 | import android.widget.Toast; |
46 | + | |
58 | 47 | import com.android.keyguard.KeyguardStatusView; |
59 | 48 | import com.android.systemui.BatteryMeterView; |
60 | 49 | import com.android.systemui.FontSizeUtils; |
61 | 50 | import com.android.systemui.R; |
62 | -import com.android.systemui.cm.UserContentObserver; | |
63 | 51 | import com.android.systemui.qs.QSPanel; |
64 | 52 | import com.android.systemui.qs.QSPanel.Callback; |
65 | 53 | import com.android.systemui.qs.QSTile; |
@@ -75,13 +63,14 @@ import com.android.systemui.tuner.TunerService; | ||
75 | 63 | import java.text.NumberFormat; |
76 | 64 | |
77 | 65 | import cyanogenmod.providers.CMSettings; |
66 | +import cyanogenmod.weather.util.WeatherUtils; | |
78 | 67 | |
79 | 68 | /** |
80 | 69 | * The view to manage the header area in the expanded status bar. |
81 | 70 | */ |
82 | 71 | public class StatusBarHeaderView extends BaseStatusBarHeader implements View.OnClickListener, |
83 | 72 | BatteryController.BatteryStateChangeCallback, NextAlarmController.NextAlarmChangeCallback, |
84 | - EmergencyListener, WeatherController.Callback { | |
73 | + EmergencyListener, WeatherController.Callback, TunerService.Tunable { | |
85 | 74 | |
86 | 75 | private boolean mExpanded; |
87 | 76 | private boolean mListening; |
@@ -152,7 +141,6 @@ public class StatusBarHeaderView extends BaseStatusBarHeader implements View.OnC | ||
152 | 141 | private float mCurrentT; |
153 | 142 | private boolean mShowingDetail; |
154 | 143 | private boolean mDetailTransitioning; |
155 | - private SettingsObserver mSettingsObserver; | |
156 | 144 | private boolean mShowWeather; |
157 | 145 | |
158 | 146 | private boolean mAllowExpand = true; |
@@ -195,7 +183,6 @@ public class StatusBarHeaderView extends BaseStatusBarHeader implements View.OnC | ||
195 | 183 | mWeatherContainer.setOnClickListener(this); |
196 | 184 | mWeatherLine1 = (TextView) findViewById(R.id.weather_line_1); |
197 | 185 | mWeatherLine2 = (TextView) findViewById(R.id.weather_line_2); |
198 | - mSettingsObserver = new SettingsObserver(new Handler()); | |
199 | 186 | loadDimens(); |
200 | 187 | updateVisibilities(); |
201 | 188 | updateClockScale(); |
@@ -419,7 +406,8 @@ public class StatusBarHeaderView extends BaseStatusBarHeader implements View.OnC | ||
419 | 406 | |
420 | 407 | private void updateListeners() { |
421 | 408 | if (mListening) { |
422 | - mSettingsObserver.observe(); | |
409 | + TunerService.get(getContext()).addTunable(this, | |
410 | + "cmsystem:" + CMSettings.System.STATUS_BAR_SHOW_WEATHER); | |
423 | 411 | mBatteryController.addStateChangedCallback(this); |
424 | 412 | mNextAlarmController.addStateChangedCallback(this); |
425 | 413 | mWeatherController.addCallback(this); |
@@ -427,7 +415,7 @@ public class StatusBarHeaderView extends BaseStatusBarHeader implements View.OnC | ||
427 | 415 | mBatteryController.removeStateChangedCallback(this); |
428 | 416 | mNextAlarmController.removeStateChangedCallback(this); |
429 | 417 | mWeatherController.removeCallback(this); |
430 | - mSettingsObserver.unobserve(); | |
418 | + TunerService.get(getContext()).removeTunable(this); | |
431 | 419 | } |
432 | 420 | } |
433 | 421 |
@@ -479,12 +467,12 @@ public class StatusBarHeaderView extends BaseStatusBarHeader implements View.OnC | ||
479 | 467 | |
480 | 468 | @Override |
481 | 469 | public void onWeatherChanged(WeatherController.WeatherInfo info) { |
482 | - if (info.temp == null || info.condition == null) { | |
470 | + if (Double.isNaN(info.temp) || info.condition == null) { | |
483 | 471 | mWeatherLine1.setText(null); |
484 | 472 | } else { |
485 | 473 | mWeatherLine1.setText(mContext.getString( |
486 | 474 | R.string.status_bar_expanded_header_weather_format, |
487 | - info.temp, | |
475 | + WeatherUtils.formatTemperature(info.temp, info.tempUnit), | |
488 | 476 | info.condition)); |
489 | 477 | } |
490 | 478 | mWeatherLine2.setText(info.city); |
@@ -930,35 +918,10 @@ public class StatusBarHeaderView extends BaseStatusBarHeader implements View.OnC | ||
930 | 918 | } |
931 | 919 | }; |
932 | 920 | |
933 | - class SettingsObserver extends UserContentObserver { | |
934 | - SettingsObserver(Handler handler) { | |
935 | - super(handler); | |
936 | - } | |
937 | - | |
938 | - @Override | |
939 | - protected void observe() { | |
940 | - super.observe(); | |
941 | - | |
942 | - ContentResolver resolver = mContext.getContentResolver(); | |
943 | - resolver.registerContentObserver(CMSettings.System.getUriFor( | |
944 | - CMSettings.System.STATUS_BAR_SHOW_WEATHER), false, this, UserHandle.USER_ALL); | |
945 | - update(); | |
946 | - } | |
947 | - | |
948 | - @Override | |
949 | - protected void unobserve() { | |
950 | - super.unobserve(); | |
951 | - | |
952 | - ContentResolver resolver = mContext.getContentResolver(); | |
953 | - resolver.unregisterContentObserver(this); | |
954 | - } | |
955 | - | |
956 | - @Override | |
957 | - public void update() { | |
958 | - | |
959 | - ContentResolver resolver = mContext.getContentResolver(); | |
960 | - mShowWeather = CMSettings.System.getInt( | |
961 | - resolver, CMSettings.System.STATUS_BAR_SHOW_WEATHER, 1) == 1; | |
921 | + @Override | |
922 | + public void onTuningChanged(String key, String newValue) { | |
923 | + if (key.endsWith(CMSettings.System.STATUS_BAR_SHOW_WEATHER)) { | |
924 | + mShowWeather = newValue == null || "1".equals(newValue); | |
962 | 925 | updateVisibilities(); |
963 | 926 | } |
964 | 927 | } |
@@ -25,8 +25,9 @@ public interface WeatherController { | ||
25 | 25 | void onWeatherChanged(WeatherInfo temp); |
26 | 26 | } |
27 | 27 | public static class WeatherInfo { |
28 | - public String temp = null; | |
28 | + public double temp = Double.NaN; | |
29 | 29 | public String city = null; |
30 | 30 | public String condition = null; |
31 | + public int tempUnit; | |
31 | 32 | } |
32 | 33 | } |
@@ -16,43 +16,49 @@ | ||
16 | 16 | |
17 | 17 | package com.android.systemui.statusbar.policy; |
18 | 18 | |
19 | -import android.content.BroadcastReceiver; | |
20 | 19 | import android.content.ComponentName; |
21 | -import android.content.ContentResolver; | |
22 | 20 | import android.content.Context; |
23 | 21 | import android.content.Intent; |
24 | -import android.content.IntentFilter; | |
25 | 22 | import android.database.ContentObserver; |
26 | 23 | import android.database.Cursor; |
27 | 24 | import android.net.Uri; |
28 | 25 | import android.os.Handler; |
29 | -import android.provider.Settings; | |
30 | 26 | import android.util.Log; |
27 | +import cyanogenmod.providers.CMSettings; | |
28 | +import cyanogenmod.providers.WeatherContract; | |
29 | +import cyanogenmod.weather.CMWeatherManager; | |
30 | +import cyanogenmod.weather.util.WeatherUtils; | |
31 | 31 | |
32 | 32 | import java.util.ArrayList; |
33 | 33 | |
34 | +import static cyanogenmod.providers.WeatherContract.WeatherColumns.CURRENT_CITY; | |
35 | +import static cyanogenmod.providers.WeatherContract.WeatherColumns.CURRENT_CONDITION; | |
36 | +import static cyanogenmod.providers.WeatherContract.WeatherColumns.CURRENT_TEMPERATURE; | |
37 | +import static cyanogenmod.providers.WeatherContract.WeatherColumns.CURRENT_TEMPERATURE_UNIT; | |
38 | +import static cyanogenmod.providers.WeatherContract.WeatherColumns.TempUnit.CELSIUS; | |
39 | +import static cyanogenmod.providers.WeatherContract.WeatherColumns.TempUnit.FAHRENHEIT; | |
40 | + | |
34 | 41 | public class WeatherControllerImpl implements WeatherController { |
35 | 42 | |
36 | 43 | private static final String TAG = WeatherController.class.getSimpleName(); |
37 | 44 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
45 | + private WeatherContentObserver mWeatherContentObserver; | |
46 | + private Handler mHandler; | |
47 | + private int mWeatherUnit; | |
48 | + private Uri mWeatherTempetarureUri; | |
38 | 49 | |
39 | 50 | public static final ComponentName COMPONENT_WEATHER_FORECAST = new ComponentName( |
40 | 51 | "com.cyanogenmod.lockclock", "com.cyanogenmod.lockclock.weather.ForecastActivity"); |
41 | - public static final String ACTION_UPDATE_FINISHED | |
42 | - = "com.cyanogenmod.lockclock.action.WEATHER_UPDATE_FINISHED"; | |
43 | - public static final String EXTRA_UPDATE_CANCELLED = "update_cancelled"; | |
44 | 52 | public static final String ACTION_FORCE_WEATHER_UPDATE |
45 | 53 | = "com.cyanogenmod.lockclock.action.FORCE_WEATHER_UPDATE"; |
46 | - public static final Uri CURRENT_WEATHER_URI | |
47 | - = Uri.parse("content://com.cyanogenmod.lockclock.weather.provider/weather/current"); | |
48 | - public static final String[] WEATHER_PROJECTION = new String[]{ | |
49 | - "temperature", | |
50 | - "city", | |
51 | - "condition" | |
54 | + private static final String[] WEATHER_PROJECTION = new String[]{ | |
55 | + CURRENT_TEMPERATURE, | |
56 | + CURRENT_TEMPERATURE_UNIT, | |
57 | + CURRENT_CITY, | |
58 | + CURRENT_CONDITION | |
52 | 59 | }; |
53 | 60 | |
54 | 61 | private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); |
55 | - private final Receiver mReceiver = new Receiver(); | |
56 | 62 | private final Context mContext; |
57 | 63 | |
58 | 64 | private WeatherInfo mCachedInfo = new WeatherInfo(); |
@@ -60,10 +66,16 @@ public class WeatherControllerImpl implements WeatherController { | ||
60 | 66 | public WeatherControllerImpl(Context context) { |
61 | 67 | mContext = context; |
62 | 68 | mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
69 | + mHandler = new Handler(); | |
70 | + mWeatherContentObserver = new WeatherContentObserver(mHandler); | |
71 | + mWeatherTempetarureUri | |
72 | + = CMSettings.Global.getUriFor(CMSettings.Global.WEATHER_TEMPERATURE_UNIT); | |
73 | + mContext.getContentResolver().registerContentObserver( | |
74 | + WeatherContract.WeatherColumns.CURRENT_WEATHER_URI,true, mWeatherContentObserver); | |
75 | + mContext.getContentResolver().registerContentObserver(mWeatherTempetarureUri, true, | |
76 | + mWeatherContentObserver); | |
77 | + queryWeatherTempUnit(); | |
63 | 78 | queryWeather(); |
64 | - final IntentFilter filter = new IntentFilter(); | |
65 | - filter.addAction(ACTION_UPDATE_FINISHED); | |
66 | - mContext.registerReceiver(mReceiver, filter); | |
67 | 79 | } |
68 | 80 | |
69 | 81 | public void addCallback(Callback callback) { |
@@ -85,17 +97,29 @@ public class WeatherControllerImpl implements WeatherController { | ||
85 | 97 | } |
86 | 98 | |
87 | 99 | private void queryWeather() { |
88 | - Cursor c = mContext.getContentResolver().query(CURRENT_WEATHER_URI, WEATHER_PROJECTION, | |
100 | + Cursor c = mContext.getContentResolver().query( | |
101 | + WeatherContract.WeatherColumns.CURRENT_WEATHER_URI, WEATHER_PROJECTION, | |
89 | 102 | null, null, null); |
90 | 103 | if (c == null) { |
91 | 104 | if(DEBUG) Log.e(TAG, "cursor was null for temperature, forcing weather update"); |
105 | + //LockClock keeps track of the user settings (temp unit, search by geo location/city) | |
106 | + //so we delegate the responsibility of handling a weather update to LockClock | |
92 | 107 | mContext.sendBroadcast(new Intent(ACTION_FORCE_WEATHER_UPDATE)); |
93 | 108 | } else { |
94 | 109 | try { |
95 | 110 | c.moveToFirst(); |
96 | - mCachedInfo.temp = c.getString(0); | |
97 | - mCachedInfo.city = c.getString(1); | |
98 | - mCachedInfo.condition = c.getString(2); | |
111 | + double temp = c.getDouble(0); | |
112 | + int reportedUnit = c.getInt(1); | |
113 | + if (reportedUnit == CELSIUS && mWeatherUnit == FAHRENHEIT) { | |
114 | + temp = WeatherUtils.celsiusToFahrenheit(temp); | |
115 | + } else if (reportedUnit == FAHRENHEIT && mWeatherUnit == CELSIUS) { | |
116 | + temp = WeatherUtils.fahrenheitToCelsius(temp); | |
117 | + } | |
118 | + | |
119 | + mCachedInfo.temp = temp; | |
120 | + mCachedInfo.tempUnit = mWeatherUnit; | |
121 | + mCachedInfo.city = c.getString(2); | |
122 | + mCachedInfo.condition = c.getString(3); | |
99 | 123 | } finally { |
100 | 124 | c.close(); |
101 | 125 | } |
@@ -108,19 +132,53 @@ public class WeatherControllerImpl implements WeatherController { | ||
108 | 132 | } |
109 | 133 | } |
110 | 134 | |
111 | - private final class Receiver extends BroadcastReceiver { | |
135 | + private class WeatherContentObserver extends ContentObserver { | |
136 | + | |
137 | + public WeatherContentObserver(Handler handler) { | |
138 | + super(handler); | |
139 | + } | |
140 | + | |
112 | 141 | @Override |
113 | - public void onReceive(Context context, Intent intent) { | |
114 | - if (DEBUG) Log.d(TAG, "onReceive " + intent.getAction()); | |
115 | - if (intent.hasExtra(EXTRA_UPDATE_CANCELLED)) { | |
116 | - if (intent.getBooleanExtra(EXTRA_UPDATE_CANCELLED, false)) { | |
117 | - // no update | |
118 | - return; | |
142 | + public void onChange(boolean selfChange, Uri uri) { | |
143 | + if (uri != null) { | |
144 | + if (uri.compareTo(WeatherContract.WeatherColumns.CURRENT_WEATHER_URI) == 0) { | |
145 | + queryWeather(); | |
146 | + fireCallback(); | |
147 | + } else if (uri.compareTo(mWeatherTempetarureUri) == 0) { | |
148 | + queryWeatherTempUnit(); | |
149 | + fixCachedWeatherInfo(); | |
150 | + fireCallback(); | |
151 | + } else { | |
152 | + super.onChange(selfChange, uri); | |
119 | 153 | } |
120 | 154 | } |
121 | - queryWeather(); | |
122 | - fireCallback(); | |
155 | + } | |
156 | + | |
157 | + @Override | |
158 | + public void onChange(boolean selfChange) { | |
159 | + onChange(selfChange, null); | |
123 | 160 | } |
124 | 161 | } |
125 | 162 | |
163 | + private void queryWeatherTempUnit() { | |
164 | + try { | |
165 | + mWeatherUnit = CMSettings.Global.getInt(mContext.getContentResolver(), | |
166 | + CMSettings.Global.WEATHER_TEMPERATURE_UNIT); | |
167 | + } catch (CMSettings.CMSettingNotFoundException e) { | |
168 | + //CMSettingsProvider should have taken care of setting a default value for this setting | |
169 | + //so how is that we ended up here?? We need to set a valid temp unit anyway to keep | |
170 | + //this going | |
171 | + mWeatherUnit = WeatherContract.WeatherColumns.TempUnit.CELSIUS; | |
172 | + } | |
173 | + } | |
174 | + | |
175 | + private void fixCachedWeatherInfo() { | |
176 | + if (mCachedInfo.tempUnit == CELSIUS && mWeatherUnit == FAHRENHEIT) { | |
177 | + mCachedInfo.temp = WeatherUtils.celsiusToFahrenheit(mCachedInfo.temp); | |
178 | + mCachedInfo.tempUnit = FAHRENHEIT; | |
179 | + } else if (mCachedInfo.tempUnit == FAHRENHEIT && mWeatherUnit == CELSIUS) { | |
180 | + mCachedInfo.temp = WeatherUtils.fahrenheitToCelsius(mCachedInfo.temp); | |
181 | + mCachedInfo.tempUnit = CELSIUS; | |
182 | + } | |
183 | + } | |
126 | 184 | } |