<commit_before>package com.mapswithme.country;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.mapswithme.maps.MWMActivity;
import com.mapswithme.maps.MapStorage;
import com.mapswithme.maps.MapStorage.Index;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.MapsWithMeBaseListActivity;


public class DownloadActivity extends MapsWithMeBaseListActivity implements MapStorage.Listener, View.OnClickListener
{
  static String TAG = DownloadActivity.class.getName();
  private ExtendedDownloadAdapterWrapper mExtendedAdapter;
  private DownloadedAdapter mDownloadedAdapter;
  private TextView mAbButton;
  private int mMode = MODE_DISABLED;

  private static final int MODE_DISABLED = -1;
  private static final int MODE_NONE = 0;
  private static final int MODE_UPDATE_ALL = 1;
  private static final int MODE_CANCEL_ALL = 2;


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

    setContentView(R.layout.downloader_list_view);

    mExtendedAdapter = new ExtendedDownloadAdapterWrapper(this, new DownloadAdapter(this));
    setListAdapter(mExtendedAdapter);
    mMode = MODE_DISABLED;
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.ab_downloader, menu);

    final MenuItem item = menu.findItem(R.id.item_update);
    mAbButton = (TextView) item.getActionView();
    mAbButton.setOnClickListener(this);
    mAbButton.setVisibility(View.GONE);

    updateActionBar();
    return true;
  }

  private BaseDownloadAdapter getDownloadAdapter()
  {
    return (BaseDownloadAdapter) getListView().getAdapter();
  }

  @Override
  protected void onResume()
  {
    super.onResume();
    getDownloadAdapter().onResume(getListView());
  }

  @Override
  protected void onPause()
  {
    super.onPause();
    getDownloadAdapter().onPause();
  }

  @Override
  public void onBackPressed()
  {
    if (getDownloadAdapter().onBackPressed())
    {
      // scroll list view to the top
      setSelection(0);
    }
    else if (getListAdapter() instanceof DownloadedAdapter)
    {
      mMode = MODE_DISABLED;
      mDownloadedAdapter.onPause();
      mExtendedAdapter.onResume(getListView());
      setListAdapter(mExtendedAdapter);
      updateActionBar();
    }
    else
    {
      super.onBackPressed();
      // Always show map as parent
      startActivity(new Intent(this, MWMActivity.class));
    }
  }

  @Override
  public void onCountryStatusChanged(final Index idx)
  {
    updateActionBar();
  }

  @Override
  public void onCountryProgress(Index idx, long current, long total)
  {

  }

  private void updateActionBar()
  {
    if (mAbButton == null)
      return;
    if (mMode == MODE_DISABLED)
    {
      mAbButton.setVisibility(View.GONE);
      return;
    }
    if (ActiveCountryTree.isDownloadingActive())
    {
      mMode = MODE_CANCEL_ALL;
      mAbButton.setText(getString(R.string.downloader_cancel_all));
      mAbButton.setTextColor(getResources().getColor(R.color.downloader_red));
      mAbButton.setVisibility(View.VISIBLE);
    }
    else if (ActiveCountryTree.getCountInGroup(ActiveCountryTree.GROUP_OUT_OF_DATE) > 0)
    {
      mMode = MODE_UPDATE_ALL;
      mAbButton.setText(getString(R.string.downloader_update_all));
      mAbButton.setTextColor(getResources().getColor(R.color.downloader_green));
      mAbButton.setVisibility(View.VISIBLE);
    }
    invalidateOptionsMenu();
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id)
  {
    if (getListAdapter().getItemViewType(position) == ExtendedDownloadAdapterWrapper.TYPE_EXTENDED)
    {
      setListAdapter(getDownloadedAdapter());
      mMode = MODE_NONE;
      updateActionBar();
      mExtendedAdapter.onPause();
      mDownloadedAdapter.onResume(getListView());
    }
  }

  private ListAdapter getDownloadedAdapter()
  {
    if (mDownloadedAdapter == null)
      mDownloadedAdapter = new DownloadedAdapter(this);

    return mDownloadedAdapter;
  }

  @Override
  public void onClick(View v)
  {
    switch (v.getId())
    {
    case R.id.item_update:
      if (mMode == MODE_UPDATE_ALL)
        ActiveCountryTree.updateAll();
      else
        ActiveCountryTree.cancelAll();
      updateActionBar();
      break;
    }
  }
}<commit_msg>Cancel all/update all button updates.<commit_after>package com.mapswithme.country;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.mapswithme.maps.MWMActivity;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.MapsWithMeBaseListActivity;

import static com.mapswithme.country.ActiveCountryTree.ActiveCountryListener;


