Android Open Source - One-Button-App---Android Test O B A






From Project

Back to project page One-Button-App---Android.

License

The source code is released under:

Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (...

If you think the Android project One-Button-App---Android 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 edu.cc.oba;
//from www. ja va  2  s  .c  om
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

//import static org.developerworks.android.BaseFeedParser.*;
import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Xml;
import android.widget.Toast;

import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
import org.xmlrpc.android.*;


public class TestOBA extends Activity {
  public static TestOBA oba;
  private String username;
  
  private String password;

  public ArrayList<Integer> activeRequests;
  public static HashMap obaImagesHash;

  public XMLRPCClient client;
  //here

  public TestOBA(final String username, final String password) {
    this.username = username;
    this.password = password;
    this.activeRequests = new ArrayList<Integer>();
    
//    this.context=getBaseContext();
        
    this.client = new XMLRPCClient("https://vcl.ncsu.edu/scheduling/index.php?mode=xmlrpccall",username, password);
      }


//    //here
  
  
  private Object xmlRPCcall(String op_name, Object[] params) {
    Object result = null;

    try {
      result = client.callEx(op_name, params);

      if (result instanceof HashMap) {
        // do nothing
        return (HashMap) result;
      } else {
        Object[] result_array = (Object[]) result;
//        for (int i = 0; i < result_array.length; i++)
          //System.out.println(result_array[i]);
        return result_array;
      }

    } catch (XMLRPCException e) {
//      Log.d("REASON",e.getCause().toString());
      e.printStackTrace();
      Log.i("ERROR", e.getMessage());
    Toast.makeText(getBaseContext(), "Login Failed", 4).show();
    finish();
      
      
    }

    return result;
  }

   private HashMap parseResult(Object[] result) {
   HashMap<String, Object> resultHash = new HashMap<String, Object>();
  
   for(Object each_obj : result) {
   Object[] each_obj_array = (Object[])each_obj;
   resultHash.put((String)each_obj_array[0], each_obj_array[1]);
   }
  
   return resultHash;
   }

  public Object[] getImagesID() {
    String[] params = null;
    return (Object[]) xmlRPCcall("XMLRPCgetImages", params);
    
  }
  
  
  
  
  
  
  public class makeReservation extends AsyncTask<Integer, Void, Boolean>{
    
    @Override
    protected Boolean doInBackground(Integer... image_id_arr) {
      
    String[] params = new String[3];
//    int image_id = 2422; // VCL2.2.1 SandBox image id 
    int image_id=image_id_arr[0];

    params[0] = Integer.toString(image_id);
    params[1] = "now";
    params[2] = Integer.toString(60); // Resever for one hour

    
    //here
    HashMap result = (HashMap) xmlRPCcall("XMLRPCaddRequest", params);

    boolean success_in_resv = false;
    
    Log.d("Hash Table",result.toString());
    
    /*if (result.get("status").equals("success")) {
      int request_id = Integer.parseInt((String) result.get("requestid"));
      
      
      
      TestOBA.oba.activeRequests.add(request_id);
      System.out
          .println("Succeed in making the reservation, request id is: "
              + request_id);

      // Check whether the reservation is ready
      while (true) {
        HashMap status = getRequestStatus(request_id);

        if (status.get("status").equals("ready")) {
          System.out.println("The reservation is ready!");
          Log.d("Active Requests" , oba.activeRequests.toString());
          success_in_resv = true;
          break;
        } else if (status.get("status").equals("loading")) {
          
          int remain_time = (Integer) status.get("time");
          try {
            System.out
                .println("The reservation is still loading with "
                    + remain_time + " minutes remained...");
            // Wait for half of the remaining time and then check again. 
            if (remain_time <= 1) {
              TimeUnit.SECONDS.sleep(30);
            } else {              
              TimeUnit.MINUTES.sleep((int)(remain_time / 2));
            }
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        } else {
          System.err.println("Fail to make a reservation.");
          break;
        }
      }
    } else {
      System.err.println("Fail to make a reservation.");
    }
*/
    
    return success_in_resv;
    }
    
    protected void onPostExecute(Long result) {
           Log.d("Reservation", "Executed");
       }

    
  }  
  
  
  
  public HashMap getRequestStatus(int request_id) {
    Object[] params = new Object[1];
    params[0] = request_id;
    return (HashMap) xmlRPCcall("XMLRPCgetRequestStatus", params);
  }
  
  public HashMap getRequestIDs() {
    Object[] params = new Object[1];
    params[0] = "";
    return (HashMap) xmlRPCcall("XMLRPCgetRequestIds", params);
  }
  
  

  public Boolean cancelReservation() {
    if (!this.activeRequests.isEmpty()) {
      int request_id = this.activeRequests.get(0);
      Object[] params = new Object[1];
      params[0] = request_id;
      HashMap result = (HashMap) xmlRPCcall("XMLRPCendRequest", params);

      if (result.get("status").equals("success")) {
        System.out
            .println("End reservation, request id: " + request_id);
        this.activeRequests.remove(0);
        return true;
      } else {
        System.err.println("Fail to end reservation, request id: "
            + request_id);
        return false;
      }
    }
    return false;
  }
  
  public Boolean cancelReservation(int request_id) {


      Object[] params = new Object[1];
      params[0] = request_id;
      HashMap result = (HashMap) xmlRPCcall("XMLRPCendRequest", params);

      if (result.get("status").equals("success")) {
        System.out
            .println("End reservation, request id: " + request_id);
        this.activeRequests.remove(0);
        return true;
      } else {
        System.err.println("Fail to end reservation, request id: "
            + request_id);
        return false;
      }
  }
  
  

  public String[] getConnectData() {
    InetAddress addr;
    String ipAddr;

    try {
      addr = InetAddress.getLocalHost();
      // Get IP Address
      ipAddr = addr.getHostAddress();
    } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return null;
    }

    Object[] params = { this.activeRequests.get(0), ipAddr };
    // Object[] params = { 1744326, ipAddr };

    HashMap result = (HashMap) xmlRPCcall("XMLRPCgetRequestConnectData",
        params);

    String[] conn_data = null;
    if (result.get("status").equals("ready")) {
      conn_data = new String[3];
      conn_data[0] = (String) result.get("serverIP");
      conn_data[1] = (String) result.get("user");
      conn_data[2] = (String) result.get("password");

      System.out.println("Reserved IP: " + conn_data[0]);
      Log.d("Reserved IP: ", conn_data[0] );
      System.out.println("Username: " + conn_data[1]);
      Log.d("Username: ", conn_data[1] );
      if (conn_data[2].equals("")) {
        conn_data[2] = this.password;//Security Issue
        System.out.println("Password: (use your campus password)");
      } else {
        System.out.println("Password: " + conn_data[2]);
        Log.d("Password: ", conn_data[2] );
      }
    } else if (result.get("status").equals("notready")) {
      System.err.println("The reservation is not ready.");
    } else {
      System.err.println("Fail to get the connect data.");
    }

    return conn_data;
  }

  //Overridden GetConnectData ( Possible delete of previous)
  public String[] getConnectData(int req_id) {
    InetAddress addr;
    String ipAddr;

    try {
      addr = InetAddress.getLocalHost();
      // Get IP Address
      ipAddr = addr.getHostAddress();
    } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return null;
    }

    Object[] params = { req_id, ipAddr };
    // Object[] params = { 1744326, ipAddr };

    HashMap result = (HashMap) xmlRPCcall("XMLRPCgetRequestConnectData",
        params);

    String[] conn_data = null;
    if (result.get("status").equals("ready")) {
      conn_data = new String[3];
      conn_data[0] = (String) result.get("serverIP");
      conn_data[1] = (String) result.get("user");
      conn_data[2] = (String) result.get("password");

      System.out.println("Reserved IP: " + conn_data[0]);
      Log.d("Reserved IP: ", conn_data[0] );
      System.out.println("Username: " + conn_data[1]);
      Log.d("Username: ", conn_data[1] );
      if (conn_data[2].equals("")) {
        conn_data[2] = this.password;//Security Issue
        System.out.println("Password: (use your campus password)");
      } else {
        System.out.println("Password: " + conn_data[2]);
        Log.d("Password: ", conn_data[2] );
      }
    } else if (result.get("status").equals("notready")) {
      System.err.println("The reservation is not ready.");
    } else {
      System.err.println("Fail to get the connect data.");
    }

    return conn_data;
  }

  
  private void termLaunch(String[] conn_data) {
    String term = "/usr/bin/gnome-terminal";
    // In order to automatic use ssh to login, we need "sshpass" to provide
    // the password to the shell
    String ssh_command = "sshpass -p " + conn_data[2]
        + " ssh -o StrictHostKeyChecking=no " + conn_data[1] + "@"
        + conn_data[0];
    System.out.println(ssh_command);

    // Using string array is due to the requirement of the argument accepted
    // by rt.exec.
    String[] commands = new String[] { term, "-e", ssh_command};
    Runtime rt = Runtime.getRuntime();
    try {
      System.out.println("Now launch the terminal");
      rt.exec(commands);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  public static void getUserObject(String username, String password)
  {
    oba=new TestOBA(username, password);
    HashMap requests=oba.getRequestIDs();
    
    Object[] req_id=(Object[]) requests.get("requests");
       for (Object object : req_id) {
          
        obaImagesHash = (HashMap)object;
          System.out.println("Adding to array: "+obaImagesHash.get("requestid").toString());
          oba.activeRequests.add(Integer.parseInt(obaImagesHash.get("requestid").toString()));

               }
      
    Log.d("Requests for this user ", requests.toString());
    Log.d("Requests for this user ", oba.activeRequests.toString());
  }

  /*
  public static void main(String[] args) {
  
    oba = new TestOBA(args[0], args[1]);
        
//     oba.getImageID();    
    if (oba.makeReservation(1)) {
      String[] conn_data = oba.getConnectData();

      // Now launch a Linux terminal to SSH to the reserved machine.
      try {
        // Wait for a short time between getConnectData and termLaunch, since the VCL needs time to process
        // remote IP in its firewall. 
        Thread.sleep(4000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      oba.termLaunch(conn_data);
      // oba.cancelReservation();
    }
  }
*/
}




Java Source Code List

edu.cc.oba.Android_OBAActivity.java
edu.cc.oba.Android_OBAActivity.java
edu.cc.oba.ConnectWithPass.java
edu.cc.oba.ConnectWithPass.java
edu.cc.oba.CopyOfcreateConnection.java
edu.cc.oba.CopyOfcreateConnection.java
edu.cc.oba.ImageDB.java
edu.cc.oba.TestOBA.java
edu.cc.oba.TestOBA.java
edu.cc.oba.chooseImage.java
edu.cc.oba.chooseImage.java
edu.cc.oba.createConnection.java
edu.cc.oba.createConnection.java
edu.cc.oba.mainUITabs.java
edu.cc.oba.mainUITabs.java
edu.cc.oba.oneButtons.java
edu.cc.oba.oneButtons.java
org.apache.http.examples.client.ClientAbortMethod.java
org.apache.http.examples.client.ClientAuthentication.java
org.apache.http.examples.client.ClientChunkEncodedPost.java
org.apache.http.examples.client.ClientConnectionRelease.java
org.apache.http.examples.client.ClientCustomContext.java
org.apache.http.examples.client.ClientCustomSSL.java
org.apache.http.examples.client.ClientEvictExpiredConnections.java
org.apache.http.examples.client.ClientExecuteDirect.java
org.apache.http.examples.client.ClientExecuteProxy.java
org.apache.http.examples.client.ClientExecuteSOCKS.java
org.apache.http.examples.client.ClientFormLogin.java
org.apache.http.examples.client.ClientGZipContentCompression.java
org.apache.http.examples.client.ClientInteractiveAuthentication.java
org.apache.http.examples.client.ClientKerberosAuthentication.java
org.apache.http.examples.client.ClientMultiThreadedExecution.java
org.apache.http.examples.client.ClientPreemptiveBasicAuthentication.java
org.apache.http.examples.client.ClientPreemptiveDigestAuthentication.java
org.apache.http.examples.client.ClientProxyAuthentication.java
org.apache.http.examples.client.ClientWithResponseHandler.java
org.apache.http.examples.conn.ManagerConnectDirect.java
org.apache.http.examples.conn.ManagerConnectProxy.java
org.apache.http.examples.conn.OperatorConnectDirect.java
org.apache.http.examples.conn.OperatorConnectProxy.java
org.apache.http.examples.entity.mime.ClientMultipartFormPost.java
org.kxml2.wap.WbxmlParser.java
org.kxml2.wap.WbxmlSerializer.java
org.kxml2.wap.wml.Wml.java
org.xmlrpc.android.Base64Coder.java
org.xmlrpc.android.IXMLRPCSerializer.java
org.xmlrpc.android.MethodCall.java
org.xmlrpc.android.Tag.java
org.xmlrpc.android.XMLRPCClient.java
org.xmlrpc.android.XMLRPCCommon.java
org.xmlrpc.android.XMLRPCException.java
org.xmlrpc.android.XMLRPCFault.java
org.xmlrpc.android.XMLRPCSerializable.java
org.xmlrpc.android.XMLRPCSerializer.java
org.xmlrpc.android.XMLRPCServer.java