示例#1
0
  @Test
  public void shouldSupportCategories() throws Exception {
    Intent intent = new Intent();
    Intent self = intent.addCategory("category.name.1");
    intent.addCategory("category.name.2");

    assertTrue(intent.hasCategory("category.name.1"));
    assertTrue(intent.hasCategory("category.name.2"));

    Set<String> categories = intent.getCategories();
    assertTrue(categories.contains("category.name.1"));
    assertTrue(categories.contains("category.name.2"));

    intent.removeCategory("category.name.1");
    assertFalse(intent.hasCategory("category.name.1"));
    assertTrue(intent.hasCategory("category.name.2"));

    intent.removeCategory("category.name.2");
    assertFalse(intent.hasCategory("category.name.2"));

    assertEquals(0, intent.getCategories().size());

    assertSame(self, intent);
  }
示例#2
0
  @Test
  public void equals_shouldTestActionComponentNameDataAndExtras() throws Exception {
    Intent intentA =
        new Intent()
            .setAction("action")
            .setData(Uri.parse("content:1"))
            .setComponent(new ComponentName("pkg", "cls"))
            .putExtra("extra", "blah")
            .setType("image/*")
            .addCategory("category.name");

    Intent intentB =
        new Intent()
            .setAction("action")
            .setData(Uri.parse("content:1"))
            .setComponent(new ComponentName("pkg", "cls"))
            .putExtra("extra", "blah")
            .setType("image/*")
            .addCategory("category.name");

    assertThat(intentA, equalTo(intentB));

    intentB.setAction("other action");
    assertThat(intentA, not(equalTo(intentB)));

    intentB.setAction("action");
    intentB.setData(Uri.parse("content:other"));
    assertThat(intentA, not(equalTo(intentB)));

    intentB.setData(Uri.parse("content:1"));
    intentB.setComponent(new ComponentName("other-pkg", "other-cls"));
    assertThat(intentA, not(equalTo(intentB)));

    intentB.setComponent(new ComponentName("pkg", "cls"));
    intentB.putExtra("extra", "foo");
    assertThat(intentA, not(equalTo(intentB)));

    intentB.putExtra("extra", "blah");
    intentB.setType("other/*");
    assertThat(intentA, not(equalTo(intentB)));

    intentB.setType("image/*");
    assertThat(intentA, equalTo(intentB));

    intentB.removeCategory("category.name");
    assertThat(intentA, not(equalTo(intentB)));
  }
示例#3
0
  @Override
  public Intent getLaunchIntentForPackage(String packageName) {
    Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
    intentToResolve.addCategory(Intent.CATEGORY_INFO);
    intentToResolve.setPackage(packageName);
    List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);

    if (ris == null || ris.isEmpty()) {
      intentToResolve.removeCategory(Intent.CATEGORY_INFO);
      intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
      intentToResolve.setPackage(packageName);
      ris = queryIntentActivities(intentToResolve, 0);
    }
    if (ris == null || ris.isEmpty()) {
      return null;
    }
    Intent intent = new Intent(intentToResolve);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(ris.get(0).activityInfo.packageName, ris.get(0).activityInfo.name);
    return intent;
  }
