Example usage for android.app AlertDialog getWindow

List of usage examples for android.app AlertDialog getWindow

Introduction

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

Prototype

public @Nullable Window getWindow() 

Source Link

Document

Retrieve the current Window for the activity.

Usage

From source file:org.opendatakit.tables.activities.TableManager.java

private void alertForNewTableName(final boolean isNewTable, final TableType tableType, final TableProperties tp,
        String givenTableName) {/* w w w  .  ja  v a2s  .com*/

    AlertDialog newTableAlert;
    // Prompt an alert box
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    View view = getLayoutInflater().inflate(R.layout.message_with_text_edit_field_dialog, null);
    alert.setView(view).setTitle(R.string.create_new_table);

    final TextView msg = (TextView) view.findViewById(R.id.message);
    msg.setText(getString(R.string.enter_new_table_name));

    // Set an EditText view to get user input
    final EditText input = (EditText) view.findViewById(R.id.edit_field);
    if (givenTableName != null)
        input.setText(givenTableName);

    // OK Action => Create new Column
    alert.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String newTableName = input.getText().toString().trim();

            if (newTableName == null || newTableName.equals("")) {
                // Table name is empty string
                toastTableNameError(getString(R.string.error_table_name_empty));
                alertForNewTableName(isNewTable, tableType, tp, null);
            } else {
                if (isNewTable)
                    alertForTableId(isNewTable, tableType, tp, newTableName, null);
                else
                    tp.setDisplayName(newTableName);
                Log.d("TM", "got here");
                refreshList();
            }
        }
    });

    // Cancel Action
    alert.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
        }
    });

    newTableAlert = alert.create();
    newTableAlert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    newTableAlert.show();
}

From source file:com.morlunk.mumbleclient.servers.PublicServerListFragment.java

private void showFilterDialog() {
    View dialogView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.dialog_server_search, null);
    final EditText nameText = (EditText) dialogView.findViewById(R.id.server_search_name);
    final EditText countryText = (EditText) dialogView.findViewById(R.id.server_search_country);

    final AlertDialog dlg = new AlertDialog.Builder(getActivity()).setTitle(R.string.search).setView(dialogView)
            .setPositiveButton(R.string.search, new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int which) {
                    String queryName = nameText.getText().toString().toUpperCase(Locale.US);
                    String queryCountry = countryText.getText().toString().toUpperCase(Locale.US);
                    mServerAdapter.filter(queryName, queryCountry);
                    dialog.dismiss();/*from   ww w. j  a  va2 s. c om*/
                }
            }).create();

    nameText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
    nameText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
            String queryName = nameText.getText().toString().toUpperCase(Locale.US);
            String queryCountry = countryText.getText().toString().toUpperCase(Locale.US);
            mServerAdapter.filter(queryName, queryCountry);
            dlg.dismiss();
            return true;
        }
    });

    countryText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
    countryText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
            String queryName = nameText.getText().toString().toUpperCase(Locale.US);
            String queryCountry = countryText.getText().toString().toUpperCase(Locale.US);
            mServerAdapter.filter(queryName, queryCountry);
            dlg.dismiss();
            return true;
        }
    });

    // Show keyboard automatically
    nameText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                dlg.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });

    dlg.show();
}

From source file:org.rssin.android.NavigationDrawerManageFeedsFragment.java

/**
 * Open dialog to add new feed//from ww  w  .jav  a2s. co m
 * For the moment, we temporarily disable rotating because we can't get it working otherwise.
 * Possible feature: make rotating possible
 */
