技术小黑屋

Use Vim in StackEdit

StackEdit is really great online editor. It could connect with Google Drive. And what’s more, you can even use Vim in this editor.
Now add the following snippet into SettingsExtensionsUserCustom extension javascript area.

linenos:false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
userCustom.onReady = function() {
        var ace = {}
        ace.require = require
        ace.define = define
    ace.require(["ace/lib/net"], function(acenet) {
        acenet.loadScript("https://rawgit.com/androidyue/resources/master/keybinding-vim.js", function() {
                        e = document.querySelector(".ace_editor").env.editor
            ace.require(["ace/keyboard/vim"], function(acevim) {
                                e.setKeyboardHandler(acevim.handler);
                            
            });
                    
        });
            
    });
        window.ace = ace;

};

##Others

Fix Notification Switching Position Issue

I once faced with a problem. I wrote a piece of code related with notifcation. Everything goes fine except one little issue. I found the ongoing notification switching order. My notification blinked each second. After Googling I found the reason and resolved the problem.

Fix No Sound Issue on Mac

Now I am dealing with Mac OSX. However I found sometimes this is no sound. People say it’s a bug. I have tried some methods. And found this goes for me every time.

1
2
3
#!/bin/bash
sudo kextunload /System/Library/Extensions/AppleHDA.kext
sudo kextload /System/Library/Extensions/AppleHDA.kext

Put the code into a shell script and run the script whenever the issue occurs.

###Others

Scan Media Files in Android

I once tried to use MediaScanner to resolve problems; however it turned out to be a failure. Now I make it.This post is to write down why I failed and how I work it out now. I think it could be deeper that other posts.

Set the Preferred Application in Android

A great application should provide a function to let users set it as the preferred or default application. In this post, I will make a description on PreferredApplication
Before an application is about to be set as default, it has two possibilities. One is that there is already a default application and the other is no default application.

日语文法:ください

##Nをください

  • 意思:请…
  • 例句:
  • りんごをください  请把苹果给我
  • ほんを私にください 请把书给我一下。

Fix Missing Command Takes Too Long to Respond in Fedora

I am getting well on with Fedora now. However when I was a fresher to Fedora, I have met a lot of problems.
One of them is that When I type some wrong commands It will take too long to respond. It’s totally different from Ubuntu,which I used before. However I like it could quickly reponse even through the command does not exist.

1
unset command_not_found_handle

Ok.Add the above code to .bashrc And it works.

##Others

Refer String Resources From Other Applications

The following code works. Here take getting String resource for example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void testUseAndroidString() {

    Context context = getContext();
    Resources res = null;
    try {
        //I want to use the clear_activities string in Package com.android.settings
        res = context.getPackageManager().getResourcesForApplication("com.android.settings");
        int resourceId = res.getIdentifier("com.android.settings:string/clear_activities", null, null);
        if(0 != resourceId) {
            CharSequence s = context.getPackageManager().getText("com.android.settings", resourceId, null);
            Log.i(VIEW_LOG_TAG, "resource=" + s);
        }
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    
}

###Others

Get Android System Available Features

An easy way to get system available features.

1
2
3
4
5
6
7
private void dumpSystemFeatures() {

    FeatureInfo[] features = this.getPackageManager().getSystemAvailableFeatures();
        for (FeatureInfo f : features) {
            Log.i(LOGTAG, "dumpSystemFeatures f" + f);
        }
}

Have a glance at the result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d74fd0 android.hardware.wifi fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75048 android.hardware.location.network fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d750d8 android.hardware.nfc fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75150 com.google.android.feature.GOOGLE_BUILD fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d751f0 android.hardware.location fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75270 android.hardware.sensor.gyroscope fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75300 android.hardware.screen.landscape fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75390 android.hardware.screen.portrait fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75420 android.hardware.wifi.direct fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d754a8 android.hardware.usb.accessory fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75530 android.hardware.bluetooth fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d755b0 android.hardware.touchscreen.multitouch.distinct fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75660 android.hardware.microphone fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d756e8 android.hardware.sensor.light fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75770 android.hardware.camera.autofocus fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75800 android.software.live_wallpaper fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75890 android.hardware.camera.flash fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75918 android.hardware.telephony fl=0x0}
I/MainActivity(13514): dumpSystemFeatures fFeatureInfo{40d75998 android.software.sip fl=0x0}
......

##The Other way You can also use adb shell to get into the shell of your device and the use pm list features to get the available features of a device.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
root@android:/ # pm list features
feature:reqGlEsVersion=0x20000
feature:android.hardware.bluetooth
feature:android.hardware.camera
feature:android.hardware.camera.autofocus
feature:android.hardware.camera.flash
feature:android.hardware.camera.front
feature:android.hardware.faketouch
feature:android.hardware.location
feature:android.hardware.location.gps
feature:android.hardware.location.network
feature:android.hardware.microphone
feature:android.hardware.nfc
feature:android.hardware.screen.landscape
feature:android.hardware.screen.portrait
feature:android.hardware.sensor.accelerometer
feature:android.hardware.sensor.compass
feature:android.hardware.sensor.gyroscope
feature:android.hardware.sensor.light
feature:android.hardware.sensor.proximity
feature:android.hardware.telephony
feature:android.hardware.telephony.gsm
feature:android.hardware.touchscreen
feature:android.hardware.touchscreen.multitouch
feature:android.hardware.touchscreen.multitouch.distinct
feature:android.hardware.touchscreen.multitouch.jazzhand
feature:android.hardware.usb.accessory
feature:android.hardware.usb.host
feature:android.hardware.wifi
feature:android.hardware.wifi.direct
feature:android.software.live_wallpaper
feature:android.software.sip
feature:android.software.sip.voip
feature:com.cisco.anyconnect.permissions.patch.htc
feature:com.htc.android.rosie.widget
feature:com.htc.lockscreen.fusion
feature:com.nxp.mifare

