From e99fb82cdad539e0467d047b5d5df6986295138a Mon Sep 17 00:00:00 2001 From: Yash Khare Date: Sat, 8 Aug 2020 16:10:51 +0530 Subject: [PATCH] Fix #147 Add feature to autofill the survey --- .../computer_vision/ComputerVisionActivity.kt | 19 ++--- .../ui/new_survey/NewPPISurveyActivity.kt | 14 +++- .../ui/new_survey/PPIQuestionFragment.kt | 72 ++++++++++++++++++- .../main/res/layout/ppi_question_layout.xml | 16 ++++- 4 files changed, 109 insertions(+), 12 deletions(-) diff --git a/visionppi/app/src/main/java/org/mifos/visionppi/ui/computer_vision/ComputerVisionActivity.kt b/visionppi/app/src/main/java/org/mifos/visionppi/ui/computer_vision/ComputerVisionActivity.kt index 278ad95..0cd7f32 100644 --- a/visionppi/app/src/main/java/org/mifos/visionppi/ui/computer_vision/ComputerVisionActivity.kt +++ b/visionppi/app/src/main/java/org/mifos/visionppi/ui/computer_vision/ComputerVisionActivity.kt @@ -38,11 +38,13 @@ class ComputerVisionActivity : AppCompatActivity(), ComputerVisionMVPView { val labelList: MutableList = ArrayList() + companion object { + @JvmStatic var finalLabels: MutableList> = ArrayList() + } + lateinit var localModel: LocalModel lateinit var customImageLabelerOptions: CustomImageLabelerOptions lateinit var imageLabeler: ImageLabeler - - var finalLabels: MutableList> = ArrayList() var counter = 0 var imageNos = 0 @@ -109,13 +111,13 @@ class ComputerVisionActivity : AppCompatActivity(), ComputerVisionMVPView { override fun fetchFromGallery() { try { if (ActivityCompat.checkSelfPermission( - this@ComputerVisionActivity, - Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED + this@ComputerVisionActivity, + Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions( - this@ComputerVisionActivity, - arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE), - PICK_FROM_GALLERY + this@ComputerVisionActivity, + arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE), + PICK_FROM_GALLERY ) } else { val intent = Intent() @@ -148,6 +150,7 @@ class ComputerVisionActivity : AppCompatActivity(), ComputerVisionMVPView { } override fun analyzeImages() { + res_list.adapter = null for (image in images) { detect(image!!) } @@ -219,7 +222,7 @@ class ComputerVisionActivity : AppCompatActivity(), ComputerVisionMVPView { PICK_FROM_GALLERY -> if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { val galleryIntent = - Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) + Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) startActivityForResult(galleryIntent, PICK_FROM_GALLERY) } else { showToastMessage(getString(R.string.cant_open_gallery)) diff --git a/visionppi/app/src/main/java/org/mifos/visionppi/ui/new_survey/NewPPISurveyActivity.kt b/visionppi/app/src/main/java/org/mifos/visionppi/ui/new_survey/NewPPISurveyActivity.kt index ece2a6c..4750561 100644 --- a/visionppi/app/src/main/java/org/mifos/visionppi/ui/new_survey/NewPPISurveyActivity.kt +++ b/visionppi/app/src/main/java/org/mifos/visionppi/ui/new_survey/NewPPISurveyActivity.kt @@ -2,6 +2,7 @@ package org.mifos.visionppi.ui.new_survey import android.content.Intent import android.os.Bundle +import android.util.Log import android.view.View import android.widget.Toast import androidx.fragment.app.Fragment @@ -22,6 +23,7 @@ class NewPPISurveyActivity : FragmentActivity(), NewSurveyMVPView { private lateinit var mPager: ViewPager var mNewSurveyPresenter : NewSurveyPresenter = NewSurveyPresenter() lateinit var mPPISurvey: PPISurvey + var list: MutableList> = ArrayList() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -39,6 +41,16 @@ class NewPPISurveyActivity : FragmentActivity(), NewSurveyMVPView { } } + override fun onResume() { + super.onResume() + Log.d("NewPPISurveyActivity", ComputerVisionActivity.finalLabels.toString()) + if (ComputerVisionActivity.finalLabels.isNotEmpty()) { + list = ComputerVisionActivity.finalLabels + val s: String = mPPISurvey.questionDatas.get(mPager.currentItem).text + Log.d("NewPPISurveyActivity", s) + } + } + fun launchCVActivity(v: View) { val intent = Intent(applicationContext, ComputerVisionActivity::class.java) startActivity(intent) @@ -59,7 +71,7 @@ class NewPPISurveyActivity : FragmentActivity(), NewSurveyMVPView { private inner class ScreenSlidePagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) { override fun getCount(): Int = mPPISurvey.questionDatas.size - override fun getItem(position: Int): Fragment = PPIQuestionFragment(mPPISurvey.questionDatas.get(position), applicationContext) { response: Response -> onResponseClicked(response) } + override fun getItem(position: Int): Fragment = PPIQuestionFragment(mPPISurvey.questionDatas.get(position), applicationContext, list) { response: Response -> onResponseClicked(response) } } diff --git a/visionppi/app/src/main/java/org/mifos/visionppi/ui/new_survey/PPIQuestionFragment.kt b/visionppi/app/src/main/java/org/mifos/visionppi/ui/new_survey/PPIQuestionFragment.kt index 69a2656..c128de7 100644 --- a/visionppi/app/src/main/java/org/mifos/visionppi/ui/new_survey/PPIQuestionFragment.kt +++ b/visionppi/app/src/main/java/org/mifos/visionppi/ui/new_survey/PPIQuestionFragment.kt @@ -2,6 +2,7 @@ package org.mifos.visionppi.ui.new_survey import android.content.Context import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -14,12 +15,81 @@ import org.mifos.visionppi.objects.Question import org.mifos.visionppi.objects.Response -class PPIQuestionFragment( var questionData: Question, val mContext: Context, val responseClick :(response : Response) -> Unit) : Fragment() { +class PPIQuestionFragment(var questionData: Question, val mContext: Context, val list: MutableList>, val responseClick: (response: Response) -> Unit) : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { val view = inflater.inflate(R.layout.ppi_question_layout, container, false) view.question.text = questionData.text view.responses.layoutManager = LinearLayoutManager(context) + if (!list.isNullOrEmpty()) { + var s: String = "" + var c: Int = 0 + var thatch = false + var sc = false + var fridge = false + var washer = false + var tv = false + var player = false + var mh = false + list.forEach { + it.forEach { + if (it.contains("thatch", ignoreCase = true)) { + thatch = true + } else if (it.contains("mobile home", ignoreCase = true) || it.contains("patio", ignoreCase = true)) { + mh = true + } else if (it.contains("studio couch", ignoreCase = true)) { + sc = true + } else if (it.contains("refrigerator", ignoreCase = true)) { + fridge = true + } else if (it.contains("washer", ignoreCase = true)) { + washer = true + } else if (it.contains("television", ignoreCase = true) || it.contains("entertainment center", ignoreCase = true) || it.contains("home theater", ignoreCase = true)) { + tv = true + } else if (it.contains("cassette player", ignoreCase = true) || it.contains("CD player", ignoreCase = true) || it.contains("tape player", ignoreCase = true)) { + player = true + } else if (it.contains("cellular telephone", ignoreCase = true) || it.contains("dial telephone", ignoreCase = true)) { + ++c; + } + } + } + + if (questionData.text.contains("construction materials", ignoreCase = true)) { + if (thatch && !mh) { + s = "The outer walls are made up of predominantly light materials" + } else { + s = "The outer walls are made of predominantly strong or strong materials" + } + } else if (questionData.text.contains("sala sets", ignoreCase = true)) { + if (sc) { + s = "There is atleast 1 salsa set present" + } else { + s = "There is no salsa set" + } + } else if (questionData.text.contains("washing machine", ignoreCase = true)) { + if (fridge && !washer) { + s = "Only a refrigerator is present" + } else if (washer && !fridge) { + s = "Only a washing machine is present" + } else if (washer && fridge) { + s = "Both are present" + } else { + s = "Neither are present" + } + } else if (questionData.text.contains("television", ignoreCase = true)) { + if (tv && !player) { + s = "Only television is present" + } else if (player && !tv) { + s = "A VTR/VHS/VCD/DVD Player is present" + } else if (!tv && !player) { + s = "Neither is pres" + } + } else if (questionData.text.contains("telephones", ignoreCase = true)) { + s = "There are $c telephones detected" + } + + view.res.text = s + view.res.visibility = View.VISIBLE + } view.responses.adapter = ResponseAdapter(questionData.responseDatas, mContext, responseClick) return view } diff --git a/visionppi/app/src/main/res/layout/ppi_question_layout.xml b/visionppi/app/src/main/res/layout/ppi_question_layout.xml index de1629c..204b521 100644 --- a/visionppi/app/src/main/res/layout/ppi_question_layout.xml +++ b/visionppi/app/src/main/res/layout/ppi_question_layout.xml @@ -36,15 +36,27 @@ + android:layout_height="wrap_content"> + + +