package com.shishixun.installapk;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final File appFolder = new File( "/sdcard" );
ArrayList<File> files = getFiles( appFolder,
true );
PackageManager pm = getPackageManager( );
for ( int i = 0, size = files.size( ); i < size; i++ )
{
File f = files.get( i );
PackageInfo pi= pm.getPackageArchiveInfo( f.getAbsolutePath( ), 0 );
if ( pi != null )
{
ApkInfo holder = new ApkInfo( );
holder.file = f;
holder.pkgName = pi.packageName;
holder.size = f.length( );
holder.sizeString = Formatter.formatFileSize( MainActivity.this,
holder.size );
holder.version = pi.versionName == null ? String.valueOf( pi.versionCode )
: pi.versionName;
System.out.println("file="+f+"pkgName="+holder.pkgName +"size"+holder.sizeString+"version"+holder.version );
}
}
}
ArrayList<File> getFiles( File parent, final boolean recursive )
{
final ArrayList<File> files = new ArrayList<File>( );
FileFilter filter = new FileFilter( ) {
public boolean accept( File f )
{
if ( f.isFile( )
&& f.getName( ).toLowerCase( ).endsWith( ".apk" ) )
{
files.add( f );
}
else if ( recursive && f.isDirectory( ) )
{
try
{
// try skip links
if ( f.getAbsolutePath( )
.equals( f.getCanonicalPath( ) ) )
{
f.listFiles( this );
}
}
catch ( Exception e )
{
Log.e( MainActivity.class.getName( ),
e.getLocalizedMessage( ),
e );
}
}
return false;
}
};
parent.listFiles( filter );
return files;
}
/**
* ApkInfo
*/
private static final class ApkInfo
{
File file;
CharSequence label;
String pkgName;
String version;
String sizeString;
long size;
Drawable icon;
int installed;
boolean checked;
ApkInfo( )
{
}
}
}