Skip to content

Commit

Permalink
ADD: added network check
Browse files Browse the repository at this point in the history
  • Loading branch information
bxute committed Apr 21, 2018
1 parent 7111221 commit 3106e81
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 4 deletions.
75 changes: 73 additions & 2 deletions app/src/main/java/app/kiti/com/kitiapp/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package app.kiti.com.kitiapp.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
Expand All @@ -28,12 +31,14 @@
import app.kiti.com.kitiapp.fragments.ProfileFragment;
import app.kiti.com.kitiapp.fragments.TransactionFragment;
import app.kiti.com.kitiapp.preference.PreferenceManager;
import app.kiti.com.kitiapp.utils.ConnectionUtils;
import app.kiti.com.kitiapp.utils.ConnectivityErrorDialog;
import app.kiti.com.kitiapp.utils.FontManager;
import app.kiti.com.kitiapp.utils.TimeUtils;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements ConnectivityErrorDialog.TryAgainListener {


@BindView(R.id.bottomBar_home)
Expand Down Expand Up @@ -80,6 +85,9 @@ public class MainActivity extends AppCompatActivity {
private FragmentManager fragmentManager;
private Typeface typface;
private Context context;
private NetworkChangeReceiver networkChangeReceiver;
private boolean networkChangeReceiverRegistered;
private ConnectivityErrorDialog connectivityErrorDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -96,7 +104,6 @@ protected void onCreate(Bundle savedInstanceState) {

}


@Override
public void onBackPressed() {
showExistAlert();
Expand All @@ -110,6 +117,10 @@ private void initObjects() {
earningFragment = new EarningFragment();
historyFragment = new TransactionFragment();
context = this;
networkChangeReceiver = new NetworkChangeReceiver();
connectivityErrorDialog = new ConnectivityErrorDialog(this);
connectivityErrorDialog.setCancelable(false);
connectivityErrorDialog.setTryAgainListener(this);

}

Expand All @@ -132,6 +143,27 @@ private void setupToolbar() {

}

@Override
protected void onResume() {
super.onResume();

if (!networkChangeReceiverRegistered) {
registerReceiver(networkChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
networkChangeReceiverRegistered = true;
}

}

@Override
protected void onDestroy() {
super.onDestroy();
if (networkChangeReceiverRegistered) {
unregisterReceiver(networkChangeReceiver);
networkChangeReceiverRegistered = false;
}

}

@Override
protected void onStop() {
super.onStop();
Expand Down Expand Up @@ -335,5 +367,44 @@ private void resetLastSelection(int lastMenuSelection) {

}

@Override
public void onTryAgain() {
if (ConnectionUtils.isConnected(this)) {
hideConnectivityDialog();
syncConfig();
}

}

public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {

if (ConnectionUtils.isConnected(MainActivity.this)) {
hideConnectivityDialog();
} else {
showConnectivityErrorDialog();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}

}

private void showConnectivityErrorDialog() {

if (connectivityErrorDialog != null) {
connectivityErrorDialog.show();
}
}

private void hideConnectivityDialog() {
if (connectivityErrorDialog != null) {
connectivityErrorDialog.dismiss();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
Expand Down Expand Up @@ -209,7 +210,35 @@ public void onRewardedVideoAdClosed() {

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
onUserNotSeenFullVideo();
showVideoLoadError();
//onUserNotSeenFullVideo();
}

private void showVideoLoadError() {
//hide general message
if (generalMessage != null) {
generalMessage.setText(getResources().getString(R.string.message_for_video_failed_to_load));
generalMessage.setVisibility(View.VISIBLE);
}

//hide loading card
if (loadingProgress != null) {
loadingProgress.setVisibility(View.GONE);
}
//show earned card
if (earnedCard != null) {
earnedCard.setVisibility(View.VISIBLE);
}
//update earn amount
if (earnedMessage != null) {
topBanner.setText("Oops!!!");
earnedMessage.setText(String.format("Failed to load video. Some wifi blocks videos"));
}
//show go back button
if (goBackBtn != null) {
goBackBtn.setVisibility(View.VISIBLE);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private void collectJokes(Map<String, Object> map) {
@Override
public void onStop() {
super.onStop();
autoSlideJokeView.cancelSliding();
//autoSlideJokeView.cancelSliding();
}

@Override
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/app/kiti/com/kitiapp/utils/ConnectionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package app.kiti.com.kitiapp.utils;

import android.content.Context;
import android.net.ConnectivityManager;

/**
* Created by Ankit on 2/4/2018.
*/

public class ConnectionUtils {

public static boolean isConnected(Context context) {

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo mobileData = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
final android.net.NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mobileData.isConnected()) {
return true;
} else if (wifi.isConnected()) {
return true;
}

// context.startActivity(new Intent(context, ErrorSplash.class));
// ((AppCompatActivity) context).finish();

return false;
}

// for wifi service [where login is required before internet ]
private static boolean isWorking() {
try {
Process p1 = Runtime.getRuntime().exec("ping -c 1 www.google.com");
int returnVal = p1.waitFor();
return (returnVal == 0);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package app.kiti.com.kitiapp.utils;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import app.kiti.com.kitiapp.R;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
* Created by Ankit on 4/21/2018.
*/

public class ConnectivityErrorDialog extends Dialog {

public Activity c;
public Dialog d;
@BindView(R.id.tryAgain)
TextView tryAgain;
private TextView tryButton;

public ConnectivityErrorDialog(Activity a) {
super(a);
this.c = a;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.connectivity_error_dialog);
ButterKnife.bind(this);
tryAgain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(tryAgainListener!=null){
tryAgainListener.onTryAgain();
}
}
});
}

private TryAgainListener tryAgainListener;

public void setTryAgainListener(TryAgainListener tryAgainListener) {
this.tryAgainListener = tryAgainListener;
}

public interface TryAgainListener{
void onTryAgain();
}

}
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/try_again_bg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<size android:width="10dp" android:height="10dp"/>
<solid android:color="@color/colorPrimary"/>
<corners android:radius="4dp"/>

</shape>
26 changes: 26 additions & 0 deletions app/src/main/res/layout/connectivity_error_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:padding="16dp"
android:layout_height="match_parent">

<TextView
android:layout_margin="36dp"
android:textColor="#e72d2d2d"
android:gravity="center_horizontal"
android:text="Please check your internet connectivity."
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tryAgain"
android:text="Try Again"
android:textSize="18sp"
android:gravity="center"
android:padding="8dp"
android:textColor="#ffffff"
android:background="@drawable/try_again_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
<string name="you_can_redeem_after_s">You can redeem after %d</string>
<string name="default_payment_medium">PAYTM</string>

<string name="message_for_video_failed_to_load">
Troubleshooting:\n
\u2022 You might be connected to a cyberoam network.
\n\u2022 Connect to other network.
\n\u2022 Try again after sometime.
</string>


<string-array name="featured_image">
<item>
Expand Down

0 comments on commit 3106e81

Please sign in to comment.