Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING/UNDO BREAKING Add support for excluding unlaunchable apps - as per pre-1.5.1 #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ possible.
``` dart
List<AppInfo> apps = await InstalledApps.getInstalledApps(
bool excludeSystemApps,
bool excludeUnlaunchable,
bool withIcon,
String packageNamePrefix
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ class InstalledAppsPlugin() : MethodCallHandler, FlutterPlugin, ActivityAware {
}
when (call.method) {
"getInstalledApps" -> {
val includeSystemApps = call.argument("exclude_system_apps") ?: true
val excludeSystemApps = call.argument("exclude_system_apps") ?: true
val excludeLaunchableApps = call.argument("exclude_unlaunchable") ?: true
val withIcon = call.argument("with_icon") ?: false
val packageNamePrefix: String = call.argument("package_name_prefix") ?: ""
Thread {
val apps: List<Map<String, Any?>> =
getInstalledApps(includeSystemApps, withIcon, packageNamePrefix)
getInstalledApps(excludeSystemApps, excludeLaunchableApps, withIcon, packageNamePrefix)
result.success(apps)
}.start()
}
Expand Down Expand Up @@ -120,6 +121,7 @@ class InstalledAppsPlugin() : MethodCallHandler, FlutterPlugin, ActivityAware {

private fun getInstalledApps(
excludeSystemApps: Boolean,
excludeUnlaunchable: Boolean,
withIcon: Boolean,
packageNamePrefix: String
): List<Map<String, Any?>> {
Expand All @@ -128,6 +130,8 @@ class InstalledAppsPlugin() : MethodCallHandler, FlutterPlugin, ActivityAware {
if (excludeSystemApps)
installedApps =
installedApps.filter { app -> !isSystemApp(packageManager, app.packageName) }
if (excludeUnlaunchable)
installedApps = installedApps.filter { app -> isLaunchable(packageManager, app.packageName) }
if (packageNamePrefix.isNotEmpty())
installedApps = installedApps.filter { app ->
app.packageName.startsWith(
Expand Down Expand Up @@ -163,6 +167,10 @@ class InstalledAppsPlugin() : MethodCallHandler, FlutterPlugin, ActivityAware {
}
}

private fun isLaunchable(packageManager: PackageManager, packageName: String): Boolean {
return packageManager.getLaunchIntentForPackage(packageName) != null
}

private fun openSettings(packageName: String?) {
if (!isAppInstalled(packageName)) {
print("App $packageName is not installed on this device.")
Expand Down
2 changes: 1 addition & 1 deletion example/lib/screens/app_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AppListScreen extends StatelessWidget {

Widget _buildBody() {
return FutureBuilder<List<AppInfo>>(
future: InstalledApps.getInstalledApps(true, true),
future: InstalledApps.getInstalledApps(false, true, true),
builder: (
BuildContext buildContext,
AsyncSnapshot<List<AppInfo>> snapshot,
Expand Down
2 changes: 1 addition & 1 deletion example/lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HomeScreen extends StatelessWidget {
_buildListItem(
context,
"Installed Apps",
"Get installed apps on device. With options to exclude system app, get app icon & matching package name prefix.",
"Get installed apps on device. With options to exclude system app, exclude unlaunchable apps, get app icon & matching package name prefix.",
() => Navigator.push(
context,
MaterialPageRoute(builder: (context) => AppListScreen()),
Expand Down
3 changes: 3 additions & 0 deletions lib/installed_apps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ class InstalledApps {
/// Retrieves a list of installed apps on the device.
///
/// [excludeSystemApps] specifies whether to exclude system apps from the list.
/// [excludeUnlaunchable] specifies whether to exclude apps that cannot be launched
/// [withIcon] specifies whether to include app icons in the list.
/// [packageNamePrefix] is an optional parameter to filter apps with package names starting with a specific prefix.
///
/// Returns a list of [AppInfo] objects representing the installed apps.
static Future<List<AppInfo>> getInstalledApps([
bool excludeSystemApps = true,
bool excludeUnlaunchable = true,
bool withIcon = false,
String packageNamePrefix = "",
]) async {
dynamic apps = await _channel.invokeMethod(
"getInstalledApps",
{
"exclude_system_apps": excludeSystemApps,
"exclude_unlaunchable": excludeUnlaunchable,
"with_icon": withIcon,
"package_name_prefix": packageNamePrefix,
},
Expand Down