Pages

Tuesday, March 20, 2012

[Android] Intent setAction

I am an absolutely newer to Android, and suffered while I developing a project using MIT Funf framework.

these several codes make me scratch my head.
----------------
final Context context = this;
Intent archiveIntent = new Intent(context, MainPipeline.class);
               
 archiveIntent.setAction(MainPipeline.ACTION_ARCHIVE_DATA);
 startService(archiveIntent);
----------------         

The most easy and precise explanation I have saw is:
http://milkmidi.blogspot.com/2012/02/android-intent.html (in Mandarin)

Monday, March 19, 2012

[Android] Resursively compress files in Android

References:
http://stackoverflow.com/questions/5414925/how-to-zip-folder-recursively-in-android
http://ucla.jamesyxu.com/?p=112

and I prefer this:
http://www.mkyong.com/java/how-to-compress-files-in-zip-format/


and I modified it a bit here:
-----------------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class AppZip
{
   List<String> fileList;

   String SOURCE_FOLDER;
   String OUTPUT_ZIP_FILE;

   AppZip(String SOURCE_FOLDER, String OUTPUT_ZIP_FILE){
       this.SOURCE_FOLDER = SOURCE_FOLDER;
       this.OUTPUT_ZIP_FILE = OUTPUT_ZIP_FILE;
       fileList = new ArrayList<String>();
       generateFileList(new File(SOURCE_FOLDER));
   }

 

   /**
    * Zip it
    * @param zipFile output ZIP file location
    */
   public void zipIt(){

    String zipFile = this.OUTPUT_ZIP_FILE;
    byte[] buffer = new byte[1024];

    try{

       FileOutputStream fos = new FileOutputStream(zipFile);
       ZipOutputStream zos = new ZipOutputStream(fos);

       System.out.println("Output to Zip : " + zipFile);

       for(String file : this.fileList){

           System.out.println("File Added : " + file);
           ZipEntry ze= new ZipEntry(file);
           zos.putNextEntry(ze);

           FileInputStream in =
                      new FileInputStream(SOURCE_FOLDER + File.separator + file);

           int len;
           while ((len = in.read(buffer)) > 0) {
               zos.write(buffer, 0, len);
           }

           in.close();
       }

       zos.closeEntry();
       //remember close it
       zos.close();

       System.out.println("Done");
   }catch(IOException ex){
      ex.printStackTrace();  
   }
  }

   /**
    * Traverse a directory and get all files,
    * and add the file into fileList 
    * @param node file or directory
    */
   public void generateFileList(File node){

       //add file only
    if(node.isFile()){
        fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
    }

    if(node.isDirectory()){
        String[] subNote = node.list();
        for(String filename : subNote){
            generateFileList(new File(node, filename));
        }
    }

   }

   /**
    * Format the file path for zip
    * @param file file path
    * @return Formatted file path
    */
   private String generateZipEntry(String file){
       return file.substring(SOURCE_FOLDER.length(), file.length());
   }
}
--------------------------------------
example:
String myFiles = "C:\\myfiles\\"; //do not forgot the backslash in this version
String  outZipName = "compressedFiles"; //compressedFile.zip
AppZip az = new AppZip(myFiles, outZipName);
az.zipIt();




non-recursive version:
http://stackoverflow.com/questions/8409219/zipping-files-and-folders-in-android

Thursday, March 1, 2012

