Example usage for android.app AlertDialog.Builder setOnCancelListener

List of usage examples for android.app AlertDialog.Builder setOnCancelListener

Introduction

In this page you can find the example usage for android.app AlertDialog.Builder setOnCancelListener.

Prototype

public void setOnCancelListener(@Nullable OnCancelListener listener) 

Source Link

Document

Set a listener to be invoked when the dialog is canceled.

Usage

From source file:de.toshsoft.tsremote.configuration.ConfigurationMainFragment.java

private void startAddRemoteWizard(final View rootView) {
    AlertDialog.Builder alert = new AlertDialog.Builder(rootView.getContext());
    final LayoutInflater inflater = LayoutInflater.from(rootView.getContext());
    final View alertView = inflater.inflate(R.layout.configuration_add_remote_wizard,
            (ViewGroup) rootView.getParent(), false);

    alert.setTitle(getResources().getString(R.string.add_new_remote_title));
    alert.setMessage(getResources().getString(R.string.add_new_remote_message));
    alert.setView(alertView);/*  w  w w  . j  av a 2s. c o  m*/

    alert.setPositiveButton(getResources().getString(R.string.add_remote_ok_button),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    DatabaseHelper dbHelper = new DatabaseHelper(rootView.getContext());
                    String name = ((EditText) alertView.findViewById(R.id.remote_name)).getText().toString();
                    String vendor = ((EditText) alertView.findViewById(R.id.remote_vendor)).getText()
                            .toString();
                    String type = ((EditText) alertView.findViewById(R.id.remote_type)).getText().toString();
                    if (!name.isEmpty()) {
                        // Add the remote to the db...
                        dbHelper.insertRemote(name, vendor, type, "", "", "", "", "", "", "", "", "", "", "",
                                "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
                                "", "", "", "", "", "", "", "");
                        remotesListAdapter.add(name);

                        // ...and refresh the list
                        LinearLayout remotePreview = (LinearLayout) rootView.findViewById(R.id.theme_preview);
                        final View remoteView = inflater.inflate(R.layout.standout_activity_remote,
                                remotePreview, true);
                        ListView list = (ListView) rootView.findViewById(R.id.configuration_remotes_list);
                        list.setSelection(0);
                        TextView remoteId = (TextView) remoteView.findViewById(R.id.textview_remote_id);
                        remoteId.setText(name);
                        remoteView.setVisibility(View.VISIBLE);

                        // Move the Add Device to the end of the list
                        remotesListAdapter.remove(getResources().getString(R.string.add_device_prompt));
                        remotesListAdapter.add(getResources().getString(R.string.add_device_prompt));

                        // Now load the remote ...
                        try {
                            dbHelper.open();
                            Cursor cur = dbHelper.getRemote(remotesListAdapter.getItem(0));

                            // ... and set the keys
                            if (cur.getCount() == 1) {
                                prepareRemote(remoteView, cur);
                            }

                            cur.close();
                            dbHelper.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    } else {
                        // TODO: There was no data entered, find a better way
                        // to redisplay the Settings and show the error
                        getActivity().finish();
                        Intent dialogIntent = new Intent(rootView.getContext(), ConfigurationActivity.class);
                        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        rootView.getContext().startActivity(dialogIntent);
                    }
                }
            });

    alert.setNegativeButton(getResources().getString(R.string.add_remote_cancel_button),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    if (remotesListAdapter.getCount() == 1) {
                        // Close as there is no remote
                        getActivity().finish();
                    }
                }
            });

    alert.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            if (remotesListAdapter.getCount() == 1) {
                // Close as there is no remote
                getActivity().finish();
            }
        }
    });

    alert.show();
}

From source file:com.cerema.cloud2.ui.activity.Uploader.java