public class DownloadActivity extends MapsWithMeBaseListActivity implements View.OnClickListener, ActiveCountryListener
{
  static String TAG = DownloadActivity.class.getName();
  private ExtendedDownloadAdapterWrapper mExtendedAdapter;
  private DownloadedAdapter mDownloadedAdapter;
  private TextView mAbButton;
  private int mMode = MODE_DISABLED;
  private int mListenerSlotId;

  private static final int MODE_DISABLED = -1;
  private static final int MODE_NONE = 0;
  private static final int MODE_UPDATE_ALL = 1;
  private static final int MODE_CANCEL_ALL = 2;


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

    setContentView(R.layout.downloader_list_view);

    mExtendedAdapter = new ExtendedDownloadAdapterWrapper(this, new DownloadAdapter(this));
    setListAdapter(mExtendedAdapter);
    mMode = MODE_DISABLED;
    mListenerSlotId = ActiveCountryTree.addListener(this);
  }

  @Override
  protected void onDestroy()
  {
    super.onDestroy();

    ActiveCountryTree.removeListener(mListenerSlotId);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.ab_downloader, menu);

    final MenuItem item = menu.findItem(R.id.item_update);
    mAbButton = (TextView) item.getActionView();
    mAbButton.setOnClickListener(this);
    mAbButton.setVisibility(View.GONE);

    updateActionBar();
    return true;
  }

  private BaseDownloadAdapter getDownloadAdapter()
  {
    return (BaseDownloadAdapter) getListView().getAdapter();
  }

  @Override
  protected void onResume()
  {
    super.onResume();
    getDownloadAdapter().onResume(getListView());
  }

  @Override
  protected void onPause()
  {
    super.onPause();
    getDownloadAdapter().onPause();
  }

  @Override
  public void onBackPressed()
  {
    if (getDownloadAdapter().onBackPressed())
    {
      // scroll list view to the top
      setSelection(0);
    }
    else if (getListAdapter() instanceof DownloadedAdapter)
    {
      mMode = MODE_DISABLED;
      mDownloadedAdapter.onPause();
      mExtendedAdapter.onResume(getListView());
      setListAdapter(mExtendedAdapter);
      updateActionBar();
    }
    else
    {
      super.onBackPressed();
      // Always show map as parent
      startActivity(new Intent(this, MWMActivity.class));
    }
  }

  private void updateActionBar()
  {
    if (mAbButton == null)
      return;
    if (mMode == MODE_DISABLED)
    {
      mAbButton.setVisibility(View.GONE);
      return;
    }
    if (ActiveCountryTree.isDownloadingActive())
    {
      mMode = MODE_CANCEL_ALL;
      mAbButton.setText(getString(R.string.downloader_cancel_all));
      mAbButton.setTextColor(getResources().getColor(R.color.downloader_red));
      mAbButton.setVisibility(View.VISIBLE);
    }
    else if (ActiveCountryTree.getCountInGroup(ActiveCountryTree.GROUP_OUT_OF_DATE) > 0)
    {
      mMode = MODE_UPDATE_ALL;
      mAbButton.setText(getString(R.string.downloader_update_all));
      mAbButton.setTextColor(getResources().getColor(R.color.downloader_green));
      mAbButton.setVisibility(View.VISIBLE);
    }
    invalidateOptionsMenu();
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id)
  {
    if (getListAdapter().getItemViewType(position) == ExtendedDownloadAdapterWrapper.TYPE_EXTENDED)
    {
      setListAdapter(getDownloadedAdapter());
      mMode = MODE_NONE;
      updateActionBar();
      mExtendedAdapter.onPause();
      mDownloadedAdapter.onResume(getListView());
    }
  }

  private ListAdapter getDownloadedAdapter()
  {
    if (mDownloadedAdapter == null)
      mDownloadedAdapter = new DownloadedAdapter(this);

    return mDownloadedAdapter;
  }

  @Override
  public void onClick(View v)
  {
    switch (v.getId())
    {
    case R.id.item_update:
      if (mMode == MODE_UPDATE_ALL)
        ActiveCountryTree.updateAll();
      else
        ActiveCountryTree.cancelAll();
      updateActionBar();
      break;
    }
  }

  @Override
  public void onCountryProgressChanged(int group, int position, long[] sizes) { }

  @Override
  public void onCountryStatusChanged(int group, int position, int oldStatus, int newStatus)
  {
    updateActionBar();
  }

  @Override
  public void onCountryGroupChanged(int oldGroup, int oldPosition, int newGroup, int newPosition) { }

  @Override
  public void onCountryOptionsChanged(int group, int position, int newOptions, int requestOptions)
  {
    updateActionBar();
  }
}
{'fraction_non_alphanumeric': 0.09545454545454546, 'fraction_numerical': 0.0012396694214876034, 'mean_word_length': 3.8526315789473684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3746498', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2893, 'n_words': 564}