Commit 3b9c4fdb authored by Romain Bertozzi's avatar Romain Bertozzi
Browse files

profile: gather parts and build vcard

This patch gathers all the parts of a given profile. It builds the
final vcard string from these parts and saves it on the disk.

Change-Id: I853caa25005469986d76fad3e892de7d813f0cb0
Tuleap: #856
parent 6334ba9d
......@@ -5,14 +5,17 @@ import android.os.RemoteException;
import android.util.Log;
import java.util.HashMap;
import java.util.Hashtable;
import cx.ring.history.HistoryText;
import cx.ring.model.TextMessage;
import cx.ring.model.SipCall;
import cx.ring.utils.ProfileChunk;
import cx.ring.utils.VCardUtils;
public class CallManagerCallBack extends Callback {
private static final String TAG = "CallManagerCallBack";
private DRingService mService;
private ProfileChunk mProfileChunk;
static public final String CALL_STATE_CHANGED = "call-State-changed";
static public final String INCOMING_CALL = "incoming-call";
......@@ -39,6 +42,10 @@ public class CallManagerCallBack extends Callback {
@Override
public void callStateChanged(String callID, String newState, int detail_code) {
Log.w(TAG, "on_call_state_changed : (" + callID + ", " + newState + ")");
if (newState.equals(SipCall.stateToString(SipCall.State.INCOMING)) ||
newState.equals(SipCall.stateToString(SipCall.State.OVER))) {
this.mProfileChunk = null;
}
Intent intent = new Intent(CALL_STATE_CHANGED);
intent.putExtra("call", callID);
intent.putExtra("state", newState);
......@@ -74,9 +81,38 @@ public class CallManagerCallBack extends Callback {
public void incomingMessage(String call_id, String from, StringMap messages) {
String msg = null;
final String textPlainMime = "text/plain";
if (null != messages && messages.has_key(textPlainMime)) {
msg = messages.getRaw(textPlainMime).toJavaString();
final String ringProfileVCardMime = "x-ring/ring.profile.vcard";
if (messages != null) {
Hashtable<String,String> messageKeyValue = VCardUtils.parseMimeAttributes(messages);
if (messageKeyValue.containsKey(VCardUtils.VCARD_KEY_MIME_TYPE) &&
messageKeyValue.get(VCardUtils.VCARD_KEY_MIME_TYPE).equals(textPlainMime)) {
msg = messages.getRaw(textPlainMime).toJavaString();
}
else if (messageKeyValue.containsKey(VCardUtils.VCARD_KEY_MIME_TYPE) &&
messageKeyValue.get(VCardUtils.VCARD_KEY_MIME_TYPE).equals(ringProfileVCardMime)) {
int part = Integer.parseInt(messageKeyValue.get(VCardUtils.VCARD_KEY_PART));
int nbPart = Integer.parseInt(messageKeyValue.get(VCardUtils.VCARD_KEY_OF));
if (mProfileChunk == null) {
mProfileChunk = new ProfileChunk(nbPart);
}
if (messages.keys() != null && messages.keys().size() > 0) {
String content = messages.getRaw(messages.keys().get(0)).toJavaString();
mProfileChunk.addPartAtIndex(content, part);
}
if (mProfileChunk.isProfileComplete()) {
Log.d(TAG, "Complete Profile: " + mProfileChunk.getCompleteProfile());
String splitFrom[] = from.split("@");
if (splitFrom.length == 2) {
String filename = splitFrom[0] + ".vcf";
VCardUtils.saveToDisk(mProfileChunk.getCompleteProfile(),
filename,
this.mService.getApplicationContext());
}
}
}
}
if (msg == null)
return;
......
/*
* Copyright (C) 2004-2016 Savoir-faire Linux Inc.
*
* Author: Romain Bertozzi <romain.bertozzi@savoirfairelinux.com>
*
* 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 3 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package cx.ring.utils;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import cx.ring.service.StringVect;
public class ProfileChunk {
public final static String TAG = ProfileChunk.class.getSimpleName();
private long mNumberOfParts;
private long mInsertedParts;
private StringVect mParts;
/**
* Constructor
* @param numberOfParts Number of part to complete the Profile
*/
public ProfileChunk(long numberOfParts) {
Log.d(TAG, "Create ProfileChink of size " + numberOfParts);
this.mInsertedParts = 0;
this.mNumberOfParts = numberOfParts;
this.mParts = new StringVect(mNumberOfParts + 1);
}
/**
* Inserts a profile part in the data structure, at a given position
* @param part the part to insert
* @param index the given position to insert the part
*/
public void addPartAtIndex(@NonNull String part, int index) {
this.mParts.set(index, part);
this.mInsertedParts++;
Log.d(TAG, "Inserting part " + part + " at index " + index);
}
/**
* Tells if the profile is complete: all the needed parts have been gathered
* @return true if complete, false otherwise
*/
public boolean isProfileComplete() {
return this.mInsertedParts == this.mNumberOfParts;
}
/**
* Builds the profile based on the gathered parts.
* @return the complete profile as a String
*/
@Nullable
public String getCompleteProfile() {
if (this.isProfileComplete()) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < this.mParts.size(); ++i) {
stringBuilder.append(this.mParts.get(i));
}
return stringBuilder.toString();
}
else {
return null;
}
}
}
/*
* Copyright (C) 2004-2016 Savoir-faire Linux Inc.
*
* Author: Romain Bertozzi <romain.bertozzi@savoirfairelinux.com>
*
* 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 3 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package cx.ring.utils;
import android.content.Context;
import android.text.TextUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Hashtable;
import cx.ring.service.StringMap;
public class VCardUtils {
public static final String VCARD_KEY_MIME_TYPE = "mimeType";
public static final String VCARD_KEY_PART = "part";
public static final String VCARD_KEY_OF = "of";
/**
* Parse the "stringmap" of the mime attributes to build a proper hashtable
* @param stringMap the mimetype as returned by the daemon
* @return a correct hashtable, null if invalid input
*/
public static Hashtable<String, String> parseMimeAttributes(StringMap stringMap) {
if (stringMap == null || stringMap.empty()) {
return null;
}
Hashtable<String, String> messageKeyValue = new Hashtable<>();
String origin = stringMap.keys().toString().replace("[","");
origin = origin.replace("]","");
String elements[] = origin.split(";");
if (elements.length < 2) {
return messageKeyValue;
}
messageKeyValue.put(VCARD_KEY_MIME_TYPE, elements[0]);
String pairs[] = elements[1].split(",");
for (String pair : pairs) {
String kv[] = pair.split("=");
messageKeyValue.put(kv[0].trim(), kv[1]);
}
return messageKeyValue;
}
/**
* Saves a vcard string to an internal new vcf file.
* @param vcard the string to save
* @param filename the filename of the vcf
* @param context the context used to open streams.
*/
public static void saveToDisk(String vcard, String filename, Context context) {
if (TextUtils.isEmpty(vcard) || TextUtils.isEmpty(filename) || context == null) {
return;
}
String path = context.getFilesDir().getAbsolutePath() + File.separator + "peer_profiles";
File peerProfilesFile = new File(path);
if (!peerProfilesFile.exists())
peerProfilesFile.mkdirs();
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(path+"/"+filename);
outputStream.write(vcard.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment