/config/src/com/google/marvin/config/Utils.java

http://eyes-free.googlecode.com/ · Java · 47 lines · 35 code · 5 blank · 7 comment · 0 complexity · 70517ed8d95f62fd00d17dad5f94ce8e MD5 · raw file

  1. package com.google.marvin.config;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.pm.PackageManager.NameNotFoundException;
  5. import android.net.Uri;
  6. /**
  7. * A collection of commonly used utility functions.
  8. *
  9. * @author clchen@google.com (Charles L. Chen)
  10. */
  11. public class Utils {
  12. public static boolean applicationInstalled(Context ctx, String packageName) {
  13. try {
  14. Context myContext = ctx.createPackageContext(packageName, 0);
  15. } catch (NameNotFoundException e) {
  16. return false;
  17. }
  18. return true;
  19. }
  20. public static Intent getMarketIntent(String packageName) {
  21. Uri marketUri = Uri.parse("market://search?q=pname:" + packageName);
  22. Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
  23. return marketIntent;
  24. }
  25. public static Intent getAppStartIntent(Context ctx, String packageName, String className) {
  26. Intent intent = null;
  27. try {
  28. int flags = Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY;
  29. Context myContext = ctx.createPackageContext(packageName, flags);
  30. Class<?> appClass = myContext.getClassLoader().loadClass(className);
  31. intent = new Intent(myContext, appClass);
  32. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  33. } catch (NameNotFoundException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. } catch (ClassNotFoundException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40. return intent;
  41. }
  42. }