@Override
protected Dialog onCreateDialog(final int id) {
    final AlertDialog.Builder builder = new Builder(this);
    switch (id) {
    case DIALOG_WAITING:
        final ProgressDialog pDialog = new ProgressDialog(this, R.style.ProgressDialogTheme);
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);/*from w ww  .  j a  va2  s.  c  o m*/
        pDialog.setMessage(getResources().getString(R.string.uploader_info_uploading));
        pDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                ProgressBar v = (ProgressBar) pDialog.findViewById(android.R.id.progress);
                v.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.color_accent),
                        android.graphics.PorterDuff.Mode.MULTIPLY);

            }
        });
        return pDialog;
    case DIALOG_NO_ACCOUNT:
        builder.setIcon(R.drawable.ic_warning);
        builder.setTitle(R.string.uploader_wrn_no_account_title);
        builder.setMessage(
                String.format(getString(R.string.uploader_wrn_no_account_text), getString(R.string.app_name)));
        builder.setCancelable(false);
        builder.setPositiveButton(R.string.uploader_wrn_no_account_setup_btn_text, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                    // using string value since in API7 this
                    // constant is not defined
                    // in API7 < this constant is defined in
                    // Settings.ADD_ACCOUNT_SETTINGS
                    // and Settings.EXTRA_AUTHORITIES
                    Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
                    intent.putExtra("authorities", new String[] { MainApp.getAuthTokenType() });
                    startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
                } else {
                    // since in API7 there is no direct call for
                    // account setup, so we need to
                    // show our own AccountSetupAcricity, get
                    // desired results and setup
                    // everything for ourself
                    Intent intent = new Intent(getBaseContext(), AccountAuthenticator.class);
                    startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
                }
            }
        });
        builder.setNegativeButton(R.string.uploader_wrn_no_account_quit_btn_text, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        return builder.create();
    case DIALOG_MULTIPLE_ACCOUNT:
        CharSequence ac[] = new CharSequence[mAccountManager
                .getAccountsByType(MainApp.getAccountType()).length];
        for (int i = 0; i < ac.length; ++i) {
            ac[i] = DisplayUtils.convertIdn(mAccountManager.getAccountsByType(MainApp.getAccountType())[i].name,
                    false);
        }
        builder.setTitle(R.string.common_choose_account);
        builder.setItems(ac, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                setAccount(mAccountManager.getAccountsByType(MainApp.getAccountType())[which]);
                onAccountSet(mAccountWasRestored);
                dialog.dismiss();
                mAccountSelected = true;
                mAccountSelectionShowing = false;
            }
        });
        builder.setCancelable(true);
        builder.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                mAccountSelectionShowing = false;
                dialog.cancel();
                finish();
            }
        });
        return builder.create();
    case DIALOG_NO_STREAM:
        builder.setIcon(R.drawable.ic_warning);
        builder.setTitle(R.string.uploader_wrn_no_content_title);
        builder.setMessage(R.string.uploader_wrn_no_content_text);
        builder.setCancelable(false);
        builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        return builder.create();
    default:
        throw new IllegalArgumentException("Unknown dialog id: " + id);
    }
}

From source file:org.petero.droidfish.DroidFish.java

private final Dialog selectEngineDialog(final boolean abortOnCancel) {
    final ArrayList<String> items = new ArrayList<String>();
    final ArrayList<String> ids = new ArrayList<String>();
    ids.add("stockfish");
    items.add(getString(R.string.stockfish_engine));
    ids.add("cuckoochess");
    items.add(getString(R.string.cuckoochess_engine));

    final String sep = File.separator;
    final String base = Environment.getExternalStorageDirectory() + sep + engineDir + sep;
    {//from   w w  w.j a v a  2  s .c om
        ChessEngineResolver resolver = new ChessEngineResolver(this);
        List<ChessEngine> engines = resolver.resolveEngines();
        ArrayList<Pair<String, String>> oexEngines = new ArrayList<Pair<String, String>>();
        for (ChessEngine engine : engines) {
            if ((engine.getName() != null) && (engine.getFileName() != null)
                    && (engine.getPackageName() != null)) {
                oexEngines.add(
                        new Pair<String, String>(EngineUtil.openExchangeFileName(engine), engine.getName()));
            }
        }
        Collections.sort(oexEngines, new Comparator<Pair<String, String>>() {
            @Override
            public int compare(Pair<String, String> lhs, Pair<String, String> rhs) {
                return lhs.second.compareTo(rhs.second);
            }
        });
        for (Pair<String, String> eng : oexEngines) {
            ids.add(base + EngineUtil.openExchangeDir + sep + eng.first);
            items.add(eng.second);
        }
    }

    String[] fileNames = findFilesInDirectory(engineDir, new FileNameFilter() {
        @Override
        public boolean accept(String filename) {
            return !reservedEngineName(filename);
        }
    });
    for (String file : fileNames) {
        ids.add(base + file);
        items.add(file);
    }

    String currEngine = ctrl.getEngine();
    int defaultItem = 0;
    final int nEngines = items.size();
    for (int i = 0; i < nEngines; i++) {
        if (ids.get(i).equals(currEngine)) {
            defaultItem = i;
            break;
        }
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.select_chess_engine);
    builder.setSingleChoiceItems(items.toArray(new String[0]), defaultItem,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    if ((item < 0) || (item >= nEngines))
                        return;
                    Editor editor = settings.edit();
                    String engine = ids.get(item);
                    editor.putString("engine", engine);
                    editor.commit();
                    dialog.dismiss();
                    int strength = settings.getInt("strength", 1000);
                    setEngineOptions(false);
                    setEngineStrength(engine, strength);
                }
            });
    builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            if (!abortOnCancel) {
                removeDialog(MANAGE_ENGINES_DIALOG);
                showDialog(MANAGE_ENGINES_DIALOG);
            }
        }
    });
    AlertDialog alert = builder.create();
    return alert;
}

