Android Open Source - android-rackspacecloud Add Server Activity






From Project

Back to project page android-rackspacecloud.

License

The source code is released under:

MIT License

If you think the Android project android-rackspacecloud 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

/**
 * //from   w w w  .ja v a2s  . c  o m
 */
package com.rackspacecloud.android;

import java.util.Arrays;
import java.util.Iterator;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.rackspace.cloud.servers.api.client.CloudServersException;
import com.rackspace.cloud.servers.api.client.Flavor;
import com.rackspace.cloud.servers.api.client.Image;
import com.rackspace.cloud.servers.api.client.Server;
import com.rackspace.cloud.servers.api.client.ServerManager;

/**
 * @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
 *
 */
public class AddServerActivity extends GaActivity implements OnItemSelectedListener, OnClickListener {

  private Image[] images;
  private Flavor[] flavors;
  private String selectedImageId;
  private String selectedFlavorId;
  private EditText serverName;
  private Spinner imageSpinner;
  private Spinner flavorSpinner;
  private Server server;
  private SeekBar numberBar;
  private TextView numberDisplay;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    trackPageView(PAGE_ADD_SERVER);  
    setContentView(R.layout.createserver);
    serverName = (EditText) findViewById(R.id.server_name);
    ((Button) findViewById(R.id.save_button)).setOnClickListener(this);
    loadImageSpinner();
    loadFlavorSpinner();
    loadServerCount();
  }

  private void loadServerCount(){
    numberDisplay = (TextView)findViewById(R.id.server_count_text);
    numberBar = (SeekBar)findViewById(R.id.number_of_servers);
    numberBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

      }

      @Override
      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {
        // TODO Auto-generated method stub
        numberDisplay.setText(getCountText(progress));
        TextView number = (TextView)findViewById(R.id.server_name_number);
        if(progress == 0){
          number.setText("");
        }
        else{
          number.setText("[1.." + (progress+1) + "]");
        }
      }

      private String getCountText(int i){
        if(i == 0){
          return "1 Server";
        }
        else{
          //count starts at 0
          return i+1 + " Servers";
        }
      }
    });

  }

  private void loadImageSpinner() {
    imageSpinner = (Spinner) findViewById(R.id.image_spinner);
    imageSpinner.setOnItemSelectedListener(this);
    String imageNames[] = new String[Image.getImages().size()]; 
    images = new Image[Image.getImages().size()];

    Iterator<Image> iter = Image.getImages().values().iterator();
    int i = 0;
    while (iter.hasNext()) {
      Image image = iter.next();
      images[i] = image;
      imageNames[i] = image.getName();
      i++;
    }

    //Sort so they display better in the spinner
    Arrays.sort(images);
    Arrays.sort(imageNames);

    selectedImageId = images[0].getId();
    ArrayAdapter<String> imageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, imageNames);
    imageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    imageSpinner.setAdapter(imageAdapter);
  }

  private void loadFlavorSpinner() {
    flavorSpinner = (Spinner) findViewById(R.id.flavor_spinner);
    flavorSpinner.setOnItemSelectedListener(this);
    String flavorNames[] = new String[Flavor.getFlavors().size()]; 
    flavors = new Flavor[Flavor.getFlavors().size()];

    Iterator<Flavor> iter = Flavor.getFlavors().values().iterator();
    int i = 0;
    while (iter.hasNext()) {
      Flavor flavor = iter.next();
      flavors[i] = flavor;
      flavorNames[i] = flavor.getName() + ", " + flavor.getDisk() + " GB disk";
      i++;
    }
    selectedFlavorId = flavors[0].getId();
    ArrayAdapter<String> flavorAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, flavorNames);
    flavorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    flavorSpinner.setAdapter(flavorAdapter);
  }

  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (parent == imageSpinner) {
      selectedImageId = images[position].getId();
    } else if (parent == flavorSpinner) {
      selectedFlavorId = flavors[position].getId();
    }
  }

  public void onNothingSelected(AdapterView<?> parent) {
  }

  public void onClick(View arg0) {
    if ("".equals(serverName.getText().toString())) {
      showAlert("Required Fields Missing", "Server name is required.");
    } else {
      trackEvent(CATEGORY_SERVER, EVENT_CREATE, "", numberBar.getProgress()+1);
      showActivityIndicators();
      server = new Server();
      server.setName(serverName.getText().toString()); 
      server.setImageId(selectedImageId);
      server.setFlavorId(selectedFlavorId);
      new SaveServerTask().execute((Void[]) null);
    }
  }

  private void showAlert(String title, String message) {
    AlertDialog alert = new AlertDialog.Builder(this).create();
    alert.setTitle(title);
    alert.setMessage(message);
    alert.setButton("OK", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        return;
      } }); 
    alert.show();
    hideActivityIndicators();
  }

  private void setActivityIndicatorsVisibility(int visibility) {
    ProgressBar pb = (ProgressBar) findViewById(R.id.save_server_progress_bar);
    TextView tv = (TextView) findViewById(R.id.saving_server_label);
    pb.setVisibility(visibility);
    tv.setVisibility(visibility);
  }

  private void showActivityIndicators() {
    setActivityIndicatorsVisibility(View.VISIBLE);
  }

  private void hideActivityIndicators() {
    setActivityIndicatorsVisibility(View.INVISIBLE);
  }

  private class SaveServerTask extends AsyncTask<Void, Void, Server> {

    private CloudServersException exception;

    @Override
    protected Server doInBackground(Void... arg0) {
      try {
        if(numberBar.getProgress() == 0){
          (new ServerManager()).create(server, getApplicationContext());
        }
        else{
          for(int i = 0; i < numberBar.getProgress() + 1; i++){
            server.setName(serverName.getText().toString() + Integer.toString(i+1));
            (new ServerManager()).create(server, getApplicationContext());
          }
        }
      } catch (CloudServersException e) {
        exception = e;
      }
      return server;
    }

    @Override
    protected void onPostExecute(Server result) {
      if (exception != null) {
        showAlert("Error", "There was a problem creating your server: " + exception.getMessage());
      } else {
        hideActivityIndicators();
        setResult(Activity.RESULT_OK);
        finish();
      }
    }
  }

}




