diff --git a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java index 1f0ceca..1846875 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java @@ -50,7 +50,7 @@ public class LoginActivity extends AppCompatActivity { aUtils = new AccountUtils(this); //Check for connectivity - if(!APIRequestTask.isAPIavailable(this)){ + if(!APIRequest.isAPIavailable(this)){ Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show(); } @@ -93,7 +93,7 @@ public class LoginActivity extends AppCompatActivity { } //Check internet connection - if(!APIRequestTask.isAPIavailable(this)){ + if(!APIRequest.isAPIavailable(this)){ show_form_error(getString(R.string.err_no_internet_connection)); stop = 1; } 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 7364bb1..ba27656 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 @@ -13,6 +13,7 @@ import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; +import org.communiquons.android.comunic.client.api.APIRequest; import org.communiquons.android.comunic.client.api.APIRequestTask; import org.communiquons.android.comunic.client.data.Account.Account; import org.communiquons.android.comunic.client.data.Account.AccountUtils; @@ -40,15 +41,6 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - //Enable bottom navigation menu - init_bottom_menu(); - - //Check for connectivity - if(!APIRequestTask.isAPIavailable(this)){ - Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show(); - } //Initialize account objects account = new Account(this); @@ -57,6 +49,18 @@ public class MainActivity extends AppCompatActivity { if(!account.signed_in()){ //Open the login activity startActivity(new Intent(this, LoginActivity.class)); + return; + } + + //Set the content of the activity + setContentView(R.layout.activity_main); + + //Enable bottom navigation menu + init_bottom_menu(); + + //Check for connectivity + if(!APIRequest.isAPIavailable(this)){ + Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show(); } //If it is the first time the application is launched, started the user friends tab diff --git a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java new file mode 100644 index 0000000..cb7970b --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java @@ -0,0 +1,149 @@ +package org.communiquons.android.comunic.client.api; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; + +import org.communiquons.android.comunic.client.BuildConfig; +import org.communiquons.android.comunic.client.data.Account.Account; + +import java.io.BufferedOutputStream; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; + +/** + * API request file + * + * Perform an API request + * + * @author Pierre HUBERT + * Created by pierre on 11/12/17. + */ + +public class APIRequest { + + /** + * Public constructor + */ + public APIRequest(){ + + } + + /** + * Perform an API request on the server and return the result as a JSON code + * + * @param parameters The parameters to pass to the server + * @return The result of the request + */ + public APIResponse exec(APIRequestParameters parameters) throws Exception{ + + //Add login tokens + addLoginTokens(parameters); + + APIResponse result = new APIResponse(); + + InputStream is = null; + + try { + + //Determine the URL of the request + URL url = new URL(BuildConfig.api_url + parameters.getRequest_uri()); + + //The request is being performed on an http server + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + //Configure the connection + conn.setReadTimeout(3000); + conn.setConnectTimeout(3000); + conn.setRequestMethod("POST"); + conn.setDoInput(true); + conn.setDoOutput(true); + + + + //Send request parameters + OutputStream out = new BufferedOutputStream(conn.getOutputStream()); + BufferedWriter writer = new BufferedWriter (new OutputStreamWriter(out, "UTF-8")); + writer.write(parameters.get_parameters_encoded()); + writer.flush(); + writer.close(); + out.close(); + + //Connect to the server + conn.connect(); + + //Get response code + result.setResponse_code(conn.getResponseCode()); + + is = conn.getInputStream(); + String response = readIt(is, 5000); + result.setResponse(response); + + conn.disconnect(); + + } finally { + //Close streams + if(is != null) + is.close(); + } + //Return result + return result; + } + + // Reads an InputStream and converts it to a String. + private String readIt(InputStream stream, int len) throws IOException { + Reader reader = new InputStreamReader(stream, "UTF-8"); + char[] buffer = new char[len]; + reader.read(buffer); + return new String(buffer); + } + + /** + * Add the login tokens to an API request object + * + * @param params The parameters of the request to update + */ + private void addLoginTokens(APIRequestParameters params){ + + //Create account object + Account account = new Account(params.getContext()); + + //Check if user is signed in or not + if(!account.signed_in()) + return; //Do nothing + + //Get login tokens + ArrayList tokens = account.getLoginTokens(); + + if(tokens.size() < 2) + return; //Not enough tokens + + //Add them to the request + params.addParameter("userToken1", tokens.get(0)); + params.addParameter("userToken2", tokens.get(1)); + + } + + /** + * Determine it is possible or not to connect to the API now + * + * @param context The context used to perform the test + * @return True if a network connection is available + */ + public static boolean isAPIavailable(Context context){ + ConnectivityManager connMgr = (ConnectivityManager) + context.getSystemService(Context.CONNECTIVITY_SERVICE); + + NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); + + return networkInfo != null && networkInfo.isConnected(); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java index 766e91d..0a19a81 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java @@ -46,8 +46,8 @@ public abstract class APIRequestTask extends AsyncTask tokens = account.getLoginTokens(); - - if(tokens.size() < 2) - return; //Not enough tokens - - //Add them to the request - params.addParameter("userToken1", tokens.get(0)); - params.addParameter("userToken2", tokens.get(1)); - - } }