Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AC-821 Fix Java and Kotlin deprecation warnings #938

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ package com.openmrs.android_sdk.utilities

import android.content.Context
import android.net.ConnectivityManager
import android.preference.PreferenceManager
import com.openmrs.android_sdk.library.OpenmrsAndroid

@Suppress("DEPRECATION")
object NetworkUtils {
@JvmStatic
fun hasNetwork(): Boolean {
Expand All @@ -28,7 +28,7 @@ object NetworkUtils {

@JvmStatic
fun isOnline(): Boolean {
val prefs = PreferenceManager.getDefaultSharedPreferences(OpenmrsAndroid.getInstance())
val prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(OpenmrsAndroid.getInstance())
val toggle = prefs.getBoolean("sync", true)
return if (toggle) {
val connectivityManager = OpenmrsAndroid.getInstance()?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ActiveVisitsRecyclerViewAdapter(private val mContext: Context, private val
}

override fun onBindViewHolder(visitViewHolder: VisitViewHolder, position: Int) {
val adapterPos = visitViewHolder.adapterPosition
val adapterPos = visitViewHolder.absoluteAdapterPosition
val visit = this.mVisits?.get(adapterPos)
val patient = PatientDAO().findPatientByID(visit?.patient?.id.toString())
visitViewHolder.mVisitPlace.text = mContext.getString(R.string.visit_in, visit?.location?.display)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
@file:Suppress("DEPRECATION")

LuGO0 marked this conversation as resolved.
Show resolved Hide resolved
package org.openmrs.mobile.activities.dashboard

import android.content.Intent
import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.os.Bundle
import android.util.SparseArray
Expand All @@ -31,7 +33,6 @@ import com.github.amlcurran.showcaseview.ShowcaseView
import com.github.amlcurran.showcaseview.targets.Target
import com.github.amlcurran.showcaseview.targets.ViewTarget
import org.openmrs.mobile.R
import org.openmrs.mobile.activities.ACBaseActivity
import org.openmrs.mobile.activities.ACBaseFragment
import org.openmrs.mobile.databinding.FragmentDashboardBinding
import org.openmrs.mobile.utilities.ThemeUtils
Expand All @@ -52,6 +53,7 @@ class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), Dashboa
private var mProviderManagementView: RelativeLayout? = null
private var mBitmapCache: SparseArray<Bitmap?>? = null

@SuppressLint("CommitPrefEdits")
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

Expand Down Expand Up @@ -109,14 +111,12 @@ class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), Dashboa
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
savedInstanceState: Bundle?): View {

_binding = FragmentDashboardBinding.inflate(inflater, container, false)
val root = binding.root
if (root != null) {
initFragmentFields(binding)
setListeners()
}
initFragmentFields(binding)
setListeners()
return root
}

Expand Down Expand Up @@ -193,14 +193,6 @@ class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), Dashboa
}
}

/**
* Starts new Activity depending on which ImageView triggered it
*/
private fun startNewActivity(clazz: Class<out ACBaseActivity?>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Anshul-9923 As I can see, this PR is about fixing Java & Kotlin deprecation warnings but I see some deletions for the redundant code (which is not wrong). @LuGO0 Shouldn't a separate PR be opened for removing redundant code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or better remove all the redundant code under this PR only? @LuGO0

val intent = Intent(activity, clazz)
startActivity(intent)
}

