Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add ApplicationUtils class.

Add methods to determine if an application is installed or running in
the foreground, and a method to inject Esc key presses (the only way
to programmatically 'exit' an application).
  • Loading branch information...
commit 74e04ec165aed621435157bd24d2c362a5dd9db0 1 parent ab036cc
jtyberg authored

Showing 1 changed file with 96 additions and 0 deletions. Show diff stats Hide diff stats

  1. +96 0 framework/ext/src/com/phonegap/util/ApplicationUtils.java
96 framework/ext/src/com/phonegap/util/ApplicationUtils.java
... ... @@ -0,0 +1,96 @@
  1 +/*
  2 + * PhoneGap is available under *either* the terms of the modified BSD license *or* the
  3 + * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
  4 + *
  5 + * Copyright (c) 2011, IBM Corporation
  6 + */
  7 +package com.phonegap.util;
  8 +
  9 +import com.phonegap.camera.Camera;
  10 +
  11 +import net.rim.device.api.system.ApplicationDescriptor;
  12 +import net.rim.device.api.system.ApplicationManager;
  13 +import net.rim.device.api.system.Characters;
  14 +import net.rim.device.api.system.CodeModuleManager;
  15 +import net.rim.device.api.system.ControlledAccessException;
  16 +import net.rim.device.api.system.EventInjector;
  17 +import net.rim.device.api.ui.UiApplication;
  18 +
  19 +public class ApplicationUtils {
  20 + /**
  21 + * Determines if the specified application is running in the foreground.
  22 + *
  23 + * @param handle
  24 + * the name of the application handle (e.g., net_rim_bb_camera")
  25 + * @return <code>true</code> if the application is running and in the
  26 + * foreground
  27 + */
  28 + public static boolean isApplicationInForeground(String handle) {
  29 + // determine if the specified application is running in the foreground
  30 + ApplicationManager manager = ApplicationManager.getApplicationManager();
  31 + int foregroundProcessId = manager.getForegroundProcessId();
  32 + ApplicationDescriptor descriptors[] = manager.getVisibleApplications();
  33 + for (int i = 0; i < descriptors.length; i++) {
  34 + if (descriptors[i].getModuleName().equals(handle)
  35 + && manager.getProcessId(descriptors[i]) == foregroundProcessId) {
  36 + return true;
  37 + }
  38 + }
  39 + return false;
  40 + }
  41 +
  42 + /**
  43 + * Determines if the specified application is installed.
  44 + *
  45 + * @param handle
  46 + * the name of the application handle (e.g., net_rim_bb_camera")
  47 + * @return <code>true</code> if the application is installed on the device
  48 + */
  49 + public static boolean isModuleInstalled(String handle) {
  50 + return (CodeModuleManager.getModuleHandle(handle) != 0);
  51 + }
  52 +
  53 + /**
  54 + * Use this method when another native application has been launched by this
  55 + * application, and you would like the application to be closed.
  56 + * <p>
  57 + * Unfortunately, the only way to do this programmatically is to simulate
  58 + * the Escape (back) key being pressed. We do this by injecting key events,
  59 + * which means the application permissions must have the key injection
  60 + * permissions enabled for it to work.
  61 + * <p>
  62 + * An alternative to closing the applications would be to simply request
  63 + * that our application be brought to the foreground; however, this just
  64 + * pushes all the applications we've launched to the background, leaving a
  65 + * mess for the user to cleanup after this application has been closed.
  66 + *
  67 + * @param repeat
  68 + * the number of times to press the Esc key
  69 + */
  70 + public static void injectEscKeyPress(final int repeat) {
  71 + // simulate escape characters (back button press)
  72 + Runnable escKeyPresser = new Runnable() {
  73 + public void run() {
  74 + try {
  75 + EventInjector.KeyEvent inject = new EventInjector.KeyEvent(
  76 + EventInjector.KeyEvent.KEY_DOWN, Characters.ESCAPE,
  77 + 0);
  78 + int count = 0;
  79 + while (count < repeat) {
  80 + inject.post();
  81 + count++;
  82 + }
  83 + }
  84 + catch (ControlledAccessException e) {
  85 + // the application doesn't have key injection
  86 + // permissions
  87 + Logger.log(Camera.class.getName() + ": "
  88 + + ApplicationDescriptor
  89 + .currentApplicationDescriptor().getName()
  90 + + " does not have key injection permissions.");
  91 + }
  92 + }
  93 + };
  94 + UiApplication.getUiApplication().invokeLater(escKeyPresser);
  95 + }
  96 +}

0 comments on commit 74e04ec

Please sign in to comment.
Something went wrong with that request. Please try again.