android/app/src/main/java/org/openlp/android2/fragments/LiveListFragment.java

178 lines
6.9 KiB
Java
Raw Normal View History

2014-11-15 15:43:52 +00:00
/******************************************************************************
* OpenLP - Open Source Lyrics Projection *
* --------------------------------------------------------------------------- *
2015-01-19 21:09:12 +00:00
* Copyright (c) 2011-2015 OpenLP Android Developers *
2014-11-15 15:43:52 +00:00
* --------------------------------------------------------------------------- *
* This program 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; version 2 of the License. *
* *
* This program 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. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., 59 *
* Temple Place, Suite 330, Boston, MA 02111-1307 USA *
*******************************************************************************/
2014-11-14 16:34:14 +00:00
package org.openlp.android2.fragments;
2014-11-24 21:35:52 +00:00
import android.content.Context;
2015-01-19 20:26:35 +00:00
import android.graphics.Color;
import android.graphics.Typeface;
2014-11-14 16:34:14 +00:00
import android.os.Bundle;
2014-11-24 21:35:52 +00:00
import android.content.SharedPreferences;
import android.text.Html;
import android.util.Log;
import android.util.TypedValue;
2014-11-14 16:34:14 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
2014-11-24 21:35:52 +00:00
import android.widget.SimpleAdapter;
2014-11-14 16:34:14 +00:00
import android.widget.TextView;
2014-11-24 21:35:52 +00:00
import android.widget.Toast;
2015-01-23 17:53:00 +00:00
2014-11-24 21:35:52 +00:00
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
2014-11-14 16:34:14 +00:00
import org.openlp.android2.R;
2014-11-24 21:35:52 +00:00
import org.openlp.android2.api.Api;
import org.openlp.android2.common.JsonHelpers;
import org.openlp.android2.common.OpenLPFragment;
import org.openlp.android2.common.OpenLPHttpClient;
2014-11-14 16:34:14 +00:00
2014-11-24 21:35:52 +00:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
2014-11-14 16:34:14 +00:00
2014-11-24 21:35:52 +00:00
public class LiveListFragment extends OpenLPFragment {
2014-11-14 16:34:14 +00:00
2015-01-19 20:26:35 +00:00
private int selected = 0;
2014-11-24 21:35:52 +00:00
private String LOG_TAG = LiveListFragment.class.getName();
2014-11-14 16:34:14 +00:00
public static LiveListFragment newInstance() {
2015-01-23 17:53:00 +00:00
return new LiveListFragment();
2014-11-14 16:34:14 +00:00
}
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public LiveListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
2014-11-24 21:35:52 +00:00
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = getActivity();
updateUrl = Api.LIVE_TEXT;
httpClient = new OpenLPHttpClient(context);
return super.onCreateView(inflater, container, savedInstanceState);
2014-11-14 16:34:14 +00:00
}
@Override
2015-03-08 11:36:29 +00:00
public void populateDisplay(String json, boolean notInError) {
2014-11-24 21:35:52 +00:00
Log.i(LOG_TAG, "populate_display - entry");
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
2015-01-19 20:26:35 +00:00
selected = 0;
2014-11-24 21:35:52 +00:00
2015-03-08 11:36:29 +00:00
if (notInError) {
try {
JSONArray items = new JSONObject(json).getJSONObject("results").getJSONArray("slides");
for (int i = 0; i < items.length(); ++i) {
JSONObject item = items.getJSONObject(i);
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("tag", item.getString("tag"));
if (item.getString("selected").equals("true")) {
selected = i;
}
hm.put("liveListNormal", Html.fromHtml(item.getString("html")).toString());
aList.add(hm);
2014-11-24 21:35:52 +00:00
}
2015-03-08 11:36:29 +00:00
} catch (JSONException e) {
2015-04-02 20:52:53 +00:00
Log.e(LOG_TAG,json);
2015-03-08 11:36:29 +00:00
e.printStackTrace();
2014-11-24 21:35:52 +00:00
}
2015-01-23 17:53:00 +00:00
}
2014-11-24 21:35:52 +00:00
// Keys used in Hashmap
String[] from = {"tag", "liveListNormal", "liveListSelected"};
// Ids of views in live_list_fragment
int[] to = {R.id.tag, R.id.liveListNormal, R.id.liveListSelected};
SharedPreferences prefs = context.getSharedPreferences(
2015-01-23 17:53:00 +00:00
context.getString(R.string.key_shared_preferences),
2014-11-24 21:35:52 +00:00
Context.MODE_PRIVATE);
final int size = Integer.parseInt(prefs.getString(
2015-01-23 17:53:00 +00:00
context.getString(R.string.key_text_size),
2014-11-24 21:35:52 +00:00
String.valueOf(context.getResources().getInteger(
R.integer.textSizeDefaultValue))));
// Instantiating an adapter to store each items
ListAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList,
2015-01-23 17:53:00 +00:00
R.layout.fragment_livelist, from, to) {
2014-11-24 21:35:52 +00:00
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
2015-01-19 20:26:35 +00:00
TextView text1 = (TextView) view.findViewById(R.id.tag);
2014-11-24 21:35:52 +00:00
text1.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
2015-01-19 20:26:35 +00:00
TextView text2 = (TextView) view.findViewById(R.id.liveListNormal);
2014-11-24 21:35:52 +00:00
text2.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
2015-01-23 17:53:00 +00:00
if (selected == position) {
2015-01-19 20:26:35 +00:00
text2.setTextColor(Color.parseColor("#000000"));
text2.setTypeface(null, Typeface.BOLD_ITALIC);
} else{
text2.setTypeface(null, Typeface.NORMAL);
2015-01-19 20:26:35 +00:00
}
2014-11-24 21:35:52 +00:00
return view;
2015-01-23 17:53:00 +00:00
}
2014-11-24 21:35:52 +00:00
};
setListAdapter(adapter);
2015-09-20 21:02:37 +00:00
//getListView().setSelection(selected - 1);
2014-11-24 21:35:52 +00:00
Log.i(LOG_TAG, "populate_display - exit");
2014-11-14 16:34:14 +00:00
}
@Override
2015-01-23 17:53:00 +00:00
public void refreshDisplay() {
2014-11-24 21:35:52 +00:00
Log.d(LOG_TAG, "Resuming...");
triggerTextRequest(Api.LIVE_TEXT);
2014-11-14 16:34:14 +00:00
}
@Override
2014-11-24 21:35:52 +00:00
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "Resuming...");
triggerTextRequest(Api.LIVE_TEXT);
Log.d(LOG_TAG, "Resumed...");
2014-11-14 16:34:14 +00:00
}
2015-04-02 20:52:53 +00:00
public void processUpdate(String response, boolean inError) {
2014-11-24 21:35:52 +00:00
triggerTextRequest(Api.LIVE_TEXT);
2014-11-14 16:34:14 +00:00
}
2014-11-24 21:35:52 +00:00
public void itemClicked(int position) {
try {
String request = JsonHelpers.createRequestJSON("id", Integer.toString(position));
triggerTextRequest(String.format("%s%s", Api.LIVE_SET, request));
Log.d(LOG_TAG, String.format("Setting list data. apiBase(%s), position(%s)",
Api.LIVE_SET, position));
} catch (JsonHelpers.JSONHandlerException e) {
e.printStackTrace();
Toast.makeText(getActivity().getBaseContext(), "Request Failed", Toast.LENGTH_SHORT).show();
}
2014-11-14 16:34:14 +00:00
}
}