Moniker: Developments

From Notes

Jump to: navigation, search

About || Users || Development || Download || Issues || Developments

Contents

Introduction

The Moniker client/server architecture (pictured here) consists of three separate software components: the App Inventor component, which runs on the mobile device, provides all of the user interface code for the app; an Java-based Android component, which handles file downloading and management on the mobile device; and the PHP-based server component, which handles file management on the server.

The source code for each of these components is available here:

  • link
  • link
  • link

App Inventor

Moniker was developed under App Inventor, which is a program that allows anyone, including people without any prior knowledge of computer programming, to create mobile applications for Android phones. Developers use actual blocks of code, which are puzzle pieces that can snap together. App Inventor uses a web based designer, a Blocks Editor, and either a mobile device or emulator. The web based designer enables the user to design the user interface of the app while the Block Editor enables the user to design the logic behind the app. You can see the changes that you make being reflected on your mobile device or the emulator when you connect either one to the Blocks Editor.

Below are what the App Inventor Web Designer and Blocks Editor screens look like.

Web Designer
BlocksEditor

Android

FileDownloader was developed under the Android platform, which is written in Java. The workspace environment that we used was called Eclipse. The code written for the two main actions of this app, which are to download and unzip a file, were found online, written by other developers. In addition to downloading and unzipping a .zip file, FileDownloader also extracts the names of each individual file that were contained in the downloaded .zip file and automatically pass them onto Moniker (App Inventor).

Below is what our Eclipse workspace looks like:


To download the apk file for the FileDownloader app, please visit the Downloads page.

Server (PHP)

The server that was written was developed in PHP (Hypertext Preprocessor), which is a language that is well suited for web development.

To download the entire server file, please visit the Downloads page.

Functions

Below are some of the functions that are defined so that they can be called more than once by separate commands.

*Please note that these are only some of the functions defined, meaning not all of them are shown below*

writelog ($string)

The writelog() function carries out the important task of writing to the "testlog.txt" file every time a command is performed. Below is the script written for the writelog() function.

/** Function to open testlog file
  * Write appropriate log message
  * And close the testlog file
  */
function writelog ($string) {
        $logfile = "testlog.txt";
       	chown($thisdir . $logfile, $owner);
        chmod($thisdir . $logfile, 0777);
        $fh = fopen($logfile, 'a') or die("\nCan't open $logfile");
        fwrite($fh, date("D, d M Y, H:i:s") . ": " . $string . "\n");
        fclose($fh);
}

writeZipList ($string)

The writeZipList() function carries out the task of writing to the "zipList.txt" file all of the existing .zip files in the current directory. Below is the script written for the writeZipList() function.

/** Function to open zipList text file,
  * write zip file names,
  * and close the zipList text file.
  */
function writeZipList ($string) {
        $zipListFile = "zipList.txt";
        $fh = fopen($zipListFile, 'a') or die("\nCan't open $zipListFile");\
				file_put_contents("zipList.txt", "");
        fwrite($fh, $string . "\n");
        fclose($fh);
       	chown($thisdir . $zipListFile, $owner);
        chmod($thisdir . $zipListFile, 0777);
}

generate ($filename)

The generate() function carries out the task of creating a new folder in the current directory and changing the owner and permissions of the file. Below is the script written for the generate() function.


/** This function creates a new folder if passed a filename
  * folder is created in this PHP file's current directory
  * owner is set to variable $owner, which is decided above
  */
function generate($filename) {
	if (file_exists($thidir . $filename)) {
   		writelog($string = "Error: Folder "  . $thisdir . $filename . " already exists.");
    }
    elseif (mkdir($thisdir . $filename)) {
       	chown($thisdir . $filename, $owner);
        chmod($thisdir . $filename, 0777);
        writelog($string = "Folder " . $thisdir . $filename . " created successfully...");
    }
    else {
       	writelog($string = "Error: Unable to create folder " . $thisdir . $filename);
   	}
}

create_zip($files=array()...)

The generate() function carries out the task of creating a new folder in the current directory and changing the owner and permissions of the file. This code was found online written by David Walsh, at this site here. Below is part of the script written for the generate() function.

/** Function that creates .zip folders containing 15 photos
  * each, all of which are located in the currrent directory
  * 
  * We found this function at this site:
  * http://davidwalsh.name/create-zip-php
  */
