2016年9月14日星期三

Statusbar height, navibar height, screen valid rect ...

  1. public static int getActionBarHeight(Context cxt) {
      int[] abSzAttr;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        abSzAttr = new int[] { android.R.attr.actionBarSize };
      } else {
        abSzAttr = new int[] { R.attr.actionBarSize };
      }
      TypedArray a = cxt.obtainStyledAttributes(abSzAttr);
      return a.getDimensionPixelSize(0, -1);
    }
    
    
    public static void setStatusBarColor(Activity activity, int color) {
      Window window = activity.getWindow();
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      }
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      }
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.setStatusBarColor(color);
      }
    }
    
    public static int getNavigationBarHeight(Context cxt) {
      boolean hasMenuKey = ViewConfiguration.get(cxt)
                                            .hasPermanentMenuKey();
      boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
      int navigationBarHeight = 0;
      if (!hasMenuKey && !hasBackKey) {
        final Resources resources = cxt.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
          navigationBarHeight = resources.getDimensionPixelSize(resourceId);
        }
      }
      return navigationBarHeight;
    }
    
    public static int getStatusBarHeight(Context cxt) {
      int result = 0;
      final Resources resources = cxt.getResources();
      int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
      if (resourceId > 0) {
        result = resources.getDimensionPixelSize(resourceId);
      }
      return result;
    }
    
    public static int getScreenVisibleHeight(Activity cxt) {
      Window window = cxt.getWindow();
      Rect rect = new Rect();
      window.getDecorView()
            .getWindowVisibleDisplayFrame(rect);
      int height = rect.height();
    
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        height -= getStatusBarHeight(cxt);
      }
      return height;
    }

2016年8月2日星期二

老程序员对新入行的朋友的一些建议

从事编程这一行也已经16,7年了,到今天为止,有关编程方面的知识我也写了六七门课程了,教授课程的时间也已超过了1000小时,下面我所说的全部是我多年来的实践的经验之谈。

编程真的不是一件容易的事。

不管你多喜欢或是多会编程,在学习和解决问题上总会碰到障碍。工作的时间越久就越能明白这个道理。不过这倒是一个让人进步的机会,因为你要一直不断的学习才能很好的解决你面前的难题。如果你已不有了进取心,那么当遇到难道无法解决时你就会想要放弃。

编程也是最让人沮丧的事情

正常的编程过程都是这样的:尝试、失败、学习、思考、灵感、成功。在这个漫长的过程中,常有人会感到沮丧也是很正常的事,如果最终成功了,那么接下来你会想挑战更难的项目,当然,之前的沮丧就会从新而来,又是一个从新学习的机会,在这个过程中,我们要懂得调整好心态,从新从发。

办法总是要比困难多

经过不断的学习,我们的编程技术会越来越强,对自己也会更加自信。每个人小时候都会有大大小小的闯祸经历,但随着我们的成长和能力的增强,我们很少再去办错事情了。而如今我们也有办法解决我们所犯的错。

编程就像魔法,而我们每个人都成为魔法师。

经过我们的努力,学习好编程的各种技能,当你做出来的各种编程代码,让别人操作起来轻而易举的就能得到他们想要的效果时,他们就会觉得我们像魔法师一样,因为我们所提供的这些功能可以让他们更轻松,他们也更需要我们的这种技能。

要学会很好的利用网络资源

当你刚开始学习编程时,如果遇到难道,你可以很好的利用网络来搜索你想要的东西,因为在你之前一定有很多人像你一样遇到过类似的问题,所以你一定要尽快的掌握如何有效的网上搜索技能,这样你会更快的得到有效学习。

对于一个程序员来讲,工具箱一定是我们的好助手。

工具箱里的功能是程序员不可缺少的,这些工具将伴随我们每一天,随着我们经验的增长,我们还会学习到更高级的工具,有些工具虽然不是很常用,但他们更擅长解决一些特定的问题(例如,二叉树数据结构、A*、Valgrind、调试器等等)。大多数编程领域(游戏开发、Web开发、数据库)都有自己特有的一系列解决问题的模式。所以掌握的工具越多对你越有利。

如果想更擅长编程,最好是阅读大量代码和亲自写大量的代码。

在这个行业里有成就的老程序员早已写下了很多可供我们参考的代码,所以,为什么不去多看看呢?当然,自己的实践也是不可缺少的,好好练习写代码吧,努力总是有收获的不是吗?

不要满足于现状,因为你永远不会是最好的。

