Skip to content
Ider

沉淀我所学习,累积我所见闻,分享我所体验

Primary Navigation Menu
Menu
  • Home
  • About Ider
    • Who Ider?
    • Why Ider?
    • How Ider?
    • Where Ider?
    • What Ider?

Mobile

2022-12-08
08 December
On December 8, 2022
In IT Products(数码产品)

Pixel Watch智能手表和Pixel 5, 6 Pro 及 7 Pro手机

上个月我去亲身参加了 Google Android Summit 2022。过去几年因为疫情的关系,各类技术大会都在线上举行,今年终于可以小规模地搬到了线下,让社交变得更近。整个会议讲了很多关于 Android 开发的好货,主要集中在开发工具和 Kotlin 的 Compose,但最让大家激动的是在傍晚结束前送给每个参加的人一个最新发布的 Pixel Watch。这应该是最近5年Google在谷歌各种大会上给的最慷慨的一次了。

第三方厂基于Google配合Android系统手机为手表开发的Wear OS发布过很多智能手表,但是由于各种原因都没有Apple Watch那边受欢迎。Pixel Watch是第一款由Google自己发布的智能手表,紧靠着Pixel手机的产品线。因为是第一款自然跟比较成熟的Apple Watch还是无法比拟。

我个人对手表有比较强的需求,这样我可以随时方便地抬手看时间,而不需要每次掏出手机再点亮屏幕。不过我对智能手表一直没有很大的热情和期盼,一来它的大多功能都还无法取代手机对我而言主要功能还是看时间,而来它的低续航又需要我付出其他的精力去维持它的功能。

所以过去一年里, 我日常使用的是 Fitbit Charge 5,在保持一周左右的续航的同时,还能获得简单的健康和锻炼信息。可能正是因为 Pixel Watch 跟 Fitbit 有自然的继承,让我在 Pixel Watch 的转换感到无比的舒适。在拥有同样的 Fitbit 的功能同时,还有更好的交互界面。虽然续航是最大的短板,最多只能持续24小时,但是基于更好得电池布局其充电速度非常得快,很大程度得弥补续航得不足。另外,每天我也会在洗漱时讲手表摘下拿去充电,并没有感到不便,因此 Pixel Watch 自然成了我的日常佩戴。

 

Pixel Watch有不少 Google 和 Fitbit 出的第一方应用,也可以在Play Store上下载很多第三方应用。由于屏幕过小,我并不是特别习惯使用这些应用。但是也有几个功能我非常喜欢使用。

一个是用手表唤起手机铃声,这样就能很快找到手机;另一个是通过手表来控制在手机上的媒体播放,特别是调节音量,我就不需要拿出手机来控制了。

目前还没有对 Pixel Watch 进行深度发掘,希望以后能有更多的发现和惊喜。

Pixel Phones

自2018年介绍了一下 Pixel 3 XL 手机已经有4年时间了,Pixel系列也到了第7代。中间因为没有看到特别大的提升以及听到的质量问题我跳过了 Pixel 4,但集齐了 Pixel 5,Pixel 6 Pro 和 Pixel 7 Pro。

Read More →

2021-01-14
14 January
On January 14, 2021
In Design Patterns(设计模式), English Posts(英文写作), Knowledge Base(心得笔库), Mobile Development(移动开发), Software Engineering(软件工程)

ProtoBuf 2.0 method count optimization for android development

The Protocol Buffer library was not initially built for Android, it just turned out to have a Java version to be used in Android, but it’s not optimized for Android apps, for example: it doesn’t consider the method count limit in Android.

So, in this article, I’d like to share some work I found specifically on Protocol Buffer Version 2 that can reduce the method count. If your app is also heavily relying on Protocol Buffer, I hope these approaches are useful for you too.

General Approaches

1. Use Protocol Buffer Java Lite Runtime

Just as the name indicates, the dependency library is much smaller than the regular Protocol Buffer Java runtime, the generated code is also much slimmer. However, the APIs are compatible between those two versions, so the call sites would not be affected when changing the library.

2. Don’t use <Message>OrBuilder interface

For each Protocol Buffer message definition in .proto file, the Protocol Buffer compiler generates an interface named <Message>OrBuilder (<Message> is the name defined in .proto file). This interface would be implemented by the concrete <Message> class and the corresponding Builder.

