Search in sources :

Example 1 with Compat

use of com.ichi2.compat.Compat in project AnkiChinaAndroid by ankichinateam.

the class CardBrowser method onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    Timber.d("onCreateOptionsMenu()");
    mActionBarMenu = menu;
    if (!mInMultiSelectMode) {
        // restore drawer click listener and icon
        // restoreDrawerIcon();
        getMenuInflater().inflate(R.menu.card_browser, menu);
        mSaveSearchItem = menu.findItem(R.id.action_save_search);
        // the searchview's query always starts empty.
        mSaveSearchItem.setVisible(false);
        mMySearchesItem = menu.findItem(R.id.action_list_my_searches);
        JSONObject savedFiltersObj = getCol().getConf().optJSONObject("savedFilters");
        mMySearchesItem.setVisible(savedFiltersObj != null && savedFiltersObj.length() > 0);
        mSearchItem = menu.findItem(R.id.action_search);
        mSearchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // SearchView doesn't support empty queries so we always reset the search when collapsing
                mSearchTerms = "";
                mSearchView.setQuery(mSearchTerms, false);
                searchCards();
                // invalidate options menu so that disappeared icons would appear again
                supportInvalidateOptionsMenu();
                return true;
            }
        });
        mSearchView = (SearchView) mSearchItem.getActionView();
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextChange(String newText) {
                mSaveSearchItem.setVisible(!TextUtils.isEmpty(newText));
                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                onSearch();
                mSearchView.clearFocus();
                return true;
            }
        });
        mSearchView.setOnSearchClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Provide SearchView with the previous search terms
                mSearchView.setQuery(mSearchTerms, false);
            }
        });
        // Fixes #6500 - keep the search consistent
        if (!TextUtils.isEmpty(mSearchTerms)) {
            mSearchItem.expandActionView();
            mSearchView.setQuery(mSearchTerms, false);
        }
    } else {
        // multi-select mode
        getMenuInflater().inflate(R.menu.card_browser_multiselect, menu);
    // showBackIcon();
    }
    if (mActionBarMenu != null && mActionBarMenu.findItem(R.id.action_undo) != null) {
        MenuItem undo = mActionBarMenu.findItem(R.id.action_undo);
        undo.setVisible(getCol().undoAvailable());
        undo.setTitle(getResources().getString(R.string.studyoptions_congrats_undo, getCol().undoName(getResources())));
    }
    // Maybe we were called from ACTION_PROCESS_TEXT.
    // In that case we already fill in the search.
    Intent intent = getIntent();
    Compat compat = CompatHelper.getCompat();
    if (intent.getAction() == compat.ACTION_PROCESS_TEXT) {
        CharSequence search = intent.getCharSequenceExtra(compat.EXTRA_PROCESS_TEXT);
        if (search != null && search.length() != 0) {
            Timber.i("CardBrowser :: Called with search intent: %s", search.toString());
            mSearchView.setQuery(search, true);
            intent.setAction(Intent.ACTION_DEFAULT);
        }
    }
    mPreviewItem = menu.findItem(R.id.action_preview);
    onSelectionChanged();
    updatePreviewMenuItem();
    return super.onCreateOptionsMenu(menu);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) SearchView(androidx.appcompat.widget.SearchView) Compat(com.ichi2.compat.Compat) MenuItem(android.view.MenuItem) Intent(android.content.Intent) View(android.view.View) AdapterView(android.widget.AdapterView) SearchView(androidx.appcompat.widget.SearchView) TextView(android.widget.TextView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView)

Example 2 with Compat

use of com.ichi2.compat.Compat in project AnkiChinaAndroid by ankichinateam.

the class NotificationChannels method setup.

/**
 * Create or update all the notification channels for the app
 *
 * TODO should be called in response to {@link android.content.Intent#ACTION_LOCALE_CHANGED}
 * @param context the context for access to localized strings for channel names
 */
public static void setup(Context context) {
    Resources res = context.getResources();
    Compat compat = CompatHelper.getCompat();
    for (Channel channel : Channel.values()) {
        compat.setupNotificationChannel(context, getId(channel), getName(channel, res));
    }
}
Also used : Compat(com.ichi2.compat.Compat) Resources(android.content.res.Resources)

Example 3 with Compat

use of com.ichi2.compat.Compat in project Anki-Android by ankidroid.

