Skip to content

Commit

Permalink
Internet Check on Activity Start
Browse files Browse the repository at this point in the history
MainActivity set as Parent Activity of NoConnectionActivity
LanguageActivity set as Parent Activity of ProgramsActivity
  • Loading branch information
amanmehara committed Dec 22, 2015
1 parent 35ce095 commit 42c78b9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 121 deletions.
12 changes: 10 additions & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
</activity>
<activity
android:name=".ProgramsActivity"
android:label="@string/app_name">
android:label="@string/app_name"
android:parentActivityName=".LanguageActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.amanmehara.programming.android.LanguageActivity"/>
</activity>
<activity
android:name=".DetailActivity"
Expand All @@ -38,7 +42,11 @@
<activity
android:name=".NoConnectionActivity"
android:label="@string/app_name"
android:noHistory="true">
android:noHistory="true"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.amanmehara.programming.android.MainActivity"/>
</activity>
</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}


Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 42c78b9

Please sign in to comment.