From 7287f80e144177895a5b17f7e0eb3488303b0d1c Mon Sep 17 00:00:00 2001 From: John Reck Date: Tue, 17 Jan 2012 13:20:02 -0800 Subject: Add a folder index view in debug mode Change-Id: I67bb28d95cbe5ab6cf361271b46acc44398f188e --- .../android/browser/homepages/HomeProvider.java | 26 +++++++- .../android/browser/homepages/RequestHandler.java | 73 ++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) (limited to 'src/com/android/browser/homepages') diff --git a/src/com/android/browser/homepages/HomeProvider.java b/src/com/android/browser/homepages/HomeProvider.java index 98fcfbe..49ae694 100644 --- a/src/com/android/browser/homepages/HomeProvider.java +++ b/src/com/android/browser/homepages/HomeProvider.java @@ -16,8 +16,6 @@ */ package com.android.browser.homepages; -import com.android.browser.BrowserSettings; - import android.content.ContentProvider; import android.content.ContentValues; import android.content.Context; @@ -28,8 +26,13 @@ import android.os.ParcelFileDescriptor; import android.util.Log; import android.webkit.WebResourceResponse; +import com.android.browser.BrowserSettings; + +import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; public class HomeProvider extends ContentProvider { @@ -95,8 +98,27 @@ public class HomeProvider extends ContentProvider { return new WebResourceResponse("text/html", "utf-8", ins); } } + boolean listFiles = BrowserSettings.getInstance().isDebugEnabled(); + if (listFiles && interceptFile(url)) { + PipedInputStream ins = new PipedInputStream(); + PipedOutputStream outs = new PipedOutputStream(ins); + new RequestHandler(context, Uri.parse(url), outs).start(); + return new WebResourceResponse("text/html", "utf-8", ins); + } } catch (Exception e) {} return null; } + private static boolean interceptFile(String url) { + if (!url.startsWith("file:///")) { + return false; + } + String fpath = url.substring(7); + File f = new File(fpath); + if (!f.isDirectory()) { + return false; + } + return true; + } + } diff --git a/src/com/android/browser/homepages/RequestHandler.java b/src/com/android/browser/homepages/RequestHandler.java index 3eb58e6..1ae5b3e 100644 --- a/src/com/android/browser/homepages/RequestHandler.java +++ b/src/com/android/browser/homepages/RequestHandler.java @@ -29,10 +29,14 @@ import android.util.Base64; import android.util.Log; import com.android.browser.R; +import com.android.browser.homepages.Template.ListEntityIterator; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.text.DateFormat; +import java.text.DecimalFormat; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -71,6 +75,10 @@ public class RequestHandler extends Thread { } void doHandleRequest() throws IOException { + if ("file".equals(mUri.getScheme())) { + writeFolderIndex(); + return; + } int match = sUriMatcher.match(mUri); switch (match) { case INDEX: @@ -127,6 +135,71 @@ public class RequestHandler extends Thread { t.write(mOutput); } + void writeFolderIndex() throws IOException { + File f = new File(mUri.getPath()); + final File[] files = f.listFiles(); + Template t = Template.getCachedTemplate(mContext, R.raw.folder_view); + t.assign("path", mUri.getPath()); + t.assign("parent_url", f.getParent() != null ? f.getParent() : f.getPath()); + t.assignLoop("files", new ListEntityIterator() { + int index = -1; + + @Override + public void writeValue(OutputStream stream, String key) throws IOException { + File f = files[index]; + if ("name".equals(key)) { + stream.write(f.getName().getBytes()); + } + if ("url".equals(key)) { + stream.write(("file://" + f.getAbsolutePath()).getBytes()); + } + if ("type".equals(key)) { + stream.write((f.isDirectory() ? "dir" : "file").getBytes()); + } + if ("size".equals(key)) { + if (f.isFile()) { + stream.write(readableFileSize(f.length()).getBytes()); + } + } + if ("last_modified".equals(key)) { + String date = DateFormat.getDateTimeInstance( + DateFormat.SHORT, DateFormat.SHORT) + .format(f.lastModified()); + stream.write(date.getBytes()); + } + if ("alt".equals(key)) { + if (index % 2 == 0) { + stream.write("alt".getBytes()); + } + } + } + + @Override + public ListEntityIterator getListIterator(String key) { + return null; + } + + @Override + public void reset() { + index = -1; + } + + @Override + public boolean moveToNext() { + return (++index) < files.length; + } + }); + t.write(mOutput); + } + + static String readableFileSize(long size) { + if(size <= 0) return "0"; + final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" }; + int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); + return new DecimalFormat("#,##0.#").format( + size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; + } + String getUriResourcePath() { final Pattern pattern = Pattern.compile("/?res/([\\w/]+)"); Matcher m = pattern.matcher(mUri.getPath()); -- cgit v1.1