Tim Yang's blog


  • Home

  • Categories

  • Archives

  • Tags

从 Json 到 List:使用 Gson 反序列化异构列表

Posted on 2018-01-27 | In Java

本文要解决的问题:从 json 字符串反序列化形如 List<BaseItem> 的列表,做采集功能模板化时遇到的问题。

1. 需求

App 端需要采集小区、房屋等实体的信息。采集功能是模板化的,配置文件采用 json 格式。整个模板配置包含许多异构的 json 数组,解析成 Java 对象是 List<>。

例如,一个采集任务可能包括多个页面(List<Page>),每个页面有多个卡片(List<Card>)。Page 和 Card 都是接口,有多种类型的子类,这种列表是异构的列表。

Read more »

Compile LineageOS 14.1 for Nexus 5

Posted on 2017-10-22 | In Android

1. 起因

我的三星 S7 时不时整个系统卡顿几秒钟,无法进行任务操作,加上三星对系统是负优化,就刷了 LineageOS 14.1。想要通话录音功能,查了一下,Lineage Team Member - UX maintainer - ddzgam9 说 LineageOS 有通话录音功能,但有一个白名单,在这个名单里的地区才能使用这个功能。于是萌生了修改这个白名单的想法。

三星 S7 是日常使用的手机,不能每天折腾刷机,所以先在闲置的 Nexus 5 上测试一下,于是有了本文。

2. 编译

LineageOS 的文档 写的很好,照着一步一步来就行了,比编译 AOSP 源码简单很多。文档写的很明白,不赘述。在中国需要下拉代码设置代理,方法参考前一篇编译AOSP源码的文章。

3. 刷机

编译完成后,cd $OUT,目录下有一个 lineage-14.1-20171021-UNOFFICIAL-hammerhead.zip ,这个文件就是刷机包,通过 TWRP 就可以刷机了。

Read more »

Compile AOSP Android 7.1.2 Source Code

Posted on 2017-05-13 | In Android

目标:在 Ubuntu 16.04.2 中编译 Android 7.1.2 的源代码, 并在模拟器上运行。

这不是一个 step by step 的教学,只记录过程。本文假设读者熟悉命令行,只是需要知道具体的步骤和命令。

官方文档:https://source.android.com/source/requirements,希望直接去看官方文档,我这里列出的步骤,只适用于我的特定环境。

  • 2017-10-22 更新

LineageOS 编译起来简单多了,新手可以拿这个试手。

Read more »

使用ThreadGroup监控线程退出

Posted on 2016-03-29 | In 工作经验

问题

做性能优化时,有这样一个需求:将运行时间短、申请内存多、结果不留在内存中的操作放到临时进程,运行完后杀掉这个进程,达到迅速降低内存占用的目的。

这个需求的难点在于:

  1. 临时任务在服务里运行,实测发现所有任务线程退出后,进程也不退出。进程中还有main, binder0, binder1等线程在运行
  2. 放到临时进程的的代码里会起新的线程,有的是第三方SDK里new出来的,无法修改。怎样监控这些线程成了问题
Read more »

一个典型的多线程问题

Posted on 2016-03-07 | In 工作经验

一天,同事抛给我一个线上崩溃。堆栈如下(复现的代码)

1
2
3
4
5
6
java.lang.NullPointerException
at java.util.Timer.scheduleImpl(Timer.java:561)
at java.util.Timer.schedule(Timer.java:459)
at run.yang.test.ThreadProblem.problematicMethod(ThreadProblem.java:21)
at run.yang.test.TestTimerMultiThreadProblem$1.run(TestTimerMultiThreadProblem.java:25)
at java.lang.Thread.run(Thread.java:841)

相应代码如下:

Read more »

使用二进制格式存储double数组,加载速度提升100倍

Posted on 2016-03-07 | In 工作经验

问题

遇到一个配置文件,里面有近10000个double类型的数字,每个数字1行,加载到内存里变成List<Double>,在小米3上加载时间需要200多ms。这种写法可读性好,但牺牲了速度和文件大小。而这个文件的使用场景则是越快起好,使用文本方式储存double数组就显得非常naive了。