public void openAddDialog() {
    //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    final Context context = rootView.getContext();

    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setTitle(getString(R.string.feeds_activity_add_feed));
    alert.setMessage(getString(R.string.feeds_activity_url));

    final EditText input = new EditText(context);
    input.setFocusable(true);
    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
    input.setMaxLines(1);
    input.requestFocus();

    AlertDialog dialog = alert.setView(input).setPositiveButton(getResources().getString(R.string.button_apply),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String value = input.getText().toString();
                    try {
                        Feed f = new Feed(value, DefaultStorageProvider.getInstance(context),
                                VolleyFetcher.getInstance(context), new FallibleListener<String, Object>() {
                                    @Override
                                    public void onError(Object error) {

                                    }

                                    @Override
                                    public void onReceive(String data) {
                                        feedAdapter.notifyDataSetChanged();
                                    }
                                });

                        f.store(DefaultStorageProvider.getInstance(context));
                        feedsList.getFeeds().add(f);
                        feedAdapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        Frontend.error(context, R.string.error_save_feeds, e);
                    }
                    input.setText("");
                    //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                }
            }).setNegativeButton(getResources().getString(R.string.button_cancel),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            input.setText("");
                            //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                        }
                    })
            .setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    input.setText("");
                    //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                }
            }).create();

    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    dialog.show();
}

From source file:org.smap.smapTask.android.activities.MainTabsActivity.java

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case PROGRESS_DIALOG:
        mProgressDialog = new ProgressDialog(this);
        DialogInterface.OnClickListener loadingButtonListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//from  ww w . j  a  va 2s .c om
                mDownloadTasks.setDownloaderListener(null, mContext);
                mDownloadTasks.cancel(true);
                // Refresh the task list
                Intent intent = new Intent("refresh");
                LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
            }
        };
        mProgressDialog.setTitle(getString(R.string.downloading_data));
        mProgressDialog.setMessage(mProgressMsg);
        mProgressDialog.setIcon(android.R.drawable.ic_dialog_info);
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setButton(getString(R.string.cancel), loadingButtonListener);
        return mProgressDialog;
    case ALERT_DIALOG:
        mAlertDialog = new AlertDialog.Builder(this).create();
        mAlertDialog.setMessage(mAlertMsg);
        mAlertDialog.setTitle(getString(R.string.smap_get_tasks));
        DialogInterface.OnClickListener quitListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
                dialog.dismiss();
            }
        };
        mAlertDialog.setCancelable(false);
        mAlertDialog.setButton(getString(R.string.ok), quitListener);
        mAlertDialog.setIcon(android.R.drawable.ic_dialog_info);
        return mAlertDialog;
    case PASSWORD_DIALOG:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog passwordDialog = builder.create();

        passwordDialog.setTitle(getString(R.string.enter_admin_password));
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        input.setTransformationMethod(PasswordTransformationMethod.getInstance());
        passwordDialog.setView(input, 20, 10, 20, 10);

        passwordDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.ok),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String value = input.getText().toString();
                        String pw = mAdminPreferences.getString(AdminPreferencesActivity.KEY_ADMIN_PW, "");
                        if (pw.compareTo(value) == 0) {
                            Intent i = new Intent(getApplicationContext(), AdminPreferencesActivity.class);
                            startActivity(i);
                            input.setText("");
                            passwordDialog.dismiss();
                        } else {
                            Toast.makeText(MainTabsActivity.this, getString(R.string.admin_password_incorrect),
                                    Toast.LENGTH_SHORT).show();
                            Collect.getInstance().getActivityLogger().logAction(this, "adminPasswordDialog",
                                    "PASSWORD_INCORRECT");
                        }
                    }
                });

        passwordDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.cancel),
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        Collect.getInstance().getActivityLogger().logAction(this, "adminPasswordDialog",
                                "cancel");
                        input.setText("");
                        return;
                    }
                });

        passwordDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        return passwordDialog;
    }
    return null;
}

From source file:cm.aptoide.pt.MainActivity.java

public void showAbout() {
    View aboutView = LayoutInflater.from(this).inflate(R.layout.dialog_about, null);
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this).setView(aboutView);
    final AlertDialog aboutDialog = dialogBuilder.create();
    aboutDialog.setIcon(android.R.drawable.ic_menu_help);
    aboutDialog.setTitle(getString(R.string.about));
    aboutDialog.setCancelable(true);//from ww  w.j av a2  s.  c  o m

    WindowManager.LayoutParams params = aboutDialog.getWindow().getAttributes();
    params.width = WindowManager.LayoutParams.MATCH_PARENT;
    aboutDialog.getWindow().setAttributes(params);

    aboutDialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.btn_chlog),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Uri uri = Uri.parse(getString(R.string.change_log_url));
                    startActivity(new Intent(Intent.ACTION_VIEW, uri));
                }
            });

    aboutDialog.show();
}

