From 1efed76680de6e8a6adafe87f71c4628d75b5950 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sat, 15 Nov 2014 15:59:52 +0000 Subject: [PATCH] Missing files --- .../java/org/openlp/android2/api/Api.java | 127 ++++++++++++++ .../openlp/android2/common/JsonHelpers.java | 56 +++++++ .../android2/common/NavigationOptions.java | 28 ++++ .../openlp/android2/common/OpenLPDialog.java | 69 ++++++++ .../android2/common/OpenLPHttpClient.java | 157 +++++++++++++++++ .../android2/common/OpenLPHttpReturn.java | 62 +++++++ .../common/OpenLPSSLSocketFactory.java | 48 ++++++ .../android2/dialogs/AlertDisplayDialog.java | 124 ++++++++++++++ .../android2/dialogs/BlankDisplayDialog.java | 158 ++++++++++++++++++ .../main/res/layout/alert_display_dialog.xml | 24 +++ .../main/res/layout/blank_display_dialog.xml | 53 ++++++ 11 files changed, 906 insertions(+) create mode 100644 app/src/main/java/org/openlp/android2/api/Api.java create mode 100644 app/src/main/java/org/openlp/android2/common/JsonHelpers.java create mode 100644 app/src/main/java/org/openlp/android2/common/NavigationOptions.java create mode 100644 app/src/main/java/org/openlp/android2/common/OpenLPDialog.java create mode 100644 app/src/main/java/org/openlp/android2/common/OpenLPHttpClient.java create mode 100644 app/src/main/java/org/openlp/android2/common/OpenLPHttpReturn.java create mode 100644 app/src/main/java/org/openlp/android2/common/OpenLPSSLSocketFactory.java create mode 100644 app/src/main/java/org/openlp/android2/dialogs/AlertDisplayDialog.java create mode 100644 app/src/main/java/org/openlp/android2/dialogs/BlankDisplayDialog.java create mode 100644 app/src/main/res/layout/alert_display_dialog.xml create mode 100644 app/src/main/res/layout/blank_display_dialog.xml diff --git a/app/src/main/java/org/openlp/android2/api/Api.java b/app/src/main/java/org/openlp/android2/api/Api.java new file mode 100644 index 0000000..726f5db --- /dev/null +++ b/app/src/main/java/org/openlp/android2/api/Api.java @@ -0,0 +1,127 @@ +/****************************************************************************** + * 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.api; + +/** + *

Routes:

+ *

+ *

+ *

+ * ``/``
+ * Go to the web interface.
+ *
+ * ``/files/{filename}``
+ *
+ * ``/api/poll``
+ * {"results": {"type": "controller"}}
+ * Or, if there were no results, False::
+ * {"results": False}
+ *
+ * ``/api/display/{hide|show}``
+ * Blank or unblank the screen.
+ *
+ * ``/api/alert``
+ * {"request": {"text": ""}}
+ * ``/api/controller/{live|preview}/{action}``
+ * ``next``
+ * Load the next slide.
+ *
+ * ``previous``
+ * Load the previous slide.
+ *
+ * ``set``
+ * Set a specific slide. Requires an id return in a JSON-encoded dict like
+ * this::
+ *
+ * {"request": {"id": 1}}
+ *
+ * ``first``
+ * Load the first slide.
+ *
+ * ``last``
+ * Load the last slide.
+ *
+ * ``text``
+ * Fetches the text of the current song. The output is a JSON-encoded
+ * dict which looks like this::
+ *
+ * {"result": {"slides": ["...", "..."]}}
+ *
+ * ``/api/service/{action}``
+ * Perform ``{action}`` on the service manager (e.g. go live). Data is
+ * passed as a json-encoded ``data`` parameter. Valid actions are:
+ *
+ * ``next``
+ * Load the next item in the service.
+ *
+ * ``previous``
+ *
+ * ``set``
+ * Set a specific item in the service. Requires an id returned in a
+ * JSON-encoded dict like this::
+ *
+ * {"request": {"id": 1}}
+ *
+ * ``list``
+ * Request a list of items in the service. Returns a list of items in the
+ * current service in a JSON-encoded dict like this::
+ *
+ * {"results": {"items": [{...}, {...}]}}
+ * """
+ * 
+ */ + +public interface Api { + + public final String LIVE_BASE = "/api/controller/live/"; + public final String LIVE_NEXT = "/api/controller/live/next"; + public final String LIVE_PREVIOUS = "/api/controller/live/previous"; + public final String LIVE_TEXT = "/api/controller/live/text"; + public final String LIVE_SET = "/api/controller/live/set?data="; + public final String STAGE_VIEW = "/stage"; + public final String LIVE_VIEW = "/main"; + + public final String SERVICE_LIST = "/api/service/list"; + public final String SERVICE_SET = "/api/service/set?data="; + + public final String DISPLAY_SHOW = "/api/display/show"; + public final String DISPLAY_BLANK = "/api/display/blank"; + public final String DISPLAY_THEME = "/api/display/theme"; + public final String DISPLAY_DESKTOP = "/api/display/desktop"; + public final String POLL_STATUS = "/api/poll"; + + public final String ALERT = "/api/alert?data="; + + public final String SEARCHABLE_PLUGINS = "/api/plugin/search"; + /** + * This is a special string that uses the String.format() method. See + * {@link String#format(String, Object...)} + */ + public final String SEARCH_PLUGIN_FORMATTED = "/api/%s/search?data="; + /** + * Match intent extra key with regex since multiple plugins can be inserted + */ + public final String SEARCH_PLUGIN_ADD = "/api/%s/add?data="; + /** + * Match intent extra key with regex since multiple plugins can be inserted + */ + public final String SEARCH_PLUGIN_LIVE = "/api/%s/live?data="; +} diff --git a/app/src/main/java/org/openlp/android2/common/JsonHelpers.java b/app/src/main/java/org/openlp/android2/common/JsonHelpers.java new file mode 100644 index 0000000..7ef0c70 --- /dev/null +++ b/app/src/main/java/org/openlp/android2/common/JsonHelpers.java @@ -0,0 +1,56 @@ +/****************************************************************************** + * 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.common; + +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONStringer; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +public class JsonHelpers { + + private static String LOG_TAG = JsonHelpers.class.getName(); + + public static String createRequestJSON(String key, String value) throws JSONHandlerException { + try { + String responseJSON; + JSONObject jo = new JSONObject(); + jo.put(key, value); + responseJSON = new JSONStringer().object().key("request").value(jo) + .endObject().toString(); + responseJSON = URLEncoder.encode(responseJSON, "UTF-8"); + return responseJSON; + } catch (JSONException e) { + throw new JSONHandlerException(e); + } catch (UnsupportedEncodingException e) { + throw new JSONHandlerException(e); + } + } + + public static class JSONHandlerException extends Exception { + private static final long serialVersionUID = -6772307308404816615L; + + public JSONHandlerException(Throwable throwable) { + super(throwable); + } + } +} diff --git a/app/src/main/java/org/openlp/android2/common/NavigationOptions.java b/app/src/main/java/org/openlp/android2/common/NavigationOptions.java new file mode 100644 index 0000000..2bfd433 --- /dev/null +++ b/app/src/main/java/org/openlp/android2/common/NavigationOptions.java @@ -0,0 +1,28 @@ +/****************************************************************************** + * 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.common; + +public class NavigationOptions{ + public final static int Home = 0; + public final static int ServiceList = 1; + public final static int LiveList = 2; + public final static int StageView = 3; + public final static int LiveView = 4; +} diff --git a/app/src/main/java/org/openlp/android2/common/OpenLPDialog.java b/app/src/main/java/org/openlp/android2/common/OpenLPDialog.java new file mode 100644 index 0000000..58877c5 --- /dev/null +++ b/app/src/main/java/org/openlp/android2/common/OpenLPDialog.java @@ -0,0 +1,69 @@ +/****************************************************************************** + * 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.common; + +import android.app.DialogFragment; +import android.content.Context; +import android.util.Log; +import com.loopj.android.http.AsyncHttpClient; +import com.loopj.android.http.TextHttpResponseHandler; +import org.apache.http.Header; +import org.openlp.android2.api.Api; + + +abstract public class OpenLPDialog extends DialogFragment { + + private final String LOG_TAG = OpenLPDialog.class.getName(); + protected String calledURL; + protected OpenLPHttpClient httpClient; + protected Context context; + + private static AsyncHttpClient client = new AsyncHttpClient(); + + protected void populateDisplay(String responseString) {} + protected void processUpdate(String responseString) {} + protected void errorDisplay(int statusCode, String responseString) {} + + protected void triggerTextRequest(String url) { + calledURL = url; + Log.d(LOG_TAG, "Trigger Request for url " + url); + String callurl = String.format("%s%s", httpClient.getAbsoluteUrl(client), url ); + client.get(callurl, null, new TextHttpResponseHandler() { + @Override + public void onSuccess(int statusCode, Header[] headers, String responseString) { + // called when response HTTP status is "200 OK" + manageResponse(responseString); + } + @Override + public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { + // called when response HTTP status is "4XX" (eg. 401, 403, 404) + errorDisplay(statusCode, responseString); + } + }); + } + + public void manageResponse(String response) { + if (calledURL.equals(Api.POLL_STATUS)) { + populateDisplay(response); + }else { + processUpdate(response); + } + } +} diff --git a/app/src/main/java/org/openlp/android2/common/OpenLPHttpClient.java b/app/src/main/java/org/openlp/android2/common/OpenLPHttpClient.java new file mode 100644 index 0000000..1c2da77 --- /dev/null +++ b/app/src/main/java/org/openlp/android2/common/OpenLPHttpClient.java @@ -0,0 +1,157 @@ +/****************************************************************************** + * 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.common; + + +import java.security.KeyStore; +import java.util.*; +import android.content.Context; +import android.util.Log; +import com.loopj.android.http.AsyncHttpClient; +import com.loopj.android.http.MySSLSocketFactory; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.openlp.android2.R; + +/** + * Personalised HttpClient to be used throughout OpenLP with customisable + * parameters. + */ +public class OpenLPHttpClient { + + private final String LOG_TAG = OpenLPHttpClient.class.getName(); + private Context context; + private Boolean useSSL = Boolean.FALSE; + + public OpenLPHttpClient(Context context) { + this.context = context; + } + + private String getPreference(Map preferences, String key, String defaultValue) { + if (preferences.containsKey(key)) { + return preferences.get(key).toString(); + } else { + return defaultValue; + } + } + + private Integer getPreference(Map preferences, String key, Integer defaultValue) { + if (preferences.containsKey(key)) { + return Integer.valueOf(preferences.get(key).toString()); + } else { + return defaultValue; + } + } + + private Boolean getPreference(Map preferences, String key, Boolean defaultValue) { + if (preferences.containsKey(key)) { + return Boolean.valueOf(preferences.get(key).toString()); + } else { + return defaultValue; + } + } + + public String getAbsoluteUrl(AsyncHttpClient client) { + + if (useSSL){ + try { + KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); + trustStore.load(null, null); + MySSLSocketFactory sf = new MySSLSocketFactory(trustStore); + sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + client.setSSLSocketFactory(sf); + } + catch (Exception e) {; + } + } + Map preferences = context.getSharedPreferences(context.getString(R.string.keySharedPreferences), Context.MODE_PRIVATE).getAll(); + + useSSL = getPreference(preferences, context.getString(R.string.key_ssl_use), false); + String host = getPreference(preferences, context.getString(R.string.keyHost), + context.getString(R.string.hostDefaultValue)); + String port = getPreference(preferences, context.getString(R.string.keyPort), "4316"); + + + String userid = getPreference(preferences, context.getString(R.string.key_userid), "openlp"); + + String password = getPreference(preferences, context.getString(R.string.key_password), "password"); + + Log.d(LOG_TAG, "Credentials set to " + userid + " : " + password); + client.setBasicAuth(userid,password); + + Credentials creds = new UsernamePasswordCredentials(userid, password); + + String urlBase = String.format("http%s://%s:%s", useSSL ? "s" : "", host, port); + + int connectionTimeout = context.getResources().getInteger( + R.integer.connectionTimeoutDefaultValue); + + if (getPreference(preferences, context.getString(R.string.keyEnableCustomTimeout), false)) { + Log.d(LOG_TAG, "Overriding Connection and Socket timeouts"); + + connectionTimeout = getPreference(preferences, + context.getString(R.string.keyConnectionTimeout), + context.getResources().getInteger( + R.integer.connectionTimeoutDefaultValue) + ); + } + client.setTimeout(connectionTimeout); + if (useSSL){ + try { + KeyStore trustStore = KeyStore.getInstance((KeyStore.getDefaultType())); + trustStore.load(null, null); + OpenLPSSLSocketFactory sf = new OpenLPSSLSocketFactory(trustStore); + sf.setHostnameVerifier((OpenLPSSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)); + client.setSSLSocketFactory(sf); + } + catch (Exception e){ + } + } + return urlBase; + } + + +/* public OpenLPHttpReturn handleExecute() throws IOException { + //HttpResponse response = this.execute(); + + Log.d(LOG_TAG, "Http response code " + String.valueOf(response.getStatusLine().getStatusCode())); + + if (response.getStatusLine().getStatusCode() == 200) { + BufferedReader bufferedReader; + HttpEntity entity = response.getEntity(); + + if (entity != null) { + bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); + StringBuilder stringBuilder = new StringBuilder(); + + String line = bufferedReader.readLine(); + while (line != null) { + stringBuilder.append(line); + line = bufferedReader.readLine(); + } + bufferedReader.close(); + return new OpenLPHttpReturn(0, stringBuilder.toString(), this.context); + } + } + return new OpenLPHttpReturn(response.getStatusLine().getStatusCode(), null, this.context); + }*/ + + +} diff --git a/app/src/main/java/org/openlp/android2/common/OpenLPHttpReturn.java b/app/src/main/java/org/openlp/android2/common/OpenLPHttpReturn.java new file mode 100644 index 0000000..cfe5503 --- /dev/null +++ b/app/src/main/java/org/openlp/android2/common/OpenLPHttpReturn.java @@ -0,0 +1,62 @@ +/****************************************************************************** + * 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.common; + +import android.content.Context; +import org.openlp.android2.R; + +public class OpenLPHttpReturn { + private int return_code = 0; + private String data = null; + private Context context; + + public OpenLPHttpReturn() { + this.return_code = -1; + this.data = ""; + this.context = null; + } + + public OpenLPHttpReturn(int return_code, String data, Context context) { + this.return_code = return_code; + this.data = data; + this.context = context; + } + + public String getData() { + return this.data; + } + + public boolean isError() { + return return_code != 0; + } + + public boolean isSecurityError() { + return return_code == 401; + } + + public String getErrorMessage(String message) { + return return_code == 401 ? this.context.getString(R.string.httpreturn_unauthorised) : message; + } + + @Override + public String toString() { + return "HttpReturn{" + "data='" + data + '\'' + ", return code=" + return_code + '}'; + } +} diff --git a/app/src/main/java/org/openlp/android2/common/OpenLPSSLSocketFactory.java b/app/src/main/java/org/openlp/android2/common/OpenLPSSLSocketFactory.java new file mode 100644 index 0000000..4dd8416 --- /dev/null +++ b/app/src/main/java/org/openlp/android2/common/OpenLPSSLSocketFactory.java @@ -0,0 +1,48 @@ +package org.openlp.android2.common; + +import org.apache.http.conn.ssl.SSLSocketFactory; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import java.io.IOException; +import java.net.Socket; +import java.net.UnknownHostException; +import java.security.*; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +/** + * Created by tim on 14/11/14. + */ +public class OpenLPSSLSocketFactory extends SSLSocketFactory { + SSLContext sslContext = SSLContext.getInstance("TLS"); + + public OpenLPSSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { + super(truststore); + + TrustManager tm = new X509TrustManager() { + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + } + + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + } + + public X509Certificate[] getAcceptedIssuers() { + return null; + } + }; + + sslContext.init(null, new TrustManager[] { tm }, null); + } + + @Override + public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { + return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose); + } + + @Override + public Socket createSocket() throws IOException { + return sslContext.getSocketFactory().createSocket(); + } +} diff --git a/app/src/main/java/org/openlp/android2/dialogs/AlertDisplayDialog.java b/app/src/main/java/org/openlp/android2/dialogs/AlertDisplayDialog.java new file mode 100644 index 0000000..6698bd4 --- /dev/null +++ b/app/src/main/java/org/openlp/android2/dialogs/AlertDisplayDialog.java @@ -0,0 +1,124 @@ +/****************************************************************************** + * 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.dialogs; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +import org.openlp.android2.R; +import org.openlp.android2.api.Api; +import org.openlp.android2.common.JsonHelpers; +import org.openlp.android2.common.OpenLPDialog; + +import org.openlp.android2.common.OpenLPHttpReturn; + +public class AlertDisplayDialog extends OpenLPDialog { + private final String LOG_TAG = AlertDisplayDialog.class.getName(); + public AlertDialog dialog; + + /** + * The system calls this only when creating the layout in a dialog. + */ + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + // The only reason you might override this method when using onCreateView() is + // to modify any dialog characteristics. For example, the dialog includes a + // title by default, but your custom layout might not need it. So here you can + // remove the dialog title, but you must call the superclass to get the Dialog. + + context = getActivity(); + + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + // Get the layout inflater + LayoutInflater inflater = getActivity().getLayoutInflater(); + + // Inflate and set the layout for the dialog + // Pass null as the parent view because its going in the dialog layout + View view = inflater.inflate(R.layout.alert_display_dialog, null); + builder.setView(view); + + builder.setPositiveButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + AlertDisplayDialog.this.getDialog().cancel(); + } + }); + builder.setNegativeButton(R.string.process, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog1, int id) { + EditText text = (EditText) dialog.findViewById(R.id.alertText); + requestAlert(text.getText().toString()); + } + }); + dialog = builder.create(); + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialogI) { + Button btnNegative = dialog.getButton(Dialog.BUTTON_NEGATIVE); + btnNegative.setTextSize(20); + Button btnPositive = dialog.getButton(Dialog.BUTTON_POSITIVE); + btnPositive.setTextSize(20); + } + }); + return dialog; + } + + @Override + public void onResume() { + super.onResume(); + Log.d(LOG_TAG, "Resuming..."); + triggerRequest(Api.POLL_STATUS); + Log.d(LOG_TAG, "Resumed..."); + } + + private void triggerRequest(String url) { + ExecuteHttpRequestTask task = new ExecuteHttpRequestTask(); + task.execute(new String[]{url}); + } + + public void populateDisplay(OpenLPHttpReturn response) { + Log.d(LOG_TAG, "populateDisplay"); + if (response.isSecurityError()) { + Toast.makeText(context, R.string.httpreturn_unauthorised, Toast.LENGTH_LONG).show(); + } else if (response.isError()) { + Toast.makeText(context, R.string.unable, Toast.LENGTH_LONG).show(); + } + } + + public void requestAlert(String text) { + try { + String request = JsonHelpers.createRequestJSON("text", text); + triggerRequest(String.format("%s%s", Api.ALERT, request)); + Log.d(LOG_TAG, String.format("Setting list data. apiBase(%s), text(%s)", + Api.ALERT, text)); + Toast.makeText(getActivity().getBaseContext(), "Alert Requested", Toast.LENGTH_SHORT).show(); + } catch (JsonHelpers.JSONHandlerException e) { + e.printStackTrace(); + Toast.makeText(getActivity().getBaseContext(), "Request Failed", Toast.LENGTH_SHORT).show(); + } + } +} diff --git a/app/src/main/java/org/openlp/android2/dialogs/BlankDisplayDialog.java b/app/src/main/java/org/openlp/android2/dialogs/BlankDisplayDialog.java new file mode 100644 index 0000000..c3ae01e --- /dev/null +++ b/app/src/main/java/org/openlp/android2/dialogs/BlankDisplayDialog.java @@ -0,0 +1,158 @@ +/****************************************************************************** + * 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.dialogs; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.Toast; +import org.json.JSONException; +import org.json.JSONObject; +import org.openlp.android2.R; +import org.openlp.android2.api.Api; + +import org.openlp.android2.common.OpenLPDialog; +import org.openlp.android2.common.OpenLPHttpClient; + +public class BlankDisplayDialog extends OpenLPDialog { + private final String LOG_TAG = BlankDisplayDialog.class.getName(); + public AlertDialog dialog; + Button desktop; + Button screen; + Button theme; + Button reset; + + /** + * The system calls this only when creating the layout in a dialog. + */ + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + // The only reason you might override this method when using onCreateView() is + // to modify any dialog characteristics. For example, the dialog includes a + // title by default, but your custom layout might not need it. So here you can + // remove the dialog title, but you must call the superclass to get the Dialog. + + context = getActivity(); + httpClient = new OpenLPHttpClient(context); + + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + // Get the layout inflater + LayoutInflater inflater = getActivity().getLayoutInflater(); + + // Inflate and set the layout for the dialog + // Pass null as the parent view because its going in the dialog layout + View view = inflater.inflate(R.layout.blank_display_dialog, null); + builder.setView(view); + + reset = (Button) view.findViewById(R.id.buttonReset); + reset.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + triggerTextRequest(Api.DISPLAY_SHOW); + } + }); + screen = (Button) view.findViewById(R.id.buttonScreen); + screen.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + triggerTextRequest(Api.DISPLAY_BLANK); + } + }); + theme = (Button) view.findViewById(R.id.buttonTheme); + theme.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + triggerTextRequest(Api.DISPLAY_THEME); + } + }); + desktop = (Button) view.findViewById(R.id.buttonDesktop); + desktop.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + triggerTextRequest(Api.DISPLAY_DESKTOP); + } + }); + builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + BlankDisplayDialog.this.getDialog().cancel(); + } + }); + dialog = builder.create(); + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialogI) { + Button btnNegative = dialog.getButton(Dialog.BUTTON_NEGATIVE); + btnNegative.setTextSize(20); + } + }); + return dialog; + } + + @Override + public void onResume() { + super.onResume(); + Log.d(LOG_TAG, "Resuming..."); + triggerTextRequest(Api.POLL_STATUS); + Log.d(LOG_TAG, "Resumed..."); + } + + public void processUpdate(String response) { + triggerTextRequest(Api.POLL_STATUS); + } + + public void populateDisplay(String json) { + + Log.d(LOG_TAG, "populateDisplay"); + try { + JSONObject item = new JSONObject(json).getJSONObject("results"); + if (item.getString("theme").equals("false") & + item.getString("display").equals("false") & + item.getString("blank").equals("false")) { + screen.setEnabled(true); + theme.setEnabled(true); + desktop.setEnabled(true); + reset.setEnabled(false); + } else { + screen.setEnabled(false); + theme.setEnabled(false); + desktop.setEnabled(false); + reset.setEnabled(true); + } + } catch (JSONException e) { + Log.e(LOG_TAG, "Exception with Json = " + json); + e.printStackTrace(); + } + } + + public void errorDisplay(int statusCode, String responseString) { + Log.d(LOG_TAG, String.format("URL Error status code %d text %s", statusCode, responseString)); + screen.setEnabled(false); + theme.setEnabled(false); + desktop.setEnabled(false); + reset.setEnabled(false); + Toast.makeText(context, R.string.unable, Toast.LENGTH_LONG).show(); + } +} diff --git a/app/src/main/res/layout/alert_display_dialog.xml b/app/src/main/res/layout/alert_display_dialog.xml new file mode 100644 index 0000000..69e39d9 --- /dev/null +++ b/app/src/main/res/layout/alert_display_dialog.xml @@ -0,0 +1,24 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/blank_display_dialog.xml b/app/src/main/res/layout/blank_display_dialog.xml new file mode 100644 index 0000000..ec3aaa3 --- /dev/null +++ b/app/src/main/res/layout/blank_display_dialog.xml @@ -0,0 +1,53 @@ + + + + + +