文件内容类似下面这样

1
2
3
4
5
6
-0.07442092964745924 
-0.2802236054615799
0

-9.125085940770578e-018
0.264550123930026

......

Read more »

JNI Tips annotated

Posted on 2015-12-12 | In Android

copied from JNI Tips .

see also Getting Started with the NDK.


Java Native Interface Specification (Java 7)

JavaVM and JNIEnv

Both of these are essentially pointers to pointers to function tables. (In the C++ version, they’re classes with a pointer to a function table and a member function for each JNI function that indirects through the table.)

  1. In theory you can have multiple JavaVMs per process, but Android only allows one.
  2. The JNIEnv provides most of the JNI functions. Your native functions all receive a JNIEnv as the first argument
  3. The JNIEnv is used for thread-local storage. For this reason, you cannot share a JNIEnv between threads. If a piece of code has no other way to get its JNIEnv, you should share the JavaVM, and use GetEnv to discover the thread’s JNIEnv. (Assuming it has one; see AttachCurrentThread below.)
  4. The C declarations of JNIEnv and JavaVM are different from the C++ declarations. It’s a bad idea to include JNIEnv arguments in header files included by both languages.
Read more »

Resource: Droidcon NYC 2015 - Using Styles and Themes Without Going Crazy

Posted on 2015-12-08 | In Android

video: https://www.youtube.com/watch?v=Jr8hJdVGHAk
slide: https://speakerdeck.com/dlew/using-styles-and-themes-without-going-crazy-1

Main points:

  1. When to style
  2. When NOT to style
  3. Parenting
  4. TextAppearance
  5. Themes vs. Styles
  6. Applying to View
  7. Use AppCompat
  8. Window Attributes
  9. Color Attributes
  10. Default Styles
  11. Resource Attributes
  12. Namespacing
  13. Finding Attributes
  14. Attributes in XML
  15. Debug Color Theme
  16. Dump Theme ( HierarchyViewer)
  17. Custom Attributes
  18. Dynamic Styles
  19. Dynamic Theming

Themes vs. Styles

  • Same thing!
  • Different scope
  • Different attributes

Resource: Advanced Android TextView

Posted on 2015-12-06 | In Android

Talk given by Chiu-Ki Chan on AnDevCon 2015.

Video: Advanced Android TextView
Slide: http://bit.ly/advtext
Source code: https://github.com/chiuki/advanced-textview

Main points:

  • Animated CompoundDrawable
  • Text shadow
  • Custom font
  • Gradient text
  • Patterned text
  • HTML.fromHtml()
  • setFontFeatureSettings()
  • Styled string
  • AlignmentSpan
  • ClickableSpan
  • Rainbow span, animated
  • Lined paper
  • Emoji

whatis, which, whence, where, whereis, type, apropos

Posted on 2015-12-06 | In Linux

command lines in this article assumes zsh.

whatis(1)

whatis - display one-line manual page descriptions. useful options:

  • -r --regex: interpret each keyword as a regex
  • -w --wildcard: keyword(s) contain wildcards
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ whatis gcc
gcc (1) - GNU project C and C++ compiler

$ where whatis
/usr/bin/whatis

$ whatis -r '^wh'
whatis (1) - display one-line manual page descriptions
whereis (1) - locate the binary, source, and manual page files for a...
which (1) - locate a command
whiptail (1) - display dialog boxes from shell scripts
WhitePixel (3) - Display macros and functions
WhitePixelOfScreen (3) - screen information functions and macros
who (1) - show who is logged on
whoami (1) - print effective userid
whois (1) - client for the whois directory service
whois.conf (5) - alternative WHOIS servers list for whois client
Read more »
12
Tim Yang

Tim Yang

Android developer

12 posts
6 categories
20 tags
© 2015 - 2019 Tim Yang
Powered by Hexo
Theme - NexT.Muse