Sunday, June 19, 2011

Setting up the free audio file download

Ok, I did a test on the nag screen. Now, what I need to do is set up the *automatic* download of the files if it's the free version. The free version = nag number = 100. So, in that case, what I need to do is if it's the first time the app is run, and it's free, get shorter version of the audio zip files. Otherwise, if it's the first time (do they have to uninstall? and the nag number > 100, and thelevel is 4 or 5, get the complete version.

Here's the whole logic:

if (level >= 4)
If nag <= 100
check for existence of first audio file at that level
if not found
download short version for that level
else
check for existence of 101st audio file at that level
if not found
download full version for that level

Where should I put it? In the start activity.

Ok, I just extracted some the download logic to a separate class and it's great. I love good refactoring.

Let's test it.

I have to remember to disable the get audio button if the level >4, but first let's test.

Ok, it's downloading. Great. Now commit.

Ok. I'm working on getting the download on the open of the program. I just need to check if one audio file for that level exists.

I had to rename the folders for the free to match the full so the android folder for both will be the same.

Ok, now I'm just going to have my new download class check for the existence of audio.

Ouch. I'm all set, but I forgot I ordered the file names by level, number instead of by frequency. So, I need a script to copy the files from the full directory to the free directory based on the first 100 frequency numbers.

The logic will be

Read the database all_words table in order by frequency

for the first 100 found create a file name based on leve and number.

Copy that file to a new directory.

Can I get this done, before the place closes?

Maybe I can read an extract file...

OK. I exported a csv file; then combined a parse csv java routine with a copy file routine to create the copy. For prosperity, here's the program:

/*
Parse CSV File using StringTokenizer example.
This example shows how to parse comma separated file (CSV file) using
Java StringTokenizer and BufferedReader classes.
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore.Entry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class ParseCSVFileExample {

public static void main(String[] args) {

ArrayList recs = new ArrayList();

try {

// csv file containing data
String strFile = "/Users/mypath/Level_5_by_freq_col_change.csv";

// create BufferedReader to read csv file
BufferedReader br = new BufferedReader(new FileReader(strFile));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;

HashMap fields = new HashMap();

// read comma separated file line by line
while ((strLine = br.readLine()) != null) {

lineNumber++;
fields = new HashMap();

// break :separated line using ":"
st = new StringTokenizer(strLine, ":");

while (st.hasMoreTokens()) {
// display csv values
tokenNumber++;
String name = null;

// why doesn't case statment work?
if (1 == tokenNumber)
name = "_id";
if (2 == tokenNumber)
name = "level";
if (3 == tokenNumber)
name = "number";
if (4 == tokenNumber)
name = "freq";
if (5 == tokenNumber)
name = "kanji";
if (6 == tokenNumber)
name = "kana";
if (6 == tokenNumber)
name = "english";

String nextToken = st.nextToken();
/*
* System.out.println("Line # " + lineNumber + ", Token # "
* + tokenNumber + ", name " + name + ", Token : " +
* nextToken);
*/

fields.put(name, nextToken);
}

// reset token number
tokenNumber = 0;

recs.add(fields);
}

} catch (Exception e) {
System.out.println("Exception while reading csv file: " + e);
}

int cntr = 0;

for (HashMap rec : recs) {

cntr++;

if (cntr <= 100)
setupCopyFile(cntr, rec);

}

}

private static void setupCopyFile(int cntr, HashMap rec) {

String level = rec.get("level");
String number = rec.get("number");
String freq = rec.get("freq");

System.out.println("cntr: " + cntr + ", level: " + level + ", number: "
+ number + ", freq: " + freq);

String rootSrc = "/Users/mypath/full/aud5";
String rootDst = "/Users/mypath/free/aud5";

String srcFileName = rootSrc + "/" + level + "_" + number + ".mp3";
String dstFileName = rootDst + "/" + level + "_" + number + ".mp3";

copyfile(srcFileName, dstFileName);
}

private static void copyfile(String srFile, String dtFile) {
try {
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);

// For Append the file.
// OutputStream out = new FileOutputStream(f2,true);

// For Overwrite the file.
OutputStream out = new FileOutputStream(f2);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
} catch (FileNotFoundException ex) {
System.out
.println(ex.getMessage() + " in the specified directory.");
//System.exit(0);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}

}

No comments:

Post a Comment