tab3.java | |
File Size: | 8 kb |
File Type: | java |
package c.test2;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;
/*********************************************************************************************
* Tab3 Class/Activity
* This activity is just a botton that when clicked, connects to the location.txt file on
the compsci02 server and changed the hide location bit from 0 to 1 or 1 to 0.
* executes asyncTask ConnectTask and WriteConnectTask
*********************************************************************************************/
public class Tab3 extends AppCompatActivity {
private String me; //get from login pages
Button btnHide;
String Hide = "Hide Location";
String Show = "Show Location";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab3);
Bundle extras = getIntent().getExtras();
me = extras.getString("user");
btnHide = (Button) findViewById(R.id.btnHide);
new ConnectTask().execute();
/**************************************************************
* btnHide OnClickListener
* sets the Hidetoggle variable to 0 if the button reads
"Hide Location" and sets it to 1 if the button reads
"Show Location". Then it executes the WriteConnectTask
**************************************************************/
btnHide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Hidetoggle;
if(btnHide.getText() == Hide) {
Hidetoggle = "0";
}
else{
Hidetoggle = "1";
}
new WriteConnectTask().execute(Hidetoggle);
}
});
}
/******************************************************************************************
* ConnectTask
* This AsyncTask uses an HttpURLConnection to contact the compsci02 server and read the
data from the location.txt file.
* Has background function and OnPostExecute function
*****************************************************************************************/
public class ConnectTask extends AsyncTask<String, String, String>
{
/**************************************************************
* doInBackground
* Creates a connection with the file on the server and reads
each line one by one until the end of the file or until
if finds the specific user.
**************************************************************/
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
String user[]; //find user by reading line in file
try {
URL url = new URL("http://compsci02.snc.edu/cs460/2016/lewicj/WhereAreYou/locations.txt");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
String line = "";
while ((line = reader.readLine()) != null)
{
user = line.split(";");
if(Objects.equals(user[2], me))
{
return line;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "error";
}
/**************************************************************
* onPostExecute
* the AsyncTask's onPostExecute function connects the
independant thread with the main threads UI. aka this
is where the data gets placed to the screen or where
the program can start a new activity.
* This function checks to see if what the result returned from the
background function is. If the result is not error then it
changes the buttons text to either hide or show depending
on what the file says.
**************************************************************/
@Override
protected void onPostExecute(String line) {
super.onPostExecute(line);
String user[]; //find user by reading line in file
user = line.split(";");
if(!Objects.equals(line, "error")) {
if (Objects.equals(user[3], "1")) {
btnHide.setText(Hide);
} else {
btnHide.setText(Show);
}
}
}
}
/******************************************************************************************
* WriteConnectTask
* This AsyncTask uses an HttpURLConnection to contact the compsci02 server and execute
a php script which then writes to the location.txt file.
* Has background function and OnPostExecute function
*****************************************************************************************/
public class WriteConnectTask extends AsyncTask<String, String, String>
{
/**************************************************************
* doInBackground
* Creates a connection with the file on the server and executes
the locationhide.php script with the parameters sent to it.
**************************************************************/
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
String phpscript = "http://compsci02.snc.edu/cs460/2016/lewicj/WhereAreYou/locationhide.php?hide="+params[0]+"&user="+me;
try {
URL url = new URL(phpscript);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
return params[0];
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return "error";
}
/**************************************************************
* onPostExecute
* This function checks to see if what the result returned from the
background function is. if the result is 1 it changes
button text to hide otherwise it changes it to show.
**************************************************************/
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(Objects.equals(s,"1"))
{
btnHide.setText(Hide);
}
else{
btnHide.setText(Show);
}
}
}
}
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;
/*********************************************************************************************
* Tab3 Class/Activity
* This activity is just a botton that when clicked, connects to the location.txt file on
the compsci02 server and changed the hide location bit from 0 to 1 or 1 to 0.
* executes asyncTask ConnectTask and WriteConnectTask
*********************************************************************************************/
public class Tab3 extends AppCompatActivity {
private String me; //get from login pages
Button btnHide;
String Hide = "Hide Location";
String Show = "Show Location";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab3);
Bundle extras = getIntent().getExtras();
me = extras.getString("user");
btnHide = (Button) findViewById(R.id.btnHide);
new ConnectTask().execute();
/**************************************************************
* btnHide OnClickListener
* sets the Hidetoggle variable to 0 if the button reads
"Hide Location" and sets it to 1 if the button reads
"Show Location". Then it executes the WriteConnectTask
**************************************************************/
btnHide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Hidetoggle;
if(btnHide.getText() == Hide) {
Hidetoggle = "0";
}
else{
Hidetoggle = "1";
}
new WriteConnectTask().execute(Hidetoggle);
}
});
}
/******************************************************************************************
* ConnectTask
* This AsyncTask uses an HttpURLConnection to contact the compsci02 server and read the
data from the location.txt file.
* Has background function and OnPostExecute function
*****************************************************************************************/
public class ConnectTask extends AsyncTask<String, String, String>
{
/**************************************************************
* doInBackground
* Creates a connection with the file on the server and reads
each line one by one until the end of the file or until
if finds the specific user.
**************************************************************/
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
String user[]; //find user by reading line in file
try {
URL url = new URL("http://compsci02.snc.edu/cs460/2016/lewicj/WhereAreYou/locations.txt");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
String line = "";
while ((line = reader.readLine()) != null)
{
user = line.split(";");
if(Objects.equals(user[2], me))
{
return line;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "error";
}
/**************************************************************
* onPostExecute
* the AsyncTask's onPostExecute function connects the
independant thread with the main threads UI. aka this
is where the data gets placed to the screen or where
the program can start a new activity.
* This function checks to see if what the result returned from the
background function is. If the result is not error then it
changes the buttons text to either hide or show depending
on what the file says.
**************************************************************/
@Override
protected void onPostExecute(String line) {
super.onPostExecute(line);
String user[]; //find user by reading line in file
user = line.split(";");
if(!Objects.equals(line, "error")) {
if (Objects.equals(user[3], "1")) {
btnHide.setText(Hide);
} else {
btnHide.setText(Show);
}
}
}
}
/******************************************************************************************
* WriteConnectTask
* This AsyncTask uses an HttpURLConnection to contact the compsci02 server and execute
a php script which then writes to the location.txt file.
* Has background function and OnPostExecute function
*****************************************************************************************/
public class WriteConnectTask extends AsyncTask<String, String, String>
{
/**************************************************************
* doInBackground
* Creates a connection with the file on the server and executes
the locationhide.php script with the parameters sent to it.
**************************************************************/
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
String phpscript = "http://compsci02.snc.edu/cs460/2016/lewicj/WhereAreYou/locationhide.php?hide="+params[0]+"&user="+me;
try {
URL url = new URL(phpscript);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
return params[0];
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return "error";
}
/**************************************************************
* onPostExecute
* This function checks to see if what the result returned from the
background function is. if the result is 1 it changes
button text to hide otherwise it changes it to show.
**************************************************************/
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(Objects.equals(s,"1"))
{
btnHide.setText(Hide);
}
else{
btnHide.setText(Show);
}
}
}
}