Java tutorial
/* * Copyright (C) 2015 Naman Dwivedi * * Licensed under the GNU General Public License v3 * * This is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. */ package com.rohan.app.fragments; import android.Manifest; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.Html; 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 com.cleveroad.audiovisualization.AudioVisualization; import com.cleveroad.audiovisualization.DbmHandler; import com.cleveroad.audiovisualization.GLAudioVisualizationView; import com.cleveroad.audiovisualization.VisualizerDbmHandler; import com.melnykov.fab.FloatingActionButton; import com.rohan.app.MusicPlayer; import com.rohan.app.R; import com.rohan.app.activities.BaseActivity; import com.rohan.app.adapters.SongsListAdapter; import com.rohan.app.dataloaders.SongLoader; import com.rohan.app.listeners.MusicStateListener; import com.rohan.app.models.Song; import com.rohan.app.utils.PreferencesUtility; import com.rohan.app.utils.SortOrder; import com.rohan.app.widgets.DividerItemDecoration; import com.rohan.app.widgets.FastScroller; import java.util.List; public class SongsFragment extends Fragment implements MusicStateListener { private static final int REQUEST_CODE = 1; private boolean shouldOpenFragment; private SongsListAdapter mAdapter; private RecyclerView recyclerView; private PreferencesUtility mPreferences; private AudioVisualization audioVisualization; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPreferences = PreferencesUtility.getInstance(getActivity()); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_recyclerview, container, false); /*if (savedInstanceState == null) { if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.RECORD_AUDIO) || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.MODIFY_AUDIO_SETTINGS)) { AlertDialog.OnClickListener onClickListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { requestPermissions(); } else if (which == DialogInterface.BUTTON_NEGATIVE) { //permissionsNotGranted(); } } }; new AlertDialog.Builder(getActivity()) .setTitle(getString(R.string.title_permissions)) .setMessage(Html.fromHtml(getString(R.string.message_permissions))) .setPositiveButton(getString(R.string.btn_next), onClickListener) .setNegativeButton(getString(R.string.btn_cancel), onClickListener) .show(); } else { requestPermissions(); } }*/ recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab); FastScroller fastScroller = (FastScroller) rootView.findViewById(R.id.fastscroller); fastScroller.setRecyclerView(recyclerView); audioVisualization = (AudioVisualization) rootView.findViewById(R.id.visualizer_view); fab.attachToRecyclerView(recyclerView); fab.setColorNormal(getResources().getColor(R.color.colorAccent)); fab.setColorPressed(getResources().getColor(R.color.widget_pause)); fab.setColorRipple(getResources().getColor(R.color.window_background)); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { MusicPlayer.shuffleAll(getActivity()); } }, 80); } }); new loadSongs().execute(""); ((BaseActivity) getActivity()).setMusicStateListenerListener(this); /*if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.MODIFY_AUDIO_SETTINGS) == PackageManager.PERMISSION_GRANTED) { requestPermissions(); }*/ return rootView; } /*@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_CODE) { boolean bothGranted = true; for (int i = 0; i < permissions.length; i++) { if (Manifest.permission.RECORD_AUDIO.equals(permissions[i]) || Manifest.permission.MODIFY_AUDIO_SETTINGS.equals(permissions[i])) { bothGranted &= grantResults[i] == PackageManager.PERMISSION_GRANTED; } } if (bothGranted) { shouldOpenFragment = true; } else { //permissionsNotGranted(); } } } private void requestPermissions() { ActivityCompat.requestPermissions( getActivity(), new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.MODIFY_AUDIO_SETTINGS}, REQUEST_CODE ); }*/ public View audioVisualization() { return new GLAudioVisualizationView.Builder(getContext()).setLayersCount(4) .setBackgroundColorRes(R.color.av_color_bg).setLayerColors(R.array.av_colors).build(); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //audioVisualization = (AudioVisualization) view; //audioVisualization.linkTo(DbmHandler.Factory.newVisualizerHandler(getContext(), 0)); VisualizerDbmHandler visualizerHandler = DbmHandler.Factory.newVisualizerHandler(getContext(), 0); audioVisualization.linkTo(visualizerHandler); } @Override public void onResume() { super.onResume(); audioVisualization.onResume(); } @Override public void onPause() { audioVisualization.onPause(); super.onPause(); } @Override public void onDestroyView() { audioVisualization.release(); super.onDestroyView(); } public void restartLoader() { } public void onPlaylistChanged() { } public void onMetaChanged() { if (mAdapter != null) mAdapter.notifyDataSetChanged(); } private void reloadAdapter() { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(final Void... unused) { List<Song> songList = SongLoader.getAllSongs(getActivity()); mAdapter.updateDataSet(songList); return null; } @Override protected void onPostExecute(Void aVoid) { mAdapter.notifyDataSetChanged(); } }.execute(); } @Override public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.song_sort_by, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_sort_by_az: mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_A_Z); reloadAdapter(); return true; case R.id.menu_sort_by_za: mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_Z_A); reloadAdapter(); return true; case R.id.menu_sort_by_artist: mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_ARTIST); reloadAdapter(); return true; case R.id.menu_sort_by_album: mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_ALBUM); reloadAdapter(); return true; case R.id.menu_sort_by_year: mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_YEAR); reloadAdapter(); return true; case R.id.menu_sort_by_duration: mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_DURATION); reloadAdapter(); return true; } return super.onOptionsItemSelected(item); } private class loadSongs extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { if (getActivity() != null) mAdapter = new SongsListAdapter((AppCompatActivity) getActivity(), SongLoader.getAllSongs(getActivity()), false); return "Executed"; } @Override protected void onPostExecute(String result) { recyclerView.setAdapter(mAdapter); if (getActivity() != null) recyclerView.addItemDecoration( new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST)); } @Override protected void onPreExecute() { } } }