aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/utils/ProcessUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/utils/ProcessUtils.java')
-rw-r--r--main/src/cgeo/geocaching/utils/ProcessUtils.java52
1 files changed, 50 insertions, 2 deletions
diff --git a/main/src/cgeo/geocaching/utils/ProcessUtils.java b/main/src/cgeo/geocaching/utils/ProcessUtils.java
index b566b79..3345ff1 100644
--- a/main/src/cgeo/geocaching/utils/ProcessUtils.java
+++ b/main/src/cgeo/geocaching/utils/ProcessUtils.java
@@ -2,8 +2,14 @@ package cgeo.geocaching.utils;
import cgeo.geocaching.cgeoapplication;
+import org.apache.commons.collections.CollectionUtils;
+
import android.content.Intent;
+import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+
+import java.util.List;
public final class ProcessUtils {
@@ -11,10 +17,44 @@ public final class ProcessUtils {
// utility class
}
- public static boolean isInstalled(final String packageName) {
+ /**
+ * Preferred method to detect the availability of an external app
+ *
+ * @param packageName
+ * @return
+ */
+ public static boolean isLaunchable(final String packageName) {
return getLaunchIntent(packageName) != null;
}
+ /**
+ * Checks whether a launch intent is available or if the package is just installed
+ * This function is relatively costly, so if you know that the package in question has
+ * a launch intent, use isLaunchable() instead.
+ *
+ * @param packageName
+ * @return
+ */
+ public static boolean isInstalled(final String packageName) {
+ return isLaunchable(packageName) || hasPackageInstalled(packageName);
+ }
+
+ /**
+ * This will find installed applications even without launch intent (e.g. the streetview plugin).
+ */
+ private static boolean hasPackageInstalled(final String packageName) {
+ final List<PackageInfo> packs = cgeoapplication.getInstance().getPackageManager().getInstalledPackages(0);
+ for (final PackageInfo packageInfo : packs) {
+ if (packageName.equals(packageInfo.packageName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * This will find applications, which can be launched.
+ */
public static Intent getLaunchIntent(final String packageName) {
if (packageName == null) {
return null;
@@ -24,8 +64,16 @@ public final class ProcessUtils {
// This can throw an exception where the exception type is only defined on API Level > 3
// therefore surround with try-catch
return packageManager.getLaunchIntentForPackage(packageName);
- } catch (Exception e) {
+ } catch (final Exception e) {
return null;
}
}
+
+ public static boolean isIntentAvailable(final String intent) {
+ final PackageManager packageManager = cgeoapplication.getInstance().getPackageManager();
+ final List<ResolveInfo> list = packageManager.queryIntentActivities(
+ new Intent(intent), PackageManager.MATCH_DEFAULT_ONLY);
+
+ return CollectionUtils.isNotEmpty(list);
+ }
}