From b20bd7a0225a5dcc885c6274af91f858368bfb20 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 5 Jul 2016 17:21:43 +0200 Subject: [PATCH] Removed useless ChatStorage class --- src/org/linphone/ChatFragment.java | 22 +- src/org/linphone/ChatListFragment.java | 8 +- src/org/linphone/ChatStorage.java | 409 ------------------------- src/org/linphone/LinphoneActivity.java | 86 +++--- src/org/linphone/LinphoneManager.java | 10 - 5 files changed, 62 insertions(+), 473 deletions(-) delete mode 100644 src/org/linphone/ChatStorage.java diff --git a/src/org/linphone/ChatFragment.java b/src/org/linphone/ChatFragment.java index 94f110459..d1e2ede31 100644 --- a/src/org/linphone/ChatFragment.java +++ b/src/org/linphone/ChatFragment.java @@ -742,10 +742,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC chatRoom.sendChatMessage(message); lAddress = chatRoom.getPeerAddress(); - if (LinphoneActivity.isInstanciated()) { - LinphoneActivity.instance().onMessageSent(sipUri, messageToSend); - } - message.setListener(LinphoneManager.getInstance()); if (newChatConversation) { exitNewConversationMode(lAddress.asStringUriOnly()); @@ -819,9 +815,21 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC } private void copyTextMessageToClipboard(int id) { - String msg = LinphoneActivity.instance().getChatStorage().getTextMessageForId(chatRoom, id); - if (msg != null) { - Compatibility.copyTextToClipboard(getActivity(), msg); + LinphoneChatMessage message = null; + for (int i = 0; i < adapter.getCount(); i++) { + LinphoneChatMessage msg = adapter.getItem(i); + if (msg.getStorageId() == id) { + message = msg; + break; + } + } + + String txt = null; + if (message != null) { + txt = message.getText(); + } + if (txt != null) { + Compatibility.copyTextToClipboard(getActivity(), txt); LinphoneActivity.instance().displayCustomToast(getString(R.string.text_copied_to_clipboard), Toast.LENGTH_SHORT); } } diff --git a/src/org/linphone/ChatListFragment.java b/src/org/linphone/ChatListFragment.java index 497281f5f..c303e0554 100644 --- a/src/org/linphone/ChatListFragment.java +++ b/src/org/linphone/ChatListFragment.java @@ -57,7 +57,7 @@ import android.widget.TextView; */ public class ChatListFragment extends Fragment implements OnClickListener, OnItemClickListener { private LayoutInflater mInflater; - private List mConversations, mDrafts; + private List mConversations; private ListView chatList; private TextView noChatHistory; private ImageView edit, selectAll, deselectAll, delete, newDiscussion, cancel, backInCall; @@ -164,7 +164,7 @@ public class ChatListFragment extends Fragment implements OnClickListener, OnIte } private void hideAndDisplayMessageIfNoChat() { - if (mConversations.size() == 0 && mDrafts.size() == 0) { + if (mConversations.size() == 0) { noChatHistory.setVisibility(View.VISIBLE); chatList.setVisibility(View.GONE); edit.setEnabled(false); @@ -179,8 +179,6 @@ public class ChatListFragment extends Fragment implements OnClickListener, OnIte public void refresh() { mConversations = LinphoneActivity.instance().getChatList(); - mDrafts = LinphoneActivity.instance().getDraftChatList(); - mConversations.removeAll(mDrafts); hideAndDisplayMessageIfNoChat(); } @@ -240,8 +238,6 @@ public class ChatListFragment extends Fragment implements OnClickListener, OnIte LinphoneActivity.instance().removeFromChatList(sipUri); mConversations = LinphoneActivity.instance().getChatList(); - mDrafts = LinphoneActivity.instance().getDraftChatList(); - mConversations.removeAll(mDrafts); hideAndDisplayMessageIfNoChat(); return true; } diff --git a/src/org/linphone/ChatStorage.java b/src/org/linphone/ChatStorage.java deleted file mode 100644 index a685620ae..000000000 --- a/src/org/linphone/ChatStorage.java +++ /dev/null @@ -1,409 +0,0 @@ -package org.linphone; -/* -ChatStorage.java -Copyright (C) 2012 Belledonne Communications, Grenoble, France - -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; either version 2 -of the License, or (at your option) any later version. - -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. -*/ -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -import org.linphone.core.LinphoneChatMessage; -import org.linphone.core.LinphoneChatRoom; -import org.linphone.mediastream.Log; - -import android.content.ContentValues; -import android.content.Context; -import android.content.SharedPreferences; -import android.content.pm.PackageManager.NameNotFoundException; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.graphics.Bitmap; -import android.graphics.Bitmap.CompressFormat; -import android.graphics.BitmapFactory; -import android.preference.PreferenceManager; - -/** - * @author Sylvain Berfini - */ -public class ChatStorage { - private static final int INCOMING = 1; - private static final int OUTGOING = 0; - private static final int READ = 1; - private static final int NOT_READ = 0; - - private static ChatStorage instance; - private Context context; - private SQLiteDatabase db; - private boolean useNativeAPI; - private static final String TABLE_NAME = "chat"; - private static final String DRAFT_TABLE_NAME = "chat_draft"; - - public synchronized static final ChatStorage getInstance() { - if (instance == null) - instance = new ChatStorage(LinphoneService.instance().getApplicationContext()); - return instance; - } - - public void restartChatStorage() { - if (instance != null) - instance.close(); - instance = new ChatStorage(LinphoneService.instance().getApplicationContext()); - } - - private boolean isVersionUsingNewChatStorage() { - try { - return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode >= 2200; - } catch (NameNotFoundException e) { - Log.e(e); - } - return true; - } - - private ChatStorage(Context c) { - context = c; - boolean useLinphoneStorage = true; - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(LinphoneService.instance()); - boolean updateNeeded = prefs.getBoolean(c.getString(R.string.pref_first_time_linphone_chat_storage), !LinphonePreferences.instance().isFirstLaunch()); - updateNeeded = updateNeeded && !isVersionUsingNewChatStorage(); - useNativeAPI = useLinphoneStorage && !updateNeeded; - Log.d("Using native API: " + useNativeAPI); - - if (!useNativeAPI) { - ChatHelper chatHelper = new ChatHelper(context); - db = chatHelper.getWritableDatabase(); - } - } - - public void close() { - if (!useNativeAPI) { - db.close(); - } - } - - public void updateMessageStatus(String to, String message, int status) { - if (useNativeAPI) { - return; - } - - String[] whereArgs = { String.valueOf(OUTGOING), to, message }; - Cursor c = db.query(TABLE_NAME, null, "direction LIKE ? AND remoteContact LIKE ? AND message LIKE ?", whereArgs, null, null, "id DESC"); - - String id = null; - if (c.moveToFirst()) { - try { - id = c.getString(c.getColumnIndex("id")); - } catch (Exception e) { - Log.e(e); - } - } - c.close(); - - if (id != null && id.length() > 0) { - int intID = Integer.parseInt(id); - updateMessageStatus(to, intID, status); - } - } - - public void updateMessageStatus(String to, int id, int status) { - if (useNativeAPI) { - return; - } - - ContentValues values = new ContentValues(); - values.put("status", status); - - db.update(TABLE_NAME, values, "id LIKE " + id, null); - } - - public int saveTextMessage(String from, String to, String message, long time) { - if (useNativeAPI) { - return -1; - } - - ContentValues values = new ContentValues(); - if (from.equals("")) { - values.put("localContact", from); - values.put("remoteContact", to); - values.put("direction", OUTGOING); - values.put("read", READ); - values.put("status", LinphoneChatMessage.State.InProgress.toInt()); - } else if (to.equals("")) { - values.put("localContact", to); - values.put("remoteContact", from); - values.put("direction", INCOMING); - values.put("read", NOT_READ); - values.put("status", LinphoneChatMessage.State.Idle.toInt()); - } - values.put("message", message); - values.put("time", time); - return (int) db.insert(TABLE_NAME, null, values); - } - - public int saveImageMessage(String from, String to, Bitmap image, String url, long time) { - if (useNativeAPI) { - return -1; - } - - ContentValues values = new ContentValues(); - if (from.equals("")) { - values.put("localContact", from); - values.put("remoteContact", to); - values.put("direction", OUTGOING); - values.put("read", READ); - values.put("status", LinphoneChatMessage.State.InProgress.toInt()); - } else if (to.equals("")) { - values.put("localContact", to); - values.put("remoteContact", from); - values.put("direction", INCOMING); - values.put("read", NOT_READ); - values.put("status", LinphoneChatMessage.State.Idle.toInt()); - } - values.put("url", url); - - if (image != null) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - image.compress(CompressFormat.JPEG, 100, baos); - values.put("image", baos.toByteArray()); - } - - values.put("time", time); - return (int) db.insert(TABLE_NAME, null, values); - } - - public void saveImage(int id, Bitmap image) { - if (useNativeAPI) { - //Handled before this point - return; - } - - if (image == null) - return; - - ContentValues values = new ContentValues(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - image.compress(CompressFormat.JPEG, 100, baos); - values.put("image", baos.toByteArray()); - - db.update(TABLE_NAME, values, "id LIKE " + id, null); - } - - public int saveDraft(String to, String message) { - if (useNativeAPI) { - //TODO - return -1; - } - - ContentValues values = new ContentValues(); - values.put("remoteContact", to); - values.put("message", message); - return (int) db.insert(DRAFT_TABLE_NAME, null, values); - } - - public void updateDraft(String to, String message) { - if (useNativeAPI) { - //TODO - return; - } - - ContentValues values = new ContentValues(); - values.put("message", message); - - db.update(DRAFT_TABLE_NAME, values, "remoteContact LIKE \"" + to + "\"", null); - } - - public void deleteDraft(String to) { - if (useNativeAPI) { - //TODO - return; - } - - db.delete(DRAFT_TABLE_NAME, "remoteContact LIKE \"" + to + "\"", null); - } - - public String getDraft(String to) { - if (useNativeAPI) { - //TODO - return ""; - } - - Cursor c = db.query(DRAFT_TABLE_NAME, null, "remoteContact LIKE \"" + to + "\"", null, null, null, "id ASC"); - - String message = null; - while (c.moveToNext()) { - try { - message = c.getString(c.getColumnIndex("message")); - } catch (Exception e) { - Log.e(e); - } - } - c.close(); - - return message; - } - - public List getDrafts() { - List drafts = new ArrayList(); - - if (useNativeAPI) { - //TODO - } else { - Cursor c = db.query(DRAFT_TABLE_NAME, null, null, null, null, null, "id ASC"); - - while (c.moveToNext()) { - try { - String to = c.getString(c.getColumnIndex("remoteContact")); - drafts.add(to); - } catch (Exception e) { - Log.e(e); - } - } - c.close(); - } - - return drafts; - } - - public String getTextMessageForId(LinphoneChatRoom chatroom, int id) { - String message = null; - - if (useNativeAPI) { - LinphoneChatMessage msg = getMessage(chatroom, id); - - if (msg != null) { - message = msg.getText(); - } - } else { - Cursor c = db.query(TABLE_NAME, null, "id LIKE " + id, null, null, null, null); - - if (c.moveToFirst()) { - try { - message = c.getString(c.getColumnIndex("message")); - } catch (Exception e) { - Log.e(e); - } - } - c.close(); - } - - return message; - } - - public LinphoneChatMessage getMessage(LinphoneChatRoom chatroom, int id) { - LinphoneChatMessage[] history = chatroom.getHistory(); - for (LinphoneChatMessage msg : history) { - if (msg.getStorageId() == id) { - return msg; - } - } - return null; - } - - public void removeDiscussion(String correspondent) { - LinphoneChatRoom chatroom = LinphoneManager.getLc().getOrCreateChatRoom(correspondent); - chatroom.deleteHistory(); - } - - public ArrayList getChatList() { - ArrayList chatList = new ArrayList(); - - LinphoneChatRoom[] chats = LinphoneManager.getLc().getChatRooms(); - List rooms = new ArrayList(); - - for (LinphoneChatRoom chatroom : chats) { - if (chatroom.getHistorySize() > 0) { - rooms.add(chatroom); - } - } - - if (rooms.size() > 1) { - Collections.sort(rooms, new Comparator() { - @Override - public int compare(LinphoneChatRoom a, LinphoneChatRoom b) { - LinphoneChatMessage[] messagesA = a.getHistory(1); - LinphoneChatMessage[] messagesB = b.getHistory(1); - long atime = messagesA[0].getTime(); - long btime = messagesB[0].getTime(); - - if (atime > btime) - return -1; - else if (btime > atime) - return 1; - else - return 0; - } - }); - } - - for (LinphoneChatRoom chatroom : rooms) { - chatList.add(chatroom.getPeerAddress().asStringUriOnly()); - } - - return chatList; - } - - public void deleteMessage(LinphoneChatRoom chatroom, int id) { - if (useNativeAPI) { - LinphoneChatMessage msg = getMessage(chatroom, id); - if (msg != null){ - chatroom.deleteMessage(msg); - } - } else { - db.delete(TABLE_NAME, "id LIKE " + id, null); - } - } - - public void markMessageAsRead(int id) { - if (!useNativeAPI) { - ContentValues values = new ContentValues(); - values.put("read", READ); - db.update(TABLE_NAME, values, "id LIKE " + id, null); - } - } - - public void markConversationAsRead(LinphoneChatRoom chatroom) { - if (useNativeAPI) { - chatroom.markAsRead(); - } - } - - - class ChatHelper extends SQLiteOpenHelper { - - private static final int DATABASE_VERSION = 15; - private static final String DATABASE_NAME = "linphone-android"; - - ChatHelper(Context context) { - super(context, DATABASE_NAME, null, DATABASE_VERSION); - } - - @Override - public void onCreate(SQLiteDatabase db) { - db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, localContact TEXT NOT NULL, remoteContact TEXT NOT NULL, direction INTEGER, message TEXT, image BLOB, url TEXT, time NUMERIC, read INTEGER, status INTEGER);"); - db.execSQL("CREATE TABLE " + DRAFT_TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, remoteContact TEXT NOT NULL, message TEXT);"); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";"); - db.execSQL("DROP TABLE IF EXISTS " + DRAFT_TABLE_NAME + ";"); - onCreate(db); - } - } -} diff --git a/src/org/linphone/LinphoneActivity.java b/src/org/linphone/LinphoneActivity.java index 3db9c5eeb..de98f146f 100644 --- a/src/org/linphone/LinphoneActivity.java +++ b/src/org/linphone/LinphoneActivity.java @@ -23,6 +23,8 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import org.linphone.LinphoneManager.AddressType; @@ -61,7 +63,6 @@ import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; -import android.graphics.Bitmap; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; @@ -803,43 +804,52 @@ public class LinphoneActivity extends Activity implements OnClickListener, Conta } public List getChatList() { - return getChatStorage().getChatList(); - } + ArrayList chatList = new ArrayList(); - public List getDraftChatList() { - return getChatStorage().getDrafts(); + LinphoneChatRoom[] chats = LinphoneManager.getLc().getChatRooms(); + List rooms = new ArrayList(); + + for (LinphoneChatRoom chatroom : chats) { + if (chatroom.getHistorySize() > 0) { + rooms.add(chatroom); + } + } + + if (rooms.size() > 1) { + Collections.sort(rooms, new Comparator() { + @Override + public int compare(LinphoneChatRoom a, LinphoneChatRoom b) { + LinphoneChatMessage[] messagesA = a.getHistory(1); + LinphoneChatMessage[] messagesB = b.getHistory(1); + long atime = messagesA[0].getTime(); + long btime = messagesB[0].getTime(); + + if (atime > btime) + return -1; + else if (btime > atime) + return 1; + else + return 0; + } + }); + } + + for (LinphoneChatRoom chatroom : rooms) { + chatList.add(chatroom.getPeerAddress().asStringUriOnly()); + } + + return chatList; } public void removeFromChatList(String sipUri) { - getChatStorage().removeDiscussion(sipUri); - } - - public void removeFromDrafts(String sipUri) { - getChatStorage().deleteDraft(sipUri); + LinphoneChatRoom chatroom = LinphoneManager.getLc().getOrCreateChatRoom(sipUri); + chatroom.deleteHistory(); } public void updateMissedChatCount() { displayMissedChats(getUnreadMessageCount()); } - public int onMessageSent(String to, String message) { - getChatStorage().deleteDraft(to); - return getChatStorage().saveTextMessage("", to, message, System.currentTimeMillis()); - } - - public int onMessageSent(String to, Bitmap image, String imageURL) { - getChatStorage().deleteDraft(to); - return getChatStorage().saveImageMessage("", to, image, imageURL, System.currentTimeMillis()); - } - - public void onMessageStateChanged(String to, String message, int newState) { - getChatStorage().updateMessageStatus(to, message, newState); - } - - public void onImageMessageStateChanged(String to, int id, int newState) { - getChatStorage().updateMessageStatus(to, id, newState); - } - public void displayMissedCalls(final int missedCallsCount) { if (missedCallsCount > 0) { missedCalls.setText(missedCallsCount + ""); @@ -1041,10 +1051,6 @@ public class LinphoneActivity extends Activity implements OnClickListener, Conta return currentFragment; } - public ChatStorage getChatStorage() { - return ChatStorage.getInstance(); - } - public void addContact(String displayName, String sipUri) { Bundle extras = new Bundle(); @@ -1054,19 +1060,17 @@ public class LinphoneActivity extends Activity implements OnClickListener, Conta public void editContact(LinphoneContact contact) { - Bundle extras = new Bundle(); - extras.putSerializable("Contact", contact); - changeCurrentFragment(FragmentsAvailable.CONTACT_EDITOR, extras); - + Bundle extras = new Bundle(); + extras.putSerializable("Contact", contact); + changeCurrentFragment(FragmentsAvailable.CONTACT_EDITOR, extras); } public void editContact(LinphoneContact contact, String sipAddress) { - - Bundle extras = new Bundle(); - extras.putSerializable("Contact", contact); - extras.putSerializable("NewSipAdress", sipAddress); - changeCurrentFragment(FragmentsAvailable.CONTACT_EDITOR, extras); + Bundle extras = new Bundle(); + extras.putSerializable("Contact", contact); + extras.putSerializable("NewSipAdress", sipAddress); + changeCurrentFragment(FragmentsAvailable.CONTACT_EDITOR, extras); } public void quit() { diff --git a/src/org/linphone/LinphoneManager.java b/src/org/linphone/LinphoneManager.java index c3a27a732..7ee8ffd33 100644 --- a/src/org/linphone/LinphoneManager.java +++ b/src/org/linphone/LinphoneManager.java @@ -796,9 +796,6 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void doDestroy() { - if (LinphoneService.isReady()) // indeed, no need to crash - ChatStorage.getInstance().close(); - BluetoothManager.getInstance().destroy(); try { mTimer.cancel(); @@ -873,13 +870,6 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag LinphoneAddress from = message.getFrom(); String textMessage = message.getText(); - String url = message.getExternalBodyUrl(); - if (textMessage != null && textMessage.length() > 0) { - ChatStorage.getInstance().saveTextMessage(from.asStringUriOnly(), "", textMessage, message.getTime()); - } else if (url != null && url.length() > 0) { - ChatStorage.getInstance().saveImageMessage(from.asStringUriOnly(), "", null, message.getExternalBodyUrl(), message.getTime()); - } - try { LinphoneContact contact = ContactsManager.getInstance().findContactFromAddress(from); if (!mServiceContext.getResources().getBoolean(R.bool.disable_chat_message_notification)) {