From source file:com.synox.android.ui.activity.Uploader.java

@Override
protected Dialog onCreateDialog(final int id) {
    final AlertDialog.Builder builder = new Builder(this);
    switch (id) {
    case DIALOG_WAITING:
        final ProgressDialog pDialog = new ProgressDialog(this, R.style.ProgressDialogTheme);
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);/*from  ww w.jav  a2s  . c  o  m*/
        pDialog.setMessage(getResources().getString(R.string.uploader_info_uploading));
        pDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                ProgressBar v = (ProgressBar) pDialog.findViewById(android.R.id.progress);
                v.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.accent),
                        android.graphics.PorterDuff.Mode.MULTIPLY);

            }
        });
        return pDialog;
    case DIALOG_NO_ACCOUNT:
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(R.string.uploader_wrn_no_account_title);
        builder.setMessage(
                String.format(getString(R.string.uploader_wrn_no_account_text), getString(R.string.app_name)));
        builder.setCancelable(false);
        builder.setPositiveButton(R.string.uploader_wrn_no_account_setup_btn_text, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                    // using string value since in API7 this
                    // constatn is not defined
                    // in API7 < this constatant is defined in
                    // Settings.ADD_ACCOUNT_SETTINGS
                    // and Settings.EXTRA_AUTHORITIES
                    Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
                    intent.putExtra("authorities", new String[] { MainApp.getAuthTokenType() });
                    startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
                } else {
                    // since in API7 there is no direct call for
                    // account setup, so we need to
                    // show our own AccountSetupAcricity, get
                    // desired results and setup
                    // everything for ourself
                    Intent intent = new Intent(getBaseContext(), AccountAuthenticator.class);
                    startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
                }
            }
        });
        builder.setNegativeButton(R.string.uploader_wrn_no_account_quit_btn_text, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        return builder.create();
    case DIALOG_MULTIPLE_ACCOUNT:
        CharSequence ac[] = new CharSequence[mAccountManager
                .getAccountsByType(MainApp.getAccountType()).length];
        for (int i = 0; i < ac.length; ++i) {
            ac[i] = DisplayUtils.convertIdn(mAccountManager.getAccountsByType(MainApp.getAccountType())[i].name,
                    false);
        }
        builder.setTitle(R.string.common_choose_account);
        builder.setItems(ac, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                setAccount(mAccountManager.getAccountsByType(MainApp.getAccountType())[which]);
                onAccountSet(mAccountWasRestored);
                dialog.dismiss();
                mAccountSelected = true;
                mAccountSelectionShowing = false;
            }
        });
        builder.setCancelable(true);
        builder.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                mAccountSelectionShowing = false;
                dialog.cancel();
                finish();
            }
        });
        return builder.create();
    case DIALOG_NO_STREAM:
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(R.string.uploader_wrn_no_content_title);
        builder.setMessage(R.string.uploader_wrn_no_content_text);
        builder.setCancelable(false);
        builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        return builder.create();
    default:
        throw new IllegalArgumentException("Unknown dialog id: " + id);
    }
}

From source file:edu.mit.viral.shen.DroidFish.java

