-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12884 from nextcloud/feature/app-config
App Config With Restriction Manager
- Loading branch information
Showing
11 changed files
with
270 additions
and
9 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
app/src/androidTest/java/com/nextcloud/utils/AppConfigManagerTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Nextcloud - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2024 Your Name <[email protected]> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
package com.nextcloud.utils | ||
|
||
import android.os.Bundle | ||
import com.owncloud.android.AbstractIT | ||
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory | ||
import com.owncloud.android.utils.appConfig.AppConfigKeys | ||
import com.owncloud.android.utils.appConfig.AppConfigManager | ||
import org.junit.AfterClass | ||
import org.junit.Test | ||
|
||
class AppConfigManagerTests : AbstractIT() { | ||
|
||
private val testBaseUrl = "nextcloud.cloud.cloud" | ||
private val testProxyHost = "nextcloud.cloud.cloud.com" | ||
|
||
@Suppress("MagicNumber") | ||
private val testProxyPort = 8800 | ||
|
||
@Test | ||
fun testSetProxyConfigWhenGivenClientBrandedPlusAndCorrectBundleDataProxyConfigurationShouldSet() { | ||
val proxySetting = Bundle().apply { | ||
putString(AppConfigKeys.ProxyHost.key, testProxyHost) | ||
putInt(AppConfigKeys.ProxyPort.key, testProxyPort) | ||
} | ||
|
||
AppConfigManager(targetContext, proxySetting).run { | ||
setProxyConfig(true) | ||
} | ||
|
||
val proxyHost = OwnCloudClientManagerFactory.getProxyHost() | ||
val proxyPort = OwnCloudClientManagerFactory.getProxyPort() | ||
|
||
assert(proxyHost.equals(testProxyHost)) | ||
assert(proxyPort == testProxyPort) | ||
} | ||
|
||
@Test | ||
fun testSetProxyConfigWhenGivenClientNotBrandedPlusAndCorrectBundleDataProxyConfigurationShouldNotSet() { | ||
val proxySetting = Bundle().apply { | ||
putString(AppConfigKeys.ProxyHost.key, testProxyHost) | ||
putInt(AppConfigKeys.ProxyPort.key, testProxyPort) | ||
} | ||
|
||
AppConfigManager(targetContext, proxySetting).run { | ||
setProxyConfig(false) | ||
} | ||
|
||
val proxyHost = OwnCloudClientManagerFactory.getProxyHost() | ||
val proxyPort = OwnCloudClientManagerFactory.getProxyPort() | ||
|
||
assert(proxyHost.equals("")) | ||
assert(proxyPort == -1) | ||
} | ||
|
||
@Test | ||
fun testGetBaseUrlConfigWhenGivenClientBrandedPlusAndCorrectBundleDataBaseUrlConfigurationShouldSet() { | ||
val baseUrlConfig = Bundle().apply { | ||
putString(AppConfigKeys.BaseUrl.key, testBaseUrl) | ||
} | ||
val sut = AppConfigManager(targetContext, baseUrlConfig) | ||
assert(!sut.getBaseUrl(true).isNullOrEmpty()) | ||
} | ||
|
||
@Test | ||
fun testGetBaseUrlConfigWhenGivenClientBrandedPlusAndBrokenBundleDataBaseUrlConfigurationShouldNotSet() { | ||
val baseUrlConfig = Bundle() | ||
val sut = AppConfigManager(targetContext, baseUrlConfig) | ||
assert(sut.getBaseUrl(true).isNullOrEmpty()) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
@AfterClass | ||
fun tearDown() { | ||
OwnCloudClientManagerFactory.setProxyHost("") | ||
OwnCloudClientManagerFactory.setProxyPort(-1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/owncloud/android/utils/appConfig/AppConfigKeys.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Nextcloud - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2024 Your Name <[email protected]> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
package com.owncloud.android.utils.appConfig | ||
|
||
/** | ||
* These keys are connected to app_config.xml | ||
*/ | ||
enum class AppConfigKeys(val key: String) { | ||
BaseUrl("base_url"), | ||
ProxyHost("proxy_host"), | ||
ProxyPort("proxy_port") | ||
} |
68 changes: 68 additions & 0 deletions
68
app/src/main/java/com/owncloud/android/utils/appConfig/AppConfigManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Nextcloud - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2024 Your Name <[email protected]> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
package com.owncloud.android.utils.appConfig | ||
|
||
import android.content.Context | ||
import android.content.res.Resources | ||
import android.os.Bundle | ||
import android.text.TextUtils | ||
import com.owncloud.android.R | ||
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory | ||
import com.owncloud.android.lib.common.utils.Log_OC | ||
|
||
class AppConfigManager(private val context: Context, private val appRestrictions: Bundle) { | ||
|
||
private val tag = "AppConfigManager" | ||
|
||
fun setProxyConfig(isBrandedPlus: Boolean) { | ||
if (!isBrandedPlus) { | ||
Log_OC.d(tag, "Proxy configuration cannot be set. Client is not branded plus.") | ||
return | ||
} | ||
|
||
val host = if (appRestrictions.containsKey(AppConfigKeys.ProxyHost.key)) { | ||
appRestrictions.getString(AppConfigKeys.ProxyHost.key) | ||
} else { | ||
context.getString(R.string.proxy_host) | ||
} | ||
|
||
val port = if (appRestrictions.containsKey(AppConfigKeys.ProxyPort.key)) { | ||
appRestrictions.getInt(AppConfigKeys.ProxyPort.key) | ||
} else { | ||
context.resources.getInteger(R.integer.proxy_port) | ||
} | ||
|
||
if (TextUtils.isEmpty(host) || port == -1) { | ||
Log_OC.d(tag, "Proxy configuration cannot be found") | ||
return | ||
} | ||
|
||
try { | ||
OwnCloudClientManagerFactory.setProxyHost(host) | ||
OwnCloudClientManagerFactory.setProxyPort(port) | ||
|
||
Log_OC.d(tag, "Proxy configuration successfully set") | ||
} catch (e: Resources.NotFoundException) { | ||
Log_OC.e(tag, "Proxy config cannot able to set due to: $e") | ||
} | ||
} | ||
|
||
fun getBaseUrl(isBrandedPlus: Boolean): String? { | ||
if (!isBrandedPlus) { | ||
Log_OC.d(tag, "Proxy configuration cannot be set. Client is not branded plus. Default url applied") | ||
return null | ||
} | ||
|
||
return if (appRestrictions.containsKey(AppConfigKeys.BaseUrl.key)) { | ||
appRestrictions.getString(AppConfigKeys.BaseUrl.key) | ||
} else { | ||
Log_OC.d(tag, "BaseUrl configuration cannot be found, default url applied") | ||
null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.