2017年7月

Android TV中WebView的按键响应问题

软硬件环境

  • Macbook Pro MGX 72
  • Android studio 1.0.2
  • BesTV小红

问题场景

当布局中是一个WebView时,打开页面后,焦点就移到了WebView上,默认情况下,此时遥控器只响应返回键,如果需要响应其它键值(比如菜单键),这时该怎么做呢?本文就来解答这个问题。

应对方法

一般的键值处理是放在onKeyDown(int keyCode,KeyEvent keyevent)里处理的,不过在上述场景中,onKeyDown方法中接收不到菜单键,如果想让其也能够接收到,也不难,往下看。

webView = (WebView)findViewById(R.id.webView);

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);

webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            //return super.shouldOverrideUrlLoading(view, url);
            return true;
        }

        @Override
        public boolean shouldOverrideKeyEvent(WebView webView,KeyEvent keyEvent) {
            //让onKeyDown来处理KeyEvent.KEYCODE_STAR,此处对应小红遥控器菜单键
            if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_STAR) {
                return true;
            } else {
                return false;
            }
        }
    }

Ubuntu桌面通知

软硬件环境

  • ubuntu
  • notify-osd
  • libnotify-bin

概述

D-Bus是一种消息总线系统,它为应用程序之间互相调用提供了更为简单的方法。本文中实现的桌面通知就是基于D-Bus机制。桌面通知在ubuntu系统的早期版本中就有应用,如常见的音量调节、thunderbird的来信通知、QQ消息通知等都是如此。

先给一张系统架构图,方便理解。

notify_01

为简单起见,在这里称请求方为Client,响应请求的为Server。

Server

sudo apt-get source notify-osd    
cd notify-osd-0.9.24    
vi notify-osd/src/default.c

#define DEFAULT_DESKTOP_BOTTOM_GAP 6.0f
#define DEFAULT_BUBBLE_WIDTH 24.0f
#define DEFAULT_BUBBLE_MIN_HEIGHT 5.0f
#define DEFAULT_BUBBLE_MAX_HEIGHT 12.2f
#define DEFAULT_BUBBLE_VERT_GAP 0.5f
#define DEFAULT_BUBBLE_HORZ_GAP 0.5f
#define DEFAULT_BUBBLE_SHADOW_SIZE 0.7f
#define DEFAULT_BUBBLE_SHADOW_COLOR "#000000"
#define DEFAULT_BUBBLE_BG_COLOR "#131313"
#define DEFAULT_BUBBLE_BG_OPACITY "#cc"
#define DEFAULT_BUBBLE_HOVER_OPACITY "#66"
#define DEFAULT_BUBBLE_CORNER_RADIUS 0.375f
#define DEFAULT_CONTENT_SHADOW_SIZE 0.125f
#define DEFAULT_CONTENT_SHADOW_COLOR "#000000"
#define DEFAULT_MARGIN_SIZE 1.0f
#define DEFAULT_ICON_SIZE 3.0f
#define DEFAULT_GAUGE_SIZE 0.625f
#define DEFAULT_GAUGE_OUTLINE_WIDTH 0.125f
#define DEFAULT_TEXT_FONT_FACE "Sans"
#define DEFAULT_TEXT_TITLE_COLOR "#ffffff"
#define DEFAULT_TEXT_TITLE_WEIGHT TEXT_WEIGHT_BOLD
#define DEFAULT_TEXT_TITLE_SIZE 1.0f
#define DEFAULT_TEXT_BODY_COLOR "#eaeaea"
#define DEFAULT_TEXT_BODY_WEIGHT TEXT_WEIGHT_NORMAL
#define DEFAULT_TEXT_BODY_SIZE 0.9f
#define DEFAULT_PIXELS_PER_EM 10.0f
#define DEFAULT_SYSTEM_FONT_SIZE 10.0f
#define DEFAULT_SCREEN_DPI 96.0f
#define DEFAULT_GRAVITY GRAVITY_NORTH_EAST

Note

DEFAULT_GRAVITY可以取如下3个值
* GRAVITY_NONE: the behaviour in Jaunty. A notification will always go into the top right corner.    

* GRAVITY_EAST: the behaviour NotifyOsd has been having for about a week in Karmic's development cycle, with the notifications centered vertically on the screen.    

* GRAVITY_NORTH_EAST: the Karmic behaviour. Works like GRAVITY_NONE but does not put asynchronous notifications on top of the screen, even if there is no synchronous notification being displayed.

./configure    
make    
make install(if you need)    

Client

sudo apt-get install libnotify-bin

Have a test

  1. 启动server

    notify-osd/src/notify-osd

  2. 修改文字显示大小
    将 DEFAULT_TEXT_TITLE_SIZE 改为4.0f

  3. Client发送请求

    notify-send "djstava" "I Love It"

notify_02

桌面通知的python实现

  1. 安装pynotify模块
    http://www.galago-project.org/downloads.php下载notify-python-0.1.1.tar.gz,解压然后进行安装三部曲./configure、make、sudo make install

  2. 模块的使用

    try:
    import pynotify
    if pynotify.init('Fist Test'):

         n = pynotify.Notification('Title','message')
         n.show()
    
     else:
    
         print 'Pynotify module not initialized'
    except:
    
     print 'Pynotify module not installed'
    


    可以像下面一样来设置消息的紧急级别

    n.set_urgency(pynotify.URGENCY_LOW)
    n.set_urgency(pynotify.URGENCY_NORMAL)
    n.set_urgency(pynotify.URGENCY_CRITICAL)