private final Dialog networkEngineDialog() {
    String[] fileNames = findFilesInDirectory(engineDir, new FileNameFilter() {
        @Override//from   www  . j  av a  2 s.co  m
        public boolean accept(String filename) {
            if (internalEngine(filename))
                return false;
            try {
                InputStream inStream = new FileInputStream(filename);
                InputStreamReader inFile = new InputStreamReader(inStream);
                char[] buf = new char[4];
                boolean ret = (inFile.read(buf) == 4) && "NETE".equals(new String(buf));
                inFile.close();
                return ret;
            } catch (IOException e) {
                return false;
            }
        }
    });
    final int numFiles = fileNames.length;
    final int numItems = numFiles + 1;
    final String[] items = new String[numItems];
    final String[] ids = new String[numItems];
    int idx = 0;
    String sep = File.separator;
    String base = Environment.getExternalStorageDirectory() + sep + engineDir + sep;
    for (int i = 0; i < numFiles; i++) {
        ids[idx] = base + fileNames[i];
        items[idx] = fileNames[i];
        idx++;
    }
    ids[idx] = "";
    items[idx] = getString(R.string.new_engine);
    idx++;
    String currEngine = ctrl.getEngine();
    int defaultItem = 0;
    for (int i = 0; i < numItems; i++)
        if (ids[i].equals(currEngine)) {
            defaultItem = i;
            break;
        }
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.configure_network_engine);
    builder.setSingleChoiceItems(items, defaultItem, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            if ((item < 0) || (item >= numItems))
                return;
            dialog.dismiss();
            if (item == numItems - 1) {
                showDialog(NEW_NETWORK_ENGINE_DIALOG);
            } else {
                networkEngineToConfig = ids[item];
                removeDialog(NETWORK_ENGINE_CONFIG_DIALOG);
                showDialog(NETWORK_ENGINE_CONFIG_DIALOG);
            }
        }
    });
    builder.setOnCancelListener(new Dialog.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            removeDialog(MANAGE_ENGINES_DIALOG);
            showDialog(MANAGE_ENGINES_DIALOG);
        }
    });
    AlertDialog alert = builder.create();
    return alert;
}

From source file:edu.mit.viral.shen.DroidFish.java

private final Dialog selectEngineDialog(final boolean abortOnCancel) {
    String[] fileNames = findFilesInDirectory(engineDir, new FileNameFilter() {
        @Override//w  w  w . j a va2s.com
        public boolean accept(String filename) {
            return !internalEngine(filename);
        }
    });
    final int numFiles = fileNames.length;
    boolean haveSf = EngineUtil.internalStockFishName() != null;
    final int nEngines = numFiles + 1 + (haveSf ? 1 : 0);
    final String[] items = new String[nEngines];
    final String[] ids = new String[nEngines];
    int idx = 0;
    if (haveSf) {
        ids[idx] = "stockfish";
        items[idx] = getString(R.string.stockfish_engine);
        idx++;
    }
    ids[idx] = "cuckoochess";
    items[idx] = getString(R.string.cuckoochess_engine);
    idx++;
    String sep = File.separator;
    String base = Environment.getExternalStorageDirectory() + sep + engineDir + sep;
    for (int i = 0; i < numFiles; i++) {
        ids[idx] = base + fileNames[i];
        items[idx] = fileNames[i];
        idx++;
    }
    String currEngine = ctrl.getEngine();
    int defaultItem = 0;
    for (int i = 0; i < nEngines; i++) {
        if (ids[i].equals(currEngine)) {
            defaultItem = i;
            break;
        }
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.select_chess_engine);
    builder.setSingleChoiceItems(items, defaultItem, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            if ((item < 0) || (item >= nEngines))
                return;
            Editor editor = settings.edit();
            String engine = ids[item];
            editor.putString("engine", engine);
            editor.commit();
            dialog.dismiss();
            int strength = settings.getInt("strength", 1000);
            setEngineOptions(false);
            setEngineStrength(engine, strength);
        }
    });
    builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            if (!abortOnCancel) {
                removeDialog(MANAGE_ENGINES_DIALOG);
                showDialog(MANAGE_ENGINES_DIALOG);
            }
        }
    });
    AlertDialog alert = builder.create();
    return alert;
}

From source file:edu.cmu.cylab.starslinger.view.HomeActivity.java

