diff --git a/app/src/main/java/org/communiquons/android/comunic/client/Account.java b/app/src/main/java/org/communiquons/android/comunic/client/Account.java new file mode 100644 index 0000000..464ce3d --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/Account.java @@ -0,0 +1,159 @@ +package org.communiquons.android.comunic.client; + +import android.content.Context; +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; + +import java.util.ArrayList; +import java.util.Objects; + +/** + * Comunic account class + * + * Created by pierre on 10/29/17. + */ + +class Account { + + /** + * Utilities object + */ + private Utilities utils; + + /** + * Tokens list + */ + private ArrayList tokens; + + /** + * Tokens file + */ + private String tokFilename = "login_tokens.json"; + + /** + * Application context + */ + private Context mContext; + + /** + * Account class constructor + * + * @param context Context of the application + */ + Account(Context context){ + mContext = context; + utils = new Utilities(context); + + //Initializate tokens array + tokens = new ArrayList<>(); + + //Debug test + /*ArrayList tokens = new ArrayList<>(); + tokens.add("cXNkZjVxNnM1NGRmNTRxczZkNGZxc2RmNAo"); + tokens.add("cXNkZjVxNnM1cXM4ZDdmODdxczhkZ3FzZGp"); + save_new_tokens(tokens); + Log.v("Account", utils.file_get_content(tokFilename)); + + Log.v("Account", "You are signed in: " + (signed_in() ? "Yes" : "No"));*/ + } + + /** + * Determine whether user is signed in or not + * + * @return True if signed in + */ + boolean signed_in(){ + + //Check if tokens are already loaded + if(tokens.size() < 1){ + if(!load_tokens()) + return false; + } + + return true; + } + + /** + * Try to load tokens in tokens array + * + * @return False in case of failure + */ + private boolean load_tokens(){ + + String tokens_file; + + //Get the tokens file content + tokens_file = utils.file_get_content(tokFilename); + + //Check if there was an error + if(Objects.equals(tokens_file, "")) + return false; + + //Try to parse tokens file + try { + JSONArray tokens_array = new JSONArray(tokens_file); + + //Process login tokens + if(tokens_array.length() == 0) + return false; //No tokens to process + + for (int i = 0; i < tokens_array.length(); i++) { + tokens.add(tokens_array.getString(i)); + } + + + } catch (JSONException e) { + e.printStackTrace(); + return false; + } + + //Success + return true; + } + + /** + * Save new tokens in tokens file + * + * @param toks The array containing the tokens + * @return False in case of failure + */ + private boolean save_new_tokens(ArrayList toks){ + + //Create tokens array + JSONArray tokens = new JSONArray(); + + //Populate tokens array with new tokens + for(int i = 0; i < toks.size(); i++){ + tokens.put(toks.get(i)); + } + + //Convert JSON array into a string + String tokens_string = tokens.toString(); + + //Try to write result to file + if(!utils.file_put_contents(tokFilename, tokens_string)){ + Log.e("Account", "Couldn't save new tokens !"); + return false; + } + + return true; + + } + + /** + * Remove login tokens from device + * @return False in case of failure + */ + private boolean remove_login_tokens(){ + if(!utils.file_put_contents(tokFilename, "[]")) + return false; + + //Create new array + tokens = new ArrayList<>(); + + //Success + return true; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java index 8572eee..d3d8244 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java @@ -5,9 +5,16 @@ import android.os.Bundle; public class MainActivity extends AppCompatActivity { + /** + * Acount object + */ + Account account; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + account = new Account(this); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/Utilities.java b/app/src/main/java/org/communiquons/android/comunic/client/Utilities.java new file mode 100644 index 0000000..ddae22d --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/Utilities.java @@ -0,0 +1,94 @@ +package org.communiquons.android.comunic.client; + +import android.content.Context; +import android.util.Log; + +import java.io.FileInputStream; +import java.io.FileOutputStream; + +/** + * Application utilities + * + * Created by pierre on 10/29/17. + */ + +class Utilities { + + /** + * Application context + */ + private Context mContext; + + /** + * Account class constructor + * + * @param context Context of the application + */ + 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 + */ + 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 + */ + 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/res/xml/backup_scheme.xml b/app/src/main/res/xml/backup_scheme.xml index 79862d5..bacfcec 100644 --- a/app/src/main/res/xml/backup_scheme.xml +++ b/app/src/main/res/xml/backup_scheme.xml @@ -6,4 +6,7 @@ --> + + + \ No newline at end of file