From a78594772a82ea80a822102e997321d2e69d9e64 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 1 Sep 2018 09:21:28 +0200 Subject: [PATCH] Rearranged utilities --- .../client/data/helpers/AccountHelper.java | 20 +- .../client/data/models/ConversationsInfo.java | 5 +- .../comunic/client/data/models/Friend.java | 4 +- .../comunic/client/data/models/GroupInfo.java | 4 +- .../comunic/client/data/models/UserInfo.java | 4 +- .../data/runnables/ImageDownloadRunnable.java | 4 +- .../comunic/client/data/utils/FilesUtils.java | 87 +++++- .../client/data/utils/ImageLoadUtils.java | 4 +- .../client/data/utils/StreamsUtils.java | 63 ++++ .../client/data/utils/StringsUtils.java | 61 ++++ .../comunic/client/data/utils/TimeUtils.java | 77 +++++ .../comunic/client/data/utils/Utilities.java | 293 ------------------ .../ui/activities/CreateAccountActivity.java | 4 +- .../client/ui/activities/LoginActivity.java | 4 +- .../adapters/ConversationMessageAdapter.java | 6 - .../ui/adapters/ConversationsListAdapter.java | 9 +- .../ui/adapters/NotificationsAdapter.java | 12 +- .../client/ui/adapters/PostsAdapter.java | 16 +- .../fragments/AdvancedUserInfoFragment.java | 6 +- .../comunic/client/ui/utils/UiUtils.java | 15 + .../client/ui/views/CountDownView.java | 6 +- 21 files changed, 342 insertions(+), 362 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/utils/StreamsUtils.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/utils/TimeUtils.java delete mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountHelper.java index 2005a12..4861873 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountHelper.java @@ -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 diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/ConversationsInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/ConversationsInfo.java index c299733..72d2b6e 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/ConversationsInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/ConversationsInfo.java @@ -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); } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/Friend.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/Friend.java index f38b1dd..ca5f978 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/Friend.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/Friend.java @@ -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; } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java index 326575e..bec6775 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java @@ -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() { diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/UserInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/UserInfo.java index 8cc32d2..a32879b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/UserInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/UserInfo.java @@ -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()); } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/ImageDownloadRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/ImageDownloadRunnable.java index 37b8075..91d01bb 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/ImageDownloadRunnable.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/ImageDownloadRunnable.java @@ -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(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FilesUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FilesUtils.java index 895a027..63950b5 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FilesUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FilesUtils.java @@ -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; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/ImageLoadUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/ImageLoadUtils.java index ef185f0..1a4e589 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/ImageLoadUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/ImageLoadUtils.java @@ -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); } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StreamsUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StreamsUtils.java new file mode 100644 index 0000000..788b8a0 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StreamsUtils.java @@ -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; + } + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StringsUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StringsUtils.java index 6f66091..6ca7af6 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StringsUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StringsUtils.java @@ -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; + + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/TimeUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/TimeUtils.java new file mode 100644 index 0000000..5a59650 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/TimeUtils.java @@ -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); + } + } +} + diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java deleted file mode 100644 index 6b8daf3..0000000 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java +++ /dev/null @@ -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); - } - } - -} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CreateAccountActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CreateAccountActivity.java index 8d78395..f10b03a 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CreateAccountActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CreateAccountActivity.java @@ -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; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/LoginActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/LoginActivity.java index c314282..fafade8 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/LoginActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/LoginActivity.java @@ -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; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java index 6355cf9..a5baa62 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java @@ -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 diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationsListAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationsListAdapter.java index ca82817..6b7ea5d 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationsListAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationsListAdapter.java @@ -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 { - private Utilities utils; - /** * Class constructor * @@ -38,8 +36,6 @@ public class ConversationsListAdapter extends ArrayAdapter { */ public ConversationsListAdapter(Context context, ArrayList list){ super(context, 0, list); - - utils = new Utilities(context); } @@ -91,7 +87,8 @@ public class ConversationsListAdapter extends ArrayAdapter { //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; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java index 2fa0d38..4d0ac9f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java @@ -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{ - /** - * Utilities - */ - private Utilities mUtils; - /** * Information about the users of the notifications */ @@ -55,8 +50,6 @@ public class NotificationsAdapter extends ArrayAdapter{ //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{ //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; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java index a321dc5..4e59086 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java @@ -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 diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AdvancedUserInfoFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AdvancedUserInfoFragment.java index f3c954e..ef5d6ac 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AdvancedUserInfoFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AdvancedUserInfoFragment.java @@ -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 diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/UiUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/UiUtils.java index 04761bd..9eb2e49 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/UiUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/UiUtils.java @@ -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(); + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/CountDownView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/CountDownView.java index 7e36861..4eea277 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/CountDownView.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/CountDownView.java @@ -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