private AlertDialog.Builder xshowManagePassphrases(final Activity act, Bundle args) {
    String msg = args.getString(extra.RESID_MSG);
    boolean allowDelete = args.getBoolean(extra.ALLOW_DELETE);
    AlertDialog.Builder ad = new AlertDialog.Builder(act);
    View layout;/*  w ww  .j  a v a2s. co m*/
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        layout = View.inflate(new ContextThemeWrapper(act, R.style.Theme_AppCompat), R.layout.about, null);
    } else {
        LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.about, null);
    }
    TextView textViewAbout = (TextView) layout.findViewById(R.id.TextViewAbout);
    ad.setTitle(R.string.menu_ManagePassphrases);
    textViewAbout.setText(msg);
    ad.setView(layout);
    ad.setCancelable(true);
    if (allowDelete) { // only have delete key when recent keys exist
        ad.setPositiveButton(R.string.btn_DeleteKeys, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                // delete all more recent keys for now...
                doRemoveMoreRecentKeys();
                refreshView();
            }
        });
    }
    ad.setNegativeButton(R.string.btn_Cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    ad.setOnCancelListener(new OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            dialog.dismiss();
        }
    });
    return ad;
}

From source file:edu.mit.viral.shen.DroidFish.java

private Dialog deleteNetworkEngineDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.delete_network_engine);
    String msg = networkEngineToConfig;
    if (msg.lastIndexOf('/') >= 0)
        msg = msg.substring(msg.lastIndexOf('/') + 1);
    builder.setMessage(getString(R.string.network_engine) + ": " + msg);
    builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            new File(networkEngineToConfig).delete();
            String engine = settings.getString("engine", "stockfish");
            if (engine.equals(networkEngineToConfig)) {
                engine = "stockfish";
                Editor editor = settings.edit();
                editor.putString("engine", engine);
                editor.commit();// w  ww .j  a v a 2  s .c om
                dialog.dismiss();
                int strength = settings.getInt("strength", 1000);
                setEngineOptions(false);
                setEngineStrength(engine, strength);
            }
            dialog.cancel();
            removeDialog(NETWORK_ENGINE_DIALOG);
            showDialog(NETWORK_ENGINE_DIALOG);
        }
    });
    builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
            removeDialog(NETWORK_ENGINE_DIALOG);
            showDialog(NETWORK_ENGINE_DIALOG);
        }
    });
    builder.setOnCancelListener(new Dialog.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            removeDialog(NETWORK_ENGINE_DIALOG);
            showDialog(NETWORK_ENGINE_DIALOG);
        }
    });
    AlertDialog alert = builder.create();
    return alert;
}

From source file:com.dsi.ant.antplus.pluginsampler.watchdownloader.Activity_WatchScanList.java