From source file:org.odk.collect.android.activities.MainMenuActivity.java

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case PASSWORD_DIALOG:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog passwordDialog = builder.create();
        passwordDialog.setTitle(getString(R.string.enter_admin_password));
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialogbox_layout, null);
        passwordDialog.setView(dialogView, 20, 10, 20, 10);
        final CheckBox checkBox = (CheckBox) dialogView.findViewById(R.id.checkBox);
        final EditText input = (EditText) dialogView.findViewById(R.id.editText);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override/*from w  ww . j av a  2 s.  com*/
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!checkBox.isChecked()) {
                    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            }
        });
        passwordDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.ok),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String value = input.getText().toString();
                        String pw = adminPreferences.getString(AdminKeys.KEY_ADMIN_PW, "");
                        if (pw.compareTo(value) == 0) {
                            Intent i = new Intent(getApplicationContext(), AdminPreferencesActivity.class);
                            startActivity(i);
                            input.setText("");
                            passwordDialog.dismiss();
                        } else {
                            ToastUtils.showShortToast(R.string.admin_password_incorrect);
                            Collect.getInstance().getActivityLogger().logAction(this, "adminPasswordDialog",
                                    "PASSWORD_INCORRECT");
                        }
                    }
                });

        passwordDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.cancel),
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        Collect.getInstance().getActivityLogger().logAction(this, "adminPasswordDialog",
                                "cancel");
                        input.setText("");
                        return;
                    }
                });

        passwordDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        return passwordDialog;

    }
    return null;
}

From source file:org.opendatakit.tables.activities.TableManager.java

/**
 * Following the entering of a table display name a dialog should be
 * presented that suggests a table id and offers the user a chance to change
 * it. Only upon accepting the name here is the table created.
 * <p>/*from  w w  w  .java2s .  c  o  m*/
 * The first call can include a proposedTableId of null.
 * @param isNewTable
 * @param tableType
 * @param tp
 * @param proposedDisplayName
 * @param proposedTableId
 */
private void alertForTableId(final boolean isNewTable, final TableType tableType, final TableProperties tp,
        final String proposedDisplayName, String proposedTableId) {
    AlertDialog newTableAlert;
    // Prompt an alert box
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    View view = getLayoutInflater().inflate(R.layout.message_with_text_edit_field_dialog, null);
    alert.setView(view).setTitle(R.string.create_new_table);

    final TextView msg = (TextView) view.findViewById(R.id.message);
    msg.setText(getString(R.string.confirm_new_table_id));

    // Set an EditText view to get user input
    final EditText input = (EditText) view.findViewById(R.id.edit_field);
    // Try to create the id.
    if (proposedTableId == null) {
        proposedTableId = proposedDisplayName;
    }
    final String knownSafeId = NameUtil.createUniqueTableId(proposedDisplayName, dbh);
    input.setText(knownSafeId);

    Log.d(TAG, "[alertForTableId] received: " + proposedTableId + " as " + "proposed id, known safeId is: "
            + knownSafeId);

    // OK Action => Create new Column
    alert.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String proposedTableId = input.getText().toString().trim();

            if (proposedTableId == null || proposedTableId.equals("")) {
                // Table name is empty string
                toastTableNameError(getString(R.string.error_table_name_empty));
                alertForNewTableName(isNewTable, tableType, tp, null);
            } else if (!NameUtil.isValidUserDefinedDatabaseName(proposedTableId)) {
                Log.d(TAG, "[alertForTableId] proposed is is not valid " + "as a user-defined database name: "
                        + proposedTableId);
                toastTableNameError(getString(R.string.error_table_id_invalid));
                alertForTableId(isNewTable, tableType, tp, proposedDisplayName, knownSafeId);
            } else if (NameUtil.tableIdAlreadyExists(proposedTableId, dbh)) {
                // Then we've got a name conflict.
                Log.d(TAG, "[alertForTableId] name conflict on: " + proposedTableId);
                toastTableNameError(getString(R.string.error_table_id_exists));
                alertForTableId(isNewTable, tableType, tp, proposedDisplayName, knownSafeId);
            } else {
                if (isNewTable)
                    addTable(proposedDisplayName, proposedTableId, tableType);
                else
                    tp.setTableId(proposedTableId);
                refreshList();
            }
        }
    });

    // Cancel Action
    alert.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
        }
    });

    newTableAlert = alert.create();
    newTableAlert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    newTableAlert.show();
}

