public Dialog createSelectGroupDialog(final MainActivity activity, final BusHandler busHandler) {
    Log.i(TAG, "createParticipantsDialog()");
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.selectgroupdialog);

    ArrayAdapter<String> groupListAdapter =
        new ArrayAdapter<String>(activity, android.R.layout.test_list_item);
    final ListView groupList = (ListView) dialog.findViewById(R.id.groupList);
    groupList.setAdapter(groupListAdapter);

    List<String> groups = new ArrayList<String>(busHandler.listHostedGroups());
    List<String> joinedGroups = busHandler.listJoinedGroups();

    // Combine the list of Hosted Groups and Joined Groups
    for (String group : joinedGroups) {
      if (!groups.contains(group)) {
        groups.add(group);
      }
    }

    // Transfer groups from the ArrayList given by the GroupManager to the local ArrayAdapter
    for (String group : groups) {
      groupListAdapter.add(group);
    }
    groupListAdapter.notifyDataSetChanged();

    groupList.setOnItemClickListener(
        new ListView.OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Create the get peers dialog after the user selects the group
            String name = groupList.getItemAtPosition(position).toString();
            Bundle args = new Bundle();
            args.putString("groupName", name);
            activity.showDialog(MainActivity.DIALOG_GET_PEERS_ID, args);
            /*
             * Android likes to reuse dialogs for performance reasons.  If
             * we reuse this one, the list of channels will eventually be
             * wrong since it can change.  We have to tell the Android
             * application framework to forget about this dialog completely.
             */
            activity.removeDialog(MainActivity.DIALOG_SELECT_GET_PEER_GROUP_ID);
          }
        });

    Button ok = (Button) dialog.findViewById(R.id.cancelSelectGroupButton);
    ok.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View view) {
            activity.removeDialog(MainActivity.DIALOG_SELECT_GET_PEER_GROUP_ID);
          }
        });

    return dialog;
  }
  public Dialog createJoinGroupDialog(
      final MainActivity activity, final BusHandler busHandler, final UIHandler uiHandler) {
    Log.i(TAG, "createJoinGroupDialog()");
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.joingroupdialog);

    ArrayAdapter<String> groupListAdapter =
        new ArrayAdapter<String>(activity, android.R.layout.test_list_item);
    final ListView groupList = (ListView) dialog.findViewById(R.id.joinGroupList);
    groupList.setAdapter(groupListAdapter);

    List<String> groups = busHandler.listGroups();

    // remove all already hosted and joined groups
    groups.removeAll(busHandler.listHostedGroups());
    groups.removeAll(busHandler.listJoinedGroups());

    // Transfer groups from the ArrayList given by the GroupManager to the local ArrayAdapter
    for (String group : groups) {
      groupListAdapter.add(group);
    }
    groupListAdapter.notifyDataSetChanged();

    groupList.setOnItemClickListener(
        new ListView.OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Call join group on the selected advertised group
            String name = groupList.getItemAtPosition(position).toString();
            activity.joinGroup(name);
            /*
             * Android likes to reuse dialogs for performance reasons.  If
             * we reuse this one, the list of channels will eventually be
             * wrong since it can change.  We have to tell the Android
             * application framework to forget about this dialog completely.
             */
            activity.removeDialog(MainActivity.DIALOG_JOIN_GROUP_ID);
          }
        });

    Button ok = (Button) dialog.findViewById(R.id.cancelJoinGroupButton);
    ok.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View view) {
            activity.removeDialog(MainActivity.DIALOG_JOIN_GROUP_ID);
          }
        });

    return dialog;
  }
  public Dialog createParticipantsDialog(
      final MainActivity activity, final BusHandler busHandler, String groupName) {
    Log.i(TAG, "createParticipantsDialog()");
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.peersdialog);

    ArrayAdapter<String> peersListAdapter =
        new ArrayAdapter<String>(activity, android.R.layout.test_list_item);
    final ListView peersList = (ListView) dialog.findViewById(R.id.peerList);
    peersList.setAdapter(peersListAdapter);

    // Call getParticipants
    List<String> peers = busHandler.getParticipants(groupName);
    for (String peer : peers) {
      peersListAdapter.add(peer);
    }
    peersListAdapter.notifyDataSetChanged();

    Button ok = (Button) dialog.findViewById(R.id.closeButton);
    ok.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View view) {
            activity.removeDialog(MainActivity.DIALOG_GET_PEERS_ID);
          }
        });

    return dialog;
  }
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mColorSpinner = (Spinner) findViewById(R.id.color_spinner);
    ArrayAdapter<CharSequence> colorAdapter =
        ArrayAdapter.createFromResource(
            this, R.array.color_array, android.R.layout.simple_spinner_item);
    colorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mColorSpinner.setAdapter(colorAdapter);
    mColorSpinner.setOnItemSelectedListener(new ColorSelectedListener());

    mTextSizeSpinner = (Spinner) findViewById(R.id.text_size);
    ArrayAdapter<CharSequence> textSizeAdapter =
        ArrayAdapter.createFromResource(
            this, R.array.text_size_array, android.R.layout.simple_spinner_item);
    textSizeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mTextSizeSpinner.setAdapter(textSizeAdapter);
    mTextSizeSpinner.setSelection(3); // 3 is regular text size at startup.
    mTextSizeSpinner.setOnItemSelectedListener(new TextSizeSelectedListener());

    mTextSizeLabel = (TextView) findViewById(R.id.text_size_label);
    mBackgroundColorLabel = (TextView) findViewById(R.id.background_color_label);

    mGetPropertiesButton = (Button) findViewById(R.id.get_properties);
    mGetPropertiesButton.setOnClickListener(new GetPropertiesListener());

    mSetPropertiesButton = (Button) findViewById(R.id.set_properties);
    mSetPropertiesButton.setOnClickListener(new SetPropertiesListener());

    mBackgroundColor = "";
    mTextSize = -1;

    /* Make all AllJoyn calls through a separate handler thread to prevent blocking the UI. */
    HandlerThread busThread = new HandlerThread("BusHandler");
    busThread.start();
    mBusHandler = new BusHandler(busThread.getLooper());

    /* Connect to an AllJoyn object. */
    mBusHandler.sendEmptyMessage(BusHandler.CONNECT);
    mHandler.sendEmptyMessage(MESSAGE_START_PROGRESS_DIALOG);
  }
  @Override
  protected void onDestroy() {
    super.onDestroy();

    mBusHandler.sendEmptyMessage(BusHandler.DISCONNECT);
  }