It might be attractive to use it as a variable type thereby you can depend on abstractions to not concrete classes. But calling methods on Java interface would make Dex take count of those methods.

In reality, every place can directly use either <Message> or Builder, then the optimization tool (like R8, ProGuard) can safely remove the methods declared on the interface.

Special Tricks

Protocol Buffer Java Lite is very good, but if I open the magic box of generated Java classes, I notice it’s still not optimal for Android. There are a couple places I could modify to make it more effective for Android applications.

Instead of copying the Java files and making the change, which is error prone when engineers update the .proto file, I created a script to automate the job. Just add the execution of this script at the end of the Protocol Buffer gradle task, so it works just as a complement of Protocol Buffer code generation process.

[codesyntax lang=”groovy” lines=”normal”]

protobuf {
  protoc {
    artifact = 'com.google.protobuf:protoc:3.7.0'
  }
  plugins {
    javalite {
      artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
    }
  }
  generateProtoTasks {
    all().each { task ->
      task.builtins {
        remove java
      }
      task.plugins {
        javalite { }
      }

      // `getOutputDir()` can only be read during configuration, 
      // so it’s out of action block
      def outputDir = task.getOutputDir(task.plugins[0])
      task.doLast {
          exec {
              commandLine file('protobuf-optimizer.py')
              args outputDir
          }
      }    
    }
  }
}

[/codesyntax]

In the script, I have made the following modification on generated java code.

1. Change the modifier of Builder constructor from private to package

Simply running the sample addressbook.proto from Protocol Buffer tutorial side, you would get a class like the following snippet:

[codesyntax lang=”java” lines=”normal”]

public static final class Person {
    private Person() {}

    protected final Object dynamicMethod(
            com.google.protobuf.GeneratedMessageLite.MethodToInvoke method,
            Object arg0, Object arg1) {
        switch (method) {
            case NEW_BUILDER: {
                return new Builder();
            }
        }
    }

    public static final class Builder {
        private Builder() {}
    }

}

[/codesyntax]

When analyzing the APK file, we could would find some synthetic classes and methods:

This is because the outer class is accessing the private constructor of the inner class. The generated synthetic class would contribute an extra constructor to the Dex file. Therefore, by simply removing the private from Builder(), you can remove such classes and methods.
Read More →

2019-08-29
29 August
On August 29, 2019
In Data Structures(数据结构), Knowledge Base(心得笔库), Mobile Development(移动开发), Software Engineering(软件工程)

Gradle android-library 的各种坑

软件开发随着项目越来越大,我们会开始拆封出多个子项目(sub-project)或者库(library)来更好地独立维护。Android开发也不例外,我们也会需要创建各种库。如果使用Android Studio和Gradle,他们为我们提供了便捷的方式来创建libray。但是他们也带来了不少的维护问题,本文就来讲讲android-library里的坑来帮助大家避开。

android-library v.s. java-library

对于普通Android手机项目,我们有创建两种不同的Library可以选用: android-library 和 java-library

它们的gradle文件内容分别如下

[codesyntax lang=”groovy” lines=”normal” highlight_lines=”1″]

apply plugin: 'com.android.library'

android {
    // ...
}

dependencies {
    // ...
}

[/codesyntax]

[codesyntax lang=”groovy” lines=”normal” highlight_lines=”1″]

apply plugin: 'java-library'

sourceCompatibility = "7"
targetCompatibility = "7"

dependencies {
    // ...
}

[/codesyntax]

两种库在定义上的根本差别其实就是不同gradle plugin的使用。另一个小的差别就是android-library需要有一个 AndroidManifest.xml 文件,但它可以简单到只有一行内容(其中package的值通常是文件夹的路径)

[codesyntax lang=”xml” lines=”normal”]

<manifest package="com.ider.android.library" />

[/codesyntax]

使用上他们有一些差异,这些差异也是我们决定使用哪种Library的标准:

  • android-library 可以使用 Android API(比如Activity, Service等等),java-library 则不能使用
  • android-library 可以编译android资源文件(resources files),java-library 则无视这样内容
  • android-library 编译出 arr 文件,java-library编译出 jar 文件(本质上他们都是 zip 文件)
  • android-library 的配置可以包含各种编译类型(build variants)来控制,java-library没有那个复杂
  • android-library 可以依赖于另一个android-library 或者 java-library, java-library只能依赖于java-library而不能依赖于android-library

