⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 securitytoken.java

📁 用于移动设备上的java虚拟机源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        PermissionDialog dialog =            new PermissionDialog(token, title, question, app, resource,                extraValue, maximumLevel, defaultLevel, 0, BLANKET_ANSWER,                SESSION_ANSWER, CANCEL_ANSWER, DENY_ANSWER);                return dialog.waitForAnswer();    }    /**     * Ask the user permission and wait for the answer.     *     * <p>     * The title, question, and answer strings will be translated,     * if a string resource is available.     * Since the strings can have sustitution token in them, if there is a     * "%" it must changed to "%%". If a string has a %1, the app parameter     * will be substituted for it. If a string has a "%2, the resource     * parameter will be substituted for it. If a string has a %3, the     * extraValue parameter will be substituted for it.     *     * @param token security token with the permission to peempt the     *        foreground display     * @param title title of the dialog     * @param question question to ask user     * @param app name of the application to insert into a string     *        can be null if no %1 a string     * @param resource string to insert into a string,     *        can be null if no %2 in a string     * @param extraValue string to insert into a string,     *        can be null if no %3 in a string     * @param maximumLevel maximum permission level to display     * @param defaultLevel default permission level, DENY_SESSION,     *         or CANCELLED for "Not Now". If the level is greater than the     *        maximum level, maximum level will be used.     * @param skip permission to skip, SESSION, DENY,     *        or any other for skip none, multiple permissions can be added     *        together     * @param blanketAnswer text for the blanket answer     * @param sessionAnswer text for the session answer or one shot if     *        the maximum permission level is one shot     * @param cancelAnswer text for the cancel answer     * @param denyAnswer text for the deny answer     *     * @return new permission level or -1 if user cancelled     *     * @exception InterruptedException if another thread interrupts the     *   calling thread while this method is waiting to preempt the     *   display.     */    public static int askUserForPermission(SecurityToken token, String title,            String question, String app, String resource, String extraValue,            int maximumLevel, int defaultLevel, int skip,            String blanketAnswer, String sessionAnswer, String cancelAnswer,            String denyAnswer) throws InterruptedException {        PermissionDialog dialog =            new PermissionDialog(token, title, question, app, resource,                extraValue, maximumLevel, defaultLevel, skip, blanketAnswer,                sessionAnswer, cancelAnswer, denyAnswer);                return dialog.waitForAnswer();    }}/** Implements security permission dialog. */class PermissionDialog implements CommandListener, MIDletEventListener {    /** Answer that indicates that the dialog was cancelled. */    static final int CANCELLED = -1;    /** Caches the display manager reference. */    private DisplayManager displayManager =        DisplayManagerFactory.getDisplayManager();    /** This item will have the user's choice. */    private RadioButtonSet choice;    /** Command object for "OK" command. */    private Command okCmd = new Command(Resource.getString("OK"),                                            Command.OK, 1);    /** Holds the preempt token so the form can end. */    private Object preemptToken;        /** Holds the answer to the security question. */    private int answer = CANCELLED;    /**     * Construct permission dialog with a different answer for cancel.     * <p>     * The title, question, and answer strings will be translated,     * if a string resource is available.     * Since the strings can have sustitution token in them, if there is a     * "%" it must changed to "%%". If a string has a %1, the app parameter     * will be substituted for it. If a string has a "%2, the resource     * parameter will be substituted for it. If a string has a %3, the     * extraValue parameter will be substituted for it.     *     * @param token security token with the permission to peempt the     *        foreground display     * @param title title of the dialog     * @param question question to ask user     * @param app name of the application to insert into a string     *        can be null if no %1 a string     * @param resource string to insert into a string,     *        can be null if no %2 in a string     * @param extraValue string to insert into a string,     *        can be null if no %3 in a string     * @param maximumLevel maximum permission level to display     * @param defaultLevel default permission level, DENY_SESSION,     *         or CANCELLED for "Not Now". If the level is greater than the     *        maximum level, maximum level will be used.     * @param skip permission to skip, SESSION, DENY,     *        or any other for skip none, multiple permissions can be added     *        together     * @param blanketAnswer text for the blanket answer     * @param sessionAnswer text for the session answer or one shot if     *        the maximum permission level is one shot     * @param cancelAnswer text for the cancel answer     * @param denyAnswer text for the deny answer     *     * @exception InterruptedException if another thread interrupts the     *   calling thread while this method is waiting to preempt the     *   display.     */    PermissionDialog(SecurityToken token, String title, String question,            String app, String resource, String extraValue, int maximumLevel,            int defaultLevel, int skip, String blanketAnswer,            String sessionAnswer, String cancelAnswer, String denyAnswer)            throws InterruptedException {        String[] substitutions = {app, resource, extraValue};        Form form = new Form(Resource.getString(title, substitutions));        choice = new RadioButtonSet(5,                     Resource.getString(question, substitutions));        switch (maximumLevel) {        case Permissions.BLANKET:            choice.append(Resource.getString(blanketAnswer, substitutions),                null, Permissions.BLANKET);        case Permissions.SESSION:            if ((skip & Permissions.SESSION) != Permissions.SESSION) {                choice.append(Resource.getString(sessionAnswer, substitutions),                    null, Permissions.SESSION);            }            break;        case Permissions.ONE_SHOT:            choice.append(Resource.getString(sessionAnswer, substitutions),                null, Permissions.ONE_SHOT);        }        choice.append(Resource.getString(cancelAnswer, substitutions), null,                      CANCELLED);        if ((skip & Permissions.DENY) != Permissions.DENY) {            choice.append(Resource.getString(denyAnswer, substitutions),                null, Permissions.DENY);        }        switch (defaultLevel) {        case Permissions.BLANKET:        case Permissions.SESSION:        case Permissions.ONE_SHOT:        case Permissions.DENY:            break;        case Permissions.DENY_SESSION:            // adjust for internal purposes        default:            defaultLevel = CANCELLED;        }        try {            choice.setDefaultButton(defaultLevel);        } catch (IndexOutOfBoundsException e) {            choice.setDefaultButton(CANCELLED);        }        choice.setPreferredSize(form.getWidth(), -1);        form.append(choice);        form.addCommand(okCmd);        form.setCommandListener(this);        preemptToken = displayManager.preemptDisplay(token, this, form, true);    }    /**     * Waits for the user's answer.     *     * @return user's answer     */    int waitForAnswer() {        synchronized (this) {            if (preemptToken == null) {                return CANCELLED;            }            try {                wait();            } catch (Throwable t) {                return CANCELLED;            }            return answer;        }    }    /**     * Sets the user's answer and notifies waitForAnswer and     * ends the form.     *     * @param theAnswer user's answer or CANCEL if system cancelled the     *        screen     */    private void setAnswer(int theAnswer) {        synchronized (this) {            answer = theAnswer;            displayManager.donePreempting(preemptToken);            notify();        }    }    /**     * Respond to a command issued on security question form.     *     * @param c command activiated by the user     * @param s the Displayable the command was on.     */    public void commandAction(Command c, Displayable s) {        if (c == okCmd) {            setAnswer(choice.getSelectedButton());        }    }    /**     * Pause the current foreground MIDlet and return to the     * AMS or "selector" to possibly run another MIDlet in the     * currently active suite.     * <p>     * This is not apply to the security dialog.     *     * @param midlet midlet that the event applies to     */    public void pauseMIDlet(MIDlet midlet) {}     /**     * Start the currently suspended state. This is not apply to     * the security dialog.     *     * @param midlet midlet that the event applies to     */    public void startMIDlet(MIDlet midlet) {}     /**     * Destroy the MIDlet given midlet.     * <p>     * This is not apply to the security dialog.     *     * @param midlet midlet that the event applies to     */    public void destroyMIDlet(MIDlet midlet) {    }}/** * A <code>RadioButtonSet</code> is a group radio buttons intended to be * placed within a <code>Form</code>. However the radio buttons can be * accessed by a assigned ID instead of by index. This lets the calling * code be the same when dealing with dynamic sets. */class RadioButtonSet extends ChoiceGroup {    /** Keeps track of the button IDs. */    private int[] ids;    /**     * Creates a new, empty <code>RadioButtonSet</code>, specifying its     * title.     *     * @param maxButtons maximum number of buttons the set could have     * @param label the item's label (see {@link Item Item})     */    RadioButtonSet(int maxButtons, String label) {        super(label, Choice.EXCLUSIVE);        ids = new int[maxButtons];    }    /**     * Appends choice to the set.     *     * @param stringPart the string part of the element to be added     * @param imagePart the image part of the element to be added, or     * <code>null</code> if there is no image part     * @param id ID for the radio button     *     * @throws IllegalArgumentException if the image is mutable     * @throws NullPointerException if <code>stringPart</code> is     * <code>null</code>     * @throws IndexOutOfBoundsException this call would exceed the maximum     *         number of buttons for this set     */    public void append(String stringPart, Image imagePart, int id) {        ids[append(stringPart, imagePart)] = id;    }    /**     * Set the default button.     *     * @param id ID of default button     *     * @throws IndexOutOfBoundsException if <code>id</code> is invalid     */    public void setDefaultButton(int id) {         setSelectedIndex(indexFor(id), true);    }    /**     * Returns the ID of the selected radio button.     *     * @return ID of selected element     */    public int getSelectedButton() {         return ids[getSelectedIndex()];    }    /**     * Find the index for an ID.     *     * @param id button id     *     * @return index for a button     *     * @exception IndexOutOfBoundsException If no element exists with that ID     */    private int indexFor(int id) {        for (int i = 0; i < ids.length; i++) {            if (ids[i] == id) {                return i;            }        }        throw new IndexOutOfBoundsException();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -