# HG changeset patch # User "Vincent Veronis" # Date 1293624278 -3600 # Node ID 49e6af1887f78003a845e70e32ea7d4dd8ccb13b # Parent 41d5d97b74fdf8e15f275606e2a47eb010cfeda2 Feature #245 Chat history support in SDCard diff -r 41d5d97b74fd -r 49e6af1887f7 res/layout/preferences.xml --- a/res/layout/preferences.xml Tue Dec 28 11:35:34 2010 +0100 +++ b/res/layout/preferences.xml Wed Dec 29 13:04:38 2010 +0100 @@ -8,6 +8,9 @@ + - + @@ -73,7 +75,8 @@ + android:key="proxy_port" android:numeric="signed" + android:hint="@string/comments_proxy_port" /> Set your incoming message ringtone Chat compact Set the chat windows compact - + History + You need to have SDcard mounted and writable to enable history + Enable/Disable history messages + Subscription accepted Subscription error diff -r 41d5d97b74fd -r 49e6af1887f7 src/com/beem/project/beem/service/BeemChatManager.java --- a/src/com/beem/project/beem/service/BeemChatManager.java Tue Dec 28 11:35:34 2010 +0100 +++ b/src/com/beem/project/beem/service/BeemChatManager.java Wed Dec 29 13:04:38 2010 +0100 @@ -40,7 +40,7 @@ Flavien Astraud, November 26, 2009 Head of the EIP Laboratory. -*/ + */ package com.beem.project.beem.service; import java.util.ArrayList; @@ -61,6 +61,7 @@ import android.preference.PreferenceManager; import android.util.Log; +import com.beem.project.beem.BeemApplication; import com.beem.project.beem.BeemService; import com.beem.project.beem.service.aidl.IChat; import com.beem.project.beem.service.aidl.IChatManager; @@ -78,8 +79,7 @@ private final ChatManager mAdaptee; private final Map mChats = new HashMap(); private final ChatListener mChatListener = new ChatListener(); - private final RemoteCallbackList mRemoteChatCreationListeners = - new RemoteCallbackList(); + private final RemoteCallbackList mRemoteChatCreationListeners = new RemoteCallbackList(); private final BeemService mService; /** @@ -172,6 +172,12 @@ return mChats.get(key); } ChatAdapter res = new ChatAdapter(chat); + boolean history = PreferenceManager.getDefaultSharedPreferences(mService.getBaseContext()).getBoolean( + "settings_key_history", false); + String accountUser = PreferenceManager.getDefaultSharedPreferences(mService.getBaseContext()).getString( + BeemApplication.ACCOUNT_USERNAME_KEY, ""); + res.setHisory(history); + res.setAccountUser(accountUser); Log.d(TAG, "getChat put " + key); mChats.put(key, res); return res; @@ -255,7 +261,7 @@ private PendingIntent makeChatIntent(IChat chat) { Intent chatIntent = new Intent(mService, com.beem.project.beem.ui.Chat.class); chatIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP - | Intent.FLAG_ACTIVITY_NEW_TASK); + | Intent.FLAG_ACTIVITY_NEW_TASK); try { chatIntent.setData(chat.getParticipant().toUri()); } catch (RemoteException e) { @@ -275,7 +281,7 @@ SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mService); try { CharSequence tickerText = mService.getBind().getRoster().getContact(chat.getParticipant().getJID()) - .getName(); + .getName(); Notification notification = new Notification(android.R.drawable.stat_notify_chat, tickerText, System .currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL; @@ -305,6 +311,7 @@ } @Override - public void stateChanged(final IChat chat) { } + public void stateChanged(final IChat chat) { + } } } diff -r 41d5d97b74fd -r 49e6af1887f7 src/com/beem/project/beem/service/ChatAdapter.java --- a/src/com/beem/project/beem/service/ChatAdapter.java Tue Dec 28 11:35:34 2010 +0100 +++ b/src/com/beem/project/beem/service/ChatAdapter.java Wed Dec 29 13:04:38 2010 +0100 @@ -40,18 +40,23 @@ Flavien Astraud, November 26, 2009 Head of the EIP Laboratory. -*/ + */ package com.beem.project.beem.service; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.Collections; import java.util.LinkedList; import java.util.List; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.XMPPException; -import org.jivesoftware.smackx.ChatStateListener; +import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smackx.ChatState; +import org.jivesoftware.smackx.ChatStateListener; +import android.os.Environment; import android.os.RemoteCallbackList; import android.os.RemoteException; import android.util.Log; @@ -74,6 +79,8 @@ private final List mMessages; private final RemoteCallbackList mRemoteListeners = new RemoteCallbackList(); private final MsgListener mMsgListener = new MsgListener(); + private boolean mIsHisory; + private String mAccountUser; /** * Constructor. @@ -115,6 +122,8 @@ // TODO Auto-generated catch block e.printStackTrace(); } + //TODO replace me + saveHistory(message, mAccountUser); } /** @@ -192,20 +201,73 @@ if (mMessages.size() == HISTORY_MAX_SIZE) mMessages.remove(0); mMessages.add(msg); + if (!"".equals(msg.getBody()) && msg.getBody() != null) { + saveHistory(msg, msg.getFrom()); + } } /** + * Save message in SDCard + * @param msg the message receive + */ + public void saveHistory(Message msg, String contactName) { + String state = Environment.getExternalStorageState(); + if (mIsHisory && Environment.MEDIA_MOUNTED.equals(state)) { + File path = new File(Environment.getExternalStorageDirectory(), "beem"); + File filepath; + if (msg.getFrom() == contactName) + filepath = new File(path, StringUtils.parseBareAddress(contactName)); + else + filepath = new File(path,StringUtils.parseBareAddress(msg.getTo())); + path.mkdirs(); + try { + FileWriter file = new FileWriter(filepath, true); + String log = msg.getTimestamp() + " " + contactName + " " + msg.getBody() + System.getProperty("line.separator"); + file.write(log); + file.close(); + Log.i(TAG, log); + } catch(IOException e) { + e.printStackTrace(); + } + } + } + + /** + * set History state + * @param mIsHisory + */ + public void setHisory(boolean isHisory) { + this.mIsHisory = isHisory; + } + + /** + * get History state + * @return + */ + public boolean getHisory() { + return mIsHisory; + } + + public void setAccountUser(String accountUser) { + mAccountUser = accountUser; + } + + public String getAccountUser() { + return mAccountUser; + } + /** * Listener. */ private class MsgListener implements ChatStateListener { /** * Constructor. */ - public MsgListener() { } + public MsgListener() { + } @Override public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) { - Message msg = new Message(message); + Message msg = new Message(message); //TODO add que les message pas de type errors ChatAdapter.this.addMessage(msg); final int n = mRemoteListeners.beginBroadcast(); @@ -241,4 +303,3 @@ } } } - diff -r 41d5d97b74fd -r 49e6af1887f7 src/com/beem/project/beem/ui/Chat.java --- a/src/com/beem/project/beem/ui/Chat.java Tue Dec 28 11:35:34 2010 +0100 +++ b/src/com/beem/project/beem/ui/Chat.java Wed Dec 29 13:04:38 2010 +0100 @@ -40,7 +40,7 @@ Flavien Astraud, November 26, 2009 Head of the EIP Laboratory. -*/ + */ package com.beem.project.beem.ui; import java.text.DateFormat; @@ -179,7 +179,6 @@ sendMessage(); } }); - prepareIconsStatus(); } @@ -801,7 +800,6 @@ /** * Set the Date of the message. - * * @param date date of the message. */ public void setTimestamp(Date date) { @@ -810,7 +808,6 @@ /** * Get the Date of the message. - * * @return if it is a delayed message get the date the message was sended. */ public Date getTimestamp() { diff -r 41d5d97b74fd -r 49e6af1887f7 src/com/beem/project/beem/ui/Settings.java --- a/src/com/beem/project/beem/ui/Settings.java Tue Dec 28 11:35:34 2010 +0100 +++ b/src/com/beem/project/beem/ui/Settings.java Wed Dec 29 13:04:38 2010 +0100 @@ -40,12 +40,14 @@ Flavien Astraud, November 26, 2009 Head of the EIP Laboratory. -*/ + */ package com.beem.project.beem.ui; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; +import android.os.Environment; +import android.preference.CheckBoxPreference; import android.preference.PreferenceActivity; import android.view.Menu; import android.view.MenuInflater; @@ -74,6 +76,15 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.layout.preferences); + CheckBoxPreference history = (CheckBoxPreference) findPreference("settings_key_history"); + String state = Environment.getExternalStorageState(); + if (!Environment.MEDIA_MOUNTED.equals(state)) { + history.setSelectable(false); + history.setSummary(R.string.history_mount); + } else { + history.setSelectable(true); + history.setSummary(R.string.history_on_off); + } } /**