|
|
@ -31,6 +31,7 @@ import android.graphics.BitmapFactory;
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
import android.net.NetworkInfo.State;
|
|
|
|
import android.net.NetworkInfo.State;
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.InputType;
|
|
|
|
import android.text.InputType;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.MotionEvent;
|
|
|
@ -160,6 +161,8 @@ public class AndroidUtilities {
|
|
|
|
value.getClass());
|
|
|
|
value.getClass());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- serialization
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Rips apart a content value into two string arrays, keys and value
|
|
|
|
* Rips apart a content value into two string arrays, keys and value
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -192,20 +195,36 @@ public class AndroidUtilities {
|
|
|
|
public static String contentValuesToSerializedString(ContentValues source) {
|
|
|
|
public static String contentValuesToSerializedString(ContentValues source) {
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
for(Entry<String, Object> entry : source.valueSet()) {
|
|
|
|
for(Entry<String, Object> entry : source.valueSet()) {
|
|
|
|
result.append(entry.getKey().replace(SERIALIZATION_SEPARATOR, SEPARATOR_ESCAPE)).append(
|
|
|
|
addSerialized(result, entry.getKey(), entry.getValue());
|
|
|
|
SERIALIZATION_SEPARATOR);
|
|
|
|
}
|
|
|
|
Object value = entry.getValue();
|
|
|
|
return result.toString();
|
|
|
|
if(value instanceof Integer)
|
|
|
|
}
|
|
|
|
result.append('i').append(value);
|
|
|
|
|
|
|
|
else if(value instanceof Double)
|
|
|
|
/** add serialized helper */
|
|
|
|
result.append('d').append(value);
|
|
|
|
private static void addSerialized(StringBuilder result,
|
|
|
|
else if(value instanceof Long)
|
|
|
|
String key, Object value) {
|
|
|
|
result.append('l').append(value);
|
|
|
|
result.append(key.replace(SERIALIZATION_SEPARATOR, SEPARATOR_ESCAPE)).append(
|
|
|
|
else if(value instanceof String)
|
|
|
|
SERIALIZATION_SEPARATOR);
|
|
|
|
result.append('s').append(value.toString());
|
|
|
|
if(value instanceof Integer)
|
|
|
|
else
|
|
|
|
result.append('i').append(value);
|
|
|
|
throw new UnsupportedOperationException(value.getClass().toString());
|
|
|
|
else if(value instanceof Double)
|
|
|
|
result.append(SERIALIZATION_SEPARATOR);
|
|
|
|
result.append('d').append(value);
|
|
|
|
|
|
|
|
else if(value instanceof Long)
|
|
|
|
|
|
|
|
result.append('l').append(value);
|
|
|
|
|
|
|
|
else if(value instanceof String)
|
|
|
|
|
|
|
|
result.append('s').append(value.toString().replace(SERIALIZATION_SEPARATOR, SEPARATOR_ESCAPE));
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
throw new UnsupportedOperationException(value.getClass().toString());
|
|
|
|
|
|
|
|
result.append(SERIALIZATION_SEPARATOR);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Serializes a {@link android.os.Bundle} into a string
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String bundleToSerializedString(Bundle source) {
|
|
|
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
|
|
|
for(String key : source.keySet()) {
|
|
|
|
|
|
|
|
addSerialized(result, key, source.get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result.toString();
|
|
|
|
return result.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -219,34 +238,80 @@ public class AndroidUtilities {
|
|
|
|
if(string == null)
|
|
|
|
if(string == null)
|
|
|
|
return new ContentValues();
|
|
|
|
return new ContentValues();
|
|
|
|
|
|
|
|
|
|
|
|
String[] pairs = string.split("\\" + SERIALIZATION_SEPARATOR); //$NON-NLS-1$
|
|
|
|
|
|
|
|
ContentValues result = new ContentValues();
|
|
|
|
ContentValues result = new ContentValues();
|
|
|
|
for(int i = 0; i < pairs.length; i += 2) {
|
|
|
|
fromSerialized(string, result, new SerializedPut<ContentValues>() {
|
|
|
|
String key = pairs[i].replaceAll(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR);
|
|
|
|
public void put(ContentValues object, String key, char type, String value) throws NumberFormatException {
|
|
|
|
String value = pairs[i+1].substring(1);
|
|
|
|
switch(type) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
switch(pairs[i+1].charAt(0)) {
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
case 'i':
|
|
|
|
result.put(key, Integer.parseInt(value));
|
|
|
|
object.put(key, Integer.parseInt(value));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'd':
|
|
|
|
result.put(key, Double.parseDouble(value));
|
|
|
|
object.put(key, Double.parseDouble(value));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
case 'l':
|
|
|
|
result.put(key, Long.parseLong(value));
|
|
|
|
object.put(key, Long.parseLong(value));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
case 's':
|
|
|
|
result.put(key, value.replace(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR));
|
|
|
|
object.put(key, value.replace(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Turn {@link android.os.Bundle} into a string
|
|
|
|
|
|
|
|
* @param string
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static Bundle bundleFromSerializedString(String string) {
|
|
|
|
|
|
|
|
if(string == null)
|
|
|
|
|
|
|
|
return new Bundle();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Bundle result = new Bundle();
|
|
|
|
|
|
|
|
fromSerialized(string, result, new SerializedPut<Bundle>() {
|
|
|
|
|
|
|
|
public void put(Bundle object, String key, char type, String value) throws NumberFormatException {
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
|
|
|
|
object.putInt(key, Integer.parseInt(value));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
|
|
|
|
object.putDouble(key, Double.parseDouble(value));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
|
|
|
|
object.putLong(key, Long.parseLong(value));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
|
|
|
|
object.putString(key, value.replace(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface SerializedPut<T> {
|
|
|
|
|
|
|
|
public void put(T object, String key, char type, String value) throws NumberFormatException;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static <T> void fromSerialized(String string, T object, SerializedPut<T> putter) {
|
|
|
|
|
|
|
|
String[] pairs = string.split("\\" + SERIALIZATION_SEPARATOR); //$NON-NLS-1$
|
|
|
|
|
|
|
|
for(int i = 0; i < pairs.length; i += 2) {
|
|
|
|
|
|
|
|
String key = pairs[i].replaceAll(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR);
|
|
|
|
|
|
|
|
String value = pairs[i+1].substring(1);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
putter.put(object, key, pairs[i+1].charAt(0), value);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
// failed parse to number, try to put a string
|
|
|
|
// failed parse to number
|
|
|
|
result.put(key, value);
|
|
|
|
putter.put(object, key, 's', value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Turn ContentValues into a string
|
|
|
|
* Turn ContentValues into a string
|
|
|
|
* @param string
|
|
|
|
* @param string
|
|
|
|