/** Called when the activity is first created. */
@Override/*from w w w.j a v  a2s.c o  m*/
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_device_list);

    tv_status = (TextView) findViewById(R.id.textView_Status);

    deviceList_Display = new ArrayList<Map<String, String>>();
    adapter_deviceList_Display = new SimpleAdapter(this, deviceList_Display,
            android.R.layout.simple_list_item_2, new String[] { "title", "desc" },
            new int[] { android.R.id.text1, android.R.id.text2 });

    ListView listView_Devices = (ListView) findViewById(R.id.listView_deviceList);
    listView_Devices.setAdapter(adapter_deviceList_Display);

    //Set the list to download the data for the selected device and display it.
    listView_Devices.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos, long id) {
            if (!bDevicesInList || watchPcc == null)
                return;

            final CharSequence[] downloadOptions = { "All Activities", "New Activities",
                    "Wait For New Activities" };
            AlertDialog.Builder builder = new AlertDialog.Builder(Activity_WatchScanList.this);
            builder.setTitle("Download...");
            builder.setItems(downloadOptions, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int downloadSelection) {
                    antFsProgressDialog = new ProgressDialog(Activity_WatchScanList.this);
                    antFsProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    antFsProgressDialog.setMessage("Sending Request...");
                    antFsProgressDialog.setCancelable(false);
                    antFsProgressDialog.setIndeterminate(false);

                    fitFileList = new ArrayList<FitFile>();

                    if (downloadSelection == 0) // download all
                    {
                        if (watchPcc.requestDownloadAllActivities(deviceInfoArray[pos].getDeviceUUID(),
                                new DownloadActivitiesFinished(pos), new FileDownloadedReceiver(),
                                new AntFsUpdateReceiver())) {
                            antFsProgressDialog.show();
                        }

                    } else if (downloadSelection == 1) // download new
                    {
                        if (watchPcc.requestDownloadNewActivities(deviceInfoArray[pos].getDeviceUUID(),
                                new DownloadActivitiesFinished(pos), new FileDownloadedReceiver(),
                                new AntFsUpdateReceiver())) {
                            antFsProgressDialog.show();
                        }
                    } else if (downloadSelection == 2) {
                        boolean reqSubmitted = watchPcc.listenForNewActivities(
                                deviceInfoArray[pos].getDeviceUUID(),
                                new IDownloadActivitiesFinishedReceiver() {
                                    @Override
                                    public void onNewDownloadActivitiesFinished(AntFsRequestStatus status) {
                                        //Only received on cancel right now, only thing to do is cancel dialog, already taken care of below
                                    }
                                }, new FileDownloadedReceiver() {
                                    @Override
                                    public void onNewFitFileDownloaded(FitFile downloadedFitFile) {
                                        super.onNewFitFileDownloaded(downloadedFitFile);

                                        //Now show each file as we get it
                                        List<FitFile> newActivityOnly = new ArrayList<FitFile>(fitFileList);
                                        fitFileList.clear();
                                        final Dialog_WatchData dataDialog = new Dialog_WatchData(
                                                deviceInfoArray[pos], newActivityOnly);
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                dataDialog.show(getSupportFragmentManager(), "DeviceData");
                                            }
                                        });
                                    }
                                });

                        //Once the listener is started we leave this dialog open until it is cancelled
                        //Note: Because the listener is an asynchronous process, you do not need to block the UI like the sample app does with this, you can leave it invisible to the user
                        if (reqSubmitted) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(Activity_WatchScanList.this);
                            LayoutInflater inflater = Activity_WatchScanList.this.getLayoutInflater();

                            // Inflate and set the layout for the dialog
                            // Pass null as the parent view because its going in the dialog layout
                            View detailsView = inflater.inflate(R.layout.dialog_progresswaiter, null);
                            TextView textView_status = (TextView) detailsView
                                    .findViewById(R.id.textView_Status);
                            textView_status.setText("Waiting for new activities on "
                                    + deviceInfoArray[pos].getDisplayName() + "...");
                            builder.setView(detailsView);
                            builder.setNegativeButton("Cancel", new OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //Let onCancelListener take care of business
                                    dialog.cancel();
                                }
                            });
                            builder.setOnCancelListener(new OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface dialog) {
                                    if (watchPcc != null) {
                                        watchPcc.cancelListenForNewActivities(
                                                deviceInfoArray[pos].getDeviceUUID());
                                    }
                                }
                            });
                            AlertDialog waitDialog = builder.create();
                            waitDialog.show();
                        }
                    }
                }
            });

            AlertDialog alert = builder.create();
            alert.show();
        }
    });
    resetPcc();
}

From source file:org.petero.droidfish.DroidFish.java

private final Dialog newNetworkEngineDialog() {
    View content = View.inflate(this, R.layout.create_network_engine, null);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(content);/*w w w  .j a v  a2s  .  c o m*/
    builder.setTitle(R.string.create_network_engine);
    final EditText engineNameView = (EditText) content.findViewById(R.id.create_network_engine);
    engineNameView.setText("");
    final Runnable createEngine = new Runnable() {
        public void run() {
            String engineName = engineNameView.getText().toString();
            String sep = File.separator;
            String pathName = Environment.getExternalStorageDirectory() + sep + engineDir + sep + engineName;
            File file = new File(pathName);
            boolean nameOk = true;
            int errMsg = -1;
            if (engineName.contains("/")) {
                nameOk = false;
                errMsg = R.string.slash_not_allowed;
            } else if (reservedEngineName(engineName) || file.exists()) {
                nameOk = false;
                errMsg = R.string.engine_name_in_use;
            }
            if (!nameOk) {
                Toast.makeText(getApplicationContext(), errMsg, Toast.LENGTH_LONG).show();
                removeDialog(NETWORK_ENGINE_DIALOG);
                showDialog(NETWORK_ENGINE_DIALOG);
                return;
            }
            networkEngineToConfig = pathName;
            removeDialog(NETWORK_ENGINE_CONFIG_DIALOG);
            showDialog(NETWORK_ENGINE_CONFIG_DIALOG);
        }
    };
    builder.setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            createEngine.run();
        }
    });
    builder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            removeDialog(NETWORK_ENGINE_DIALOG);
            showDialog(NETWORK_ENGINE_DIALOG);
        }
    });
    builder.setOnCancelListener(new Dialog.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            removeDialog(NETWORK_ENGINE_DIALOG);
            showDialog(NETWORK_ENGINE_DIALOG);
        }
    });

    final Dialog dialog = builder.create();
    engineNameView.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                createEngine.run();
                dialog.cancel();
                return true;
            }
            return false;
        }
    });
    return dialog;
}