Java tutorial
/* * </summary> * Source File : SearchFragment.java * Project : Sarigama * Owner : Kasun * </summary> * * <license> * Copyright 2016 Kasun Gunathilaka * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http:www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * </license> */ package com.kasungunathilaka.sarigama.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.RelativeLayout; import android.widget.TableLayout; import android.widget.TextView; import android.widget.Toast; import com.kasungunathilaka.sarigama.R; import com.kasungunathilaka.sarigama.adapter.ArtistAdapter; import com.kasungunathilaka.sarigama.adapter.SongAdapter; import com.kasungunathilaka.sarigama.business.URLBusiness; import com.kasungunathilaka.sarigama.domain.Artist; import com.kasungunathilaka.sarigama.domain.Song; import com.kasungunathilaka.sarigama.ui.HomeActivity; import com.kasungunathilaka.sarigama.util.PlayerQueue; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; import java.util.ArrayList; import cz.msebera.android.httpclient.Header; public class SearchFragment extends Fragment { RelativeLayout rlPb, rlRecyclerView; ArrayList<Song> songs; ArrayList<Artist> artists; RecyclerView rv; IFragmentCommunicator iFragmentCommunicator; HomeActivity activity; URLBusiness uRLBusiness; String searchText = ""; String type; PlayerQueue playerQueue; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_search, container, false); activity = (HomeActivity) getActivity(); playerQueue = PlayerQueue.getInstance(activity); rlRecyclerView = (RelativeLayout) view.findViewById(R.id.rlRecyclerView); rlPb = (RelativeLayout) view.findViewById(R.id.rlPb); uRLBusiness = new URLBusiness(activity); setHasOptionsMenu(true); rv = (RecyclerView) view.findViewById(R.id.rv); getSearchResult(); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); iFragmentCommunicator = (IFragmentCommunicator) getActivity(); } public void setSearchText(String searchText, String type) { this.searchText = searchText; this.type = type; } @Override public void onPrepareOptionsMenu(Menu menu) { MenuItem searchItem = menu.findItem(R.id.action_search); MenuItem refreshItem = menu.findItem(R.id.action_refresh); searchItem.setVisible(false); refreshItem.setVisible(false); } private void getSearchResult() { try { showProgressBar(true); AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); RequestParams requestParams = new RequestParams(); requestParams.add("query", searchText); asyncHttpClient.post(getActivity(), "http://sarigama.lk/search/search-result", requestParams, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { if (type.contentEquals(HomeActivity.TYPE_SONG)) { getSearchSong(new String(responseBody)); rv.setHasFixedSize(true); rv.setLayoutManager(new LinearLayoutManager(getActivity())); rv.setAdapter(new SongAdapter(getActivity(), songClickListener, songs)); showProgressBar(false); } else if (type.contentEquals(HomeActivity.TYPE_ARTIST)) { getSearchArtist(new String(responseBody)); rv.setHasFixedSize(true); rv.setLayoutManager(new GridLayoutManager(getActivity(), 3)); rv.setAdapter(new ArtistAdapter(getActivity(), artistClickListener, artists)); showProgressBar(false); } } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { showProgressBar(false); Bundle bundle = new Bundle(); bundle.putString(HomeActivity.FRAGMENT_NAME, "ConnectionFailFragment"); iFragmentCommunicator.fragmentData(bundle); switch (statusCode) { case 404: Toast.makeText(getActivity(), "Server not Found", Toast.LENGTH_SHORT).show(); break; case 500: Toast.makeText(getActivity(), "Server Error", Toast.LENGTH_SHORT).show(); break; } } }); } catch (Exception e) { e.printStackTrace(); } } private void getSearchSong(String body) { songs = new ArrayList<>(); int startIndex = body.indexOf("search-song-item"); int lastIndex = body.indexOf("search-heading-name artists-header"); while (startIndex >= 0 && lastIndex > startIndex) { Song song = new Song(); startIndex = startIndex + 37; int endIndex = body.indexOf(">", startIndex + 1); endIndex = endIndex - 2; song.setSongItem(body.substring(startIndex, endIndex).trim()); System.out.println(body.substring(startIndex, endIndex).trim()); startIndex = body.indexOf("s-song-title", endIndex + 1); startIndex = startIndex + 14; endIndex = body.indexOf("</label>", startIndex + 1); song.setSongTitle(body.substring(startIndex, endIndex).trim()); System.out.println(body.substring(startIndex, endIndex).trim()); startIndex = body.indexOf("s-song-artists", endIndex + 1); startIndex = startIndex + 16; endIndex = body.indexOf("</label>", startIndex + 1); song.setSongArtist(body.substring(startIndex, endIndex).trim()); System.out.println(body.substring(startIndex, endIndex).trim()); songs.add(song); startIndex = body.indexOf("search-song-item", startIndex + 1); } } private void getSearchArtist(String body) { artists = new ArrayList<>(); int startIndex = body.indexOf("search-heading-name artists-header"); startIndex = body.indexOf("search-song-item", startIndex + 1); while (startIndex >= 0) { Artist artist = new Artist(); startIndex = startIndex + 37; int endIndex = body.indexOf(">", startIndex + 1); endIndex = endIndex - 2; artist.setUrl(body.substring(startIndex, endIndex).trim()); System.out.println(body.substring(startIndex, endIndex).trim()); startIndex = body.indexOf("song-details-image s-artist-img", endIndex + 1); startIndex = body.indexOf("http://sarigama.lk/img/", startIndex + 1); endIndex = body.indexOf(">", startIndex + 1); endIndex = endIndex - 2; artist.setImage(body.substring(startIndex, endIndex).trim()); System.out.println(body.substring(startIndex, endIndex).trim()); startIndex = body.indexOf("s-song-title", endIndex + 1); startIndex = startIndex + 14; endIndex = body.indexOf("</label>", startIndex + 1); artist.setName(body.substring(startIndex, endIndex).trim()); System.out.println(body.substring(startIndex, endIndex).trim()); artists.add(artist); startIndex = body.indexOf("search-song-item", startIndex + 1); } } private void showProgressBar(Boolean show) { rlPb.setVisibility(show ? View.VISIBLE : View.GONE); rlRecyclerView.setVisibility(show ? View.GONE : View.VISIBLE); } SongAdapter.OnItemClickListener songClickListener = new SongAdapter.OnItemClickListener() { @Override public void onItemClick(Song Song, int position) { playerQueue.clearQueue(); activity.getPlayerService().stopPlayerQueue(); playerQueue.addSongs(songs); playerQueue.setCurrentPosition(position); Bundle bundle = new Bundle(); bundle.putString(HomeActivity.FRAGMENT_NAME, "PlayerFragment"); bundle.putString(HomeActivity.PLAYER_FRAGMENT_LAUNCH_MODE, HomeActivity.PLAYER_FRAGMENT_LAUNCH_MODE_NEW_QUEUE); iFragmentCommunicator.fragmentData(bundle); } }; ArtistAdapter.OnItemClickListener artistClickListener = new ArtistAdapter.OnItemClickListener() { @Override public void onItemClick(Artist artist) { Bundle bundle = new Bundle(); bundle.putString(HomeActivity.FRAGMENT_NAME, "ArtistDetailFragment"); bundle.putString(ArtistDetailFragment.ARTIST_URL, artist.getUrl()); bundle.putString(ArtistDetailFragment.IMG_URL, artist.getImage()); bundle.putString(ArtistDetailFragment.ARTIST_NAME, artist.getName()); bundle.putString(ArtistDetailFragment.SONG_COUNT, artist.getSongCount()); iFragmentCommunicator.fragmentData(bundle); } }; }