From source file:org.opendatakit.survey.android.activities.MainMenuActivity.java

protected void createPasswordDialog() {
    if (mAlertDialog != null) {
        mAlertDialog.dismiss();//from www  .j  a  va 2s .  com
        mAlertDialog = null;
    }
    final AlertDialog passwordDialog = new AlertDialog.Builder(this).create();

    passwordDialog.setTitle(getString(R.string.enter_admin_password));
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
    input.setTransformationMethod(PasswordTransformationMethod.getInstance());
    passwordDialog.setView(input, 20, 10, 20, 10);

    passwordDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.ok),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String value = input.getText().toString();
                    String pw = PropertiesSingleton.getProperty(appName, AdminPreferencesActivity.KEY_ADMIN_PW);
                    if (pw != null && pw.compareTo(value) == 0) {
                        Intent i = new Intent(getApplicationContext(), AdminPreferencesActivity.class);
                        // TODO: convert this activity into a preferences fragment
                        i.putExtra(APP_NAME, getAppName());
                        startActivity(i);
                        input.setText("");
                        passwordDialog.dismiss();
                    } else {
                        Toast.makeText(MainMenuActivity.this, getString(R.string.admin_password_incorrect),
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });

    passwordDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.cancel),
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    input.setText("");
                    return;
                }
            });

    passwordDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    mAlertDialog = passwordDialog;
    mAlertDialog.show();
}

From source file:com.mantz_it.rfanalyzer.MainActivity.java

/**
 * Will pop up a dialog to let the user adjust gain settings
 *//*from www. j ava 2s  . c  o  m*/