编程中的知识你永远都学不完,他就像一个大集会一样,总是有新的难道需要我们去解决。所以我们也应该要学习新的知识来解决面前的难题。只有你能做到这一点,你将永远不必担心自己的饭碗。

2016年7月29日星期五

Release the Android App: Universal Photos from NASA - NasaPic

Recently I released my new Android App NasaPic. No surprise that the app is a small search-machine for APOD to the NASA platform.

API:

I organize the origin API of NASA into my own one that provides last three days photos , month photos, and photos of a relevant date. The reason to these new methods is that the app can use less calls to backend and save net-traffic.

Souce

Backend:

I host on GAE again :)

UI:

The app uses  customized transition (TransitCompat)codes instead platform ones, although  I can do. The reason for this is that I want to minimize coding diff between high level and low level fragments on Android platform.

Standalone sample source

Database:

I use realm .










2016年7月13日星期三

十分钟学会Python

转: http://www.infocool.net/kb/Python/201606/152824.html


Python是一种动态解释型的编程语言。Python可以在Windows、UNIX、MAC等多种操作系统上使用,也可以在Java、.NET开发平台上使用。


【简介】

Python是一种动态解释型的编程语言。Python可以在Windows、UNIX、MAC等多种操作系统上使用,也可以在Java、.NET开发平台上使用。

 【特点】

1、Python使用C语言开发,但是Python不再有C语言中的指针等复杂的数据类型。

2、Python具有很强的面向对象特性,而且简化了面向对象的实现。它消除了保护类型、抽象类、接口等面向对象的元素。

3、Python代码块使用空格或制表符缩进的方式分隔代码。

4、Python仅有31个保留字,而且没有分号、begin、end等标记。

5、Python是强类型语言,变量创建后会对应一种数据类型,出现在统一表达式中的不同类型的变量需要做类型转换。

【搭建开发环境】

1、可以到www.python.org下载安装包,然后通过configure、make、make install进行安装。

2、也可以到www.activestate.com去下载ActivePython组件包。(ActivePython是对Python核心和常用模块的二进制包装,它是ActiveState公司发布的Python开发环境。ActivePython使得Python的安装更加容易,并且可以应用在各种操作系统上。ActivePython包含了一些常用的Python扩展,以及Windows环境的编程接口)。对ActivePython来说,如果你是windows用户,下载msi包安装即可;如果你是Unix用户,下载tar.gz包直接解压即可。

3、Python的IDE,包括PythonWin、Eclipse+PyDev插件、Komodo、EditPlus

【版本】

python2与python3是目前主要的两个版本。

如下两种情况下,建议使用python2:

1、你无法完全控制你即将部署的环境时;
2、你需要使用一些特定的第三方包或扩展时;

python3 是官方推荐的且是未来全力支持的版本,目前很多功能提升仅在python3版本上进行。

【hello world】

1、创建hello.py
2、编写程序:
3、运行程序:

1、无论是行注释还是段注释,均以#加一个空格来注释。
2、如果需要在代码中使用中文注释,必须在python文件的最前面加上如下注释说明:

# -* - coding: UTF-8 -* -

【文件类型】

1、Python的文件类型分为3种,即源代码、字节代码和优化代码。这些都可以直接运行,不需要进行编译或连接。

2、源代码以.py为扩展名,由python来负责解释;

3、源文件经过编译后生成扩展名为.pyc的文件,即编译过的字节文件。这种文件不能使用文本编辑器修改。pyc文件是和平台无关的,可以在大部分操作系统上运行。如下语句可以用来产生pyc文件:

import py_compile 
py_compile.compile(‘hello.py’)

4、经过优化的源文件会以.pyo为后缀,即优化代码。它也不能直接用文本编辑器修改,如下命令可用来生成pyo文件:

1、python中的变量不需要声明,变量的赋值操作即使变量声明和定义的过程。

2、python中一次新的赋值,将创建一个新的变量。即使变量的名称相同,变量的标识并不相同。用id()函数可以获取变量标识:

4、在函数之外定义的变量都可以称为全局变量。全局变量可以被文件内部的任何函数和外部文件访问。

5、全局变量建议在文件的开头定义。

6、也可以把全局变量放到一个专门的文件中,然后通过import来引用:

gl.py文件中内容如下:

_a = 1
_b = 2

【常量】

python中没有提供定义常量的保留字。可以自己定义一个常量类来实现常量的功能。

1、python的数字类型分为整型、长整型、浮点型、布尔型、复数类型。

2、python没有字符类型

3python内部没有普通类型,任何类型都是对象。