总体而言 android-library 基本是 java-library 的一个超集, 但 android-library 比 java-libray 使用起来要更加复杂一些。从上边的对比来看,对于Android的项目,因为我们不确定什么时候会需要调用 Android 的API,或者定义一下 Android 的资源内容。再者最后一条的约束也决定了创建的 java-library 要保证在整个依赖树(dependency tree)上要在接近叶子节点的位置。所以不如优先选择创建android-library,以免掉入之后改 build.gradle 的坑中。
Read More →

2018-10-29
29 October
On October 29, 2018
In IT Products(数码产品), Mobile Development(移动开发), Tangential Speech(漫话杂谈)

Google Pixel 3 XL 用户体验

上周收到了Pixel 3 XL,简单上手使用之后,最直接的感受是比较失望:

  • 丑陋的刘海造型,而且特别的厚,大部分应用都还没有对其做适应,所以状态条看起来非常不协调
  • 背后的材料变成了玻璃的,没有了原来铝材料的质感好
  • 新的导航手势也不是很舒适,特别是上划两次才能打开应用列表,因为用上划一次来取代了最近使用应用列表
  • 扬声器非常的差,播放声音时背后震动很强烈,声音也闷得好像是老式收音机
  • 跟Pixel 2 XL相比没有特别大的提升

值得点赞的地方可能就是:相机拍摄的照片更加清晰了,特别是前置摄像头带了两个摄像头,拍人像更好看了;还有那 Nexus 系列就有的到了 Pixel 前两代却去掉了的无线充电功能。

对于刘海就特别想吐槽,这部分跟状态栏重叠,所以会导致能显示的通知消息比原来少,非常的讨厌。对于有些应用将状态栏改成其他颜色时,刘海就会特别明显,但是如果进行屏幕截图,刘海的黑色部分又会被自动补全,造成所见影像和生成的图片不一致,非常的抓狂。
要想去除刘海,就需要打开“开发者选项(Developers Options)”来设置,但是真缺了那状态栏的一点空间又觉得挺可惜的。最搞笑的是“Double Cutout”,对称的上下两个刘海,可惜截图是看不出来的。

应用方便,对于 Pixel 3 和 Pixel 3 XL 独享的 Screening Call 表示赞叹,可以过滤掉不少垃圾电话。有些不足的地方是翻译过来的文字必须当场阅读,电话挂断之后就会消失,如果能自动把内容转到Google Voice的留言信息保存起来就更好了。

另外,非常喜欢 Google 还在 beta 测试中的 Digital Wellbeing 的服务,可以看到每天在各种应用上浪费的时间。

同时设置时间来约束使用时长,当天的配额用完后,系统就会应用启动图标变灰并且不能再使用。比如我把 YouTube 的时间设置成了30分钟来防止自己沉迷,但实际是经常我就去设置那边把约束时间去掉,然后静静地在 YouTube 上度过了3个小时。所以本身的自制力才是关键。
希望以后这个服务可以推出更多功能,比如这些功能都是我希望其提供的:导出数据;家长约束并设置不同密码(这样就可以交给太太来管制我);桌面图标小插件;更方便的访问入口。

 

最后,放两张Pixel XL系列的照片(比较讽刺的是照片使用太太的 iPhone X 拍摄的),以及三款手机的规格对比。

