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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/**
* Android ownCloud News
*
* @author David Luhmer
* @copyright 2013 David Luhmer david-dev@live.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.luhmer.owncloudnewsreader.services;
import android.app.ActivityManager;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import de.luhmer.owncloudnewsreader.Constants;
import de.luhmer.owncloudnewsreader.Constants.SYNC_TYPES;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.helper.AidlException;
import de.luhmer.owncloudnewsreader.helper.NotificationManagerNewsReader;
import de.luhmer.owncloudnewsreader.reader.FeedItemTags.TAGS;
import de.luhmer.owncloudnewsreader.reader.OnAsyncTaskCompletedListener;
import de.luhmer.owncloudnewsreader.reader.owncloud.API;
import de.luhmer.owncloudnewsreader.reader.owncloud.OwnCloud_Reader;
import de.luhmer.owncloudnewsreader.services.IOwnCloudSyncService.Stub;
import de.luhmer.owncloudnewsreader.widget.WidgetProvider;
public class OwnCloudSyncService extends Service {
protected static final String TAG = "OwnCloudSyncService";
private RemoteCallbackList<IOwnCloudSyncServiceCallback> callbacks = new RemoteCallbackList<>();
private Stub mBinder = new IOwnCloudSyncService.Stub() {
public void registerCallback(IOwnCloudSyncServiceCallback callback) {
callbacks.register(callback);
}
public void unregisterCallback(IOwnCloudSyncServiceCallback callback) {
callbacks.unregister(callback);
}
@Override
public void startSync() throws RemoteException {
if(!isSyncRunning()) {
startedSync(SYNC_TYPES.SYNC_TYPE__GET_API);
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(OwnCloudSyncService.this);
String username = mPrefs.getString(SettingsActivity.EDT_USERNAME_STRING, "");
String password = mPrefs.getString(SettingsActivity.EDT_PASSWORD_STRING, "");
_Reader.Start_AsyncTask_GetVersion(Constants.TaskID_GetVersion, OwnCloudSyncService.this, onAsyncTask_GetVersionFinished, username, password);
}
}
@Override
public boolean isSyncRunning() throws RemoteException {
return _Reader.isSyncRunning();
}
};
static OwnCloud_Reader _Reader;
@Override
public void onCreate() {
super.onCreate();
if(_Reader == null)
_Reader = new OwnCloud_Reader();
Log.d(TAG, "onCreate() called");
}
@Override
public boolean onUnbind(Intent intent) {
//Destroy service if no sync is running
if(!_Reader.isSyncRunning()) {
Log.v(TAG, "Stopping service because of inactivity");
stopSelf();
}
return super.onUnbind(intent);
}
OnAsyncTaskCompletedListener onAsyncTask_GetVersionFinished = new OnAsyncTaskCompletedListener() {
@Override
public void onAsyncTaskCompleted(int task_id, Object task_result) {
finishedSync(SYNC_TYPES.SYNC_TYPE__GET_API);
if(!(task_result instanceof Exception))
{
String appVersion = task_result.toString();
API api = API.GetRightApiForVersion(appVersion, OwnCloudSyncService.this);
_Reader.setApi(api);
_Reader.Start_AsyncTask_PerformItemStateChange(Constants.TaskID_PerformStateChange, OwnCloudSyncService.this, onAsyncTask_PerformTagExecute);
startedSync(SYNC_TYPES.SYNC_TYPE__ITEM_STATES);
}
else
ThrowException((Exception) task_result);
}
};
//Sync state of items e.g. read/unread/starred/unstarred
OnAsyncTaskCompletedListener onAsyncTask_PerformTagExecute = new OnAsyncTaskCompletedListener() {
@Override
public void onAsyncTaskCompleted(int task_id, Object task_result) {
finishedSync(SYNC_TYPES.SYNC_TYPE__ITEM_STATES);
if(task_result != null)//task result is null if there was an error
{
if((Boolean) task_result)
{
if(task_id == Constants.TaskID_PerformStateChange) {
_Reader.Start_AsyncTask_GetFolder(Constants.TaskID_GetFolder, OwnCloudSyncService.this, onAsyncTask_GetFolder);
startedSync(SYNC_TYPES.SYNC_TYPE__FOLDER);
}
else
_Reader.setSyncRunning(true);
}
}
}
};
OnAsyncTaskCompletedListener onAsyncTask_GetFolder = new OnAsyncTaskCompletedListener() {
@Override
public void onAsyncTaskCompleted(int task_id, Object task_result) {
finishedSync(SYNC_TYPES.SYNC_TYPE__FOLDER);
if(task_result != null)
ThrowException((Exception) task_result);
else {
_Reader.Start_AsyncTask_GetFeeds(Constants.TaskID_GetFeeds, OwnCloudSyncService.this, onAsyncTask_GetFeed);
startedSync(SYNC_TYPES.SYNC_TYPE__FEEDS);
}
Log.d(TAG, "onAsyncTask_GetFolder Finished");
}
};
OnAsyncTaskCompletedListener onAsyncTask_GetFeed = new OnAsyncTaskCompletedListener() {
@Override
public void onAsyncTaskCompleted(int task_id, Object task_result) {
finishedSync(SYNC_TYPES.SYNC_TYPE__FEEDS);
if(task_result != null)
ThrowException((Exception) task_result);
else {
_Reader.Start_AsyncTask_GetItems(Constants.TaskID_GetItems, OwnCloudSyncService.this, onAsyncTask_GetItems, TAGS.ALL);//Recieve all unread Items
startedSync(SYNC_TYPES.SYNC_TYPE__ITEMS);
}
Log.d(TAG, "onAsyncTask_GetFeed Finished");
}
};
OnAsyncTaskCompletedListener onAsyncTask_GetItems = new OnAsyncTaskCompletedListener() {
@Override
public void onAsyncTaskCompleted(int task_id, Object task_result) {
finishedSync(SYNC_TYPES.SYNC_TYPE__ITEMS);
if(task_result != null)
ThrowException((Exception) task_result);
else
{
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(OwnCloudSyncService.this);
int newItemsCount = mPrefs.getInt(Constants.LAST_UPDATE_NEW_ITEMS_COUNT_STRING, 0);
if(newItemsCount > 0) {
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = am.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(!componentInfo.getPackageName().equals("de.luhmer.owncloudnewsreader")) {
Resources res = getResources();
String tickerText = res.getQuantityString(R.plurals.notification_new_items_ticker, newItemsCount, newItemsCount);
String contentText = res.getQuantityString(R.plurals.notification_new_items_text, newItemsCount, newItemsCount);
String title = getString(R.string.app_name);
if(mPrefs.getBoolean(SettingsActivity.CB_SHOW_NOTIFICATION_NEW_ARTICLES_STRING, true))//Default is true
NotificationManagerNewsReader.getInstance(OwnCloudSyncService.this).ShowMessage(title, tickerText, contentText);
}
UpdateWidget();
}
}
Log.d(TAG, "onAsyncTask_GetItems Finished");
//fireUpdateFinishedClicked();
}
};
private void UpdateWidget()
{
Intent intent = new Intent(this, WidgetProvider.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int ids[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), WidgetProvider.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,ids);
sendBroadcast(intent);
}
private void ThrowException(Exception ex) {
List<IOwnCloudSyncServiceCallback> callbackList = getCallBackItemsAndBeginBroadcast();
for (IOwnCloudSyncServiceCallback icb : callbackList) {
try {
icb.throwException(new AidlException(ex));
} catch (RemoteException e) {
e.printStackTrace();
}
}
callbacks.finishBroadcast();
}
private void startedSync(SYNC_TYPES sync_type) {
List<IOwnCloudSyncServiceCallback> callbackList = getCallBackItemsAndBeginBroadcast();
for(IOwnCloudSyncServiceCallback icb : callbackList) {
try {
icb.startedSync(sync_type.toString());
//icb.finishedSyncOfItems();
} catch (RemoteException e) {
e.printStackTrace();
}
}
callbacks.finishBroadcast();
}
private void finishedSync(SYNC_TYPES sync_type) {
Log.v(TAG, "Finished Sync: " + sync_type.toString());
List<IOwnCloudSyncServiceCallback> callbackList = getCallBackItemsAndBeginBroadcast();
for(IOwnCloudSyncServiceCallback icb : callbackList) {
try {
icb.finishedSync(sync_type.toString());
//icb.finishedSyncOfItems();
} catch (RemoteException e) {
e.printStackTrace();
}
}
callbacks.finishBroadcast();
}
private List<IOwnCloudSyncServiceCallback> getCallBackItemsAndBeginBroadcast() {
// Broadcast to all clients the new value.
List<IOwnCloudSyncServiceCallback> callbackList = new ArrayList<>();
final int N = callbacks.beginBroadcast();
for (int i=0; i < N; i++) {
callbackList.add(callbacks.getBroadcastItem(i));
}
return callbackList;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
|