Android Open Source - android-rackspacecloud Add Container 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

package com.rackspacecloud.android;
/* w  w w  . j  ava 2  s  .com*/
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.impl.client.BasicResponseHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.rackspace.cloud.files.api.client.ContainerManager;
import com.rackspace.cloud.servers.api.client.CloudServersException;
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
/** 
 * 
 * @author Phillip Toohill
 *
 */
public class AddContainerActivity extends GaActivity implements  OnClickListener {

  private EditText containerName;
  private Context context;  
  private boolean isSaving;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    trackPageView(PAGE_ADD_CONTAINER);
    context = getApplicationContext();
    setContentView(R.layout.createcontainer);
    containerName = (EditText) findViewById(R.id.container_name);
    ((Button) findViewById(R.id.save_button)).setOnClickListener(this);
    isSaving = savedInstanceState != null && savedInstanceState.containsKey("isSaving")
      && savedInstanceState.getBoolean("isSaving");
    if(isSaving){
      showActivityIndicators();
    }
  }
  
  @Override
  protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putBoolean("isSaving", isSaving);
    
  }
  
  public void onClick(View arg0) {
    if ("".equals(containerName.getText().toString())) {
      showAlert("Required Fields Missing", " Container name is required.");
    } else {
      showActivityIndicators();
      trackEvent(CATEGORY_CONTAINER, EVENT_CREATE, "", -1);
      new CreateContainerTask().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 showToast(String message) {
    Context context = getApplicationContext();
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, message, duration);
    toast.show();
  }

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

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

  private void hideActivityIndicators() {
    isSaving = false;
    setActivityIndicatorsVisibility(View.INVISIBLE);
  }
  //using cloudServersException, it works for us too
  private CloudServersException parseCloudServersException(HttpResponse response) {
    CloudServersException cse = new CloudServersException();
    try {
      BasicResponseHandler responseHandler = new BasicResponseHandler();
      String body = responseHandler.handleResponse(response);
      CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
      SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
      XMLReader xmlReader = saxParser.getXMLReader();
      xmlReader.setContentHandler(parser);
      xmlReader.parse(new InputSource(new StringReader(body)));          
      cse = parser.getException();          
    } catch (ClientProtocolException e) {
      cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
    } catch (IOException e) {
      cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
    } catch (ParserConfigurationException e) {
      cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
    } catch (SAXException e) {
      cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
    } catch (FactoryConfigurationError e) {
      cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
    }
    return cse;
  }

  private void startFileError(String message, HttpBundle bundle){
    Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
    viewIntent.putExtra("errorMessage", message);
    viewIntent.putExtra("response", bundle.getResponseText());
    viewIntent.putExtra("request", bundle.getCurlRequest());
    startActivity(viewIntent);
  }

  private class CreateContainerTask extends AsyncTask<Void, Void, HttpBundle> {
    private CloudServersException exception;

    @Override
    protected HttpBundle doInBackground(Void... arg0) {
      HttpBundle bundle = null;
      try {
        bundle = (new ContainerManager(context)).create(containerName.getText());
      } catch (CloudServersException e) {
        exception = e;
      }
      return bundle;
    }

    @Override
    protected void onPostExecute(HttpBundle bundle) {
      HttpResponse response = bundle.getResponse();
      if (response != null) {
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 201) {
          setResult(Activity.RESULT_OK);
          finish();
        } else {
          CloudServersException cse = parseCloudServersException(response);
          if ("".equals(cse.getMessage())) {
            startFileError("There was a problem creating your container.", bundle);
          } else {
            //if container with same name already exists
            showToast("There was a problem creating your container: " + cse.getMessage() + " Check container name and try again");
          }
        }
      } else if (exception != null) {
        startFileError("There was a problem creating your container: " + exception.getMessage()+" Check container name and try again", bundle);        
      }
      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