diff --git a/installer/src/main/java/com/oasisfeng/island/installer/AppInfoForwarderActivity.kt b/installer/src/main/java/com/oasisfeng/island/installer/AppInfoForwarderActivity.kt index d12e2c07..f85742f8 100644 --- a/installer/src/main/java/com/oasisfeng/island/installer/AppInfoForwarderActivity.kt +++ b/installer/src/main/java/com/oasisfeng/island/installer/AppInfoForwarderActivity.kt @@ -21,6 +21,7 @@ import com.oasisfeng.island.shuttle.ActivityShuttle import com.oasisfeng.island.shuttle.Shuttle import com.oasisfeng.island.util.CallerAwareActivity import com.oasisfeng.island.util.Users +import com.oasisfeng.island.util.Users.Companion.isParentProfile /** * Created by Oasis on 2018-11-16. @@ -70,7 +71,7 @@ class AppInfoForwarderActivity : CallerAwareActivity() { if (SDK_INT >= O) chooser.putExtra(IntentCompat.EXTRA_AUTO_LAUNCH_SINGLE_CHOICE, isCallerNotSettings) val initialIntents: MutableList = ArrayList() if (isCallerNotSettings && detailsActivity != null) { - if (user != null && ! Users.isParentProfile(user) && Users.isProfileManagedByIsland(this, user)) { // Use mainland resolve to replace the misleading forwarding-resolved "Switch to work profile". + if (user != null && ! user.isParentProfile() && Users.isProfileManagedByIsland(this, user)) { // Use mainland resolve to replace the misleading forwarding-resolved "Switch to work profile". val labelRes = detailsActivity.run { if (labelRes != 0) labelRes else applicationInfo.labelRes } initialIntents.add(LabeledIntent(details, detailsActivity.packageName, labelRes, detailsActivity.iconResource)) } else initialIntents.add(details) } diff --git a/mobile/src/main/java/com/oasisfeng/island/controller/IslandAppClones.kt b/mobile/src/main/java/com/oasisfeng/island/controller/IslandAppClones.kt index 5b404b4e..717e0460 100644 --- a/mobile/src/main/java/com/oasisfeng/island/controller/IslandAppClones.kt +++ b/mobile/src/main/java/com/oasisfeng/island/controller/IslandAppClones.kt @@ -54,6 +54,7 @@ import com.oasisfeng.island.model.interactive import com.oasisfeng.island.shuttle.Shuttle import com.oasisfeng.island.ui.ModelBottomSheetFragment import com.oasisfeng.island.util.* +import com.oasisfeng.island.util.Users.Companion.isParentProfile import com.oasisfeng.island.util.Users.Companion.toId import eu.chainfire.libsuperuser.Shell import kotlinx.coroutines.Dispatchers @@ -86,7 +87,7 @@ class IslandAppClones(val activity: FragmentActivity, val vm: AndroidViewModel, val shouldShowBadge: Boolean = targets.size > 2 val icons: Map = targets.entries.stream().collect(Collectors.toMap({ obj: Map.Entry -> obj.key }) { e: Map.Entry -> val user = e.key - val res = if (Users.isParentProfile(user)) R.drawable.ic_portrait_24dp else R.drawable.ic_island_black_24dp + val res = if (user.isParentProfile()) R.drawable.ic_portrait_24dp else R.drawable.ic_island_black_24dp val drawable: Drawable = context.getDrawable(res)!! val dark = (context.resources.configuration.uiMode and UI_MODE_NIGHT_MASK) == UI_MODE_NIGHT_YES drawable.setTint(context.getColor(if (dark) android.R.color.white else android.R.color.black)) // TODO: Decouple @@ -138,7 +139,7 @@ class IslandAppClones(val activity: FragmentActivity, val vm: AndroidViewModel, } private suspend fun cloneApp(source: IslandAppInfo, target: UserHandle, mode: @AppCloneMode Int) { - if (Users.isParentProfile(target) && isInstallerUsable()) @Suppress("DEPRECATION") // Only works in parent profile due to a bug in AOSP. + if (target.isParentProfile() && isInstallerUsable()) @Suppress("DEPRECATION") // Only works in parent profile due to a bug in AOSP. return activity.startActivityForResult(Intent(Intent.ACTION_INSTALL_PACKAGE, Uri.fromParts("package", pkg, null)), 1) // startActivityForResult() is required on Android U+ due to a bug in AOSP. val context = source.context(); val pkg = source.packageName diff --git a/mobile/src/main/java/com/oasisfeng/island/data/IslandAppListProvider.kt b/mobile/src/main/java/com/oasisfeng/island/data/IslandAppListProvider.kt index 718184e5..bb97ab2f 100644 --- a/mobile/src/main/java/com/oasisfeng/island/data/IslandAppListProvider.kt +++ b/mobile/src/main/java/com/oasisfeng/island/data/IslandAppListProvider.kt @@ -24,6 +24,7 @@ import com.oasisfeng.island.shuttle.Shuttle import com.oasisfeng.island.util.DPM import com.oasisfeng.island.util.DevicePolicies import com.oasisfeng.island.util.Users +import com.oasisfeng.island.util.Users.Companion.isParentProfile import com.oasisfeng.island.util.Users.Companion.toId import java.util.function.Predicate import java.util.stream.Stream @@ -37,7 +38,7 @@ import kotlin.streams.asSequence class IslandAppListProvider : AppListProvider() { operator fun get(pkg: String, profile: UserHandle): IslandAppInfo? { - return if (Users.isParentProfile(profile)) super.get(pkg) else loadAppsInProfileIfNotYet(profile)[pkg] + return if (profile.isParentProfile()) super.get(pkg) else loadAppsInProfileIfNotYet(profile)[pkg] } fun addPlaceholder(pkg: String, profile: UserHandle) { @@ -62,7 +63,7 @@ class IslandAppListProvider : AppListProvider() { fun isInstalled(pkg: String, profile: UserHandle) = get(pkg, profile)?.run { installed && shouldShowAsEnabled() } == true fun isExclusive(app: IslandAppInfo): Boolean { - if (Users.isParentProfile(app.user) && ! Users.hasProfile()) return true + if (app.user.isParentProfile() && ! Users.hasProfile()) return true return Users.getProfilesManagedByIsland().asSequence().plus(Users.parentProfile).minus(app.user).all { profile -> ! isInstalled(app.packageName, profile) } } @@ -84,7 +85,7 @@ class IslandAppListProvider : AppListProvider() { } fun installedApps(profile: UserHandle): Stream { - return if (Users.isParentProfile(profile)) installedAppsInOwnerUser() else loadAppsInProfileIfNotYet(profile).values.stream() + return if (profile.isParentProfile()) installedAppsInOwnerUser() else loadAppsInProfileIfNotYet(profile).values.stream() } private fun loadAppsInProfileIfNotYet(profile: UserHandle): Map diff --git a/mobile/src/main/java/com/oasisfeng/island/model/MainViewModel.kt b/mobile/src/main/java/com/oasisfeng/island/model/MainViewModel.kt index 040218e0..de6252d9 100644 --- a/mobile/src/main/java/com/oasisfeng/island/model/MainViewModel.kt +++ b/mobile/src/main/java/com/oasisfeng/island/model/MainViewModel.kt @@ -18,6 +18,7 @@ import com.oasisfeng.island.data.LiveProfileStates.ProfileState import com.oasisfeng.island.mobile.R import com.oasisfeng.island.util.Users import com.oasisfeng.island.util.Users.Companion.ACTION_USER_INFO_CHANGED +import com.oasisfeng.island.util.Users.Companion.isParentProfile import com.oasisfeng.island.util.Users.Companion.toId class MainViewModel(app: Application, state: SavedStateHandle): AppListViewModel(app, state) { @@ -33,10 +34,9 @@ class MainViewModel(app: Application, state: SavedStateHandle): AppListViewModel override fun onTabReselected(tab: TabLayout.Tab) {}}) // Tab "Discovery" and "Mainland" are always present - tabs.addTab(tabs.newTab().setText(R.string.tab_discovery),/* setSelected = */false) + tabs.addTab(tabs.newTab().setText(R.string.tab_discovery), /* setSelected */false) val currentProfile = currentProfile - tabs.addTab(tabs.newTab().setText(com.oasisfeng.island.shared.R.string.mainland_name), - /* setSelected */currentProfile?.let { Users.isParentProfile(it) } == true) + tabs.addTab(tabs.newTab().setText(com.oasisfeng.island.shared.R.string.mainland_name), /* setSelected */currentProfile.isParentProfile()) for ((profile, name) in IslandNameManager.getAllNames(activity)) { val tab = tabs.newTab().setTag(profile).setText(name) diff --git a/mobile/src/main/java/com/oasisfeng/island/settings/SettingsActivity.kt b/mobile/src/main/java/com/oasisfeng/island/settings/SettingsActivity.kt index f707b9a1..013c22ce 100644 --- a/mobile/src/main/java/com/oasisfeng/island/settings/SettingsActivity.kt +++ b/mobile/src/main/java/com/oasisfeng/island/settings/SettingsActivity.kt @@ -27,6 +27,7 @@ import com.oasisfeng.island.mobile.R import com.oasisfeng.island.util.DevicePolicies import com.oasisfeng.island.util.Modules import com.oasisfeng.island.util.Users +import com.oasisfeng.island.util.Users.Companion.isParentProfile @Suppress("DEPRECATION") class SettingsActivity : android.preference.PreferenceActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -67,7 +68,7 @@ import com.oasisfeng.island.util.Users val names = IslandNameManager.getAllNames(this) val labels = users.map { user -> - if (Users.isParentProfile(user)) getText(com.oasisfeng.island.shared.R.string.mainland_name) else names[user] + if (user.isParentProfile()) getText(com.oasisfeng.island.shared.R.string.mainland_name) else names[user] }.toTypedArray() Dialogs.buildList(this, null, labels) { _, which -> if (which == 0) super.onHeaderClick(header, position) diff --git a/shared/src/main/java/com/oasisfeng/island/IslandNameManager.kt b/shared/src/main/java/com/oasisfeng/island/IslandNameManager.kt index 9b7966ac..276110f6 100644 --- a/shared/src/main/java/com/oasisfeng/island/IslandNameManager.kt +++ b/shared/src/main/java/com/oasisfeng/island/IslandNameManager.kt @@ -14,6 +14,7 @@ import com.oasisfeng.island.util.ProfileUser import com.oasisfeng.island.util.Users import com.oasisfeng.island.util.Users.Companion.ACTION_USER_INFO_CHANGED import com.oasisfeng.island.util.Users.Companion.EXTRA_USER_HANDLE +import com.oasisfeng.island.util.Users.Companion.isParentProfile import com.oasisfeng.island.util.Users.Companion.toId object IslandNameManager { @@ -31,7 +32,7 @@ object IslandNameManager { getStore(context).getString(buildIslandNameKey(context), null) ?: getDefaultName(context) private fun getDefaultName(context: Context, profile: UserHandle = Users.current()): String { - if (Users.isParentProfile(profile)) return context.getString(R.string.mainland_name) + if (profile.isParentProfile()) return context.getString(R.string.mainland_name) val islandCount = Users.run { if (isParentProfile()) getProfilesManagedByIsland().size else getProfileCount() - 1 } return if (islandCount > 1) getDefaultSpecificName(context, profile) else context.getString(R.string.default_island_name) } diff --git a/shared/src/main/java/com/oasisfeng/island/util/Users.kt b/shared/src/main/java/com/oasisfeng/island/util/Users.kt index ec9d1f56..9ace8413 100644 --- a/shared/src/main/java/com/oasisfeng/island/util/Users.kt +++ b/shared/src/main/java/com/oasisfeng/island/util/Users.kt @@ -104,12 +104,12 @@ class Users : PseudoContentProvider() { @JvmStatic fun isSystemUser() = CURRENT_ID == 0 @JvmStatic fun isParentProfile() = CURRENT_ID == parentProfile.toId() - @JvmStatic fun isParentProfile(user: UserHandle) = user == parentProfile - @JvmStatic fun isParentProfile(user_id: Int) = user_id == parentProfile.toId() + @JvmStatic fun UserHandle?.isParentProfile() = this == parentProfile + @JvmStatic fun isParentProfile(userId: Int) = userId == parentProfile.toId() @OwnerUser @JvmStatic fun isProfileManagedByIsland(context: Context, user: UserHandle): Boolean { ensureParentProfile() - if (isParentProfile(user)) { + if (user.isParentProfile()) { if (isParentProfile()) return DevicePolicies(context).isProfileOwner throw IllegalArgumentException("Not working for profile parent user") } return sProfilesManagedByIsland.contains(user)