1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
package org.codefirex.cfxweather;
import org.codefirex.utils.WeatherAdapter;
import org.codefirex.utils.WeatherAdapter.WeatherListener;
import org.codefirex.utils.WeatherInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.os.Handler;
import android.provider.Settings;
import android.widget.RemoteViews;
public class WeatherNotification implements WeatherListener {
Context mContext;
WeatherAdapter mAdapter;
WeatherInfo mInfo;
RemoteViews mForecastView;
SettingsObserver mObserver;
Handler mHandler;
boolean mViewEnabled = false;
boolean mServiceEnabled = false;
boolean mStatusbarIconHide = false;
private static final boolean ADD_SYMBOL = true;
public WeatherNotification(Context context) {
mContext = context;
mHandler = new Handler();
mObserver = new SettingsObserver(mHandler);
mObserver.observe();
mAdapter = new WeatherAdapter(mContext, this);
mAdapter.startUpdates();
}
void unregister() {
mAdapter.stopUpdates();
}
private class SettingsObserver extends ContentObserver {
public SettingsObserver(Handler handler) {
super(handler);
}
void observe() {
ContentResolver resolver = mContext.getContentResolver();
resolver.registerContentObserver(
Settings.System.getUriFor("cfx_weather_notification"),
false, this);
resolver.registerContentObserver(
Settings.System.getUriFor("cfx_weather_icon"),
false, this);
onChange(true);
}
@Override
public void onChange(boolean selfChange) {
ContentResolver resolver = mContext.getContentResolver();
mViewEnabled = Settings.AOKP.getBoolean(resolver,
"cfx_weather_notification", false);
mStatusbarIconHide = Settings.AOKP.getBoolean(resolver,
"cfx_weather_icon", false);
updateNotification();
}
}
@Override
public void onServiceStateChanged(int state) {
switch (state) {
case WeatherAdapter.STATE_ON:
mServiceEnabled = true;
mObserver.onChange(true);
break;
case WeatherAdapter.STATE_OFF:
mServiceEnabled = false;
mObserver.onChange(true);
break;
case WeatherAdapter.STATE_SCALE:
mInfo = mAdapter.getLastKnownWeather();
updateNotification();
break;
case WeatherAdapter.STATE_UPDATED:
mInfo = mAdapter.getLatestWeather();
updateNotification();
break;
case WeatherAdapter.STATE_REFRESHING:
break;
}
}
void updateNotification() {
if (mServiceEnabled && mViewEnabled) {
buildNotification();
} else {
cancelNotification();
}
}
void cancelNotification() {
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(1);
}
void buildNotification() {
int icon = R.drawable.app_icon;
long when = System.currentTimeMillis();
String title = "CFXWeather";
if (mInfo == null) {
mInfo = mAdapter.getLatestWeather();
}
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(mContext);
RemoteViews contentView = new RemoteViews(mContext.getPackageName(),
R.layout.notification_view);
contentView
.setImageViewBitmap(R.id.n_current_weather, WeatherInfo
.getBitmapFromProvider(mContext, mInfo.mCurrentCode));
contentView.setTextViewText(R.id.n_current_temp, addSymbol(mInfo.getCurrentTemp()));
contentView.setImageViewBitmap(R.id.n_day_one_weather, WeatherInfo
.getBitmapFromProvider(mContext, mInfo.getForecastInfo1()
.getForecastCode()));
contentView.setTextViewText(R.id.n_day_one_day, mInfo
.getForecastInfo1().getForecastDay());
contentView.setTextViewText(R.id.n_day_one_high_text, addSymbol(mInfo
.getForecastInfo1().getForecastHighTemp()));
contentView.setTextViewText(R.id.n_day_one_low_text, addSymbol(mInfo
.getForecastInfo1().getForecastLowTemp()));
contentView.setImageViewBitmap(R.id.n_day_two_weather, WeatherInfo
.getBitmapFromProvider(mContext, mInfo.getForecastInfo2()
.getForecastCode()));
contentView.setTextViewText(R.id.n_day_two_day, mInfo
.getForecastInfo2().getForecastDay());
contentView.setTextViewText(R.id.n_day_two_high_text, addSymbol(mInfo
.getForecastInfo2().getForecastHighTemp()));
contentView.setTextViewText(R.id.n_day_two_low_text, addSymbol(mInfo
.getForecastInfo2().getForecastLowTemp()));
contentView.setImageViewBitmap(R.id.n_day_three_weather, WeatherInfo
.getBitmapFromProvider(mContext, mInfo.getForecastInfo3()
.getForecastCode()));
contentView.setTextViewText(R.id.n_day_three_day, mInfo
.getForecastInfo3().getForecastDay());
contentView.setTextViewText(R.id.n_day_three_high_text, addSymbol(mInfo
.getForecastInfo3().getForecastHighTemp()));
contentView.setTextViewText(R.id.n_day_three_low_text, addSymbol(mInfo
.getForecastInfo3().getForecastLowTemp()));
contentView.setImageViewBitmap(R.id.n_day_four_weather, WeatherInfo
.getBitmapFromProvider(mContext, mInfo.getForecastInfo4()
.getForecastCode()));
contentView.setTextViewText(R.id.n_day_four_day, mInfo
.getForecastInfo4().getForecastDay());
contentView.setTextViewText(R.id.n_day_four_high_text, addSymbol(mInfo
.getForecastInfo4().getForecastHighTemp()));
contentView.setTextViewText(R.id.n_day_four_low_text, addSymbol(mInfo
.getForecastInfo4().getForecastLowTemp()));
mForecastView = contentView;
Intent notificationIntent = new Intent(mContext, WeatherActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
notificationIntent, 0);
builder.setSmallIcon(icon);
builder.setOngoing(true);
builder.setContentIntent(contentIntent);
builder.setContent(mForecastView);
if (mStatusbarIconHide) {
builder.setPriority(Notification.PRIORITY_MIN);
}
mNotificationManager.notify(1, builder.build());
}
String addSymbol(String s) {
if (ADD_SYMBOL)
return WeatherInfo.addSymbol(s);
return s;
}
}
|