@Override
 protected void onDestroy() {
   super.onDestroy();
   if (textWatcher != null) {
     EditText filterText = (EditText) findViewById(R.id.search_box);
     filterText.removeTextChangedListener(textWatcher);
   }
   downloadListIndexThread.setUiActivity(null);
 }
 @Override
 protected void onResume() {
   super.onResume();
   BasicProgressAsyncTask<?, ?, ?> t = downloadListIndexThread.getCurrentRunningTask();
   updateProgress(false);
   if (t instanceof DownloadIndexesThread.DownloadIndexesAsyncTask) {
     View mainView = findViewById(R.id.MainLayout);
     if (mainView != null) {
       mainView.setKeepScreenOn(true);
     }
   }
 }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   if (isFinishing()) {
     downloadFileHelper.setInterruptDownloading(true);
   }
   if (textWatcher != null) {
     EditText filterText = (EditText) findViewById(R.id.search_box);
     filterText.removeTextChangedListener(textWatcher);
   }
   downloadListIndexThread.setUiActivity(null);
 }
 @Override
 public boolean onPrepareOptionsMenu(Menu menu) {
   super.onPrepareOptionsMenu(menu);
   menu.clear();
   menu.add(0, RELOAD_ID, 0, R.string.reload);
   if (getExpandableListAdapter() != null) {
     // item.setIcon(R.drawable.ic_menu_refresh);
     menu.add(0, SELECT_ALL_ID, 0, R.string.select_all);
     menu.add(0, DESELECT_ALL_ID, 0, R.string.deselect_all);
     menu.add(0, FILTER_EXISTING_REGIONS, 0, R.string.filter_existing_indexes);
   }
   return true;
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    settings = ((OsmandApplication) getApplication()).getSettings();
    if (downloadListIndexThread == null) {
      downloadListIndexThread = new DownloadIndexesThread(this);
    }
    setContentView(R.layout.download_index);
    indeterminateProgressBar = (ProgressBar) findViewById(R.id.IndeterminateProgressBar);
    determinateProgressBar = (ProgressBar) findViewById(R.id.DeterminateProgressBar);
    progressView = findViewById(R.id.ProgressView);
    progressMessage = (TextView) findViewById(R.id.ProgressMessage);
    progressPercent = (TextView) findViewById(R.id.ProgressPercent);
    cancel = (ImageView) findViewById(R.id.Cancel);
    int d =
        settings.isLightContent()
            ? R.drawable.a_1_navigation_cancel_small_light
            : R.drawable.a_1_navigation_cancel_small_dark;
    cancel.setImageDrawable(getResources().getDrawable(d));
    cancel.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View v) {
            makeSureUserCancelDownload();
          }
        });
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setTitle(R.string.local_index_download);
    // recreation upon rotation is prevented in manifest file
    findViewById(R.id.DownloadButton)
        .setOnClickListener(
            new View.OnClickListener() {

              @Override
              public void onClick(View v) {
                downloadFilesCheckFreeVersion();
              }
            });

    filterText = (EditText) findViewById(R.id.search_box);
    textWatcher =
        new TextWatcher() {
          @Override
          public void afterTextChanged(Editable s) {}

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            DownloadIndexAdapter adapter = ((DownloadIndexAdapter) getExpandableListAdapter());
            if (adapter != null) {
              adapter.getFilter().filter(s);
            }
          }
        };
    filterText.addTextChangedListener(textWatcher);
    final Intent intent = getIntent();
    if (intent != null && intent.getExtras() != null) {
      final String filter = intent.getExtras().getString(FILTER_KEY);
      if (filter != null) {
        filterText.setText(filter);
      }
    }
    List<IndexItem> list = new ArrayList<IndexItem>();
    downloadListIndexThread.setUiActivity(this);
    if (downloadListIndexThread.getCachedIndexFiles() != null
        && downloadListIndexThread.isDownloadedFromInternet()) {
      downloadListIndexThread.runCategorization(type);
    } else {
      downloadListIndexThread.runReloadIndexFiles();
    }
    DownloadIndexAdapter adapter = new DownloadIndexAdapter(this, list);
    setListAdapter(adapter);
    if (getMyApplication().getResourceManager().getIndexFileNames().isEmpty()) {
      boolean showedDialog = false;
      if (Build.VERSION.SDK_INT < OsmandSettings.VERSION_DEFAULTLOCATION_CHANGED) {
        SuggestExternalDirectoryDialog.showDialog(this, null, null);
      }
      if (!showedDialog) {
        showDialogOfFreeDownloadsIfNeeded();
      }
    } else {
      showDialogOfFreeDownloadsIfNeeded();
    }
    final DownloadActivityType[] downloadTypes = getDownloadTypes();
    spinnerAdapter =
        new ArrayAdapter<String>(
            getSupportActionBar().getThemedContext(),
            R.layout.sherlock_spinner_item,
            new ArrayList<String>(Arrays.asList(toString(downloadTypes))));
    spinnerAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
    getSupportActionBar()
        .setListNavigationCallbacks(
            spinnerAdapter,
            new OnNavigationListener() {

              @Override
              public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                changeType(downloadTypes[itemPosition]);
                return true;
              }
            });
    if (Build.VERSION.SDK_INT >= OsmandSettings.VERSION_DEFAULTLOCATION_CHANGED) {
      if (!settings
          .getExternalStorageDirectory()
          .getAbsolutePath()
          .equals(settings.getDefaultExternalStorageLocation())) {
        AccessibleAlertBuilder ab = new AccessibleAlertBuilder(this);
        ab.setMessage(
            getString(
                R.string.android_19_location_disabled, settings.getExternalStorageDirectory()));
        ab.setPositiveButton(
            R.string.default_buttons_yes,
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                copyFilesForAndroid19();
              }
            });
        ab.setNegativeButton(R.string.default_buttons_cancel, null);
        ab.show();
      }
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    settings = ((OsmandApplication) getApplication()).getSettings();
    if (downloadListIndexThread == null) {
      downloadListIndexThread = new DownloadIndexListThread(this);
    }
    // recreation upon rotation is prevented in manifest file
    CustomTitleBar titleBar =
        new CustomTitleBar(
            this, R.string.local_index_download, R.drawable.tab_download_screen_icon);
    setContentView(R.layout.download_index);
    titleBar.afterSetContentView();

    downloadFileHelper = new DownloadFileHelper(this);
    findViewById(R.id.DownloadButton)
        .setOnClickListener(
            new View.OnClickListener() {

              @Override
              public void onClick(View v) {
                downloadFilesCheckFreeVersion();
              }
            });

    updateLoadedFiles();

    filterText = (EditText) findViewById(R.id.search_box);
    textWatcher =
        new TextWatcher() {
          @Override
          public void afterTextChanged(Editable s) {}

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            DownloadIndexAdapter adapter = ((DownloadIndexAdapter) getExpandableListAdapter());
            if (adapter != null) {
              adapter.getFilter().filter(s);
            }
          }
        };
    filterText.addTextChangedListener(textWatcher);
    final Intent intent = getIntent();
    if (intent != null && intent.getExtras() != null) {
      final String filter = intent.getExtras().getString(FILTER_KEY);
      if (filter != null) {
        filterText.setText(filter);
      }
    }

    if (downloadListIndexThread.getCachedIndexFiles() != null) {
      setListAdapter(new DownloadIndexAdapter(downloadListIndexThread.getCachedIndexFiles()));
    } else {
      downloadIndexList();
    }
    if (Version.isFreeVersion(this) && settings.checkFreeDownloadsNumberZero()) {
      Builder msg = new AlertDialog.Builder(this);
      msg.setTitle(R.string.free_version_title);
      msg.setMessage(
          getString(R.string.free_version_message, MAXIMUM_AVAILABLE_FREE_DOWNLOADS + "", ""));
      msg.show();
    }
  }