Android Open Source - android-rackspacecloud Rackspace Cloud 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 a  v a  2s.  co m*/
import java.util.ArrayList;
import java.util.TreeMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.rackspace.cloud.servers.api.client.Account;
import com.rackspace.cloud.servers.api.client.Flavor;
import com.rackspace.cloud.servers.api.client.FlavorManager;
import com.rackspace.cloud.servers.api.client.Image;
import com.rackspace.cloud.servers.api.client.ImageManager;
import com.rackspace.cloud.servers.api.client.http.Authentication;

public class RackspaceCloudActivity extends Activity implements View.OnClickListener, OnEditorActionListener {
  
  private static final String OPT_USERNAME = "username";
  private static final String OPT_USERNAME_DEF = "";
  private static final String OPT_API_KEY = "apiKey";
  private static final String OPT_API_KEY_DEF = "";
  
  private Intent tabViewIntent;
  private boolean authenticating;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ((Button) findViewById(R.id.button)).setOnClickListener(this);
        ((EditText) findViewById(R.id.login_apikey)).setOnEditorActionListener(this);
        loadLoginPreferences();
        restoreState(savedInstanceState);
        
        // use the TabViewActivity when Cloud Files is added
        // tabViewIntent = new Intent(this, TabViewActivity.class);
        
        tabViewIntent = new Intent(this, TabViewActivity.class);
    }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("authenticating", authenticating);
  }

    private void restoreState(Bundle state) {
      if (state != null && state.containsKey("authenticating") && state.getBoolean("authenticating")) {
        showActivityIndicators();
      } else {
        hideActivityIndicators();
      }
    }
    
    public void login() {
      if (hasValidInput()) {
          showActivityIndicators();
          setLoginPreferences();
          new AuthenticateTask().execute((Void[]) null);
      } else {
        showAlert("Fields Missing", "User Name and API Key are required.");
      }
    }
    
    public void onClick(View view) {
      login();
    }
    
  public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
    login();
    return false;
  }    

  private void loadLoginPreferences() {
      SharedPreferences sp = this.getPreferences(Context.MODE_PRIVATE);
      String username = sp.getString(OPT_USERNAME, OPT_USERNAME_DEF);      
      String apiKey = sp.getString(OPT_API_KEY, OPT_API_KEY_DEF);
      EditText usernameText = (EditText) findViewById(R.id.login_username);
      usernameText.setText(username);
      EditText apiKeyText = (EditText) findViewById(R.id.login_apikey);
      apiKeyText.setText(apiKey);
    }
    
    private void setLoginPreferences() {
      String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
      String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
      Account.setUsername(username);
      Account.setApiKey(apiKey);

      Editor e = this.getPreferences(Context.MODE_PRIVATE).edit();
      e.putString(OPT_USERNAME, username);
      e.putString(OPT_API_KEY, apiKey);
      e.commit();          
    }
    
    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 boolean hasValidInput() {
      String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
      String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
      return !"".equals(username) && !"".equals(apiKey);
    }

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

    private void showActivityIndicators() {
      setActivityIndicatorsVisibility(View.VISIBLE);
    }
    
    private void hideActivityIndicators() {
      setActivityIndicatorsVisibility(View.INVISIBLE);
    }
    
    private class AuthenticateTask extends AsyncTask<Void, Void, Boolean> {
      
    @Override
    protected Boolean doInBackground(Void... arg0) {
      authenticating = true;
      return new Boolean(Authentication.authenticate());
    }
      
    @Override
    protected void onPostExecute(Boolean result) {
      authenticating = false;
      if (result.booleanValue()) {
        //startActivity(tabViewIntent);
            new LoadImagesTask().execute((Void[]) null);        
      } else {
        showAlert("Login Failure", "Authentication failed.  Please check your User Name and API Key.");
      }
    }
    }

    private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> {
      
    @Override
    protected ArrayList<Flavor> doInBackground(Void... arg0) {
      return (new FlavorManager()).createList(true);
    }
      
    @Override
    protected void onPostExecute(ArrayList<Flavor> result) {
      if (result != null && result.size() > 0) {
        TreeMap<String, Flavor> flavorMap = new TreeMap<String, Flavor>();
        for (int i = 0; i < result.size(); i++) {
          Flavor flavor = result.get(i);
          flavorMap.put(flavor.getId(), flavor);
        }
        Flavor.setFlavors(flavorMap);
        startActivity(tabViewIntent);
      } else {
        showAlert("Login Failure", "There was a problem loading server flavors.  Please try again.");
      }
      hideActivityIndicators();
    }
    }

    private class LoadImagesTask extends AsyncTask<Void, Void, ArrayList<Image>> {
      
    @Override
    protected ArrayList<Image> doInBackground(Void... arg0) {
      return (new ImageManager()).createList(true);
    }
      
    @Override
    protected void onPostExecute(ArrayList<Image> result) {
      if (result != null && result.size() > 0) {
        TreeMap<String, Image> imageMap = new TreeMap<String, Image>();
        for (int i = 0; i < result.size(); i++) {
          Image image = result.get(i);
          imageMap.put(image.getId(), image);
        }
        Image.setImages(imageMap);
        new LoadFlavorsTask().execute((Void[]) null);
        //startActivity(tabViewIntent);
      } else {
        showAlert("Login Failure", "There was a problem loading server images.  Please try again.");
      }
      //hideActivityIndicators();
    }
    }

}




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.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.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.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.AddContainerActivity.java
com.rackspacecloud.android.AddServerActivity.java
com.rackspacecloud.android.ContainerObjectDetails.java
com.rackspacecloud.android.ContainerObjectsActivity.java
com.rackspacecloud.android.EnableCDNActivity.java
com.rackspacecloud.android.ListContainerActivity.java
com.rackspacecloud.android.ListServersActivity.java
com.rackspacecloud.android.RackspaceCloudActivity.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