Feature #245
author"Vincent Veronis"
Wed, 29 Dec 2010 13:04:38 +0100
changeset 832 49e6af1887f7
parent 831 41d5d97b74fd
child 833 97a429f938ae
Feature #245 Chat history support in SDCard
res/layout/preferences.xml
res/values/strings.xml
src/com/beem/project/beem/service/BeemChatManager.java
src/com/beem/project/beem/service/ChatAdapter.java
src/com/beem/project/beem/ui/Chat.java
src/com/beem/project/beem/ui/Settings.java
--- 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 @@
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
 	android:shouldDisableView="true" android:selectable="true">
 	<PreferenceCategory android:title="@string/general_preferences">
+		<CheckBoxPreference android:id="@+id/settings_key_history"
+			android:title="@string/history" android:defaultValue="false"
+			android:key="settings_key_history" />
 		<PreferenceScreen android:key="contact_list"
 			android:title="@string/contact_list_preferences" android:summary="@string/contact_list_preferences_sum">
 			<CheckBoxPreference android:title="@string/CLP_hidden_contact"
@@ -31,10 +34,9 @@
 			<CheckBoxPreference android:title="@string/notification_enable_vibrate_title"
 				android:defaultValue="false" android:summary="@string/notification_enable_vibrate_sum"
 				android:key="notification_vibrate" />
-			<RingtonePreference
-				android:title="@string/notification_snd_title" android:key="notification_sound"
-				android:summary="@string/notification_snd_sum" android:ringtoneType="notification"
-				android:showDefault="true" />
+			<RingtonePreference android:title="@string/notification_snd_title"
+				android:key="notification_sound" android:summary="@string/notification_snd_sum"
+				android:ringtoneType="notification" android:showDefault="true" />
 		</PreferenceScreen>
 	</PreferenceCategory>
 	<PreferenceCategory android:title="@string/user_preferences">
@@ -73,7 +75,8 @@
 				<EditTextPreference android:singleLine="true"
 					android:dependency="proxy_use" android:name="port"
 					android:summary="@string/SettingsProxyPort" android:title="@string/settings_proxy_port"
-					android:key="proxy_port" android:numeric="signed" android:hint="@string/comments_proxy_port" />
+					android:key="proxy_port" android:numeric="signed"
+					android:hint="@string/comments_proxy_port" />
 			</PreferenceCategory>
 			<PreferenceCategory android:title="@string/proxy_user_settings">
 				<EditTextPreference android:singleLine="true"
--- a/res/values/strings.xml	Tue Dec 28 11:35:34 2010 +0100
+++ b/res/values/strings.xml	Wed Dec 29 13:04:38 2010 +0100
@@ -119,7 +119,10 @@
 	<string name="notification_snd_sum">Set your incoming message ringtone</string>
 	<string name="settings_chat_compact">Chat compact</string>
 	<string name="settings_chat_compact_sum">Set the chat windows compact</string>
-	
+	<string name="history">History</string>
+	<string name="history_mount">You need to have SDcard mounted and writable to enable history</string>
+	<string name="history_on_off">Enable/Disable history messages</string>
+
 	<!-- Subscription class -->
 	<string name="SubscriptAccept">Subscription accepted</string>
 	<string name="SubscriptError">Subscription error</string>
--- 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<String, ChatAdapter> mChats = new HashMap<String, ChatAdapter>();
     private final ChatListener mChatListener = new ChatListener();
-    private final RemoteCallbackList<IChatManagerListener> mRemoteChatCreationListeners =
-	new RemoteCallbackList<IChatManagerListener>();
+    private final RemoteCallbackList<IChatManagerListener> mRemoteChatCreationListeners = new RemoteCallbackList<IChatManagerListener>();
     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) {
+	}
     }
 }
--- 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<Message> mMessages;
     private final RemoteCallbackList<IMessageListener> mRemoteListeners = new RemoteCallbackList<IMessageListener>();
     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 @@
 	}
     }
 }
-
--- 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() {
--- 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);
+	}
     }
 
     /**