override fun onClick(v: View) {
val directionToRegister = DashboardFragmentDirections.actionDashboardFragmentToAddEditPatientActivity()
val directionToFindPatent = DashboardFragmentDirections.actionDashboardFragmentToSyncedPatientsActivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.openmrs.android_sdk.library.api.repository.ProviderRepository
import org.openmrs.mobile.application.OpenMRS
import com.openmrs.android_sdk.library.listeners.retrofitcallbacks.DefaultResponseCallback

@Suppress("UNREACHABLE_CODE")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to suppress this warning. It can be resolved.

class FormAdmissionPresenter : BasePresenter, FormAdmissionContract.Presenter {
private var view: FormAdmissionContract.View
private var patientID: Long? = null
Expand Down Expand Up @@ -60,7 +61,7 @@ class FormAdmissionPresenter : BasePresenter, FormAdmissionContract.Presenter {
this.restApi = restApi
view.setPresenter(this)
mContext = context
providerRepository = ProviderRepository(restApi, logger);
providerRepository = ProviderRepository(restApi, logger)
}

override fun subscribe() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.mobile.activities.formentrypatientlist

import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView
import com.openmrs.android_sdk.library.models.Patient
import com.openmrs.android_sdk.library.models.Visit
Expand All @@ -33,15 +35,15 @@ class FormEntryPatientListAdapter(private val mContext: FormEntryPatientListFrag
return PatientViewHolder(itemView)
}

override fun onBindViewHolder(holder: PatientViewHolder, position: Int) {
val adapterPos = holder.adapterPosition
override fun onBindViewHolder(holder: PatientViewHolder, position: Int){
val adapterPos = holder.absoluteAdapterPosition
val patient = this.mItems?.get(position)
VisitDAO().getActiveVisitByPatientId(patient?.id)
.observeOn(AndroidSchedulers.mainThread())
.subscribe { visit: Visit? ->
if (visit != null) {
val icon = mContext.resources.getDrawable(R.drawable.active_visit_dot)
icon.setBounds(0, 0, icon.intrinsicHeight, icon.intrinsicWidth)
val icon:Drawable? = ResourcesCompat.getDrawable(Resources.getSystem(),R.drawable.active_visit_dot,null)
icon?.setBounds(0, 0, icon.intrinsicHeight, icon.intrinsicWidth)
holder.mVisitStatus.setCompoundDrawables(icon, null, null, null)
holder.mVisitStatus.text = mContext.getString(R.string.active_visit_label_capture_vitals)
holder.mRowLayout.setOnClickListener { mContext.startEncounterForPatient(mItems?.get(adapterPos)?.id) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package org.openmrs.mobile.activities.formlist

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Typeface
import android.os.Build
Expand Down Expand Up @@ -45,21 +46,27 @@ import retrofit2.Response
import java.io.IOException
import java.nio.charset.StandardCharsets

public final class FormListFragment : ACBaseFragment<FormListContract.Presenter?>(), FormListContract.View {
class FormListFragment : ACBaseFragment<FormListContract.Presenter?>(), FormListContract.View {
private var _binding: FragmentFormListBinding? = null
private val binding get() = _binding!!
private var snackbar: Snackbar? = null

@Nullable
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentFormListBinding.inflate(inflater, container, false)
val root: View = binding.root
binding.formlist.setOnItemClickListener({ parent: AdapterView<*>?, view: View, position: Int, id: Long -> mPresenter!!.listItemClicked(position, (view as TextView).text.toString()) })
binding.formlist.setOnItemClickListener { _: AdapterView<*>?, view: View, position: Int, _: Long ->
mPresenter!!.listItemClicked(
position,
(view as TextView).text.toString()
)
}
return root
}

@SuppressLint("InflateParams")
override fun showFormList(forms: Array<String?>?) {
if (forms!!.size == 0) {
if (forms!!.isEmpty()) {
snackbar = Snackbar.make(binding.root, ApplicationConstants.EMPTY_STRING, Snackbar.LENGTH_INDEFINITE)
val customSnackBarView = layoutInflater.inflate(R.layout.snackbar, null)
val snackBarLayout = snackbar!!.view as SnackbarLayout
Expand All @@ -69,7 +76,7 @@ public final class FormListFragment : ACBaseFragment<FormListContract.Presenter?
val dismissButton = customSnackBarView.findViewById<TextView>(R.id.snackbar_action_button)
val typeface = Typeface.createFromAsset(requireActivity().assets, ApplicationConstants.TypeFacePathConstants.ROBOTO_MEDIUM)
dismissButton.typeface = typeface
dismissButton.setOnClickListener { v: View? -> snackbar!!.dismiss() }
dismissButton.setOnClickListener { snackbar!!.dismiss() }
snackBarLayout.addView(customSnackBarView, 0)
snackbar!!.show()
}
Expand Down Expand Up @@ -104,68 +111,72 @@ public final class FormListFragment : ACBaseFragment<FormListContract.Presenter?
override fun formCreate(uuid: String?, formName: String?): Boolean? {
formCreateFlag = false
val apiService = RestServiceBuilder.createService(RestApi::class.java)
if (formName!!.contains("admission")) {
val obj = loadJSONFromAsset("admission.json")
val call2 = apiService.formCreate(uuid, obj)
call2.enqueue(object : Callback<FormCreate> {
override fun onResponse(call: Call<FormCreate>, response: Response<FormCreate>) {
if (response.isSuccessful && response.body()!!.name == "json") {
formCreateFlag = true
}
}

override fun onFailure(call: Call<FormCreate>, t: Throwable) {
//This method is lef blank intentionally
}
})
} else if (formName.contains("vitals")) {
val obj = loadJSONFromAsset("vitals1.json")
val obj2 = loadJSONFromAsset("vitals2.json")
val call2 = apiService.formCreate(uuid, obj)
call2.enqueue(object : Callback<FormCreate> {
override fun onResponse(call: Call<FormCreate>, response: Response<FormCreate>) {
if (response.isSuccessful && response.body()!!.name == "json") {
formCreateFlag = true
}
}

override fun onFailure(call: Call<FormCreate>, t: Throwable) {
//This method is lef blank intentionally
}
})
val call = apiService.formCreate(uuid, obj2)
call.enqueue(object : Callback<FormCreate> {
override fun onResponse(call: Call<FormCreate>, response: Response<FormCreate>) {
if (response.isSuccessful && response.body()!!.name == "json") {
formCreateFlag = true
}
}

override fun onFailure(call: Call<FormCreate>, t: Throwable) {
//This method is lef blank intentionally
}
})
} else if (formName.contains("visit note")) {
val obj = loadJSONFromAsset("visit_note.json")
val call2 = apiService.formCreate(uuid, obj)
call2.enqueue(object : Callback<FormCreate> {
override fun onResponse(call: Call<FormCreate>, response: Response<FormCreate>) {
if (response.isSuccessful && response.body()!!.name == "json") {
formCreateFlag = true
}
}

override fun onFailure(call: Call<FormCreate>, t: Throwable) {
//This method is lef blank intentionally
}
})
when {
formName!!.contains("admission") -> {
val obj = loadJSONFromAsset("admission.json")
val call2 = apiService.formCreate(uuid, obj)
call2.enqueue(object : Callback<FormCreate> {
override fun onResponse(call: Call<FormCreate>, response: Response<FormCreate>) {
if (response.isSuccessful && response.body()!!.name == "json") {
formCreateFlag = true
}
}

override fun onFailure(call: Call<FormCreate>, t: Throwable) {
//This method is lef blank intentionally
}
})
}
formName.contains("vitals") -> {
val obj = loadJSONFromAsset("vitals1.json")
val obj2 = loadJSONFromAsset("vitals2.json")
val call2 = apiService.formCreate(uuid, obj)
call2.enqueue(object : Callback<FormCreate> {
override fun onResponse(call: Call<FormCreate>, response: Response<FormCreate>) {
if (response.isSuccessful && response.body()!!.name == "json") {
formCreateFlag = true
}
}

override fun onFailure(call: Call<FormCreate>, t: Throwable) {
//This method is lef blank intentionally
}
})
val call = apiService.formCreate(uuid, obj2)
call.enqueue(object : Callback<FormCreate> {
override fun onResponse(call: Call<FormCreate>, response: Response<FormCreate>) {
if (response.isSuccessful && response.body()!!.name == "json") {
formCreateFlag = true
}
}

override fun onFailure(call: Call<FormCreate>, t: Throwable) {
//This method is lef blank intentionally
}
})
}
formName.contains("visit note") -> {
val obj = loadJSONFromAsset("visit_note.json")
val call2 = apiService.formCreate(uuid, obj)
call2.enqueue(object : Callback<FormCreate> {
override fun onResponse(call: Call<FormCreate>, response: Response<FormCreate>) {
if (response.isSuccessful && response.body()!!.name == "json") {
formCreateFlag = true
}
}

override fun onFailure(call: Call<FormCreate>, t: Throwable) {
//This method is lef blank intentionally
}
})
}
}
return formCreateFlag
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private fun loadJSONFromAsset(filename: String): FormData? {
var json: String? = null
val json: String?
json = try {
val `is` = requireActivity().assets.open("forms/$filename")
val size = `is`.available()
Expand All @@ -177,7 +188,7 @@ public final class FormListFragment : ACBaseFragment<FormListContract.Presenter?
ex.printStackTrace()
return null
}
var obj: JSONObject? = null
val obj: JSONObject?
try {
obj = JSONObject(json)
val data = FormData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LogsFragment : ACBaseFragment<LogsContract.Presenter>(), LogsContract.View
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
savedInstanceState: Bundle?): View {
_binding = FragmentLogsBinding.inflate(inflater, container, false)
return binding.root
}
Expand All @@ -42,13 +42,13 @@ class LogsFragment : ACBaseFragment<LogsContract.Presenter>(), LogsContract.View

override fun fabCopyAll(textLogs: String?) {
binding.fab.setOnClickListener {
setClipboard(context, textLogs)
setClipboard(textLogs)
Toast.makeText(context, R.string.logs_copied_to_clipboard_message,
Toast.LENGTH_SHORT).show()
}
}

private fun setClipboard(context: Context?, text: String?) {
private fun setClipboard(text: String?) {
val clipboard = requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(getString(R.string.copied_text), text)
clipboard.setPrimaryClip(clip)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.widget.EditText
import com.openmrs.android_sdk.utilities.ApplicationConstants
import com.openmrs.android_sdk.utilities.StringUtils.notEmpty

@Suppress("UNREACHABLE_CODE")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this too. Can be resolved.

class PatientBirthdateValidatorWatcher(private val eddob: EditText,
private val edmonth: EditText,
private val edyr: EditText) : TextWatcher {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
*/
package org.openmrs.mobile.utilities

import android.annotation.SuppressLint
import com.openmrs.android_sdk.utilities.DateUtils.getDateFromString
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.formatter.IAxisValueFormatter
import com.github.mikephil.charting.formatter.ValueFormatter
import java.text.DateFormat
import java.text.SimpleDateFormat
import kotlin.collections.ArrayList
import kotlin.math.abs

class DayAxisValueFormatter(private val dates: ArrayList<String>) : ValueFormatter(), IAxisValueFormatter {
@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class DayAxisValueFormatter(private val dates: ArrayList<String>) : ValueFormatter() {
@SuppressLint("SimpleDateFormat")
override fun getFormattedValue(value: Float, axis: AxisBase): String {
val intValue = value.toInt()
val vitalDate = getDateFromString(dates[abs(intValue)])
Expand Down