Android Open Source - webots-remote-control Add Server Activity






From Project

Back to project page webots-remote-control.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project webots-remote-control listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.black_mesa.webots_remote_control.activities;
/*  w w w .j a  v  a 2 s.  co m*/
import java.util.List;

import org.black_mesa.webots_remote_control.R;
import org.black_mesa.webots_remote_control.database.DataSource;
import org.black_mesa.webots_remote_control.database.Server;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.EditText;

public class AddServerActivity extends Activity {
  private DataSource mDatasource;
  private boolean mIsEdit;

  // Activity lifecycle

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mIsEdit = getIntent().hasExtra("id");
    setContentView(R.layout.activity_add_server);
    getActionBar().setHomeButtonEnabled(false);
    getActionBar().setDisplayHomeAsUpEnabled(false);
    mDatasource = new DataSource(this);
    mDatasource.open();
    if (mIsEdit) {
      Server server = getServerById(getIntent().getExtras().getLong("id"));
      ((EditText) findViewById(R.id.serverName)).setText(server.getName());
      ((EditText) findViewById(R.id.serverAdress)).setText(server.getAdress());
      ((EditText) findViewById(R.id.serverPort)).setText(String.valueOf(server.getPort()));
      setTitle(R.string.title_activity_edit_server);
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.add_server, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.new_server_validate:
      if (isInputValid()) {
        saveServer();
      }
      finish();
      break;
    case R.id.new_server_cancel:
      finish();
      break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onPause() {
    mDatasource.close();
    super.onPause();
  }

  @Override
  protected void onResume() {
    mDatasource.open();
    super.onResume();
  }

  // Private methods

  private boolean isInputValid() {
    String name = ((EditText) findViewById(R.id.serverName)).getText().toString();
    String address = ((EditText) findViewById(R.id.serverAdress)).getText().toString();
    String portText = ((EditText) findViewById(R.id.serverPort)).getText().toString();
    return !(name.matches("") || address.matches("") || portText.matches(""));
  }

  private void saveServer() {
    String name = ((EditText) findViewById(R.id.serverName)).getText().toString();
    String address = ((EditText) findViewById(R.id.serverAdress)).getText().toString();
    String portText = ((EditText) findViewById(R.id.serverPort)).getText().toString();
    if (mIsEdit) {
      Server server = getServerById(getIntent().getExtras().getLong("id"));
      server.setName(name);
      server.setAdress(address);
      server.setPort(Integer.parseInt(portText));
      mDatasource.updateServer(server);
    } else {
      mDatasource.createServer(name, address, Integer.parseInt(portText));
    }
  }

  private Server getServerById(long id) {
    List<Server> servers = mDatasource.getAllServers();
    for (Server s : servers) {
      if (s.getId() == id) {
        return s;
      }
    }
    return null;
  }

}




Java Source Code List

org.black_mesa.webots_remote_control.activities.AboutFragment.java
org.black_mesa.webots_remote_control.activities.AddServerActivity.java
org.black_mesa.webots_remote_control.activities.CameraFragment.java
org.black_mesa.webots_remote_control.activities.ConnectionFragment.java
org.black_mesa.webots_remote_control.activities.MainActivity.java
org.black_mesa.webots_remote_control.client.CamerasManager.java
org.black_mesa.webots_remote_control.client.Client.java
org.black_mesa.webots_remote_control.client.ConnectionManager.java
org.black_mesa.webots_remote_control.client.ConnectionState.java
org.black_mesa.webots_remote_control.client.package-info.java
org.black_mesa.webots_remote_control.communication_structures.CameraInstructionQueue.java
org.black_mesa.webots_remote_control.communication_structures.CameraInstruction.java
org.black_mesa.webots_remote_control.communication_structures.CommunicationStructure.java
org.black_mesa.webots_remote_control.communication_structures.package-info.java
org.black_mesa.webots_remote_control.database.DataBaseContract.java
org.black_mesa.webots_remote_control.database.DataBaseHelper.java
org.black_mesa.webots_remote_control.database.DataSource.java
org.black_mesa.webots_remote_control.database.ServerListAdapter.java
org.black_mesa.webots_remote_control.database.Server.java
org.black_mesa.webots_remote_control.listeners.CameraJoysticksViewListener.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV1.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV2.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV3.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV4.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV5.java
org.black_mesa.webots_remote_control.listeners.ClientListener.java
org.black_mesa.webots_remote_control.listeners.ConnectionManagerListener.java
org.black_mesa.webots_remote_control.listeners.OnListEventsListener.java
org.black_mesa.webots_remote_control.listeners.package-info.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV1.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV2.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV3.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV4.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV5.java
org.black_mesa.webots_remote_control.utils.package-info.java
org.black_mesa.webots_remote_control.views.CameraJoysticksView.java
org.black_mesa.webots_remote_control.views.CameraView.java