diff --git a/.idea/compiler.xml b/.idea/compiler.xml index fb7f4a8..659bf43 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index f76dd6e..f14f5c0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -11,7 +11,7 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7cdcd80..3bdfd9d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,9 @@ xmlns:tools="http://schemas.android.com/tools" package="com.example.tourguide"> + + + + diff --git a/app/src/main/java/com/example/tourguide/Country.java b/app/src/main/java/com/example/tourguide/Country.java new file mode 100644 index 0000000..84e21ea --- /dev/null +++ b/app/src/main/java/com/example/tourguide/Country.java @@ -0,0 +1,20 @@ +package com.example.tourguide; + +public class Country { + private String name; + + + public Country(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + +} diff --git a/app/src/main/java/com/example/tourguide/Country_Activity.java b/app/src/main/java/com/example/tourguide/Country_Activity.java new file mode 100644 index 0000000..ab6049e --- /dev/null +++ b/app/src/main/java/com/example/tourguide/Country_Activity.java @@ -0,0 +1,76 @@ +package com.example.tourguide; + + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; + +import android.os.AsyncTask; +import android.os.Bundle; +import android.view.View; +import android.widget.ListView; +import android.widget.TextView; + +import androidx.appcompat.app.AppCompatActivity; + +import java.util.ArrayList; +import java.util.List; + +public class Country_Activity extends AppCompatActivity { + ListView listView; + CustomAdapter mAdapter; + private static final String URL="https://countriesnow.space/api/v0.1/countries"; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.country_name); + listView=findViewById(R.id.listView); + listView.setAdapter(mAdapter); + CountryAsyncTask task = new CountryAsyncTask(); + task.execute(URL); + mAdapter = new CustomAdapter(this, new ArrayList()); + listView.setAdapter(mAdapter); + } + private boolean isConnected() { + ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); + } + private class CountryAsyncTask extends AsyncTask> { + + @Override + protected List doInBackground(String... urls) { + if (urls.length < 1 || urls[0] == null) { + return null; + } + + List result = QueryUtils.fetchCountryData(urls[0]); + return result; + } + + @Override + protected void onPostExecute(List data) { + boolean isConnected = isConnected(); + View loadingIndicator = findViewById(R.id.loading_indicator); + + mAdapter.clear(); + if(isConnected) { + + loadingIndicator.setVisibility(View.GONE); + mAdapter.addAll(data); + } + else{ + loadingIndicator.setVisibility(View.GONE); + TextView txt=findViewById(R.id.empty_view); + txt.setText("No Internet"); + } + + if (data != null && !data.isEmpty()) { + + } + + + + } + } +} diff --git a/app/src/main/java/com/example/tourguide/CustomAdapter.java b/app/src/main/java/com/example/tourguide/CustomAdapter.java new file mode 100644 index 0000000..18f4bb5 --- /dev/null +++ b/app/src/main/java/com/example/tourguide/CustomAdapter.java @@ -0,0 +1,52 @@ +package com.example.tourguide; + +import android.content.Context; +import android.graphics.Color; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.util.ArrayList; + + +public class CustomAdapter extends ArrayAdapter { + + + public CustomAdapter(@NonNull Context context, ArrayList arrayList) { + + // pass the context and arrayList for the super + // constructor of the ArrayAdapter class + super(context, 0, arrayList); + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + + // convertView which is recyclable view + View currentItemView = convertView; + + // of the recyclable view is null then inflate the custom layout for the same + if (currentItemView == null) { + currentItemView = LayoutInflater.from(getContext()).inflate(R.layout.customlist, parent, false); + } + + // get the position of the view from the ArrayAdapter + Country country = getItem(position); + TextView magnitude = currentItemView.findViewById(R.id.country); + magnitude.setText(country.getName()); + magnitude.setTextColor(Color.parseColor("#585F65")); + + + // then return the recyclable view + return currentItemView; + } + + +} + diff --git a/app/src/main/java/com/example/tourguide/Home_Activity.java b/app/src/main/java/com/example/tourguide/Home_Activity.java index b610aa7..d6db3a9 100644 --- a/app/src/main/java/com/example/tourguide/Home_Activity.java +++ b/app/src/main/java/com/example/tourguide/Home_Activity.java @@ -2,7 +2,10 @@ import androidx.appcompat.app.AppCompatActivity; +import android.content.Intent; import android.os.Bundle; +import android.view.View; +import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; @@ -28,10 +31,12 @@ protected void onCreate(Bundle savedInstanceState) { ivclick_hotel = findViewById(R.id.iv_home_hotel); ivclick_travel = findViewById(R.id.iv_home_travel); + String name=""; //get the name from firebase(login) - tv_welcome.setText("Welcome, "+name); + // tv_welcome.setText("Welcome, "+name); super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/tourguide/QueryUtils.java b/app/src/main/java/com/example/tourguide/QueryUtils.java new file mode 100644 index 0000000..ce0a820 --- /dev/null +++ b/app/src/main/java/com/example/tourguide/QueryUtils.java @@ -0,0 +1,172 @@ +package com.example.tourguide; + +import android.text.TextUtils; +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +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.nio.charset.Charset; +import java.util.ArrayList; + +/** + * Helper methods related to requesting and receiving earthquake data from USGS. + */ +public final class QueryUtils { + public static final String LOG_TAG = Country_Activity.class.getSimpleName(); + private QueryUtils() { + } + + /** + * Return a list of {@link Country} objects that has been built up from + * parsing a JSON response. + */ + public static ArrayList extractFromJson(String earthquakeJson) { + + if(TextUtils.isEmpty(earthquakeJson)){ + return null; + } + // Create an empty ArrayList that we can start adding earthquakes to + ArrayList earthquakes = new ArrayList<>(); + + + // Try to parse the SAMPLE_JSON_RESPONSE. If there's a problem with the way the JSON + // is formatted, a JSONException exception object will be thrown. + // Catch the exception so the app doesn't crash, and print the error message to the logs. + try { + JSONObject baseJsonResponse = new JSONObject(earthquakeJson); + JSONArray earthquakeArray = baseJsonResponse.getJSONArray("data"); + + + for(int i=0;i< earthquakeArray.length();i++) { + // Get a single earthquake at position i within the list of earthquakes + JSONObject currentCountry = earthquakeArray.getJSONObject(i); + + + + // Extract the value for the key called "place" + String name = currentCountry.getString("country"); + + + // Create a new {@link Earthquake} object with the magnitude, location, time, + // and url from the JSON response. + Country earthquake = new Country(name); + + // Add the new {@link Earthquake} to the list of earthquakes. + earthquakes.add(earthquake); + // build up a list of Earthquake objects with the corresponding data. + } + + } catch (JSONException e) { + // If an error is thrown when executing any of the above statements in the "try" block, + // catch the exception here, so the app doesn't crash. Print a log message + // with the message from the exception. + Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e); + } + + // Return the list of earthquakes + return earthquakes; + } + public static ArrayList fetchCountryData(String requestUrl) { + // Create URL object + URL url = createUrl(requestUrl); + + // Perform HTTP request to the URL and receive a JSON response back + String jsonResponse = null; + try { + jsonResponse = makeHttpRequest(url); + } catch (IOException e) { + Log.e(LOG_TAG, "Problem making the HTTP request.", e); + } + + // Extract relevant fields from the JSON response and create a list of {@link Earthquake}s + ArrayList earthquakes = extractFromJson(jsonResponse); + + // Return the list of {@link Earthquake}s + return earthquakes; + } + + /** + * Returns new URL object from the given string URL. + */ + private static URL createUrl(String stringUrl) { + URL url = null; + try { + url = new URL(stringUrl); + } catch (MalformedURLException e) { + Log.e(LOG_TAG, "Problem building the URL ", e); + } + return url; + } + + /** + * Make an HTTP request to the given URL and return a String as the response. + */ + private static String makeHttpRequest(URL url) throws IOException { + String jsonResponse = ""; + + // If the URL is null, then return early. + if (url == null) { + return jsonResponse; + } + + HttpURLConnection urlConnection = null; + InputStream inputStream = null; + try { + urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setReadTimeout(10000 /* milliseconds */); + urlConnection.setConnectTimeout(15000 /* milliseconds */); + urlConnection.setRequestMethod("GET"); + urlConnection.connect(); + + // If the request was successful (response code 200), + // then read the input stream and parse the response. + if (urlConnection.getResponseCode() == 200) { + inputStream = urlConnection.getInputStream(); + jsonResponse = readFromStream(inputStream); + } else { + Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode()); + } + } catch (IOException e) { + Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e); + } finally { + if (urlConnection != null) { + urlConnection.disconnect(); + } + if (inputStream != null) { + // Closing the input stream could throw an IOException, which is why + // the makeHttpRequest(URL url) method signature specifies than an IOException + // could be thrown. + inputStream.close(); + } + } + return jsonResponse; + } + + /** + * Convert the {@link InputStream} into a String which contains the + * whole JSON response from the server. + */ + private static String readFromStream(InputStream inputStream) throws IOException { + StringBuilder output = new StringBuilder(); + if (inputStream != null) { + InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8")); + BufferedReader reader = new BufferedReader(inputStreamReader); + String line = reader.readLine(); + while (line != null) { + output.append(line); + line = reader.readLine(); + } + } + return output.toString(); + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_home.xml b/app/src/main/res/layout/activity_home.xml index e685cd2..8583a37 100644 --- a/app/src/main/res/layout/activity_home.xml +++ b/app/src/main/res/layout/activity_home.xml @@ -38,7 +38,7 @@ android:layout_marginTop="10dp" android:layout_marginEnd="10dp"/> - //cardview for search bar + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/customlist.xml b/app/src/main/res/layout/customlist.xml new file mode 100644 index 0000000..5b40f37 --- /dev/null +++ b/app/src/main/res/layout/customlist.xml @@ -0,0 +1,17 @@ + + + + + + + \ No newline at end of file