From 491e75e81418d7c2fff11ae571903b68b95bed0d Mon Sep 17 00:00:00 2001 From: Pierre Date: Fri, 29 Dec 2017 12:37:11 +0100 Subject: [PATCH] Display conversation names --- .../android/comunic/client/MainActivity.java | 2 +- .../comunic/client/data/DatabaseHelper.java | 15 ++- .../data/UsersInfo/UsersInfosDbHelper.java | 10 +- .../ConversationMessagesDbHelper.java | 6 +- .../data/conversations/ConversationsInfo.java | 3 +- .../ConversationsListDbHelper.java | 81 +++++++++++++- .../ConversationsListHelper.java | 105 ++++++++++++++++++ .../data/friendsList/FriendsListDbHelper.java | 10 +- .../fragments/ConversationFragment.java | 60 +++++++++- .../fragments/ConversationsListFragment.java | 3 +- .../client/fragments/FriendsListFragment.java | 2 +- .../client/fragments/UserInfosFragment.java | 2 +- app/src/main/res/values/strings.xml | 1 + 13 files changed, 277 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java index 990ffe9..76c916c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java @@ -83,7 +83,7 @@ public class MainActivity extends AppCompatActivity } //Initialize DatabaseHelper - dbHelper = new DatabaseHelper(getApplicationContext()); + dbHelper = DatabaseHelper.getInstance(this); //If it is the first time the application is launched, start the user friends tab if(savedInstanceState == null){ diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseHelper.java index ca8d44b..0e3e2b5 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseHelper.java @@ -95,10 +95,21 @@ public class DatabaseHelper extends SQLiteOpenHelper { /** - * Public constructor + * Get the current database helper instance + * + * @param context The context + * @return DatabaseHelper object + */ + public static synchronized DatabaseHelper getInstance(Context context){ + return new DatabaseHelper(context.getApplicationContext()); + } + + /** + * Private constructor + * * @param context The context where the database is used */ - public DatabaseHelper(Context context){ + private DatabaseHelper(Context context){ super(context, DatabaseContract.DATABASE_NAME, null, DatabaseContract.DATABASE_VERSION); } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java index 20ec2a2..eba8ac3 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java @@ -88,7 +88,7 @@ class UsersInfosDbHelper { c.close(); //Close the database - db.close(); + //db.close(); return number_entries > 0; } @@ -115,7 +115,7 @@ class UsersInfosDbHelper { long newRowId = db.insert(UsersInfoSchema.TABLE_NAME, null, newValues); //Close the database - db.close(); + //db.close(); return (int) newRowId; } @@ -187,7 +187,7 @@ class UsersInfosDbHelper { c.close(); //Close the database - db.close(); + //db.close(); return result; } @@ -211,7 +211,7 @@ class UsersInfosDbHelper { int result = db.delete(UsersInfoSchema.TABLE_NAME, condition, conditionArgs); //Close database - db.close(); + //db.close(); return result > 0; } @@ -243,7 +243,7 @@ class UsersInfosDbHelper { //Perform the request int result = db.update(UsersInfoSchema.TABLE_NAME, newValues, conditions, conditionArgs); - db.close(); + //db.close(); return result > 0; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessagesDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessagesDbHelper.java index 9993149..a8be7cc 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessagesDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessagesDbHelper.java @@ -88,7 +88,7 @@ class ConversationMessagesDbHelper { } response.close(); - db.close(); + //db.close(); return message; } @@ -114,7 +114,7 @@ class ConversationMessagesDbHelper { } - db.close(); + //db.close(); return success; } @@ -154,7 +154,7 @@ class ConversationMessagesDbHelper { //Close objects cur.close(); - db.close(); + //db.close(); return list; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java index 43b1e1e..16532b6 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java @@ -1,6 +1,7 @@ package org.communiquons.android.comunic.client.data.conversations; import android.support.annotation.Nullable; +import android.util.Log; import java.util.ArrayList; @@ -91,7 +92,7 @@ public class ConversationsInfo { public void setName(@Nullable String name) { //Check the validity of the name - if(name == "false" || name == "null" || name == null) + if(("false").equals(name) || ("null").equals(name) || name == null) this.name = null; else this.name = name; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListDbHelper.java index 70ab5d6..29cf5ab 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListDbHelper.java @@ -1,8 +1,11 @@ package org.communiquons.android.comunic.client.data.conversations; import android.content.ContentValues; +import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.Log; import org.communiquons.android.comunic.client.data.DatabaseContract.ConversationsListSchema; import org.communiquons.android.comunic.client.data.DatabaseHelper; @@ -58,11 +61,55 @@ public class ConversationsListDbHelper { success = false; } - db.close(); + //db.close(); return success; } + /** + * Get information about a single conversation + * + * @param convID The conversation ID + * @return Information about the conversation (if available locally) or null in case of failure + */ + @Nullable + ConversationsInfo getInfos(int convID){ + SQLiteDatabase db = databaseHelper.getReadableDatabase(); + + //Prepare database request + String table = ConversationsListSchema.TABLE_NAME; + String[] columns = { + ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID, + ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID_OWNER, + ConversationsListSchema.COLUMN_NAME_CONVERSATION_LAST_ACTIVE, + ConversationsListSchema.COLUMN_NAME_CONVERSATION_NAME, + ConversationsListSchema.COLUMN_NAME_CONVERSATION_FOLLOWING, + ConversationsListSchema.COLUMN_NAME_CONVERSATION_SAW_LAST_MESSAGES, + ConversationsListSchema.COLUMN_NAME_CONVERSATION_MEMBERS, + }; + String selection = ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID + " = ?"; + String[] selectionArgs = {""+convID}; + + //Perform database request + Cursor c = db.query(table, columns, selection, selectionArgs, null, null, null); + + ConversationsInfo infos = null; + + //Check for result + if(c.getCount() != 0){ + c.moveToFirst(); + + //Parse result + infos = getConvObj(c); + } + + c.close(); + //db.close(); + + return infos; + + } + /** * Delete all the list of conversations */ @@ -72,7 +119,7 @@ public class ConversationsListDbHelper { //Prepare the request db.delete(TABLE_NAME, null, null); - db.close(); + //db.close(); } /** @@ -117,4 +164,34 @@ public class ConversationsListDbHelper { return values; } + + /** + * Get the conversation object related to a current cursor position + * + * @param c The cursor + * @return The Generated conversation information + */ + private ConversationsInfo getConvObj(Cursor c){ + + ConversationsInfo infos = new ConversationsInfo(); + + //Get the values + infos.setID(c.getInt(c.getColumnIndexOrThrow( + ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID))); + infos.setID_owner(c.getInt(c.getColumnIndexOrThrow( + ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID_OWNER))); + infos.setLast_active(c.getInt(c.getColumnIndexOrThrow( + ConversationsListSchema.COLUMN_NAME_CONVERSATION_LAST_ACTIVE))); + infos.setName(c.getString(c.getColumnIndexOrThrow( + ConversationsListSchema.COLUMN_NAME_CONVERSATION_NAME))); + infos.setFollowing(c.getInt(c.getColumnIndexOrThrow( + ConversationsListSchema.COLUMN_NAME_CONVERSATION_FOLLOWING)) == 1); + infos.setSaw_last_message(c.getInt(c.getColumnIndexOrThrow( + ConversationsListSchema.COLUMN_NAME_CONVERSATION_SAW_LAST_MESSAGES)) == 1); + infos.parseMembersString(c.getString(c.getColumnIndexOrThrow( + ConversationsListSchema.COLUMN_NAME_CONVERSATION_MEMBERS))); + + return infos; + + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java index 30857b4..adf7e49 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java @@ -3,12 +3,15 @@ package org.communiquons.android.comunic.client.data.conversations; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.ArrayMap; import android.util.Log; import org.communiquons.android.comunic.client.api.APIRequest; import org.communiquons.android.comunic.client.api.APIRequestParameters; import org.communiquons.android.comunic.client.api.APIResponse; import org.communiquons.android.comunic.client.data.DatabaseHelper; +import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; +import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -36,6 +39,11 @@ public class ConversationsListHelper { */ private ConversationsListDbHelper convDBHelper; + /** + * Database helper + */ + private DatabaseHelper dbHelper; + /** * The constructor of the class * @@ -45,6 +53,7 @@ public class ConversationsListHelper { public ConversationsListHelper(Context context, DatabaseHelper dbHelper){ mContext = context; convDBHelper = new ConversationsListDbHelper(dbHelper); + this.dbHelper = dbHelper; } /** @@ -67,6 +76,73 @@ public class ConversationsListHelper { return list; } + /** + * Get information about a conversation + * + * @param convID The conversation ID + * @param allowDownload In case the conversation was not found locally, allow informations about + * the conversation to be fetched online + * @return Information about the conversation, or false in case of failure + */ + @Nullable + public ConversationsInfo getInfosSingle(int convID, boolean allowDownload){ + + ConversationsInfo infos; + + //Try to fetch information from the local database + if((infos = convDBHelper.getInfos(convID)) != null) + return infos; + + //Check if we are not allowed to fetch informations online + if(!allowDownload) + return null; + + //Get informations about the conversation online + return downloadSingle(convID); + } + + /** + * Get the display name of a conversation + * + * @param infos Informations about a conversation + * @return The name of the conversation + */ + public String getDisplayName(ConversationsInfo infos){ + + //Check if a specific name has been specified + if(infos.hasName()) + return infos.getName(); + + //Get the list of members of the conversation + ArrayList members = infos.getMembers(); + + //Get information about the users + ArrayMap users = + new GetUsersHelper(mContext, dbHelper).getMultiple(members); + + if(users == null) + return ""; //No name by default + + String name = ""; + + int count = 0; + for(Integer id : users.keySet()){ + if(users.get(id) != null){ + + if(count > 0) + name += ", "; + + name += users.get(id).getFullName(); + count++; + + if(count > 3) + break; + } + } + + return name; + } + /** * Get online (download) the list of all the conversations * @@ -104,6 +180,35 @@ public class ConversationsListHelper { return list; } + /** + * Download informations about a single conversation online + * + * @param convID The ID of the conversation to fetch + * @return Informations about the conversation in case of success / null else + */ + @Nullable + private ConversationsInfo downloadSingle(int convID){ + + //Perform an API request + APIRequestParameters params = new APIRequestParameters(mContext, + "conversations/getInfosOne"); + params.addParameter("conversationID", ""+convID); + + try { + + APIResponse response = new APIRequest().exec(params); + + JSONObject object = response.getJSONObject(); + + return parseConversationJSON(object); + + } catch (Exception e){ + e.printStackTrace(); + return null; + } + + } + /** * Parse a JSONObject into a conversation info element * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java index ff27c69..1d54523 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java @@ -97,7 +97,7 @@ public class FriendsListDbHelper { c.close(); //Close the access to the database - db.close(); + //db.close(); return friendsList; } @@ -123,7 +123,7 @@ public class FriendsListDbHelper { } //Close the database - db.close(); + //db.close(); return success; } @@ -167,7 +167,7 @@ public class FriendsListDbHelper { int result = db.delete(table_name, whereClause, whereArgs); //Close the database - db.close(); + //db.close(); return result; } @@ -190,7 +190,7 @@ public class FriendsListDbHelper { int result = db.delete(table_name, whereClause, whereValues); //Close access to the database - db.close(); + //db.close(); return result > 0; } @@ -216,7 +216,7 @@ public class FriendsListDbHelper { int result = db.update(table, values, whereClause, whereArgs); //Close access to the database - db.close(); + //db.close(); return result > 0; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java index 06da9f3..3a11fea 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java @@ -31,6 +31,8 @@ import org.communiquons.android.comunic.client.data.conversations.ConversationMe import org.communiquons.android.comunic.client.data.conversations.ConversationMessageAdapter; import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable; +import org.communiquons.android.comunic.client.data.conversations.ConversationsInfo; +import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper; import org.communiquons.android.comunic.client.data.utils.BitmapUtils; import java.io.FileNotFoundException; @@ -71,6 +73,11 @@ public class ConversationFragment extends Fragment */ private int conversation_id; + /** + * Information about the conversation + */ + private ConversationsInfo conversationInfo = null; + /** * The last available message id */ @@ -101,6 +108,11 @@ public class ConversationFragment extends Fragment */ private ConversationMessagesHelper convMessHelper; + /** + * Conversation list helper + */ + private ConversationsListHelper convListHelper; + /** * Conversation messages adapter */ @@ -141,11 +153,14 @@ public class ConversationFragment extends Fragment super.onCreate(savedInstanceState); //Database helper - DatabaseHelper dbHelper = new DatabaseHelper(getActivity()); + DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity()); //Set conversation message helper convMessHelper = new ConversationMessagesHelper(getActivity(), dbHelper); + //Set conversation list helper + convListHelper = new ConversationsListHelper(getActivity(), dbHelper); + //Get the conversation ID conversation_id = getArguments().getInt(ARG_CONVERSATION_ID); @@ -156,6 +171,9 @@ public class ConversationFragment extends Fragment throw new RuntimeException(TAG + " requires a valid conversation ID when created !"); } + //Get information about the conversation + + } @Override @@ -238,9 +256,34 @@ public class ConversationFragment extends Fragment //Create and start the thread new Thread(refreshRunnable).start(); + //Update conversation title + getActivity().setTitle(R.string.fragement_conversation_title); + //Update the bottom navigation menu ((MainActivity) getActivity()) .setSelectedNavigationItem(R.id.main_bottom_navigation_conversations); + + //Check for conversation information + if(conversationInfo == null){ + + //Query information about the conversation + new AsyncTask(){ + @Override + protected ConversationsInfo doInBackground(Void... params) { + ConversationsInfo infos = convListHelper.getInfosSingle(conversation_id, true); + if(infos != null) + infos.setDisplayName(convListHelper.getDisplayName(infos)); + return infos; + } + + @Override + protected void onPostExecute(ConversationsInfo conversationsInfo) { + onGotConversationInfos(conversationsInfo); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + else + onGotConversationInfos(conversationInfo); } @Override @@ -319,6 +362,21 @@ public class ConversationFragment extends Fragment convMessAdapter.notifyDataSetChanged(); } + /** + * What to do once we got conversation informations + * + * @param infos Informations about the conversation + */ + private void onGotConversationInfos(ConversationsInfo infos){ + + //Save conversation informations + conversationInfo = infos; + + //Update the name of the conversation + getActivity().setTitle(infos.getDisplayName()); + + } + /** * This method is called when the user request to add an image to a message */ diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java index 1df3c78..548723e 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java @@ -91,7 +91,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O super.onViewCreated(view, savedInstanceState); //Database helper - DatabaseHelper dbHelper = new DatabaseHelper(getActivity()); + DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity()); //Instantiate the user informations helper userHelper = new GetUsersHelper(getActivity(), dbHelper); @@ -240,6 +240,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O if(list == null) { Toast.makeText(getActivity(), R.string.fragment_conversationslist_err_get_list, Toast.LENGTH_LONG).show(); + display_progress_bar(false); return; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java index eaa3d2e..2219a5d 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java @@ -93,7 +93,7 @@ public class FriendsListFragment extends Fragment { mContext = getActivity().getApplicationContext(); //Create database helper - mDbHelper = new DatabaseHelper(mContext); + mDbHelper = DatabaseHelper.getInstance(mContext); //Create friendlist operation object flist = new FriendsList(mDbHelper, mContext); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserInfosFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserInfosFragment.java index 383ff40..0bee1fa 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserInfosFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserInfosFragment.java @@ -57,7 +57,7 @@ public class UserInfosFragment extends Fragment { //Open DBHelper if required if(dbHelper == null) - dbHelper = new DatabaseHelper(mContext); + dbHelper = DatabaseHelper.getInstance(mContext); //Get required views final ImageView imageView = view.findViewById(R.id.fragments_userinfos_account_image); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c8a0c44..33d0e69 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -65,4 +65,5 @@ Friends Conversations About me + Title