For a detailed understanding of use-filter,please read this post http://developer.android.com/guide/topics/manifest/uses-feature-element.html

###Others

Get an Application Required Features

How to get application’s required features? Actually the aapt really does a great help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#Usage:aapt dump badging apk_location | grep feature 

#Example
aapt dump badging /tmp/language_check_maxthon_99985f_2793_4.1.3.1000_remote_develop.apk | grep Features

#Result
uses-feature-not-required:'android.hardware.location'
uses-feature-not-required:'android.hardware.location.network'
uses-feature-not-required:'android.hardware.location.gps'
uses-feature-not-required:'android.hardware.screen.portrait'
uses-feature-not-required:'android.hardware.telephony'
uses-feature-not-required:'android.hardware.wifi'
uses-feature:'android.hardware.touchscreen'
uses-implied-feature:'android.hardware.touchscreen','assumed you require a touch screen unless explicitly made optional'

To understand the feature more detailed, please visit http://developer.android.com/guide/topics/manifest/uses-feature-element.html

###Others

Jar Mismatch! Fix Your Dependencies

There was a requirement of my work. It requires me to integrated my current project with Facebook SDK for measuring. However this came into my sights.

1
Jar mismatch! Fix your dependencies 

The fact is that Both my project and my library project which the former refers to have used android-support-v4.jar. However I realize the two android-support-v4.jar are different after making the md5 hash.
My solution:
I use the android-support-v4.jar in my project as the right one. And then replace the one in Facebook SDK with my project one. And then it works.
But my question remains; why it asks me for fix the dependencies to use the same lib jar file?
I guess android will keep only one file for all the references so this will ask developers to make all the same lib all the same. Sorry for the codeless post.

###Others

How to Create Facebook Key Hash

When I create a new application on Facebook, I meet the problem. Facebook asks me to provide the Key Hash. But it does not show the guidance about how to generate. After Googling, I have found this works for me.
Before executing the following command, you need install openssl

1
keytool -exportcert -alias your-alias-value -keystore your-keystore-path | openssl  sha1 -binary | openssl  base64

Replace your-alias-value and your-keystore-path with the real data.

###Others

Dump Table Structure in SQLite3

Best answer to the question
Use PRAGMA table_info

This pragma returns one row for each column in the named table. Columns in the result set include the column name, data type, whether or not the column can be NULL, and the default value for the column. The “pk” column in the result set is zero for columns that are not part of the primary key, and is the index of the column in the primary key for columns that are part of the primary key.

Reference:http://www.sqlite.org/pragma.html#pragma_table_info

1
2
3
4
5
6
7
8
sqlite> .header on
sqlite> PRAGMA table_info(password);
cid|name|type|notnull|dflt_value|pk
0|_id|INTEGER|0||1
1|host|TEXT|0||0
2|username|TEXT|0||0
3|password|TEXT|0||0
#password is the name of a table.

Another answer may also work
.schema [tablename] will show the CREATE statement(s) for a table or tables

1
2
sqlite> .schema password
CREATE TABLE password (_id INTEGER PRIMARY KEY, host TEXT, username TEXT, password TEXT, UNIQUE (host, username) ON CONFLICT REPLACE);

###Others

Check if a Python Module Is Installed

I was once stucked in How to check Whether a Python module has been installed or not. After Googling, I found this trick.
Python allows user to pass command from out of a python file.See here

1
-c cmd : program passed in as string (terminates option list)

The result if we import an installed module

1
2
3
4
20:15:45-androidyue~/osc_git/LnxClient (master)$ python -c "import os"
20:31:24-androidyue~/osc_git/LnxClient (master)$ echo $?
0
#0 means the module has been installed

Now if we import an module which is not installed.

1
2
3
4
5
6
7
20:31:41-androidyue~/osc_git/LnxClient (master)$ python -c "import aaa"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named aaa
20:31:46-androidyue~/osc_git/LnxClient (master)$ echo $?
1
#1 means that module is not installed.

###Others

Java Get Full Exception StackTrace

As a coder, I am always handling exceptions or errors,or in a word throwables. To impove the release build, I need to collect every throwable information.
And I need to get the information as string and post it to the Bug Collect Server. Now here is an easy trick to get stacktrace from a Throwable

1
2
3
4
5
6
7
private String getStackTrace(Throwable t) {

    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    t.printStackTrace(printWriter);
    return result.toString();
}

###Others

Visit Comet Browser