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.animation.Animator; import android.animation.ObjectAnimator; import android.app.Activity; import android.content.Intent; import android.graphics.PorterDuff; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.MenuItemCompat; import android.support.v4.view.ViewPager; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.animation.AccelerateDecelerateInterpolator; import android.widget.EditText; import android.widget.ImageView; import java.lang.ref.WeakReference; import fr.shywim.antoinedaniel.R; import fr.shywim.antoinedaniel.service.DownloadService; import fr.shywim.antoinedaniel.service.SoundService; import fr.shywim.antoinedaniel.ui.MainActivity; import fr.shywim.antoinedaniel.utils.FragmentUtils; import fr.shywim.antoinedaniel.utils.LogUtils; import fr.shywim.antoinedaniel.utils.PrefUtils; import fr.shywim.antoinedaniel.utils.SoundUtils; import fr.shywim.antoinedaniel.utils.UiUtils; public class SoundPagerFragment extends Fragment implements SearchView.OnQueryTextListener, FragmentUtils.ScrollListener { private static final String TAG = LogUtils.makeLogTag(SoundPagerFragment.class); public static final String ARG_SOUNDS_GROUP = "sounds_group"; private View mFab; private ViewPager mPager; private boolean mFabIsOpen = false; private boolean mFabAnimUp = false; private boolean mFabAnimDown = false; private float mFabOriginalPosition = -1; private float mFabHiddenPosition = -1; private ObjectAnimator mFabAnim; private Activity mActivity; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_soundpager, container, false); UiUtils.setStatusBarBackground(root, getResources()); mPager = (ViewPager) root.findViewById(R.id.pager); mPager.setAdapter(new CollectionPagerAdapter(getChildFragmentManager())); Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar_actionbar); toolbar.setTitle(R.string.drawer_sounds); ((FragmentsCallbacks) mActivity).setActionBarToolbar(toolbar); mFab = root.findViewById(R.id.fab_settings); setupFabButton(); return root; } @Override public void onAttach(Activity activity) { super.onAttach(activity); mActivity = activity; FragmentsCallbacks callbacks = (FragmentsCallbacks) activity; callbacks.onFragmentAttached(this); } @Override public void onStart() { super.onStart(); DownloadService.startActionFetchItems(mActivity, DownloadService.SOUNDS); DownloadService.startActionFetchItems(mActivity, DownloadService.VIDEOS); DownloadService.startActionFetchItems(mActivity, DownloadService.MUSICS); } //********************************************************************************************** //* Options Menu * //********************************************************************************************** @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { menu.removeGroup(R.id.sound_menu); inflater.inflate(R.menu.sound_options_menu, menu); // find the searchView and handle search manually inside fragment MenuItem searchItem = menu.findItem(R.id.search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); if (searchView != null) { searchView.setOnQueryTextListener(this); MainActivity act = (MainActivity) getActivity(); if (act.searchQuery != null) { searchItem.expandActionView(); ((EditText) searchView.findViewById(R.id.search_src_text)).setText(act.searchQuery); switchFragmentsIfNecessary(true, act.searchQuery); } } super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_stop: Intent intent = new Intent(mActivity, SoundService.class); mActivity.stopService(intent); return true; default: return super.onOptionsItemSelected(item); } } private void setupFabButton() { View root = (View) mFab.getParent(); View fabMiniLoop = root.findViewById(R.id.fab_mini_loop); View fabMiniDownloaded = root.findViewById(R.id.fab_mini_downloaded); mFab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mFabIsOpen) { closeFab(v); } else { openFab(v); } } }); fabMiniDownloaded.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PrefUtils.setShowOnlyDownloaded(mActivity, !PrefUtils.getShowOnlyDownloaded(mActivity)); if (PrefUtils.getShowOnlyDownloaded(mActivity)) { setFabMiniChecked(v); } else { setFabMiniUnchecked(v); } CollectionPagerAdapter adapter = (CollectionPagerAdapter) mPager.getAdapter(); adapter.restartLoaderEachFragment(); } }); fabMiniLoop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SoundUtils.switchLooping(); if (SoundUtils.isLoopingSet()) { setFabMiniChecked(v); } else { setFabMiniUnchecked(v); } } }); } private void setFabMiniChecked(View view) { view.getBackground().mutate().setColorFilter(getResources().getColor(R.color.primary_lighter), PorterDuff.Mode.SRC_ATOP); } private void setFabMiniUnchecked(View view) { view.getBackground().mutate().clearColorFilter(); } private void openFab(View fab) { if (mFabIsOpen) return; mFabIsOpen = true; ImageView icSettings = (ImageView) fab.findViewById(R.id.fab_settings_icon); ImageView icClose = (ImageView) fab.findViewById(R.id.fab_settings_icon_close); View root = (View) fab.getParent(); View fabMiniLoop = root.findViewById(R.id.fab_mini_loop); View fabMiniLoopHint = root.findViewById(R.id.fab_mini_loop_hint); View fabMiniDownloaded = root.findViewById(R.id.fab_mini_downloaded); View fabMiniDownloadedHint = root.findViewById(R.id.fab_mini_downloaded_hint); icSettings.animate().rotation(360).alpha(0).setDuration(250); icClose.animate().rotation(360).alpha(1).setDuration(250); fabMiniLoop.clearAnimation(); ((View) fabMiniLoop.getParent()).setVisibility(View.VISIBLE); fabMiniLoop.animate().scaleX(1).scaleY(1).setListener(null).setDuration(150); fabMiniLoopHint.animate().alpha(1).setDuration(150); fabMiniDownloaded.clearAnimation(); ((View) fabMiniDownloaded.getParent()).setVisibility(View.VISIBLE); fabMiniDownloaded.animate().scaleX(1).scaleY(1).setListener(null).setStartDelay(100).setDuration(150); fabMiniDownloadedHint.animate().alpha(1).setStartDelay(100).setDuration(150); if (PrefUtils.getShowOnlyDownloaded(mActivity)) { setFabMiniChecked(fabMiniDownloaded); } else { setFabMiniUnchecked(fabMiniDownloaded); } if (SoundUtils.isLoopingSet()) { setFabMiniChecked(fabMiniLoop); } else { setFabMiniUnchecked(fabMiniLoop); } } private void closeFab(View fab) { if (!mFabIsOpen) return; mFabIsOpen = false; ImageView icSettings = (ImageView) fab.findViewById(R.id.fab_settings_icon); ImageView icClose = (ImageView) fab.findViewById(R.id.fab_settings_icon_close); View root = (View) fab.getParent(); final View fabMiniLoop = root.findViewById(R.id.fab_mini_loop); View fabMiniLoopHint = root.findViewById(R.id.fab_mini_loop_hint); final View fabMiniDownloaded = root.findViewById(R.id.fab_mini_downloaded); View fabMiniDownloadedHint = root.findViewById(R.id.fab_mini_downloaded_hint); icSettings.animate().rotation(0).alpha(1).setDuration(250); icClose.animate().rotation(0).alpha(0).setDuration(250); fabMiniLoop.animate().scaleX(0).scaleY(0).setListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { ((View) fabMiniLoop.getParent()).setVisibility(View.GONE); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }).setDuration(150); fabMiniLoopHint.animate().alpha(0).setDuration(150); fabMiniDownloaded.animate().scaleX(0).scaleY(0).setListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { ((View) fabMiniDownloaded.getParent()).setVisibility(View.GONE); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }).setDuration(150); fabMiniDownloadedHint.animate().alpha(0).setDuration(150); } @Override public boolean onQueryTextSubmit(String s) { return false; } @Override public boolean onQueryTextChange(String s) { if (s != null && !s.isEmpty()) { switchFragmentsIfNecessary(true, s); } else { switchFragmentsIfNecessary(false, null); } return true; } private void switchFragmentsIfNecessary(boolean filter, String constraint) { final String FRAGMENT_TAG = "filter_fragment"; FragmentManager fm = getFragmentManager(); View root = getView(); if (root != null) { ViewGroup container = (ViewGroup) root.findViewById(R.id.filter_container); Fragment fragment = fm.findFragmentByTag(FRAGMENT_TAG); if (filter) { if (fragment == null) { fragment = SoundFragment.newInstance(MainActivity.LoaderID.SOUND_FILTER); } if (!fragment.isAdded()) { FragmentTransaction ft = fm.beginTransaction(); ft.add(container.getId(), fragment, FRAGMENT_TAG); ft.addToBackStack(null); ft.commit(); } sendFilterToFragment((FilterListener) fragment, constraint); } else { if (fragment != null) { FragmentTransaction ft = fm.beginTransaction(); ft.remove(fragment); ft.commit(); } } } } private void sendFilterToFragment(FilterListener listener, String constraint) { listener.filter(constraint); } @Override public void onScrollUp() { if (mFab != null) { if (mFabIsOpen) { closeFab(mFab); } if (!mFabAnimUp) { if (mFabOriginalPosition == -1) { mFabOriginalPosition = mFab.getY(); mFabHiddenPosition = mFabOriginalPosition + mActivity.getResources().getDimension(R.dimen.fab_height) + mActivity.getResources().getDimension(R.dimen.fab_margin); } mFabAnimUp = true; mFabAnimDown = false; if (mFabAnim != null) { mFabAnim.cancel(); mFab.clearAnimation(); } mFabAnim = ObjectAnimator.ofFloat(mFab, "Y", mFabOriginalPosition); mFabAnim.setInterpolator(new AccelerateDecelerateInterpolator()); mFabAnim.start(); } } } @Override public void onScrollDown() { if (mFab != null) { if (mFabIsOpen) { closeFab(mFab); } if (!mFabAnimDown) { if (mFabOriginalPosition == -1) { mFabOriginalPosition = mFab.getY(); mFabHiddenPosition = mFabOriginalPosition + mActivity.getResources().getDimension(R.dimen.fab_height) + mActivity.getResources().getDimension(R.dimen.fab_margin); } mFabAnimDown = true; mFabAnimUp = false; if (mFabAnim != null) { mFabAnim.cancel(); mFab.clearAnimation(); } mFabAnim = ObjectAnimator.ofFloat(mFab, "Y", mFabHiddenPosition); mFabAnim.setInterpolator(new AccelerateDecelerateInterpolator()); mFabAnim.start(); } } } public class CollectionPagerAdapter extends FragmentStatePagerAdapter { private final String[] PAGER_TITLES; private SparseArray<WeakReference<SoundFragment>> fragments = new SparseArray<>(); FragmentManager fragmentManager; public CollectionPagerAdapter(FragmentManager fm) { super(fm); fragmentManager = fm; PAGER_TITLES = getResources().getStringArray(R.array.pager_sounds_titles); } @Override public Fragment getItem(int i) { Fragment fragment = null; switch (i) { case 0: fragment = SoundFragment.newInstance(MainActivity.LoaderID.SOUND_PLAYER); break; case 1: fragment = SoundFragment.newInstance(MainActivity.LoaderID.SOUND_BOSSES); break; case 2: fragment = SoundFragment.newInstance(MainActivity.LoaderID.SOUND_SALUTATIONS); break; } return fragment; } @Override public int getCount() { return PAGER_TITLES.length; } @Override public Object instantiateItem(ViewGroup container, int position) { SoundFragment fragment = (SoundFragment) super.instantiateItem(container, position); fragments.put(position, new WeakReference<>(fragment)); return fragment; } @Override public void destroyItem(ViewGroup container, int position, Object object) { fragments.remove(position); super.destroyItem(container, position, object); } @Override public CharSequence getPageTitle(int position) { CharSequence cs; String title = PAGER_TITLES[position]; if (title != null) cs = title; else cs = ""; return cs; } public void restartLoaderEachFragment() { for (int i = 0; i < fragments.size(); i++) { fragments.valueAt(i).get().restartLoader(); } } } public interface FilterListener { public void filter(String constraint); } }