Created
November 29, 2018 07:50
-
-
Save vishalhalani/43c23ea579ce82aff6c44c99c10ac46c to your computer and use it in GitHub Desktop.
Application class to create singltone instance of retrofit and use full instances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppController extends Application { | |
public static final int DISK_CACHE_SIZE = 10 * 1024 * 1024; // 10 MB | |
private static AppController appInstance = null; | |
private static Retrofit retrofit; | |
private static MyDatabase database; | |
private static Gson gson = new GsonBuilder() | |
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) | |
// .setDateFormat(DATE_FORMATS) | |
// .registerTypeAdapter(Date.class, new DateTypeDeserializer()) | |
.create(); | |
private ApiClient apiClient; | |
public static MyDatabase getDatabase() { | |
return database; | |
} | |
public static AppController getAppInstance() { | |
if (appInstance == null) { | |
appInstance = new AppController(); | |
} | |
return appInstance; | |
} | |
public static Retrofit getRetrofit() { | |
return retrofit; | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
JodaTimeAndroid.init(this); | |
Timber.plant(new Timber.DebugTree()); | |
database = Room.databaseBuilder(getApplicationContext(), MyDatabase.class, ApiConst.DATABASE_NAME).build(); | |
} | |
public ApiClient getApiClient() { | |
if (apiClient == null) { | |
apiClient = provideRetrofit(ApiClient.URL).create(ApiClient.class); | |
} | |
return apiClient; | |
} | |
private Retrofit provideRetrofit(String url) { | |
if (retrofit == null) { | |
retrofit = new Retrofit.Builder() | |
.baseUrl(url) | |
.client(provideOkHttpClient()) | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.build(); | |
} | |
return retrofit; | |
} | |
/** | |
* @return boolean : True or false | |
*/ | |
public boolean isInternetOn() { | |
ConnectivityManager connec; | |
connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = null; | |
if (connec != null) { | |
activeNetwork = connec.getActiveNetworkInfo(); | |
} | |
// Check for network connections | |
if (activeNetwork != null) { | |
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { | |
if (activeNetwork.getState() == NetworkInfo.State.CONNECTED) { | |
// if connected with internet | |
return true; | |
} else // connected to the mobile provider's data plan | |
return activeNetwork.getState() == NetworkInfo.State.CONNECTING; | |
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { | |
if (activeNetwork.getState() == NetworkInfo.State.CONNECTED) { | |
// connected to the mobile provider's data plan | |
return true; | |
} else // connected to the mobile provider's data plan | |
return activeNetwork.getState() == NetworkInfo.State.CONNECTING; | |
} | |
} else { | |
// buildDialog(activity); | |
// Toast.makeText(activity, "Internet Not Available ", Toast.LENGTH_LONG).show(); | |
return false; | |
} | |
return false; | |
} | |
private OkHttpClient provideOkHttpClient() { | |
// Install the all-trusting trust manager | |
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); | |
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | |
OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient.Builder(); | |
okhttpClientBuilder.connectTimeout(30, TimeUnit.SECONDS); | |
okhttpClientBuilder.readTimeout(30, TimeUnit.SECONDS); | |
okhttpClientBuilder.writeTimeout(30, TimeUnit.SECONDS); | |
// okhttpClientBuilder.addInterceptor(new RequestHeaderInterceptor()); | |
okhttpClientBuilder.addInterceptor(interceptor); | |
return okhttpClientBuilder.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment