From 0da86e7d9d8598b8ce4230f1ffa2579f4d87e98f Mon Sep 17 00:00:00 2001 From: Juan Miguel Boyero Corral Date: Sat, 7 Apr 2012 14:57:17 +0000 Subject: [PATCH] Fixed merge conflicts git-svn-id: https://forja.rediris.es/svn/cusl6-swadroid/trunk@263 5bc14d19-1e4b-4ba2-aa50-860af135f48c --- .gitignore | 6 + SWADroid/AndroidManifest.xml | 2 + SWADroid/gen/es/ugr/swad/swadroid/R.java | 347 +++++++++--------- SWADroid/res/layout/notice_dialog.xml | 46 +++ SWADroid/res/values-es/strings.xml | 4 + SWADroid/res/values/strings.xml | 4 + SWADroid/res/xml/tables.xml | 1 + SWADroid/src/es/ugr/swad/swadroid/Global.java | 5 + .../swadroid/ImageExpandableListAdapter.java | 24 +- .../src/es/ugr/swad/swadroid/SWADMain.java | 11 +- .../es/ugr/swad/swadroid/model/Course.java | 29 +- .../swad/swadroid/model/DataBaseHelper.java | 4 +- .../src/es/ugr/swad/swadroid/model/User.java | 112 +++--- .../es/ugr/swad/swadroid/modules/Courses.java | 9 +- .../es/ugr/swad/swadroid/modules/Login.java | 29 +- .../es/ugr/swad/swadroid/modules/Notices.java | 303 +++++++++++++++ 16 files changed, 673 insertions(+), 263 deletions(-) create mode 100644 SWADroid/res/layout/notice_dialog.xml create mode 100644 SWADroid/src/es/ugr/swad/swadroid/modules/Notices.java diff --git a/.gitignore b/.gitignore index c7ecc16e..b415d47c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,9 @@ capturas/ iconos/ gitstats/ SWADroidTest/ +.DS_Store +SWADroid/.DS_Store +SWADroid/gen/es/ugr/swad/swadroid/BuildConfig.java +SWADroid/gen/es/ugr/swad/swadroid/R.java + + diff --git a/SWADroid/AndroidManifest.xml b/SWADroid/AndroidManifest.xml index fc38c6c5..590ded16 100644 --- a/SWADroid/AndroidManifest.xml +++ b/SWADroid/AndroidManifest.xml @@ -38,6 +38,8 @@ + + + + + + + + + + + + + + + + + + + diff --git a/SWADroid/res/values-es/strings.xml b/SWADroid/res/values-es/strings.xml index 1e0854ca..f3857af2 100644 --- a/SWADroid/res/values-es/strings.xml +++ b/SWADroid/res/values-es/strings.xml @@ -6,6 +6,7 @@ Asignaturas Tests Enviar mensaje + Publicar nuevo aviso ERROR Cerrar ConfiguraciĆ³n @@ -111,6 +112,9 @@ Destinatarios (separados por comas) Asunto Mensaje + Aviso + Publicando aviso... + Aviso publicadoError al publicar el nuevo aviso Enviar Todos los datos de la base de datos han sido borrados Estoy usando #SWADroid en mi Android para acceder a la diff --git a/SWADroid/res/values/strings.xml b/SWADroid/res/values/strings.xml index 240a8e23..8c8ed056 100644 --- a/SWADroid/res/values/strings.xml +++ b/SWADroid/res/values/strings.xml @@ -6,6 +6,7 @@ Courses Tests Send message + Publish new notice ERROR Close Preferences @@ -108,6 +109,9 @@ Receivers (separated by comma) Subject Message + Notice + Publishing notice... + Notice publishedError while publishing the new notice Send The database has been cleaned I\'m using #SWADroid in my Android mobile for access to diff --git a/SWADroid/res/xml/tables.xml b/SWADroid/res/xml/tables.xml index ed0ece24..1c3f0756 100644 --- a/SWADroid/res/xml/tables.xml +++ b/SWADroid/res/xml/tables.xml @@ -3,6 +3,7 @@ +
diff --git a/SWADroid/src/es/ugr/swad/swadroid/Global.java b/SWADroid/src/es/ugr/swad/swadroid/Global.java index c1fc01a5..c4a115fc 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/Global.java +++ b/SWADroid/src/es/ugr/swad/swadroid/Global.java @@ -72,6 +72,10 @@ public class Global { * Request code for Tests module. */ public static final int MESSAGES_REQUEST_CODE = 8; + /** + * Request code for Notice module + */ + public static final int NOTICES_REQUESET_CODE = 9; /** * Prefix tag name for Logcat */ @@ -177,6 +181,7 @@ public class Global { } /** + * Function to parse from Boolean to String * @param b Boolean to be parsed * @return "Y" if b==true, "N" in other case diff --git a/SWADroid/src/es/ugr/swad/swadroid/ImageExpandableListAdapter.java b/SWADroid/src/es/ugr/swad/swadroid/ImageExpandableListAdapter.java index c99a26db..d860140f 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/ImageExpandableListAdapter.java +++ b/SWADroid/src/es/ugr/swad/swadroid/ImageExpandableListAdapter.java @@ -38,6 +38,10 @@ public class ImageExpandableListAdapter extends SimpleExpandableListAdapter { final String NAME = "listText"; final String IMAGE = "listIcon"; final LayoutInflater layoutInflater; + List> groupData; + List>> childData; + Context context; + public ImageExpandableListAdapter(Context context, List> groupData, int expandedGroupLayout, @@ -48,7 +52,8 @@ public class ImageExpandableListAdapter extends SimpleExpandableListAdapter { super(context, groupData, expandedGroupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo); - + this.groupData = groupData; + this.childData = childData; layoutInflater = LayoutInflater.from(context); } @@ -93,4 +98,21 @@ public class ImageExpandableListAdapter extends SimpleExpandableListAdapter { public View newChildView(boolean isLastChild, ViewGroup parent) { return layoutInflater.inflate(R.layout.image_list_item, parent, false); } + + /** + * Removes the child which located at childPosition under the group located at groupPosition. + * If it is removed, it will not be shown. + * @param groupPosition + * @param childPosition + * @return true if the child was removed; + * */ + public boolean removeChild(int groupPosition,int childPosition){ + + if(groupPosition>= getGroupCount() || childPosition>=getChildrenCount(groupPosition)) + return false; + childData.get(groupPosition).remove(childPosition); + super.notifyDataSetChanged(); + + return true; + } } diff --git a/SWADroid/src/es/ugr/swad/swadroid/SWADMain.java b/SWADroid/src/es/ugr/swad/swadroid/SWADMain.java index 84c97d9f..35be0645 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/SWADMain.java +++ b/SWADroid/src/es/ugr/swad/swadroid/SWADMain.java @@ -42,6 +42,7 @@ import es.ugr.swad.swadroid.model.User; import es.ugr.swad.swadroid.modules.Messages; import es.ugr.swad.swadroid.modules.notifications.Notifications; import es.ugr.swad.swadroid.modules.tests.Tests; +import es.ugr.swad.swadroid.modules.Notices; import es.ugr.swad.swadroid.ssl.SecureConnection; /** @@ -125,6 +126,9 @@ public class SWADMain extends MenuExpandableListActivity { activity = new Intent(getBaseContext(), Messages.class); activity.putExtra("notificationCode", new Long(0)); startActivityForResult(activity, Global.MESSAGES_REQUEST_CODE); + } else if(keyword.equals(getString(R.string.noticesModuleLabel))){ + activity = new Intent(getBaseContext(), Notices.class); + startActivityForResult(activity, Global.NOTICES_REQUESET_CODE); } return true; @@ -176,6 +180,11 @@ public class SWADMain extends MenuExpandableListActivity { map.put(IMAGE, getResources().getDrawable(R.drawable.msg)); messagesData.add(map); + map = new HashMap(); + map.put(NAME, getString(R.string.noticesModuleLabel) ); + map.put(IMAGE, getResources().getDrawable(R.drawable.note)); + messagesData.add(map); + //Evaluation category map = new HashMap(); map.put(NAME, getString(R.string.testsModuleLabel) ); @@ -216,7 +225,7 @@ public class SWADMain extends MenuExpandableListActivity { text = (TextView)this.findViewById(R.id.moduleName); text.setText(R.string.app_name); - + createMainMenu(); try { diff --git a/SWADroid/src/es/ugr/swad/swadroid/model/Course.java b/SWADroid/src/es/ugr/swad/swadroid/model/Course.java index ff428f7c..d65f4e49 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/model/Course.java +++ b/SWADroid/src/es/ugr/swad/swadroid/model/Course.java @@ -31,13 +31,16 @@ public class Course extends Model { * Course name */ private String name; + private int userRole; private static PropertyInfo PI_id = new PropertyInfo(); private static PropertyInfo PI_name = new PropertyInfo(); + private static PropertyInfo PI_userRole = new PropertyInfo(); @SuppressWarnings("unused") private static PropertyInfo[] PI_PROP_ARRAY = { PI_id, - PI_name + PI_name, + PI_userRole }; /** @@ -45,9 +48,10 @@ public class Course extends Model { * @param id Course identifier * @param name Course name */ - public Course(long id, String name) { + public Course(long id, String name, int userRole) { super(id); this.name = name; + this.userRole = userRole; } /** @@ -65,6 +69,15 @@ public class Course extends Model { public void setName(String name) { this.name = name; } + + /** + * Gets user role inside the course + * @return user role (2 = student, 3 = teacher) + */ + public int getUserRole(){ + return userRole; + } + /* (non-Javadoc) * @see java.lang.Object#hashCode() @@ -82,7 +95,7 @@ public class Course extends Model { */ @Override public String toString() { - return "Course [name=" + name + ", getId()=" + getId() + "]"; + return "Course [name=" + name + ", getId()=" + getId() + " getUserRole()="+ getUserRole()+"]"; } /* (non-Javadoc) @@ -94,6 +107,7 @@ public class Course extends Model { { case 0 : object = this.getId();break; case 1 : object = name;break; + case 2 : object = userRole;break; } return object; @@ -103,7 +117,7 @@ public class Course extends Model { * @see org.ksoap2.serialization.KvmSerializable#getPropertyCount() */ public int getPropertyCount() { - return 2; + return 3; } /* (non-Javadoc) @@ -118,7 +132,11 @@ public class Course extends Model { case 1: propertyInfo.type = PropertyInfo.STRING_CLASS; propertyInfo.name = "name"; - break; + break; + case 2: + propertyInfo.type = PropertyInfo.INTEGER_CLASS; + propertyInfo.name = "userRole"; + break; } } @@ -130,6 +148,7 @@ public class Course extends Model { { case 0 : this.setId((Long)obj); break; case 1 : name = (String)obj; break; + case 2 : userRole = (Integer)obj; break; } } } \ No newline at end of file diff --git a/SWADroid/src/es/ugr/swad/swadroid/model/DataBaseHelper.java b/SWADroid/src/es/ugr/swad/swadroid/model/DataBaseHelper.java index 6642f918..b85952d0 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/model/DataBaseHelper.java +++ b/SWADroid/src/es/ugr/swad/swadroid/model/DataBaseHelper.java @@ -113,7 +113,8 @@ public class DataBaseHelper { if(table.equals(Global.DB_TABLE_COURSES)) { o = new Course(ent.getInt("id"), - ent.getString("name")); + ent.getString("name"), + ent.getInt("userRole")); } else if(table.equals(Global.DB_TABLE_TEST_QUESTIONS_COURSE) || table.equals(Global.DB_TABLE_TEST_QUESTION_ANSWERS)) { @@ -266,6 +267,7 @@ public class DataBaseHelper { Entity ent = new Entity(Global.DB_TABLE_COURSES); ent.setValue("id", c.getId()); ent.setValue("name", c.getName()); + ent.setValue("userRole", c.getUserRole()); ent.save(); } diff --git a/SWADroid/src/es/ugr/swad/swadroid/model/User.java b/SWADroid/src/es/ugr/swad/swadroid/model/User.java index 6d6bf6f1..04e8cee4 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/model/User.java +++ b/SWADroid/src/es/ugr/swad/swadroid/model/User.java @@ -33,10 +33,7 @@ public class User implements KvmSerializable { * User code. */ private static String userCode; - /** - * Code of user type. - */ - private static String userTypeCode; + /** * Webservices session key. */ @@ -58,28 +55,27 @@ public class User implements KvmSerializable { */ private static String userFirstName; /** - * Name of the user type. + * User role. 1:guest 2: student 3: teacher */ - private static String userTypeName; + private static int userRole; + private static PropertyInfo PI_userCode = new PropertyInfo(); - private static PropertyInfo PI_userTypeCode = new PropertyInfo(); private static PropertyInfo PI_wsKey = new PropertyInfo(); private static PropertyInfo PI_userID = new PropertyInfo(); private static PropertyInfo PI_userSurname1 = new PropertyInfo(); private static PropertyInfo PI_userSurname2 = new PropertyInfo(); private static PropertyInfo PI_userFirstName = new PropertyInfo(); - private static PropertyInfo PI_userTypeName = new PropertyInfo(); + private static PropertyInfo PI_userRole = new PropertyInfo(); @SuppressWarnings("unused") private static PropertyInfo[] PI_PROP_ARRAY = { PI_userCode, - PI_userTypeCode, PI_wsKey, PI_userID, PI_userSurname1, PI_userSurname2, PI_userFirstName, - PI_userTypeName + PI_userRole }; /** @@ -99,15 +95,14 @@ public class User implements KvmSerializable { * @param userFirstName User name. * @param userTypeName Name of user type. */ - public User(String userCode, String userTypeCode, String wsKey, String userID, String userSurname1, String userSurname2, String userFirstName, String userTypeName) { + public User(String userCode, String wsKey, String userID, String userSurname1, String userSurname2, String userFirstName, int userRole) { User.userCode = userCode; - User.userTypeCode = userTypeCode; User.wsKey = wsKey; User.userID = userID; User.userSurname1 = userSurname1; User.userSurname2 = userSurname2; User.userFirstName = userFirstName; - User.userTypeName = userTypeName; + User.userRole = userRole; } /** @@ -189,39 +184,7 @@ public class User implements KvmSerializable { public static void setUserSurname2(String userSurname2) { User.userSurname2 = userSurname2; } - - /** - * Gets Code of user type. - * @return Code of user type. - */ - public static String getUserTypeCode() { - return userTypeCode; - } - - /** - * Sets Code of user type. - * @param userTypeCode Code of user type. - */ - public static void setUserTypeCode(String userTypeCode) { - User.userTypeCode = userTypeCode; - } - - /** - * Gets Name of user type. - * @return Name of user type. - */ - public static String getUserTypeName() { - return userTypeName; - } - - /** - * Sets Name of user type. - * @param userTypeName Name of user type. - */ - public static void setUserTypeName(String userTypeName) { - User.userTypeName = userTypeName; - } - + /** * Gets Webservices session key. * @return Webservices session key. @@ -237,26 +200,42 @@ public class User implements KvmSerializable { public static void setWsKey(String wsKey) { User.wsKey = wsKey; } + + /** + * Gets user role + * @return user role 1:guest 2:student 3:teacher + * */ + public static int getUserRole(){ + return userRole; + } + /** + * Sets user role + * @param userRole + * */ + //TODO check userRole is 1,2,3 + public static void setUserRole(int userRole){ + User.userRole = userRole; + } + public Object getProperty(int param) { Object object = null; switch(param) { case 0 : object = userCode;break; - case 1 : object = userTypeCode;break; - case 2 : object = wsKey;break; - case 3 : object = userID;break; - case 4 : object = userSurname1;break; - case 5 : object = userSurname2;break; - case 6 : object = userFirstName;break; - case 7 : object = userTypeName;break; + case 1 : object = wsKey;break; + case 2 : object = userID;break; + case 3 : object = userSurname1;break; + case 4 : object = userSurname2;break; + case 5 : object = userFirstName;break; + case 6 : object = userRole; break; } return object; } public int getPropertyCount() { - return 8; + return 7; } public void getPropertyInfo(int param, @SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo propertyInfo) { @@ -267,32 +246,28 @@ public class User implements KvmSerializable { break; case 1: propertyInfo.type = PropertyInfo.STRING_CLASS; - propertyInfo.name = "userTypeCode"; + propertyInfo.name = "wsKey"; break; case 2: propertyInfo.type = PropertyInfo.STRING_CLASS; - propertyInfo.name = "wsKey"; + propertyInfo.name = "userID"; break; case 3: - propertyInfo.type = PropertyInfo.STRING_CLASS; - propertyInfo.name = "userID"; - break; - case 4: propertyInfo.type = PropertyInfo.STRING_CLASS; propertyInfo.name = "userSurname1"; break; - case 5: + case 4: propertyInfo.type = PropertyInfo.STRING_CLASS; propertyInfo.name = "userSurname2"; break; - case 6: + case 5: propertyInfo.type = PropertyInfo.STRING_CLASS; propertyInfo.name = "userFirstName"; - break; - case 7: - propertyInfo.type = PropertyInfo.STRING_CLASS; - propertyInfo.name = "userTypeName"; - break; + break; + case 6: + propertyInfo.type = PropertyInfo.INTEGER_CLASS; + propertyInfo.name = "userRole"; + break; } } @@ -300,13 +275,12 @@ public class User implements KvmSerializable { switch(param) { case 0 : userCode = (String)obj; break; - case 1 : userTypeCode = (String)obj; break; case 2 : wsKey = (String)obj; break; case 3 : userID = (String)obj; break; case 4 : userSurname1 = (String)obj; break; case 5 : userSurname2 = (String)obj; break; case 6 : userFirstName = (String)obj; break; - case 7 : userTypeName = (String)obj; break; + case 8 : userRole = (Integer)obj; break; } } diff --git a/SWADroid/src/es/ugr/swad/swadroid/modules/Courses.java b/SWADroid/src/es/ugr/swad/swadroid/modules/Courses.java index 7f15beb6..5c05a7fe 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/modules/Courses.java +++ b/SWADroid/src/es/ugr/swad/swadroid/modules/Courses.java @@ -122,16 +122,17 @@ public class Courses extends Module { int csSize = soap.getPropertyCount(); for (int i = 0; i < csSize; i++) { SoapObject pii = (SoapObject)soap.getProperty(i); - long id = Long.parseLong(pii.getProperty(0).toString()); - String name = pii.getProperty(1).toString(); - Course c = new Course(id, name); + long id = Long.parseLong(pii.getProperty("courseCode").toString()); + String name = pii.getProperty("courseName").toString(); + int userRole = Integer.parseInt(pii.getProperty("userRole").toString()); + Course c = new Course(id, name, userRole); coursesSWAD.add(c); /*if(isDebuggable) Log.d(TAG, c.toString());*/ } - Log.i(TAG, "Retrieved " + csSize + " courses"); + Log.i(TAG, "Retrieved " + csSize + " courses"); //Obtain old unregistered courses obsoleteCourses.addAll(coursesDB); diff --git a/SWADroid/src/es/ugr/swad/swadroid/modules/Login.java b/SWADroid/src/es/ugr/swad/swadroid/modules/Login.java index a7c8b238..38e61142 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/modules/Login.java +++ b/SWADroid/src/es/ugr/swad/swadroid/modules/Login.java @@ -20,6 +20,7 @@ package es.ugr.swad.swadroid.modules; import android.os.Bundle; +import android.util.Log; import es.ugr.swad.swadroid.Base64; import es.ugr.swad.swadroid.Global; import es.ugr.swad.swadroid.R; @@ -30,6 +31,7 @@ import java.security.NoSuchAlgorithmException; import org.ksoap2.SoapFault; import org.ksoap2.serialization.KvmSerializable; +import org.ksoap2.serialization.SoapObject; import org.xmlpull.v1.XmlPullParserException; /** @@ -108,26 +110,26 @@ public class Login extends Module { md.update(prefs.getUserPassword().getBytes()); userPassword = new String(Base64.encodeBytes(md.digest())); userPassword = userPassword.replace('+','-').replace('/','_').replace('=', ' ').replaceAll("\\s+", "").trim(); - + Log.i("Login", "pre send login"); //Creates webservice request, adds required params and sends request to webservice createRequest(); addParam("userID", prefs.getUserID()); addParam("userPassword", userPassword); addParam("appKey", Global.getAppKey()); sendRequest(User.class, true); - + Log.i("Login", "sended login"); if (result != null) { - KvmSerializable ks = (KvmSerializable) result; + SoapObject soap = (SoapObject) result; //Stores user data returned by webservice response - User.setUserCode(ks.getProperty(0).toString()); - User.setUserTypeCode(ks.getProperty(1).toString()); - User.setWsKey(ks.getProperty(2).toString()); - User.setUserID(ks.getProperty(3).toString()); - User.setUserSurname1(ks.getProperty(4).toString()); - User.setUserSurname2(ks.getProperty(5).toString()); - User.setUserFirstName(ks.getProperty(6).toString()); - User.setUserTypeName(ks.getProperty(7).toString()); + User.setUserCode(soap.getProperty("userCode").toString()); + User.setWsKey(soap.getProperty("wsKey").toString()); + User.setUserID(soap.getProperty("userID").toString()); + User.setUserSurname1(soap.getProperty("userSurname1").toString()); + User.setUserSurname2(soap.getProperty("userSurname2").toString()); + User.setUserFirstName(soap.getProperty("userFirstname").toString()); + int userRole =Integer.parseInt(soap.getProperty("userRole").toString()); + User.setUserRole(userRole); //Update application last login time Global.setLastLoginTime(System.currentTimeMillis()); @@ -136,13 +138,14 @@ public class Login extends Module { /*if(isDebuggable) { Log.d(TAG, "userCode=" + User.getUserCode()); - Log.d(TAG, "userTypeCode=" + User.getUserTypeCode()); + //Log.d(TAG, "userTypeCode=" + User.getUserTypeCode()); Log.d(TAG, "wsKey=" + User.getWsKey()); Log.d(TAG, "userID=" + User.getUserID()); Log.d(TAG, "userSurname1=" + User.getUserSurname1()); Log.d(TAG, "userSurname2=" + User.getUserSurname2()); Log.d(TAG, "userFirstName=" + User.getUserFirstName()); - Log.d(TAG, "userTypeName=" + User.getUserTypeName()); + //Log.d(TAG, "userTypeName=" + User.getUserTypeName()); + Log.d(TAG, "userRole=" + String.valueOf(User.getUserRole())); Log.d(TAG, "lastLoginTime=" + Global.getLastLoginTime()); }*/ diff --git a/SWADroid/src/es/ugr/swad/swadroid/modules/Notices.java b/SWADroid/src/es/ugr/swad/swadroid/modules/Notices.java new file mode 100644 index 00000000..85a33cc4 --- /dev/null +++ b/SWADroid/src/es/ugr/swad/swadroid/modules/Notices.java @@ -0,0 +1,303 @@ +/* + * This file is part of SWADroid. + * + * Copyright (C) 2010 Juan Miguel Boyero Corral + * + * SWADroid 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, either version 3 of the License, or + * (at your option) any later version. + * + * SWADroid 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 SWADroid. If not, see . + */ +package es.ugr.swad.swadroid.modules; + +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.util.List; + +import org.ksoap2.SoapFault; +import org.xmlpull.v1.XmlPullParserException; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.database.Cursor; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup.LayoutParams; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; +import es.ugr.swad.swadroid.Global; +import es.ugr.swad.swadroid.Preferences; +import es.ugr.swad.swadroid.R; +import es.ugr.swad.swadroid.model.Course; +import es.ugr.swad.swadroid.model.Model; +import es.ugr.swad.swadroid.model.User; + +/** + * Module for send messages. + * @author Helena Rodriguez Gijon + */ +public class Notices extends Module { + /** + * Messages tag name for Logcat + */ + public static final String TAG = Global.APP_TAG + " Notice"; + /** + * Course code + */ + private Long courseCode; + /** + * Notice's body + */ + private String body; + private Dialog noticeDialog; + + /** + * Application preferences. + */ + protected static Preferences prefs = new Preferences(); + + /** + * Cursor for database access + */ + private Cursor dbCursor; + + /** + * User courses list + */ + private ListlistCourses; + /** + * Selected course code + */ + private long selectedCourseCode = 0; + + private OnClickListener positiveClickListener = new OnClickListener() { + + public void onClick(View v) { + if(isDebuggable) { + Log.i(TAG, "on click positive before send request to server"); + } + + try { + /*if(isDebuggable) { + Log.i(TAG, "selectedCourseCode = " + Long.toString(courseCode)); + }*/ + + runConnection(); + } catch (Exception ex) { + String errorMsg = getString(R.string.errorServerResponseMsg); + error(errorMsg); + + if(isDebuggable) { + Log.e(ex.getClass().getSimpleName(), errorMsg); + ex.printStackTrace(); + } + } + } + }; + + private OnClickListener negativeClickListener = new OnClickListener() { + public void onClick(View v) { + finish(); + } + }; + + private void launchNoticeDialog(){ + + noticeDialog = new Dialog(this); + Button acceptButton, cancelButton; + + //Course selectedCourse = (Course)listCourses.get(selectedCourseCode); + //String selectedCourseName = selectedCourse.getName(); + + noticeDialog.setTitle(R.string.noticesModuleLabel); + //TODO noticeDialog.setTitle(R.string.noticeModuleLabel + listCourses.get(selectedCourseCode)); + noticeDialog.setContentView(R.layout.notice_dialog); + noticeDialog.setCancelable(true); + + noticeDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); + + acceptButton = (Button) noticeDialog.findViewById(R.id.notice_button_accept); + acceptButton.setOnClickListener(positiveClickListener); + + cancelButton = (Button) noticeDialog.findViewById(R.id.notice_button_cancel); + cancelButton.setOnClickListener(negativeClickListener); + + noticeDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { + + public void onCancel(DialogInterface dialog) { + setResult(RESULT_CANCELED); + finish(); + } + }); + noticeDialog.show(); + + } + @Override + protected void requestService() throws NoSuchAlgorithmException, + IOException, XmlPullParserException, SoapFault, + IllegalAccessException, InstantiationException { + + readData(); + + createRequest(); + + addParam("wsKey",User.getWsKey()); + addParam("courseCode",(int)selectedCourseCode); + addParam("body",body); + + sendRequest(User.class,false); + + setResult(RESULT_OK); + } + + @Override + protected void connect() { + String progressDescription = getString(R.string.noticesModuleLabel); + int progressTitle = R.string.noticesModuleLabel; + + new Connect(false, progressDescription, progressTitle).execute(); + + Toast.makeText(this, R.string.publishingNotice, Toast.LENGTH_SHORT).show(); + Log.i(TAG, getString(R.string.publishingNotice)); + + } + + @Override + protected void postConnect() { + String noticeSended = getString(R.string.noticePublished); + Toast.makeText(this, noticeSended, Toast.LENGTH_LONG).show(); + Log.i(TAG, noticeSended); + finish(); + } + + private void readData() { + EditText bd = (EditText) noticeDialog.findViewById(R.id.notice_body_text); + body = bd.getText().toString(); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setMETHOD_NAME("sendNotice"); + } + + @Override + protected void onPause() { + super.onPause(); + //noticeDialog.dismiss(); + } + + @Override + protected void onStart() { + Intent activity; + + super.onStart(); + prefs.getPreferences(getBaseContext()); + activity = new Intent(getBaseContext(), Courses.class ); + Toast.makeText(getBaseContext(), R.string.coursesProgressDescription, Toast.LENGTH_LONG).show(); + startActivityForResult(activity,Global.COURSES_REQUEST_CODE); + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + int lastCourseSelected; + + super.onActivityResult(requestCode, resultCode, data); + if(resultCode == Activity.RESULT_OK){ + switch(requestCode){ + //After get the list of courses, a dialog is launched to choice the course + case Global.COURSES_REQUEST_CODE: + final AlertDialog.Builder coursesDialog = new AlertDialog.Builder(this); + + dbCursor = dbHelper.getDb().getCursor(Global.DB_TABLE_COURSES, "userRole>=3", "name"); + listCourses = dbHelper.getAllRows(Global.DB_TABLE_COURSES, "userRole>=3", "name"); + lastCourseSelected = prefs.getLastCourseSelected(); + coursesDialog.setSingleChoiceItems(dbCursor, lastCourseSelected, "name", new DialogInterface.OnClickListener() { + + public void onClick(DialogInterface dialog, int whichButton) { + Course c = (Course) listCourses.get(whichButton); + selectedCourseCode = c.getId(); + prefs.setLastCourseSelected(whichButton); + + if(isDebuggable){ + Integer s = whichButton; + Log.i(TAG, "singleChoice = " + s.toString()); + } + + } + }); + coursesDialog.setTitle(R.string.selectCourseTitle); + coursesDialog.setPositiveButton(R.string.acceptMsg, new DialogInterface.OnClickListener() { + + public void onClick(DialogInterface dialog, int which) { + try { + if(selectedCourseCode == 0) { + Course c = (Course) listCourses.get(prefs.getLastCourseSelected()); + selectedCourseCode = c.getId(); + } + + if(isDebuggable) { + Log.i(TAG, "selectedCourseCode = " + Long.toString(selectedCourseCode)); + } + dialog.dismiss(); + launchNoticeDialog(); + } catch (Exception ex) { + String errorMsg = getString(R.string.errorServerResponseMsg); + error(errorMsg); + + if(isDebuggable) { + Log.e(ex.getClass().getSimpleName(), errorMsg); + ex.printStackTrace(); + } + } + } + }); + coursesDialog.setNegativeButton(R.string.cancelMsg, new DialogInterface.OnClickListener() { + + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + setResult(RESULT_CANCELED); + finish(); + } + }); + coursesDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { + + public void onCancel(DialogInterface dialog) { + //dialog.cancel(); + setResult(RESULT_CANCELED); + finish(); + + } + }); + coursesDialog.show(); + break; + } + + } else { + setResult(RESULT_CANCELED); + finish(); + } + } + @Override + protected void onError() { + // TODO Auto-generated method stub + + } + + + +} \ No newline at end of file