private void adjustGain() {
    if (source == null)
        return;

    int sourceType = Integer.valueOf(preferences.getString(getString(R.string.pref_sourceType), "1"));
    switch (sourceType) {
    case FILE_SOURCE:
        Toast.makeText(this, getString(R.string.filesource_doesnt_support_gain), Toast.LENGTH_LONG).show();
        break;
    case HACKRF_SOURCE:
        // Prepare layout:
        final LinearLayout view_hackrf = (LinearLayout) this.getLayoutInflater().inflate(R.layout.hackrf_gain,
                null);
        final SeekBar sb_hackrf_vga = (SeekBar) view_hackrf.findViewById(R.id.sb_hackrf_vga_gain);
        final SeekBar sb_hackrf_lna = (SeekBar) view_hackrf.findViewById(R.id.sb_hackrf_lna_gain);
        final TextView tv_hackrf_vga = (TextView) view_hackrf.findViewById(R.id.tv_hackrf_vga_gain);
        final TextView tv_hackrf_lna = (TextView) view_hackrf.findViewById(R.id.tv_hackrf_lna_gain);
        sb_hackrf_vga.setMax(HackrfSource.MAX_VGA_RX_GAIN / HackrfSource.VGA_RX_GAIN_STEP_SIZE);
        sb_hackrf_lna.setMax(HackrfSource.MAX_LNA_GAIN / HackrfSource.LNA_GAIN_STEP_SIZE);
        sb_hackrf_vga.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_hackrf_vga.setText("" + progress * HackrfSource.VGA_RX_GAIN_STEP_SIZE);
                ((HackrfSource) source).setVgaRxGain(progress * HackrfSource.VGA_RX_GAIN_STEP_SIZE);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        sb_hackrf_lna.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_hackrf_lna.setText("" + progress * HackrfSource.LNA_GAIN_STEP_SIZE);
                ((HackrfSource) source).setLnaGain(progress * HackrfSource.LNA_GAIN_STEP_SIZE);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        sb_hackrf_vga.setProgress(((HackrfSource) source).getVgaRxGain() / HackrfSource.VGA_RX_GAIN_STEP_SIZE);
        sb_hackrf_lna.setProgress(((HackrfSource) source).getLnaGain() / HackrfSource.LNA_GAIN_STEP_SIZE);

        // Show dialog:
        AlertDialog hackrfDialog = new AlertDialog.Builder(this).setTitle("Adjust Gain Settings")
                .setView(view_hackrf).setPositiveButton("Set", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // safe preferences:
                        SharedPreferences.Editor edit = preferences.edit();
                        edit.putInt(getString(R.string.pref_hackrf_vgaRxGain),
                                sb_hackrf_vga.getProgress() * HackrfSource.VGA_RX_GAIN_STEP_SIZE);
                        edit.putInt(getString(R.string.pref_hackrf_lnaGain),
                                sb_hackrf_lna.getProgress() * HackrfSource.LNA_GAIN_STEP_SIZE);
                        edit.apply();
                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // do nothing
                    }
                }).create();
        hackrfDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                // sync source with (new/old) settings
                int vgaRxGain = preferences.getInt(getString(R.string.pref_hackrf_vgaRxGain),
                        HackrfSource.MAX_VGA_RX_GAIN / 2);
                int lnaGain = preferences.getInt(getString(R.string.pref_hackrf_lnaGain),
                        HackrfSource.MAX_LNA_GAIN / 2);
                if (((HackrfSource) source).getVgaRxGain() != vgaRxGain)
                    ((HackrfSource) source).setVgaRxGain(vgaRxGain);
                if (((HackrfSource) source).getLnaGain() != lnaGain)
                    ((HackrfSource) source).setLnaGain(lnaGain);
            }
        });
        hackrfDialog.show();
        hackrfDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        break;
    case RTLSDR_SOURCE:
        final int[] possibleGainValues = ((RtlsdrSource) source).getPossibleGainValues();
        final int[] possibleIFGainValues = ((RtlsdrSource) source).getPossibleIFGainValues();
        if (possibleGainValues.length <= 1 && possibleIFGainValues.length <= 1) {
            Toast.makeText(MainActivity.this, source.getName() + " does not support gain adjustment!",
                    Toast.LENGTH_LONG).show();
        }
        // Prepare layout:
        final LinearLayout view_rtlsdr = (LinearLayout) this.getLayoutInflater().inflate(R.layout.rtlsdr_gain,
                null);
        final LinearLayout ll_rtlsdr_gain = (LinearLayout) view_rtlsdr.findViewById(R.id.ll_rtlsdr_gain);
        final LinearLayout ll_rtlsdr_ifgain = (LinearLayout) view_rtlsdr.findViewById(R.id.ll_rtlsdr_ifgain);
        final Switch sw_rtlsdr_manual_gain = (Switch) view_rtlsdr.findViewById(R.id.sw_rtlsdr_manual_gain);
        final CheckBox cb_rtlsdr_agc = (CheckBox) view_rtlsdr.findViewById(R.id.cb_rtlsdr_agc);
        final SeekBar sb_rtlsdr_gain = (SeekBar) view_rtlsdr.findViewById(R.id.sb_rtlsdr_gain);
        final SeekBar sb_rtlsdr_ifGain = (SeekBar) view_rtlsdr.findViewById(R.id.sb_rtlsdr_ifgain);
        final TextView tv_rtlsdr_gain = (TextView) view_rtlsdr.findViewById(R.id.tv_rtlsdr_gain);
        final TextView tv_rtlsdr_ifGain = (TextView) view_rtlsdr.findViewById(R.id.tv_rtlsdr_ifgain);

        // Assign current gain:
        int gainIndex = 0;
        int ifGainIndex = 0;
        for (int i = 0; i < possibleGainValues.length; i++) {
            if (((RtlsdrSource) source).getGain() == possibleGainValues[i]) {
                gainIndex = i;
                break;
            }
        }
        for (int i = 0; i < possibleIFGainValues.length; i++) {
            if (((RtlsdrSource) source).getIFGain() == possibleIFGainValues[i]) {
                ifGainIndex = i;
                break;
            }
        }
        sb_rtlsdr_gain.setMax(possibleGainValues.length - 1);
        sb_rtlsdr_ifGain.setMax(possibleIFGainValues.length - 1);
        sb_rtlsdr_gain.setProgress(gainIndex);
        sb_rtlsdr_ifGain.setProgress(ifGainIndex);
        tv_rtlsdr_gain.setText("" + possibleGainValues[gainIndex]);
        tv_rtlsdr_ifGain.setText("" + possibleIFGainValues[ifGainIndex]);

        // Assign current manual gain and agc setting
        sw_rtlsdr_manual_gain.setChecked(((RtlsdrSource) source).isManualGain());
        cb_rtlsdr_agc.setChecked(((RtlsdrSource) source).isAutomaticGainControl());

        // Add listener to gui elements:
        sw_rtlsdr_manual_gain.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sb_rtlsdr_gain.setEnabled(isChecked);
                tv_rtlsdr_gain.setEnabled(isChecked);
                sb_rtlsdr_ifGain.setEnabled(isChecked);
                tv_rtlsdr_ifGain.setEnabled(isChecked);
                ((RtlsdrSource) source).setManualGain(isChecked);
                if (isChecked) {
                    ((RtlsdrSource) source).setGain(possibleGainValues[sb_rtlsdr_gain.getProgress()]);
                    ((RtlsdrSource) source).setIFGain(possibleIFGainValues[sb_rtlsdr_ifGain.getProgress()]);
                }
            }
        });
        cb_rtlsdr_agc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ((RtlsdrSource) source).setAutomaticGainControl(isChecked);
            }
        });
        sb_rtlsdr_gain.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_rtlsdr_gain.setText("" + possibleGainValues[progress]);
                ((RtlsdrSource) source).setGain(possibleGainValues[progress]);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        sb_rtlsdr_ifGain.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_rtlsdr_ifGain.setText("" + possibleIFGainValues[progress]);
                ((RtlsdrSource) source).setIFGain(possibleIFGainValues[progress]);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        // Disable gui elements if gain cannot be adjusted:
        if (possibleGainValues.length <= 1)
            ll_rtlsdr_gain.setVisibility(View.GONE);
        if (possibleIFGainValues.length <= 1)
            ll_rtlsdr_ifgain.setVisibility(View.GONE);

        if (!sw_rtlsdr_manual_gain.isChecked()) {
            sb_rtlsdr_gain.setEnabled(false);
            tv_rtlsdr_gain.setEnabled(false);
            sb_rtlsdr_ifGain.setEnabled(false);
            tv_rtlsdr_ifGain.setEnabled(false);
        }

        // Show dialog:
        AlertDialog rtlsdrDialog = new AlertDialog.Builder(this).setTitle("Adjust Gain Settings")
                .setView(view_rtlsdr).setPositiveButton("Set", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // safe preferences:
                        SharedPreferences.Editor edit = preferences.edit();
                        edit.putBoolean(getString(R.string.pref_rtlsdr_manual_gain),
                                sw_rtlsdr_manual_gain.isChecked());
                        edit.putBoolean(getString(R.string.pref_rtlsdr_agc), cb_rtlsdr_agc.isChecked());
                        edit.putInt(getString(R.string.pref_rtlsdr_gain),
                                possibleGainValues[sb_rtlsdr_gain.getProgress()]);
                        edit.putInt(getString(R.string.pref_rtlsdr_ifGain),
                                possibleIFGainValues[sb_rtlsdr_ifGain.getProgress()]);
                        edit.apply();
                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // do nothing
                    }
                }).create();
        rtlsdrDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                boolean manualGain = preferences.getBoolean(getString(R.string.pref_rtlsdr_manual_gain), false);
                boolean agc = preferences.getBoolean(getString(R.string.pref_rtlsdr_agc), false);
                int gain = preferences.getInt(getString(R.string.pref_rtlsdr_gain), 0);
                int ifGain = preferences.getInt(getString(R.string.pref_rtlsdr_ifGain), 0);
                ((RtlsdrSource) source).setGain(gain);
                ((RtlsdrSource) source).setIFGain(ifGain);
                ((RtlsdrSource) source).setManualGain(manualGain);
                ((RtlsdrSource) source).setAutomaticGainControl(agc);
                if (manualGain) {
                    // Note: This is a workaround. After setting manual gain to true we must
                    // rewrite the manual gain values:
                    ((RtlsdrSource) source).setGain(gain);
                    ((RtlsdrSource) source).setIFGain(ifGain);
                }
            }
        });
        rtlsdrDialog.show();
        rtlsdrDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        break;
    default:
        Log.e(LOGTAG, "adjustGain: Invalid source type: " + sourceType);
        break;
    }
}

