Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OwnCloudSyncAdapter.java « authentication « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 90e42a697943a4b26275f155e8e21566fed0b1a8 (plain)
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
package de.luhmer.owncloudnewsreader.authentication;

import android.accounts.Account;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SyncResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;

import java.util.List;

import javax.inject.Inject;

import de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter;
import de.luhmer.owncloudnewsreader.NewsReaderApplication;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.database.model.Feed;
import de.luhmer.owncloudnewsreader.database.model.Folder;
import de.luhmer.owncloudnewsreader.di.ApiProvider;
import de.luhmer.owncloudnewsreader.helper.ForegroundListener;
import de.luhmer.owncloudnewsreader.helper.StopWatch;
import de.luhmer.owncloudnewsreader.notification.NextcloudNotificationManager;
import de.luhmer.owncloudnewsreader.reader.InsertIntoDatabase;
import de.luhmer.owncloudnewsreader.reader.nextcloud.ItemStateSync;
import de.luhmer.owncloudnewsreader.reader.nextcloud.RssItemObservable;
import de.luhmer.owncloudnewsreader.services.DownloadImagesService;
import de.luhmer.owncloudnewsreader.services.events.SyncFailedEvent;
import de.luhmer.owncloudnewsreader.services.events.SyncFinishedEvent;
import de.luhmer.owncloudnewsreader.services.events.SyncStartedEvent;
import de.luhmer.owncloudnewsreader.ssl.OkHttpSSLClient;
import de.luhmer.owncloudnewsreader.widget.WidgetProvider;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function3;
import io.reactivex.schedulers.Schedulers;

public class OwnCloudSyncAdapter extends AbstractThreadedSyncAdapter {

    private static final String TAG = OwnCloudSyncAdapter.class.getCanonicalName();
    public boolean syncRunning = false;

    protected @Inject SharedPreferences mPrefs;
    protected @Inject ApiProvider mApi;