the class CardBrowser method onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    Timber.d("onCreateOptionsMenu()");
    mActionBarMenu = menu;
    if (!mInMultiSelectMode) {
        // restore drawer click listener and icon
        restoreDrawerIcon();
        getMenuInflater().inflate(R.menu.card_browser, menu);
        mSaveSearchItem = menu.findItem(R.id.action_save_search);
        // the searchview's query always starts empty.
        mSaveSearchItem.setVisible(false);
        mMySearchesItem = menu.findItem(R.id.action_list_my_searches);
        JSONObject savedFiltersObj = getCol().get_config("savedFilters", (JSONObject) null);
        mMySearchesItem.setVisible(savedFiltersObj != null && savedFiltersObj.length() > 0);
        mSearchItem = menu.findItem(R.id.action_search);
        mSearchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // SearchView doesn't support empty queries so we always reset the search when collapsing
                mSearchTerms = "";
                mSearchView.setQuery(mSearchTerms, false);
                searchCards();
                // invalidate options menu so that disappeared icons would appear again
                supportInvalidateOptionsMenu();
                mTempSearchQuery = null;
                return true;
            }
        });
        mSearchView = (CardBrowserSearchView) mSearchItem.getActionView();
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextChange(String newText) {
                if (mSearchView.shouldIgnoreValueChange()) {
                    return true;
                }
                mSaveSearchItem.setVisible(!TextUtils.isEmpty(newText));
                mTempSearchQuery = newText;
                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                onSearch();
                mSearchView.clearFocus();
                return true;
            }
        });
        // Fixes #9010 - consistent search after drawer change calls supportInvalidateOptionsMenu (mTempSearchQuery)
        if (!TextUtils.isEmpty(mTempSearchQuery) || !TextUtils.isEmpty(mSearchTerms)) {
            // This calls mSearchView.setOnSearchClickListener
            mSearchItem.expandActionView();
            String toUse = !TextUtils.isEmpty(mTempSearchQuery) ? mTempSearchQuery : mSearchTerms;
            mSearchView.setQuery(toUse, false);
        }
        mSearchView.setOnSearchClickListener(v -> {
            // Provide SearchView with the previous search terms
            mSearchView.setQuery(mSearchTerms, false);
        });
    } else {
        // multi-select mode
        getMenuInflater().inflate(R.menu.card_browser_multiselect, menu);
        showBackIcon();
    }
    if (mActionBarMenu != null && mActionBarMenu.findItem(R.id.action_undo) != null) {
        MenuItem undo = mActionBarMenu.findItem(R.id.action_undo);
        undo.setVisible(getCol().undoAvailable());
        undo.setTitle(getResources().getString(R.string.studyoptions_congrats_undo, getCol().undoName(getResources())));
    }
    // Maybe we were called from ACTION_PROCESS_TEXT.
    // In that case we already fill in the search.
    Intent intent = getIntent();
    Compat compat = CompatHelper.getCompat();
    if (compat.ACTION_PROCESS_TEXT.equals(intent.getAction())) {
        CharSequence search = intent.getCharSequenceExtra(compat.EXTRA_PROCESS_TEXT);
        if (search != null && search.length() != 0) {
            Timber.i("CardBrowser :: Called with search intent: %s", search.toString());
            mSearchView.setQuery(search, true);
            intent.setAction(Intent.ACTION_DEFAULT);
        }
    }
    mPreviewItem = menu.findItem(R.id.action_preview);
    onSelectionChanged();
    updatePreviewMenuItem();
    return super.onCreateOptionsMenu(menu);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) SearchView(androidx.appcompat.widget.SearchView) CardBrowserSearchView(com.ichi2.ui.CardBrowserSearchView) LanguageUtil.getLocaleCompat(com.ichi2.utils.LanguageUtil.getLocaleCompat) Compat(com.ichi2.compat.Compat) MenuItem(android.view.MenuItem) Intent(android.content.Intent)

Aggregations

Compat (com.ichi2.compat.Compat)3 Intent (android.content.Intent)2 MenuItem (android.view.MenuItem)2 SearchView (androidx.appcompat.widget.SearchView)2 JSONObject (com.ichi2.utils.JSONObject)2 Resources (android.content.res.Resources)1 View (android.view.View)1 AbsListView (android.widget.AbsListView)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 CardBrowserSearchView (com.ichi2.ui.CardBrowserSearchView)1 LanguageUtil.getLocaleCompat (com.ichi2.utils.LanguageUtil.getLocaleCompat)1