Java Source Code List

com.rackspace.cloud.files.api.client.ContainerManager.java
com.rackspace.cloud.files.api.client.ContainerObjectManager.java
com.rackspace.cloud.files.api.client.ContainerObjects.java
com.rackspace.cloud.files.api.client.Container.java
com.rackspace.cloud.files.api.client.CustomHttpClient.java
com.rackspace.cloud.files.api.client.parsers.ContainerObjectXMLparser.java
com.rackspace.cloud.files.api.client.parsers.ContainerXMLParser.java
com.rackspace.cloud.servers.api.client.Account.java
com.rackspace.cloud.servers.api.client.Backup.java
com.rackspace.cloud.servers.api.client.CloudServersException.java
com.rackspace.cloud.servers.api.client.EntityManager.java
com.rackspace.cloud.servers.api.client.Entity.java
com.rackspace.cloud.servers.api.client.FlavorManager.java
com.rackspace.cloud.servers.api.client.Flavor.java
com.rackspace.cloud.servers.api.client.ImageManager.java
com.rackspace.cloud.servers.api.client.Image.java
com.rackspace.cloud.servers.api.client.ServerManager.java
com.rackspace.cloud.servers.api.client.Server.java
com.rackspace.cloud.servers.api.client.http.Authentication.java
com.rackspace.cloud.servers.api.client.http.HttpBundle.java
com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser.java
com.rackspace.cloud.servers.api.client.parsers.FlavorsXMLParser.java
com.rackspace.cloud.servers.api.client.parsers.ImagesXMLParser.java
com.rackspace.cloud.servers.api.client.parsers.ServersXMLParser.java
com.rackspacecloud.android.AddAccountActivity.java
com.rackspacecloud.android.AddContainerActivity.java
com.rackspacecloud.android.AddFileActivity.java
com.rackspacecloud.android.AddServerActivity.java
com.rackspacecloud.android.AndroidCloudApplication.java
com.rackspacecloud.android.BackupServerActivity.java
com.rackspacecloud.android.Config.java
com.rackspacecloud.android.ConfirmResizeActivity.java
com.rackspacecloud.android.ContactActivity.java
com.rackspacecloud.android.ContainerObjectDetails.java
com.rackspacecloud.android.ContainerObjectsActivity.java
com.rackspacecloud.android.CreatePasswordActivity.java
com.rackspacecloud.android.EnableCDNActivity.java
com.rackspacecloud.android.ErrorDetailsActivity.java
com.rackspacecloud.android.GaActivity.java
com.rackspacecloud.android.GaListActivity.java
com.rackspacecloud.android.ListAccountsActivity.java
com.rackspacecloud.android.ListContainerActivity.java
com.rackspacecloud.android.ListServersActivity.java
com.rackspacecloud.android.PasswordManager.java
com.rackspacecloud.android.PasswordServerActivity.java
com.rackspacecloud.android.PingServerActivity.java
com.rackspacecloud.android.Preferences.java
com.rackspacecloud.android.ServerErrorActivity.java
com.rackspacecloud.android.TabViewActivity.java
com.rackspacecloud.android.ViewServerActivity.java
net.elasticgrid.rackspace.cloudservers.Addresses.java
net.elasticgrid.rackspace.cloudservers.BackupSchedule.java
net.elasticgrid.rackspace.cloudservers.CloudServersException.java
net.elasticgrid.rackspace.cloudservers.CloudServers.java
net.elasticgrid.rackspace.cloudservers.File.java
net.elasticgrid.rackspace.cloudservers.Flavor.java
net.elasticgrid.rackspace.cloudservers.HTTPVerb.java
net.elasticgrid.rackspace.cloudservers.Image.java
net.elasticgrid.rackspace.cloudservers.Limits.java
net.elasticgrid.rackspace.cloudservers.Personality.java
net.elasticgrid.rackspace.cloudservers.RebootType.java
net.elasticgrid.rackspace.cloudservers.Server.java
net.elasticgrid.rackspace.cloudservers.SharedIPGroup.java
net.elasticgrid.rackspace.cloudservers.XMLCloudServers.java
net.elasticgrid.rackspace.cloudservers.package-info.java
net.elasticgrid.rackspace.common.RackspaceConnection.java
net.elasticgrid.rackspace.common.RackspaceException.java
net.elasticgrid.rackspace.common.package-info.java
org.jclouds.date.joda.JodaDateService.java
org.jclouds.date.joda.config.JodaDateServiceModule.java
org.jclouds.encryption.bouncycastle.BouncyCastleEncryptionService.java
org.jclouds.encryption.bouncycastle.config.BouncyCastleEncryptionServiceModule.java
org.jclouds.enterprise.config.EnterpriseConfigurationModule.java
org.jclouds.gae.GaeHttpCommandExecutorService.java
org.jclouds.gae.config.GoogleAppEngineConfigurationModule.java
org.jclouds.http.apachehc.ApacheHCHttpCommandExecutorService.java
org.jclouds.http.apachehc.ApacheHCUtils.java
org.jclouds.http.apachehc.config.ApacheHCHttpCommandExecutorServiceModule.java
org.jclouds.http.httpnio.config.NioTransformingHttpCommandExecutorServiceModule.java
org.jclouds.http.httpnio.pool.NioHttpCommandConnectionHandle.java
org.jclouds.http.httpnio.pool.NioHttpCommandConnectionPool.java
org.jclouds.http.httpnio.pool.NioHttpCommandExecutionHandler.java
org.jclouds.http.httpnio.pool.NioTransformingHttpCommandExecutorService.java
org.jclouds.http.httpnio.util.NioHttpUtils.java
org.jclouds.http.pool.ConnectionPoolTransformingHttpCommandExecutorService.java
org.jclouds.http.pool.HttpCommandConnectionHandle.java
org.jclouds.http.pool.HttpCommandConnectionPool.java
org.jclouds.http.pool.config.ConnectionPoolCommandExecutorServiceModule.java
org.jclouds.logging.log4j.Log4JLogger.java
org.jclouds.logging.log4j.config.Log4JLoggingModule.java
org.jclouds.ssh.jsch.JschSshClient.java
org.jclouds.ssh.jsch.config.JschSshClientModule.java