Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case DIALOG_WIFI_PREF:
- {
- final Command delete = new Command() {
- public void execute() {
- removeDialog(DIALOG_WIFI_PREF);
- showDialog(DIALOG_WIFI_PREF);
- }
- };
- Dialog dialog = new Dialog(this);
- final View dialogLayout = inflater.inflate(R.layout.dialog_wifi_pref, null);
- builder = new AlertDialog.Builder(this);
- builder.setView(dialogLayout);
- builder.setTitle("IP Configuration");
- final EditText ipIn = (EditText)dialogLayout.findViewById(R.id.wifi_ip_in);
- final EditText portIn = (EditText)dialogLayout.findViewById(R.id.wifi_port_in);
- final EditText labelIn = (EditText)dialogLayout.findViewById(R.id.site_label_in);
- final EditText codeIn = (EditText)dialogLayout.findViewById(R.id.activation_code);
- final Spinner siteSpn = (Spinner)dialogLayout.findViewById(R.id.site_spn);
- //final Button deleteBtn = (Button)dialogLayout.findViewById(R.id.delete_btn);
- Cursor tempCur = null;
- try {
- tempCur = mDb.rawQuery("SELECT * FROM " + smsDbSchema.SiteSchema.TABLE_NAME, null);
- } catch(Exception e) {
- Log.e(LOG_TAG, "db query error: "+e.getMessage());
- }
- final Cursor cur = tempCur;
- final SimpleCursorAdapter tempAdapter = new SimpleCursorAdapter(
- this,
- android.R.layout.simple_spinner_item,
- cur,
- new String[] { smsDbSchema.SiteSchema.COLUMN_LABEL },
- new int[] { android.R.id.text1 }
- );
- tempAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- siteSpn.setAdapter(tempAdapter);
- //fill the initial values
- String initSite = pref.getString("site_id", "New Site");
- String spnLabel = null;
- final Cursor initCur = mDb.query(smsDbSchema.SiteSchema.TABLE_NAME, null, smsDbSchema.SiteSchema.COLUMN_LABEL + "=?", new String[] { initSite }, null, null, null);
- initCur.moveToFirst();
- cur.moveToFirst();
- if(initCur.getCount()>0) {
- ipIn.setText(initCur.getString(initCur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_IP)));
- portIn.setText(initCur.getString(initCur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_PORT)));
- codeIn.setText(initCur.getString(initCur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_ACTIVATION_CODE)));
- spnLabel = initCur.getString(initCur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL));
- labelIn.setText(spnLabel, TextView.BufferType.SPANNABLE);
- if(cur.getCount()>0) {
- do {
- if(spnLabel.equals(cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL)))) {
- siteSpn.setSelection(cur.getPosition());
- }
- } while(cur.moveToNext());
- }
- }
- siteSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- cur.moveToFirst();
- if(cur.getCount()>0) {
- do {
- String tempLabel = cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL));
- if(tempLabel.equals(((TextView)view.findViewById(android.R.id.text1)).getText().toString())) {
- labelIn.setText(tempLabel, TextView.BufferType.SPANNABLE);
- labelIn.selectAll();
- portIn.setText(cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_PORT)));
- ipIn.setText(cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_IP)));
- codeIn.setText(cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_ACTIVATION_CODE)));
- }
- } while(cur.moveToNext());
- }
- }
- public void onNothingSelected(AdapterView<?> parent) {
- //
- }
- });
- labelIn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- public void onFocusChange(View v, boolean hasFocus) {
- if(hasFocus) {
- EditText et = (EditText)v;
- String temp = et.getText().toString();
- et.setText(temp, TextView.BufferType.SPANNABLE);
- et.selectAll();
- }
- }
- });
- builder.setNeutralButton("Delete", new CommandWrapper(delete) {
- public void onClick(DialogInterface dialog, int which) {
- //do things
- cur.moveToFirst();
- while(cur.moveToNext()) {
- String tempLabel = cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL));
- View selectedSiteView = siteSpn.getSelectedView();
- String label = ((TextView)selectedSiteView.findViewById(android.R.id.text1)).getText().toString();
- if(tempLabel.equals(label)) {
- mDb.delete(smsDbSchema.SiteSchema.TABLE_NAME, smsDbSchema.SiteSchema.COLUMN_LABEL+"=?", new String[] { label });
- }
- }
- //TODO: complete this code to auto-select the next site after deletion
- //Cursor siteCur = mDb.rawQuery("SELECT * FROM " + smsDbSchema.SiteSchema.TABLE_NAME, null);
- //if(siteCur.getCount()>0) {
- // //place "ip" and "port" of first site record in SharedPreferences
- //}
- //siteCur.close();
- dialog.dismiss();
- this.execute();
- }
- });
- builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- cur.moveToFirst();
- boolean newRecord = true;
- do {
- String tempLabel = null;
- if(cur.getCount()>0) {
- tempLabel = cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL));
- }
- if(tempLabel!=null && tempLabel.equals(labelIn.getText().toString())) {
- //update
- ContentValues cv = new ContentValues();
- cv.put(smsDbSchema.SiteSchema.COLUMN_IP, ipIn.getText().toString());
- cv.put(smsDbSchema.SiteSchema.COLUMN_PORT, portIn.getText().toString());
- cv.put(smsDbSchema.SiteSchema.COLUMN_ACTIVATION_CODE, codeIn.getText().toString());
- smsMobile.this.mDb.update(smsDbSchema.SiteSchema.TABLE_NAME, cv, smsDbSchema.SiteSchema.COLUMN_LABEL+"=?", new String[] { tempLabel });
- newRecord = false;
- break;
- }
- } while(cur.moveToNext());
- if(newRecord) {
- //new entry
- ContentValues cv = new ContentValues();
- cv.put(smsDbSchema.SiteSchema.COLUMN_IP, ipIn.getText().toString());
- cv.put(smsDbSchema.SiteSchema.COLUMN_PORT, portIn.getText().toString());
- cv.put(smsDbSchema.SiteSchema.COLUMN_LABEL, labelIn.getText().toString());
- cv.put(smsDbSchema.SiteSchema.COLUMN_ACTIVATION_CODE, codeIn.getText().toString());
- smsMobile.this.mDb.insert(smsDbSchema.SiteSchema.TABLE_NAME, null, cv);
- }
- SharedPreferences.Editor editor = pref.edit();
- editor.putString("site_id", labelIn.getText().toString());
- editor.putString("activation", codeIn.getText().toString());
- editor.commit();
- //smsActivity.this.writeCSVFile("dashboard_settings.csv");
- dialog.dismiss();
- }
- });
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
- dialog = builder.create();
- //InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
- //mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
- //mgr.toggleSoftInput(0,0);
- return dialog;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement