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

NewsReaderListFragment.java « 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: 95e8ae4fc01e99ffe9d82b4115f6fcf40ce4cd6e (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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/**
* 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;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Parcelable;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.michaelflisar.messagebar.MessageBar;
import com.michaelflisar.messagebar.messages.BaseMessage;
import com.michaelflisar.messagebar.messages.TextMessage;

import org.apache.http.client.HttpResponseException;
import org.apache.http.conn.HttpHostConnectException;

import de.luhmer.owncloudnewsreader.Constants.SYNC_TYPES;
import de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter;
import de.luhmer.owncloudnewsreader.authentication.AccountGeneral;
import de.luhmer.owncloudnewsreader.database.DatabaseConnection;
import de.luhmer.owncloudnewsreader.helper.AidlException;
import de.luhmer.owncloudnewsreader.helper.PostDelayHandler;
import de.luhmer.owncloudnewsreader.helper.ThemeChooser;
import de.luhmer.owncloudnewsreader.interfaces.ExpListTextClicked;
import de.luhmer.owncloudnewsreader.model.FolderSubscribtionItem;
import de.luhmer.owncloudnewsreader.services.IOwnCloudSyncService;
import de.luhmer.owncloudnewsreader.services.IOwnCloudSyncServiceCallback;
import de.luhmer.owncloudnewsreader.services.OwnCloudSyncService;
import uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;

/**
 * A list fragment representing a list of NewsReader. This fragment also
 * supports tablet devices by allowing list items to be given an 'activated'
 * state upon selection. This helps indicate which item is currently being
 * viewed in a {@link NewsReaderDetailFragment}.
 * <p>
 * Activities containing this fragment MUST implement the {@link Callbacks}
 * interface.
 */
public class NewsReaderListFragment extends SherlockFragment implements OnCreateContextMenuListener, OnRefreshListener/*
																ExpandableListView.OnChildClickListener,
																ExpandableListView.OnGroupCollapseListener,
																ExpandableListView.OnGroupExpandListener*/ {

	IOwnCloudSyncService _ownCloudSyncService;
	private IOwnCloudSyncServiceCallback callback = new IOwnCloudSyncServiceCallback.Stub() {

		@Override
		public void throwException(AidlException ex) throws RemoteException {
			HandleExceptionMessages(ex.getmException());
		}

		@Override
		public void startedSync(String sync_type) throws RemoteException {
			Handler refresh = new Handler(Looper.getMainLooper());
			refresh.post(new Runnable() {
				public void run() {
					UpdateSyncButtonLayout();
				}
			});
		}

		@Override
		public void finishedSync(String sync_type) throws RemoteException {
			Handler refresh = new Handler(Looper.getMainLooper());
			refresh.post(new Runnable() {
				public void run() {
					UpdateSyncButtonLayout();
				}
			});

			SYNC_TYPES st = SYNC_TYPES.valueOf(sync_type);

			switch(st) {
				case SYNC_TYPE__GET_API:
					break;
				case SYNC_TYPE__ITEM_STATES:
					break;
				case SYNC_TYPE__FOLDER:
					break;
				case SYNC_TYPE__FEEDS:
					break;
				case SYNC_TYPE__ITEMS:

					Log.d(TAG, "finished sync");
					refresh = new Handler(Looper.getMainLooper());
					refresh.post(new Runnable() {
						public void run() {
                        ReloadAdapter();
                        NewsReaderListActivity nlActivity = (NewsReaderListActivity) getActivity();
						if (nlActivity != null) {
                            nlActivity.UpdateItemList();

                            nlActivity.UpdatePodcastView();
                        }

                        SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
                        int newItemsCount = mPrefs.getInt(Constants.LAST_UPDATE_NEW_ITEMS_COUNT_STRING, 0);
                        if(newItemsCount > 0) {
                            MessageBar messageBar = new MessageBar(getActivity(), true);
                            TextMessage textMessage = new TextMessage( newItemsCount + " " + getString(R.string.message_bar_new_articles_available), getString(R.string.message_bar_reload), R.drawable.ic_menu_refresh);
                            textMessage.setClickListener(mListener);
                            messageBar.show(textMessage);
                        }
						}
					});
					break;
			}
		}

	};

	/*
	public IOwnCloudSyncServiceCallback getCallback() {
		return callback;
	}*/

    public void ListViewNotifyDataSetChanged()  {
        lvAdapter.NotifyDataSetChangedAsync();
    }

    public void ReloadAdapter() {
        new ReloadAdapterAsync().execute((Void) null);
    }

    private class ReloadAdapterAsync extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            progressBar.setVisibility(View.VISIBLE);

            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            lvAdapter.ReloadAdapter();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            //lvAdapter.notifyDataSetChanged();
            lvAdapter.NotifyDataSetChangedAsync();

            progressBar.setVisibility(View.GONE);

            super.onPostExecute(aVoid);
        }
    }



	/**
	 * The serialization (saved instance state) Bundle key representing the
	 * activated item position. Only used on tablets.
	 */
	//private static final String STATE_ACTIVATED_POSITION = "activated_position";

	protected static final String TAG = "NewsReaderListFragment";

	/**
	 * The fragment's current callback object, which is notified of list item
	 * clicks.
	 */
	private Callbacks mCallbacks = sExpListCallbacks;

    @Override
    public void onRefreshStarted(View view) {
        StartSync();
    }

    /**
	 * The current activated item position. Only used on tablets.
	 */
	//private static int mActivatedPosition = ListView.INVALID_POSITION;

	/**
	 * A callback interface that all activities containing this fragment must
	 * implement. This mechanism allows activities to be notified of item
	 * selections.
	 */
	public interface Callbacks {
		/**
		 * Callback for when an item has been selected.
		 */
		public void onChildItemClicked(String idSubscription, String optional_folder_id);
		public void onTopItemClicked(String idSubscription, boolean isFolder, String optional_folder_id);
	}

	/**
	 * A dummy implementation of the {@link Callbacks} interface that does
	 * nothing. Used only when this fragment is not attached to an activity.
	 */
	private static Callbacks sExpListCallbacks = new Callbacks() {
		@Override
		public void onChildItemClicked(String idSubscription, String optional_folder_id) {
		}

		@Override
		public void onTopItemClicked(String idSubscription, boolean isFolder, String optional_folder_id) {
		}
	};

	DatabaseConnection dbConn;
	//SubscriptionExpandableListAdapter lvAdapter;
	private SubscriptionExpandableListAdapter lvAdapter;
	//ExpandableListView eListView;
    ExpandableListView eListView;
    View progressBar;
    PullToRefreshLayout mPullToRefreshLayout;
	//public static IReader _Reader = null;  //AsyncTask_GetGReaderTags asyncTask_GetUnreadFeeds = null;

	//public static String username;
	//public static String password;
	//AsyncUpdateFinished asyncUpdateFinished;
	ServiceConnection mConnection = null;

    private BaseMessage.OnMessageClickListener mListener = null;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public NewsReaderListFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//setRetainInstance(true);

		try
		{
			dbConn = new DatabaseConnection(getActivity());

			//SettingsActivity.CheckForUnsycedChangesInDatabase(context);
			//dbConn.resetDatabase();
			//dbConn.clearDatabaseOverSize();
			//dbConn.resetRssItemsDatabase();

            mListener = new BaseMessage.OnMessageClickListener()
            {
                @Override
                public void onButton2Click(Parcelable data)
                {
                }

                @Override
                public void onButton1Click(Parcelable data)
                {
                    //Toast.makeText(getActivity(), "button 1 pressed", 3000).show();

                    NewsReaderDetailFragment ndf = ((NewsReaderDetailFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.content_frame));
                    if(ndf != null) {
                        try {
                            ndf.UpdateCurrentRssView(getActivity());
                            ndf.getLoaderManager().restartLoader(0, null, ndf.loaderCallbacks);
                            ndf.setActivatedPosition(0);
                            ndf.setMarginFromTop(0);
                        } catch(Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            };

		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}



	@Override
	public void onStart() {
		Intent serviceIntent = new Intent(getActivity(), OwnCloudSyncService.class);
		mConnection = generateServiceConnection();
        getActivity().bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
		super.onStart();
	}

	@Override
	public void onStop() {
		if(_ownCloudSyncService != null) {
			try {
                _ownCloudSyncService.unregisterCallback(callback);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		getActivity().unbindService(mConnection);
		mConnection = null;
		super.onStop();
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onResume()
	 */
	/*
	@Override
	public void onResume() {
		try {
			if(mActivatedPosition != ListView.INVALID_POSITION)
				geteListView().getRefreshableView().setSelection(mActivatedPosition);
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		super.onResume();
	}
	*/



	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onPause()
	 */
	@Override
	public void onPause() {
		//mActivatedPosition = geteListView().getRefreshableView().getFirstVisiblePosition();
		super.onPause();
	}

	private ServiceConnection generateServiceConnection() {
		return new ServiceConnection() {

	    	@Override
	    	public void onServiceConnected(ComponentName name, IBinder binder) {
                _ownCloudSyncService = IOwnCloudSyncService.Stub.asInterface(binder);
	    		try {
                    _ownCloudSyncService.registerCallback(callback);

	    			//Start auto sync if enabled
	    			SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
	    			if(mPrefs.getBoolean(SettingsActivity.CB_SYNCONSTARTUP_STRING, false))
	    				StartSync();

	    			if(getActivity() instanceof NewsReaderListActivity)
	    				((NewsReaderListActivity) getActivity()).UpdateButtonSyncLayout();
	    		}
	    		catch (Exception e) {
	    			e.printStackTrace();
	    		}
	    	}

	    	@Override
	    	public void onServiceDisconnected(ComponentName name) {
	    		try {
                    _ownCloudSyncService.unregisterCallback(callback);
	    		}
	    		catch (Exception e) {
	    			e.printStackTrace();
	    		}
	    	}
		};
    }


	public void StartSync()
	{
		SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

		if(mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null) == null)
			NewsReaderListActivity.StartLoginFragment((SherlockFragmentActivity) getActivity());
		else {
			try {
				if (!_ownCloudSyncService.isSyncRunning())
				{
					new PostDelayHandler(getActivity()).stopRunningPostDelayHandler();//Stop pending sync handler

					//_ownCloadSyncService.startSync();

					/*
			         * Request the sync for the default account, authority, and
			         * manual sync settings
			         */
					Bundle accBundle = new Bundle();
					accBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
					AccountManager mAccountManager = AccountManager.get(getActivity());
					Account[] accounts = mAccountManager.getAccounts();
					for(Account acc : accounts)
						if(acc.type.equals(AccountGeneral.ACCOUNT_TYPE))
							ContentResolver.requestSync(acc, AccountGeneral.ACCOUNT_TYPE, accBundle);
					//http://stackoverflow.com/questions/5253858/why-does-contentresolver-requestsync-not-trigger-a-sync
				} else {
                    UpdateSyncButtonLayout();
                }
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			//else
	            //_Reader.attachToRunningTask(-10, getActivity(), onAsyncTask_GetVersionFinished);
		}
	}



	private void HandleExceptionMessages(Exception ex) {
		if(ex instanceof HttpHostConnectException)
            ShowToastLong("Cannot connect to the Host !");
        else if(ex instanceof HttpResponseException)
        {
            HttpResponseException responseException = (HttpResponseException) ex;
            //if(responseException.getStatusCode() == 401)
            //    ShowToastLong("Authentication failed");
            //else
            ShowToastLong(responseException.getLocalizedMessage());
        }
        else
            ShowToastLong(ex.getLocalizedMessage());

		UpdateSyncButtonLayout();
	}



    public void ShowToastLong(String message)
    {
        Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
    }


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.expandable_list_layout, container, false);

        if(!ThemeChooser.isDarkTheme(getActivity())) {
            view.setBackgroundColor(getResources().getColor(R.color.slider_listview_background_color_light_theme));
        }

        progressBar = view.findViewById(R.id.pbProgress);

        eListView = (ExpandableListView) view.findViewById(R.id.expandableListView);

        lvAdapter = new SubscriptionExpandableListAdapter(getActivity(), dbConn, eListView);
        lvAdapter.setHandlerListener(expListTextClickedListener);

		eListView.setGroupIndicator(null);

        // Now find the PullToRefreshLayout to setup
        mPullToRefreshLayout = (PullToRefreshLayout) view.findViewById(R.id.ptr_layout);

        // Now setup the PullToRefreshLayout
        ActionBarPullToRefresh.from(getActivity())
                // Mark All Children as pullable
                .allChildrenArePullable()
                        // Set the OnRefreshListener
                .listener(this)
                        // Finally commit the setup to our PullToRefreshLayout
                .setup(mPullToRefreshLayout);

		eListView.setOnChildClickListener(onChildClickListener);
		//eListView.setSmoothScrollbarEnabled(true);

		eListView.setClickable(true);
		eListView.setAdapter(lvAdapter);


        lvAdapter.notifyDataSetChanged();
        ReloadAdapter();

		return view;
	}


	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		// Activities containing this fragment must implement its callbacks.
		if (!(activity instanceof Callbacks)) {
			throw new IllegalStateException(
					"Activity must implement fragment's callbacks.");
		}

		mCallbacks = (Callbacks) activity;
	}

	@Override
	public void onDetach() {
		super.onDetach();

		// Reset the active callbacks interface to the dummy implementation.
		mCallbacks = sExpListCallbacks;
	}

	ExpListTextClicked expListTextClickedListener = new ExpListTextClicked() {

		@Override
		public void onTextClicked(String idSubscription, Context context, boolean isFolder, String optional_folder_id) {
			mCallbacks.onTopItemClicked(idSubscription, isFolder, optional_folder_id);
		}
	};

	/*
	@Override
	public void onListItemClick(ListView listView, View view, int position,
			long id) {
		super.onListItemClick(listView, view, position, id);

		// Notify the active callbacks interface (the activity, if the
		// fragment is attached to one) that an item has been selected.

		//mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
	}*/


	OnChildClickListener onChildClickListener = new OnChildClickListener() {

		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {

			long idItem = lvAdapter.getChildId(groupPosition, childPosition);

			String optional_id_folder = null;
			FolderSubscribtionItem groupItem = (FolderSubscribtionItem) lvAdapter.getGroup(groupPosition);
			if(groupItem != null)
				optional_id_folder = String.valueOf(groupItem.id_database);

			mCallbacks.onChildItemClicked(String.valueOf(idItem), optional_id_folder);

			return false;
		}
	};

	/**
	 * Turns on activate-on-click mode. When this mode is on, list items will be
	 * given the 'activated' state when touched.
	 */
	public void setActivateOnItemClick(boolean activateOnItemClick) {
		// When setting CHOICE_MODE_SINGLE, ListView will automatically
		// give items the 'activated' state when touched.


		eListView.setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
	}

    public void UpdateSyncButtonLayout()
    {
    	if(getActivity() != null)
    		((NewsReaderListActivity) getActivity()).UpdateButtonSyncLayout();
    }
}