4如果需要查看变量的类型,可以使用type类,该类可以返回变量的类型或创建一个新的类型。

5python有3种表示字符串类型的方式,即单引号、双引号、三引号。单引号和双引号的作用是相同的。python程序员更喜欢用单引号,C/Java程序员则习惯使用双引号表示字符串。三引号中可以输入单引号、双引号或换行等字符。

【运算符和表达式】

1python不支持自增运算符和自减运算符。例如i++/i-是错误的,但i+=1是可以的。

21/2在python2.5之前会等于0.5,在python2.5之后会等于0。

3不等于为!=或<>

4等于用==表示

5逻辑表达式中and表示逻辑与,or表示逻辑或,not表示逻辑非

【控制语句】

1、条件语句:

if (表达式1) : 

  if (表达式2) : 

    语句1

  elif (表达式3) : 

    语句2

  … 

  else: 

    语句3

elif (表达式n) : 

   … 

else : 

   …

4、python本身没有switch语句。
5、循环语句:

1、元组(tuple):python中一种内置的数据结构。元组由不同的元素组成,每个元素可以存储不同类型的数据,如字符串、数字甚至元素。元组是写保护的,即元组创建之后不能再修改。元组往往代表一行数据,而元组中的元素代表不同的数据项。可以把元组看做不可修改的数组。创建元组示例如下:

tuple_name=(“apple”,”banana”,”grape”,”orange”)

2列表(list):列表和元组相似,也由一组元素组成,列表可以实现添加、删除和查找操作,元素的值可以被修改。列表是传统意义上的数组。列表创建示例如下:

list=[“apple”,”banana”,”grage”,”orange”]

可以使用append方法来在尾部追加元素,使用remove来删除元素。

3、字典(dictionary):由键-值对组成的集合,字典中的值通过键来引用。键和值之间用冒号隔开,键-值对之间用逗号隔开,并且被包含在一对花括号中。创建示例如下:

dict={“a”:”apple”, “b”:”banana”, “g”:”grage”, “o”:”orange”}

4、序列:序列是具有索引和切片能力的集合。元组、列表和字符串都属于序列。

【函数相关】

1、python程序由包(package)、模块(module)和函数组成。包是由一系列模块组成的集合。模块是处理某一类问题的函数和类的集合。

2、包就是一个完成特定任务的工具箱。

3、包必须含有一个__init__.py文件,它用于标识当前文件夹是一个包。

4、python的程序是由一个个模块组成的。模块把一组相关的函数或代码组织到一个文件中,一个文件即是一个模块。模块由代码、函数和类组成。导入模块使用import语句。

5、包的作用是实现程序的重用。

6、函数是一段可以重复多次调用的代码,函数定义示例如下:

def arithmetic(x,y,operator): 

   result={ 

      “+”:x+y, 

      “-“:x-y, 

      “*”:x*y, 

      “/”:x/y 

   }

7、函数返回值可以用return来控制。

【字符串相关】

1、格式化输出:

format=”%s%d” % (str1,num)
print format

4、通过切片截取字符串:

word=”world”
print word[0:3]

5、python使用==和!=来进行字符串比较。如果比较的两个变量的类型不相同,那么结果必然为不同。

【文件处理】

1、简单处理文件:

context=”hello,world” 

f=file(“hello.txt”,’w’) 

f.write(context); 

f.close()

2、读取文件可以使用readline()函数、readlines()函数和read函数。
3、写入文件可以使用write()、writelines()函数

【对象和类】

1、python用class保留字来定义一个类,类名的首字符要大写。当程序员需要创建的类型不能用简单类型来表示时,就需要定义类,然后利用定义的类创建对象。定义类示例:

2、当一个对象被创建后,包含了三方面的特性,即对象的句柄、属性和方法。创建对象的方法:

fruit = Fruit()
fruit.grow()

4、类的方法也分为公有方法和私有方法。私有函数不能被该类之外的函数调用,私有的方法也不能被外部的类或函数调用。

5、python使用函数”staticmethod()“或”@ staticmethod“指令的方法把普通的函数转换为静态方法。静态方法相当于全局函数。

6、python的构造函数名为__init__,析构函数名为__del__

7、继承的使用方法:

class Apple(Fruit):
     def …

2016年1月11日星期一

Expand: NASA 's Astronomy Picture of the Day

Limit of original API https://api.nasa.gov/api.html#apod

-> no List with assoicated datetimes.

Now, adding calling with different date-time scopes.

Specification

APIMethodComment
/listPOSTGet list of photos with dates.
/month_listPOSTGet list of photos of a month.
/last_three_listPOSTGet list of photos of last 3 days including today.

