Skip to content

Commit

Permalink
feat: apply hidePrivate and allowRoot settings
Browse files Browse the repository at this point in the history
  • Loading branch information
butzist committed Feb 17, 2024
1 parent 2320800 commit 65edee6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.drawable.Drawable
import dagger.hilt.android.qualifiers.ActivityContext
import de.szalkowski.activitylauncher.services.internal.componentName
import de.szalkowski.activitylauncher.services.internal.isPrivate
import javax.inject.Inject

interface ActivityListService {
Expand Down Expand Up @@ -72,11 +74,11 @@ class ActivityListServiceImpl @Inject constructor(
activityInfo: ActivityInfo,
nameInfo: ActivityName,
): MyActivityInfo {
val componentName = getComponentName(activityInfo)
val componentName = activityInfo.componentName
val name = nameInfo.name
val icon = getIcon(activityInfo)
val iconResourceName = getIconResourceName(activityInfo)
val isPrivate = isPrivate(activityInfo)
val isPrivate = activityInfo.isPrivate(packageManager)

return MyActivityInfo(
componentName,
Expand All @@ -87,8 +89,7 @@ class ActivityListServiceImpl @Inject constructor(
)
}

private fun getComponentName(activityInfo: ActivityInfo) =
ComponentName(activityInfo.packageName, activityInfo.name)


private fun getIconResourceName(
activityInfo: ActivityInfo
Expand All @@ -98,7 +99,7 @@ class ActivityListServiceImpl @Inject constructor(
}

return try {
packageManager.getResourcesForActivity(getComponentName(activityInfo))
packageManager.getResourcesForActivity(activityInfo.componentName)
.getResourceName(activityInfo.iconResource)
} catch (ignored: Exception) {
null
Expand All @@ -111,17 +112,6 @@ class ActivityListServiceImpl @Inject constructor(
packageManager.defaultActivityIcon
}

private fun isPrivate(activityInfo: ActivityInfo): Boolean {
if (!activityInfo.exported) return true

val enabledState = packageManager.getComponentEnabledSetting(getComponentName(activityInfo))
return when (enabledState) {
PackageManager.COMPONENT_ENABLED_STATE_DISABLED -> true
PackageManager.COMPONENT_ENABLED_STATE_ENABLED -> false
else -> !activityInfo.isEnabled
}
}

private fun createNameFromClass(cls: String): String {
val name = cls.substringAfterLast('.')
return name.replaceFirstChar { if (it.isLowerCase()) it.titlecase(config.locale) else it.toString() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.util.DisplayMetrics
import dagger.hilt.android.qualifiers.ApplicationContext
import de.szalkowski.activitylauncher.services.internal.isPrivate
import javax.inject.Inject

interface PackageListService {
val packages: List<MyPackageInfo>
}

class PackageListServiceImpl @Inject constructor(
@ApplicationContext context: Context, settingsService: SettingsService
@ApplicationContext context: Context, val settingsService: SettingsService
) : PackageListService {

private val config: Configuration = settingsService.getLocaleConfiguration()
Expand All @@ -43,7 +44,9 @@ class PackageListServiceImpl @Inject constructor(
val icon = getIcon(app)
val iconResourceName = getIconResourceName(app, appRes)
val defaultActivityName = getDefaultActivityName(packageName, appRes)
val activities = info.activities.orEmpty().map { getActivityName(it, appRes) }
val activities = info.activities.orEmpty()
.filter{ !settingsService.hidePrivate || !it.isPrivate(packageManager) }
.map { getActivityName(it, appRes) }
.filter { it != defaultActivityName }

return MyPackageInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface SettingsService {
fun setTheme(theme: String?)

var disclaimerAccepted: Boolean
val hideHidePrivate: Boolean
val hidePrivate: Boolean
val language: String
val allowRoot: Boolean
val theme: String
Expand Down Expand Up @@ -74,7 +74,7 @@ class SettingsServiceImpl @Inject constructor(@ApplicationContext val context: C
override val language: String
get() = prefs.getString(PREF_LANGUAGE, LANGUAGE_DEFAULT)!!

override val hideHidePrivate: Boolean
override val hidePrivate: Boolean
get() = prefs.getBoolean(PREF_HIDE_HIDE_PRIVATE, false)

override fun applyLocaleConfiguration(context: Context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.szalkowski.activitylauncher.services.internal

import android.content.ComponentName
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager

fun ActivityInfo.isPrivate(packageManager: PackageManager): Boolean {
if (!this.exported) return true

val enabledState = packageManager.getComponentEnabledSetting(this.componentName)
return when (enabledState) {
PackageManager.COMPONENT_ENABLED_STATE_DISABLED -> true
PackageManager.COMPONENT_ENABLED_STATE_ENABLED -> false
else -> !this.isEnabled
}
}

val ActivityInfo.componentName
get() = ComponentName(this.packageName, this.name)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import de.szalkowski.activitylauncher.services.ActivityListService
import de.szalkowski.activitylauncher.services.IconCreatorService
import de.szalkowski.activitylauncher.services.IconLoaderService
import de.szalkowski.activitylauncher.services.MyActivityInfo
import de.szalkowski.activitylauncher.services.SettingsService
import javax.inject.Inject

@AndroidEntryPoint
Expand All @@ -34,6 +35,9 @@ class ActivityDetailsFragment : Fragment() {
@Inject
internal lateinit var iconLoaderService: IconLoaderService

@Inject
internal lateinit var settingsService: SettingsService

private var _binding: FragmentActivityDetailsBinding? = null

// This property is only valid between onCreateView and
Expand Down Expand Up @@ -99,6 +103,14 @@ class ActivityDetailsFragment : Fragment() {
editedActivityInfo.componentName, asRoot = true, showToast = true
)
}

if (!settingsService.allowRoot) {
binding.btCreateShortcutAsRoot.visibility = View.GONE
binding.btLaunchAsRoot.visibility = View.GONE
} else {
binding.btCreateShortcutAsRoot.visibility = View.VISIBLE
binding.btLaunchAsRoot.visibility = View.VISIBLE
}
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.szalkowski.activitylauncher.ui

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -54,6 +55,7 @@ class PackageListAdapter @Inject constructor(packageListService: PackageListServ
return filteredPackages.size
}

@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val view = holder.itemView
val tvName = view.findViewById<TextView>(R.id.tvName)
Expand Down

0 comments on commit 65edee6

Please sign in to comment.