Pixel 3 XL Pixel 2 XL Pixel XL
Height 158 mm (6.2 inches) 158 mm (6.2 inches) 154.7 mm (6 inches)
Width 76.7 mm (3.0 inches) 76.7 mm (3.0 inches) 75.7 mm (2.9 inches)
Thickness 7.9 mm (0.3 inches) 7.9 mm (0.3 inches) 8.5 mm (0.3 inches)
Weight 184 grams (6.49 ounces) 175 grams (6.2 ounces) 168 grams (5.93 ounces)
Screen Size 160 mm (6.3 inches) 152.4 mm (6.0 inches) 139.7 mm (5.5 inches)
Screen resolution 2,960 x 1,440 pixels 2,880 x 1,440 pixels 2,560 x 1,440 pixels
Pixel Density 523 ppi 538 ppi 534 ppi
Screen To Body Ratio 83.49% 76.71% 71.04%
RAM 4 GB 4 GB 4 GB
Storage space 64 GB, 128 GB 64 GB, 128GB 32 GB, 128 GB
Processor Qualcomm Snapdragon 845 Qualcomm Snapdragon 835 Qualcomm Snapdragon 821
Graphics Adreno 630 Adreno 540 Adreno 530
Camera 12MP rear, dual 8MP lenses front 12MP rear, 8MP front 12MP rear, 8MP front
Colors Just Black, Clearly White, Not Pink Just Black, Black and White Very Silver, Quite Black, Really Blue
Release date October 9, 2018 October 4, 2017 October 4, 2016
2016-10-04
04 October
On October 4, 2016
In Knowledge Base(心得笔库), Special Tricks(奇技妙招)

从Terminal截取Android设备的屏幕

做Android开发的时候,经常会需要截取设备的屏幕甚至录制设备屏幕,来让同事知道提交的代码实现的功能,或者让他们知道产品中出现来什么问题。截取Android设备的屏幕有很多方法,比如在Nexus系列同时按下“电源”和“音量调低”按钮来截取,也可以用第三方程序来完成。通过另一部手机来拍照录像也是可行的方案。但是这些方法还需要想办法将文件传输到电脑上,又要多一步麻烦的操作。

Mac电脑上也有一些应用程序可以截屏Android设备,但是我更喜欢用终端指令,令人开心的是Android自带的 adb 已经提供了截取屏幕和录制屏幕的指定:

[codesyntax lang=”bash” lines=”normal”]

$ adb shell screencap /sdcard/screen.png
$ adb shell screenrecord --verbose /sdcard/demo.mp4

[/codesyntax]

不过生成的文件依然保存在设备中,还需要用adb pull指定来转到电脑上

为了让整个过程更加的便捷,我将这些指令包裹在bash方法中,然后一个指令来完成截取和传输的工作。

截取Android设备当前屏幕并保存到执行指令的电脑目录上:

[codesyntax lang=”bash” lines=”normal”]

# capture screen of android device
andrdroidScreenCapture() {
    curTime=`date +%Y-%m-%d-%H-%M-%S`
    tmpeName="$curTime.png"
    [[ -n $1 ]] && fileName=$1 || fileName=$tmpeName
    devicePath="/sdcard/$tmpeName"
    adb shell screencap -p $devicePath
    adb pull $devicePath $fileName
    adb shell rm $devicePath
}

[/codesyntax]

录取Android设备屏幕活动,结束后将视频文件保存到执行指令的电脑目录上:

[codesyntax lang=”bash” lines=”normal”]

export ADB_SHELL_SCREENRECORD_ARGS='--verbose --bit-rate 2000000'
# record screen of android device
androidScreenRecord() {
    echo -e "\033[1m(press Ctrl-C to stop recording)\033[0m"
    curTime=`date +%Y-%m-%d-%H-%M-%S`
    tmpeName="$curTime.mp4"
    [[ -n $1 ]] && fileName=$1 || fileName=$tmpeName
    devicePath="/sdcard/$tmpeName"
    adb shell screenrecord $ADB_SHELL_SCREENRECORD_ARGS $devicePath
    sleep 1 # wait for video encoding finish
    adb pull $devicePath $fileName
    # Don't delete copy in device.
    # adb shell rm $devicePath
    open $fileName
}

[/codesyntax]

较短的指令别名,并通过提供的文件的扩展名来决定是截屏还是录制:

[codesyntax lang=”bash” lines=”normal”]

function asc() {
    if [[ -z $1 ]]; then
        echo "Please provide a filename."
        echo "Provideing .png extension for capturing the device screen, and providing .mp4 for recording the device screen."
        return
    fi

    if [[ $1 == *.png ]]; then
        andrdroidScreenCapture $1
    elif [[ $1 == *.mp4 ]]; then
        androidScreenRecord $1
    else
        echo "Filename with unknow extension, only .png and .mp4 are supported"
    fi
}

