Skip to content

Commit

Permalink
UPDATE: Minor tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
oasisfeng committed Oct 17, 2023
1 parent a694bd0 commit 294a3fb
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -70,7 +71,7 @@ class AppInfoForwarderActivity : CallerAwareActivity() {
if (SDK_INT >= O) chooser.putExtra(IntentCompat.EXTRA_AUTO_LAUNCH_SINGLE_CHOICE, isCallerNotSettings)
val initialIntents: MutableList<Intent> = 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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,7 +87,7 @@ class IslandAppClones(val activity: FragmentActivity, val vm: AndroidViewModel,
val shouldShowBadge: Boolean = targets.size > 2
val icons: Map<UserHandle, Drawable> = targets.entries.stream().collect(Collectors.toMap({ obj: Map.Entry<UserHandle, String> -> obj.key }) { e: Map.Entry<UserHandle, String> ->
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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,7 +38,7 @@ import kotlin.streams.asSequence
class IslandAppListProvider : AppListProvider<IslandAppInfo>() {

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) {
Expand All @@ -62,7 +63,7 @@ class IslandAppListProvider : AppListProvider<IslandAppInfo>() {
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) }
}
Expand All @@ -84,7 +85,7 @@ class IslandAppListProvider : AppListProvider<IslandAppInfo>() {
}

fun installedApps(profile: UserHandle): Stream<IslandAppInfo> {
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<String, IslandAppInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions shared/src/main/java/com/oasisfeng/island/util/Users.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 294a3fb

Please sign in to comment.