Back to project page Android-Apps.
The source code is released under:
Apache License
If you think the Android project Android-Apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.kniezrec.xbmcgear.presentation; // w w w. jav a2s .c o m import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.EditText; import com.kniezrec.xbmcgear.R; import com.kniezrec.xbmcgear.connection.Connection; import com.kniezrec.xbmcgear.preferences.Host; import com.kniezrec.xbmcgear.preferences.HostsDataSource; import com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil; public class HostSetActivity extends ActionBarActivity { public final static String INSTANCE_NAME = "instance_name"; private static Host sHost; private static ViewMode sMode; private HostsDataSource mDataSource; private EditText profileName; private EditText ip; private EditText port; private EditText password; private EditText userName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_host_set); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); if (toolbar != null) { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } mDataSource = HostsDataSource.getInstance(); sHost = (Host) getIntent().getSerializableExtra(INSTANCE_NAME); boolean isEditFound = getIntent().getBooleanExtra( AutoConfigurationActivity.TO_EDIT, false); boolean isManual = getIntent().getBooleanExtra( AutoConfigurationActivity.ADD_MANUAL, false); if (sHost == null) { sHost = new Host(); sMode = (isManual) ? ViewMode.MAN_NEW : ViewMode.NEW; } else if (isEditFound) { sMode = ViewMode.EDIT_FOUND_HOST; } else { sMode = ViewMode.EDIT; } profileName = (EditText) findViewById(R.id.edittext_profileName); ip = (EditText) findViewById(R.id.edittext_ip); port = (EditText) findViewById(R.id.edittext_port); password = (EditText) findViewById(R.id.edittext_password); userName = (EditText) findViewById(R.id.edittext_username); if (sMode == ViewMode.EDIT || sMode == ViewMode.EDIT_FOUND_HOST) { profileName.setText(sHost.getProfileName()); ip.setText(sHost.getIp()); port.setText(Integer.toString(sHost.getPort())); password.setText(sHost.getPassword()); userName.setText(sHost.getUsername()); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.host_set, menu); return true; } @Override protected void onStop() { super.onStop(); sHost = null; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { super.onBackPressed(); this.overridePendingTransition(R.anim.slideback, R.anim.soutback); return true; } return super.onKeyDown(keyCode, event); } void fillUpHost() { sHost.setProfileName(profileName.getText().toString()); sHost.setIp(ip.getText().toString()); String _port = port.getText().toString(); if (!_port.isEmpty()) { sHost.setPort(Integer.valueOf(_port)); } else { sHost.setPort(-1); } sHost.setPassword(password.getText().toString()); sHost.setUsername(userName.getText().toString()); } void shakeEmptyRequired(Host h) { Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); if (h.getProfileName().isEmpty()) { profileName.startAnimation(shake); } if (h.getIp().isEmpty()) { ip.startAnimation(shake); } if (h.getPort() < 0) { port.startAnimation(shake); } } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.save) { fillUpHost(); if (checkNullValues()) { switch (sMode) { case NEW: mDataSource.addHost(sHost); break; case EDIT: mDataSource.updateHost(sHost); Connection.getInstance().setHost(sHost); break; case EDIT_FOUND_HOST: Intent resultIntent = new Intent(); resultIntent.putExtra(INSTANCE_NAME, sHost); setResult(RESULT_OK, resultIntent); finish(); break; case MAN_NEW: mDataSource.addHost(sHost); SharedPreferencesUtil.changePreferences(sHost); Connection.getInstance().setHost(sHost); break; default: break; } super.onBackPressed(); this.overridePendingTransition(R.anim.slideback, R.anim.soutback); } else { shakeEmptyRequired(sHost); showDialogBox(); } return true; } else { super.onBackPressed(); this.overridePendingTransition(R.anim.slideback, R.anim.soutback); } return super.onOptionsItemSelected(item); } private boolean checkNullValues() { return !(sHost.getProfileName().isEmpty() || sHost.getIp().isEmpty() || sHost.getPort() < 0); } private void showDialogBox() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.setMessage(getResources().getString(R.string.empty_name)); dialog.setTitle(getResources().getString(R.string.empty_title)); dialog.show(); } }