示例#4
0
  private Intent makeIntent(Map<String, Object> params) {
    Intent intent = new Intent();

    Object actionObj = params.get(HK_ACTION);
    Object categoriesObj = params.get(HK_CATEGORIES);
    Object appNameObj = params.get(HK_APP_NAME);
    Object targetClassObj = params.get(HK_TARGET_CLASS);
    Object uriObj = params.get(HK_URI);
    Object mimeObj = params.get(HK_MIME_TYPE);
    Object extrasObj = params.get(HK_DATA);

    String action = null;
    List<String> categories = null;
    String appName = null;
    String targetClass = null;
    String uri = null;
    String mime = null;
    Map<String, Object> extras = null;

    // --- Check param types ---

    if (actionObj != null) {
      if (!String.class.isInstance(actionObj)) {
        throw new RuntimeException("Wrong intent action: " + actionObj.toString());
      }
      action = constant((String) actionObj);
    }

    if (categoriesObj != null) {
      if (!List.class.isInstance(categoriesObj)) {
        throw new RuntimeException("Wrong intent categories: " + categoriesObj.toString());
      }
      categories = new ArrayList<String>();
      for (String cat : (List<String>) categoriesObj) {
        categories.add(constant(cat));
      }
    }

    if (appNameObj != null) {
      if (!String.class.isInstance(appNameObj)) {
        throw new RuntimeException("Wrong intent appName: " + appNameObj.toString());
      }
      appName = (String) appNameObj;
    }

    if (targetClassObj != null) {
      if (!String.class.isInstance(targetClassObj)) {
        throw new RuntimeException("Wrong intent targetClass: " + targetClassObj.toString());
      }
      targetClass = (String) targetClassObj;
    }

    if (uriObj != null) {
      if (!String.class.isInstance(uriObj)) {
        throw new RuntimeException("Wrong intent uri: " + uriObj.toString());
      }
      uri = (String) uriObj;
    }

    if (mimeObj != null) {
      if (!String.class.isInstance(mimeObj)) {
        throw new RuntimeException("Wrong intent mimeType: " + mimeObj.toString());
      }
      mime = (String) mimeObj;
    }

    if (extrasObj != null) {
      if (!Map.class.isInstance(extrasObj)) {
        throw new RuntimeException("Wrong intent data: " + extrasObj.toString());
      }
      extras = (Map<String, Object>) extrasObj;
    }

    // --- Fill intent fields ---

    if (action != null) {
      intent.setAction(action);
    }

    if (categories != null) {
      for (String category : categories) {
        intent.addCategory(category);
        if (intent.toString().contains(" xxx ") && isTablet(ContextFactory.getContext())) {
          intent.removeCategory(category);
        }
      }
    }

    if (targetClass != null) {
      if (appName == null) {
        throw new RuntimeException("Wrong intent appName: cannot be nil if targetClass is set");
      }
      intent.setClassName(appName, targetClass);
    } else if (appName != null) {
      intent.setPackage(appName);
    }

    if (uri != null) {
      Uri data = Uri.parse(uri);
      if (mime == null) {
        intent.setData(data);
      } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
          intent.setDataAndTypeAndNormalize(data, mime);
        } else {
          intent.setDataAndType(data, mime);
        }
      }
      intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_FORWARD_RESULT);
    } else if (mime != null) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        intent.setTypeAndNormalize(mime);
      } else {
        intent.setType(mime);
      }
    }

    if (extras != null) {
      for (Map.Entry<String, Object> entry : extras.entrySet()) {

        if (String.class.isInstance(entry.getValue())) {
          intent.putExtra(constant(entry.getKey()), (String) entry.getValue());
        } else if (Boolean.class.isInstance(entry.getValue())) {
          intent.putExtra(constant(entry.getKey()), ((Boolean) entry.getValue()).booleanValue());
        } else if (Integer.class.isInstance(entry.getValue())) {
          intent.putExtra(constant(entry.getKey()), ((Integer) entry.getValue()).intValue());
        } else if (Double.class.isInstance(entry.getValue())) {
          intent.putExtra(constant(entry.getKey()), ((Double) entry.getValue()).doubleValue());
        } else if (ArrayList.class.isInstance(entry.getValue())) {
          ArrayList list = (ArrayList) entry.getValue();
          if (list.size() > 0) {
            if (String.class.isInstance(list.get(0))) {
              intent.putExtra(constant(entry.getKey()), list.toArray(new String[list.size()]));
            } else if (Integer.class.isInstance(list.get(0))) {
              intent.putExtra(constant(entry.getKey()), list.toArray(new Integer[list.size()]));
            } else {
              throw new RuntimeException(
                  "Wrong intent data: array of "
                      + list.get(0).getClass().getName()
                      + " is not supported as value");
            }
          } else {
            intent.putExtra(constant(entry.getKey()), new String[0]);
          }
        } else {
          throw new RuntimeException(
              "Wrong intent data: "
                  + entry.getValue().getClass().getName()
                  + " is not supported as value");
        }
      }
    }

    return intent;
  }