Month: November 2016

  • How to prevent screen capturing in Android

    How to prevent screen capturing in Android

    Prevent screen capturing is way for saving your content.

    Android SDK have special flag for it.
    https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SECURE

    Just declare it in your Activity like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        .....
    }

    And user will see next message when try make screenshot:

    2016-11-25-11-02-43

  • QuickSort (qsort) Algorithm

    QuickSort (qsort) Algorithm

    Overview

    The quicksort algorithm was discovered by Tony Hoare in 1959.

    This algorithm is considered one of the fastest sorting algorithms and shows the best results in most cases.

    Here is the algorithm visualization. Below I will describe how it works.

    The algorithm works on the “divide and conquer” principle.

    Worst case

    asymptotic complexity O(n^2)

    The answer depends on strategy for choosing pivot. In early versions of Quick Sort where leftmost (or rightmost) element is chosen as pivot, the worst occurs in following cases.

    1. Array is already sorted in same order.
    2. Array is already sorted in reverse order.
    3. All elements are same (special case of case 1 and 2)

    It can be fixed by choosing random pivot position

    Task

    Let’s see how the algorithm works on the example of a simple array of 12 numbers.

    [10, 5, 7, 4, 1, 2, 5, 7, 9, 8, 3, 6]

    Below I will describe the steps that will show how this algorithm works.

    Step 1.

    Select “Pivot”. This is the element that will split the array into two parts in the future. On the left side of the pivot, there will be elements that are less than the pivot, and on the right, more or equal. There are many methods for choosing a pivot, and the speed at which the array will be sorted depends on the choice of the pivot. One of the methods for selecting a pivot is to select the last element of the array. Another method is selecting random element, in this case the time complexity always will be O(n log n).

    We also need to create a variable “i”, which will point to the first element in the array, which is less than the pivot and at the same time is closest to it. We also need the index of the current element “j”

    In our case,

    • pivot = 6 (value of the last element in the array),
    • i = 0,
    • j = 0

    Step 2.

    Compare the array element with index j with the pivot. 10 > 6. Array elements do not need to be moved.
    Change the index “j”.
    j = j + 1 = 1

    Step 3.

    Compare the array element with index j with the pivot. 5 < 6. Swap the elements with indices i and j. Now the array looks like this
    [10, 5, 7, 4, 1, 2, 5, 7, 9, 8, 3, 6] –> [5, 10, 7, 4, 1, 2, 5, 7, 9, 8, 3, 6]
    Change the indexes
    i = i + 1 = 1
    j = j + 1 = 2

    Step 4.

    Compare the array element with index j with the pivot. 7 > 6. Array elements do not need to be moved.
    Change the index “j”.
    j = j + 1 = 3

    Step 5.

    Compare the array element with index j with the pivot. 4 < 6. Swap the elements with indices i and j. Now the array looks like this
    [5, 10, 7, 4, 1, 2, 5, 7, 9, 8, 3, 6] –> [5, 4, 7, 10, 1, 2, 5, 7, 9, 8, 3, 6]
    Change the indexes
    i = i + 1 = 2
    j = j + 1 = 4

    Step 6.

    Compare the array element with index j with the pivot. 1 < 6. Swap the elements with indices i and j. Now the array looks like this
    [5, 4, 7, 10, 1, 2, 5, 7, 9, 8, 3, 6] –> [5, 4, 1, 10, 7, 2, 5, 7, 9, 8, 3, 6]
    Change the indexes
    i = i + 1 = 3
    j = j + 1 = 5

    Step 7.

    Compare the array element with index j with the pivot. 2 < 6. Swap the elements with indices i and j. Now the array looks like this
    [5, 4, 1, 10, 7, 2, 5, 7, 9, 8, 3, 6] –> [5, 4, 1, 2, 7, 10, 5, 7, 9, 8, 3, 6]
    Change the indexes
    i = i + 1 = 4
    j = j + 1 = 6

    Step 8.

    Compare the array element with index j with the pivot. 5 < 6. Swap the elements with indices i and j. Now the array looks like this
    [5, 4, 1, 2, 7, 10, 5, 7, 9, 8, 3, 6] –> [5, 4, 1, 2, 5, 10, 7, 7, 9, 8, 3, 6]
    Change the indexes
    i = i + 1 = 5
    j = j + 1 = 7

    Step 9.

    Compare the array element with index j with the pivot. 7 > 6. Array elements do not need to be moved.
    Change the index “j”.
    j = j + 1 = 8

    Step 10.

    Compare the array element with index j with the pivot. 9 > 6. Array elements do not need to be moved.
    Change the index “j”.
    j = j + 1 = 9

    Step 11.

    Compare the array element with index j with the pivot. 8 > 6. Array elements do not need to be moved.
    Change the index “j”.
    j = j + 1 = 10

    Step 12.

    Compare the array element with index j with the pivot. 3 < 6. Swap the elements with indices i and j. Now the array looks like this
    [5, 4, 1, 2, 5, 10, 7, 7, 9, 8, 3, 6] –> [5, 4, 1, 2, 5, 3, 7, 7, 9, 8, 10, 6]
    Change the indexes
    i = i + 1 = 6
    j = j + 1 = 11

    Step 13.

    The “j” index is 11, which is the pivot index. We finish rearranging the elements in this iteration. Now we can split this array into 2 arrays. The left one will contain elements less than the pivot, and the right one is greater than or equal to the pivot. We leave the pivot in the middle between the arrays. It has already taken its place in the sorted array.
    The first element of the array in which all elements are greater than or equal to the pivot will be the element with index i. We transfer the pivot in the middle between the arrays.
    [5, 4, 1, 2, 5, 3] [6] [7, 7, 9, 8, 10]

    Then we sort the resulting 2 subarrays ([5, 4, 1, 2, 5, 3] and [7, 7, 9, 8, 10]) according to the above algorithm. We no longer use pivot “6”. Each subarray will have its own pivot

    To make it easier to write, I will describe the steps in the form of a table. You will be able to follow the steps and check the instructions above.

    Step 14-19.

    Sort the sub-array [5, 4, 1, 2, 5, 3]. pivot = 3; i = 0; j = 0

    CompareCurrent arrayProcedureResult arrayij
    5 > 3[5, 4, 1, 2, 5, 3]Do not swap elements.

    Increment “j”

    [5, 4, 1, 2, 5, 3]01
    4 > 3[5, 4, 1, 2, 5, 3]Do not swap elements.

    Increment “j”

    [5, 4, 1, 2, 5, 3] 02
    1 < 3[5, 4, 1, 2, 5, 3] Swap elements “i” and “j”.

    Increment “i” and “j”

    [1, 4, 5, 2, 5, 3]13
    2 < 3[1, 4, 5, 2, 5, 3]Swap elements “i” and “j”.

    Increment “i” and “j”

    [1, 2, 5, 4, 5, 3]24
    5 > 3[1, 2, 5, 4, 5, 3]Do not swap elements.

    Increment “j”

    [1, 2, 5, 4, 5, 3]25

    Step 20-24.

    Sort the sub-array [ 7, 7, 9, 8, 10]. pivot = 10; i = 0; j = 0

    CompareCurrent arrayProcedureResult arrayij
    7 > 10[7, 7, 9, 8, 10]Do not swap elements.

    Increment “j”

    [7, 7, 9, 8, 10]01
    7 > 10[7, 7, 9, 8, 10]Do not swap elements.

    Increment “j”

    [7, 7, 9, 8, 10]02
    9 > 10[7, 7, 9, 8, 10]Do not swap elements.

    Increment “j”

    [7, 7, 9, 8, 10]03
    8 > 10[7, 7, 9, 8, 10]Do not swap elements.

    Increment “j”

    [7, 7, 9, 8, 10]04

    We split the array. Now the general array looks like this:
    [1, 2] [3] [5, 4, 5] [6] [7, 7, 9, 8] [10]

    Then we sort the resulting 3 subarrays according to the above algorithm. It turned out 3 subarrays because in the second subarray ([7, 7, 9, 8, 10]) the pivot was the largest element in the subarray

    Step 25.

    Sort the sub-array [1, 2]. pivot = 2, i = 0, j = 0,

    CompareCurrent arrayProcedureResult arrayij
    1 < 2[1, 2]Swap elements “i” and “j”.

    Increment “i” and “j”

    [1, 2]01

    We split the array. Now the general array looks like this:
    [1] [2] [3] [5, 4, 5] [6] [7, 7, 9, 8] [10]

    Step 26-27.

    Sort the sub-array [5, 4, 5]. Pivot = 5, i = 0, j = 0,

    CompareCurrent arrayProcedureResult arrayij
    5 = 5[5, 4, 5]Do not swap elements.

    Increment “j”

    [5, 4, 5]01
    4 < 5[5, 4, 5]Swap elements “i” and “j”.

    Increment “i” and “j”

    [4, 5, 5]11

    Step 27-29.

    Sort the sub-array [7, 7, 9, 8]. pivot = 8, i = 0, j = 0,

    CompareCurrent arrayProcedureResult arrayij
    7 < 8[7, 7, 9, 8]Swap elements “i” and “j”.

    Increment “i” and “j”

    [7, 7, 9, 8]11
    7 < 8[7, 7, 9, 8]Swap elements “i” and “j”.

    Increment “i” and “j”

    [7, 7, 9, 8]22
    9 > 8[7, 7, 9, 8]Do not swap elements.

    Increment “j”

    [7, 7, 9, 8]23

    We split the array. Now the general array looks like this:
    [1] [2] [3] [4, 5] [5] [6] [7, 7] [8] [9] [10]

    Step 30.

    Sort the sub-array [4, 5]. pivot = 5, i = 0, j = 0,

    CompareCurrent arrayProcedureResult arrayij
    4 < 5[4, 5] Swap elements “i” and “j”.

    Increment “i” and “j”

    [4, 5] 11

    We split the subarray. Now the general array looks like this:
    [1] [2] [3] [4] [5] [5] [6] [7, 7] [8] [9] [10]

    Step 31.

    Sort the sub-array [7, 7]. pivot = 7, i = 0, j = 0,

    CompareCurrent arrayProcedureResult arrayij
    7 = 7[7, 7]Swap elements “i” and “j”.

    Increment “i” and “j”

    [7, 7] 11

    We split the subarray. Now the general array looks like this:
    [1] [2] [3] [4] [5] [5] [6] [7] [7] [8] [9] [10]
    The array sorting is finished.

    PlantUML diagram of the algorithm

    PlantUML Syntax:<br /> @startuml</p><p>start</p><p>partition “quickSort(minArrayIndex, maxArrayIndex)” {<br /> :set Pivot as last element;<br /> :set smallestElementIndex = minArrayIndex – 1;<br /> :set currentElementIndex = minArrayIndex;</p><p> repeat<br /> if (item at index currentElementIndex less or equal to Pivot) then (yes)<br /> :smallestElementIndex++;<br /> :swapElementsWithIndexes(currentElementIndex, smallestElementIndex);<br /> endif<br /> :currentElementIndex++;<br /> repeat while (currentElementIndex == pivot index) is (no)</p><p> :partitionElementIndex = smallestElementIndex+1;<br /> :swapElementsInArray(partitionElementIndex, pivotIndex);<br /> }</p><p>:quickSort(minArrayIndex, partitionElementIndex-1);<br /> :quickSort(partitionElementIndex+1, maxArrayIndex);</p><p>stop</p><p>@enduml<br /> ” usemap=”#plantuml_map”></p><h3 class=Sample code

    Below you will get link to github repo with Kotlin, Java, and Python code

    Download it from github
  • Insertion Sort Algorithm

    Insertion Sort Algorithm

    Insertion Sort appeared a long time ago. Intuitively, we use it to sort the cards in hand during card games. This is the easiest way to illustrate how this algorithm works.

    How the algorithm works

    Each element of the input sequence is scanned one at a time and compared with each element starting from the beginning of the list. The selected item takes its place in the list as soon as its place is found. And so with each element of the list.

    The time complexity of the algorithm is O(n^2), but on small amounts of data, the algorithm sometimes performs very well.

    Video shows how algorithm works

    Sequence of operations

    For example, let’s take the list [5, 3, 1, 6, 4].

    We take the first item from the list. This is “5”. If it were a list of one element, then we would say that a list of one element is always sorted.

    Moving on to the next item. This is “3”. Where should be “3” relative to “5”. Answer: before “5”. To do this, we must put “3” in front of “5”, and move “5”.

    Now the list has taken the following form [3, 5, 1, 6, 4]. We can now say that the sublist [3, 5] is sorted.

    Take the next item. This is “1”. Compare with the elements in front of it.
    5 > 1, 3 > 1.
    Obviously, “1” must come before “5” and “3”.

    Let’s move the sublist [3, 5] forward and put “1” in front of the sublist.

    Now the list has taken the following form [1, 3, 5, 6, 4]. We can now say that the sublist [1, 3, 5] is sorted.

    Take the next item. This is “6”. Compare with the elements in front of it.
    5 <6.
    It makes no sense to check further since the sublist [1, 3, 5] was sorted. Item “6” remains in place.

    Now the list has taken the following form [1, 3, 5, 6, 4]. We can now say that the sublist [1, 3, 5, 6] is sorted.

    Take the next item. This is “4”. Compare with the elements in front of it.
    6 > 4, 5 > 4, 3 < 4.
    There is no point in checking further since the sublist [1, 3, 5, 6] has been sorted. Obviously, “4” must come before “6”, “5”, but before “3”.

    Let’s move the sublist [5, 6] forward, and put “4” in front of the sublist.

    Now the list has taken the following form [1, 3, 4, 5, 6]. We have reached the end of the list and we can say that the list is all sorted from the original list.

    Diagram

    PlantUML Syntax:<br /> @startuml</p><p>start</p><p>while (End of array?) is (no)<br /> :Save Active Index;<br /> while (Previous element bigger than next element?) is (yes)<br /> :Swap elements;<br /> endwhile (no)<br /> endwhile (yes)<br /> :Finish. Array Sorted;</p><p>stop</p><p>@enduml<br /> ” usemap=”#plantuml_map”></p><h2 class=Sample code

    Below you will get a link to GitHub repo with Kotlin, Java, and Python source code

    Download it from github
  • Android Keyboard. LatinIME gradle module

    Android Keyboard. LatinIME gradle module

    My application requires Android keyboard.
    I’ve got LatinIME from AOSP and made gradle module.

    LatinIME support Android Accessibility and works perfect with TalkBack.

    Here is screenshots:

    screenshot_2016-11-17-00-49-59

    screenshot_2016-11-17-00-50-03

     

     

     

     

     

     

    Here is link for my github repo:

    Download it from github

  • How to fix “undefined reference to ‘__atomic_exchange_4”

    How to fix “undefined reference to ‘__atomic_exchange_4”

    When I built native library for LatinIME I’ve got next error:

    undefined reference to ‘__atomic_exchange_4

    Full stacktrace:

    /Volumes/Android/buildbot/src/android/ndk-r13-release/external/libcxx/include/memory:635: error: undefined reference to '__atomic_load_4'
    /Volumes/Android/buildbot/src/android/ndk-r13-release/external/libcxx/../../external/libcxxabi/src/cxa_handlers.cpp:112: error: undefined reference to '__atomic_exchange_4'
    /Volumes/Android/buildbot/src/android/ndk-r13-release/external/libcxx/../../external/libcxxabi/src/cxa_default_handlers.cpp:106: error: undefined reference to '__atomic_exchange_4'
    /Volumes/Android/buildbot/src/android/ndk-r13-release/external/libcxx/../../external/libcxxabi/src/cxa_default_handlers.cpp:117: error: undefined reference to '__atomic_exchange_4'
    clang++: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [obj/local/armeabi/libjni_latinime.so] Error 1

    To avoid it open Android.mk file and add next string:

    LOCAL_EXPORT_LDLIBS += -latomic
  • Build native libs for LatinIME. Error: base class ‘class latinime::NgramListener’ should be explicitly initialized in the copy constructor [-Werror=extra]

    Build native libs for LatinIME. Error: base class ‘class latinime::NgramListener’ should be explicitly initialized in the copy constructor [-Werror=extra]

    I working on LatinIME Android Keyboard keyboard.

    I’ve got source from Google repo.
    Here is structure:

    java – contains java code
    native – jni code.

    When I try to build native I receive next error:

    'latinime::MultiBigramMap::BigramMap::BigramMap(const latinime::MultiBigramMap::BigramMap&amp;amp;)':
    jni/src/suggest/core/dictionary/multi_bigram_map.h:56:9: error: base class 'class latinime::NgramListener' should be explicitly initialized in the copy constructor [-Werror=extra]

    Full stacktrace:

    ➜ native ndk-build
    [arm64-v8a] Compile++ : jni_latinime_common_static &lt;= com_android_inputmethod_keyboard_ProximityInfo.cpp
    [arm64-v8a] Compile++ : jni_latinime_common_static &lt;= com_android_inputmethod_latin_BinaryDictionary.cpp
    [arm64-v8a] Compile++ : jni_latinime_common_static &lt;= com_android_inputmethod_latin_BinaryDictionaryUtils.cpp
    [arm64-v8a] Compile++ : jni_latinime_common_static &lt;= com_android_inputmethod_latin_DicTraverseSession.cpp
    In file included from jni/src/suggest/core/session/dic_traverse_session.h:25:0,
    from jni/com_android_inputmethod_latin_DicTraverseSession.cpp:24:
    jni/src/suggest/core/dictionary/multi_bigram_map.h: In copy constructor 'latinime::MultiBigramMap::BigramMap::BigramMap(const latinime::MultiBigramMap::BigramMap&amp;)':
    jni/src/suggest/core/dictionary/multi_bigram_map.h:56:9: error: base class 'class latinime::NgramListener' should be explicitly initialized in the copy constructor [-Werror=extra]
    BigramMap(const BigramMap &amp;bigramMap)
    ^
    cc1plus: all warnings being treated as errors
    make: *** [obj/local/arm64-v8a/objs/jni_latinime_common_static/com_android_inputmethod_latin_DicTraverseSession.o] Error 1

    To avoid it I’ve edited Android.mk in jni dir and replace:

    LOCAL_CFLAGS += -Werror -Wall -Wextra -Weffc++ -Wformat=2 -Wcast-qual -Wcast-align \
        -Wwrite-strings -Wfloat-equal -Wpointer-arith -Winit-self -Wredundant-decls \
        -Woverloaded-virtual -Wsign-promo -Wno-system-headers

    with

    LOCAL_CFLAGS += -w

    It is not remove the code problem, but you able to build .so lib