From 091aee4255a2ab59c0e97a94c1a48b8d6eff39a9 Mon Sep 17 00:00:00 2001 From: miPlodder Date: Wed, 12 Jun 2019 06:53:23 +0530 Subject: [PATCH] Feat: Convert Retrofit Model Java classes to Kotlin Convert EndPoints class from Java to Kotlin --- app/src/main/AndroidManifest.xml | 10 +- .../apache/fineract/FineractApplication.java | 2 + .../datamanager/api/DataManagerRoles.java | 2 +- .../fineract/data/remote/BaseApiManager.java | 145 ------------------ .../fineract/data/remote/BaseApiManager.kt | 87 +++++++++++ .../apache/fineract/data/remote/BaseUrl.java | 25 --- .../apache/fineract/data/remote/BaseUrl.kt | 22 +++ .../data/remote/ConnectivityInterceptor.java | 36 ----- .../data/remote/ConnectivityInterceptor.kt | 24 +++ .../fineract/data/remote/EndPoints.java | 22 --- .../apache/fineract/data/remote/EndPoints.kt | 20 +++ .../data/remote/FineractInterceptor.java | 86 ----------- .../data/remote/FineractInterceptor.kt | 71 +++++++++ .../data/remote/FineractOkHttpClient.java | 109 ------------- .../data/remote/FineractOkHttpClient.kt | 85 ++++++++++ .../remote/NullOnEmptyConverterFactory.java | 10 +- .../remote/ReceivedCookiesInterceptor.java | 39 ----- .../data/remote/ReceivedCookiesInterceptor.kt | 38 +++++ .../ui/base/FineractBaseActivity.java | 11 +- .../fineract/ui/online/PassCodeActivity.kt | 44 ++++++ .../fineract/ui/online/SplashActivity.kt | 1 + .../ui/online/launcher/LauncherActivity.java | 25 ++- .../ui/online/launcher/LauncherContract.java | 4 +- .../ui/online/login/LoginActivity.java | 8 +- .../ui/online/login/LoginPresenter.java | 3 +- .../fineract/utils/MifosErrorUtils.java | 4 +- 26 files changed, 444 insertions(+), 489 deletions(-) delete mode 100644 app/src/main/java/org/apache/fineract/data/remote/BaseApiManager.java create mode 100644 app/src/main/java/org/apache/fineract/data/remote/BaseApiManager.kt delete mode 100755 app/src/main/java/org/apache/fineract/data/remote/BaseUrl.java create mode 100644 app/src/main/java/org/apache/fineract/data/remote/BaseUrl.kt delete mode 100644 app/src/main/java/org/apache/fineract/data/remote/ConnectivityInterceptor.java create mode 100644 app/src/main/java/org/apache/fineract/data/remote/ConnectivityInterceptor.kt delete mode 100644 app/src/main/java/org/apache/fineract/data/remote/EndPoints.java create mode 100644 app/src/main/java/org/apache/fineract/data/remote/EndPoints.kt delete mode 100644 app/src/main/java/org/apache/fineract/data/remote/FineractInterceptor.java create mode 100644 app/src/main/java/org/apache/fineract/data/remote/FineractInterceptor.kt delete mode 100644 app/src/main/java/org/apache/fineract/data/remote/FineractOkHttpClient.java create mode 100644 app/src/main/java/org/apache/fineract/data/remote/FineractOkHttpClient.kt delete mode 100644 app/src/main/java/org/apache/fineract/data/remote/ReceivedCookiesInterceptor.java create mode 100644 app/src/main/java/org/apache/fineract/data/remote/ReceivedCookiesInterceptor.kt create mode 100644 app/src/main/java/org/apache/fineract/ui/online/PassCodeActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dfb80827..d28cf451 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -31,14 +31,18 @@ android:resource="@xml/filepaths" /> - + - + + + - + diff --git a/app/src/main/java/org/apache/fineract/FineractApplication.java b/app/src/main/java/org/apache/fineract/FineractApplication.java index f9bd6a4f..de322057 100644 --- a/app/src/main/java/org/apache/fineract/FineractApplication.java +++ b/app/src/main/java/org/apache/fineract/FineractApplication.java @@ -5,6 +5,7 @@ import com.crashlytics.android.Crashlytics; import com.evernote.android.job.JobManager; +import com.mifos.mobile.passcode.utils.ForegroundChecker; import com.raizlabs.android.dbflow.config.FlowManager; import org.apache.fineract.injection.component.ApplicationComponent; @@ -35,6 +36,7 @@ public void onCreate() { instance = this; Fabric.with(this, new Crashlytics()); FlowManager.init(this); + ForegroundChecker.init(this); } public static Context getContext() { diff --git a/app/src/main/java/org/apache/fineract/data/datamanager/api/DataManagerRoles.java b/app/src/main/java/org/apache/fineract/data/datamanager/api/DataManagerRoles.java index 116bde11..331fe2d9 100644 --- a/app/src/main/java/org/apache/fineract/data/datamanager/api/DataManagerRoles.java +++ b/app/src/main/java/org/apache/fineract/data/datamanager/api/DataManagerRoles.java @@ -34,7 +34,7 @@ public DataManagerRoles(BaseApiManager baseApiManager, public Observable> getRoles() { return authenticatedObservableApi( - baseApiManager.getRolesAndPermissionsService().getRoles()) + baseApiManager.rolesService.getRoles()) .onErrorResumeNext( new Function>>() { @Override diff --git a/app/src/main/java/org/apache/fineract/data/remote/BaseApiManager.java b/app/src/main/java/org/apache/fineract/data/remote/BaseApiManager.java deleted file mode 100644 index adb27346..00000000 --- a/app/src/main/java/org/apache/fineract/data/remote/BaseApiManager.java +++ /dev/null @@ -1,145 +0,0 @@ -package org.apache.fineract.data.remote; - -import android.content.Context; - -import org.apache.fineract.data.services.AccountingService; -import org.apache.fineract.data.services.AnonymousService; -import org.apache.fineract.data.services.AuthService; -import org.apache.fineract.data.services.CustomerService; -import org.apache.fineract.data.services.DepositService; -import org.apache.fineract.data.services.IndividualLendingService; -import org.apache.fineract.data.services.LoanService; -import org.apache.fineract.data.services.ProductService; -import org.apache.fineract.data.services.PayrollService; -import org.apache.fineract.data.services.RolesService; -import org.apache.fineract.data.services.TellersService; - -import okhttp3.OkHttpClient; -import okhttp3.logging.HttpLoggingInterceptor; -import retrofit2.Retrofit; -import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; -import retrofit2.converter.gson.GsonConverterFactory; -import retrofit2.converter.scalars.ScalarsConverterFactory; - - -/** - * @author Rajan Maurya - * @since 16/3/2017 - */ -public class BaseApiManager { - - public static Retrofit retrofit; - public static Retrofit anonyMousRetrofit; - private static AuthService authApi; - private static CustomerService customerApi; - private static DepositService depositApi; - private static LoanService loanApi; - private static IndividualLendingService individualLendingService; - private static AnonymousService anonymousService; - private static RolesService rolesService; - private static AccountingService accountingService; - private static TellersService tellerService; - private static ProductService productService; - private static PayrollService payrollService; - - public BaseApiManager(Context context) { - createService(context); - createAnonymousService(); - } - - private static void init() { - authApi = createApi(AuthService.class); - customerApi = createApi(CustomerService.class); - depositApi = createApi(DepositService.class); - loanApi = createApi(LoanService.class); - individualLendingService = createApi(IndividualLendingService.class); - rolesService = createApi(RolesService.class); - accountingService = createApi(AccountingService.class); - tellerService = createApi(TellersService.class); - productService = createApi(ProductService.class); - payrollService = createApi(PayrollService.class); - } - - private static void initAnonymous() { - anonymousService = anonyMousRetrofit.create(AnonymousService.class); - } - - private static T createApi(Class clazz) { - return retrofit.create(clazz); - } - - private static void createService(Context context) { - - retrofit = new Retrofit.Builder() - .baseUrl(BaseUrl.getDefaultBaseUrl()) - .addConverterFactory(new NullOnEmptyConverterFactory()) - .addConverterFactory(ScalarsConverterFactory.create()) - .addConverterFactory(GsonConverterFactory.create()) - .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) - .client(new FineractOkHttpClient(context).getFineractOkHttpClient()) - .build(); - init(); - } - - private static void createAnonymousService() { - - HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); - interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); - - OkHttpClient okHttpClient = new OkHttpClient.Builder() - .addInterceptor(interceptor) - .build(); - - anonyMousRetrofit = new Retrofit.Builder() - .baseUrl(BaseUrl.getDefaultBaseUrl()) - .addConverterFactory(GsonConverterFactory.create()) - .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) - .client(okHttpClient) - .build(); - initAnonymous(); - } - - public AuthService getAuthApi() { - return authApi; - } - - public CustomerService getCustomerApi() { - return customerApi; - } - - public DepositService getDepositApi() { - return depositApi; - } - - public LoanService getLoanApi() { - return loanApi; - } - - public IndividualLendingService getIndividualLendingService() { - return individualLendingService; - } - - public AnonymousService getAnonymousService() { - return anonymousService; - } - - public RolesService getRolesAndPermissionsService() { - return rolesService; - } - - public AccountingService getAccountingService() { - return accountingService; - } - - public TellersService getTellerService() { - return tellerService; - } - - public ProductService getProductService() { - return productService; - } - - public PayrollService getPayrollService() { - return payrollService; - } -} diff --git a/app/src/main/java/org/apache/fineract/data/remote/BaseApiManager.kt b/app/src/main/java/org/apache/fineract/data/remote/BaseApiManager.kt new file mode 100644 index 00000000..2624880a --- /dev/null +++ b/app/src/main/java/org/apache/fineract/data/remote/BaseApiManager.kt @@ -0,0 +1,87 @@ +package org.apache.fineract.data.remote + +import android.content.Context +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor +import org.apache.fineract.data.services.* +import retrofit2.Retrofit +import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory +import retrofit2.converter.gson.GsonConverterFactory +import retrofit2.converter.scalars.ScalarsConverterFactory + +/** + * Created by Ahmad Jawid Muhammadi on 1/6/20 + */ + +class BaseApiManager(context: Context) { + + companion object { + lateinit var retrofit: Retrofit + } + + lateinit var anonyMousRetrofit: Retrofit + lateinit var authApi: AuthService + lateinit var customerApi: CustomerService + lateinit var depositApi: DepositService + lateinit var loanApi: LoanService + lateinit var individualLendingService: IndividualLendingService + lateinit var anonymousService: AnonymousService + lateinit var rolesService: RolesService + lateinit var accountingService: AccountingService + lateinit var tellerService: TellersService + lateinit var productService: ProductService + lateinit var payrollService: PayrollService + + init { + createService(context) + createAnonymousService() + } + + private fun init() { + authApi = createApi(AuthService::class.java) + customerApi = createApi(CustomerService::class.java) + depositApi = createApi(DepositService::class.java) + loanApi = createApi(LoanService::class.java) + individualLendingService = createApi(IndividualLendingService::class.java) + rolesService = createApi(RolesService::class.java) + accountingService = createApi(AccountingService::class.java) + tellerService = createApi(TellersService::class.java) + productService = createApi(ProductService::class.java) + payrollService = createApi(PayrollService::class.java) + } + + private fun initAnonymous() { + anonymousService = anonyMousRetrofit!!.create(AnonymousService::class.java) + } + + private fun createApi(clazz: Class): T { + return retrofit!!.create(clazz) + } + + private fun createService(context: Context) { + retrofit = Retrofit.Builder() + .baseUrl(BaseUrl.getDefaultBaseUrl()) + .addConverterFactory(NullOnEmptyConverterFactory()) + .addConverterFactory(ScalarsConverterFactory.create()) + .addConverterFactory(GsonConverterFactory.create()) + .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) + .client(FineractOkHttpClient.getFineractOkHttpClient(context)) + .build() + init() + } + + private fun createAnonymousService() { + val interceptor = HttpLoggingInterceptor() + interceptor.level = HttpLoggingInterceptor.Level.BODY + val okHttpClient = OkHttpClient.Builder() + .addInterceptor(interceptor) + .build() + anonyMousRetrofit = Retrofit.Builder() + .baseUrl(BaseUrl.getDefaultBaseUrl()) + .addConverterFactory(GsonConverterFactory.create()) + .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) + .client(okHttpClient) + .build() + initAnonymous() + } +} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/data/remote/BaseUrl.java b/app/src/main/java/org/apache/fineract/data/remote/BaseUrl.java deleted file mode 100755 index 95fee4b5..00000000 --- a/app/src/main/java/org/apache/fineract/data/remote/BaseUrl.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * This project is licensed under the open source MPL V2. - * See https://github.com/openMF/android-client/blob/master/LICENSE.md - */ - -package org.apache.fineract.data.remote; - -/** - * @author Rajan Maurya - */ -public class BaseUrl { - - public static final String PROTOCOL_HTTPS = "https://"; - public static final String API_ENDPOINT = "pilot.kuelap.io"; - public static final String PORT = "80"; - // "/" in the last of the base url always - - public String getName() { - return "fineract"; - } - - public static String getDefaultBaseUrl() { - return PROTOCOL_HTTPS + API_ENDPOINT; - } -} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/data/remote/BaseUrl.kt b/app/src/main/java/org/apache/fineract/data/remote/BaseUrl.kt new file mode 100644 index 00000000..065b8d58 --- /dev/null +++ b/app/src/main/java/org/apache/fineract/data/remote/BaseUrl.kt @@ -0,0 +1,22 @@ +package org.apache.fineract.data.remote + +/** + * Created by Ahmad Jawid Muhammadi on 1/6/20 + */ + +object BaseUrl { + private const val PROTOCOL_HTTPS = "https://" + private const val API_ENDPOINT = "pilot.kuelap.io" + val PORT = "80" + // "/" in the last of the base url always + + // "/" in the last of the base url always + fun getName(): String? { + return "fineract" + } + + @JvmStatic + fun getDefaultBaseUrl(): String? { + return PROTOCOL_HTTPS + API_ENDPOINT + } +} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/data/remote/ConnectivityInterceptor.java b/app/src/main/java/org/apache/fineract/data/remote/ConnectivityInterceptor.java deleted file mode 100644 index 8c48d7af..00000000 --- a/app/src/main/java/org/apache/fineract/data/remote/ConnectivityInterceptor.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.apache.fineract.data.remote; - -import android.content.Context; -import androidx.annotation.NonNull; - -import org.apache.fineract.exceptions.NoConnectivityException; -import org.apache.fineract.utils.NetworkUtil; - -import java.io.IOException; - -import okhttp3.Interceptor; -import okhttp3.Request; -import okhttp3.Response; - -/** - * @author Rajan Maurya - * On 23/09/17. - */ -public class ConnectivityInterceptor implements Interceptor { - - private Context context; - - public ConnectivityInterceptor(Context context) { - this.context = context; - } - - @Override - public Response intercept(@NonNull Chain chain) throws IOException { - if (!NetworkUtil.isConnected(context)) { - throw new NoConnectivityException(); - } - - Request.Builder builder = chain.request().newBuilder(); - return chain.proceed(builder.build()); - } -} diff --git a/app/src/main/java/org/apache/fineract/data/remote/ConnectivityInterceptor.kt b/app/src/main/java/org/apache/fineract/data/remote/ConnectivityInterceptor.kt new file mode 100644 index 00000000..30765210 --- /dev/null +++ b/app/src/main/java/org/apache/fineract/data/remote/ConnectivityInterceptor.kt @@ -0,0 +1,24 @@ +package org.apache.fineract.data.remote + +import android.content.Context +import okhttp3.Interceptor +import okhttp3.Response +import org.apache.fineract.exceptions.NoConnectivityException +import org.apache.fineract.utils.NetworkUtil +import java.io.IOException + +/** + * Created by Ahmad Jawid Muhammadi on 1/6/20 + */ + +class ConnectivityInterceptor(private var context: Context?) : Interceptor { + + @Throws(IOException::class) + override fun intercept(chain: Interceptor.Chain): Response? { + if (!NetworkUtil.isConnected(context)) { + throw NoConnectivityException() + } + val builder = chain.request().newBuilder() + return chain.proceed(builder.build()) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/data/remote/EndPoints.java b/app/src/main/java/org/apache/fineract/data/remote/EndPoints.java deleted file mode 100644 index 20d709a6..00000000 --- a/app/src/main/java/org/apache/fineract/data/remote/EndPoints.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.apache.fineract.data.remote; - -/** - * @author Rajan Maurya - * On 16/06/17. - */ - -public class EndPoints { - - /* - * API End Paths - * <- This section manage the different type of end points - */ - public static final String API_IDENTITY_PATH = "/identity/v1"; - public static final String API_CUSTOMER_PATH = "/api/customer/v1"; - public static final String API_DEPOSIT_PATH = "/api/deposit/v1"; - public static final String API_PORTFOLIO_PATH = "/api/portfolio/v1"; - public static final String API_ACCOUNTING_PATH = "/api/accounting/v1"; - public static final String API_TELLER_PATH = "/api/teller/v1"; - public static final String API_PAYROLL_PATH = "/api//payroll/v1"; - -} diff --git a/app/src/main/java/org/apache/fineract/data/remote/EndPoints.kt b/app/src/main/java/org/apache/fineract/data/remote/EndPoints.kt new file mode 100644 index 00000000..2ea92473 --- /dev/null +++ b/app/src/main/java/org/apache/fineract/data/remote/EndPoints.kt @@ -0,0 +1,20 @@ +package org.apache.fineract.data.remote + +/** + * Created by Ahmad Jawid Muhammadi on 1/6/20 + */ +object EndPoints { + + /* + * API End Paths + * <- This section manage the different type of end points + */ + + const val API_IDENTITY_PATH = "/identity/v1" + const val API_CUSTOMER_PATH = "/api/customer/v1" + const val API_DEPOSIT_PATH = "/api/deposit/v1" + const val API_PORTFOLIO_PATH = "/api/portfolio/v1" + const val API_ACCOUNTING_PATH = "/api/accounting/v1" + const val API_TELLER_PATH = "/api/teller/v1" + const val API_PAYROLL_PATH = "/api//payroll/v1" +} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/data/remote/FineractInterceptor.java b/app/src/main/java/org/apache/fineract/data/remote/FineractInterceptor.java deleted file mode 100644 index a5619697..00000000 --- a/app/src/main/java/org/apache/fineract/data/remote/FineractInterceptor.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * This project is licensed under the open source MPL V2. - * See https://github.com/openMF/android-client/blob/master/LICENSE.md - */ - -package org.apache.fineract.data.remote; - -import android.content.Context; -import androidx.annotation.NonNull; -import android.text.TextUtils; - -import org.apache.fineract.FineractApplication; -import org.apache.fineract.data.local.PreferenceKey; -import org.apache.fineract.data.local.PreferencesHelper; - -import java.io.IOException; -import java.util.HashSet; - -import javax.inject.Inject; - -import okhttp3.Interceptor; -import okhttp3.Request; -import okhttp3.Request.Builder; -import okhttp3.Response; - -/** - * @author Rajan Maurya - * @since 17/03/2017 - */ -public class FineractInterceptor implements Interceptor { - - public static final String HEADER_TENANT = "X-Tenant-Identifier"; - public static final String HEADER_AUTH = "Authorization"; - private static final String HEADER_ACCEPT_JSON = "Accept"; - private static final String HEADER_CONTENT_TYPE = "Content-type"; - public static final String HEADER_USER = "User"; - - @Inject - PreferencesHelper preferencesHelper; - - public FineractInterceptor(Context context) { - FineractApplication.get(context).getComponent().inject(this); - } - - @Override - public Response intercept(@NonNull Chain chain) throws IOException { - Request chainRequest = chain.request(); - Builder builder = chainRequest.newBuilder(); - - //TODO fix call single time instead of calling every request - String authToken = preferencesHelper.getAccessToken(); - String tenantIdentifier = preferencesHelper.getTenantIdentifier(); - String user = preferencesHelper.getUserName(); - Boolean refreshTokenStatus = preferencesHelper.getBoolean( - PreferenceKey.PREF_KEY_REFRESH_ACCESS_TOKEN, false); - - builder.header(HEADER_ACCEPT_JSON, "application/json"); - builder.header(HEADER_CONTENT_TYPE, "application/json"); - - if (refreshTokenStatus) { - //Add Cookies - HashSet cookies = (HashSet) preferencesHelper.getStringSet( - PreferenceKey.PREF_KEY_COOKIES); - if (cookies != null) { - for (String cookie : cookies) { - builder.addHeader("Cookie", cookie); - } - } - } else { - if (!TextUtils.isEmpty(authToken)) { - builder.header(HEADER_AUTH, authToken); - } - - if (!TextUtils.isEmpty(user)) { - builder.header(HEADER_USER, user); - } - } - - if (!TextUtils.isEmpty(tenantIdentifier)) { - builder.header(HEADER_TENANT, tenantIdentifier); - } - - Request request = builder.build(); - return chain.proceed(request); - } -} diff --git a/app/src/main/java/org/apache/fineract/data/remote/FineractInterceptor.kt b/app/src/main/java/org/apache/fineract/data/remote/FineractInterceptor.kt new file mode 100644 index 00000000..b6d21775 --- /dev/null +++ b/app/src/main/java/org/apache/fineract/data/remote/FineractInterceptor.kt @@ -0,0 +1,71 @@ +package org.apache.fineract.data.remote + +import android.content.Context +import android.text.TextUtils +import okhttp3.Interceptor +import okhttp3.Response +import org.apache.fineract.FineractApplication +import org.apache.fineract.data.local.PreferenceKey +import org.apache.fineract.data.local.PreferencesHelper +import java.io.IOException +import java.util.* +import javax.inject.Inject + +/** + * Created by Ahmad Jawid Muhammadi on 1/6/20 + */ + +class FineractInterceptor(context: Context?) : Interceptor { + + @Inject + lateinit var preferencesHelper: PreferencesHelper + + @Throws(IOException::class) + override fun intercept(chain: Interceptor.Chain): Response { + val chainRequest = chain.request() + val builder = chainRequest.newBuilder() + + //TODO fix call single time instead of calling every request + val authToken = preferencesHelper!!.accessToken + val tenantIdentifier = preferencesHelper!!.tenantIdentifier + val user = preferencesHelper!!.userName + val refreshTokenStatus = preferencesHelper!!.getBoolean( + PreferenceKey.PREF_KEY_REFRESH_ACCESS_TOKEN, false) + builder.header(HEADER_ACCEPT_JSON, "application/json") + builder.header(HEADER_CONTENT_TYPE, "application/json") + if (refreshTokenStatus) { + //Add Cookies + val cookies = preferencesHelper!!.getStringSet( + PreferenceKey.PREF_KEY_COOKIES) as HashSet + if (cookies != null) { + for (cookie in cookies) { + builder.addHeader("Cookie", cookie) + } + } + } else { + if (!TextUtils.isEmpty(authToken)) { + builder.header(HEADER_AUTH, authToken) + } + if (!TextUtils.isEmpty(user)) { + builder.header(HEADER_USER, user) + } + } + if (!TextUtils.isEmpty(tenantIdentifier)) { + builder.header(HEADER_TENANT, tenantIdentifier) + } + val request = builder.build() + return chain.proceed(request) + } + + companion object { + const val HEADER_TENANT = "X-Tenant-Identifier" + const val HEADER_AUTH = "Authorization" + private const val HEADER_ACCEPT_JSON = "Accept" + private const val HEADER_CONTENT_TYPE = "Content-type" + const val HEADER_USER = "User" + } + + init { + FineractApplication.get(context).component.inject(this) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/data/remote/FineractOkHttpClient.java b/app/src/main/java/org/apache/fineract/data/remote/FineractOkHttpClient.java deleted file mode 100644 index a97741f8..00000000 --- a/app/src/main/java/org/apache/fineract/data/remote/FineractOkHttpClient.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.apache.fineract.data.remote; - -import android.content.Context; - -import java.security.KeyStore; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; -import java.util.Arrays; -import java.util.concurrent.TimeUnit; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSession; -import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509TrustManager; - -import okhttp3.OkHttpClient; -import okhttp3.logging.HttpLoggingInterceptor; -import okhttp3.logging.HttpLoggingInterceptor.Level; - -public class FineractOkHttpClient { - - private Context context; - - public FineractOkHttpClient(Context context) { - this.context = context; - } - - public OkHttpClient getFineractOkHttpClient() { - - OkHttpClient.Builder builder = new OkHttpClient.Builder(); - - try { - // Create a trust manager that does not validate certificate chains - TrustManager[] trustAllCerts = { - new X509TrustManager() { - @Override - public void checkClientTrusted( - X509Certificate[] chain, - String authType) throws CertificateException { - } - - @Override - public void checkServerTrusted( - X509Certificate[] chain, - String authType) throws CertificateException { - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - } - }; - - // Install the all-trusting trust manager - SSLContext sslContext = SSLContext.getInstance("SSL"); - sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); - // Create an ssl socket factory with our all-trusting manager - SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); - - //Enable Full Body Logging - HttpLoggingInterceptor logger = new HttpLoggingInterceptor(); - logger.setLevel(Level.BODY); - - TrustManagerFactory trustManagerFactory = TrustManagerFactory. - getInstance(TrustManagerFactory.getDefaultAlgorithm()); - trustManagerFactory.init((KeyStore) null); - TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); - if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) { - throw new IllegalStateException("Unexpected default trust managers:" + Arrays. - toString(trustManagers)); - } - X509TrustManager trustManager = (X509TrustManager) trustManagers[0]; - - //Set SSL certificate to OkHttpClient Builder - - builder.sslSocketFactory(sslSocketFactory, trustManager); - - builder.hostnameVerifier(new HostnameVerifier() { - @Override - public boolean verify(String hostname, SSLSession session) { - return true; - } - }); - } catch (Exception e) { - throw new RuntimeException(e); - } - - //Enable Full Body Logging - HttpLoggingInterceptor logger = new HttpLoggingInterceptor(); - logger.setLevel(Level.BODY); - - //Setting Timeout 30 Seconds - builder.connectTimeout(60, TimeUnit.SECONDS); - builder.readTimeout(60, TimeUnit.SECONDS); - - //Interceptor :> Full Body Logger and ApiRequest Header - builder.addInterceptor(logger); - builder.addInterceptor(new ConnectivityInterceptor(context)); - builder.addInterceptor(new FineractInterceptor(context)); - builder.addInterceptor(new ReceivedCookiesInterceptor(context)); - - return builder.build(); - - } -} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/data/remote/FineractOkHttpClient.kt b/app/src/main/java/org/apache/fineract/data/remote/FineractOkHttpClient.kt new file mode 100644 index 00000000..cb5ba2cb --- /dev/null +++ b/app/src/main/java/org/apache/fineract/data/remote/FineractOkHttpClient.kt @@ -0,0 +1,85 @@ +package org.apache.fineract.data.remote + +import android.content.Context +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor +import java.security.KeyStore +import java.security.SecureRandom +import java.security.cert.CertificateException +import java.security.cert.X509Certificate +import java.util.* +import java.util.concurrent.TimeUnit +import javax.net.ssl.SSLContext +import javax.net.ssl.TrustManager +import javax.net.ssl.TrustManagerFactory +import javax.net.ssl.X509TrustManager + +/** + * Created by Ahmad Jawid Muhammadi on 1/6/20 + */ + +object FineractOkHttpClient { + + @JvmStatic + fun getFineractOkHttpClient(context: Context): OkHttpClient { + val builder = OkHttpClient.Builder() + try { + // Create a trust manager that does not validate certificate chains + val trustAllCerts = arrayOf( + object : X509TrustManager { + @Throws(CertificateException::class) + override fun checkClientTrusted( + chain: Array, + authType: String) { + } + + @Throws(CertificateException::class) + override fun checkServerTrusted( + chain: Array, + authType: String) { + } + + override fun getAcceptedIssuers(): Array { + return arrayOfNulls(0) + } + } + ) + + // Install the all-trusting trust manager + val sslContext = SSLContext.getInstance("SSL") + sslContext.init(null, trustAllCerts, SecureRandom()) + // Create an ssl socket factory with our all-trusting manager + val sslSocketFactory = sslContext.socketFactory + + //Enable Full Body Logging + val logger = HttpLoggingInterceptor() + logger.level = HttpLoggingInterceptor.Level.BODY + val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()) + trustManagerFactory.init(null as KeyStore?) + val trustManagers = trustManagerFactory.trustManagers + check(!(trustManagers.size != 1 || trustManagers[0] !is X509TrustManager)) { "Unexpected default trust managers:" + Arrays.toString(trustManagers) } + val trustManager = trustManagers[0] as X509TrustManager + + //Set SSL certificate to OkHttpClient Builder + builder.sslSocketFactory(sslSocketFactory, trustManager) + builder.hostnameVerifier { hostname, session -> true } + } catch (e: Exception) { + throw RuntimeException(e) + } + + //Enable Full Body Logging + val logger = HttpLoggingInterceptor() + logger.level = HttpLoggingInterceptor.Level.BODY + + //Setting Timeout 30 Seconds + builder.connectTimeout(60, TimeUnit.SECONDS) + builder.readTimeout(60, TimeUnit.SECONDS) + + //Interceptor :> Full Body Logger and ApiRequest Header + builder.addInterceptor(logger) + builder.addInterceptor(ConnectivityInterceptor(context)) + builder.addInterceptor(FineractInterceptor(context)) + builder.addInterceptor(ReceivedCookiesInterceptor(context)) + return builder.build() + } +} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/data/remote/NullOnEmptyConverterFactory.java b/app/src/main/java/org/apache/fineract/data/remote/NullOnEmptyConverterFactory.java index 58e3af7c..df2b31ba 100644 --- a/app/src/main/java/org/apache/fineract/data/remote/NullOnEmptyConverterFactory.java +++ b/app/src/main/java/org/apache/fineract/data/remote/NullOnEmptyConverterFactory.java @@ -1,6 +1,5 @@ package org.apache.fineract.data.remote; -import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Type; @@ -15,12 +14,9 @@ public class NullOnEmptyConverterFactory extends Converter.Factory { Retrofit retrofit) { final Converter delegate = retrofit.nextResponseBodyConverter(this, type, annotations); - return new Converter() { - @Override - public Object convert(ResponseBody body) throws IOException { - if (body.contentLength() == 0) return null; - return delegate.convert(body); - } + return (Converter) body -> { + if (body.contentLength() == 0) return null; + return delegate.convert(body); }; } } diff --git a/app/src/main/java/org/apache/fineract/data/remote/ReceivedCookiesInterceptor.java b/app/src/main/java/org/apache/fineract/data/remote/ReceivedCookiesInterceptor.java deleted file mode 100644 index cde04c89..00000000 --- a/app/src/main/java/org/apache/fineract/data/remote/ReceivedCookiesInterceptor.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.apache.fineract.data.remote; - -import android.content.Context; - -import org.apache.fineract.FineractApplication; -import org.apache.fineract.data.local.PreferenceKey; -import org.apache.fineract.data.local.PreferencesHelper; - -import java.io.IOException; -import java.util.HashSet; - -import javax.inject.Inject; - -import okhttp3.Interceptor; -import okhttp3.Response; - -public class ReceivedCookiesInterceptor implements Interceptor { - - @Inject - PreferencesHelper preferencesHelper; - - public ReceivedCookiesInterceptor(Context context) { - FineractApplication.get(context).getComponent().inject(this); - } - - @Override - public Response intercept(Chain chain) throws IOException { - Response originalResponse = chain.proceed(chain.request()); - - if (!originalResponse.headers("Set-Cookie").isEmpty()) { - HashSet cookies = new HashSet<>(); - for (String header : originalResponse.headers("Set-Cookie")) { - cookies.add(header); - } - preferencesHelper.putStringSet(PreferenceKey.PREF_KEY_COOKIES, cookies); - } - return originalResponse; - } -} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/data/remote/ReceivedCookiesInterceptor.kt b/app/src/main/java/org/apache/fineract/data/remote/ReceivedCookiesInterceptor.kt new file mode 100644 index 00000000..8337f67b --- /dev/null +++ b/app/src/main/java/org/apache/fineract/data/remote/ReceivedCookiesInterceptor.kt @@ -0,0 +1,38 @@ +package org.apache.fineract.data.remote + +import android.content.Context +import okhttp3.Interceptor +import okhttp3.Response +import org.apache.fineract.FineractApplication +import org.apache.fineract.data.local.PreferenceKey +import org.apache.fineract.data.local.PreferencesHelper +import java.io.IOException +import java.util.* +import javax.inject.Inject + +/** + * Created by Ahmad Jawid Muhammadi on 1/6/20 + */ + +class ReceivedCookiesInterceptor(context: Context?) : Interceptor { + + @Inject + lateinit var preferencesHelper: PreferencesHelper + + @Throws(IOException::class) + override fun intercept(chain: Interceptor.Chain): Response { + val originalResponse = chain.proceed(chain.request()) + if (originalResponse.headers("Set-Cookie").isNotEmpty()) { + val cookies = HashSet() + for (header in originalResponse.headers("Set-Cookie")) { + cookies.add(header) + } + preferencesHelper!!.putStringSet(PreferenceKey.PREF_KEY_COOKIES, cookies) + } + return originalResponse + } + + init { + FineractApplication.get(context).component.inject(this) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/ui/base/FineractBaseActivity.java b/app/src/main/java/org/apache/fineract/ui/base/FineractBaseActivity.java index f40b6974..e4d953ee 100644 --- a/app/src/main/java/org/apache/fineract/ui/base/FineractBaseActivity.java +++ b/app/src/main/java/org/apache/fineract/ui/base/FineractBaseActivity.java @@ -6,18 +6,20 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; -import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; +import com.mifos.mobile.passcode.BasePassCodeActivity; + import org.apache.fineract.FineractApplication; import org.apache.fineract.R; import org.apache.fineract.injection.component.ActivityComponent; import org.apache.fineract.injection.component.ConfigPersistentComponent; import org.apache.fineract.injection.component.DaggerConfigPersistentComponent; import org.apache.fineract.injection.module.ActivityModule; +import org.apache.fineract.ui.online.PassCodeActivity; import java.util.HashMap; import java.util.Map; @@ -29,7 +31,7 @@ * creation of Dagger components and makes sure that instances of ConfigPersistentComponent survive * across configuration changes. */ -public class FineractBaseActivity extends AppCompatActivity implements BaseActivityCallback { +public class FineractBaseActivity extends BasePassCodeActivity implements BaseActivityCallback { private static final String KEY_ACTIVITY_ID = "KEY_ACTIVITY_ID"; private static final AtomicLong NEXT_ID = new AtomicLong(0); @@ -187,4 +189,9 @@ public void clearFragmentBackStack() { public int stackCount() { return getSupportFragmentManager().getBackStackEntryCount(); } + + @Override + public Class getPassCodeClass() { + return PassCodeActivity.class; + } } diff --git a/app/src/main/java/org/apache/fineract/ui/online/PassCodeActivity.kt b/app/src/main/java/org/apache/fineract/ui/online/PassCodeActivity.kt new file mode 100644 index 00000000..9cbbf844 --- /dev/null +++ b/app/src/main/java/org/apache/fineract/ui/online/PassCodeActivity.kt @@ -0,0 +1,44 @@ +package org.apache.fineract.ui.online + +import android.content.Intent +import android.os.Bundle +import android.os.PersistableBundle +import android.view.View +import com.mifos.mobile.passcode.MifosPassCodeActivity +import com.mifos.mobile.passcode.utils.EncryptionUtil +import org.apache.fineract.R +import org.apache.fineract.ui.base.Toaster +import org.apache.fineract.ui.online.login.LoginActivity + + +/* + * Created by saksham on 12/June/2019 +*/ + +class PassCodeActivity : MifosPassCodeActivity() { + + override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { + super.onCreate(savedInstanceState, persistentState) + } + + override fun showToaster(view: View?, msg: Int) { + Toaster.show(view, msg, Toaster.SHORT) + } + + override fun startLoginActivity() { + startActivity(Intent(this, LoginActivity::class.java)) + finish() + } + + override fun getLogo(): Int { + return R.drawable.mifos_logo_new + } + + override fun getEncryptionType(): Int { + return EncryptionUtil.FINERACT_CN + } + + override fun startNextActivity() { + startActivity(Intent(this, DashboardActivity::class.java)) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/ui/online/SplashActivity.kt b/app/src/main/java/org/apache/fineract/ui/online/SplashActivity.kt index a336dc09..b85c9dfc 100644 --- a/app/src/main/java/org/apache/fineract/ui/online/SplashActivity.kt +++ b/app/src/main/java/org/apache/fineract/ui/online/SplashActivity.kt @@ -14,5 +14,6 @@ class SplashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) startActivity(Intent(this, LauncherActivity::class.java)) + finish() } } \ No newline at end of file diff --git a/app/src/main/java/org/apache/fineract/ui/online/launcher/LauncherActivity.java b/app/src/main/java/org/apache/fineract/ui/online/launcher/LauncherActivity.java index f36c3049..07a44033 100644 --- a/app/src/main/java/org/apache/fineract/ui/online/launcher/LauncherActivity.java +++ b/app/src/main/java/org/apache/fineract/ui/online/launcher/LauncherActivity.java @@ -3,12 +3,14 @@ import android.content.Intent; import android.os.Bundle; +import com.mifos.mobile.passcode.utils.PassCodeConstants; + import org.apache.fineract.R; import org.apache.fineract.data.local.PreferenceKey; import org.apache.fineract.data.local.PreferencesHelper; import org.apache.fineract.data.models.Authentication; import org.apache.fineract.ui.base.FineractBaseActivity; -import org.apache.fineract.ui.online.DashboardActivity; +import org.apache.fineract.ui.online.PassCodeActivity; import org.apache.fineract.ui.online.login.LoginActivity; import org.apache.fineract.utils.DateUtils; @@ -46,18 +48,19 @@ public void checkAccessTokenExpired() { if (DateUtils.isTokenExpired(authentication.getAccessTokenExpiration())) { checkRefreshAccessToken(); } else { - startActivity(DashboardActivity.class); + startPasscodeActivity(); } } else { - startActivity(LoginActivity.class); + startLoginActivity(); } + finish(); } @Override public void checkRefreshAccessToken() { Authentication authentication = preferencesHelper.getSignedInUser(); if (DateUtils.isTokenExpired(authentication.getRefreshTokenExpiration())) { - startActivity(LoginActivity.class); + startLoginActivity(); } else { //Refresh access token preferencesHelper.putBoolean(PreferenceKey.PREF_KEY_REFRESH_ACCESS_TOKEN, true); @@ -66,24 +69,30 @@ public void checkRefreshAccessToken() { } @Override - public void startActivity(Class aClass) { - Intent intent = new Intent(this, aClass); + public void startLoginActivity() { + Intent intent = new Intent(this, LoginActivity.class); startActivity(intent); finish(); } + public void startPasscodeActivity() { + Intent intent = new Intent(this, PassCodeActivity.class); + intent.putExtra(PassCodeConstants.PASSCODE_INITIAL_LOGIN, true); + startActivity(intent); + } + @Override public void refreshAccessTokenSuccessfully(Authentication authentication) { preferencesHelper.putBoolean(PreferenceKey.PREF_KEY_REFRESH_ACCESS_TOKEN, false); preferencesHelper.putAccessToken(authentication.getAccessToken()); preferencesHelper.putSignInUser(authentication); - startActivity(DashboardActivity.class); + startPasscodeActivity(); } @Override public void refreshAccessTokenFailed() { clearCredentials(); - startActivity(LoginActivity.class); + startLoginActivity(); } @Override diff --git a/app/src/main/java/org/apache/fineract/ui/online/launcher/LauncherContract.java b/app/src/main/java/org/apache/fineract/ui/online/launcher/LauncherContract.java index 694f633a..97e2bfad 100644 --- a/app/src/main/java/org/apache/fineract/ui/online/launcher/LauncherContract.java +++ b/app/src/main/java/org/apache/fineract/ui/online/launcher/LauncherContract.java @@ -15,7 +15,9 @@ interface View extends MvpView { void checkRefreshAccessToken(); - void startActivity(Class aClass); + void startLoginActivity(); + + void startPasscodeActivity(); void refreshAccessTokenSuccessfully(Authentication authentication); diff --git a/app/src/main/java/org/apache/fineract/ui/online/login/LoginActivity.java b/app/src/main/java/org/apache/fineract/ui/online/login/LoginActivity.java index 9956991f..cb8683d3 100644 --- a/app/src/main/java/org/apache/fineract/ui/online/login/LoginActivity.java +++ b/app/src/main/java/org/apache/fineract/ui/online/login/LoginActivity.java @@ -6,12 +6,14 @@ import android.widget.EditText; import android.widget.Toast; +import com.mifos.mobile.passcode.utils.PassCodeConstants; + import org.apache.fineract.R; import org.apache.fineract.data.local.PreferencesHelper; import org.apache.fineract.data.models.Authentication; import org.apache.fineract.ui.base.FineractBaseActivity; import org.apache.fineract.ui.base.Toaster; -import org.apache.fineract.ui.online.DashboardActivity; +import org.apache.fineract.ui.online.PassCodeActivity; import javax.inject.Inject; @@ -83,7 +85,9 @@ public void showUserLoginSuccessfully(Authentication user) { preferencesHelper.putAccessToken(user.getAccessToken()); preferencesHelper.putSignInUser(user); preferencesHelper.putUserName(etUsername.getEditableText().toString().trim()); - startActivity(new Intent(this, DashboardActivity.class)); + Intent intent = new Intent(this, PassCodeActivity.class); + intent.putExtra(PassCodeConstants.PASSCODE_INITIAL_LOGIN, true); + startActivity(intent); finish(); Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_LONG).show(); } diff --git a/app/src/main/java/org/apache/fineract/ui/online/login/LoginPresenter.java b/app/src/main/java/org/apache/fineract/ui/online/login/LoginPresenter.java index a32ad484..aaea3277 100644 --- a/app/src/main/java/org/apache/fineract/ui/online/login/LoginPresenter.java +++ b/app/src/main/java/org/apache/fineract/ui/online/login/LoginPresenter.java @@ -1,6 +1,5 @@ package org.apache.fineract.ui.online.login; -import static org.apache.fineract.data.remote.BaseApiManager.retrofit; import android.content.Context; import android.util.Log; @@ -28,6 +27,8 @@ import retrofit2.HttpException; import retrofit2.Response; +import static org.apache.fineract.data.remote.BaseApiManager.retrofit; + /** * @author Rajan Maurya * On 17/06/17. diff --git a/app/src/main/java/org/apache/fineract/utils/MifosErrorUtils.java b/app/src/main/java/org/apache/fineract/utils/MifosErrorUtils.java index fc50b061..ffac7a0f 100644 --- a/app/src/main/java/org/apache/fineract/utils/MifosErrorUtils.java +++ b/app/src/main/java/org/apache/fineract/utils/MifosErrorUtils.java @@ -1,7 +1,5 @@ package org.apache.fineract.utils; -import static org.apache.fineract.data.remote.BaseApiManager.retrofit; - import android.content.Context; import android.util.Log; @@ -16,6 +14,8 @@ import retrofit2.HttpException; import retrofit2.Response; +import static org.apache.fineract.data.remote.BaseApiManager.retrofit; + /** * @author Rajan Maurya * On 18/06/17.