2016.3.11补记: 如何获取docs?
从官网基本上就不行了,除非去翻墙。但没事折腾墙也闹腾,所以国内有很多下载好的,虽然版本不是最新,但通常已经足够了。这里提供一些链接:
- chm格式:http://download.csdn.net/detail/tuoguang/8196119 直接转换成chm格式的,windows上用着很方便。
- android各种资源:http://mirrors.neusoft.edu.cn/android/repository/, 包括zip格式的docs
- http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/android-platform-sdk-download-mirror.html 这个讲的很全
本文参考如下文档:
上面的文档提到的是注释掉jsapi,代码如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;
/**
* The code is refactored from http://blog.csdn.net/baggio1006/article/details/8272229
*
* example html code in index.html:
* <!-- JAVASCRIPT -->
* <script src="http://www.google.com/jsapi" type="text/javascript"></script>
* <script src="./assets/js/android_3p-bundle.js" type="text/javascript"></script>
* <script type="text/javascript">
* var toRoot = "./";
* var devsite = false;
* </script>
*
* usage:
* java -jar AndroidDocsLocalizer <the_android_docs_directory>
*/
public class AndroidDocsLocalizer {
private static final String TAG = "<script src=\"http://www.google.com/jsapi\" type=\"text/javascript\"></script>";
private static String docsPath;
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: "
+ System.getProperty("sun.java.command")
+ " <android_docs_path>");
return;
}
docsPath = args[0];
File docs = new File(docsPath);
if (!docs.exists()) {
System.out.println(docsPath + " not a valid directory!");
return;
}
System.out.println("It will take several minutes, please wait...");
traverse(docs);
System.out.println("DONE!");
}
private static void traverse(File dir) {
if (!dir.isDirectory()) return;
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
return isHtmlFile(file);
}
});
for (File file : files) {
if (file.isDirectory()) {
System.out.println("Scanning Folder: "
+ file.getAbsolutePath());
traverse(file);
} else {
try {
commentJsapi(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static boolean isHtmlFile(File f) {
return f.isFile() && f.getName().endsWith(".html");
}
/**
*
* @param file
* @throws IOException
*/
private static void commentJsapi(File file) throws IOException {
String destFileName = file.getParent() + "\\" + UUID.randomUUID().toString()
+ ".html";
File destFile = new File(destFileName);
BufferedReader reader = null;
BufferedWriter writer = null;
boolean changed = false;
destFile.createNewFile();
reader = new BufferedReader(new FileReader(file));
writer = new BufferedWriter(new FileWriter(destFile));
String buffer;
while ((buffer = reader.readLine()) != null) {
if (buffer.trim().equalsIgnoreCase(TAG)) {
changed = true;
buffer = "<!--" + TAG + "-->";
}
writer.write(buffer + "\n");
writer.flush();
}
reader.close();
writer.close();
if (changed) {
file.delete();
destFile.renameTo(file);
System.out.println("Changed: " + file.getAbsolutePath());
} else {
destFile.delete();
System.out.println("Unchanged: " + file.getAbsolutePath());
}
}
}
采用这种方法优化之后,速度的确快了一些,但远不是预期的样子。
进一步分析,发现在打开网页的过程中,会有如下的一些提示信息:
因此,可以遵循上述思路继续优化。参考“三种方法解决android帮助文档打开慢”的方法,对代码优化如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* The code is refactored from:
* 1. http://blog.csdn.net/baggio1006/article/details/8272229
* 2. http://blog.csdn.net/liu1164316159/article/details/32335743
*
*/
public class AndroidDocsLocalizer {
private static final String REMOVE_START = "<!--";
private static final String REMOVE_END = "-->";
private static final String JSAPI_RAW = "<script src=\"http://www.google.com/jsapi\" type=\"text/javascript\"></script>";
private static final String JSAPI_RAW_PATTERN = "<script src=\"http://www\\.google\\.com/jsapi\" type=\"text/javascript\"></script>";
private static final String JSAPI_REMOVED_RAW = REMOVE_START + JSAPI_RAW + REMOVE_END;
private static final String FONTS_RAW = "<link rel=\"stylesheet\"\nhref=\"http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold\" title=\"roboto\">";
private static final String FONTS_RAW_PATTERN = "<link rel=\"stylesheet\"\nhref=\"http://fonts\\.googleapis\\.com/css\\?family=Roboto:regular,medium,thin,italic,mediumitalic,bold\" title=\"roboto\">";
private static final String FONTS_REMOVED_RAW = REMOVE_START + FONTS_RAW + REMOVE_END;
private static String docsPath;
public static void main(String[] args) {
docsPath = "/home/flying-bird/software/android/adt-bundle-linux-x86-20140321/sdk/docs"; // /guide/topics/ui/layout";
File docs = new File(docsPath);
if (!docs.exists()) {
System.out.println(docsPath + " not a valid directory!");
return;
}
System.out.println("It will take several minutes, please wait...");
traverse(docs);
System.out.println("DONE!");
}
private static void traverse(File dir) {
if (!dir.isDirectory()) return;
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
return isHtmlFile(file);
}
});
for (File file : files) {
if (file.isDirectory()) {
System.out.println("Scanning Folder: "
+ file.getAbsolutePath());
traverse(file);
} else {
try {
removeJscript(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static boolean isHtmlFile(File f) {
return f.isFile() && f.getName().endsWith(".html");
}
/**
*
* @param file
* @throws IOException
*/
private static void removeJscript(File file) throws IOException {
String content = read(file);
boolean changed = false;
if (content.indexOf(JSAPI_REMOVED_RAW) == -1) {
System.out.println("jsapi will be removed.");
changed = true;
content = content.replaceFirst(JSAPI_RAW_PATTERN, JSAPI_REMOVED_RAW);
} else {
System.out.println("jsapi has beed removed");
}
if (content.indexOf(FONTS_REMOVED_RAW) == -1) {
System.out.println("fonts will be removed.");
changed = true;
content = content.replaceFirst(FONTS_RAW_PATTERN, FONTS_REMOVED_RAW);
} else {
System.out.println("fonts has been removed.");
}
if (changed) {
write(content, file);
System.out.println("Changed: " + file.getAbsolutePath());
} else {
System.out.println("Unchanged: " + file.getAbsolutePath());
}
}
public static String read(File src) {
StringBuffer res = new StringBuffer();
String line = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(src));
int i = 0;
while ((line = reader.readLine()) != null) {
if (i != 0) {
res.append('\n');
}
res.append(line);
i++;
}
res.append('\n'); // the last line of the file
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res.toString();
}
public static boolean write(String cont, File dist) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
writer.write(cont);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
此时打开docs就没有任何性能问题了。