[/codesyntax]

完整脚本代码可以在Gist上找到,把它们加入到Mac电脑的~/.bash_profile中,连接上开启了“开发者模式”的Android设备就可以方便截图了。

http://blog.iderzheng.com/wp-content/uploads/2016/10/android-screen-capture-SD.mp4

 

References:
  1. Capture or record android screen, pull file to Mac when it’s completed
  2. ADB Shell Commands | Android Studio
  3. Android Debug Bridge | Android Studio
2016-05-09
09 May
On May 9, 2016
In Article Collection(聚宝收藏), IT Products(数码产品), Knowledge Base(心得笔库)

Android开发中真机和模拟器的体验

做移动开发总是离不开在移动设备上做测试,虽然说Android设备单机可能比iPhone便宜,但是真要各种设备集齐也是不小的开支,再加上要试验不同的Android版本那组合起来又有好多了。所以一般开发者往往只在特定几个设备中做检验。需要测试不同版本也会求助于模拟器(Emulator) 。

Google为Android提供的自带模拟器的效果以前实在惨不忍睹,随着Android Studio 2.0出的模拟器倒是提高了不少,但依然挺糟糕的。
android-avd

大多公司都会购买Genymotion使用,可以创建多种不同版本的模拟器,即使是最新的版本也能很快支持。虽说价格抵上一部真机,却得到好多不同版本和厂商的设备,而且效果优越、功能丰富,真实物超所值。
genymotion

由于某些纠纷,Genymotion默认没有Google Play Services,就无法使用Google的Play Store,Account等等。不过网上也有很多攻略教大家怎将其么安装到Genymotion上,于是都可以在上边打《炉石传说》了。

真机和模拟器各有下边这样那样的缺点:

真实设备 模拟器
  • 价格昂贵
  • 不易携带多个
  • 同一设备不支持多种版本
  • 容易没电
  • 老设备会断货
  • 无SIM卡
  • 无摄像头
  • 不支持推送
  • 多手势不方便
  • 使用习惯不一致

Read More →

2016-01-20
20 January
On January 20, 2016
In Article Collection(聚宝收藏), IT Products(数码产品)

一些实用的在线课程的网站

现在的学习已经不仅仅局限于课堂之上,也不光来自于工作之中,借着在线教育网站(Massive open online course, MOOC)的崛起,越来越多的人通过在线学习了补充自身的知识,提高各个方面的能力。之前分享过《一些视频教程网站推荐》,这里再分享一些非常有用的提供在线课程的网站,可以在上下班路上或者平时休息时播放观看。

Udacity

Udacity

Udacity主要是编程开发技术类的在线课程,而且这些课程的由诸如Google、Facebook这些大公司提供,所以质量也很高。这些视频大部分需要付费才能观看,但也有很多免费又值得学习的内容。我个人在上边获得了很多关于Android开发方面的能力提升。

对于付费用户,网站还会在完成某些课程内容后提供“Nanodegree”的荣誉来证明对某项知识已被掌握。

Udemy

Udemy

Udemy上的视频教程包罗万象,对于不满足于一个领域的知识的人,可以在上边了解其他领域的基础知识,比如房产投资,股票交易。

同样的,网站的大部分课程的需要付费,而且价格还不低,但是一些免费课程也非常的受欢迎。另外很多课程每节视频的长度都较短,所以很容易快速完成一节课程的学习。
Read More →

2015-10-08
08 October
On October 8, 2015
In Knowledge Base(心得笔库), Mobile Development(移动开发)

Style在Android中的继承关系

Android的Styles(样式)和Themes(主题)非常类似Web开发里的CSS,方便开发者将页面内容和布局呈现分开。Style和Theme在Android里的定义方式是完全一样的,两者只是概念上的区别:Style作用在单个视图或控件上,而Theme用于Activity或整个应用程序。由于作用范围的不同,Theme也就需要比Style包含更多的定义属性值的项目(item)。不过本文,我将Style和Theme都归为Style来称呼。

Android的Style和Web的CSS相比,有一个缺陷就是只能针对一个对象只能通过android:theme="@style/AppTheme"或style="@style/MyStyle"指定一个值。而CSS则可以通过class属性在DOM元素上定义多个样式来达到组合的效果。不过Style也有CSS没有的功能,那就是继承(Inheritance)。(当然CSS通过LESS和SASS这些工具也获得继承的能力。)
Read More →