Source
https://github.com/XinyueZ/nasaphoto-server




2015年12月18日星期五

2015年12月3日星期四

Push and install APK to different on PC connected devices

There's one classical problem for multi-devices that connect onto PC when we develop Android. You cannot push or install apk by command-line when more than one on it. Fine you can use two steps to do this:

1. Find devices:
    # adb devices -list

2. Select  one or more serial-numbers in your head.

3. Run:
   #  adb -s serial-number install -r  ../asdfasdf/hello.apk

That works but only for one device.

I've packed this command in to a batch and we can do with more than one devices.

Just run:  
   # installapk --all ../asdfasdf/hello.apk 
or
  # installapk serial-number serial-number serial-number serial-number ../asdfasdf/hello.apk 


Try and fork it.

P.S There's also a program to uninstall application from devices.
The command-line to uninstall app is easy like install by instead typing path of apk by package name:

# uninstallapp --all com.helloworld
or
# uninstallapp 23445345sdf sdffsdg com.helloworld

Try



2015年11月30日星期一

Should I move API level to 14

It is an interesting question.  Some % is still in the list of current version fragments.

Is it a real time to switch normal development to API 14 (android 4.0) ?


2015年11月28日星期六

Permissions type: what "must" be done and "can" be done.

If we develop with SDK >= android 6.x (target 2.3 or above) then we need permission check before application starts. It is so nice for user to let them know what the application needs, however for developer is f"x", because the codes need refactoring. There're some "know-how" to do this, even with some 3rd libraries can simplify the functions like https://github.com/hotchemi/PermissionsDispatcher .


I used this library in my projects for permission check for android 6.x.  However there's two parts of permissions which we can be granted. First one are normal ones that only should be declared in mainfest like before  (PROTECTION_NORMAL). The other part of it is the ones that not only be written in mainfest but also in your Java codes to test. Like with PermissionDispatcher you must give user system build-in popup to grant next route.


Automatically granted permissions :

android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT

Just simply declare those permissions in AndroidManifest.xml and it will work just fine. No need to check for the permission listed above since it couldn't be revoked.

The permissions must be checked:



2015年11月20日星期五

Debug with wifi for android dev

  1. Connect device via USB and make sure debugging is working.
  2. adb tcpip 5555
  3. adb connect <DEVICE_IP_ADDRESS>:5555
  4. Disconnect USB and proceed with wireless debugging.
  5. adb -s <DEVICE_IP_ADDRESS>:5555 usb to switch back when done.

2015年11月17日星期二

IT Books 3.0 released

New version includes Google Login, Google Drive to save books,  Material Design on detail-view.

The new Google Login by new Google SigIn API  which provides easy way to connect on Google. A lot confused codes are removed when login and new thinking of App-Flow is created:

1. App start without account.
2. Some features depending on account will be first hidden or unavailable.
3. App connects to account.
4. Features will be opened.


Google Drive is a very nice place to save data background permanently.  With integration of Drive SDK Android  we can implement  backup very easily.  The SDK controls whole upload and download process as user doesn't take any care on network transactions. The only ones to do is success-handling and error-handling to info UI.


To the detail-view the CoordinatorLayout is used to replace my own implement. The standard behavior with FAB and progress-toast cooperate downloading process dynamically.  Checkout a simple sample of  CoordinatorLayout here https://github.com/chrisbanes/cheesesquare

IT Books 3.0


P.S App has a new icon now :)





2015年11月3日星期二

Topfeeds API updated

I updated API to fetch some latest tech-news from China or lots from who hosted by www.blogger.com.

SDK has also been updated.

"Developer Daily" runs internationally.

The Android-App "Developer Daily" which is known as "IT Daily News" or "Developer Daily News" has been updated with international data-source.

The first group of international data is from Android Developer Blog, Google Developer Blog, Android Official Blog, Google Official Blog, Golang Blog, Google Cloud Platform Blog. Including former Chinese source, the application is now available for west users.

The idea of "Developer Daily" is not a beautiful app with to many animation etc, the target of this gadget is only to catch latest what happening in the developer world like Android, Google , might be Apple .

Almost every developer sits on the his desk to code and code without time to read what's going on in his tech-world.  Obversely by using this small tool-kit developer can get latest information just on the way to work. To obtain latest info is more important than standby.

More an more news source will be added into.

Download:




P.S I updated API to fetch some latest tech-news from China or lots from who hosted by www.blogger.com.

SDK has also been updated.