[Java] Recursive listFiles with filter

 /** original: http://snippets.dzone.com/posts/show/1875
*
*    
*    modified by Spencer, Mar. 1, 2012
*    overload member function listfiles: directory name and extension are also acceptable.
*/
import java.io.*;
import java.util.*;
public class FileRecursor
{
public static File[] listFilesAsArray(
        File directory,
        FilenameFilter filter,
        boolean recurse)
{
    Collection<File> files = listFiles(directory,
            filter, recurse);
//Java4: Collection files = listFiles(directory, filter, recurse);
   
    File[] arr = new File[files.size()];
    return files.toArray(arr);
}

public static File[] listFilesAsArray(
        final String sDirectory,
        final String sFilter,
        boolean recurse)
{
    File directory = new File(sDirectory);
    FilenameFilter filter = new FilenameFilter()
    {
        public boolean accept(File dir, String name)
        {
             return name.endsWith(sFilter);
         }
    };
    Collection<File> files = listFiles(directory,
            filter, recurse);
//Java4: Collection files = listFiles(directory, filter, recurse);
   
    File[] arr = new File[files.size()];
    return files.toArray(arr);
}

public static Collection<File> listFiles(
// Java4: public static Collection listFiles(
        File directory,
        FilenameFilter filter,
        boolean recurse)
{
    // List of files / directories
    Vector<File> files = new Vector<File>();
// Java4: Vector files = new Vector();
   
    // Get files / directories in the directory
    File[] entries = directory.listFiles();
   
    // Go over entries
    for (File entry : entries)
    {
// Java4: for (int f = 0; f < files.length; f++) {
// Java4:     File entry = (File) files[f];

        // If there is no filter or the filter accepts the
        // file / directory, add it to the list
        if (filter == null || filter.accept(directory, entry.getName()))
        {
            files.add(entry);
        }
       
        // If the file is a directory and the recurse flag
        // is set, recurse into the directory
        if (recurse && entry.isDirectory())
        {
            files.addAll(listFiles(entry, filter, recurse));
        }
    }
   
    // Return collection of files
    return files;       
}
}

////////////

Example:     
//list the .java files under the current directory
File[] filelist = new FileRecursor().listFilesAsArray("./" ,".java", true);
for(File f:filelist)
   System.out.println(f.getName());

Monday, February 20, 2012

[Android] Android + Eclipse on Mac

1.Download JDK
  
  developer.apple.com

  Click the 'Resource' at the top bar to find the package suit your os

  install it, we assume the path (JDK)is:

  /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Commands



2. Download Android SDK
   
    http://developer.android.com/sdk/

   Choose the suitable version


   Extract it, here, we assume you put the extracted file under : '/Library/android-sdk-macosx'
  $ cd  /Library/android-skd-macosx/tools
  $ ./android

  Choose those version of Android SDK you wish to develop on, then 'Accept all'


3. Download Eclipse and android ADT plug-in
    http://www.eclipse.org/downloads/

    Install and open Eclipse

    Click Help -> Install New Software...

    Click and 'Add...' button on the right side of 'With with'

    Enter you self-description in Name field, say, 'Android Development Kit (ADT)'
    fill the location filed with: 'https://dl-ssl.google.com/android/eclipse/'

    Install 'Android DDMS' and 'Android Development tools' at least
    (If there are error messages under Win7, try running your eclipse as Administrator)

    Click Eclipse -> Preferences -> Android    and set the SDK location as:
    /Library/android-sdk-macosx

Sunday, February 19, 2012

[Git] Git on your MAC

[By macport]
1. Install macport at http://www.macports.org/install.php

sudo port selfupdate 

sudo port install git-core

For default, it will be installed in
/opt/local/bin/git

Note:
Xcode package include git also, its path is:
/usr/bin/git


GUI for git
http://www.sourcetreeapp.com/
or on AppleStore
http://itunes.apple.com/us/app/sourcetree/id411678673


[w/o macport]
 Download the installer for OSX
 http://code.google.com/p/git-osx-installer/


Initialize the current directory:
git init
Add the current directory to tracking repository
git  add .
Commit all the file under the current directory with message 'XXX'
git commit -a -m 'XXX'
Modify the commit message
git commit --amend -m 'new message'
Name the last commit file as 'ver1.0' (branch)
git tag ver1.0
Show all file whose version is 'ver1.0'
git show ver1.0[:[spec file]]
Show the message
git show HEAD
show last message
git show HEAD^
Checkout (restore) all the files to commit 'ver1.0'
git reset ver1.0
Checkout (restore) to last commit
git reset HEAD^

Work with a remote repository
http://spencerimp.blogspot.tw/2013/10/git-how-to-use-git-on-bitbucket.html


You can use option -h to know detail about the command


reference
http://tkg.im.ncue.edu.tw/?p=755

Tuesday, January 10, 2012

[VNC] VNC fullscreen

1. use SSH to login your account

2. edit your ~/.vnc/xstartup
   comment and add
   #twm
  gnome-session &
  #if you use kde
  #startkde

3 start the vncserver by this

   vncserver -geometry 1920x1080


and your session would have a window with 1920x1080 volume