2015-06-30
30 June
On June 30, 2015
In Data Structures(数据结构), Design Patterns(设计模式), Knowledge Base(心得笔库), Mobile Development(移动开发)

Android自定义View中MeasureSpec浅析

Android的API为开发者提供了很多好用的widget和control,但在开发中依然会需要自定义一些额外的控件来满足特定的需求。当自定义的视图比较复杂时,就会跟onMeasure(int, int)、onLayout(boolean, int, int, int, int)、onDraw(android.graphics.Canvas)这些方法打交道。要在新创建自定义视图类里正确重载实现这些方法,就需要对这些方法的作用,参数意义有所了解。

本文就来对onMeasure(int, int)方法涉及到的MeasureSpec来做个简单介绍,讲讲它的值的含义,每个状态量都是在什么情况下形成,又该如何使用这些值。
Read More →

2015-04-30
30 April
On April 30, 2015
In Design Patterns(设计模式), Knowledge Base(心得笔库), Mobile Development(移动开发), Programming Life(程序人生)

黑科技尝鲜从ListView迁移到RecyclerView

在《RecyclerView体验简介》里已经介绍了RecyclerView目前的可用情况,也提供了一些文章的链接来了解这个新的Android视图的使用方法,API的调用方式。但由于RecyclerView的Adapter需要开发者重新实现,这相当于一次完整的重构了。本文就来介绍一下我在尝鲜RecyclerView所使用到的非常规手段,如果您也只是想尝试一下把原有的程序转到RecyclerView下看看效果,也许本文会有一些帮助。

ViewHolder-挂羊头卖狗肉

之前已经强调RecyclerView强制要求使用ViewHolder模式。但即使是强制,也可以采用“上有政策,下有对策”,敷衍地实现一个子类就好:

[codesyntax lang=”java” lines=”normal”]

public class DummyViewHolder extends RecyclerView.ViewHolder {
    public DummyViewHolder(View itemView) {
        super(itemView);
    }
}

[/codesyntax]

至于缓存子项视图的内部视图到变量上,就先免了。

对于比较复杂的ListView子视图应该都会遵循使用ViewHolder,只是没有具体继承与某个基类。所以只要将那些自定义的ViewHolder显示继承RecyclerView.ViewHolder,再在构造函数里调用一下super(view);就好。
Read More →

Posts pagination

1 2 3 Next
Facebook
Twitter
LinkedIn
RSS
ZhiHu

Recent Posts

  • 三年居家工作感受
  • Pixel Watch智能手表和Pixel 5, 6 Pro 及 7 Pro手机
  • 我拥有过的无线耳机
  • 毕业工作一个月,我差点被开除
  • 我拥有过的移动硬盘
  • ProtoBuf 2.0 method count optimization for android development
  • 面过100场行为面试后

Categories

  • Algorithm Analysis(算法分析)
  • Article Collection(聚宝收藏)
  • Data Structures(数据结构)
  • Design Patterns(设计模式)
  • English Posts(英文写作)
  • Front Interface(界面构想)
  • IT Products(数码产品)
  • Knowledge Base(心得笔库)
  • Language Tips(语言初试)
  • Mathematical Theory(数学理论)
  • Mobile Development(移动开发)
  • Programming Life(程序人生)
  • Reading Notes(阅而后知)
  • Software Engineering(软件工程)
  • Special Tricks(奇技妙招)
  • Tangential Speech(漫话杂谈)

Tags

Aero Android API Bash Binary Search Bitwise Operation Book C/C++ Career Chrome Conference CSS Debug Device DOM Extension Framework Game Gradle Hearthstone HTML Initialization Intellij Interview iOS Java JavaScript jQuery Keyword Language Issues Mac Microsoft Mobile Modifier Objective-C PHP Principle Reference Regular Expression Static String Tools Tutorial UI XML

Blogroll

  • Ahmed's Blog
  • Gert Lombard's Blog
  • Gordon Luk
  • Jack & Allison
  • 开发部落

Archives

Designed using Chromatic. Powered by WordPress.