Java tutorial
/* * Copyright (c) 2015 Matthieu Harl * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package fr.shywim.antoinedaniel.ui.fragment; import android.app.Activity; import android.content.Context; import android.content.res.Configuration; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import fr.shywim.antoinedaniel.R; import fr.shywim.antoinedaniel.provider.ProviderConstants; import fr.shywim.antoinedaniel.provider.ProviderContract; import fr.shywim.antoinedaniel.ui.MainActivity; import fr.shywim.antoinedaniel.ui.adapter.CursorRecyclerAdapter; import fr.shywim.antoinedaniel.ui.adapter.SoundAdapter; import fr.shywim.antoinedaniel.utils.FragmentUtils; import fr.shywim.antoinedaniel.utils.LogUtils; import fr.shywim.antoinedaniel.utils.PrefUtils; public class SoundFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, SoundPagerFragment.FilterListener { private static final String TAG = LogUtils.makeLogTag(SoundFragment.class); public static final String ARG_CONTENT = "mContentId"; public static final String ARG_FILTER = "filter"; public static final String SOUND_TO_PLAY = "fr.shywim.antoinedaniel.FRAGMENT_SOUND_TO_PLAY"; public static final String LOOP = "fr.shywim.antoinedaniel.FRAGMENT_LOOP"; int mContentId; Context mContext; RecyclerView mSoundList; /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param content fragment for player's sound or bosses' or salutations. * @return A new instance of fragment SoundFragment. */ public static SoundFragment newInstance(int content) { SoundFragment fragment = new SoundFragment(); Bundle args = new Bundle(); args.putInt(ARG_CONTENT, content); fragment.setArguments(args); return fragment; } public SoundFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle args = getArguments(); if (args != null) { this.mContentId = getArguments().getInt(ARG_CONTENT); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); mContext = activity; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_sounds, container, false); final FragmentUtils.ScrollListener scrollListener = FragmentUtils.getParent(this, FragmentUtils.ScrollListener.class); mSoundList = (RecyclerView) rootView.findViewById(R.id.sound_list); mSoundList.setLayoutManager( new StaggeredGridLayoutManager(getColumnNumber(), StaggeredGridLayoutManager.VERTICAL)); mSoundList.setAdapter(new SoundAdapter(mContext, null)); mSoundList.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { // Show or hide the FAB depending on the scroll orientation if (dy > 10) { scrollListener.onScrollDown(); } else if (dy < -10) { scrollListener.onScrollUp(); } } }); return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (mContentId != MainActivity.LoaderID.SOUND_FILTER) { Loader<Cursor> loader = getLoaderManager().getLoader(mContentId); if (loader == null) getLoaderManager().initLoader(mContentId, null, this); else getLoaderManager().restartLoader(mContentId, null, this); } } private int getColumnNumber() { Configuration configuration = getResources().getConfiguration(); return configuration.screenWidthDp / 100; } @Override public void filter(String constraint) { if (!isAdded()) { return; } StringBuilder sb = new StringBuilder(constraint); sb.insert(0, '%'); sb.append('%'); Bundle args = new Bundle(); args.putString(ARG_FILTER, sb.toString()); getLoaderManager().restartLoader(mContentId, args, this); } public void restartLoader() { getLoaderManager().restartLoader(mContentId, null, this); } //********************************************************************************************** //* LoaderManager * //********************************************************************************************** @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String where = null; String[] whereArgs = null; if (args != null) { if (args.containsKey(ARG_FILTER)) { where = ProviderContract.SoundEntry.TABLE_NAME + '.' + ProviderContract.SoundEntry.COLUMN_SOUND_NAME + " LIKE ? OR " + ProviderContract.SoundEntry.TABLE_NAME + '.' + ProviderContract.SoundEntry.COLUMN_IMAGE_NAME + " LIKE ? OR " + ProviderContract.SoundEntry.TABLE_NAME + '.' + ProviderContract.SoundEntry.COLUMN_DESC + " LIKE ? OR " + ProviderContract.VideosEntry.TABLE_NAME + '.' + ProviderContract.VideosEntry.COLUMN_NAME_DESC + " LIKE ?"; whereArgs = new String[] { args.getString(ARG_FILTER), args.getString(ARG_FILTER), args.getString(ARG_FILTER), args.getString(ARG_FILTER) }; } } if (PrefUtils.getShowOnlyDownloaded(mContext)) { String append = ProviderContract.SoundEntry.TABLE_NAME + '.' + ProviderContract.SoundEntry.COLUMN_DOWNLOADED + "=1"; if (where != null) { where = where + append; } else { where = append; } } switch (id) { case MainActivity.LoaderID.SOUND_FILTER: return new CursorLoader(mContext, ProviderConstants.SOUND_URI, null, where, whereArgs, null); case MainActivity.LoaderID.SOUND_PLAYER: return new CursorLoader(mContext, ProviderConstants.SOUND_PAGE_P_URI, null, where, whereArgs, null); case MainActivity.LoaderID.SOUND_BOSSES: return new CursorLoader(mContext, ProviderConstants.SOUND_PAGE_B_URI, null, where, whereArgs, null); case MainActivity.LoaderID.SOUND_SALUTATIONS: return new CursorLoader(mContext, ProviderConstants.SOUND_PAGE_S_URI, null, where, whereArgs, null); } return null; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { if (cursor.isClosed()) { getLoaderManager().restartLoader(mContentId, null, this); return; } if (loader.getId() == MainActivity.LoaderID.SOUND_FILTER) { cursor.setNotificationUri(mContext.getContentResolver(), ProviderConstants.PLAYLIST_URI); } ((CursorRecyclerAdapter) mSoundList.getAdapter()).swapCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> loader) { ((CursorRecyclerAdapter) mSoundList.getAdapter()).swapCursor(null); } }