Android Open Source - android-rackspacecloud List Servers 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. j  a  va 2 s  . c  o m
 */
package com.rackspacecloud.android;

import java.util.ArrayList;

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.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.rackspace.cloud.servers.api.client.CloudServersException;
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 ListServersActivity extends GaListActivity {
  
  private Server[] servers;
  private Context context;
  
  
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        trackPageView(PAGE_SERVERS);
        context = getApplicationContext();
        restoreState(savedInstanceState);
    }
  
  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("servers", servers);
  }

    private void restoreState(Bundle state) {
      if (state != null && state.containsKey("servers") && state.getSerializable("servers") != null) {
        servers = (Server[]) state.getSerializable("servers");
        if (servers.length == 0) {
          displayNoServersCell();
        } else {
          getListView().setDividerHeight(1); // restore divider lines 
          setListAdapter(new ServerAdapter());
        }
      } else {
            loadServers();        
      }
    }
        
    protected void onListItemClick(ListView l, View v, int position, long id) {
      if (servers != null && servers.length > 0) {
        Intent viewIntent = new Intent(this, ViewServerActivity.class);
        viewIntent.putExtra("server", servers[position]);
      startActivityForResult(viewIntent, 55); // arbitrary number; never used again
      }
    }
    
    private void loadServers() {
      displayLoadingCell();
      new LoadServersTask().execute((Void[]) null);
    }
    
    private void setServerList(ArrayList<Server> servers) {
      if (servers == null) {
        servers = new ArrayList<Server>();
      }
      String[] serverNames = new String[servers.size()];
      this.servers = new Server[servers.size()];
      
    if (servers != null) {
      for (int i = 0; i < servers.size(); i++) {
        Server server = servers.get(i);
        this.servers[i] = server;
        serverNames[i] = server.getName();
      }
    }
    
    if (serverNames.length == 0) {
      displayNoServersCell();
    } else {
      getListView().setDividerHeight(1); // restore divider lines 
      setListAdapter(new ServerAdapter());
    }
    }
    
    private void displayLoadingCell() {
      String a[] = new String[1];
      a[0] = "Loading...";
        setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell, R.id.loading_label, a));
        getListView().setTextFilterEnabled(true);
        getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
        getListView().setItemsCanFocus(false);
    }
    
    private void displayNoServersCell() {
      String a[] = new String[1];
      a[0] = "No Servers";
        setListAdapter(new ArrayAdapter<String>(this, R.layout.noserverscell, R.id.no_servers_label, a));
        getListView().setTextFilterEnabled(true);
        getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
        getListView().setItemsCanFocus(false);
    }
    
    private void showAlert(String title, String message) {
      //Can't create handler inside thread that has not called Looper.prepare()
      //Looper.prepare();
      try {
    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();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
            
    private class LoadServersTask extends AsyncTask<Void, Void, ArrayList<Server>> {
      
      private CloudServersException exception;
      
    @Override
    protected ArrayList<Server> doInBackground(Void... arg0) {
      ArrayList<Server> servers = null;
      try {
        servers = (new ServerManager()).createList(true, context);
      } catch (CloudServersException e) {
        exception = e;        
      }
      return servers;
    }
      
    @Override
    protected void onPostExecute(ArrayList<Server> result) {
      if (exception != null) {
        showAlert("Error", exception.getMessage());
      }
      setServerList(result);
    }
    }
    
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.servers_list_menu, menu);
    return true;
  } 
    
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.add_server:
      startActivityForResult(new Intent(this, AddServerActivity.class), 56); // arbitrary number; never used again
      return true;
    case R.id.refresh:
      servers = null;
      loadServers();
          return true;
    }
    return false;
  } 
  //*/
    
  class ServerAdapter extends ArrayAdapter<Server> {
    ServerAdapter() {
      super(ListServersActivity.this, R.layout.listservercell, servers);
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
      
      Server server = servers[position];
      LayoutInflater inflater = getLayoutInflater();
      View row = inflater.inflate(R.layout.listservercell, parent, false);

      TextView label = (TextView) row.findViewById(R.id.label);
      label.setText(server.getName());
      
      TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
      sublabel.setText(server.getFlavor().getName() + " - " + server.getImage().getName());
      
      ImageView icon = (ImageView) row.findViewById(R.id.icon);
      icon.setImageResource(server.getImage().iconResourceId());

      return(row);
    }
  }
    
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    
    if (resultCode == RESULT_OK) {    
      // a sub-activity kicked back, so we want to refresh the server list
      loadServers();
    }
  }  
}




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