gt:                 if( dateEnd == null)
context: /* Copyright 2017 Thomas Schneider
   *
   * This file is a part of Fedilab
   *
   * This program is free software; you can redistribute it and/or modify it under the terms of the
   * GNU General Public License as published by the Free Software Foundation; either version 3 of the
   * License, or (at your option) any later version.
   *
   * Fedilab 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 General
   * Public License for more details.
   *
   * You should have received a copy of the GNU General Public License along with Fedilab; if not,
   * see <http://www.gnu.org/licenses>. */
  package app.fedilab.android.activities;
  
  
  import android.annotation.SuppressLint;
  import android.app.DatePickerDialog;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.content.SharedPreferences;
  import android.database.sqlite.SQLiteDatabase;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.support.v4.content.ContextCompat;
  import android.support.v4.content.LocalBroadcastManager;
  import android.support.v4.widget.SwipeRefreshLayout;
  import android.support.v7.app.ActionBar;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.widget.DividerItemDecoration;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.support.v7.widget.Toolbar;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.CheckBox;
  import android.widget.DatePicker;
  import android.widget.EditText;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  import android.widget.Spinner;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  import java.util.Calendar;
  import java.util.Date;
  import java.util.List;
  
  import app.fedilab.android.client.APIResponse;
  import app.fedilab.android.client.Entities.Account;
  import app.fedilab.android.client.Entities.Status;
  import app.fedilab.android.drawers.StatusListAdapter;
  import app.fedilab.android.helper.FilterToots;
  import app.fedilab.android.helper.Helper;
  import app.fedilab.android.services.BackupStatusInDataBaseService;
  import app.fedilab.android.sqlite.AccountDAO;
  import app.fedilab.android.sqlite.Sqlite;
  import app.fedilab.android.sqlite.StatusCacheDAO;
  import es.dmoral.toasty.Toasty;
  import app.fedilab.android.R;
  import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
  import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
  
  
  /**
   * Created by Thomas on 17/02/2018.
   * Show owner's toots
   */
  
  public class OwnerStatusActivity extends BaseActivity implements OnRetrieveFeedsInterface {
  
  
      private ImageView pp_actionBar;
      private StatusListAdapter statusListAdapter;
      private SharedPreferences sharedpreferences;
      private String max_id;
      private List<Status> statuses;
      private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
      private boolean firstLoad;
      private SwipeRefreshLayout swipeRefreshLayout;
      private boolean swiped;
      private boolean flag_loading;
      LinearLayoutManager mLayoutManager;
      private int style;
      private Button settings_time_from, settings_time_to;
      private FilterToots filterToots;
      private Date dateIni, dateEnd;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
          int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
          switch (theme){
              case Helper.THEME_LIGHT:
                  setTheme(R.style.AppTheme_NoActionBar);
                  break;
              case Helper.THEME_DARK:
                  setTheme(R.style.AppThemeDark_NoActionBar);
                  break;
              case Helper.THEME_BLACK:
                  setTheme(R.style.AppThemeBlack_NoActionBar);
                  break;
              default:
                  setTheme(R.style.AppThemeDark_NoActionBar);
          }
          setContentView(R.layout.activity_ower_status);
  
          filterToots = new FilterToots();
  
          LocalBroadcastManager.getInstance(this)
                  .registerReceiver(backupFinishedReceiver,
                          new IntentFilter(Helper.INTENT_BACKUP_FINISH));
  
          Toolbar toolbar = findViewById(R.id.toolbar);
          if( theme == Helper.THEME_BLACK)
              toolbar.setBackgroundColor(ContextCompat.getColor(OwnerStatusActivity.this, R.color.black));
          setSupportActionBar(toolbar);
  
          ActionBar actionBar = getSupportActionBar();
          if( actionBar != null ){
              LayoutInflater inflater = (LayoutInflater) this.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
              assert inflater != null;
              @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.toot_action_bar, null);
              actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
              actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  
              ImageView close_toot = actionBar.getCustomView().findViewById(R.id.close_toot);
              close_toot.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      finish();
                  }
              });
              TextView toolbarTitle = actionBar.getCustomView().findViewById(R.id.toolbar_title);
              pp_actionBar = actionBar.getCustomView().findViewById(R.id.pp_actionBar);
              if (theme == Helper.THEME_LIGHT){
                  Helper.colorizeToolbar(actionBar.getCustomView().findViewById(R.id.toolbar), R.color.black, OwnerStatusActivity.this);
              }
              toolbarTitle.setText(getString(R.string.owner_cached_toots));
          }
          statuses = new ArrayList<>();
          RecyclerView lv_status = findViewById(R.id.lv_status);
          mainLoader =  findViewById(R.id.loader);
          nextElementLoader = findViewById(R.id.loading_next_status);
          textviewNoAction =  findViewById(R.id.no_action);
          mainLoader.setVisibility(View.VISIBLE);
          nextElementLoader.setVisibility(View.GONE);
          max_id = null;
          flag_loading = true;
          firstLoad = true;
          swiped = false;
          boolean isOnWifi = Helper.isOnWIFI(OwnerStatusActivity.this);
          lv_status.addItemDecoration(new DividerItemDecoration(OwnerStatusActivity.this, DividerItemDecoration.VERTICAL));
          String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
          statusListAdapter = new StatusListAdapter(OwnerStatusActivity.this, RetrieveFeedsAsyncTask.Type.CACHE_STATUS, userId, isOnWifi, this.statuses);
          lv_status.setAdapter(statusListAdapter);
          mLayoutManager = new LinearLayoutManager(OwnerStatusActivity.this);
          lv_status.setLayoutManager(mLayoutManager);
  
  
          if( theme == Helper.THEME_DARK){
              style = R.style.DialogDark;
          }else {
              style = R.style.Dialog;
          }
  
          SQLiteDatabase db = Sqlite.getInstance(OwnerStatusActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
          Account account = new AccountDAO(OwnerStatusActivity.this,db).getAccountByID(userId);
          String url = account.getAvatar();
          if( url.startsWith("/") ){
              url = Helper.getLiveInstanceWithProtocol(OwnerStatusActivity.this) + account.getAvatar();
          }
          Helper.loadGiF(getApplicationContext(), url, pp_actionBar);
  
          swipeRefreshLayout = findViewById(R.id.swipeContainer);
          new RetrieveFeedsAsyncTask(OwnerStatusActivity.this, filterToots, null, OwnerStatusActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
          switch (theme){
              case Helper.THEME_LIGHT:
                  swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
                          R.color.mastodonC2,
                          R.color.mastodonC3);
                  swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(OwnerStatusActivity.this, R.color.white));
                  break;
              case Helper.THEME_DARK:
                  swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4__,
                          R.color.mastodonC4,
                          R.color.mastodonC4);
                  swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(OwnerStatusActivity.this, R.color.mastodonC1_));
                  break;
              case Helper.THEME_BLACK:
                  swipeRefreshLayout.setColorSchemeResources(R.color.dark_icon,
                          R.color.mastodonC2,
                          R.color.mastodonC3);
                  swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(OwnerStatusActivity.this, R.color.black_3));
                  break;
          }
          swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
              @Override
              public void onRefresh() {
                  max_id = null;
                  firstLoad = true;
                  flag_loading = true;
                  swiped = true;
                  new RetrieveFeedsAsyncTask(OwnerStatusActivity.this, filterToots, null, OwnerStatusActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
              }
          });
  
          lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() {
              public void onScrolled(RecyclerView recyclerView, int dx, int dy)
              {
                  int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
                  if(dy > 0){
                      int visibleItemCount = mLayoutManager.getChildCount();
                      int totalItemCount = mLayoutManager.getItemCount();
                      if(firstVisibleItem + visibleItemCount == totalItemCount ) {
                          if(!flag_loading ) {
                              flag_loading = true;
                              new RetrieveFeedsAsyncTask(OwnerStatusActivity.this, filterToots, max_id, OwnerStatusActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                              nextElementLoader.setVisibility(View.VISIBLE);
                          }
                      } else {
                          nextElementLoader.setVisibility(View.GONE);
                      }
                  }
              }
          });
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.option_owner_cache, menu);
          SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
          int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
          if( theme == Helper.THEME_LIGHT)
              Helper.colorizeIconMenu(menu, R.color.black);
          return true;
      }
  
      private DatePickerDialog.OnDateSetListener iniDateSetListener =
              new DatePickerDialog.OnDateSetListener() {
  
                  public void onDateSet(DatePicker view, int year,
                                        int monthOfYear, int dayOfMonth) {
                      Calendar c = Calendar.getInstance();
                      c.set(year, monthOfYear, dayOfMonth, 0, 0);
                      dateIni = new Date(c.getTimeInMillis());
                      settings_time_from.setText(Helper.shortDateToString(new Date(c.getTimeInMillis())));
                  }
  
              };
      private DatePickerDialog.OnDateSetListener endDateSetListener =
              new DatePickerDialog.OnDateSetListener() {
  
                  public void onDateSet(DatePicker view, int year,
                                        int monthOfYear, int dayOfMonth) {
                      Calendar c = Calendar.getInstance();
                      c.set(year, monthOfYear, dayOfMonth, 23, 59);
  
                      dateEnd = new Date(c.getTimeInMillis());
                      settings_time_to.setText(Helper.shortDateToString(new Date(c.getTimeInMillis())));
                  }
  
              };
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case android.R.id.home:
                  finish();
                  return true;
              case R.id.action_sync:
                  Intent backupIntent = new Intent(OwnerStatusActivity.this, BackupStatusInDataBaseService.class);
                  startService(backupIntent);
                  return true;
              case R.id.action_filter:
                  SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
                  int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
                  if (theme == Helper.THEME_DARK) {
                      style = R.style.DialogDark;
                  } else if (theme == Helper.THEME_BLACK){
                      style = R.style.DialogBlack;
                  }else {
                      style = R.style.Dialog;
                  }
                  AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(OwnerStatusActivity.this, style);
                  LayoutInflater inflater = this.getLayoutInflater();
                  @SuppressLint("InflateParams") View dialogView = inflater.inflate(R.layout.filter_owner_toots, null);
                  dialogBuilder.setView(dialogView);
  
  
                  SQLiteDatabase db = Sqlite.getInstance(OwnerStatusActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
                  if( dateIni == null)
                      dateIni = new StatusCacheDAO(OwnerStatusActivity.this, db).getSmallerDate(StatusCacheDAO.ARCHIVE_CACHE);
  