mirror of https://gitlab.com/openlp/android.git
205 lines
8.2 KiB
Java
205 lines
8.2 KiB
Java
/******************************************************************************
|
|
* OpenLP - Open Source Lyrics Projection *
|
|
* --------------------------------------------------------------------------- *
|
|
* Copyright (c) 2011-2014 Raoul Snyman *
|
|
* Portions copyright (c) 2011-2014 Tim Bentley, Johan Mynhardt *
|
|
* *
|
|
* --------------------------------------------------------------------------- *
|
|
* 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 *
|
|
*******************************************************************************/
|
|
package org.openlp.android2.fragments;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.util.TypedValue;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.*;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.openlp.android2.OpenLP;
|
|
import org.openlp.android2.R;
|
|
|
|
import org.openlp.android2.api.Api;
|
|
import org.openlp.android2.common.JsonHelpers;
|
|
import org.openlp.android2.common.NavigationOptions;
|
|
import org.openlp.android2.common.OpenLPFragment;
|
|
import org.openlp.android2.common.OpenLPHttpClient;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
|
|
public class ServiceListFragment extends OpenLPFragment {
|
|
|
|
private final String LOG_TAG = ServiceListFragment.class.getName();
|
|
private boolean noScreenUpdate = false;
|
|
private boolean isDoubleClick = false;
|
|
|
|
public ServiceListFragment() {
|
|
}
|
|
|
|
public static ServiceListFragment newInstance() {
|
|
ServiceListFragment fragment = new ServiceListFragment();
|
|
return fragment;
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
context = getActivity();
|
|
noScreenUpdate = false;
|
|
isDoubleClick = false;
|
|
updateUrl = Api.SERVICE_LIST;
|
|
httpClient = new OpenLPHttpClient(context);
|
|
return super.onCreateView(inflater, container, savedInstanceState);
|
|
}
|
|
|
|
@Override
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
AdapterView.OnItemLongClickListener listener = new AdapterView.OnItemLongClickListener() {
|
|
@Override
|
|
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {
|
|
Toast.makeText(getActivity().getBaseContext(), "Long Clicked ", Toast.LENGTH_SHORT).show();
|
|
itemClicked(position);
|
|
noScreenUpdate = true;
|
|
isDoubleClick = true;
|
|
((OpenLP) getActivity()).setMenuActive();
|
|
((OpenLP) getActivity()).onNavigationDrawerItemSelected(NavigationOptions.LiveList);
|
|
((OpenLP) getActivity()).restoreActionBar();
|
|
return false;
|
|
}
|
|
};
|
|
getListView().setOnItemLongClickListener(listener);
|
|
}
|
|
|
|
@Override
|
|
public void onAttach(Activity activity) {
|
|
super.onAttach(activity);
|
|
}
|
|
|
|
@Override
|
|
public void onDetach() {
|
|
super.onDetach();
|
|
}
|
|
|
|
@Override
|
|
public void refreshDisplay(){
|
|
Log.d(LOG_TAG, "Resuming...");
|
|
triggerTextRequest(Api.SERVICE_LIST);
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
Log.d(LOG_TAG, "Resuming...");
|
|
triggerTextRequest(Api.SERVICE_LIST);
|
|
Log.d(LOG_TAG, "Resumed...");
|
|
}
|
|
|
|
public void processUpdate(String response) {
|
|
triggerTextRequest(Api.SERVICE_LIST);
|
|
}
|
|
|
|
@Override
|
|
public void populateDisplay(String json) {
|
|
Log.i(LOG_TAG, "populate_display - entry");
|
|
if (noScreenUpdate || isDoubleClick) {
|
|
noScreenUpdate = false;
|
|
return;
|
|
}
|
|
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
|
|
|
|
try {
|
|
JSONArray items = new JSONObject(json).getJSONObject("results").getJSONArray("items");
|
|
|
|
for (int i = 0; i < items.length(); ++i) {
|
|
JSONObject item = items.getJSONObject(i);
|
|
|
|
HashMap<String, String> hm = new HashMap<String, String>();
|
|
if (item.getString("plugin").equals("songs")) {
|
|
hm.put("icon", Integer.toString(R.drawable.ic_my_library_music));
|
|
} else if (item.getString("plugin").equals("bibles")) {
|
|
hm.put("icon", Integer.toString(R.drawable.ic_my_library_books));
|
|
} else if (item.getString("plugin").equals("media")) {
|
|
hm.put("icon", Integer.toString(R.drawable.ic_local_movies));
|
|
} else if (item.getString("plugin").equals("presentations")) {
|
|
hm.put("icon", Integer.toString(R.drawable.ic_video_collection));
|
|
} else if (item.getString("plugin").equals("images")) {
|
|
hm.put("icon", Integer.toString(R.drawable.ic_image));
|
|
} else {
|
|
hm.put("icon", Integer.toString(R.drawable.ic_edit));
|
|
}
|
|
|
|
hm.put("title", item.getString("title"));
|
|
aList.add(hm);
|
|
}
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
// Keys used in Hashmap
|
|
String[] from = {"icon", "title"};
|
|
|
|
// Ids of views in service_list_fragment
|
|
int[] to = {R.id.icon, R.id.serviceListText};
|
|
|
|
SharedPreferences prefs = context.getSharedPreferences(
|
|
context.getString(R.string.keySharedPreferences),
|
|
Context.MODE_PRIVATE);
|
|
|
|
final int size = Integer.parseInt(prefs.getString(
|
|
context.getString(R.string.keyTextSize),
|
|
String.valueOf(context.getResources().getInteger(
|
|
R.integer.textSizeDefaultValue))));
|
|
|
|
// Instantiating an adapter to store each items
|
|
ListAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList,
|
|
R.layout.fragment_service_list, from, to) {
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
View view = super.getView(position, convertView, parent);
|
|
TextView text1 = (TextView) view.findViewById(R.id.serviceListText);
|
|
text1.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
|
|
return view;
|
|
|
|
}
|
|
};
|
|
setListAdapter(adapter);
|
|
Log.i(LOG_TAG, "populate_display - exit");
|
|
}
|
|
|
|
public void itemClicked(int position) {
|
|
try {
|
|
noScreenUpdate = true;
|
|
String request = JsonHelpers.createRequestJSON("id", Integer.toString(position));
|
|
triggerTextRequest(String.format("%s%s", Api.SERVICE_SET, request));
|
|
String message = String.format("Setting list data. apiBase(%s), position(%s)",
|
|
Api.SERVICE_SET, position);
|
|
Log.d(LOG_TAG, message);
|
|
Toast.makeText(context, "Service Item selected", Toast.LENGTH_SHORT).show();
|
|
} catch (JsonHelpers.JSONHandlerException e) {
|
|
e.printStackTrace();
|
|
Toast.makeText(context, "Request Failed", Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
|
|
}
|