function create_zip($files = array(),$destination = '',$overwrite = false) {
  //if the zip file already exists and overwrite is false, return false
  if(file_exists($destination) && !$overwrite) { return false; }
  //vars
  $valid_files = array();
  //if files were passed in...
  if(is_array($files)) {
    //cycle through each file
    foreach($files as $file) {
      //make sure the file exists
      if(file_exists($file)) {
        $valid_files[] = $file;
      }
    }
  }
  //if we have good files...
  if(count($valid_files)) {
    //create the archive
    $zip = new ZipArchive();
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {

Commands

Below are some of the commands defined to carry out the important actions of the server. These actions include creating camera and animal folders, zipping up folders, creating a text file containing all existing .zip files in a current directory, and moving the photos into the appropriate animal folders. When the user performs an action on the App Inventor end and requires some sort of action on the server side, it will send a command as a string to the server, where the appropriate action will be taken.

"make"

The "make" command will call the generate($filename) function, which accepts only one parameter, the name of the folder that is to be created. Below is the script written for the "make" command.

// This is the command to generate a folder called "camera1."
if ($cmd == "make") {
        generate($filename = "camera1");

"sort"

The "sort" command will call a function called move($source, $target), which accepts two parameters. The first parameter ($source) is where the current file is located, and the second parameter ($target) is where the file will be moved to. Below is the script written for the "sort" command.

/** This is the command for sorting picures according to variables
  * passed by App Inventor.
  */
elseif ($cmd == "sort") {
        move($source = $image, $target = "camera1/" . $label . "/" . $number);
}

"list"

The "list" command looks through the current directory for all existing .zip files and then call to the writeZipList() function, which will then write the names of the existing .zip files to the "zipList.txt" file. Once this list of .zip files has been created,, it will be sent to App Inventor, where it can be used as needed. Below is the script written for the "list" command.

/** This is the command that will look through the current directory
  * for all existing .zip files and write the names of those files
  * to zipList.txt
  */
elseif ($cmd == "list"){
        $zipString = "";
        // Open the folder
        $dir_handle = @opendir($thisdir) or die("Unable to open $thisdir");
        // Loop through the files
        foreach (glob("*.zip") as $filename) {
           $zipString = $zipString.$filename.",";
   		}
        // Close
         closedir($dir_handle);
		$zipString = substr($zipString, 0, strlen($zipString)-1);
		$zipSubString = "zipList,";
        writeZipList($zipSubString . $zipString);

}

"zipup"

The "zipup" command looks through the current directory for all existing .JPG photos and and then write their names to the "picList.txt" file. It will then zip up all the photos into .zip files, each containing 15 photos. Below is part of the script written for the "zipup" command.


/** This is the command that, given the text file of all the names of the
  * photosin the current directory, will zip up the photos into files 
  * containing 15 photos each.
  */
elseif ($cmd == "zipup") {
	$picString = "";
	$dir_handle = @opendir($thisdir) or die("Unable to open $thisdir");
	writelog($string = $thisdir);
	$var = 0;
	$var2 = 1;
	foreach (glob("*.jpg") as $picName) {
		move($source = $picName, $target = "");
	}
        foreach (glob("*.JPG") as $picName) {
            $picString = $picString.$picName.",";
	    // Keep track of 15 photos
	    $var = $var + 1;
	    if ($var >= 15) {
		$var = 0;
		closedir($dir_handle);
		$picString = substr($picString,0,strlen($picString)-1);

"animals"

The "animals" command will look for a file labeled as "animals.txt" and get each individual name and generate a set of folders accordingly. It will also create the 1,2,3,4,5,6+ number folders within each animal folder. Below is the script written for the "animals" command.

/** This is the command to generate a set of folders located within the 
  * "camera1" folder, with the names that are specified by the animals.txt
  * file.
  */
elseif ($cmd == "animals") {
	$myFile = $thisdir . "/animals.txt";
	$fh = fopen($myFile, 'r');
	$theData = fgets($fh);
	fclose($fh);
	$animal_array = explode("," , "$theData");
	foreach ($animal_array as  &$animal) {
		generate($filename = $thisdir . "/camera1/" . $animal);
	    $anim_subfolders = array("1" , "2" , "3" , "4" , "5" , "6+");
        foreach ($anim_subfolders as &$number) {
                generate($filename = $thisdir . "/camera1/" . $animal . "/" . $number);
		}
	}
}

"admin"

The "admin" command is the most important command, because you need to run it before the users can start using Moniker. The "admin" command carries out the tasks of creating camera folders, animal folders withing each of the camera folders, zip up all of the photos in the current directory, and then write all of the existing .zip files into the zipList.txt file. We created the "admin" command so that we would not have to run each of the four commands mentioned earlier separately. Below is part of the script written for the "admin" command.

/** ADMIN COMMAND:
  * Will make the Camera folders, animal folders given the
  * animals.txt file, zip up the photos, and write zips in 
  * current directory to the zipList.txt file
  */
elseif ($cmd == "admin") {

	// Make Camera1 folder
	generate($filename = "camera1");

	// Make Animal folders
	$myFile = $thisdir . "/animals.txt";
	$fh = fopen($myFile, 'r');
	$theData = fgets($fh);
	fclose($fh);
	$animal_array = explode("," , "$theData");
	foreach ($animal_array as  &$animal) {
		generate($filename = $thisdir . "/camera1/" . $animal);
	    $anim_subfolders = array("1" , "2" , "3" , "4" , "5" , "6+");
        foreach ($anim_subfolders as &$number) {
                generate($filename = $thisdir . "/camera1/" . $animal . "/" . $number);
		}
	}

	// Zip up folders 

	$picString = "";
	$dir_handle = @opendir($thisdir) or die("Unable to open $thisdir");
	writelog($string = $thisdir);
	$var = 0;
	$var2 = 1;
	foreach (glob("*.jpg") as $picName) {

Personal tools
NSF K-12