Java tutorial
/* * Copyright (C) 2011 Erdene-Ochir Tuguldur (https://github.com/tugstugi) * * 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. */ package com.mongolduu.android.ng; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; import com.mongolduu.android.ng.R; import com.mongolduu.android.ng.db.DatabaseHelper; import com.mongolduu.android.ng.db.SongInfo; import com.mongolduu.android.ng.misc.HttpConnector; import com.mongolduu.android.ng.misc.SongInfoListAdapter; import com.mongolduu.android.ng.misc.TextAndProgressBarUtils; import com.mongolduu.android.ng.misc.Utils; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnCreateContextMenuListener; import android.widget.AdapterView; import android.widget.HeaderViewListAdapter; import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class ChartFragment extends Fragment { private class FetchChartListTask extends AsyncTask<Void, Void, List<SongInfo>> { private boolean newFetch; public FetchChartListTask(boolean newFetch) { this.newFetch = newFetch; } @Override protected void onPreExecute() { super.onPreExecute(); if (listview != null) { if (newFetch) { getSongInfoListAdapter().clear(); } listview.invalidateViews(); TextAndProgressBarUtils.configureTextAndProgressBar(listview, null, false, true); TextAndProgressBarUtils.showTextAndProgressBar(listview); } } @Override protected List<SongInfo> doInBackground(Void... params) { return HttpConnector.fetchChartList(chartType); } @Override protected void onPostExecute(List<SongInfo> result) { if (!isCancelled() && listview != null) { if (result != null) { try { Utils.checkIfSongSavedOnDevice(getHelper(), result, Utils.isExternalStorageMounted()); } catch (SQLException e) { Log.e(IMongolduuConstants.LOG_TAG, "exception", e); } getSongInfoListAdapter().addSongs(result); listview.invalidateViews(); TextAndProgressBarUtils.hideTextAndProgressBar(listview); } else { TextAndProgressBarUtils.configureTextAndProgressBar(listview, getString(R.string.network_problem), false, false); } } currentTask = null; super.onPostExecute(result); } } protected ListView listview; protected View textandprogressbar = null; public int fragmentIndex = -1; public String chartType = null; protected FetchChartListTask currentTask = null; public static final String FRAGMENT_INDEX_KEY = "fragmentIndex"; public static final String CHART_TYPE_KEY = "fragmentIndex"; public static ChartFragment newInstance(int fragmentIndex, String chartType) { ChartFragment f = new ChartFragment(); f.fragmentIndex = fragmentIndex + 1; f.chartType = chartType; return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup root = (ViewGroup) inflater.inflate(R.layout.list, null); listview = (ListView) root.findViewById(R.id.list); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { SongInfo songinfo = (SongInfo) parent.getAdapter().getItem(position); if (songinfo.isSavedOnDevice) { //Toast.makeText(SearchActivity.this, R.string.toast_message_already_downloaded, Toast.LENGTH_SHORT).show(); playSong(songinfo); } else { downloadSong(songinfo); } } }); listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { SongInfo songinfo = (SongInfo) listview.getAdapter() .getItem(((AdapterView.AdapterContextMenuInfo) menuInfo).position); if (songinfo.isSavedOnDevice) { menu.add(fragmentIndex, R.id.context_menu_play_song, 0, R.string.context_menu_play_song); menu.add(fragmentIndex, R.id.context_menu_set_as_ringtone, 0, R.string.context_menu_set_as_ringtone); menu.add(fragmentIndex, R.id.context_menu_remove_song_from_device, 0, R.string.context_menu_remove_song_from_device); } else { menu.add(fragmentIndex, R.id.context_menu_download, 0, R.string.context_menu_download); } } }); textandprogressbar = (ViewGroup) inflater.inflate(R.layout.text_and_progressbar, listview, false); listview.addFooterView(textandprogressbar, null, false); TextAndProgressBarUtils.initializeTextAndProgressBar(listview, textandprogressbar); TextAndProgressBarUtils.hideTextAndProgressBar(listview); listview.setAdapter(new SongInfoListAdapter(getActivity(), new LinkedList<SongInfo>(), true)); fetchSongs(true); return root; } protected SongInfoListAdapter getSongInfoListAdapter() { return (SongInfoListAdapter) ((HeaderViewListAdapter) listview.getAdapter()).getWrappedAdapter(); } public void fetchSongs(boolean firstFetch) { if (currentTask != null) { currentTask.cancel(true); } currentTask = new FetchChartListTask(firstFetch); currentTask.execute(); } public void checkIfSongSavedOnDeviceAndUpdateListView() { if (listview != null) { try { Utils.checkIfSongSavedOnDevice(getHelper(), getSongInfoListAdapter(), Utils.isExternalStorageMounted()); } catch (SQLException e) { Log.e(IMongolduuConstants.LOG_TAG, "exception", e); } listview.invalidateViews(); } } @Override public void onDestroy() { listview = null; textandprogressbar = null; super.onDestroy(); } protected void downloadSong(final SongInfo songinfo) { new AlertDialog.Builder(getActivity()).setTitle(R.string.download_dialog_title) .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Utils.downloadSong(getActivity(), listview, songinfo); } }).setNegativeButton(R.string.button_cancel, null).create().show(); } protected void deleteSong(SongInfo songinfo) { try { Utils.deleteSongFromDevice(getHelper(), songinfo.id); Toast.makeText(getActivity(), R.string.toast_message_song_removed_from_device, Toast.LENGTH_SHORT) .show(); songinfo.isSavedOnDevice = false; ((ChartActivity) getActivity()).checkIfSongSavedOnDeviceAndUpdateListView(); } catch (SQLException e) { Log.e(IMongolduuConstants.LOG_TAG, "exception", e); Utils.showAlertDialog(getActivity(), R.string.alert_dialog_title, R.string.alert_dialog_message_database_error); } } protected void playSong(SongInfo songinfo) { Utils.playSongInAllSongsPlaylist(getActivity(), getHelper(), songinfo); } protected void setSongAsRingtone(SongInfo songinfo) { Utils.setSongAsRingtone(getActivity(), songinfo); } public void onTextAndProgressBarClick(View view) { } @SuppressWarnings("unchecked") protected DatabaseHelper getHelper() { return ((OrmLiteGDActivity<DatabaseHelper>) getActivity()).getHelper(); } public boolean onContextItemSelected(MenuItem item) { if (item.getGroupId() != fragmentIndex) { return false; } AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); SongInfo songinfo = (SongInfo) listview.getAdapter().getItem(menuInfo.position); switch (item.getItemId()) { case R.id.context_menu_remove_song_from_device: deleteSong(songinfo); break; case R.id.context_menu_download: downloadSong(songinfo); break; case R.id.context_menu_play_song: playSong(songinfo); break; case R.id.context_menu_set_as_ringtone: setSongAsRingtone(songinfo); break; default: return super.onContextItemSelected(item); } return true; } }