Skip to content

Commit

Permalink
FIX: Minor crashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
oasisfeng committed Oct 17, 2020
1 parent 0002ce8 commit 65095bd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,11 @@ private void fallbackToSystemPackageInstaller(final String reason, final @Nullab
private static boolean ensureSystemPackageEnabledAndUnfrozen(final Context context, final Intent intent) {
final ResolveInfo resolve = context.getPackageManager().resolveActivity(intent,
PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.MATCH_SYSTEM_ONLY);
return resolve != null && Apps.isInstalledInCurrentUser(resolve.activityInfo.applicationInfo)
|| new DevicePolicies(context).enableSystemAppByIntent(intent)
|| resolve != null && IslandManager.ensureAppFreeToLaunch(context, resolve.activityInfo.packageName).isEmpty();
if (resolve == null) return false;
if (Apps.isInstalledInCurrentUser(resolve.activityInfo.applicationInfo)) return true;
final DevicePolicies policies = new DevicePolicies(context);
return policies.isProfileOwner() && (policies.enableSystemAppByIntent(intent)
|| IslandManager.ensureAppFreeToLaunch(context, resolve.activityInfo.packageName).isEmpty());
}

@RequiresApi(O) private boolean isCallerQualified(final ApplicationInfo caller_app_info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.util.Log
import android.widget.Toast
import com.oasisfeng.android.os.UserHandles
import com.oasisfeng.android.util.Apps
import com.oasisfeng.island.analytics.analytics
import com.oasisfeng.island.shuttle.Shuttle
import com.oasisfeng.island.util.Users
import kotlinx.coroutines.GlobalScope
Expand Down Expand Up @@ -57,7 +58,9 @@ class AppSettingsHelperService: Service() {

val uptimeMillis = SystemClock.uptimeMillis()
if (intent.action == Intent.ACTION_PACKAGE_RESTARTED) { // If triggered by system Settings, it will be followed by ACTION_QUERY_PACKAGE_RESTART immediately.
return Unit.also { Shuttle(context, to = Users.owner).launch(at = GlobalScope) { onPackageRestarted(pkg, uid, uptimeMillis) }}}
return Unit.also { Shuttle(context, to = Users.owner).launch(at = GlobalScope) {
try { onPackageRestarted(pkg, uid, uptimeMillis) }
catch (e: RuntimeException) { analytics().logAndReport(TAG, "Error transferring ACTION_PACKAGE_RESTARTED to parent user", e) }}}}

if (intent.action == ACTION_QUERY_PACKAGE_RESTART) {
if (sLastPackageRestart?.run { first + MAX_DELAY > uptimeMillis && second == pkg && third == uid } == true) {
Expand Down
10 changes: 7 additions & 3 deletions mobile/src/main/java/com/oasisfeng/island/model/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.oasisfeng.island.model

import android.app.Application
import android.content.Context
import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.Q
import android.os.UserHandle
import android.util.Log
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.SavedStateHandle
import com.google.android.material.tabs.TabLayout
import com.oasisfeng.island.analytics.analytics
import com.oasisfeng.island.data.LiveProfileStates
import com.oasisfeng.island.data.LiveProfileStates.ProfileState
import com.oasisfeng.island.mobile.R
Expand All @@ -33,7 +35,7 @@ class MainViewModel(app: Application, state: SavedStateHandle): AppListViewModel

for ((profile, name) in IslandNameManager.getAllNames(activity)) {
val tab = tabs.newTab().setTag(profile).setText(name)
mProfileStates.get(profile).observe(activity, Observer { updateTabIconForProfileState(activity, tab, profile, it) })
mProfileStates.get(profile).observe(activity, { updateTabIconForProfileState(activity, tab, profile, it) })
tabs.addTab(tab,/* selected = */profile == currentProfile) }
}
}
Expand All @@ -42,7 +44,9 @@ private fun updateTabIconForProfileState(context: Context, tab: TabLayout.Tab, p
Log.d(TAG, "Update tab icon for profile ${profile.toId()}: $state")
val icon = context.getDrawable(R.drawable.ic_island_black_24dp)!!
icon.setTint(context.getColor(if (state == ProfileState.UNAVAILABLE) R.color.state_frozen else R.color.state_alive))
tab.icon = context.packageManager.getUserBadgedIcon(icon, profile)
try { tab.icon = context.packageManager.getUserBadgedIcon(icon, profile) }
// (Mostly "vivo" devices before Android Q) "SecurityException: You need MANAGE_USERS permission to: check if specified user a managed profile outside your profile group"
catch (e: SecurityException) { if (SDK_INT >= Q) analytics().logAndReport(TAG, "Error getting user badged icon", e) }
}

private const val TAG = "Island.MVM"
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class GeneralPreferenceFragment: SettingsActivity.SubPreferenceFragment(R.xml.pr
isChecked = setting.enabled
onChange { enabled -> onChange?.invoke(this) != false && setting.set(enabled) }}

private fun TwoStatePreference.refreshActivationStateForPreserveAppOps()
= Permissions.has(activity, GET_APP_OPS_STATS).also { if (it) lock(true) }
private fun TwoStatePreference.refreshActivationStateForPreserveAppOps(): Boolean {
return Permissions.has(activity ?: return false, GET_APP_OPS_STATS).also { if (it) lock(true) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class PendingIntentShuttle: BroadcastReceiver() {
class ProfileUnlockCanceledException(message: String) : CancellationException(message)

override fun onReceive(context: Context, intent: Intent) {
Log.d(TAG, "PendingIntentShuttle.onReceive($intent)")
if (intent.action.let { it == ACTION_LOCKED_BOOT_COMPLETED || it == ACTION_MY_PACKAGE_REPLACED }) {
Log.d(TAG, "Initiated by $intent")
if (Users.isOwner()) sendToAllUnlockedProfiles(context)
Expand Down Expand Up @@ -121,7 +122,8 @@ class PendingIntentShuttle: BroadcastReceiver() {
catch (e: CanceledException) {
Log.w(TAG, "Old shuttle (${Users.current().toId()} to ${profile.toId()}) is broken, rebuild now.") }

if (context.getSystemService(UserManager::class.java)!!.isUserUnlocked(profile))
if (try { context.getSystemService(UserManager::class.java)!!.isUserUnlocked(profile) }
catch (e: SecurityException) { return@launch Unit.also { Log.w(TAG, "Error checking user unlocked state.", e) } })
sendToProfileByActivity(context, profile)
else Log.i(TAG, "Skip stopped or locked profile: ${profile.toId()}")
}
Expand Down

0 comments on commit 65095bd

Please sign in to comment.