    public OwnCloudSyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);

        ((NewsReaderApplication) context).getAppComponent().injectService(this);
    }



    @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
        Log.d("udinic", "onPerformSync for account[" + account.name + "] [" + Thread.currentThread().getName() + "]\"");
        StopWatch syncStopWatch = new StopWatch();
        syncStopWatch.start();

        // Send sync started event
        syncRunning = true;
        EventBus.getDefault().post(new SyncStartedEvent());

        // run actual sync
        sync();

        // Update Widget / Notification
        WidgetProvider.UpdateWidget(getContext());
        updateNotification();

        // Download Favicons for feeds
        startFaviconDownload();


        // Send sync finished event
        syncRunning = false;
        EventBus.getDefault().post(new SyncFinishedEvent());

        syncStopWatch.stop();
        Log.v(TAG, "Finished sync - time needed (synchronization): " + syncStopWatch.toString());
    }



    private class NextcloudSyncResult {
        private final List<Folder> folders;
        private final List<Feed>   feeds;
        private final boolean      stateSyncSuccessful;

        NextcloudSyncResult(List<Folder> folders, List<Feed> feeds, Boolean stateSyncSuccessful) {
            this.folders = folders;
            this.feeds = feeds;
            this.stateSyncSuccessful = stateSyncSuccessful;
        }
    }


    // Start sync
    private void sync() {
        if(mApi.getNewsAPI() == null) {
            throwException(new IllegalStateException("API is NOT initialized"));
            Log.e(TAG, "API is NOT initialized..");
            return;
        } else {
            Log.v(TAG, "API is initialized..");
        }

        final DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(getContext());

        Observable<Boolean> rssStateSync = Observable.fromPublisher(
                new Publisher<Boolean>() {
                    @Override
                    public void subscribe(Subscriber<? super Boolean> s) {
                        Log.v(TAG, "(rssStateSync) subscribe() called with: s = [" + s + "] [" + Thread.currentThread().getName() + "]");
                        try {
                            boolean success = ItemStateSync.PerformItemStateSync(mApi.getNewsAPI(), dbConn);
                            s.onNext(success);
                            s.onComplete();
                        } catch(Exception ex) {
                            s.onError(ex);
                        }
                    }
                }).subscribeOn(Schedulers.newThread());

        // First sync Feeds and Folders and rss item states (in parallel)
        Observable<List<Folder>> folderObservable = mApi
                .getNewsAPI()
                .folders()
                .subscribeOn(Schedulers.newThread());

        Observable<List<Feed>> feedsObservable = mApi
                .getNewsAPI()
                .feeds()
                .subscribeOn(Schedulers.newThread());

        // Wait for results
        Observable<NextcloudSyncResult> combined = Observable.zip(folderObservable, feedsObservable, rssStateSync, new Function3<List<Folder>, List<Feed>, Boolean, NextcloudSyncResult>() {
            @Override
            public NextcloudSyncResult apply(@NonNull List<Folder> folders, @NonNull List<Feed> feeds, @NonNull Boolean mRes) {
                Log.v(TAG, "apply() called with: folders = [" + folders + "], feeds = [" + feeds + "], mRes = [" + mRes + "] [" + Thread.currentThread().getName() + "]");
                return new NextcloudSyncResult(folders, feeds, mRes);
            }
        });

        Log.v(TAG, "subscribing now.. [" + Thread.currentThread().getName() + "]");


        try {
            NextcloudSyncResult syncResult = combined.blockingFirst();

            InsertIntoDatabase.InsertFoldersIntoDatabase(syncResult.folders, dbConn);
            InsertIntoDatabase.InsertFeedsIntoDatabase(syncResult.feeds, dbConn);
            Log.v(TAG, "State sync successful: " + syncResult.stateSyncSuccessful);

            // Start the sync (Rss Items)
            syncRssItems(dbConn);
        } catch(Exception ex) {
            //Log.e(TAG, "throwException: ", ex);
            throwException(ex);
        }
    }

    private void syncRssItems(final DatabaseConnectionOrm dbConn) {
        Log.v(TAG, "syncRssItems() called with: dbConn = [" + dbConn + "] [" + Thread.currentThread().getName() + "]");

        // .observeOn(AndroidSchedulers.mainThread())

        Observable.fromPublisher(new RssItemObservable(dbConn, mApi.getNewsAPI(), mPrefs))
                .subscribeOn(Schedulers.newThread())
                .blockingSubscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.d(TAG, "[syncRssItems] - onSubscribe() called");
                    }

                    @Override
                    public void onNext(@NonNull final Integer totalCount) {
                        Log.v(TAG, "[syncRssItems] - onNext() called with: totalCount = [" + totalCount + "]");

                        Handler handler = new Handler(Looper.getMainLooper());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(
                                        getContext(),
                                        getContext().getResources().getQuantityString(R.plurals.fetched_items_so_far, totalCount, totalCount),
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.v(TAG, "[syncRssItems] - onError() called with: throwable = [" + e + "]");
                        throwException(e);
                    }

                    @Override
                    public void onComplete() {
                        Log.v(TAG, "[syncRssItems] - onComplete() called");
                    }
                });
    }


    private void throwException(Throwable ex) {
        Log.e(TAG, "throwException() called [" + Thread.currentThread().getName() + "]", ex);
        syncRunning = false;
        if(ex instanceof Exception) {
            EventBus.getDefault().post(SyncFailedEvent.create(OkHttpSSLClient.HandleExceptions((Exception) ex)));
        } else {
            EventBus.getDefault().post(SyncFailedEvent.create(ex));
        }
    }

    private void updateNotification() {
        DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(getContext());
        int newItemsCount = Integer.parseInt(dbConn.getUnreadItemsCountForSpecificFolder(SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_UNREAD_ITEMS));
        //int newItemsCount = mPrefs.getInt(Constants.LAST_UPDATE_NEW_ITEMS_COUNT_STRING, 0);

        if(newItemsCount > 0) {
            boolean showNotificationOnNewArticles = mPrefs.getBoolean(SettingsActivity.CB_SHOW_NOTIFICATION_NEW_ARTICLES_STRING, true);
            // If another app is opened show a notification
            if (!ForegroundListener.isInForeground() && showNotificationOnNewArticles) {
                NextcloudNotificationManager.showUnreadRssItemsNotification(getContext(), newItemsCount, mPrefs);
            }
        }
    }

    private void startFaviconDownload() {
        Intent data = new Intent();
        data.putExtra(DownloadImagesService.DOWNLOAD_MODE_STRING, DownloadImagesService.DownloadMode.FAVICONS_ONLY);
        DownloadImagesService.enqueueWork(getContext(), data);
    }
}