From source file:de.gebatzens.ggvertretungsplan.fragment.RemoteDataFragment.java

public void createRootView(final LayoutInflater inflater, ViewGroup vg) {
    RemoteData data = GGApp.GG_APP.getDataForFragment(type);
    if (data == null) {
        setFragmentLoading();/*w w w  .  j av a 2s.c o  m*/
    } else if (data.getThrowable() != null) {
        Throwable t = data.getThrowable();
        if (t instanceof VPLoginException) {
            createButtonWithText(getActivity(), vg, getResources().getString(R.string.login_required),
                    getResources().getString(R.string.do_login), new View.OnClickListener() {
                        @Override
                        public void onClick(View c) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                            AlertDialog dialog;
                            builder.setTitle(getResources().getString(R.string.login));
                            builder.setView(inflater.inflate(R.layout.login_dialog, null));

                            builder.setPositiveButton(getResources().getString(R.string.do_login_submit),
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(final DialogInterface dialog, int which) {
                                            GGApp.GG_APP.activity.mContent.setFragmentLoading();
                                            new AsyncTask<Integer, Integer, Integer>() {

                                                @Override
                                                public void onPostExecute(Integer v) {
                                                    switch (v) {
                                                    case 1:
                                                        GGApp.GG_APP.showToast(getResources().getString(
                                                                R.string.username_or_password_wrong));
                                                        break;
                                                    case 2:
                                                        GGApp.GG_APP.showToast(getResources().getString(
                                                                R.string.could_not_contact_logon_server));
                                                        break;
                                                    case 3:
                                                        GGApp.GG_APP.showToast(getResources()
                                                                .getString(R.string.unknown_error_at_logon));
                                                        break;
                                                    }

                                                    if (v != 0)
                                                        ((MainActivity) GGApp.GG_APP.activity).mContent
                                                                .updateFragment();

                                                }

                                                @Override
                                                protected Integer doInBackground(Integer... params) {
                                                    String user = ((EditText) ((Dialog) dialog)
                                                            .findViewById(R.id.usernameInput)).getText()
                                                                    .toString();
                                                    String pass = ((EditText) ((Dialog) dialog)
                                                            .findViewById(R.id.passwordInput)).getText()
                                                                    .toString();
                                                    return GGApp.GG_APP.provider.login(user, pass);

                                                }

                                            }.execute();
                                            dialog.dismiss();
                                        }
                                    });

                            builder.setNegativeButton(getResources().getString(R.string.abort),
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    });

                            dialog = builder.create();
                            dialog.getWindow()
                                    .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
                            dialog.show();

                        }
                    });
        } else {
            createButtonWithText(getActivity(), vg,
                    getResources().getString(R.string.check_connection_and_repeat),
                    getResources().getString(R.string.again), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            GGApp.GG_APP.refreshAsync(null, true, GGApp.FragmentType.PLAN);
                        }
                    });
        }
    } else {
        createView(inflater, vg);
    }
}