XBigTK13X / snowgloo

Minimal audio catalog player
MIT License
0 stars 0 forks source link

Show a message about network connectivity instead of crashing #60

Open XBigTK13X opened 2 years ago

XBigTK13X commented 2 years ago

Something like this might work, but it will need some tweaking.


public class NetworkConnectionInterceptor implements Interceptor {

    private Context mContext;

    public NetworkConnectionInterceptor(Context context) {
        mContext = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        if (!isConnected()) {
            throw new NoConnectivityException();
            // Throwing our custom exception 'NoConnectivityException'
        }

        Request.Builder builder = chain.request().newBuilder();
        return chain.proceed(builder.build());
    }

   public boolean isConnected(){
      ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
      return (netInfo != null && netInfo.isConnected());
   }

}