mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-12-26 13:38:59 +00:00
Rearranged utilities
This commit is contained in:
parent
9bbc82d3a5
commit
a78594772a
@ -8,7 +8,7 @@ import org.communiquons.android.comunic.client.data.enums.LoginResult;
|
||||
import org.communiquons.android.comunic.client.data.models.APIRequest;
|
||||
import org.communiquons.android.comunic.client.data.models.APIResponse;
|
||||
import org.communiquons.android.comunic.client.data.models.NewAccount;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.data.utils.FilesUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@ -28,9 +28,9 @@ import java.util.Objects;
|
||||
public class AccountHelper extends BaseHelper {
|
||||
|
||||
/**
|
||||
* Utilities object
|
||||
* Files utilities object
|
||||
*/
|
||||
private Utilities utils;
|
||||
private FilesUtils mFilesUtils;
|
||||
|
||||
/**
|
||||
* Tokens list
|
||||
@ -40,7 +40,7 @@ public class AccountHelper extends BaseHelper {
|
||||
/**
|
||||
* Tokens file
|
||||
*/
|
||||
private String tokFilename = "login_tokens.json";
|
||||
private static final String tokFilename = "login_tokens.json";
|
||||
|
||||
/**
|
||||
* The name of the userID file
|
||||
@ -55,7 +55,7 @@ public class AccountHelper extends BaseHelper {
|
||||
public AccountHelper(Context context){
|
||||
super(context);
|
||||
|
||||
utils = new Utilities(context);
|
||||
mFilesUtils = new FilesUtils(context);
|
||||
|
||||
//Initialize tokens array
|
||||
tokens = new ArrayList<>();
|
||||
@ -159,7 +159,7 @@ public class AccountHelper extends BaseHelper {
|
||||
public int get_current_user_id(){
|
||||
|
||||
//Get file content
|
||||
String userIDString = utils.file_get_content(USER_ID_FILENAME);
|
||||
String userIDString = mFilesUtils.file_get_content(USER_ID_FILENAME);
|
||||
|
||||
//Convert into an int
|
||||
try {
|
||||
@ -183,7 +183,7 @@ public class AccountHelper extends BaseHelper {
|
||||
*/
|
||||
private boolean save_new_user_id(int id){
|
||||
//Save new file content
|
||||
return utils.file_put_contents(USER_ID_FILENAME, ""+id);
|
||||
return mFilesUtils.file_put_contents(USER_ID_FILENAME, ""+id);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -236,7 +236,7 @@ public class AccountHelper extends BaseHelper {
|
||||
String tokens_file;
|
||||
|
||||
//Get the tokens file content
|
||||
tokens_file = utils.file_get_content(tokFilename);
|
||||
tokens_file = mFilesUtils.file_get_content(tokFilename);
|
||||
|
||||
//Check if there was an error
|
||||
if(Objects.equals(tokens_file, ""))
|
||||
@ -303,7 +303,7 @@ public class AccountHelper extends BaseHelper {
|
||||
String tokens_string = tokens.toString();
|
||||
|
||||
//Try to write result to file
|
||||
if(!utils.file_put_contents(tokFilename, tokens_string)){
|
||||
if(!mFilesUtils.file_put_contents(tokFilename, tokens_string)){
|
||||
Log.e("Account", "Couldn't save new tokens !");
|
||||
return false;
|
||||
}
|
||||
@ -317,7 +317,7 @@ public class AccountHelper extends BaseHelper {
|
||||
* @return False in case of failure
|
||||
*/
|
||||
private boolean remove_login_tokens(){
|
||||
if(!utils.file_put_contents(tokFilename, "[]"))
|
||||
if(!mFilesUtils.file_put_contents(tokFilename, "[]"))
|
||||
return false;
|
||||
|
||||
//Create new array
|
||||
|
@ -1,10 +1,9 @@
|
||||
package org.communiquons.android.comunic.client.data.models;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.utils.ArrayUtils;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.ui.utils.UiUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -242,7 +241,7 @@ public class ConversationsInfo {
|
||||
* @return The displayed name of the conversation
|
||||
*/
|
||||
public String getDisplayName() {
|
||||
return Utilities.prepareStringTextView(displayName);
|
||||
return UiUtils.prepareStringTextView(displayName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,6 @@
|
||||
package org.communiquons.android.comunic.client.data.models;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.data.utils.TimeUtils;
|
||||
|
||||
/**
|
||||
* Friend object
|
||||
@ -119,6 +119,6 @@ public class Friend {
|
||||
* @return True if user is signed in / false else
|
||||
*/
|
||||
public boolean signed_in(){
|
||||
return (Utilities.time()-USER_INACTIVE_AFTER) < last_activity;
|
||||
return (TimeUtils.time()-USER_INACTIVE_AFTER) < last_activity;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import org.communiquons.android.comunic.client.data.enums.GroupRegistrationLevel
|
||||
import org.communiquons.android.comunic.client.data.enums.GroupVisibility;
|
||||
import org.communiquons.android.comunic.client.data.enums.GroupsMembershipLevels;
|
||||
import org.communiquons.android.comunic.client.data.enums.GroupPostsCreationLevel;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.ui.utils.UiUtils;
|
||||
|
||||
/**
|
||||
* Group information base model
|
||||
@ -48,7 +48,7 @@ public class GroupInfo {
|
||||
* @return The name of the group ready to be shown on a TextView
|
||||
*/
|
||||
public String getDisplayName(){
|
||||
return Utilities.prepareStringTextView(getName());
|
||||
return UiUtils.prepareStringTextView(getName());
|
||||
}
|
||||
|
||||
public String getIcon_url() {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package org.communiquons.android.comunic.client.data.models;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.ui.utils.UiUtils;
|
||||
|
||||
/**
|
||||
* This class contains the informations about a single user
|
||||
@ -88,7 +88,7 @@ public class UserInfo {
|
||||
* @return The full name of the user
|
||||
*/
|
||||
public String getDisplayFullName(){
|
||||
return Utilities.prepareStringTextView(getFullName());
|
||||
return UiUtils.prepareStringTextView(getFullName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,8 +3,8 @@ package org.communiquons.android.comunic.client.data.runnables;
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.utils.StreamsUtils;
|
||||
import org.communiquons.android.comunic.client.ui.utils.BitmapUtils;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
@ -73,7 +73,7 @@ class ImageDownloadRunnable implements Runnable {
|
||||
Log.v(TAG, "Downloading image (size: "+img_size+") at " + url);
|
||||
|
||||
//Big images have to written byte per byte
|
||||
Utilities.InputToOutputStream(is, os);
|
||||
StreamsUtils.InputToOutputStream(is, os);
|
||||
|
||||
//Close streams and disconnect
|
||||
is.close();
|
||||
|
@ -2,12 +2,13 @@ package org.communiquons.android.comunic.client.data.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Files utilities
|
||||
@ -18,6 +19,25 @@ import java.util.Random;
|
||||
|
||||
public class FilesUtils {
|
||||
|
||||
/**
|
||||
* Debug tag
|
||||
*/
|
||||
private static final String TAG = FilesUtils.class.getCanonicalName();
|
||||
|
||||
/**
|
||||
* Application context
|
||||
*/
|
||||
private Context mContext;
|
||||
|
||||
/**
|
||||
* Account class constructor
|
||||
*
|
||||
* @param context Context of the application
|
||||
*/
|
||||
public FilesUtils(Context context){
|
||||
mContext = context.getApplicationContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Temporary directory
|
||||
*/
|
||||
@ -64,7 +84,7 @@ public class FilesUtils {
|
||||
|
||||
try {
|
||||
FileOutputStream os = new FileOutputStream(file, false);
|
||||
Utilities.InputToOutputStream(is, os);
|
||||
StreamsUtils.InputToOutputStream(is, os);
|
||||
os.close();
|
||||
|
||||
} catch (java.io.IOException e) {
|
||||
@ -75,4 +95,67 @@ public class FilesUtils {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content of a file
|
||||
*
|
||||
* @param filename the name of the file to get
|
||||
* @return The content of the file
|
||||
*/
|
||||
public String file_get_content(String filename){
|
||||
|
||||
FileInputStream fileInputStream;
|
||||
|
||||
try {
|
||||
fileInputStream = mContext.openFileInput(filename);
|
||||
|
||||
String result = "";
|
||||
int currByte = 0;
|
||||
while(currByte != -1){
|
||||
|
||||
currByte = fileInputStream.read();
|
||||
|
||||
if(currByte != -1)
|
||||
result += (char) currByte;
|
||||
}
|
||||
|
||||
fileInputStream.close();
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e){
|
||||
|
||||
//Print error stack
|
||||
Log.e(TAG, "Couldn't get file content !");
|
||||
e.printStackTrace();
|
||||
|
||||
return ""; //This is a failure
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Write something to a file
|
||||
*
|
||||
* @param filename The name of the file to write
|
||||
* @param content The new content of the file
|
||||
* @return FALSE in case of failure
|
||||
*/
|
||||
public boolean file_put_contents(String filename, String content){
|
||||
|
||||
FileOutputStream fileOutputStream;
|
||||
|
||||
try {
|
||||
fileOutputStream = mContext.openFileOutput(filename, Context.MODE_PRIVATE);
|
||||
fileOutputStream.write(content.getBytes());
|
||||
fileOutputStream.close();
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//Success
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,6 @@ package org.communiquons.android.comunic.client.data.utils;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
@ -33,7 +31,7 @@ public class ImageLoadUtils {
|
||||
* @return The name of the file, composed of characters that can be used in filename
|
||||
*/
|
||||
public static String get_file_name(String url){
|
||||
return Utilities.sha1(url);
|
||||
return StringsUtils.sha1(url);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,63 @@
|
||||
package org.communiquons.android.comunic.client.data.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Streams utilities
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
public class StreamsUtils {
|
||||
|
||||
/**
|
||||
* Transfer all the data coming from an InputStream to an Output Stream
|
||||
*
|
||||
* @param is The Input stream
|
||||
* @param os The output stream
|
||||
*/
|
||||
public static void InputToOutputStream(InputStream is, OutputStream os){
|
||||
|
||||
int read;
|
||||
byte[] bytes = new byte[2048];
|
||||
|
||||
try {
|
||||
while ((read = is.read(bytes)) != -1){
|
||||
os.write(bytes, 0, read);
|
||||
}
|
||||
|
||||
} catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an InputStream into a string
|
||||
*
|
||||
* @param is The input stream
|
||||
* @return The string
|
||||
*/
|
||||
public static String isToString(InputStream is){
|
||||
|
||||
try {
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
String line;
|
||||
|
||||
while((line = bufferedReader.readLine()) != null){
|
||||
stringBuilder.append(line);
|
||||
}
|
||||
bufferedReader.close();
|
||||
|
||||
//Return the result
|
||||
return stringBuilder.toString();
|
||||
|
||||
} catch (IOException e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,13 @@
|
||||
package org.communiquons.android.comunic.client.data.utils;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Patterns;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Formatter;
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
|
||||
@ -77,4 +84,58 @@ public class StringsUtils {
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a specified email address is valid or not
|
||||
*
|
||||
* @param mail The E-Mail address to check
|
||||
* @return True if the mail is valid / false else
|
||||
*/
|
||||
public static boolean isValidMail(CharSequence mail){
|
||||
return !TextUtils.isEmpty(mail) && Patterns.EMAIL_ADDRESS.matcher(mail).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the SHA-1 summary of a given string
|
||||
*
|
||||
* @param source The source string
|
||||
* @return The SHA-1 encoded string
|
||||
*/
|
||||
public static String sha1(String source){
|
||||
|
||||
String sha1;
|
||||
|
||||
try {
|
||||
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
||||
crypt.reset();
|
||||
crypt.update(source.getBytes("UTF-8"));
|
||||
sha1 = byteToHex(crypt.digest());
|
||||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
return sha1;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an array of bytes into a string
|
||||
*
|
||||
* @param bList The list of bytes
|
||||
* @return The result string
|
||||
*/
|
||||
private static String byteToHex(byte[] bList){
|
||||
|
||||
Formatter formatter = new Formatter();
|
||||
|
||||
for(byte b : bList){
|
||||
formatter.format("%02x", b);
|
||||
}
|
||||
|
||||
String result = formatter.toString();
|
||||
formatter.close();
|
||||
return result;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,77 @@
|
||||
package org.communiquons.android.comunic.client.data.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Time utilities
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
public class TimeUtils {
|
||||
|
||||
/**
|
||||
* Get current timestamp
|
||||
*
|
||||
* @return The current timestamp
|
||||
*/
|
||||
public static int time(){
|
||||
Date date = new Date();
|
||||
return (int) Math.ceil(date.getTime()/1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an amount of seconds into a string like "3min" or "10hours"s
|
||||
*
|
||||
* @param time The Time to convert
|
||||
* @return Generated string
|
||||
*/
|
||||
public static String TimeToString(Context context, int time){
|
||||
|
||||
Resources res = context.getResources();
|
||||
|
||||
//Check if the time is inferior to 1 => now
|
||||
if(time < 1)
|
||||
return res.getString(R.string.date_now);
|
||||
|
||||
//Less than one minute
|
||||
else if (time < 60){
|
||||
return time + res.getString(R.string.date_s);
|
||||
}
|
||||
|
||||
//Less than one hour
|
||||
else if (time < 3600){
|
||||
int secs = (int) Math.floor(time / 60);
|
||||
return secs + res.getString(R.string.date_m);
|
||||
}
|
||||
|
||||
//Less than a day
|
||||
else if (time < 86400){
|
||||
int hours = (int) Math.floor(time / 3600);
|
||||
return hours + res.getString(R.string.date_h);
|
||||
}
|
||||
|
||||
//Less than a month
|
||||
else if (time < 2678400){
|
||||
int days = (int) Math.floor(time / 86400);
|
||||
return days + res.getString(days > 1 ? R.string.date_days : R.string.date_day);
|
||||
}
|
||||
|
||||
//Less than a year
|
||||
else if (time < 31536000){
|
||||
int months = (int) Math.floor(time / 2678400);
|
||||
return months + res.getString(months > 1 ? R.string.date_months : R.string.date_month);
|
||||
}
|
||||
|
||||
//A several amount of years
|
||||
else {
|
||||
int years = (int) Math.floor(time / 31536000);
|
||||
return years + res.getString(years > 1 ? R.string.date_years : R.string.date_year);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,293 +0,0 @@
|
||||
package org.communiquons.android.comunic.client.data.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Build;
|
||||
import android.text.Html;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.util.Patterns;
|
||||
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Date;
|
||||
import java.util.Formatter;
|
||||
|
||||
/**
|
||||
* Application utilities
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 10/29/17.
|
||||
*/
|
||||
|
||||
public class Utilities {
|
||||
|
||||
/**
|
||||
* Application context
|
||||
*/
|
||||
private Context mContext;
|
||||
|
||||
/**
|
||||
* Account class constructor
|
||||
*
|
||||
* @param context Context of the application
|
||||
*/
|
||||
public Utilities(Context context){
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content of a file
|
||||
*
|
||||
* @param filename the name of the file to get
|
||||
* @return The content of the file
|
||||
*/
|
||||
public String file_get_content(String filename){
|
||||
|
||||
FileInputStream fileInputStream;
|
||||
|
||||
try {
|
||||
fileInputStream = mContext.openFileInput(filename);
|
||||
|
||||
String result = "";
|
||||
int currByte = 0;
|
||||
while(currByte != -1){
|
||||
|
||||
currByte = fileInputStream.read();
|
||||
|
||||
if(currByte != -1)
|
||||
result += (char) currByte;
|
||||
}
|
||||
|
||||
fileInputStream.close();
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e){
|
||||
|
||||
//Print error stack
|
||||
Log.e("Utilities", "Couldn't get file content !");
|
||||
e.printStackTrace();
|
||||
|
||||
return ""; //This is a failure
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Write something to a file
|
||||
*
|
||||
* @param filename The name of the file to write
|
||||
* @param content The new content of the file
|
||||
* @return FALSE in case of failure
|
||||
*/
|
||||
public boolean file_put_contents(String filename, String content){
|
||||
|
||||
FileOutputStream fileOutputStream;
|
||||
|
||||
try {
|
||||
fileOutputStream = mContext.openFileOutput(filename, Context.MODE_PRIVATE);
|
||||
fileOutputStream.write(content.getBytes());
|
||||
fileOutputStream.close();
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//Success
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a specified email address is valid or not
|
||||
*
|
||||
* @param mail The E-Mail address to check
|
||||
* @return True if the mail is valid / false else
|
||||
*/
|
||||
public static boolean isValidMail(CharSequence mail){
|
||||
return !TextUtils.isEmpty(mail) && Patterns.EMAIL_ADDRESS.matcher(mail).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the SHA-1 summary of a given string
|
||||
*
|
||||
* @param source The source string
|
||||
* @return The SHA-1 encoded string
|
||||
*/
|
||||
public static String sha1(String source){
|
||||
|
||||
String sha1;
|
||||
|
||||
try {
|
||||
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
||||
crypt.reset();
|
||||
crypt.update(source.getBytes("UTF-8"));
|
||||
sha1 = byteToHex(crypt.digest());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (UnsupportedEncodingException e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
return sha1;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an array of bytes into a string
|
||||
*
|
||||
* @param bList The list of bytes
|
||||
* @return The result string
|
||||
*/
|
||||
private static String byteToHex(byte[] bList){
|
||||
|
||||
Formatter formatter = new Formatter();
|
||||
|
||||
for(byte b : bList){
|
||||
formatter.format("%02x", b);
|
||||
}
|
||||
|
||||
String result = formatter.toString();
|
||||
formatter.close();
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer all the data coming from an InputStream to an Output Stream
|
||||
*
|
||||
* @param is The Input stream
|
||||
* @param os The output stream
|
||||
*/
|
||||
public static void InputToOutputStream(InputStream is, OutputStream os){
|
||||
|
||||
int read = 0;
|
||||
byte[] bytes = new byte[2048];
|
||||
|
||||
try {
|
||||
while ((read = is.read(bytes)) != -1){
|
||||
os.write(bytes, 0, read);
|
||||
}
|
||||
|
||||
} catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an InputStream into a string
|
||||
*
|
||||
* @param is The input stream
|
||||
* @return The string
|
||||
*/
|
||||
public static String isToString(InputStream is){
|
||||
|
||||
try {
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
String line;
|
||||
|
||||
while((line = bufferedReader.readLine()) != null){
|
||||
stringBuilder.append(line);
|
||||
}
|
||||
bufferedReader.close();
|
||||
|
||||
//Return the result
|
||||
return stringBuilder.toString();
|
||||
|
||||
} catch (IOException e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare a string sent through the API to be shown in a TextView element
|
||||
*
|
||||
* @param input The string to prepare
|
||||
* @return The string ready to be shown
|
||||
*/
|
||||
public static String prepareStringTextView(String input){
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
return Html.fromHtml(input, Html.FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH).toString();
|
||||
}
|
||||
else
|
||||
return Html.fromHtml(input).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current timestamp
|
||||
*
|
||||
* @return The current timestamp
|
||||
*/
|
||||
public static int time(){
|
||||
Date date = new Date();
|
||||
return (int) Math.ceil(date.getTime()/1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an amount of seconds into a string like "3min" or "10hours"s
|
||||
*
|
||||
* @param time The Time to convert
|
||||
* @return Generated string
|
||||
*/
|
||||
public String timeToString(int time){
|
||||
|
||||
Resources res = mContext.getResources();
|
||||
|
||||
//Check if the time is inferior to 1 => now
|
||||
if(time < 1)
|
||||
return res.getString(R.string.date_now);
|
||||
|
||||
//Less than one minute
|
||||
else if (time < 60){
|
||||
return time + res.getString(R.string.date_s);
|
||||
}
|
||||
|
||||
//Less than one hour
|
||||
else if (time < 3600){
|
||||
int secs = (int) Math.floor(time / 60);
|
||||
return secs + res.getString(R.string.date_m);
|
||||
}
|
||||
|
||||
//Less than a day
|
||||
else if (time < 86400){
|
||||
int hours = (int) Math.floor(time / 3600);
|
||||
return hours + res.getString(R.string.date_h);
|
||||
}
|
||||
|
||||
//Less than a month
|
||||
else if (time < 2678400){
|
||||
int days = (int) Math.floor(time / 86400);
|
||||
return days + res.getString(days > 1 ? R.string.date_days : R.string.date_day);
|
||||
}
|
||||
|
||||
//Less than a year
|
||||
else if (time < 31536000){
|
||||
int months = (int) Math.floor(time / 2678400);
|
||||
return months + res.getString(months > 1 ? R.string.date_months : R.string.date_month);
|
||||
}
|
||||
|
||||
//A several amount of years
|
||||
else {
|
||||
int years = (int) Math.floor(time / 31536000);
|
||||
return years + res.getString(years > 1 ? R.string.date_years : R.string.date_year);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -17,11 +17,11 @@ import android.widget.TextView;
|
||||
|
||||
import org.communiquons.android.comunic.client.BuildConfig;
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
import org.communiquons.android.comunic.client.data.utils.StringsUtils;
|
||||
import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask;
|
||||
import org.communiquons.android.comunic.client.data.enums.CreateAccountResult;
|
||||
import org.communiquons.android.comunic.client.data.helpers.AccountHelper;
|
||||
import org.communiquons.android.comunic.client.data.models.NewAccount;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
|
||||
import static android.os.AsyncTask.Status.FINISHED;
|
||||
|
||||
@ -159,7 +159,7 @@ public class CreateAccountActivity extends AppCompatActivity
|
||||
cancel = true;
|
||||
focusView = focusView != null ? focusView : mEmailAddress;
|
||||
}
|
||||
else if(!Utilities.isValidMail(email)){
|
||||
else if(!StringsUtils.isValidMail(email)){
|
||||
mEmailAddress.setError(getString(R.string.err_invalid_email));
|
||||
cancel = true;
|
||||
focusView = focusView != null ? focusView : mEmailAddress;
|
||||
|
@ -12,11 +12,11 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
import org.communiquons.android.comunic.client.data.utils.StringsUtils;
|
||||
import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask;
|
||||
import org.communiquons.android.comunic.client.data.enums.LoginResult;
|
||||
import org.communiquons.android.comunic.client.data.helpers.APIRequestHelper;
|
||||
import org.communiquons.android.comunic.client.data.helpers.AccountHelper;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.ui.asynctasks.LoginTask;
|
||||
|
||||
/**
|
||||
@ -109,7 +109,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
//Check email address
|
||||
if(login_mail.length() < 3 || !Utilities.isValidMail(login_mail.getText())){
|
||||
if(login_mail.length() < 3 || !StringsUtils.isValidMail(login_mail.getText())){
|
||||
login_mail.setError(getString(R.string.activity_login_err_invalid_email));
|
||||
login_mail.requestFocus();
|
||||
stop = 1;
|
||||
|
@ -4,7 +4,6 @@ import android.content.Context;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@ -15,7 +14,6 @@ import org.communiquons.android.comunic.client.data.arrays.ConversationMessagesL
|
||||
import org.communiquons.android.comunic.client.data.models.ConversationMessage;
|
||||
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
||||
import org.communiquons.android.comunic.client.data.utils.StringsUtils;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.ui.listeners.OnConversationMessageActionsListener;
|
||||
import org.communiquons.android.comunic.client.ui.views.EnlargeableWebImageView;
|
||||
import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage;
|
||||
@ -60,8 +58,6 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter {
|
||||
*/
|
||||
private OnConversationMessageActionsListener mOnConversationMessageActionsListener;
|
||||
|
||||
private Utilities utils;
|
||||
|
||||
/**
|
||||
* Public class constructor
|
||||
*
|
||||
@ -77,8 +73,6 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter {
|
||||
this.mContext = context;
|
||||
this.mList = list;
|
||||
|
||||
utils = new Utilities(mContext);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -12,7 +12,7 @@ import android.widget.TextView;
|
||||
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
import org.communiquons.android.comunic.client.data.models.ConversationsInfo;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.data.utils.TimeUtils;
|
||||
import org.communiquons.android.comunic.client.ui.utils.UiUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -28,8 +28,6 @@ import java.util.ArrayList;
|
||||
|
||||
public class ConversationsListAdapter extends ArrayAdapter<ConversationsInfo> {
|
||||
|
||||
private Utilities utils;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
@ -38,8 +36,6 @@ public class ConversationsListAdapter extends ArrayAdapter<ConversationsInfo> {
|
||||
*/
|
||||
public ConversationsListAdapter(Context context, ArrayList<ConversationsInfo> list){
|
||||
super(context, 0, list);
|
||||
|
||||
utils = new Utilities(context);
|
||||
}
|
||||
|
||||
|
||||
@ -91,7 +87,8 @@ public class ConversationsListAdapter extends ArrayAdapter<ConversationsInfo> {
|
||||
//Update the last activity time of the conversation
|
||||
TextView last_activity = convertView.
|
||||
findViewById(R.id.fragment_conversationslist_item_lastactive);
|
||||
last_activity.setText(utils.timeToString(Utilities.time() - infos.getLast_active()));
|
||||
last_activity.setText(TimeUtils.TimeToString(getContext(),
|
||||
TimeUtils.time() - infos.getLast_active()));
|
||||
|
||||
return convertView;
|
||||
|
||||
|
@ -16,7 +16,7 @@ import org.communiquons.android.comunic.client.data.models.GroupInfo;
|
||||
import org.communiquons.android.comunic.client.data.models.Notif;
|
||||
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
||||
import org.communiquons.android.comunic.client.data.utils.NotifsUtils;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.data.utils.TimeUtils;
|
||||
import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage;
|
||||
|
||||
/**
|
||||
@ -28,11 +28,6 @@ import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage;
|
||||
|
||||
public class NotificationsAdapter extends ArrayAdapter<Notif>{
|
||||
|
||||
/**
|
||||
* Utilities
|
||||
*/
|
||||
private Utilities mUtils;
|
||||
|
||||
/**
|
||||
* Information about the users of the notifications
|
||||
*/
|
||||
@ -55,8 +50,6 @@ public class NotificationsAdapter extends ArrayAdapter<Notif>{
|
||||
//Save users and groups information
|
||||
mUsersInfo = list.getUsersInfo();
|
||||
mGroupsInfo = list.getGroupsInfo();
|
||||
|
||||
mUtils = new Utilities(context);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@ -84,7 +77,8 @@ public class NotificationsAdapter extends ArrayAdapter<Notif>{
|
||||
|
||||
//Update the date of the notification
|
||||
TextView date = convertView.findViewById(R.id.notification_date);
|
||||
date.setText(mUtils.timeToString(Utilities.time() - notif.getTime_create()));
|
||||
date.setText(TimeUtils.TimeToString(getContext(),
|
||||
TimeUtils.time() - notif.getTime_create()));
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ import org.communiquons.android.comunic.client.data.enums.PageType;
|
||||
import org.communiquons.android.comunic.client.data.models.Comment;
|
||||
import org.communiquons.android.comunic.client.data.models.Post;
|
||||
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.data.utils.TimeUtils;
|
||||
import org.communiquons.android.comunic.client.ui.listeners.onPostUpdateListener;
|
||||
import org.communiquons.android.comunic.client.ui.utils.UiUtils;
|
||||
import org.communiquons.android.comunic.client.ui.views.CountDownView;
|
||||
@ -68,12 +68,6 @@ public class PostsAdapter extends BaseRecyclerViewAdapter {
|
||||
*/
|
||||
private boolean mDisplayPostsTarget = true;
|
||||
|
||||
|
||||
/**
|
||||
* Utilities object
|
||||
*/
|
||||
private Utilities mUtils;
|
||||
|
||||
/**
|
||||
* Actions update listener
|
||||
*/
|
||||
@ -92,9 +86,6 @@ public class PostsAdapter extends BaseRecyclerViewAdapter {
|
||||
|
||||
mList = list;
|
||||
|
||||
//Utilities
|
||||
mUtils = new Utilities(getContext());
|
||||
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
@ -276,7 +267,8 @@ public class PostsAdapter extends BaseRecyclerViewAdapter {
|
||||
|
||||
|
||||
//Post date
|
||||
mPostDate.setText(mUtils.timeToString(Utilities.time() - post.getPost_time()));
|
||||
mPostDate.setText(TimeUtils.TimeToString(getContext(),
|
||||
TimeUtils.time() - post.getPost_time()));
|
||||
|
||||
|
||||
//Display post visibility
|
||||
@ -314,7 +306,7 @@ public class PostsAdapter extends BaseRecyclerViewAdapter {
|
||||
|
||||
|
||||
//Set post content
|
||||
mPostContent.setText(Utilities.prepareStringTextView(post.getContent()));
|
||||
mPostContent.setText(UiUtils.prepareStringTextView(post.getContent()));
|
||||
|
||||
|
||||
//Post likes
|
||||
|
@ -12,7 +12,7 @@ import android.widget.TextView;
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
import org.communiquons.android.comunic.client.data.models.AdvancedUserInfo;
|
||||
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.data.utils.TimeUtils;
|
||||
import org.communiquons.android.comunic.client.ui.views.FriendshipStatusButton;
|
||||
import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage;
|
||||
|
||||
@ -93,8 +93,8 @@ public class AdvancedUserInfoFragment extends Fragment {
|
||||
//Apply user information
|
||||
mUserAccountImage.setUser(mAdvancedUserInfo);
|
||||
mUserName.setText(mAdvancedUserInfo.getDisplayFullName());
|
||||
mMemberSinceTarget.setText(new Utilities(getActivity()).timeToString(
|
||||
Utilities.time() - mAdvancedUserInfo.getAccount_creation_time()));
|
||||
mMemberSinceTarget.setText(TimeUtils.TimeToString(getActivity(),
|
||||
TimeUtils.time() - mAdvancedUserInfo.getAccount_creation_time()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -4,6 +4,7 @@ import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.text.Html;
|
||||
import android.util.TypedValue;
|
||||
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
@ -96,4 +97,18 @@ public class UiUtils {
|
||||
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
|
||||
context.getResources().getDisplayMetrics());
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a string sent through the API to be shown in a TextView element
|
||||
*
|
||||
* @param input The string to prepare
|
||||
* @return The string ready to be shown
|
||||
*/
|
||||
public static String prepareStringTextView(String input){
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
return Html.fromHtml(input, Html.FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH).toString();
|
||||
}
|
||||
else
|
||||
return Html.fromHtml(input).toString();
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import android.widget.TextView;
|
||||
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
import org.communiquons.android.comunic.client.data.utils.StringsUtils;
|
||||
import org.communiquons.android.comunic.client.data.utils.Utilities;
|
||||
import org.communiquons.android.comunic.client.data.utils.TimeUtils;
|
||||
import org.communiquons.android.comunic.client.ui.utils.UiUtils;
|
||||
|
||||
/**
|
||||
@ -53,7 +53,7 @@ public class CountDownView extends BaseFrameLayoutView {
|
||||
public void setTime_end(final int time_end) {
|
||||
this.time_end = time_end;
|
||||
|
||||
final int remaining = time_end - Utilities.time();
|
||||
final int remaining = time_end - TimeUtils.time();
|
||||
|
||||
if(mTimer != null)
|
||||
mTimer.cancel();
|
||||
@ -68,7 +68,7 @@ public class CountDownView extends BaseFrameLayoutView {
|
||||
|
||||
@Override
|
||||
public void onTick(long millisUntilFinished) {
|
||||
mTimerView.setText(timeToString(time_end - Utilities.time()));
|
||||
mTimerView.setText(timeToString(time_end - TimeUtils.time()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user