Commit 893b86a3 authored by Christophe Deschamps's avatar Christophe Deschamps
Browse files

Code cleanup


Former-commit-id: e600d8ab777482a2b0ae5f6564e333efd6263f3e
parent d4de780f
......@@ -27,7 +27,8 @@ import org.lindoor.LindoorApplication.Companion.coreContext
import org.lindoor.LindoorApplication.Companion.ensureCoreExists
import org.lindoor.utils.DialogUtil
abstract class GenericActivity(val allowsLandscapeOnSmartPhones: Boolean = false) : AppCompatActivity() {
abstract class GenericActivity(val allowsLandscapeOnSmartPhones: Boolean = false) :
AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -43,7 +44,7 @@ abstract class GenericActivity(val allowsLandscapeOnSmartPhones: Boolean = false
// Remove service notification if it has been started by device boot
coreContext.notificationsManager.stopForegroundNotificationIfPossible()
if(!allowsLandscapeOnSmartPhones && !LindoorApplication.instance.tablet()) {
if (!allowsLandscapeOnSmartPhones && !LindoorApplication.instance.tablet()) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
......
......@@ -5,7 +5,7 @@ import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import org.lindoor.ui.toolbar.ToobarButtonClickedListener
abstract class GenericFragment: Fragment(),
abstract class GenericFragment : Fragment(),
ToobarButtonClickedListener {
val mainactivity get() = activity as MainActivity
......@@ -26,7 +26,8 @@ abstract class GenericFragment: Fragment(),
fun hideKeyboard() {
mainactivity.currentFocus?.also {
val imm = it.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
val imm =
it.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(it.windowToken, 0)
}
}
......@@ -34,6 +35,7 @@ abstract class GenericFragment: Fragment(),
fun showProgress() {
mainactivity.toolbarViewModel.activityInprogress.value = true
}
fun hideProgress() {
mainactivity.toolbarViewModel.activityInprogress.value = false
}
......
......@@ -33,7 +33,10 @@ class LindoorApplication : Application() {
corePreferences = CorePreferences(context)
corePreferences.copyAssetsFromPackage() // TODO Move in the zip - attention not to overwrite .linphone_rc
val config = Factory.instance().createConfigWithFactory(corePreferences.configPath, corePreferences.factoryConfigPath)
val config = Factory.instance().createConfigWithFactory(
corePreferences.configPath,
corePreferences.factoryConfigPath
)
corePreferences.config = config
Factory.instance().setDebugMode(corePreferences.debugLogs, Texts.appName)
......@@ -44,6 +47,7 @@ class LindoorApplication : Application() {
}
override fun onCreate() {
super.onCreate()
instance = this
......
......@@ -42,13 +42,14 @@ class MainActivity : GenericActivity() {
super.onCreate(savedInstanceState)
val decorView: View = window.decorView
val uiOptions: Int = View.SYSTEM_UI_FLAG_VISIBLE
decorView.setSystemUiVisibility(uiOptions)
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
val uiOptions: Int = View.SYSTEM_UI_FLAG_VISIBLE
decorView.systemUiVisibility = uiOptions
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
applyCommonTheme()
val binding:ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val binding: ActivityMainBinding =
DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.lifecycleOwner = this
toolbarViewModel = ViewModelProvider(this).get(ToolbarViewModel::class.java)
......@@ -65,40 +66,41 @@ class MainActivity : GenericActivity() {
applyTheme()
tabbar_devices.setOnClickListener {
if (tabBarLayoutClicked(tabbar_devices,tabbar_history))
if (tabBarLayoutClicked(tabbar_devices, tabbar_history))
navController.navigate(R.id.navigation_devices, null)
}
tabbar_history.setOnClickListener {
if (tabBarLayoutClicked(tabbar_history,tabbar_devices))
if (tabBarLayoutClicked(tabbar_history, tabbar_devices))
navController.navigate(R.id.navigation_history, null)
}
tabBarLayoutClicked(tabbar_devices,tabbar_history)
tabBarLayoutClicked(tabbar_devices, tabbar_history)
navController.addOnDestinationChangedListener {
_, destination, _ ->
navController.addOnDestinationChangedListener { _, destination, _ ->
if (sideMenuOpened()) {
navControllerSideMenu.navigateUp()
}
when (destination.id) {
R.id.navigation_devices,R.id.navigation_history -> {
R.id.navigation_devices, R.id.navigation_history -> {
enterRootFragment()
} else -> {
enterNonRootFragment()
}
}
else -> {
enterNonRootFragment()
}
}
toolbar_title.text = titleForNavigationFragment(destination.id)
}
navControllerSideMenu.addOnDestinationChangedListener {
_, destination, _ ->
navControllerSideMenu.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.fragment_sidemenu -> {
enterNonRootFragment()
toolbar_title.text = null
} else -> {
enterRootFragment()
toolbar_title.text = titleForNavigationFragment(navController.currentDestination?.id)
}
}
else -> {
enterRootFragment()
toolbar_title.text =
titleForNavigationFragment(navController.currentDestination?.id)
}
}
}
......@@ -155,13 +157,13 @@ class MainActivity : GenericActivity() {
toolbar.progress.setBackgroundColor(Theme.getColor("color_j"))
}
private fun tabBarLayoutClicked(clicked: ViewGroup, unclicked: ViewGroup):Boolean {
private fun tabBarLayoutClicked(clicked: ViewGroup, unclicked: ViewGroup): Boolean {
if (clicked.isSelected)
return false
clicked.isSelected = true
unclicked.isSelected = false
Theme.alphAnimate(clicked,1f)
Theme.alphAnimate(unclicked,0.3f)
Theme.alphAnimate(clicked, 1f)
Theme.alphAnimate(unclicked, 0.3f)
return true
}
......@@ -187,12 +189,11 @@ class MainActivity : GenericActivity() {
}
fun sideMenuOpened(): Boolean {
return navControllerSideMenu.currentDestination?.id != R.id.fragment_empty
}
private fun titleForNavigationFragment(fragmentId:Int?): String? {
private fun titleForNavigationFragment(fragmentId: Int?): String? {
return when (fragmentId) {
R.id.navigation_assistant_root,
R.id.navigation_assistant_create_lindoor,
......@@ -201,7 +202,9 @@ class MainActivity : GenericActivity() {
R.id.navigation_assistant_remote_root,
R.id.navigation_assistant_remote_qr,
R.id.navigation_assistant_remote_url -> Texts.get("assistant")
R.id.navigation_devices, R.id.navigation_device_edit, R.id.navigation_device_info -> Texts.get("devices")
R.id.navigation_devices, R.id.navigation_device_edit, R.id.navigation_device_info -> Texts.get(
"devices"
)
R.id.navigation_history -> Texts.get("history")
R.id.navigation_about -> Texts.get("about")
R.id.navigation_settings -> Texts.get("settings")
......@@ -210,16 +213,20 @@ class MainActivity : GenericActivity() {
}
@SuppressLint("NeedOnRequestPermissionsResult")
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (permissions.size > 0)
onRequestPermissionsResult(requestCode, grantResults)
}
@NeedsPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
fun start() {}
fun start() {
}
@OnPermissionDenied(Manifest.permission.WRITE_EXTERNAL_STORAGE)
fun onStorageDenied() {
......@@ -233,9 +240,9 @@ class MainActivity : GenericActivity() {
fun applyCommonTheme() {
getWindow().also { window ->
window.setStatusBarColor(Theme.getColor("color_a"))
window.setNavigationBarColor(Theme.getColor("color_j"))
window.also { window ->
window.statusBarColor = Theme.getColor("color_a")
window.navigationBarColor = Theme.getColor("color_j")
}
}
......@@ -244,11 +251,11 @@ class MainActivity : GenericActivity() {
if (sideMenuOpened()) {
if (event != null) {
val overFlowPct =
if (LindoorApplication.instance.smartPhone()) 0.75
else if (LindoorApplication.instance.landcape()) 0.3
if (LindoorApplication.instance.smartPhone()) 0.75
else if (LindoorApplication.instance.landcape()) 0.3
else 0.5
if (event.x.toInt() > overFlowPct * getWindow().getDecorView().getWidth()) {
if (event.x.toInt() > overFlowPct * window.decorView.width) {
navControllerSideMenu.navigateUp()
return false
}
......
......@@ -11,7 +11,7 @@ import kotlinx.coroutines.launch
import org.lindoor.customisation.Theme
import org.lindoor.databinding.ActivitySplashBinding
class SplashActivity : GenericActivity () {
class SplashActivity : GenericActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val decorView: View = window.decorView
......@@ -22,7 +22,7 @@ class SplashActivity : GenericActivity () {
DataBindingUtil.setContentView(this, R.layout.activity_splash) as ActivitySplashBinding
GlobalScope.launch(context = Dispatchers.Main) {
delay(Theme.arbitraryValue("splash_display_duration_ms","2000").toLong())
delay(Theme.arbitraryValue("splash_display_duration_ms", "2000").toLong())
val intent = Intent(this@SplashActivity, MainActivity::class.java)
startActivity(intent)
finish()
......
......@@ -58,8 +58,8 @@ class Api23Compatibility {
return Settings.canDrawOverlays(context)
}
fun vibrateOneShot(v:Vibrator) {
v!!.vibrate(500)
fun vibrateOneShot(v: Vibrator) {
v.vibrate(500)
}
}
......
......@@ -99,7 +99,7 @@ class Api26Compatibility {
vibrator.vibrate(effect, audioAttrs)
}
fun vibrateOneShot(v:Vibrator) {
fun vibrateOneShot(v: Vibrator) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE))
}
}
......
......@@ -100,13 +100,13 @@ class Compatibility {
}
fun vibrateOneShot() {
val v =
LindoorApplication.coreContext.context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
Api26Compatibility.vibrateOneShot(v)
} else {
Api23Compatibility.vibrateOneShot(v)
}
val v =
LindoorApplication.coreContext.context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
Api26Compatibility.vibrateOneShot(v)
} else {
Api23Compatibility.vibrateOneShot(v)
}
}
fun setColorFilter(
......
......@@ -6,25 +6,30 @@ import java.util.*
object ActionTypes {
var spinnerItems: ArrayList<SpinnerItem> = ArrayList()
init {
actionTypesConfig.let { config ->
config.sectionsNamesList.forEach {
spinnerItems.add(
SpinnerItem(config.getString(it,"textkey","missing"),config.getString(it,"icon",null),it)
SpinnerItem(
config.getString(it, "textkey", "missing"),
config.getString(it, "icon", null),
it
)
)
}
}
}
fun typeNameForActionType(typeKey:String):String {
fun typeNameForActionType(typeKey: String): String {
return actionTypesConfig.let { config ->
Texts.get(config.getString(typeKey,"textkey",null))
Texts.get(config.getString(typeKey, "textkey", null))
}
}
fun iconNameForActionType(typeKey:String):String {
fun iconNameForActionType(typeKey: String): String {
return actionTypesConfig.let { config ->
config.getString(typeKey,"icon",null)
config.getString(typeKey, "icon", null)
}
}
......
......@@ -6,11 +6,12 @@ import java.util.*
object ActionsMethodTypes {
var spinnerItems: ArrayList<SpinnerItem> = ArrayList()
init {
actionsMethodTypesConfig.let { config ->
config.sectionsNamesList.forEach {
spinnerItems.add(
SpinnerItem(config.getString(it,"textkey","missing"),null,it)
SpinnerItem(config.getString(it, "textkey", "missing"), null, it)
)
}
}
......
......@@ -53,19 +53,23 @@ object Customisation {
if (zipmd5 == context.getSharedPreferences(
context.getString(R.string.app_name),
Context.MODE_PRIVATE
).getString(context.getString(R.string.zip_md5),null)
).getString(context.getString(R.string.zip_md5), null)
) {
android.util.Log.i("","[Customisation] md5 identical - custo has not changed.")
android.util.Log.i("", "[Customisation] md5 identical - custo has not changed.")
zipInputStream.close()
return
}
if (zip.unzipInputStream(zipInputStream,LindoorApplication.instance.filesDir.absolutePath))
if (zip.unzipInputStream(
zipInputStream,
LindoorApplication.instance.filesDir.absolutePath
)
)
context.getSharedPreferences(
context.getString(R.string.app_name),
Context.MODE_PRIVATE
).edit().putString(context.getString(R.string.zip_md5), zipmd5).apply()
} catch (e: Exception) {
android.util.Log.e("","[Customisation] Failed unzipping zip from assets : $e")
android.util.Log.e("", "[Customisation] Failed unzipping zip from assets : $e")
e.printStackTrace()
return
}
......
......@@ -6,41 +6,45 @@ import java.util.*
object DeviceTypes {
var deviceTypes: ArrayList<SpinnerItem> = ArrayList()
lateinit var defaultType:String
lateinit var defaultType: String
init {
deviceTypesConfig.let { config ->
config.sectionsNamesList.forEach {
if (config.getBool(it,"default",false))
if (config.getBool(it, "default", false))
defaultType = it
deviceTypes.add(
SpinnerItem(config.getString(it,"textkey","missing"),config.getString(it,"icon",null),it)
SpinnerItem(
config.getString(it, "textkey", "missing"),
config.getString(it, "icon", null),
it
)
)
}
}
}
fun iconNameForDeviceType(typeKey:String, circle:Boolean = false):String? {
fun iconNameForDeviceType(typeKey: String, circle: Boolean = false): String? {
return deviceTypesConfig.let { config ->
config.getString(typeKey,"icon"+(if (circle) "_circle" else ""),null)
config.getString(typeKey, "icon" + (if (circle) "_circle" else ""), null)
}
}
fun typeNameForDeviceType(typeKey:String):String? {
fun typeNameForDeviceType(typeKey: String): String? {
return deviceTypesConfig.let { config ->
Texts.get(config.getString(typeKey,"textkey", deviceTypes.get(0).backingKey))
Texts.get(config.getString(typeKey, "textkey", deviceTypes.get(0).backingKey))
}
}
fun supportsAudio(typeKey:String):Boolean {
fun supportsAudio(typeKey: String): Boolean {
return deviceTypesConfig.let { config ->
config.getBool(typeKey,"hasaudio",true)
config.getBool(typeKey, "hasaudio", true)
}
}
fun supportsVideo(typeKey:String):Boolean {
fun supportsVideo(typeKey: String): Boolean {
return deviceTypesConfig.let { config ->
config.getBool(typeKey,"hasvideo",false)
config.getBool(typeKey, "hasvideo", false)
}
}
......
......@@ -18,31 +18,31 @@ object Texts {
return text
}
private fun pureGet(key: String):String {
private fun pureGet(key: String): String {
val deviceLanguage = Locale.getDefault().language.toLowerCase(Locale.ROOT)
return textsConfig.let { config ->
config.getString(key,deviceLanguage,null)?.also { translation ->
config.getString(key, deviceLanguage, null)?.also { translation ->
return translation
} ?: config.getString(key,"default",null)?.also {default ->
} ?: config.getString(key, "default", null)?.also { default ->
return default
} ?: key
}
}
fun get(textKey: String, args: Array<String>? = null): String {
return formatText(textKey,args)
return formatText(textKey, args)
}
fun get(textKey: String, oneArg: String): String {
return get(textKey, arrayOf(oneArg))
}
fun get(textKey: String, arg1: String, arg2:String): String {
return get(textKey, arrayOf(arg1,arg2))
fun get(textKey: String, arg1: String, arg2: String): String {
return get(textKey, arrayOf(arg1, arg2))
}
fun get(key: String): String {
return pureGet(key).replace("{appname}",appName).replace("\n",System.lineSeparator())
return pureGet(key).replace("{appname}", appName).replace("\n", System.lineSeparator())
}
}
......@@ -33,7 +33,11 @@ object Theme {
private var themeError: Boolean = false
private fun makeGradientDrawable(fromColor: Int, toColor: Int, orientation: String): GradientDrawable {
private fun makeGradientDrawable(
fromColor: Int,
toColor: Int,
orientation: String
): GradientDrawable {
val realOrientation = when (orientation) {
"top_bottom" -> GradientDrawable.Orientation.TOP_BOTTOM
"bottom_top" -> GradientDrawable.Orientation.BOTTOM_TOP
......@@ -46,8 +50,8 @@ object Theme {
fun getGradientColor(key: String): GradientDrawable? {
val entireKey = "gradient-color.$key"
themeConfig.getString(entireKey,"from",null)?.let { from ->
themeConfig.getString(entireKey,"to",null)?.let { to ->
themeConfig.getString(entireKey, "from", null)?.let { from ->
themeConfig.getString(entireKey, "to", null)?.let { to ->
return themeConfig.getString(entireKey, "orientation", null)
?.let { orientation ->
makeGradientDrawable(
......@@ -77,7 +81,7 @@ object Theme {
}
fun arbitraryValue(key: String, default: String): String {
var result:String? = themeConfig.getString("arbitrary-values", key, null)
var result: String? = themeConfig.getString("arbitrary-values", key, null)
if (result == null) {
Log.e("[Theme] Failed retrieving arbitrary value:$key")
themeError()
......@@ -87,7 +91,7 @@ object Theme {
}
fun arbitraryValue(key: String, default: Boolean): Boolean {
var result:Boolean? = themeConfig.getBool("arbitrary-values", key, default)
var result: Boolean? = themeConfig.getBool("arbitrary-values", key, default)
if (result == null) {
Log.e("[Theme] Failed retrieving arbitrary value:$key")
themeError()
......@@ -96,7 +100,10 @@ object Theme {
return result
}
fun setIcon(imageName: String, imageView: ImageView) { // Preferred SVG, fallback PNG or full name.
fun setIcon(
imageName: String,
imageView: ImageView
) { // Preferred SVG, fallback PNG or full name.
val svg = File(LindoorApplication.instance.filesDir, "images/$imageName.svg")
if (svg.exists())
glidesvg.load(svg).into(imageView)
......@@ -121,7 +128,7 @@ object Theme {
)
if (fromAssets != Typeface.DEFAULT)
return fromAssets
} catch (exception:Exception) {
} catch (exception: Exception) {
Log.i("[Theme] font $key is not on assets, trying from zip. be optimized putting in assets.")
}
return Typeface.createFromFile(
......@@ -133,8 +140,7 @@ object Theme {
}
fun roundRectGradientDrawable(color:Int, radius:Float): GradientDrawable {
fun roundRectGradientDrawable(color: Int, radius: Float): GradientDrawable {
val shape = GradientDrawable()
shape.shape = GradientDrawable.RECTANGLE
shape.setColor(color)
......@@ -142,16 +148,21 @@ object Theme {
return shape
}
fun roundRectGradientDrawableWithStroke(color:Int, radius:Float, strokeColor:Int, strokeWidth:Int): GradientDrawable {
fun roundRectGradientDrawableWithStroke(
color: Int,
radius: Float,
strokeColor: Int,
strokeWidth: Int
): GradientDrawable {
val shape = GradientDrawable()
shape.shape = GradientDrawable.RECTANGLE
shape.setColor(color)
shape.setStroke(strokeWidth,strokeColor)
shape.setStroke(strokeWidth, strokeColor)
shape.cornerRadius = pxFromDp(radius)
return shape
}
fun circleGradientDrawable(colorKey:String): GradientDrawable {
fun circleGradientDrawable(colorKey: String): GradientDrawable {
val shape = GradientDrawable()
shape.shape = GradientDrawable.OVAL
shape.setColor(Color.TRANSPARENT)
......@@ -160,17 +171,30 @@ object Theme {
}