From 42c78b9aeb9a98c8b6adf2ed3d2ecab19255c4f8 Mon Sep 17 00:00:00 2001 From: amanmehara Date: Tue, 22 Dec 2015 18:24:55 +0530 Subject: [PATCH] Internet Check on Activity Start MainActivity set as Parent Activity of NoConnectionActivity LanguageActivity set as Parent Activity of ProgramsActivity --- android/src/main/AndroidManifest.xml | 12 ++- .../programming/android/DetailActivity.java | 24 +----- .../programming/android/LanguageActivity.java | 68 +++++++++------ .../programming/android/MainActivity.java | 17 ---- .../programming/android/ProgramsActivity.java | 84 +++++++++---------- .../programming/android/ProgramsAdapter.java | 14 ---- 6 files changed, 98 insertions(+), 121 deletions(-) diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index f8c3fe9..1fc39ac 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -29,7 +29,11 @@ + android:label="@string/app_name" + android:parentActivityName=".LanguageActivity"> + + android:noHistory="true" + android:parentActivityName=".MainActivity"> + diff --git a/android/src/main/java/com/amanmehara/programming/android/DetailActivity.java b/android/src/main/java/com/amanmehara/programming/android/DetailActivity.java index 3da7e1d..cbe3288 100644 --- a/android/src/main/java/com/amanmehara/programming/android/DetailActivity.java +++ b/android/src/main/java/com/amanmehara/programming/android/DetailActivity.java @@ -3,8 +3,6 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -89,25 +87,9 @@ public boolean onOptionsItemSelected(MenuItem item) { } if (id == android.R.id.home) { - - context = this.getApplicationContext(); - ConnectivityManager connectivityManager = (ConnectivityManager) context - .getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); - boolean isConnected = activeNetworkInfo != null && - activeNetworkInfo.isConnectedOrConnecting(); - - if (isConnected) { - Intent intent = new Intent(this, ProgramsActivity.class); - intent.putExtra("language", bundle.getString("language")); - startActivity(intent); - } else { - Intent intent = new Intent(this, NoConnectionActivity.class); - intent.putExtra("activityInfo", ActivitiesAsEnum.PROGRAMS_ACTIVITY); - intent.putExtra("language", bundle.getString("language")); - startActivity(intent); - } - + Intent intent = new Intent(this, ProgramsActivity.class); + intent.putExtra("language", bundle.getString("language")); + startActivity(intent); } return super.onOptionsItemSelected(item); diff --git a/android/src/main/java/com/amanmehara/programming/android/LanguageActivity.java b/android/src/main/java/com/amanmehara/programming/android/LanguageActivity.java index 57d7c5b..305df64 100644 --- a/android/src/main/java/com/amanmehara/programming/android/LanguageActivity.java +++ b/android/src/main/java/com/amanmehara/programming/android/LanguageActivity.java @@ -13,6 +13,7 @@ import android.view.View; import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONObject; import java.util.concurrent.ExecutionException; @@ -32,38 +33,55 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_language); + languageRecyclerView = (RecyclerView) findViewById(R.id.language_recycler_view); + languageRecyclerView.setHasFixedSize(true); + + languageLayoutManager = new LinearLayoutManager(this); + languageRecyclerView.setLayoutManager(languageLayoutManager); String jsonLanguage = null; - WebServiceClient webServiceClient = new WebServiceClient(); - try { - jsonLanguage = webServiceClient - .execute("http://programmingwebapp.azurewebsites.net/api/languages/count") - .get(); - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - languages = null; + context = this.getApplicationContext(); + ConnectivityManager connectivityManager = (ConnectivityManager) context + .getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); + boolean isConnected = activeNetworkInfo != null && + activeNetworkInfo.isConnectedOrConnecting(); - try { - languages = new JSONArray(jsonLanguage); - } catch (JSONException e) { - e.printStackTrace(); - } + if (isConnected) { + WebServiceClient webServiceClient = new WebServiceClient(); + try { + jsonLanguage = webServiceClient + .execute("http://programmingwebapp.azurewebsites.net/api/languages/count") + .get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + languages = null; + try { + languages = new JSONArray(jsonLanguage); + } catch (JSONException e) { + e.printStackTrace(); + } - languageRecyclerView = (RecyclerView) findViewById(R.id.language_recycler_view); - languageRecyclerView.setHasFixedSize(true); + languageAdapter = new LanguageAdapter(languages); + ((LanguageAdapter) languageAdapter).setListClickListener(this); - languageLayoutManager = new LinearLayoutManager(this); - languageRecyclerView.setLayoutManager(languageLayoutManager); + //languageAdapter = new LanguageAdapter(new String[]{hello, "Hello","Bye", "Aman", "Today", "Hi", String.valueOf(new WebServiceClient().execute("http://programmingwebapp.azurewebsites.net/api/languages"))}); + languageRecyclerView.setAdapter(languageAdapter); + } else { - languageAdapter = new LanguageAdapter(languages); - ((LanguageAdapter) languageAdapter).setListClickListener(this); + languageAdapter = new LanguageAdapter(new JSONArray()); + ((LanguageAdapter) languageAdapter).setListClickListener(this); - //languageAdapter = new LanguageAdapter(new String[]{hello, "Hello","Bye", "Aman", "Today", "Hi", String.valueOf(new WebServiceClient().execute("http://programmingwebapp.azurewebsites.net/api/languages"))}); - languageRecyclerView.setAdapter(languageAdapter); + languageRecyclerView.setAdapter(languageAdapter); + + Intent intent = new Intent(this, NoConnectionActivity.class); + intent.putExtra("activityInfo", ActivitiesAsEnum.LANGUAGE_ACTIVITY); + startActivity(intent); + } } @@ -103,7 +121,8 @@ public void listItemClicked(View view, int position) { Intent intent = new Intent(LanguageActivity.this, ProgramsActivity.class); try { - intent.putExtra("language", languages.getString(position)); + JSONObject jsonObject = languages.getJSONObject(position); + intent.putExtra("language", jsonObject.getString("LanguageName")); } catch (JSONException e) { e.printStackTrace(); } @@ -114,7 +133,8 @@ public void listItemClicked(View view, int position) { intent.putExtra("activityInfo", ActivitiesAsEnum.PROGRAMS_ACTIVITY); try { - intent.putExtra("language", languages.getString(position)); + JSONObject jsonObject = languages.getJSONObject(position); + intent.putExtra("language", jsonObject.getString("LanguageName")); } catch (JSONException e) { e.printStackTrace(); } diff --git a/android/src/main/java/com/amanmehara/programming/android/MainActivity.java b/android/src/main/java/com/amanmehara/programming/android/MainActivity.java index 955ef8b..f714082 100644 --- a/android/src/main/java/com/amanmehara/programming/android/MainActivity.java +++ b/android/src/main/java/com/amanmehara/programming/android/MainActivity.java @@ -3,8 +3,6 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -45,22 +43,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } public void getStarted(View view) { - - context = this.getApplicationContext(); - ConnectivityManager connectivityManager = (ConnectivityManager) context - .getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); - boolean isConnected = activeNetworkInfo != null && - activeNetworkInfo.isConnectedOrConnecting(); - - if (isConnected) { Intent intent = new Intent(this, LanguageActivity.class); startActivity(intent); - } else { - Intent intent = new Intent(this, NoConnectionActivity.class); - intent.putExtra("activityInfo", ActivitiesAsEnum.LANGUAGE_ACTIVITY); - startActivity(intent); - } - } } diff --git a/android/src/main/java/com/amanmehara/programming/android/ProgramsActivity.java b/android/src/main/java/com/amanmehara/programming/android/ProgramsActivity.java index 9dfca72..ffe67ed 100644 --- a/android/src/main/java/com/amanmehara/programming/android/ProgramsActivity.java +++ b/android/src/main/java/com/amanmehara/programming/android/ProgramsActivity.java @@ -37,38 +37,56 @@ protected void onCreate(Bundle savedInstanceState) { bundle = getIntent().getExtras(); String language = bundle.getString("language"); - getActionBar().setDisplayHomeAsUpEnabled(true); + programsRecyclerView = (RecyclerView) findViewById(R.id.programs_recycler_view); + programsRecyclerView.setHasFixedSize(true); + + programsLayoutManager = new LinearLayoutManager(this); + programsRecyclerView.setLayoutManager(programsLayoutManager); String jsonPrograms = null; - WebServiceClient webServiceClient = new WebServiceClient(); - try { - jsonPrograms = webServiceClient - .execute("http://programmingwebapp.azurewebsites.net/api/programs/language/" - + language) - .get(); - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - } + context = this.getApplicationContext(); + ConnectivityManager connectivityManager = (ConnectivityManager) context + .getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); + boolean isConnected = activeNetworkInfo != null && + activeNetworkInfo.isConnectedOrConnecting(); + + if (isConnected) { + WebServiceClient webServiceClient = new WebServiceClient(); + + try { + jsonPrograms = webServiceClient + .execute("http://programmingwebapp.azurewebsites.net/api/programs/language/" + + language) + .get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } - programs = null; + programs = null; - try { - programs = new JSONArray(jsonPrograms); - } catch (JSONException e) { - e.printStackTrace(); - } + try { + programs = new JSONArray(jsonPrograms); + } catch (JSONException e) { + e.printStackTrace(); + } - programsRecyclerView = (RecyclerView) findViewById(R.id.programs_recycler_view); - programsRecyclerView.setHasFixedSize(true); + programsAdapter = new ProgramsAdapter(programs); + ((ProgramsAdapter) programsAdapter).setListClickListener(this); - programsLayoutManager = new LinearLayoutManager(this); - programsRecyclerView.setLayoutManager(programsLayoutManager); + programsRecyclerView.setAdapter(programsAdapter); + } else { + programsAdapter = new ProgramsAdapter(new JSONArray()); + ((ProgramsAdapter) programsAdapter).setListClickListener(this); - programsAdapter = new ProgramsAdapter(programs); - ((ProgramsAdapter) programsAdapter).setListClickListener(this); + programsRecyclerView.setAdapter(programsAdapter); - programsRecyclerView.setAdapter(programsAdapter); + Intent intent = new Intent(ProgramsActivity.this, NoConnectionActivity.class); + intent.putExtra("activityInfo", ActivitiesAsEnum.PROGRAMS_ACTIVITY); + intent.putExtra("language", bundle.getString("language")); + startActivity(intent); + } } @@ -91,26 +109,6 @@ public boolean onOptionsItemSelected(MenuItem item) { return true; } - if (id == android.R.id.home) { - - context = this.getApplicationContext(); - ConnectivityManager connectivityManager = (ConnectivityManager) context - .getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); - boolean isConnected = activeNetworkInfo != null && - activeNetworkInfo.isConnectedOrConnecting(); - - if (isConnected) { - Intent intent = new Intent(this, LanguageActivity.class); - startActivity(intent); - } else { - Intent intent = new Intent(this, NoConnectionActivity.class); - intent.putExtra("activityInfo", ActivitiesAsEnum.LANGUAGE_ACTIVITY); - startActivity(intent); - } - - } - return super.onOptionsItemSelected(item); } diff --git a/android/src/main/java/com/amanmehara/programming/android/ProgramsAdapter.java b/android/src/main/java/com/amanmehara/programming/android/ProgramsAdapter.java index 7127008..0fc8521 100644 --- a/android/src/main/java/com/amanmehara/programming/android/ProgramsAdapter.java +++ b/android/src/main/java/com/amanmehara/programming/android/ProgramsAdapter.java @@ -43,20 +43,6 @@ public void onBindViewHolder(ProgramsAdapter.ViewHolder viewHolder, int i) { e.printStackTrace(); } -// switch (i%4) { -// case 0: -// viewHolder.mLanguageName.setBackgroundColor(Color.RED); -// break; -// case 1: -// viewHolder.mLanguageName.setBackgroundColor(Color.GREEN); -// break; -// case 2: -// viewHolder.mLanguageName.setBackgroundColor(Color.BLUE); -// break; -// case 3: -// viewHolder.mLanguageName.setBackgroundColor(Color.MAGENTA); -// break; -// } } @Override