关闭消息通知操作
    n.close()

参考资料

1、http://kobesearch.cpan.org/htdocs/Desktop-Notify/Desktop/Notify.pm.html
2、http://www.galago-project.org/specs/notification/0.9/index.html
3、https://wiki.ubuntu.com/NotifyOSD
4、http://doc.trolltech.com/4.2/intro-to-dbus.html
5、http://www.freedesktop.org/wiki/Software/dbus

python使用pycurl库

软硬件环境

  • macOS Sierra
  • pycurl-7.19.5

Mac安装pycurl

源码下载地址http://pycurl.sourceforge.net

tar xvf pycurl-7.19.5.tar.gz    
cd pycurl-7.19.5    
sudo python setup.py install    

测试pycurl是否安装成功,命令行执行python

import pycurl   
print pycurl.version    

如果你使用是pycharm,可以直接到preference里安装,如下图

pycurl_01

实例

这里的示例,实现一个从web server上下载一个文件到本地的功能,利用pycurl这个模块,直接上代码

__author__ = 'djstava'    
#!/usr/bin/env python
#coding=utf-8

import pycurl
import sys

DJ_PYCURL_CONNECTTIMEOUT = 30
DJ_PYCURL_TIMEOUT = 300
DJ_PYCURL_DOWNLOADURL = "http://192.168.1.99/test.zip"
DJ_PYCURL_DOWNLOAD_FILE = "download.file"

fp = open(DJ_PYCURL_DOWNLOAD_FILE,'wb+')

def dj_pycurl_writeFile(buffer):
    fp.write(buffer)

def dj_pycurl_download(url):
    pycurl.global_init(pycurl.GLOBAL_ALL)
    c = pycurl.Curl()

    c.setopt(pycurl.URL, url)
    c.setopt(pycurl.WRITEDATA,fp)
    c.setopt(pycurl.WRITEFUNCTION,dj_pycurl_writeFile)
    c.setopt(pycurl.NOPROGRESS,0)
    c.setopt(pycurl.CONNECTTIMEOUT,DJ_PYCURL_CONNECTTIMEOUT)
    c.setopt(pycurl.TIMEOUT,DJ_PYCURL_TIMEOUT)
    c.setopt(pycurl.VERBOSE,1)

    c.perform()
    c.close()
    fp.close()

if __name__ == '__main__':
    dj_pycurl_download(DJ_PYCURL_DOWNLOADURL)

代码测试

pycurl_02

Mac安装nginx

brew install nginx

修改nginx配置文件

/usr/local/etc/nginx/nginx.conf

nginx默认根目录

/usr/local/opt/nginx/html

启动nginx服务

sudo nginx

References

1、http://pycurl.sourceforge.net

macOS搭建Spring Boot开发环境

软硬件环境

  • macOS Sierra
  • java 1.8.0_65
  • maven 3.5.0
  • idea 2017.1.5

前言

最近接触了一点java web相关的知识,了解一下最近比较火的开发框架Spring Boot,站在一个从未涉足过java web和spring的开发者角度来讲,spring boot确实是一个非常不错的框架,配置简单,容易入门,对于想入行java web的童鞋,是一个很好的切入点。

maven安装

这里选择maven作为构建工具,你也可以使用其它的,如gradle等。到apache站点https://maven.apache.org/download.cgi下载最新稳定版的zip包,解压到指定目录,比如我这里是/Users/djstava/Workshop/tools,然后编辑/Users/djstava/.bash_profile文件,在文件末尾增加语句

export PATH=$PATH:/Users/djstava/Workshop/tools/apache-maven-3.5.0/bin

设置完成后,在Terminal中执行

source ~/.bash_profile

上述操作后,最后来检查一下设置的结果是否正确,在终端中执行

mvn -version

IDE选择

我这里选择jetbrains的IntelliJ IDEA,当然你也可以选择其它的,如eclipse。

创建第一个应用

新建工程

点击Create New Project

sb_01

选择左侧的Spring Initializer,Project SDK选择java 1.8

sb_02

接下来是一些工程基本信息的填写,Type选择Maven Project,Packaging选择Jar,其它的可以随意写

sb_03

依赖项选择Web,如图所示

sb_04

sb_05

工程创建成功后,第一个构建过程会比较慢,等待完成后,点击DemoApplication.java中的main函数前的绿色按钮就可以启动工程了

sb_06

这时候打开浏览器,输入localhost:8080查看

sb_07

上面浏览器输出是正常的。接下来我们加入点代码,让它能够显示点东西出来。创建一个新的java类

sb_08

HelloController.java源码
package com.xugaoxiang;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by djstava on 15/07/2017.
 */
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }

}

重新启动工程,查看浏览器,输入地址localhost:8080/hello

sb_09

工程解释

@RestController和@RequestMapping,这是2个很重要的注解。@RestController负责处理http请求,@RequestMapping负责url的映射。