Skip to content

Commit d1474ca

Browse files
committed
Add preference for work account
1 parent cbf4784 commit d1474ca

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

play-services-auth-workaccount/core/src/main/kotlin/com/google/android/gms/auth/account/authenticator/WorkAccountAuthenticator.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.microg.gms.auth.AuthConstants
1919
import org.microg.gms.common.PackageUtils
2020
import org.microg.gms.auth.AuthRequest
2121
import org.microg.gms.auth.AuthResponse
22+
import org.microg.gms.auth.workaccount.WorkProfileSettings
2223
import java.io.IOException
2324
import kotlin.jvm.Throws
2425

@@ -38,7 +39,14 @@ class WorkAccountAuthenticator(val context: Context) : AbstractAccountAuthentica
3839
requiredFeatures: Array<out String>?,
3940
options: Bundle
4041
): Bundle? {
41-
if (
42+
43+
if (!WorkProfileSettings(context).allowCreateWorkAccount) {
44+
return Bundle().apply {
45+
putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION)
46+
putString(AccountManager.KEY_ERROR_MESSAGE, context.getString(R.string.auth_work_authenticator_disabled_error)
47+
)
48+
}
49+
} else if (
4250
!options.containsKey(KEY_ACCOUNT_CREATION_TOKEN)
4351
|| options.getString(KEY_ACCOUNT_CREATION_TOKEN) == null
4452
|| options.getInt(AccountManager.KEY_CALLER_UID) != android.os.Process.myUid()) {

play-services-auth-workaccount/core/src/main/kotlin/org/microg/gms/auth/workaccount/WorkAccountSettings.kt

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.microg.gms.auth.workaccount
2+
3+
import android.content.ContentValues
4+
import android.content.Context
5+
import android.database.Cursor
6+
import org.microg.gms.settings.SettingsContract
7+
8+
class WorkProfileSettings(private val context: Context) {
9+
private fun <T> getSettings(vararg projection: String, f: (Cursor) -> T): T =
10+
SettingsContract.getSettings(
11+
context,
12+
SettingsContract.WorkProfile.getContentUri(context),
13+
projection,
14+
f
15+
)
16+
17+
private fun setSettings(v: ContentValues.() -> Unit) =
18+
SettingsContract.setSettings(context, SettingsContract.WorkProfile.getContentUri(context), v)
19+
20+
var allowCreateWorkAccount: Boolean
21+
get() = getSettings(SettingsContract.WorkProfile.CREATE_WORK_ACCOUNT) { c -> c.getInt(0) != 0 }
22+
set(value) = setSettings { put(SettingsContract.WorkProfile.CREATE_WORK_ACCOUNT, value) }
23+
}

play-services-auth-workaccount/core/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77

88
<string name="auth_work_authenticator_label">Managed Google for Work account</string>
99
<string name="auth_work_authenticator_add_manual_error">This type of account is created automatically by your profile administrator if needed.</string>
10+
<string name="auth_work_authenticator_disabled_error">Creating a work account is disabled in microG settings.</string>
1011

1112
</resources>

play-services-base/core/src/main/kotlin/org/microg/gms/settings/SettingsContract.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@ object SettingsContract {
277277
)
278278
}
279279

280+
object WorkProfile {
281+
const val ID = "emm" // Enterprise Mobility Management
282+
fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), ID)
283+
fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$ID"
284+
285+
const val CREATE_WORK_ACCOUNT = "emm_allow_provision"
286+
287+
val PROJECTION = arrayOf(
288+
CREATE_WORK_ACCOUNT
289+
)
290+
}
291+
280292
private fun <T> withoutCallingIdentity(f: () -> T): T {
281293
val identity = Binder.clearCallingIdentity()
282294
try {

play-services-base/core/src/main/kotlin/org/microg/gms/settings/SettingsProvider.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.microg.gms.settings.SettingsContract.Location
2626
import org.microg.gms.settings.SettingsContract.Profile
2727
import org.microg.gms.settings.SettingsContract.SafetyNet
2828
import org.microg.gms.settings.SettingsContract.Vending
29+
import org.microg.gms.settings.SettingsContract.WorkProfile
2930
import org.microg.gms.settings.SettingsContract.getAuthority
3031
import java.io.File
3132

@@ -82,6 +83,7 @@ class SettingsProvider : ContentProvider() {
8283
Profile.ID -> queryProfile(projection ?: Profile.PROJECTION)
8384
Location.ID -> queryLocation(projection ?: Location.PROJECTION)
8485
Vending.ID -> queryVending(projection ?: Vending.PROJECTION)
86+
WorkProfile.ID -> queryWorkProfile(projection ?: WorkProfile.PROJECTION)
8587
else -> null
8688
}
8789

@@ -103,6 +105,7 @@ class SettingsProvider : ContentProvider() {
103105
Profile.ID -> updateProfile(values)
104106
Location.ID -> updateLocation(values)
105107
Vending.ID -> updateVending(values)
108+
WorkProfile.ID -> updateWorkProfile(values)
106109
else -> return 0
107110
}
108111
return 1
@@ -377,6 +380,25 @@ class SettingsProvider : ContentProvider() {
377380
editor.apply()
378381
}
379382

383+
private fun queryWorkProfile(p: Array<out String>): Cursor = MatrixCursor(p).addRow(p) { key ->
384+
when (key) {
385+
WorkProfile.CREATE_WORK_ACCOUNT -> getSettingsBoolean(key, false)
386+
else -> throw IllegalArgumentException("Unknown key: $key")
387+
}
388+
}
389+
390+
private fun updateWorkProfile(values: ContentValues) {
391+
if (values.size() == 0) return
392+
val editor = preferences.edit()
393+
values.valueSet().forEach { (key, value) ->
394+
when (key) {
395+
WorkProfile.CREATE_WORK_ACCOUNT -> editor.putBoolean(key, value as Boolean)
396+
else -> throw IllegalArgumentException("Unknown key: $key")
397+
}
398+
}
399+
editor.apply()
400+
}
401+
380402
private fun MatrixCursor.addRow(
381403
p: Array<out String>,
382404
valueGetter: (String) -> Any?

0 commit comments

Comments
 (0)