Example usage for android.content DialogInterface.OnMultiChoiceClickListener DialogInterface.OnMultiChoiceClickListener

List of usage examples for android.content DialogInterface.OnMultiChoiceClickListener DialogInterface.OnMultiChoiceClickListener

Introduction

In this page you can find the example usage for android.content DialogInterface.OnMultiChoiceClickListener DialogInterface.OnMultiChoiceClickListener.

Prototype

DialogInterface.OnMultiChoiceClickListener

Source Link

Usage

From source file:org.mozilla.gecko.GeckoApp.java

/**
 * @param aPermissions//from  w w  w  .  ja v a 2  s  . co m
 *        Array of JSON objects to represent site permissions.
 *        Example: { type: "offline-app", setting: "Store Offline Data: Allow" }
 */
private void showSiteSettingsDialog(String aHost, JSONArray aPermissions) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    View customTitleView = getLayoutInflater().inflate(R.layout.site_setting_title, null);
    ((TextView) customTitleView.findViewById(R.id.title)).setText(R.string.site_settings_title);
    ((TextView) customTitleView.findViewById(R.id.host)).setText(aHost);
    builder.setCustomTitle(customTitleView);

    // If there are no permissions to clear, show the user a message about that.
    // In the future, we want to disable the menu item if there are no permissions to clear.
    if (aPermissions.length() == 0) {
        builder.setMessage(R.string.site_settings_no_settings);
    } else {
        // Eventually we should use a list adapter and custom checkable list items
        // to make a two-line UI to match the mock-ups
        CharSequence[] items = new CharSequence[aPermissions.length()];
        boolean[] states = new boolean[aPermissions.length()];
        for (int i = 0; i < aPermissions.length(); i++) {
            try {
                items[i] = aPermissions.getJSONObject(i).getString("setting");
                // Make all the items checked by default
                states[i] = true;
            } catch (JSONException e) {
                Log.i(LOGTAG, "JSONException: " + e);
            }
        }
        builder.setMultiChoiceItems(items, states, new DialogInterface.OnMultiChoiceClickListener() {
            public void onClick(DialogInterface dialog, int item, boolean state) {
                // Do nothing
            }
        });
        builder.setPositiveButton(R.string.site_settings_clear, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                ListView listView = ((AlertDialog) dialog).getListView();
                SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();

                // An array of the indices of the permissions we want to clear
                JSONArray permissionsToClear = new JSONArray();
                for (int i = 0; i < checkedItemPositions.size(); i++) {
                    boolean checked = checkedItemPositions.get(i);
                    if (checked)
                        permissionsToClear.put(i);
                }
                GeckoAppShell.sendEventToGecko(
                        GeckoEvent.createBroadcastEvent("Permissions:Clear", permissionsToClear.toString()));
            }
        });
    }

    builder.setNegativeButton(R.string.site_settings_cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    mMainHandler.post(new Runnable() {
        public void run() {
            builder.create().show();
        }
    });
}