Skip to content

Commit

Permalink
0.0.16a
Browse files Browse the repository at this point in the history
- Improved code (asking of permissions)
  • Loading branch information
Sav22999 committed Jan 9, 2020
1 parent bec1264 commit 2b119d6
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 148 deletions.
2 changes: 1 addition & 1 deletion Common Voice Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
applicationId "org.commonvoice.saverio"
minSdkVersion 23
targetSdkVersion 28
versionCode = 28
versionCode = 29
versionName '0.0.16a'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,25 @@ class ListenActivity : AppCompatActivity() {

fun checkPermissions() {
try {
checkRecordVoicePermission()
} catch (e: java.lang.Exception) {
//println(" -->> Exception: " + e.toString())
}
try {
val PERMISSIONS = arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
!= PackageManager.PERMISSION_GRANTED
) {
checkStoragePermission()
ActivityCompat.requestPermissions(
this,
PERMISSIONS,
RECORD_REQUEST_CODE
)
}
} catch (e: java.lang.Exception) {
//println(" -->> Exception: " + e.toString())
} catch (e: Exception) {
//
}
}

Expand Down Expand Up @@ -403,30 +410,6 @@ class ListenActivity : AppCompatActivity() {
)
}

fun checkRecordVoicePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.RECORD_AUDIO),
RECORD_REQUEST_CODE
)
}
}

fun checkStoragePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
RECORD_REQUEST_CODE
)
}
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,42 +307,25 @@ class LoginActivity : AppCompatActivity() {

fun checkPermissions() {
try {
checkRecordVoicePermission()
} catch (e: java.lang.Exception) {
//println(" -->> Exception: " + e.toString())
}
try {
val PERMISSIONS = arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
!= PackageManager.PERMISSION_GRANTED
) {
checkStoragePermission()
ActivityCompat.requestPermissions(
this,
PERMISSIONS,
RECORD_REQUEST_CODE
)
}
} catch (e: java.lang.Exception) {
//println(" -->> Exception: " + e.toString())
}
}

fun checkRecordVoicePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.INTERNET),
RECORD_REQUEST_CODE
)
}
}

fun checkStoragePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
RECORD_REQUEST_CODE
)
} catch (e: Exception) {
//
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources
import android.net.ConnectivityManager
import android.os.Build
import android.os.Bundle
import android.widget.*
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.startActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import java.lang.Exception
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.util.*


Expand Down Expand Up @@ -351,7 +347,8 @@ class MainActivity : AppCompatActivity() {
this.selectedLanguageVar = lang

if (languageChanged) {
val sharedPref2: SharedPreferences = getSharedPreferences(UI_LANGUAGE_CHANGED, PRIVATE_MODE)
val sharedPref2: SharedPreferences =
getSharedPreferences(UI_LANGUAGE_CHANGED, PRIVATE_MODE)
val editor2 = sharedPref2.edit()
editor2.putBoolean(UI_LANGUAGE_CHANGED, true)
editor2.apply()
Expand Down Expand Up @@ -430,42 +427,25 @@ class MainActivity : AppCompatActivity() {

fun checkPermissions() {
try {
checkRecordVoicePermission()
} catch (e: Exception) {
//println(" -->> Exception: " + e.toString())
}
try {
val PERMISSIONS = arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
!= PackageManager.PERMISSION_GRANTED
) {
checkStoragePermission()
ActivityCompat.requestPermissions(
this,
PERMISSIONS,
RECORD_REQUEST_CODE
)
}
} catch (e: Exception) {
//println(" -->> Exception: " + e.toString())
}
}

fun checkRecordVoicePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.RECORD_AUDIO),
RECORD_REQUEST_CODE
)
}
}

fun checkStoragePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
RECORD_REQUEST_CODE
)
//
}
}

Expand All @@ -478,6 +458,8 @@ class MainActivity : AppCompatActivity() {
RECORD_REQUEST_CODE -> {
if (grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
checkPermissions()
} else {
checkPermissions()
}
}
}
Expand All @@ -501,7 +483,7 @@ class MainActivity : AppCompatActivity() {
res.updateConfiguration(config, res.displayMetrics)
//createConfigurationContext(config)

if (restart || type=="restart") {
if (restart || type == "restart") {
val intent = Intent(this, RestartActivity::class.java).also {
startActivity(it)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.android.volley.AuthFailureError
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonArrayRequest
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONObject
Expand All @@ -51,7 +52,7 @@ class SpeakActivity : AppCompatActivity() {
var url: String =
"https://voice.mozilla.org/api/v1/{{*{{lang}}*}}/" //API url -> replace {{*{{lang}}*}} with the selected_language
var tempUrl: String =
"https://voice.allizom.org/api/v1/{{*{{lang}}*}}/" //TEST API url
"http://52.169.248.132:9000/api/v1/{{*{{lang}}*}}/" //TEST API url

val urlWithoutLang: String =
"https://voice.mozilla.org/api/v1/" //API url (without lang)
Expand Down Expand Up @@ -88,7 +89,6 @@ class SpeakActivity : AppCompatActivity() {
this.selectedLanguage = sharedPref2.getString(LANGUAGE_NAME, "en")
this.url = this.url.replace("{{*{{lang}}*}}", this.selectedLanguage)


var skipButton: Button = this.findViewById(R.id.btn_skip_speak)
skipButton.setOnClickListener {
StopRecording()
Expand Down Expand Up @@ -199,7 +199,7 @@ class SpeakActivity : AppCompatActivity() {
} else {
headers.put(
"Authorization",
"Basic MzVmNmFmZTItZjY1OC00YTNhLThhZGMtNzQ0OGM2YTM0MjM3OjNhYzAwMWEyOTQyZTM4YzBiNmQwMWU0M2RjOTk0YjY3NjA0YWRmY2Q="
"Basic OWNlYzFlZTgtZGZmYS00YWU1LWI3M2MtYjg3NjM1OThjYmVjOjAxOTdmODU2NjhlMDQ3NTlhOWUxNzZkM2Q2MDdkOTEzNDE3ZGZkMjA="
)
}
return headers
Expand Down Expand Up @@ -422,7 +422,7 @@ class SpeakActivity : AppCompatActivity() {
} else {
println("output: " + output)
//encoded = Files.readAllBytes(Paths.get(this.output!!))
encoded = getFileBytes(this, output!!)
encoded = Files.readAllBytes(Paths.get(this.output!!))

val byteArray = output?.let { it.toByteArray() }

Expand Down Expand Up @@ -453,7 +453,7 @@ class SpeakActivity : AppCompatActivity() {
try {
val path = "clips" //API to get sentences
val params = JSONArray()
params.put(encoded2)
params.put(encoded)

tempUrl = tempUrl.replace("{{*{{lang}}*}}", this.selectedLanguage)

Expand All @@ -471,9 +471,9 @@ class SpeakActivity : AppCompatActivity() {
btnSkip.isEnabled = true
}
) {
override fun getBodyContentType(): String {
/*override fun getBodyContentType(): String {
return "application/octet-stream"//Use this function to set Content-Type for Volley
}
}*/

@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
Expand All @@ -495,7 +495,7 @@ class SpeakActivity : AppCompatActivity() {
"Basic MzVmNmFmZTItZjY1OC00YTNhLThhZGMtNzQ0OGM2YTM0MjM3OjNhYzAwMWEyOTQyZTM4YzBiNmQwMWU0M2RjOTk0YjY3NjA0YWRmY2Q="
)
}
//headers.put("Content-Type", "application/octet-stream")
headers.put("Content-Type", "application/octet-stream")
headers.put("sentence", encode(textSentence, "UTF-8").replace("+", "%20"))
headers.put("sentence_id", idSentence)
println(
Expand Down Expand Up @@ -554,42 +554,25 @@ class SpeakActivity : AppCompatActivity() {

fun checkPermissions() {
try {
checkRecordVoicePermission()
} catch (e: java.lang.Exception) {
//println(" -->> Exception: " + e.toString())
}
try {
val PERMISSIONS = arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
!= PackageManager.PERMISSION_GRANTED
) {
checkStoragePermission()
ActivityCompat.requestPermissions(
this,
PERMISSIONS,
RECORD_REQUEST_CODE
)
}
} catch (e: java.lang.Exception) {
//println(" -->> Exception: " + e.toString())
}
}

fun checkRecordVoicePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.RECORD_AUDIO),
RECORD_REQUEST_CODE
)
}
}

fun checkStoragePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
RECORD_REQUEST_CODE
)
} catch (e: Exception) {
//
}
}

Expand Down

0 comments on commit 2b119d6

Please sign in to comment.