├── .gitignore ├── README.md ├── android-extend ├── .gitignore ├── android-extend.iml ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── jni │ ├── Android.mk │ ├── Application.mk │ ├── CMakeLists.txt │ ├── cache │ │ ├── Android.mk │ │ ├── CMakeLists.txt │ │ └── src │ │ │ ├── CMakeLists.txt │ │ │ └── android │ │ │ └── cache_adapter.cpp │ ├── cmake │ │ ├── Findndk-stl.cmake │ │ ├── findInclude.cmake │ │ └── findPlatform.cmake │ ├── commen │ │ ├── Android.mk │ │ ├── cache │ │ │ ├── cache.c │ │ │ ├── cache.h │ │ │ ├── cache_data.c │ │ │ ├── cache_data.h │ │ │ ├── dllist-test.c │ │ │ ├── dllist.c │ │ │ ├── dllist.h │ │ │ ├── rbtree-test.c │ │ │ ├── rbtree.c │ │ │ └── rbtree.h │ │ ├── device │ │ │ ├── Android.mk │ │ │ ├── device.c │ │ │ └── device.h │ │ ├── image │ │ │ ├── image.c │ │ │ └── image.h │ │ ├── logger │ │ │ ├── Android.mk │ │ │ ├── logger.cpp │ │ │ └── logger.h │ │ └── render │ │ │ ├── Matrix.cpp │ │ │ ├── Matrix.h │ │ │ ├── comm_render.cpp │ │ │ ├── comm_render.h │ │ │ ├── image_render.cpp │ │ │ ├── image_render.h │ │ │ └── shader.h │ ├── image │ │ ├── Android.mk │ │ ├── CMakeLists.txt │ │ └── src │ │ │ ├── CMakeLists.txt │ │ │ └── android │ │ │ └── image_adapter.cpp │ ├── jni.ver │ ├── render │ │ ├── Android.mk │ │ ├── CMakeLists.txt │ │ └── src │ │ │ ├── CMakeLists.txt │ │ │ └── android │ │ │ └── render_adapter.cpp │ ├── serial │ │ ├── Android.mk │ │ ├── CMakeLists.txt │ │ └── src │ │ │ ├── CMakeLists.txt │ │ │ └── android │ │ │ └── serial_adapter.cpp │ └── video │ │ ├── Android.mk │ │ ├── CMakeLists.txt │ │ └── src │ │ ├── CMakeLists.txt │ │ └── android │ │ └── video_adapter.cpp │ ├── proguard-project.txt │ ├── project.properties │ ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ ├── bg_checkbox_box.png │ │ ├── bg_checkbox_checker.png │ │ ├── bg_seekbar_progress.png │ │ ├── bg_seekbar_secondary.png │ │ ├── bg_seekbar_thumb.png │ │ ├── bg_vertical_seekbar_progress.png │ │ ├── bg_vertical_seekbar_secondary.png │ │ ├── bg_vertical_seekbar_thumb.png │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── drawable │ │ ├── bgxml_checkbox.xml │ │ ├── checkbox.xml │ │ └── progress.xml │ ├── values-v17 │ │ └── styles.xml │ └── values │ │ ├── attrs.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── src │ └── com │ └── guo │ └── android_extend │ ├── GLES2Render.java │ ├── cache │ ├── BitmapCache.java │ ├── BitmapMonitor.java │ ├── BitmapMonitorThread.java │ └── BitmapStructure.java │ ├── device │ ├── Serial.java │ ├── SerialClient.java │ ├── SerialInterface.java │ ├── SerialListener.java │ ├── Video.java │ └── VideoClient.java │ ├── image │ └── ImageConverter.java │ ├── java │ ├── AbsLoop.java │ ├── ExtByteArrayOutputStream.java │ ├── ExtByteTools.java │ ├── ExtInputStream.java │ ├── ExtOutputStream.java │ ├── LRULinkedHashMap.java │ └── network │ │ ├── NetWorkFile.java │ │ ├── http │ │ ├── Downloader.java │ │ └── DownloaderManager.java │ │ ├── socket │ │ ├── Data │ │ │ ├── AbsTransmitter.java │ │ │ ├── TransmitByte.java │ │ │ └── TransmitFile.java │ │ ├── OnSocketListener.java │ │ ├── SocketClient.java │ │ ├── SocketModule.java │ │ ├── SocketServer.java │ │ └── Transfer │ │ │ ├── Receiver.java │ │ │ └── Sender.java │ │ └── udp │ │ ├── UDPDataProtocol.java │ │ ├── UDPModule.java │ │ └── UDPTransponder.java │ ├── tools │ ├── AssetsHelper.java │ ├── CameraHelper.java │ ├── DumpHelper.java │ ├── FrameHelper.java │ ├── LogcatHelper.java │ └── SensorHelper.java │ └── widget │ ├── AbsHAdapterView.java │ ├── Camera2GLSurfaceView.java │ ├── Camera2Manager.java │ ├── CameraFrameData.java │ ├── CameraGLSurfaceView.java │ ├── CameraSurfaceView.java │ ├── ExtGLSurfaceView.java │ ├── ExtImageButton.java │ ├── ExtImageView.java │ ├── ExtOrientationDetector.java │ ├── ExtRelativeLayout.java │ ├── ExtSurfaceView.java │ ├── GestureDetector.java │ ├── HListView.java │ ├── RotateRunable.java │ ├── VerticalSeekBar.java │ ├── controller │ ├── AbstractController.java │ ├── ImageController.java │ └── TouchController.java │ └── effective │ ├── HCSEffectAdapter.java │ ├── HCSGestureDetector.java │ └── HCSListView.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jitpack.yml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | ############# 2 | ## Android studio 3 | ############# 4 | .gradle 5 | .idea 6 | 7 | ################# 8 | ## Eclipse 9 | ################# 10 | 11 | *.pydevproject 12 | .project 13 | .metadata 14 | bin/ 15 | tmp/ 16 | gen/ 17 | *.tmp 18 | *.bak 19 | *.swp 20 | *~.nib 21 | local.properties 22 | jcenter.properties 23 | .classpath 24 | .settings/ 25 | .loadpath 26 | *.sln 27 | *.vcxproj* 28 | *.o 29 | *.d 30 | *.so 31 | #*.mk 32 | client* 33 | asnode 34 | # External tool builders 35 | .externalToolBuilders/ 36 | 37 | # Locally stored "Eclipse launch configurations" 38 | *.launch 39 | 40 | # CDT-specific 41 | .cproject 42 | 43 | # PDT-specific 44 | .buildpath 45 | 46 | 47 | ################# 48 | ## Visual Studio 49 | ################# 50 | 51 | ## Ignore Visual Studio temporary files, build results, and 52 | ## files generated by popular Visual Studio add-ons. 53 | 54 | # User-specific files 55 | *.suo 56 | *.user 57 | *.sln.docstates 58 | 59 | # Build results 60 | [Dd]ebug/ 61 | [Rr]elease/ 62 | x64/ 63 | build/ 64 | [Bb]in/ 65 | [Oo]bj/ 66 | 67 | # MSTest test Results 68 | [Tt]est[Rr]esult*/ 69 | [Bb]uild[Ll]og.* 70 | 71 | *_i.c 72 | *_p.c 73 | *.ilk 74 | *.meta 75 | *.obj 76 | *.pch 77 | *.pdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *.log 88 | *.vspscc 89 | *.vssscc 90 | .builds 91 | *.pidb 92 | *.log 93 | *.scc 94 | 95 | # Visual C++ cache files 96 | ipch/ 97 | *.aps 98 | *.ncb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | 103 | # Visual Studio profiler 104 | *.psess 105 | *.vsp 106 | *.vspx 107 | 108 | # Guidance Automation Toolkit 109 | *.gpState 110 | 111 | # ReSharper is a .NET coding add-in 112 | _ReSharper*/ 113 | *.[Rr]e[Ss]harper 114 | 115 | # TeamCity is a build add-in 116 | _TeamCity* 117 | 118 | # DotCover is a Code Coverage Tool 119 | *.dotCover 120 | 121 | # NCrunch 122 | *.ncrunch* 123 | .*crunch*.local.xml 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.Publish.xml 143 | *.pubxml 144 | 145 | # NuGet Packages Directory 146 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 147 | #packages/ 148 | 149 | # Windows Azure Build Output 150 | csx 151 | *.build.csdef 152 | 153 | # Windows Store app package directory 154 | AppPackages/ 155 | 156 | # Others 157 | sql/ 158 | *.Cache 159 | ClientBin/ 160 | [Ss]tyle[Cc]op.* 161 | ~$* 162 | *~ 163 | *.dbmdl 164 | *.[Pp]ublish.xml 165 | *.pfx 166 | *.publishsettings 167 | 168 | # RIA/Silverlight projects 169 | Generated_Code/ 170 | 171 | # Backup & report files from converting an old project file to a newer 172 | # Visual Studio version. Backup files are not needed, because we have git ;-) 173 | _UpgradeReport_Files/ 174 | Backup*/ 175 | UpgradeLog*.XML 176 | UpgradeLog*.htm 177 | 178 | # SQL Server files 179 | App_Data/*.mdf 180 | App_Data/*.ldf 181 | 182 | ############# 183 | ## Windows detritus 184 | ############# 185 | 186 | # Windows image file caches 187 | Thumbs.db 188 | ehthumbs.db 189 | 190 | # Folder config file 191 | Desktop.ini 192 | 193 | # Recycle Bin used on file shares 194 | $RECYCLE.BIN/ 195 | 196 | # Mac crap 197 | .DS_Store 198 | 199 | 200 | ############# 201 | ## Python 202 | ############# 203 | 204 | *.py[co] 205 | 206 | # Packages 207 | *.egg 208 | *.egg-info 209 | dist/ 210 | build/ 211 | eggs/ 212 | parts/ 213 | var/ 214 | sdist/ 215 | develop-eggs/ 216 | .installed.cfg 217 | 218 | # Installer logs 219 | pip-log.txt 220 | 221 | # Unit test / coverage reports 222 | .coverage 223 | .tox 224 | 225 | #Translations 226 | *.mo 227 | 228 | #Mr Developer 229 | .mr.developer.cfg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # android-extend 2 | create useful widget for android. 3 | 4 | ![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg) 5 | [![Build Status](https://travis-ci.com/gqjjqg/android-extend.svg?branch=master)](https://travis-ci.com/gqjjqg/android-extend) 6 | [![](https://jitpack.io/v/gqjjqg/android-extend.svg)](https://jitpack.io/#gqjjqg/android-extend) 7 | 8 | ## Getting Started 9 | [Widget List](http://gqjjqg.github.io/development/project/2014/06/16/android-widget-extend.html) | 10 | [Sample Code](https://github.com/gqjjqg/samples) 11 | 12 | Gradle Usage: 13 | 14 | ``` 15 | dependencies { 16 | implementation 'com.github.gqjjqg:android-extend:v1.0.7' 17 | } 18 | ``` 19 | 20 | 21 | 1. 横向listview、旋转按钮、扩展imageview 等自定义控件 22 | 2. camera view,支持修改图像数据和手动渲染数据 23 | 3. 串口ttySx的读写 24 | 4. video的V4L2接口 25 | 5. 常用的图像格式转换 26 | 6. 网络通信 27 | 28 | 29 | ## Communication 30 | 31 | 32 | 33 | ## Contributing 34 | 35 | ## License 36 | 37 | ## Acknowledgement 38 | 39 | ## Donate 40 | -------------------------------------------------------------------------------- /android-extend/.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | .externalNativeBuild 3 | .cxx -------------------------------------------------------------------------------- /android-extend/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.library' 3 | } 4 | 5 | android { 6 | compileSdk 31 7 | 8 | defaultConfig { 9 | minSdkVersion 11 10 | targetSdkVersion 26 11 | 12 | sourceSets.main { 13 | //jniLibs.srcDirs = ['src/main/jniLibs'] 14 | //jni.srcDirs = ['src/main/jni'] 15 | //jni.srcDirs = [] 16 | java.srcDirs = ['src/main/src'] 17 | } 18 | externalNativeBuild { 19 | cmake { 20 | //targets "stereoscan","mocap" 21 | arguments "-DANDROID_STL=system" //system stlport_static stlport_shared gnustl_static gnustl_shared 22 | //cppFlags "-std=c++11" 23 | abiFilters "arm64-v8a" //,"armeabi-v7a" 24 | } 25 | } 26 | consumerProguardFiles "consumer-rules.pro" 27 | } 28 | externalNativeBuild { 29 | cmake { 30 | path file('src/main/jni/CMakeLists.txt') 31 | } 32 | } 33 | buildTypes { 34 | release { 35 | minifyEnabled false 36 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 37 | } 38 | } 39 | compileOptions { 40 | sourceCompatibility JavaVersion.VERSION_1_8 41 | targetCompatibility JavaVersion.VERSION_1_8 42 | } 43 | } 44 | 45 | dependencies { 46 | implementation fileTree(include: ['*.jar'], dir: 'libs') 47 | } -------------------------------------------------------------------------------- /android-extend/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/consumer-rules.pro -------------------------------------------------------------------------------- /android-extend/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /android-extend/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /android-extend/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /android-extend/src/main/jni/Android.mk: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2009 The Android Open Source Project 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | include $(call all-subdir-makefiles) 16 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/Application.mk: -------------------------------------------------------------------------------- 1 | #APP_STL := stlport_shared 2 | APP_PLATFORM := android-10 3 | APP_ABI := armeabi armeabi-v7a -------------------------------------------------------------------------------- /android-extend/src/main/jni/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(SDK_LIB) 2 | 3 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 4 | 5 | #SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}) 6 | SET(CMAKE_FIND_ROOT_PATH ${PROJECT_SOURCE_DIR}) 7 | 8 | IF (${CMAKE_BUILD_TYPE} MATCHES "arm64-v8a") 9 | ADD_DEFINITIONS(-DINT64) 10 | ENDIF() 11 | 12 | ADD_SUBDIRECTORY(cache) 13 | ADD_SUBDIRECTORY(image) 14 | ADD_SUBDIRECTORY(render) 15 | ADD_SUBDIRECTORY(serial) 16 | ADD_SUBDIRECTORY(video) 17 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/cache/Android.mk: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2009 The Android Open Source Project 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | LOCAL_PATH := $(call my-dir) 16 | 17 | include $(CLEAR_VARS) 18 | 19 | LOCAL_MODULE := cache 20 | LOCAL_SRC_FILES := src\android\cache_adapter.cpp \ 21 | ..\commen\cache\rbtree.c \ 22 | ..\commen\cache\dllist.c \ 23 | ..\commen\cache\cache_data.c \ 24 | ..\commen\cache\cache.c 25 | LOCAL_C_INCLUDES := $(LOCAL_PATH)\..\commen\cache 26 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) 27 | LOCAL_LDLIBS := -llog -ljnigraphics 28 | LOCAL_STATIC_LIBRARIES := imagebase logger 29 | LOCAL_EXPORT_LDLIBS := $(LOCAL_LDLIBS) 30 | include $(BUILD_SHARED_LIBRARY) 31 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/cache/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(cache) 2 | 3 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 4 | 5 | ADD_DEFINITIONS(-DBUILD_NUMBER=44) 6 | 7 | ADD_SUBDIRECTORY(src) -------------------------------------------------------------------------------- /android-extend/src/main/jni/cache/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | INCLUDE(${PROJECT_SOURCE_DIR}/../cmake/findInclude.cmake) 2 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${PROJECT_SOURCE_DIR}) 3 | 4 | FIND_PATH(LOGGER_INCLUDE_DIR 5 | NAMES "logger.h" 6 | PATHS "${PROJECT_SOURCE_DIR}/../commen/logger" 7 | NO_DEFAULT_PATH) 8 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${LOGGER_INCLUDE_DIR}) 9 | 10 | FIND_PATH(IMAGE_INCLUDE_DIR 11 | NAMES "image.h" 12 | PATHS "${PROJECT_SOURCE_DIR}/../commen/image" 13 | NO_DEFAULT_PATH) 14 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${IMAGE_INCLUDE_DIR}) 15 | 16 | FIND_PATH(CACHE_INCLUDE_DIR 17 | NAMES "cache.h" 18 | PATHS "${PROJECT_SOURCE_DIR}/../commen/cache" 19 | NO_DEFAULT_PATH) 20 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${CACHE_INCLUDE_DIR}) 21 | 22 | FILE(GLOB_RECURSE LIB_SRC "*.cpp" "*.c") 23 | 24 | #LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib) 25 | 26 | SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake) 27 | #SET(CMAKE_FIND_ROOT_PATH ${PROJECT_SOURCE_DIR}/cmake) 28 | SET(allLibs) 29 | #SET(ndk-stl_DIR ${PROJECT_SOURCE_DIR}/cmake/modules) 30 | FIND_PACKAGE(ndk-stl REQUIRED) 31 | 32 | FILE(GLOB LOGGER_SRC "${LOGGER_INCLUDE_DIR}/*.cpp" "${LOGGER_INCLUDE_DIR}/*.c" ) 33 | FILE(GLOB IMAGE_SRC "${IMAGE_INCLUDE_DIR}/*.cpp" "${IMAGE_INCLUDE_DIR}/*.c" ) 34 | FILE(GLOB CACHE_SRC "${CACHE_INCLUDE_DIR}/*.cpp" "${CACHE_INCLUDE_DIR}/*.c" ) 35 | SET(LIB_SRC ${LIB_SRC} ${LOGGER_SRC} ${IMAGE_SRC} ${CACHE_SRC}) 36 | MESSAGE("module cache:${LIB_SRC}") 37 | 38 | IF (${CMAKE_BUILD_TYPE} MATCHES "Release") 39 | MESSAGE("Release SET jni.ver") 40 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 41 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 42 | ENDIF() 43 | 44 | ADD_LIBRARY(cache SHARED ${LIB_SRC}) 45 | TARGET_LINK_LIBRARIES(cache log android jnigraphics ${allLibs}) 46 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/cmake/Findndk-stl.cmake: -------------------------------------------------------------------------------- 1 | # Copy shared STL files to Android Studio output directory so they can be 2 | # packaged in the APK. 3 | # Usage: 4 | # 5 | # find_package(ndk-stl REQUIRED) 6 | # 7 | # or 8 | # 9 | # find_package(ndk-stl REQUIRED PATHS ".") 10 | 11 | if (NOT ANDROID_STL) 12 | message("not define ANDROID_STL") 13 | return() 14 | endif() 15 | 16 | if(NOT ${ANDROID_STL} MATCHES "_shared") 17 | message("not match _shared ${ANDROID_STL}") 18 | return() 19 | endif() 20 | 21 | function(configure_shared_stl lib_path so_base) 22 | message("Configuring STL ${so_base} for ${ANDROID_ABI}") 23 | configure_file( 24 | "${ANDROID_NDK}/sources/cxx-stl/${lib_path}/libs/${ANDROID_ABI}/lib${so_base}.so" 25 | "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${so_base}.so" 26 | COPYONLY) 27 | endfunction() 28 | 29 | if("${ANDROID_STL}" STREQUAL "system") #libstdc++ 30 | # The default minimal system C++ runtime library. 31 | elseif("${ANDROID_STL}" STREQUAL "gabi++_shared") 32 | # The GAbi++ runtime (shared). 33 | # message(FATAL_ERROR "gabi++_shared was not configured by ndk-stl package") 34 | elseif("${ANDROID_STL}" STREQUAL "stlport_shared") 35 | # The STLport runtime (shared). 36 | configure_shared_stl("stlport" "stlport_shared") 37 | elseif("${ANDROID_STL}" STREQUAL "gnustl_shared") 38 | # The GNU STL (shared). 39 | configure_shared_stl("gnu-libstdc++/4.9" "gnustl_shared") 40 | elseif("${ANDROID_STL}" STREQUAL "c++_shared") 41 | # The LLVM libc++ runtime (static). 42 | configure_shared_stl("llvm-libc++" "c++_shared") 43 | else() 44 | message(FATAL_ERROR "STL configuration ANDROID_STL=${ANDROID_STL} is not supported") 45 | endif() -------------------------------------------------------------------------------- /android-extend/src/main/jni/cmake/findInclude.cmake: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/jni/cmake/findInclude.cmake -------------------------------------------------------------------------------- /android-extend/src/main/jni/cmake/findPlatform.cmake: -------------------------------------------------------------------------------- 1 | FUNCTION(GET_SYSTEM_INFO os arch) 2 | IF (CMAKE_SYSTEM_NAME MATCHES "Linux") 3 | MESSAGE(STATUS "current platform: Linux ") 4 | SET (${os} "linux" PARENT_SCOPE) 5 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows") 6 | MESSAGE(STATUS "current platform: Windows") 7 | SET (${os} "windows" PARENT_SCOPE) 8 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD") 9 | MESSAGE(STATUS "current platform: FreeBSD") 10 | ELSE () 11 | MESSAGE(STATUS "other platform: ${CMAKE_SYSTEM_NAME}") 12 | ENDIF () 13 | 14 | IF (CMAKE_SIZEOF_VOID_P MATCHES "8") 15 | MESSAGE(STATUS "current arch: x64") 16 | SET (${arch} "x64" PARENT_SCOPE) 17 | ELSEIF (CMAKE_SIZEOF_VOID_P MATCHES "4") 18 | MESSAGE(STATUS "current arch: x86") 19 | SET (${arch} "x86" PARENT_SCOPE) 20 | ENDIF() 21 | ENDFUNCTION() -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/Android.mk: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2009 The Android Open Source Project 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | include $(call all-subdir-makefiles) 16 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/cache/cache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * cache.h 3 | * 4 | * Created on: 2014年11月17日 5 | * Author: gqj3375 6 | */ 7 | 8 | #ifndef CACHE_H_ 9 | #define CACHE_H_ 10 | 11 | #define GOK 0 12 | #define NOT_FIND -1 13 | #define NOT_SUPPORT -2 14 | #define PARAM_INVALID -3 15 | 16 | #ifndef INT64 17 | #define INT unsigned long 18 | #else 19 | #define INT unsigned long long 20 | #endif 21 | 22 | #ifdef __cplusplus 23 | extern "C"{ 24 | #endif 25 | 26 | INT CreateCache(int size); 27 | int PushCache(INT handle, int hash, int width, int height, int format, unsigned char * data); 28 | int QueryCache(INT handle, int hash, int *width, int *height, int *format); 29 | int PullCache(INT handle, int hash, int *width, int *height, int *format, unsigned char ** data); 30 | int ReleaseCache(INT handle); 31 | 32 | #ifdef __cplusplus 33 | } 34 | #endif 35 | 36 | #endif /* CACHE_H_ */ 37 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/cache/cache_data.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "cache_data.h" 4 | 5 | #define GMemMalloc malloc 6 | #define GMemFree free 7 | #define GNull NULL 8 | 9 | static int getDataSize(int width, int height, int format); 10 | 11 | int cache_data_update(LPDATA pData, int width, int height, int format, unsigned char * data) 12 | { 13 | int size = getDataSize(width, height, format); 14 | int old_size = getDataSize(pData->mWidth, pData->mHeight, pData->mFormat); 15 | pData->mFormat = format; 16 | pData->mHeight = height; 17 | pData->mWidth = width; 18 | 19 | if (pData->pByte != GNull) { 20 | if (size != old_size) { 21 | GMemFree(pData->pByte); 22 | if (0 != size) { 23 | pData->pByte = (unsigned char *)GMemMalloc(size); 24 | memcpy(pData->pByte, data, size); 25 | } 26 | } else { 27 | memcpy(pData->pByte, data, size); 28 | } 29 | } else { 30 | if (0 != size) { 31 | pData->pByte = (unsigned char *)GMemMalloc(size); 32 | memcpy(pData->pByte, data, size); 33 | } 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | int cache_data_initial(LPDATA pData) 40 | { 41 | pData->mFormat = 0; 42 | pData->mHeight = 0; 43 | pData->mWidth = 0; 44 | pData->pByte = GNull; 45 | return 0; 46 | } 47 | 48 | int cache_data_parse(LPDATA pData, int *width, int *height, int *format, unsigned char ** data) 49 | { 50 | *width = pData->mWidth; 51 | *height = pData->mHeight; 52 | *format = pData->mFormat; 53 | if (data != GNull) { 54 | *data = pData->pByte; 55 | } 56 | return 0; 57 | } 58 | 59 | int cache_data_release(LPDATA pData) 60 | { 61 | if (pData->pByte != GNull) { 62 | GMemFree(pData->pByte); 63 | } 64 | return 0; 65 | } 66 | 67 | int getDataSize(int width, int height, int format) 68 | { 69 | int size; 70 | switch (format) { 71 | case 1: //ANDROID_BITMAP_FORMAT_RGBA_8888 : 72 | size = width * height * 4; 73 | break; 74 | case 4: //ANDROID_BITMAP_FORMAT_RGB_565 : 75 | case 7: //ANDROID_BITMAP_FORMAT_RGBA_4444: 76 | size = width * height * 2; 77 | break; 78 | case 8: //ANDROID_BITMAP_FORMAT_A_8 : 79 | size = width * height; 80 | break; 81 | default :size = 0; 82 | } 83 | return size; 84 | } 85 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/cache/cache_data.h: -------------------------------------------------------------------------------- 1 | #ifndef CACHE_DATA_H_ 2 | #define CACHE_DATA_H_ 3 | 4 | typedef struct DATA_t { 5 | int mWidth; 6 | int mHeight; 7 | int mFormat; 8 | unsigned char * pByte; 9 | }DATA, *LPDATA; 10 | 11 | int cache_data_initial(LPDATA pData); 12 | int cache_data_update(LPDATA pData, int width, int height, int format, unsigned char * data); 13 | int cache_data_parse(LPDATA pData, int *width, int *height, int *format, unsigned char ** data); 14 | int cache_data_release(LPDATA pData); 15 | 16 | #endif -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/cache/dllist-test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================= 3 | * 4 | * Filename: dllist-test.c 5 | * 6 | * Description: dllist testcase. 7 | * 8 | * Created: 11/30/2014 11:39:34 PM 9 | * 10 | * Author: Guo QiJiang (gqjjqg), qijiang.guo@gmail.com 11 | * Company: 12 | * 13 | * ============================================================================= 14 | */ 15 | 16 | #include "dllist.h" 17 | //#include "CDebug.h" 18 | #include 19 | #include 20 | #include 21 | 22 | 23 | #if defined(offsetof) 24 | #undef offsetof 25 | #endif 26 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 27 | 28 | #if defined(container_of) 29 | #undef container_of 30 | #endif 31 | #define container_of(res, ptr, type, member) { \ 32 | unsigned long address = (unsigned long)(ptr); \ 33 | res = (type *)( address - offsetof(type,member) ); } 34 | 35 | struct mynode { 36 | DL_NODE node; 37 | char *string; 38 | }; 39 | 40 | #define Debug_Message(x, ...) printf(__VA_ARGS__) 41 | #define LOG_TEST 42 | #define LOG_FULL 43 | 44 | static DL_ROOT mytree = {0,0}; 45 | 46 | struct mynode * dll_search(LPDL_ROOT root, char *string) 47 | { 48 | LPDL_NODE node = dl_first(root); 49 | 50 | while (node) { 51 | struct mynode *data; 52 | int result; 53 | container_of(data, node, struct mynode, node); 54 | result = strcmp(string, data->string); 55 | 56 | if (result != 0) { 57 | node = node->dl_next; 58 | } else { 59 | return data; 60 | } 61 | } 62 | return NULL; 63 | } 64 | 65 | int dll_insert(LPDL_ROOT root, struct mynode *node) 66 | { 67 | LPDL_NODE last = dl_last(root); 68 | 69 | dl_insert_node(&(node->node), last, root); 70 | 71 | return 0; 72 | } 73 | 74 | void dll_free(struct mynode *node) 75 | { 76 | if (node != NULL) { 77 | if (node->string != NULL) { 78 | free(node->string); 79 | node->string = NULL; 80 | } 81 | free(node); 82 | node = NULL; 83 | } 84 | } 85 | 86 | #define NUM_NODES 32 87 | 88 | int dllist_test() 89 | { 90 | 91 | struct mynode *mn[NUM_NODES]; 92 | LPDL_NODE node; 93 | struct mynode *data; 94 | 95 | /* *insert */ 96 | int i = 0; 97 | Debug_Message(LOG_FULL, "insert node from 1 to NUM_NODES(32): \n"); 98 | for (; i < NUM_NODES; i++) { 99 | mn[i] = (struct mynode *)malloc(sizeof(struct mynode)); 100 | mn[i]->string = (char *)malloc(sizeof(char) * 4); 101 | sprintf(mn[i]->string, "%d", i); 102 | dll_insert(&mytree, mn[i]); 103 | } 104 | 105 | /* *search */ 106 | Debug_Message(LOG_FULL, "search all nodes: \n"); 107 | for (node = dl_first(&mytree); node; node = dl_next(node)) { 108 | struct mynode *data; 109 | container_of(data, node, struct mynode, node); 110 | Debug_Message(LOG_TEST, "key = %s\n", data->string); 111 | } 112 | 113 | /* *delete */ 114 | Debug_Message(LOG_FULL, "delete node 0: \n"); 115 | data = dll_search(&mytree, "0"); 116 | if (data) { 117 | dl_remove_node(&data->node, &mytree); 118 | dll_free(data); 119 | } 120 | 121 | /* *delete again*/ 122 | Debug_Message(LOG_FULL, "delete node 10: \n"); 123 | data = dll_search(&mytree, "10"); 124 | if (data) { 125 | dl_remove_node(&data->node, &mytree); 126 | dll_free(data); 127 | } 128 | 129 | /* *delete once again*/ 130 | Debug_Message(LOG_FULL, "delete node 31: \n"); 131 | data = dll_search(&mytree, "31"); 132 | if (data) { 133 | dl_remove_node(&data->node, &mytree); 134 | dll_free(data); 135 | } 136 | 137 | /* *search again*/ 138 | Debug_Message(LOG_FULL, "search again:\n"); 139 | for (node = dl_first(&mytree); node; node = dl_next(node)) { 140 | struct mynode *data; 141 | container_of(data, node, struct mynode, node); 142 | Debug_Message(LOG_TEST,"key = %s\n", data->string); 143 | } 144 | return 0; 145 | } 146 | 147 | 148 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/cache/dllist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dllist.h" 3 | 4 | void dl_init_node(LPDL_NODE node) 5 | { 6 | node->dl_next = NULL; 7 | node->dl_pre = NULL; 8 | } 9 | 10 | void dl_insert_node(LPDL_NODE node, LPDL_NODE insert, LPDL_ROOT root) 11 | { 12 | if (insert) { //update head 13 | node->dl_next = insert->dl_next; 14 | insert->dl_next = node; 15 | node->dl_pre = insert; 16 | if (node->dl_next) { 17 | node->dl_next->dl_pre = node; 18 | } else { //update last 19 | root->dl_last = node; 20 | } 21 | } else { 22 | if (root->dl_head) { 23 | root->dl_head->dl_pre = node; 24 | node->dl_next = root->dl_head; 25 | root->dl_head = node; 26 | root->dl_head->dl_pre = NULL; 27 | } else { 28 | root->dl_head = node; 29 | node->dl_pre = NULL; 30 | root->dl_last = node; 31 | node->dl_next = NULL; 32 | } 33 | } 34 | } 35 | 36 | void dl_remove_node(LPDL_NODE node, LPDL_ROOT root) 37 | { 38 | if (node->dl_pre) { 39 | node->dl_pre->dl_next = node->dl_next; 40 | } else { 41 | root->dl_head = node->dl_next; 42 | } 43 | 44 | if (node->dl_next) { 45 | node->dl_next->dl_pre = node->dl_pre; 46 | } else { 47 | root->dl_last = node->dl_pre; 48 | } 49 | } 50 | 51 | void dl_replace_node(LPDL_NODE node, LPDL_NODE replace, LPDL_ROOT root) 52 | { 53 | if (node->dl_pre) { 54 | node->dl_pre->dl_next = replace; 55 | replace->dl_pre = node->dl_pre; 56 | } else { 57 | root->dl_head = replace; 58 | } 59 | 60 | if (node->dl_next) { 61 | node->dl_next->dl_pre = replace; 62 | replace->dl_next = node->dl_next; 63 | } else { 64 | root->dl_last = replace; 65 | } 66 | } 67 | 68 | LPDL_NODE dl_next(LPDL_NODE node) 69 | { 70 | return node->dl_next; 71 | } 72 | 73 | LPDL_NODE dl_prev(LPDL_NODE node) 74 | { 75 | return node->dl_pre; 76 | } 77 | 78 | LPDL_NODE dl_first(const LPDL_ROOT root) 79 | { 80 | return root->dl_head; 81 | } 82 | 83 | LPDL_NODE dl_last(const LPDL_ROOT root) 84 | { 85 | return root->dl_last; 86 | } 87 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/cache/dllist.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOUBLE_LINKED_LIST_H 2 | #define _DOUBLE_LINKED_LIST_H 3 | 4 | typedef struct double_link_node_t { 5 | struct double_link_node_t *dl_pre; 6 | struct double_link_node_t *dl_next; 7 | }DL_NODE, *LPDL_NODE; 8 | 9 | typedef struct double_link_root_t { 10 | LPDL_NODE dl_head; 11 | LPDL_NODE dl_last; 12 | }DL_ROOT, *LPDL_ROOT; 13 | 14 | #ifdef __cplusplus 15 | extern "C"{ 16 | #endif 17 | 18 | void dl_init_node(LPDL_NODE node); 19 | void dl_insert_node(LPDL_NODE node, LPDL_NODE insert, LPDL_ROOT root); 20 | void dl_remove_node(LPDL_NODE node, LPDL_ROOT root); 21 | void dl_replace_node(LPDL_NODE node, LPDL_NODE replace, LPDL_ROOT root); 22 | 23 | /* Find logical next and previous nodes */ 24 | LPDL_NODE dl_next(LPDL_NODE node); 25 | LPDL_NODE dl_prev(LPDL_NODE node); 26 | LPDL_NODE dl_first(const LPDL_ROOT root); 27 | LPDL_NODE dl_last(const LPDL_ROOT root); 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/cache/rbtree-test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================= 3 | * 4 | * Filename: rbtree-tst.c 5 | * 6 | * Description: rbtree testcase. 7 | * 8 | * Created: 09/02/2012 11:39:34 PM 9 | * 10 | * Author: Fu Haiping (forhappy), haipingf@gmail.com 11 | * Company: ICT ( Institute Of Computing Technology, CAS ) 12 | * 13 | * ============================================================================= 14 | */ 15 | 16 | #include "rbtree.h" 17 | #include 18 | #include 19 | #include 20 | 21 | #if defined(offsetof) 22 | #undef offsetof 23 | #endif 24 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 25 | 26 | #if defined(container_of) 27 | #undef container_of 28 | #endif 29 | #define container_of(res, ptr, type, member) { \ 30 | unsigned long address = (unsigned long)(ptr); \ 31 | res = (type *)( address - offsetof(type,member) ); } 32 | struct mynode { 33 | RB_NODE node; 34 | char *string; 35 | }; 36 | 37 | RB_ROOT mytree = {0}; 38 | 39 | struct mynode * rbt_search(LPRB_ROOT root, char *string) 40 | { 41 | LPRB_NODE node = root->rb_node; 42 | 43 | while (node) { 44 | struct mynode *data; 45 | int result; 46 | container_of(data, node, struct mynode, node); 47 | result = strcmp(string, data->string); 48 | 49 | if (result < 0) 50 | node = node->rb_left; 51 | else if (result > 0) 52 | node = node->rb_right; 53 | else 54 | return data; 55 | } 56 | return NULL; 57 | } 58 | 59 | int rbt_insert(LPRB_ROOT root, struct mynode *node) 60 | { 61 | LPRB_NODE *new = &(root->rb_node); 62 | LPRB_NODE parent = NULL; 63 | 64 | /* Figure out where to put new node */ 65 | while (*new) { 66 | struct mynode *data; 67 | int result; 68 | container_of(data, *new, struct mynode, node); 69 | result = strcmp(node->string, data->string); 70 | 71 | parent = *new; 72 | if (result < 0) 73 | new = &((*new)->rb_left); 74 | else if (result > 0) 75 | new = &((*new)->rb_right); 76 | else 77 | return 0; 78 | } 79 | 80 | /* Add new node and rebalance tree. */ 81 | rb_link_node(&node->node, parent, new); 82 | rb_insert_color(&node->node, root); 83 | 84 | return 1; 85 | } 86 | 87 | void rbt_free(struct mynode *node) 88 | { 89 | if (node != NULL) { 90 | if (node->string != NULL) { 91 | free(node->string); 92 | node->string = NULL; 93 | } 94 | free(node); 95 | node = NULL; 96 | } 97 | } 98 | 99 | #define NUM_NODES 32 100 | 101 | int rbtree_test() 102 | { 103 | 104 | struct mynode *mn[NUM_NODES]; 105 | LPRB_NODE node; 106 | struct mynode *data; 107 | 108 | /* *insert */ 109 | int i = 0; 110 | printf("insert node from 1 to NUM_NODES(32): \n"); 111 | for (; i < NUM_NODES; i++) { 112 | mn[i] = (struct mynode *)malloc(sizeof(struct mynode)); 113 | mn[i]->string = (char *)malloc(sizeof(char) * 4); 114 | sprintf(mn[i]->string, "%d", i); 115 | rbt_insert(&mytree, mn[i]); 116 | } 117 | 118 | /* *search */ 119 | printf("search all nodes: \n"); 120 | for (node = rb_first(&mytree); node; node = rb_next(node)) { 121 | struct mynode *data; 122 | container_of(data, node, struct mynode, node); 123 | printf("key = %s\n", data->string); 124 | } 125 | 126 | /* *delete */ 127 | printf("delete node 20: \n"); 128 | data = rbt_search(&mytree, "20"); 129 | if (data) { 130 | rb_erase(&data->node, &mytree); 131 | rbt_free(data); 132 | } 133 | 134 | /* *delete again*/ 135 | printf("delete node 10: \n"); 136 | data = rbt_search(&mytree, "10"); 137 | if (data) { 138 | rb_erase(&data->node, &mytree); 139 | rbt_free(data); 140 | } 141 | 142 | /* *delete once again*/ 143 | printf("delete node 15: \n"); 144 | data = rbt_search(&mytree, "15"); 145 | if (data) { 146 | rb_erase(&data->node, &mytree); 147 | rbt_free(data); 148 | } 149 | 150 | /* *search again*/ 151 | printf("search again:\n"); 152 | for (node = rb_first(&mytree); node; node = rb_next(node)) { 153 | struct mynode *data; 154 | container_of(data, node, struct mynode, node); 155 | printf("key = %s\n", data->string); 156 | } 157 | return 0; 158 | } 159 | 160 | 161 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/cache/rbtree.h: -------------------------------------------------------------------------------- 1 | #ifndef _RBTREE_H 2 | #define _RBTREE_H 3 | 4 | #ifndef INT64 5 | #define INT unsigned long 6 | #else 7 | #define INT unsigned long long 8 | #endif 9 | 10 | typedef struct rb_node_t { 11 | INT rb_parent_color; 12 | struct rb_node_t *rb_right; 13 | struct rb_node_t *rb_left; 14 | }__attribute__((aligned(sizeof(long)))) RB_NODE, *LPRB_NODE; 15 | 16 | typedef struct rb_root_t { 17 | LPRB_NODE rb_node; 18 | }RB_ROOT, *LPRB_ROOT; 19 | 20 | #ifdef __cplusplus 21 | extern "C"{ 22 | #endif 23 | 24 | void rb_init_node(LPRB_NODE rb); 25 | void rb_link_node(LPRB_NODE node, LPRB_NODE parent, LPRB_NODE * rb_link); 26 | void rb_insert_color(LPRB_NODE , LPRB_ROOT ); 27 | void rb_erase(LPRB_NODE , LPRB_ROOT ); 28 | 29 | /* Find logical next and previous nodes in a tree */ 30 | LPRB_NODE rb_next(LPRB_NODE ); 31 | LPRB_NODE rb_prev(LPRB_NODE ); 32 | LPRB_NODE rb_first(const LPRB_ROOT ); 33 | LPRB_NODE rb_last(const LPRB_ROOT ); 34 | 35 | /* Fast replacement of a single node without remove/rebalance/add/rebalance */ 36 | void rb_replace_node(LPRB_NODE victim, LPRB_NODE new, LPRB_ROOT root); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif 43 | 44 | 45 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/device/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_MODULE := device 6 | LOCAL_SRC_FILES := device.c 7 | LOCAL_INC_FILES := device.h 8 | LOCAL_STATIC_LIBRARIES := logger 9 | 10 | LOCAL_C_INCLUDES := $(LOCAL_PATH) 11 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) 12 | 13 | include $(BUILD_STATIC_LIBRARY) -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/device/device.h: -------------------------------------------------------------------------------- 1 | #if !defined(_G_DEVICE_H_) 2 | #define _G_DEVICE_H_ 3 | 4 | #include 5 | 6 | #define DEF_SERIAL 0 7 | #define USB_TO_SERIAL 1 8 | #define FREE_TO_SERIAL 2 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | //serial 14 | int Open_Port(int com_port, char * dev, int *error, int type); 15 | int Set_Port(int fd, int baud_rate, int data_bits, char parity, int stop_bits, int vtime, int vmin); 16 | int Write_Port(int fd, void * buffer, int size); 17 | int Read_Port(int fd, void * buffer, int size); 18 | int Close_Port(int fd); 19 | //video 20 | long Open_Video(int port); 21 | int Set_Video(long fd, int width, int height, int format); 22 | int Read_Video(long fd, unsigned char * pFrameBuffer, int size); 23 | int Close_Video(long fd); 24 | int Check_Format(long fd, unsigned int format); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/image/image.h: -------------------------------------------------------------------------------- 1 | /* 2 | * image.h 3 | * 4 | * Created on: 2015年6月18日 5 | * Author: gqj3375 6 | */ 7 | 8 | #ifndef JNI_IMAGE_IMAGE_H_ 9 | #define JNI_IMAGE_IMAGE_H_ 10 | 11 | #define CP_MJPEG 0xA000 12 | #define CP_PNG 0xB000 13 | #define CP_RGBA8888 1 14 | #define CP_RGB565 4 15 | #define CP_RGBA4444 7 16 | #define CP_ALPHA8 8 17 | #define CP_PAF_NV21 0x802 18 | #define CP_PAF_NV12 0x801 19 | #define CP_PAF_YUYV 0x501 20 | #define CP_PAF_I420 0x601 21 | #define CP_PAF_BGR24 0x201 22 | 23 | #ifdef __cplusplus 24 | extern "C"{ 25 | #endif 26 | 27 | void convert_565_8888(unsigned char *p565, unsigned char * p8888, int width, int height); 28 | void convert_8888_565(unsigned char * p8888, unsigned char *p565, int width, int height); 29 | void convert_4444_8888(unsigned char *p4444, unsigned char * p8888, int width, int height); 30 | void convert_8888_4444(unsigned char * p8888, unsigned char *p4444, int width, int height); 31 | void convert_4444_565(unsigned char *p4444, unsigned char *p565, int width, int height); 32 | void convert_565_4444(unsigned char *p565, unsigned char *p4444, int width, int height); 33 | void convert_8888_NV12(unsigned char * p8888, unsigned char *pNV12, int width, int height); 34 | void convert_8888_NV21(unsigned char * p8888, unsigned char *pNV12, int width, int height); 35 | void convert_565_NV12(unsigned char * p565, unsigned char *pNV21, int width, int height); 36 | void convert_565_NV21(unsigned char * p565, unsigned char *pNV21, int width, int height); 37 | void convert_YUYV_NV21(unsigned char * YUYV, unsigned char *pNV21, int width, int height); 38 | void convert_YUYV_NV12(unsigned char * YUYV, unsigned char *pNV21, int width, int height); 39 | void convert_8888_BGR888(unsigned char * p8888, unsigned char *p888, int width, int height); 40 | void convert_565_BGR888(unsigned char * p565, unsigned char *p888, int width, int height); 41 | 42 | 43 | 44 | int calcImageSize(int width, int height, int format); 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif /* JNI_IMAGE_IMAGE_H_ */ 51 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/logger/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_MODULE := logger 6 | LOCAL_SRC_FILES := logger.cpp 7 | 8 | LOCAL_C_INCLUDES := $(LOCAL_PATH) 9 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) 10 | 11 | include $(BUILD_STATIC_LIBRARY) -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/logger/logger.cpp: -------------------------------------------------------------------------------- 1 | //jni 2 | #include 3 | #include 4 | #include 5 | 6 | //lib c 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "logger.h" 14 | 15 | #define LOG_TAG "ATC." 16 | 17 | static char str[256] = {'\0'}; 18 | static unsigned long FPS_ThisTime = 0; 19 | static unsigned long FPS_LastTime = 0; 20 | static unsigned long FPS_Count = 0; 21 | static int FPS_TimeCount = 0; 22 | 23 | unsigned long GTimeGet() 24 | { 25 | unsigned long g_Time = 0; 26 | struct timespec ts; 27 | clock_gettime(CLOCK_MONOTONIC , &ts); 28 | //微秒 29 | g_Time = 1000000*ts.tv_sec + ts.tv_nsec/1000; 30 | //毫秒 31 | return g_Time / 1000; 32 | } 33 | 34 | unsigned long GFps_GetCurFps() 35 | { 36 | if (FPS_TimeCount == 0) { 37 | FPS_LastTime = GTimeGet(); 38 | } 39 | if (++FPS_TimeCount >= 30) { 40 | if (FPS_LastTime - FPS_ThisTime != 0) { 41 | FPS_Count = 30000 / (FPS_LastTime - FPS_ThisTime); 42 | } 43 | FPS_TimeCount = 0; 44 | FPS_ThisTime = FPS_LastTime; 45 | } 46 | return FPS_Count; 47 | } 48 | 49 | 50 | void LOGI(const char * szFormat, ...) 51 | { 52 | #if defined(LOG_TAG) 53 | int cnt = 0; 54 | va_list ArgList; 55 | va_start(ArgList, szFormat); 56 | cnt = vsprintf(str, szFormat, ArgList); 57 | va_end(ArgList); 58 | __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "%s", str); 59 | #endif 60 | } 61 | 62 | void LOGD(const char * szFormat, ...) 63 | { 64 | #if defined(LOG_TAG) 65 | int cnt = 0; 66 | va_list ArgList; 67 | va_start(ArgList, szFormat); 68 | cnt = vsprintf(str, szFormat, ArgList); 69 | va_end(ArgList); 70 | __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "%s", str); 71 | #endif 72 | } 73 | 74 | void LOGE(const char * szFormat, ...) 75 | { 76 | #if defined(LOG_TAG) 77 | int cnt = 0; 78 | va_list ArgList; 79 | va_start(ArgList, szFormat); 80 | cnt = vsprintf(str, szFormat, ArgList); 81 | va_end(ArgList); 82 | __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "%s", str); 83 | #endif 84 | } 85 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/logger/logger.h: -------------------------------------------------------------------------------- 1 | #if !defined(_G_LOGGER_H_) 2 | #define _G_LOGGER_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | unsigned long GTimeGet(); 8 | unsigned long GFps_GetCurFps(); 9 | 10 | void LOGI(const char * szFormat, ...); 11 | void LOGD(const char * szFormat, ...); 12 | void LOGE(const char * szFormat, ...); 13 | 14 | #ifdef __cplusplus 15 | } 16 | #endif 17 | 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/render/Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Matrix.h" 3 | 4 | #define PI 3.1415926f 5 | #define normalize(x, y, z) \ 6 | { \ 7 | float norm = 1.0f / sqrt(x*x+y*y+z*z); \ 8 | x *= norm; y *= norm; z *= norm; \ 9 | } 10 | #define I(_i, _j) ((_j)+4*(_i)) 11 | // 12 | //void Matrix() 13 | //{ 14 | // 15 | //} 16 | // 17 | //void ~Matrix() 18 | //{ 19 | // 20 | //} 21 | 22 | void Matrix::matrixSetIdentityM(float *m) 23 | { 24 | memset((void*)m, 0, 16*sizeof(float)); 25 | m[0] = m[5] = m[10] = m[15] = 1.0f; 26 | } 27 | 28 | void Matrix::matrixSetRotateM(float *m, float a, float x, float y, float z) 29 | { 30 | float s, c; 31 | 32 | memset((void*)m, 0, 15*sizeof(float)); 33 | m[15] = 1.0f; 34 | 35 | a *= PI/180.0f; 36 | s = sin(a); 37 | c = cos(a); 38 | 39 | if (1.0f == x && 0.0f == y && 0.0f == z) { 40 | m[5] = c; m[10] = c; 41 | m[6] = s; m[9] = -s; 42 | m[0] = 1; 43 | } else if (0.0f == x && 1.0f == y && 0.0f == z) { 44 | m[0] = c; m[10] = c; 45 | m[8] = s; m[2] = -s; 46 | m[5] = 1; 47 | } else if (0.0f == x && 0.0f == y && 1.0f == z) { 48 | m[0] = c; m[5] = c; 49 | m[1] = s; m[4] = -s; 50 | m[10] = 1; 51 | } else { 52 | normalize(x, y, z); 53 | float nc = 1.0f - c; 54 | float xy = x * y; 55 | float yz = y * z; 56 | float zx = z * x; 57 | float xs = x * s; 58 | float ys = y * s; 59 | float zs = z * s; 60 | m[ 0] = x*x*nc + c; 61 | m[ 4] = xy*nc - zs; 62 | m[ 8] = zx*nc + ys; 63 | m[ 1] = xy*nc + zs; 64 | m[ 5] = y*y*nc + c; 65 | m[ 9] = yz*nc - xs; 66 | m[ 2] = zx*nc - ys; 67 | m[ 6] = yz*nc + xs; 68 | m[10] = z*z*nc + c; 69 | } 70 | } 71 | 72 | void Matrix::matrixMultiplyMM(float *m, float *lhs, float *rhs) 73 | { 74 | float t[16]; 75 | for (int i = 0; i < 4; i++) { 76 | register const float rhs_i0 = rhs[I(i, 0)]; 77 | register float ri0 = lhs[ I(0,0) ] * rhs_i0; 78 | register float ri1 = lhs[ I(0,1) ] * rhs_i0; 79 | register float ri2 = lhs[ I(0,2) ] * rhs_i0; 80 | register float ri3 = lhs[ I(0,3) ] * rhs_i0; 81 | for (int j = 1; j < 4; j++) { 82 | register const float rhs_ij = rhs[ I(i,j) ]; 83 | ri0 += lhs[ I(j,0) ] * rhs_ij; 84 | ri1 += lhs[ I(j,1) ] * rhs_ij; 85 | ri2 += lhs[ I(j,2) ] * rhs_ij; 86 | ri3 += lhs[ I(j,3) ] * rhs_ij; 87 | } 88 | t[ I(i,0) ] = ri0; 89 | t[ I(i,1) ] = ri1; 90 | t[ I(i,2) ] = ri2; 91 | t[ I(i,3) ] = ri3; 92 | } 93 | memcpy(m, t, sizeof(t)); 94 | } 95 | 96 | void Matrix::matrixScaleM(float *m, float x, float y, float z) 97 | { 98 | for (int i = 0; i < 4; i++) 99 | { 100 | m[i] *= x; m[4+i] *=y; m[8+i] *= z; 101 | } 102 | } 103 | 104 | void Matrix::matrixTranslateM(float *m, float x, float y, float z) 105 | { 106 | for (int i = 0; i < 4; i++) 107 | { 108 | m[12+i] += m[i]*x + m[4+i]*y + m[8+i]*z; 109 | } 110 | } 111 | 112 | void Matrix::matrixRotateM(float *m, float a, float x, float y, float z) 113 | { 114 | float rot[16], res[16]; 115 | matrixSetRotateM(rot, a, x, y, z); 116 | matrixMultiplyMM(res, m, rot); 117 | memcpy(m, res, 16*sizeof(float)); 118 | } 119 | 120 | void Matrix::matrixLookAtM(float *m, 121 | float eyeX, float eyeY, float eyeZ, 122 | float cenX, float cenY, float cenZ, 123 | float upX, float upY, float upZ) 124 | { 125 | float fx = cenX - eyeX; 126 | float fy = cenY - eyeY; 127 | float fz = cenZ - eyeZ; 128 | normalize(fx, fy, fz); 129 | float sx = fy * upZ - fz * upY; 130 | float sy = fz * upX - fx * upZ; 131 | float sz = fx * upY - fy * upX; 132 | normalize(sx, sy, sz); 133 | float ux = sy * fz - sz * fy; 134 | float uy = sz * fx - sx * fz; 135 | float uz = sx * fy - sy * fx; 136 | 137 | m[ 0] = sx; 138 | m[ 1] = ux; 139 | m[ 2] = -fx; 140 | m[ 3] = 0.0f; 141 | m[ 4] = sy; 142 | m[ 5] = uy; 143 | m[ 6] = -fy; 144 | m[ 7] = 0.0f; 145 | m[ 8] = sz; 146 | m[ 9] = uz; 147 | m[10] = -fz; 148 | m[11] = 0.0f; 149 | m[12] = 0.0f; 150 | m[13] = 0.0f; 151 | m[14] = 0.0f; 152 | m[15] = 1.0f; 153 | matrixTranslateM(m, -eyeX, -eyeY, -eyeZ); 154 | } 155 | 156 | void Matrix::matrixFrustumM(float *m, float left, float right, float bottom, float top, float near, float far) 157 | { 158 | float r_width = 1.0f / (right - left); 159 | float r_height = 1.0f / (top - bottom); 160 | float r_depth = 1.0f / (near - far); 161 | float x = 2.0f * (near * r_width); 162 | float y = 2.0f * (near * r_height); 163 | float A = 2.0f * ((right+left) * r_width); 164 | float B = (top + bottom) * r_height; 165 | float C = (far + near) * r_depth; 166 | float D = 2.0f * (far * near * r_depth); 167 | 168 | memset((void*)m, 0, 16*sizeof(float)); 169 | m[ 0] = x; 170 | m[ 5] = y; 171 | m[ 8] = A; 172 | m[ 9] = B; 173 | m[10] = C; 174 | m[14] = D; 175 | m[11] = -1.0f; 176 | } 177 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/render/Matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef MATRIX_H 2 | #define MATRIX_H 3 | 4 | #include 5 | #include 6 | 7 | 8 | 9 | class Matrix 10 | { 11 | 12 | // void Matrix(); 13 | // void ~Matrix(); 14 | 15 | public: 16 | static void matrixSetIdentityM(float *m); 17 | static void matrixSetRotateM(float *m, float a, float x, float y, float z); 18 | static void matrixMultiplyMM(float *m, float *lhs, float *rhs); 19 | static void matrixScaleM(float *m, float x, float y, float z); 20 | static void matrixTranslateM(float *m, float x, float y, float z); 21 | static void matrixRotateM(float *m, float a, float x, float y, float z); 22 | static void matrixLookAtM(float *m, 23 | float eyeX, float eyeY, float eyeZ, 24 | float cenX, float cenY, float cenZ, 25 | float upX, float upY, float upZ); 26 | static void matrixFrustumM(float *m, float left, float right, float bottom, float top, float near, float far); 27 | 28 | }; 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/render/comm_render.h: -------------------------------------------------------------------------------- 1 | #if !defined(_G_COMM_RENDER_H_) 2 | #define _G_COMM_RENDER_H_ 3 | 4 | #ifndef INT64 5 | #define INT unsigned long 6 | #else 7 | #define INT unsigned long long 8 | #endif 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | INT GLCommRenderInit(int mirror, int ori, int format); 15 | void GLCommRenderDrawRect( INT handle, int w, int h, int *point, int rgb, int size); 16 | void GLCommRenderChanged(INT engine, int w, int h); 17 | void GLCommRenderChangedConfig(INT engine, int mirror, int ori); 18 | void GLCommRenderUnInit(INT engine); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/render/image_render.h: -------------------------------------------------------------------------------- 1 | #if !defined(_G_IMAGE_RENDER_H_) 2 | #define _G_IMAGE_RENDER_H_ 3 | 4 | #ifndef INT64 5 | #define INT unsigned long 6 | #else 7 | #define INT unsigned long long 8 | #endif 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | INT GLImageRenderInit(int mirror, int ori, int format); 15 | void GLImageRenderChanged(INT engine, int w, int h); 16 | void GLImageRenderChangedConfig(INT engine, int mirror, int ori); 17 | void GLImageRender(INT engine, unsigned char* pData, int w, int h); 18 | void GLImageRenderUnInit(INT engine); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/commen/render/shader.h: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * When an array element i is transferred to the GL by the DrawArrays or DrawElements commands, 4 | * each generic attribute is expanded to four components. If size is one then the x component 5 | * of the attribute is specified by the array; the y, z, and w components are implicitly set to 6 | * zero, zero, and one, respectively. If size is two then the x and y components of the attribute 7 | * are specified by the array; the z, and w components are implicitly set to zero, and one, 8 | * respectively. If size is three then x, y, and z are specified, and w is implicitly set to one. 9 | * If size is four then all components are specified. 10 | * 11 | */ 12 | static const char* pVertexShaderStr = 13 | "attribute vec4 a_position; \n \ 14 | uniform mat4 umMatrix; \n \ 15 | uniform mat4 uvMatrix; \n \ 16 | attribute vec2 a_texCoord; \n \ 17 | varying highp vec2 v_texCoord; \n \ 18 | void main() \n \ 19 | { \n \ 20 | gl_Position = a_position* umMatrix * uvMatrix; \n \ 21 | v_texCoord = a_texCoord; \n \ 22 | } \n"; 23 | 24 | static const char* pVertexShaderStrMatrix = 25 | "uniform mat4 uMMatrix; \n \ 26 | uniform mat4 uVMatrix; \n \ 27 | attribute vec4 a_position; \n \ 28 | void main() { \n \ 29 | gl_Position = a_position * uMMatrix * uVMatrix ; \n \ 30 | } \n"; 31 | 32 | static const char* pVertexShaderSimple = 33 | "attribute vec4 a_position; \n \ 34 | void main() { \n \ 35 | gl_Position = a_position ; \n \ 36 | } \n"; 37 | 38 | static const char* pFragmentShaderI420 = 39 | "precision highp float; \n \ 40 | uniform sampler2D y_texture; \n \ 41 | uniform sampler2D uv_texture; \n \ 42 | uniform sampler2D v_texture; \n \ 43 | varying highp vec2 v_texCoord; \n \ 44 | void main() \n \ 45 | { \n \ 46 | mediump vec3 yuv; \n \ 47 | highp vec3 rgb; \n \ 48 | yuv.x = texture2D(y_texture, v_texCoord).r; \n \ 49 | yuv.y = texture2D(uv_texture, v_texCoord).r; \n \ 50 | yuv.z = texture2D(v_texture, v_texCoord).r; \n \ 51 | rgb = mat3( 1, 1, 1, \n \ 52 | 0, -0.344, 1.770, \n \ 53 | 1.403, -0.714, 0) * yuv; \n \ 54 | gl_FragColor = vec4(rgb, 1); \n \ 55 | } \n"; 56 | 57 | static const char* pFragmentShaderYUYV = 58 | "precision highp float; \n \ 59 | uniform sampler2D y_texture; \n \ 60 | uniform sampler2D uv_texture; \n \ 61 | varying highp vec2 v_texCoord; \n \ 62 | void main() \n \ 63 | { \n \ 64 | mediump vec3 yuv; \n \ 65 | highp vec3 rgb; \n \ 66 | yuv.x = texture2D(y_texture, v_texCoord).r; \n \ 67 | yuv.y = texture2D(uv_texture, v_texCoord).g-0.5; \n \ 68 | yuv.z = texture2D(uv_texture, v_texCoord).a-0.5; \n \ 69 | rgb = mat3( 1, 1, 1, \n \ 70 | 0, -0.344, 1.770, \n \ 71 | 1.403, -0.714, 0) * yuv; \n \ 72 | gl_FragColor = vec4(rgb, 1); \n \ 73 | } \n"; 74 | 75 | static const char* pFragmentShaderNV21 = 76 | "precision highp float; \n \ 77 | uniform sampler2D y_texture; \n \ 78 | uniform sampler2D uv_texture; \n \ 79 | varying highp vec2 v_texCoord; \n \ 80 | void main() \n \ 81 | { \n \ 82 | mediump vec3 yuv; \n \ 83 | highp vec3 rgb; \n \ 84 | yuv.x = texture2D(y_texture, v_texCoord).r; \n \ 85 | yuv.y = texture2D(uv_texture, v_texCoord).a-0.5; \n \ 86 | yuv.z = texture2D(uv_texture, v_texCoord).r-0.5; \n \ 87 | rgb = mat3( 1, 1, 1, \n \ 88 | 0, -0.344, 1.770, \n \ 89 | 1.403, -0.714, 0) * yuv; \n \ 90 | gl_FragColor = vec4(rgb, 1); \n \ 91 | } \n"; 92 | 93 | static const char* pFragmentShaderNV12 = 94 | "precision highp float; \n \ 95 | uniform sampler2D y_texture; \n \ 96 | uniform sampler2D uv_texture; \n \ 97 | varying highp vec2 v_texCoord; \n \ 98 | void main() \n \ 99 | { \n \ 100 | mediump vec3 yuv; \n \ 101 | highp vec3 rgb; \n \ 102 | yuv.x = texture2D(y_texture, v_texCoord).r; \n \ 103 | yuv.y = texture2D(uv_texture, v_texCoord).r-0.5; \n \ 104 | yuv.z = texture2D(uv_texture, v_texCoord).a-0.5; \n \ 105 | rgb = mat3( 1, 1, 1, \n \ 106 | 0, -0.344, 1.770, \n \ 107 | 1.403, -0.714, 0) * yuv; \n \ 108 | gl_FragColor = vec4(rgb, 1); \n \ 109 | } \n"; 110 | 111 | static const char* pFragmentShaderColor = 112 | "precision mediump float; \n \ 113 | uniform vec4 vColor; \n \ 114 | void main() \n \ 115 | { \n \ 116 | gl_FragColor = vColor; \n \ 117 | } \n"; 118 | 119 | static const char* pFragmentShaderSimple = 120 | "precision mediump float; \n \ 121 | void main() \n \ 122 | { \n \ 123 | gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n \ 124 | } \n"; 125 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/image/Android.mk: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2009 The Android Open Source Project 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | LOCAL_PATH := $(call my-dir) 16 | 17 | $(warning $(LOCAL_PATH)) 18 | 19 | include $(CLEAR_VARS) 20 | LOCAL_MODULE := imagebase 21 | LOCAL_SRC_FILES := ..\commen\image\image.c 22 | LOCAL_C_INCLUDES := $(LOCAL_PATH)\..\commen\image 23 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) 24 | include $(BUILD_STATIC_LIBRARY) 25 | 26 | include $(CLEAR_VARS) 27 | LOCAL_MODULE := image 28 | LOCAL_SRC_FILES := src\android\image_adapter.cpp 29 | LOCAL_C_INCLUDES := $(LOCAL_PATH)\..\commen\image 30 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) 31 | 32 | LOCAL_LDLIBS := -llog -ljnigraphics 33 | 34 | LOCAL_STATIC_LIBRARIES := imagebase logger 35 | 36 | LOCAL_EXPORT_LDLIBS := $(LOCAL_LDLIBS) 37 | 38 | include $(BUILD_SHARED_LIBRARY) -------------------------------------------------------------------------------- /android-extend/src/main/jni/image/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(image) 2 | 3 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 4 | 5 | ADD_DEFINITIONS(-DBUILD_NUMBER=44) 6 | 7 | ADD_SUBDIRECTORY(src) -------------------------------------------------------------------------------- /android-extend/src/main/jni/image/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | INCLUDE(${PROJECT_SOURCE_DIR}/../cmake/findInclude.cmake) 2 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${PROJECT_SOURCE_DIR}) 3 | 4 | FIND_PATH(LOGGER_INCLUDE_DIR 5 | NAMES "logger.h" 6 | PATHS "${PROJECT_SOURCE_DIR}/../commen/logger" 7 | NO_DEFAULT_PATH) 8 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${LOGGER_INCLUDE_DIR}) 9 | 10 | FIND_PATH(IMAGE_INCLUDE_DIR 11 | NAMES "image.h" 12 | PATHS "${PROJECT_SOURCE_DIR}/../commen/image" 13 | NO_DEFAULT_PATH) 14 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${IMAGE_INCLUDE_DIR}) 15 | 16 | 17 | FILE(GLOB_RECURSE LIB_SRC "*.cpp" "*.c") 18 | 19 | #LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib) 20 | 21 | SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake) 22 | #SET(CMAKE_FIND_ROOT_PATH ${PROJECT_SOURCE_DIR}/cmake) 23 | SET(allLibs) 24 | #SET(ndk-stl_DIR ${PROJECT_SOURCE_DIR}/cmake/modules) 25 | FIND_PACKAGE(ndk-stl REQUIRED) 26 | 27 | FILE(GLOB LOGGER_SRC "${LOGGER_INCLUDE_DIR}/*.cpp" "${LOGGER_INCLUDE_DIR}/*.c" ) 28 | FILE(GLOB IMAGE_SRC "${IMAGE_INCLUDE_DIR}/*.cpp" "${IMAGE_INCLUDE_DIR}/*.c" ) 29 | SET(LIB_SRC ${LIB_SRC} ${LOGGER_SRC} ${IMAGE_SRC}) 30 | MESSAGE("module cache:${LIB_SRC}") 31 | 32 | IF (${CMAKE_BUILD_TYPE} MATCHES "Release") 33 | MESSAGE("Release SET jni.ver") 34 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 35 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 36 | ENDIF() 37 | 38 | ADD_LIBRARY(image SHARED ${LIB_SRC}) 39 | TARGET_LINK_LIBRARIES(image log android jnigraphics ${allLibs}) 40 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/image/src/android/image_adapter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "image.h" 10 | #include "logger.h" 11 | 12 | #define _DEBUG 13 | 14 | typedef struct imageformat_t { 15 | unsigned char *pBuffer; 16 | int width; 17 | int height; 18 | int format; 19 | #ifdef _DEBUG 20 | FILE *file; 21 | int count; 22 | unsigned char *buffer; 23 | #endif 24 | }IMAGE_HANDLE, *LPIMAGE_HANDLE; 25 | 26 | static jlong NIF_initial(JNIEnv *env, jobject object, jint width, jint height, jint format); 27 | static jint NIF_convert(JNIEnv* env, jobject obj, jlong handle, jobject jbitmap, jbyteArray data); 28 | static jint NIF_uninitial(JNIEnv *env, jobject object, jlong handle); 29 | 30 | static JNINativeMethod gMethods[] = { 31 | {"image_init", "(III)J",(void*)NIF_initial}, 32 | {"image_convert", "(JLandroid/graphics/Bitmap;[B)I",(void*)NIF_convert}, 33 | {"image_uninit", "(J)I",(void*)NIF_uninitial}, 34 | }; 35 | 36 | const char* JNI_NATIVE_INTERFACE_CLASS = "com/guo/android_extend/image/ImageConverter"; 37 | 38 | JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved){ 39 | 40 | JNIEnv *env = NULL; 41 | if (vm->GetEnv((void**)&env, JNI_VERSION_1_4)){ 42 | return JNI_ERR; 43 | } 44 | 45 | jclass cls = env->FindClass(JNI_NATIVE_INTERFACE_CLASS); 46 | if (cls == NULL){ 47 | return JNI_ERR; 48 | } 49 | 50 | jint nRes = env->RegisterNatives(cls, gMethods, sizeof(gMethods)/sizeof(gMethods[0])); 51 | if (nRes < 0){ 52 | return JNI_ERR; 53 | } 54 | 55 | LOGI("image.so JNI_OnLoad"); 56 | 57 | return JNI_VERSION_1_4; 58 | } 59 | 60 | JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved){ 61 | 62 | JNIEnv *env = NULL; 63 | if (vm->GetEnv((void**)&env, JNI_VERSION_1_4)){ 64 | return; 65 | } 66 | 67 | jclass cls = env->FindClass(JNI_NATIVE_INTERFACE_CLASS); 68 | if (cls == NULL){ 69 | return; 70 | } 71 | 72 | LOGI("image.so JNI_OnUnload"); 73 | jint nRes = env->UnregisterNatives(cls); 74 | } 75 | 76 | jint NIF_uninitial(JNIEnv *env, jobject object, jlong handle) 77 | { 78 | LPIMAGE_HANDLE engine = (LPIMAGE_HANDLE)handle; 79 | if (engine->pBuffer != NULL) { 80 | free(engine->pBuffer); 81 | } 82 | #ifdef _DEBUG 83 | fclose(engine->file); 84 | #endif 85 | free(engine); 86 | return 0; 87 | } 88 | 89 | jlong NIF_initial(JNIEnv *env, jobject object, jint width, jint height, jint format) 90 | { 91 | LPIMAGE_HANDLE handle = (LPIMAGE_HANDLE)malloc(sizeof(IMAGE_HANDLE)); 92 | handle->width = width; 93 | handle->height = height; 94 | handle->format = format; 95 | #ifdef _DEBUG 96 | handle->count = 100; 97 | handle->file = fopen("/sdcard/dump.nv21", "wb"); 98 | handle->buffer = (unsigned char *)malloc(1024*1024*1024*2); 99 | if (handle->buffer != NULL) { 100 | free(handle->buffer); 101 | } 102 | #endif 103 | 104 | //int x = 2048; 105 | //unsigned char * test = (unsigned char *)malloc(1024*1024*x); 106 | //while(test == NULL && x > 0) { 107 | // test = (unsigned char *)malloc(x*1024*1024); 108 | // x -= 100; 109 | //} 110 | LOGE("TEST:%d %d\n", handle->width, handle->height); 111 | //free(test); 112 | 113 | switch (format) { 114 | case CP_PAF_BGR24: 115 | handle->pBuffer = (unsigned char *) malloc(width * height * 3); 116 | break; 117 | case CP_PAF_I420: 118 | case CP_PAF_NV12: 119 | case CP_PAF_NV21: 120 | if ((width & 1) && (height & 1)) { 121 | env->ThrowNew(env->FindClass("java/lang/Exception"), "FORMAT NOT SUPPORT THIS SIZE"); 122 | return (jint)-1; 123 | } 124 | handle->pBuffer = (unsigned char *) malloc(width * height * 3 / 2); 125 | break; 126 | case CP_RGBA8888: 127 | case CP_RGB565: 128 | case CP_RGBA4444: 129 | case CP_ALPHA8: 130 | default : 131 | env->ThrowNew(env->FindClass("java/lang/Exception"), "FORMAT ERROR!"); 132 | return (jint)-1; 133 | } 134 | 135 | return (jlong)handle; 136 | } 137 | 138 | jint NIF_convert(JNIEnv* env, jobject obj, jlong handle, jobject jbitmap, jbyteArray data) 139 | { 140 | int ret = 0; 141 | LPIMAGE_HANDLE engine = (LPIMAGE_HANDLE)handle; 142 | AndroidBitmapInfo info; 143 | if (AndroidBitmap_getInfo(env, jbitmap, &info) < 0) { 144 | return -1; 145 | } 146 | 147 | if (info.width != engine->width || info.height != engine->height || data == NULL) { 148 | LOGE("PARAM FAIL! %d %d", engine->width, engine->height); 149 | return -1; 150 | } 151 | 152 | int size = calcImageSize(info.width, info.height, engine->format); 153 | if (env->GetArrayLength(data) < size) { 154 | LOGE("DATA BUFFER NOT NOT ENOUGH!"); 155 | return -1; 156 | } 157 | 158 | unsigned char* RGBAbase; 159 | AndroidBitmap_lockPixels(env, jbitmap, (void**) &RGBAbase); 160 | 161 | //convert 162 | if (info.format == CP_RGBA8888 && engine->format == CP_PAF_NV12) { 163 | convert_8888_NV12(RGBAbase, engine->pBuffer, info.width, info.height); 164 | } else if (info.format == CP_RGBA8888 && engine->format == CP_PAF_NV21) { 165 | convert_8888_NV21(RGBAbase, engine->pBuffer, info.width, info.height); 166 | } else if (info.format == CP_RGBA8888 && engine->format == CP_PAF_BGR24) { 167 | convert_8888_BGR888(RGBAbase, engine->pBuffer, info.width, info.height); 168 | } else if (info.format == CP_RGB565 && engine->format == CP_PAF_NV12) { 169 | convert_565_NV12(RGBAbase, engine->pBuffer, info.width, info.height); 170 | } else if (info.format == CP_RGB565 && engine->format == CP_PAF_NV21) { 171 | convert_565_NV21(RGBAbase, engine->pBuffer, info.width, info.height); 172 | } else if (info.format == CP_RGB565 && engine->format == CP_PAF_BGR24) { 173 | convert_565_BGR888(RGBAbase, engine->pBuffer, info.width, info.height); 174 | } else { 175 | LOGI("format = %d\n", info.format); 176 | ret = -1; 177 | } 178 | 179 | #ifdef _DEBUG 180 | if (engine->count > 0) { 181 | int size = fwrite(engine->pBuffer, 1, info.width * info.height * 3 / 2, engine->file); 182 | engine->count--; 183 | } else if (engine->count == 0) { 184 | engine->count--; 185 | fclose(engine->file); 186 | } 187 | #endif 188 | 189 | env->SetByteArrayRegion(data, 0, size, (const jbyte*) engine->pBuffer); 190 | 191 | AndroidBitmap_unlockPixels(env, jbitmap); 192 | 193 | return ret; 194 | } 195 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/jni.ver: -------------------------------------------------------------------------------- 1 | { 2 | global: JNI_OnLoad; JNI_OnUnload; 3 | local: *; 4 | }; 5 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/render/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_MODULE := GLESRender 6 | LOCAL_SRC_FILES := ..\commen\render\image_render.cpp \ 7 | ..\commen\render\comm_render.cpp 8 | LOCAL_C_INCLUDES := $(LOCAL_PATH)\..\commen\render 9 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) 10 | LOCAL_STATIC_LIBRARIES := imagebase logger 11 | include $(BUILD_STATIC_LIBRARY) 12 | 13 | 14 | include $(CLEAR_VARS) 15 | LOCAL_MODULE := render 16 | LOCAL_SRC_FILES := src\android\render_adapter.cpp \ 17 | ..\commen\render\Matrix.cpp 18 | LOCAL_C_INCLUDES := $(LOCAL_PATH)\..\commen\render 19 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) 20 | 21 | LOCAL_LDLIBS := -llog -ljnigraphics -lGLESv2 -lGLESv1_CM -ldl 22 | 23 | LOCAL_STATIC_LIBRARIES := imagebase logger GLESRender 24 | 25 | LOCAL_EXPORT_LDLIBS := $(LOCAL_LDLIBS) 26 | 27 | include $(BUILD_SHARED_LIBRARY) -------------------------------------------------------------------------------- /android-extend/src/main/jni/render/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(render) 2 | 3 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 4 | 5 | ADD_DEFINITIONS(-DBUILD_NUMBER=44) 6 | 7 | ADD_SUBDIRECTORY(src) -------------------------------------------------------------------------------- /android-extend/src/main/jni/render/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | INCLUDE(${PROJECT_SOURCE_DIR}/../cmake/findInclude.cmake) 2 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${PROJECT_SOURCE_DIR}) 3 | 4 | FIND_PATH(LOGGER_INCLUDE_DIR 5 | NAMES "logger.h" 6 | PATHS "${PROJECT_SOURCE_DIR}/../commen/logger" 7 | NO_DEFAULT_PATH) 8 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${LOGGER_INCLUDE_DIR}) 9 | 10 | FIND_PATH(IMAGE_INCLUDE_DIR 11 | NAMES "image.h" 12 | PATHS "${PROJECT_SOURCE_DIR}/../commen/image" 13 | NO_DEFAULT_PATH) 14 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${IMAGE_INCLUDE_DIR}) 15 | 16 | FIND_PATH(RENDER_INCLUDE_DIR 17 | NAMES "shader.h" 18 | PATHS "${PROJECT_SOURCE_DIR}/../commen/render" 19 | NO_DEFAULT_PATH) 20 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${RENDER_INCLUDE_DIR}) 21 | 22 | FILE(GLOB_RECURSE LIB_SRC "*.cpp" "*.c") 23 | 24 | #LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib) 25 | 26 | SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake) 27 | #SET(CMAKE_FIND_ROOT_PATH ${PROJECT_SOURCE_DIR}/cmake) 28 | SET(allLibs) 29 | #SET(ndk-stl_DIR ${PROJECT_SOURCE_DIR}/cmake/modules) 30 | FIND_PACKAGE(ndk-stl REQUIRED) 31 | 32 | FILE(GLOB LOGGER_SRC "${LOGGER_INCLUDE_DIR}/*.cpp" "${LOGGER_INCLUDE_DIR}/*.c" ) 33 | FILE(GLOB IMAGE_SRC "${IMAGE_INCLUDE_DIR}/*.cpp" "${IMAGE_INCLUDE_DIR}/*.c" ) 34 | FILE(GLOB RENDER_SRC "${RENDER_INCLUDE_DIR}/*.cpp" "${RENDER_INCLUDE_DIR}/*.c" ) 35 | SET(LIB_SRC ${LIB_SRC} ${LOGGER_SRC} ${IMAGE_SRC} ${RENDER_SRC}) 36 | MESSAGE("module cache:${LIB_SRC}") 37 | 38 | IF (${CMAKE_BUILD_TYPE} MATCHES "Release") 39 | MESSAGE("Release SET jni.ver") 40 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 41 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 42 | ENDIF() 43 | 44 | ADD_LIBRARY(render SHARED ${LIB_SRC}) 45 | TARGET_LINK_LIBRARIES(render log android jnigraphics GLESv2 GLESv1_CM dl ${allLibs}) 46 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/serial/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_MODULE := serial 6 | LOCAL_SRC_FILES := src\android\serial_adapter.cpp 7 | 8 | LOCAL_STATIC_LIBRARIES := device logger 9 | 10 | LOCAL_LDLIBS:= -llog 11 | 12 | include $(BUILD_SHARED_LIBRARY) -------------------------------------------------------------------------------- /android-extend/src/main/jni/serial/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(serial) 2 | 3 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 4 | 5 | ADD_DEFINITIONS(-DBUILD_NUMBER=44) 6 | 7 | ADD_SUBDIRECTORY(src) -------------------------------------------------------------------------------- /android-extend/src/main/jni/serial/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | INCLUDE(${PROJECT_SOURCE_DIR}/../cmake/findInclude.cmake) 2 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${PROJECT_SOURCE_DIR}) 3 | 4 | FIND_PATH(LOGGER_INCLUDE_DIR 5 | NAMES "logger.h" 6 | PATHS "${PROJECT_SOURCE_DIR}/../commen/logger" 7 | NO_DEFAULT_PATH) 8 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${LOGGER_INCLUDE_DIR}) 9 | 10 | FIND_PATH(DEVICE_INCLUDE_DIR 11 | NAMES "device.h" 12 | PATHS "${PROJECT_SOURCE_DIR}/../commen/device" 13 | NO_DEFAULT_PATH) 14 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${DEVICE_INCLUDE_DIR}) 15 | 16 | FILE(GLOB_RECURSE LIB_SRC "*.cpp" "*.c") 17 | 18 | #LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib) 19 | 20 | SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake) 21 | #SET(CMAKE_FIND_ROOT_PATH ${PROJECT_SOURCE_DIR}/cmake) 22 | SET(allLibs) 23 | #SET(ndk-stl_DIR ${PROJECT_SOURCE_DIR}/cmake/modules) 24 | FIND_PACKAGE(ndk-stl REQUIRED) 25 | 26 | FILE(GLOB LOGGER_SRC "${LOGGER_INCLUDE_DIR}/*.cpp" "${LOGGER_INCLUDE_DIR}/*.c" ) 27 | FILE(GLOB DEVICE_SRC "${DEVICE_INCLUDE_DIR}/*.cpp" "${DEVICE_INCLUDE_DIR}/*.c" ) 28 | SET(LIB_SRC ${LIB_SRC} ${LOGGER_SRC} ${DEVICE_SRC}) 29 | MESSAGE("module cache:${LIB_SRC}") 30 | 31 | IF (${CMAKE_BUILD_TYPE} MATCHES "Release") 32 | MESSAGE("Release SET jni.ver") 33 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 34 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 35 | ENDIF() 36 | 37 | ADD_LIBRARY(serial SHARED ${LIB_SRC}) 38 | TARGET_LINK_LIBRARIES(serial log android jnigraphics ${allLibs}) 39 | -------------------------------------------------------------------------------- /android-extend/src/main/jni/video/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_MODULE := video 6 | LOCAL_SRC_FILES := src\android\video_adapter.cpp 7 | 8 | LOCAL_STATIC_LIBRARIES := device imagebase logger 9 | 10 | LOCAL_LDLIBS:= -llog 11 | 12 | include $(BUILD_SHARED_LIBRARY) -------------------------------------------------------------------------------- /android-extend/src/main/jni/video/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(video) 2 | 3 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 4 | 5 | ADD_DEFINITIONS(-DBUILD_NUMBER=44) 6 | 7 | ADD_SUBDIRECTORY(src) -------------------------------------------------------------------------------- /android-extend/src/main/jni/video/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | INCLUDE(${PROJECT_SOURCE_DIR}/../cmake/findInclude.cmake) 2 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${PROJECT_SOURCE_DIR}) 3 | 4 | FIND_PATH(LOGGER_INCLUDE_DIR 5 | NAMES "logger.h" 6 | PATHS "${PROJECT_SOURCE_DIR}/../commen/logger" 7 | NO_DEFAULT_PATH) 8 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${LOGGER_INCLUDE_DIR}) 9 | 10 | FIND_PATH(DEVICE_INCLUDE_DIR 11 | NAMES "device.h" 12 | PATHS "${PROJECT_SOURCE_DIR}/../commen/device" 13 | NO_DEFAULT_PATH) 14 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${DEVICE_INCLUDE_DIR}) 15 | 16 | FIND_PATH(IMAGE_INCLUDE_DIR 17 | NAMES "image.h" 18 | PATHS "${PROJECT_SOURCE_DIR}/../commen/image" 19 | NO_DEFAULT_PATH) 20 | INCLUDE_SUB_DIRECTORIES_RECURSIVELY(${IMAGE_INCLUDE_DIR}) 21 | 22 | FILE(GLOB_RECURSE LIB_SRC "*.cpp" "*.c") 23 | 24 | #LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib) 25 | 26 | SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake) 27 | #SET(CMAKE_FIND_ROOT_PATH ${PROJECT_SOURCE_DIR}/cmake) 28 | SET(allLibs) 29 | #SET(ndk-stl_DIR ${PROJECT_SOURCE_DIR}/cmake/modules) 30 | FIND_PACKAGE(ndk-stl REQUIRED) 31 | 32 | FILE(GLOB LOGGER_SRC "${LOGGER_INCLUDE_DIR}/*.cpp" "${LOGGER_INCLUDE_DIR}/*.c" ) 33 | FILE(GLOB DEVICE_SRC "${DEVICE_INCLUDE_DIR}/*.cpp" "${DEVICE_INCLUDE_DIR}/*.c" ) 34 | FILE(GLOB IMAGE_SRC "${IMAGE_INCLUDE_DIR}/*.cpp" "${IMAGE_INCLUDE_DIR}/*.c" ) 35 | SET(LIB_SRC ${LIB_SRC} ${LOGGER_SRC} ${DEVICE_SRC} ${IMAGE_SRC}) 36 | MESSAGE("module cache:${LIB_SRC}") 37 | 38 | IF (${CMAKE_BUILD_TYPE} MATCHES "Release") 39 | MESSAGE("Release SET jni.ver") 40 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 41 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--version-script=${PROJECT_SOURCE_DIR}/../jni.ver") 42 | ENDIF() 43 | 44 | ADD_LIBRARY(video SHARED ${LIB_SRC}) 45 | TARGET_LINK_LIBRARIES(video log android jnigraphics ${allLibs}) 46 | -------------------------------------------------------------------------------- /android-extend/src/main/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /android-extend/src/main/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-11 15 | android.library=true 16 | -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xhdpi/bg_checkbox_box.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xhdpi/bg_checkbox_box.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xhdpi/bg_checkbox_checker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xhdpi/bg_checkbox_checker.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xhdpi/bg_seekbar_progress.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xhdpi/bg_seekbar_progress.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xhdpi/bg_seekbar_secondary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xhdpi/bg_seekbar_secondary.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xhdpi/bg_seekbar_thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xhdpi/bg_seekbar_thumb.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xhdpi/bg_vertical_seekbar_progress.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xhdpi/bg_vertical_seekbar_progress.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xhdpi/bg_vertical_seekbar_secondary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xhdpi/bg_vertical_seekbar_secondary.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xhdpi/bg_vertical_seekbar_thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xhdpi/bg_vertical_seekbar_thumb.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/android-extend/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable/bgxml_checkbox.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable/checkbox.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /android-extend/src/main/res/drawable/progress.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /android-extend/src/main/res/values-v17/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /android-extend/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /android-extend/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | android-widget-externed 4 | 5 | -------------------------------------------------------------------------------- /android-extend/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | 29 | 30 | 31 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/GLES2Render.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend; 2 | 3 | import android.graphics.Rect; 4 | 5 | public class GLES2Render { 6 | private final String TAG = this.getClass().getSimpleName(); 7 | 8 | public final static int MIRROR_NONE = 0; 9 | public final static int MIRROR_X = 1; 10 | public final static int MIRROR_Y = 2; 11 | 12 | private native long render_init(int mirror, int ori, int format, int fps); 13 | private native int render_changed(long handler, int width, int height); 14 | private native int render_rotated(long handler, int mirror, int ori); 15 | private native int render_process(long handler, byte[] data, int width, int height); 16 | private native int render_uninit(long handler); 17 | private native int render_draw_rect(long handler, Rect[] rect, int count, int rgb, int stroke); 18 | static { 19 | System.loadLibrary("render"); 20 | } 21 | 22 | private long handle; 23 | 24 | public GLES2Render(int mirror, int degree, int format, boolean showFPS) { 25 | // TODO Auto-generated constructor stub 26 | handle = render_init(mirror, degree, format, showFPS ? 1 : 0); 27 | } 28 | 29 | public void destory() { 30 | render_uninit(handle); 31 | } 32 | 33 | public void setViewPort(int width, int height) { 34 | render_changed(handle, width, height); 35 | } 36 | 37 | 38 | /** 39 | * SET DISPLAY 40 | * @param mirror MIRROR_X, MIRROR_Y, MIRROR_X|MIRROR_Y 41 | * @param degree 0, 90, 180, 270 42 | */ 43 | public void setViewDisplay(int mirror, int degree) { 44 | render_rotated(handle, mirror, degree); 45 | } 46 | 47 | public void render(byte[] data, int width, int height) { 48 | render_process(handle, data, width, height); 49 | } 50 | 51 | public void draw_rect(Rect[] rect, int color, int stroke) { 52 | render_draw_rect(handle, rect, rect.length, color, stroke); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/cache/BitmapCache.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.cache; 2 | 3 | import java.lang.ref.SoftReference; 4 | import java.util.HashMap; 5 | 6 | import com.guo.android_extend.java.LRULinkedHashMap; 7 | 8 | import android.graphics.Bitmap; 9 | import android.graphics.Bitmap.Config; 10 | import android.graphics.Canvas; 11 | 12 | public class BitmapCache { 13 | 14 | private HashMap> mCacheMap; 15 | private long mCacheHandle; 16 | 17 | private int CACHE_SIZE; 18 | private boolean USE_JVM_MEMORY; 19 | 20 | private native long cache_init(int size); 21 | private native int cache_put(long handler, int hash, Bitmap bitmap); 22 | private native Bitmap cache_get(long handler, int hash); 23 | private native Bitmap cache_get(long handler, int hash, int format); 24 | private native int cache_search(long handler, int hash, BitmapStructure info); 25 | private native int cache_copy(long handler, int hash, Bitmap output); 26 | private native int cache_uninit(long handler); 27 | 28 | static { 29 | System.loadLibrary("cache"); 30 | } 31 | 32 | public BitmapCache(int CacheSize, boolean useJVMMemory) { 33 | CACHE_SIZE = CacheSize; 34 | USE_JVM_MEMORY = useJVMMemory; 35 | if (USE_JVM_MEMORY) { 36 | mCacheMap = new LRULinkedHashMap>(CACHE_SIZE, 0.75F, true); 37 | } else { 38 | mCacheHandle = cache_init(CACHE_SIZE); 39 | } 40 | } 41 | 42 | public synchronized boolean putBitmap(T id, Bitmap bitmap) { 43 | if (USE_JVM_MEMORY) { 44 | return mCacheMap.put(id, new SoftReference(bitmap)) == null; 45 | } else { 46 | return cache_put(mCacheHandle, id.hashCode(), bitmap) == 0; 47 | } 48 | } 49 | 50 | public synchronized Bitmap getBitmap(T id) { 51 | if (USE_JVM_MEMORY) { 52 | if (mCacheMap.containsKey(id)) { 53 | return mCacheMap.get(id).get(); 54 | } 55 | } else { 56 | return cache_get(mCacheHandle, id.hashCode()); 57 | } 58 | return null; 59 | } 60 | 61 | public synchronized Bitmap getBitmap(T id, Config config) { 62 | if (USE_JVM_MEMORY) { 63 | if (mCacheMap.containsKey(id)) { 64 | return mCacheMap.get(id).get(); 65 | } 66 | } else { 67 | final int format = BitmapStructure.Config2NativeFormat(config); 68 | return cache_get(mCacheHandle, id.hashCode(), format); 69 | } 70 | return null; 71 | } 72 | 73 | public synchronized boolean queryBitmap(T id, BitmapStructure info) { 74 | if (USE_JVM_MEMORY) { 75 | if (mCacheMap.containsKey(id)) { 76 | Bitmap bitmap = mCacheMap.get(id).get(); 77 | if (bitmap != null) { 78 | info.setInfo(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); 79 | return true; 80 | } 81 | } 82 | return false; 83 | } else { 84 | return 0 == cache_search(mCacheHandle, id.hashCode(), info); 85 | } 86 | } 87 | 88 | public synchronized boolean copyBitmap(T id, Bitmap out) { 89 | if (USE_JVM_MEMORY) { 90 | if (mCacheMap.containsKey(id)) { 91 | Bitmap bitmap = mCacheMap.get(id).get(); 92 | if (bitmap != null) { 93 | Canvas cvs = new Canvas(out); 94 | cvs.drawBitmap(bitmap, 0, 0, null); 95 | return true; 96 | } 97 | } 98 | return false; 99 | } else { 100 | return 0 == cache_copy(mCacheHandle, id.hashCode(), out); 101 | } 102 | } 103 | 104 | public synchronized void destroy() { 105 | if (USE_JVM_MEMORY) { 106 | mCacheMap.clear(); 107 | } else { 108 | cache_uninit(mCacheHandle); 109 | mCacheHandle = 0; 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/cache/BitmapMonitor.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.cache; 2 | 3 | import android.graphics.Bitmap; 4 | 5 | /** 6 | * @author gqj3375 7 | * 8 | * @param is the view to show bitmap. 9 | * @param is the bitmap 's identification 10 | */ 11 | public abstract class BitmapMonitor implements Runnable { 12 | protected Bitmap mBitmap; 13 | 14 | protected K mView; 15 | protected V mBitmapID; 16 | 17 | protected OnMonitoring mOnMonitoring; 18 | /** 19 | * @author gqjjqg 20 | * check the queue, if it is contains the K. 21 | * 22 | */ 23 | public interface OnMonitoring { 24 | public boolean isUpdated(BitmapMonitor monitor); 25 | } 26 | 27 | public BitmapMonitor(K view, V id) { 28 | mBitmapID = id; 29 | mView = view; 30 | mBitmap = null; 31 | mOnMonitoring = null; 32 | } 33 | 34 | /** 35 | * in decodeImage, you need implements the decode bitmap. 36 | * and set the bitmap to member. 37 | * 38 | * if you want download image use another thread. 39 | * DO NOT block this method. 40 | * 41 | * sample : 42 | * this.mBitmap = null; 43 | * this.mBitmap = BitmapFactory.decodeBitmap(); 44 | * return this.mBitmap; 45 | * 46 | * @return this.mBitmap 47 | */ 48 | protected abstract Bitmap decodeImage(); 49 | 50 | /** 51 | * this method is used to search in bitmap cache. 52 | * 53 | * @return the id. 54 | */ 55 | protected V getBitmapID() { 56 | //TODO return the id. 57 | return this.mBitmapID; 58 | } 59 | 60 | /* (non-Javadoc) 61 | * @see java.lang.Runnable#run() 62 | */ 63 | @Override 64 | public void run() { 65 | // TODO Auto-generated method stub 66 | if (mOnMonitoring != null) { 67 | freshBitmap(mOnMonitoring.isUpdated(this)); 68 | } else { 69 | freshBitmap(false); 70 | } 71 | } 72 | 73 | public OnMonitoring getOnMonitoring() { 74 | return mOnMonitoring; 75 | } 76 | 77 | public void setOnMonitoring(OnMonitoring mOnMonitoring) { 78 | this.mOnMonitoring = mOnMonitoring; 79 | } 80 | 81 | /** 82 | * current view is need fresh with this bitmap. 83 | * @param isOld the view is update to set another bitmap. 84 | */ 85 | protected abstract void freshBitmap(boolean isOld); 86 | 87 | } 88 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/cache/BitmapMonitorThread.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.cache; 2 | 3 | import android.os.Handler; 4 | import android.util.Log; 5 | 6 | import com.guo.android_extend.cache.BitmapMonitor.OnMonitoring; 7 | 8 | import java.util.HashMap; 9 | import java.util.Iterator; 10 | import java.util.LinkedHashMap; 11 | 12 | public class BitmapMonitorThread extends Thread implements OnMonitoring { 13 | private final String TAG = this.getClass().toString(); 14 | 15 | private HashMap> mWidgetMap; 16 | private volatile Thread mBlinker; 17 | private Handler mHandler; 18 | 19 | private BitmapCache mBitmapCache; 20 | private boolean mPause; 21 | 22 | public BitmapMonitorThread(Handler handler) { 23 | // TODO Auto-generated constructor stub 24 | mWidgetMap = new LinkedHashMap>(); 25 | mBitmapCache = new BitmapCache(32, true); 26 | mHandler = handler; 27 | mBlinker = this; 28 | mPause = false; 29 | } 30 | 31 | public void postLoadBitmap(BitmapMonitor monitor) { 32 | monitor.setOnMonitoring(this); 33 | synchronized(mWidgetMap) { 34 | mWidgetMap.put(monitor.mView, monitor); 35 | } 36 | synchronized (this) { 37 | this.notify(); 38 | } 39 | } 40 | 41 | /** 42 | * pause the thread. 43 | * @param sync is sync or not 44 | * @return success or not. 45 | */ 46 | public boolean pause(boolean sync) { 47 | this.mPause = true; 48 | try { 49 | if (sync) { 50 | while (this.mPause) { 51 | sleep(10); 52 | } 53 | return true; 54 | } 55 | } catch (Exception e) { 56 | e.printStackTrace(); 57 | } 58 | return false; 59 | } 60 | 61 | /** 62 | * clear the map. 63 | */ 64 | public void clear() { 65 | synchronized(mWidgetMap) { 66 | mWidgetMap.clear(); 67 | } 68 | } 69 | 70 | /** 71 | * shutdown the thread. 72 | */ 73 | public void shutdown() { 74 | mBlinker = null; 75 | try { 76 | synchronized (this) { 77 | this.notify(); 78 | } 79 | this.join(); 80 | } catch (InterruptedException e) { 81 | // TODO Auto-generated catch block 82 | e.printStackTrace(); 83 | } 84 | } 85 | 86 | /* (non-Javadoc) 87 | * @see java.lang.Thread#run() 88 | */ 89 | @Override 90 | public void run() { 91 | // TODO Auto-generated method stub 92 | Thread thisThread = Thread.currentThread(); 93 | BitmapMonitor monitor = null; 94 | while (mBlinker == thisThread) { 95 | monitor = null; 96 | synchronized(mWidgetMap) { 97 | if (!mWidgetMap.isEmpty()) { 98 | Iterator iterator = mWidgetMap.keySet().iterator(); 99 | if (iterator.hasNext()) { 100 | monitor = mWidgetMap.remove(iterator.next()); 101 | } 102 | } 103 | } 104 | if (monitor == null || mPause) { 105 | synchronized (this) { 106 | try { 107 | if (mPause) { 108 | mPause = false; 109 | } 110 | this.wait(); 111 | } catch (InterruptedException e) { 112 | // TODO Auto-generated catch block 113 | e.printStackTrace(); 114 | } 115 | } 116 | } else { 117 | try { 118 | monitor.mBitmap = mBitmapCache.getBitmap((V)monitor.getBitmapID()); 119 | if (monitor.mBitmap == null) { 120 | monitor.mBitmap = monitor.decodeImage(); 121 | mBitmapCache.putBitmap((V)monitor.getBitmapID(), monitor.mBitmap); 122 | } 123 | } catch (Exception e) { 124 | e.printStackTrace(); 125 | } finally { 126 | mHandler.post(monitor); 127 | } 128 | } 129 | } 130 | 131 | mBitmapCache.destroy(); 132 | Log.d(TAG, "Thread Over"); 133 | } 134 | 135 | /* (non-Javadoc) 136 | * @see com.guo.android_extend.cache.BitmapMonitor.OnMonitoring#isUpdated(java.lang.Object) 137 | */ 138 | @Override 139 | public boolean isUpdated(BitmapMonitor monitor) { 140 | // TODO Auto-generated method stub 141 | synchronized(mWidgetMap) { 142 | if (!mWidgetMap.containsKey(monitor.mView)) { 143 | return false; 144 | } else { 145 | if (mWidgetMap.get(monitor.mView).mBitmapID.equals(monitor.mBitmapID)) { 146 | mWidgetMap.remove(monitor.mView); 147 | return false; 148 | } 149 | } 150 | } 151 | return true; 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/cache/BitmapStructure.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.cache; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.Bitmap.Config; 5 | 6 | public class BitmapStructure { 7 | int mWidth; 8 | int mHeight; 9 | int mFormat; 10 | 11 | public BitmapStructure() { 12 | // TODO Auto-generated constructor stub 13 | mWidth = 0; 14 | mHeight = 0; 15 | mFormat = -1; 16 | } 17 | 18 | public Config getConfig() { 19 | return NativeFormat2Config(mFormat); 20 | } 21 | 22 | public void setConfig(Config config) { 23 | mFormat = Config2NativeFormat(config); 24 | } 25 | 26 | public void setInfo(int width, int height, int format) { 27 | this.mWidth = width; 28 | this.mHeight = height; 29 | this.mFormat = format; 30 | } 31 | 32 | public void setInfo(int width, int height, Config config) { 33 | this.mWidth = width; 34 | this.mHeight = height; 35 | this.mFormat = Config2NativeFormat(config); 36 | } 37 | 38 | public Bitmap createBitmap() { 39 | return Bitmap.createBitmap(mWidth, mHeight, getConfig()); 40 | } 41 | 42 | 43 | /* (non-Javadoc) 44 | * @see java.lang.Object#toString() 45 | */ 46 | @Override 47 | public String toString() { 48 | // TODO Auto-generated method stub 49 | return super.toString() + "W = " + mWidth + ", H = " + mHeight + ", F = " + mFormat; 50 | } 51 | 52 | /** 53 | * convert bitmap config to native bitmap format. 54 | * @param config the iamge format config. 55 | * @return format value. 56 | */ 57 | public static int Config2NativeFormat(Config config) { 58 | switch (config) { 59 | case ARGB_8888 : return 1; 60 | case RGB_565 : return 4; 61 | case ARGB_4444 : return 7; 62 | case ALPHA_8 : return 8; 63 | default :; 64 | } 65 | return -1; 66 | } 67 | 68 | /** 69 | * convert native bitmap format to bitmap config 70 | * @param format format value. 71 | * @return config format. 72 | */ 73 | public static Config NativeFormat2Config(int format) { 74 | switch (format) { 75 | case 1 : return Config.ARGB_8888; 76 | case 4 : return Config.RGB_565; 77 | case 7 : return Config.ARGB_4444; 78 | case 8 : return Config.ALPHA_8; 79 | default :; 80 | } 81 | return null; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/device/Serial.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.device; 2 | 3 | public class Serial { 4 | private final String TAG = this.getClass().getSimpleName(); 5 | 6 | public static final int TYPE_SERIAL = 0; 7 | public static final int TYPE_USB_SERIAL = 1; 8 | private static final int TYPE_FREE_SERIAL = 2; 9 | 10 | public static final int MAX_RECEIVE_SIZE = 255; 11 | public static final int MAX_RECEIVE_TIMEOUT = 1; //S 12 | 13 | /** 14 | * 15 | * @param port 0 == ttyS0 16 | * @param type ttyUSB0 / ttyS0 17 | * @return 18 | */ 19 | private native long initSerial(int port, byte[] dev, int type); 20 | 21 | /** 22 | * 23 | * @param handle 24 | * @param baud_rate 9600 19200 115200 25 | * @param data_bits 7 8 26 | * @param parity 'N' 'O' 'E' 'S' 27 | * @param stop_bits 1 2 28 | * @param vtime 1 (100ms), 0 (0ms) 29 | * @param vmin 255 (read buffer size.) 30 | * @param vtime read wait time. 1=(100ms) 31 | * @param vmin read min buffer size. 32 | * @return 33 | */ 34 | private native int setSerial(long handle, int baud_rate, int data_bits, byte parity, int stop_bits, int vtime, int vmin); 35 | private native int sendData(long handle, byte[] data, int length); 36 | private native int receiveData(long handle, byte[] data, int max, int timeout); 37 | private native int uninitSerial(long handle); 38 | 39 | private long mHandle; 40 | private byte[] mReceive; 41 | 42 | static { 43 | System.loadLibrary("serial"); 44 | } 45 | 46 | public Serial(String dev) { 47 | mHandle = initSerial(-1, dev.getBytes(), TYPE_FREE_SERIAL); 48 | if (mHandle == 0) { 49 | throw new RuntimeException("Open Serial device error!"); 50 | } 51 | mReceive = new byte[MAX_RECEIVE_SIZE]; 52 | } 53 | 54 | public Serial(int port, int type) { 55 | mHandle = initSerial(port, null, type); 56 | if (mHandle == 0) { 57 | throw new RuntimeException("Open Serial device error!"); 58 | } 59 | mReceive = new byte[MAX_RECEIVE_SIZE]; 60 | } 61 | 62 | /** 63 | * 64 | * @param rate 9600 19200 115200 65 | * @param data_bits 7 8 66 | * @param parity 'N' 'O' 'E' 'S' 67 | * @param stop_bits 1 2 68 | * @param vtime 1 (100ms), 0 (0ms) 69 | * @param vmin 255 (read buffer size.) 70 | * @return if success is true. 71 | */ 72 | public boolean setConfig(int rate, int data_bits, byte parity, int stop_bits, int vtime, int vmin ) { 73 | return 0 == setSerial(mHandle, rate, data_bits, parity, stop_bits, vtime, vmin); 74 | } 75 | 76 | public boolean send(byte[] data) { 77 | if (mHandle != 0) { 78 | sendData(mHandle, data, data.length); 79 | return true; 80 | } 81 | return false; 82 | } 83 | 84 | public byte[] receive() { 85 | if (mHandle != 0) { 86 | int size = receiveData(mHandle, mReceive, MAX_RECEIVE_SIZE, MAX_RECEIVE_TIMEOUT); 87 | if (size > 0) { 88 | byte[] raw = new byte[size]; 89 | for (int i = 0; i < raw.length; i++) { 90 | raw[i] = mReceive[i]; 91 | } 92 | return raw; 93 | } 94 | } 95 | return null; 96 | } 97 | 98 | public void destroy() { 99 | if (mHandle != 0) { 100 | uninitSerial(mHandle); 101 | } 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/device/SerialClient.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.device; 2 | 3 | import android.os.Handler; 4 | import android.os.Message; 5 | import android.util.Log; 6 | 7 | import com.guo.android_extend.java.AbsLoop; 8 | 9 | public class SerialClient extends AbsLoop implements SerialInterface { 10 | private final String TAG = this.getClass().toString(); 11 | 12 | public static final int SERIAL_CODE = 0x4000; 13 | public static final int RECEIVE_MSG = 0x4001; 14 | 15 | private Serial mPort; 16 | private SerialListener mSerialListener; 17 | 18 | public SerialClient(int port, int type) { 19 | this(port, type, 115200, 1, 255); 20 | } 21 | 22 | public SerialClient(int port, int type, int rate) { 23 | // TODO Auto-generated constructor stub 24 | this(port, type, rate, 1, 255); 25 | } 26 | 27 | public SerialClient(String dev, int rate, int vtime, int vmin) { 28 | // TODO Auto-generated constructor stub 29 | super(); 30 | try { 31 | mPort = new Serial(dev); 32 | mPort.setConfig(rate, 8, (byte) 'N', 1, vtime, vmin); 33 | } catch (Exception e) { 34 | e.printStackTrace(); 35 | throw new RuntimeException("Create SerialClient ERROR"); 36 | } 37 | mSerialListener = null; 38 | } 39 | 40 | public SerialClient(int port, int type, int rate, int vtime, int vmin) { 41 | // TODO Auto-generated constructor stub 42 | super(); 43 | try { 44 | mPort = new Serial(port, type); 45 | mPort.setConfig(rate, 8, (byte) 'N', 1, vtime, vmin); 46 | } catch (Exception e) { 47 | e.printStackTrace(); 48 | throw new RuntimeException("Create SerialClient ERROR"); 49 | } 50 | mSerialListener = null; 51 | } 52 | 53 | @Override 54 | public void setup() { 55 | 56 | } 57 | 58 | @Override 59 | public void loop() { 60 | byte[] data = mPort.receive(); 61 | if (data != null) { 62 | if (mSerialListener != null) { 63 | mSerialListener.onSerialReceivce(mPort, data); 64 | } 65 | } 66 | } 67 | 68 | @Override 69 | public void over() { 70 | mPort.destroy(); 71 | } 72 | 73 | public boolean sendData(String data) { 74 | return mPort.send(data.getBytes()); 75 | } 76 | 77 | public boolean sendData(byte[] data) { 78 | return mPort.send(data); 79 | } 80 | 81 | public void setSerialListener(SerialListener lis) { 82 | mSerialListener = lis; 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/device/SerialInterface.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.device; 2 | 3 | /** 4 | * Created by gqj3375 on 2015/10/19. 5 | */ 6 | public interface SerialInterface { 7 | public boolean sendData(byte[] data); 8 | public void setSerialListener(SerialListener lis); 9 | } 10 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/device/SerialListener.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.device; 2 | 3 | /** 4 | * Created by gqj3375 on 2015/10/19. 5 | */ 6 | public interface SerialListener { 7 | public void onSerialReceivce(Serial serial, byte[] data); 8 | } 9 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/device/Video.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.device; 2 | 3 | public class Video { 4 | private final String TAG = this.getClass().getSimpleName(); 5 | 6 | private native long initVideo(int port); 7 | private native int setVideo(long handle, int width, int height, int format); 8 | private native int uninitVideo(long handle); 9 | private native int readData(long handle, byte[] data, int size); 10 | 11 | static { 12 | System.loadLibrary("video"); 13 | } 14 | 15 | private long mHandle; 16 | 17 | public Video(int port) throws Exception { 18 | // TODO Auto-generated constructor stub 19 | mHandle = initVideo(port); 20 | if (mHandle == 0) { 21 | throw new RuntimeException("Open Video device error!"); 22 | } 23 | } 24 | 25 | public void setVideo(int width, int height, int format) { 26 | if (mHandle != 0) { 27 | setVideo(mHandle, width, height, format); 28 | } 29 | } 30 | 31 | public void destroy() { 32 | if (mHandle != 0) { 33 | uninitVideo(mHandle); 34 | } 35 | } 36 | 37 | public int readFrame(byte[] data) { 38 | if (mHandle != 0) { 39 | return readData(mHandle, data, data.length); 40 | } 41 | return 0; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/device/VideoClient.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.device; 2 | 3 | import android.os.Handler; 4 | import android.os.Message; 5 | 6 | import com.guo.android_extend.image.ImageConverter; 7 | import com.guo.android_extend.java.AbsLoop; 8 | import com.guo.android_extend.tools.FrameHelper; 9 | 10 | import java.util.LinkedList; 11 | 12 | public class VideoClient extends AbsLoop { 13 | private final String TAG = this.getClass().getSimpleName(); 14 | 15 | public static final int VIDEO_CODE = 0x5000; 16 | public static final int START_MSG = 0x5001; 17 | 18 | private Video mVideo; 19 | private Handler mHandler; 20 | private int mPreviewWidth, mPreviewHeight, mFormat; 21 | private FrameHelper mFrameHelper; 22 | private int mCameraID; 23 | 24 | private LinkedList mBufferQueue; 25 | 26 | private OnCameraListener mOnCameraListener; 27 | 28 | private boolean isPreviewStart; 29 | 30 | public interface OnCameraListener { 31 | /** 32 | * 33 | * @param data image data. 34 | * @param size data size. 35 | * @param camera camera id. 36 | */ 37 | public void onPreview(byte[] data, int size, int camera); 38 | } 39 | 40 | public VideoClient(Handler handle, int port) { 41 | // TODO Auto-generated constructor stub 42 | super(); 43 | mHandler = handle; 44 | mPreviewWidth = 640; 45 | mPreviewHeight = 480; 46 | mFormat = ImageConverter.CP_PAF_NV21; 47 | mCameraID = port; 48 | try { 49 | mVideo = new Video(port); 50 | } catch (Exception e) { 51 | e.printStackTrace(); 52 | throw new RuntimeException("Create VideoClient ERROR"); 53 | } 54 | 55 | mFrameHelper = new FrameHelper(); 56 | 57 | mBufferQueue = new LinkedList(); 58 | int size = ImageConverter.calcImageSize(mPreviewWidth, mPreviewHeight, mFormat); 59 | mBufferQueue.clear(); 60 | mBufferQueue.add(new byte[size]); 61 | 62 | isPreviewStart = false; 63 | } 64 | 65 | public void setPreviewSize(int w, int h) { 66 | mPreviewWidth = w; 67 | mPreviewHeight = h; 68 | } 69 | 70 | /** 71 | * @see ImageConverter 72 | * @param format image format. 73 | */ 74 | public void setPreviewFormat(int format) { 75 | mFormat = format; 76 | } 77 | 78 | @Override 79 | public void setup() { 80 | Message msg = new Message(); 81 | msg.what = VIDEO_CODE; 82 | msg.arg1 = START_MSG; 83 | mHandler.sendMessage(msg); 84 | 85 | int size = ImageConverter.calcImageSize(mPreviewWidth, mPreviewHeight, mFormat); 86 | mBufferQueue.clear(); 87 | mBufferQueue.add(new byte[size]); 88 | mVideo.setVideo(mPreviewWidth, mPreviewHeight, mFormat); 89 | } 90 | 91 | @Override 92 | public void loop() { 93 | byte[] data = mBufferQueue.poll(); 94 | int size = mVideo.readFrame(data); 95 | if (mOnCameraListener != null && isPreviewStart) { 96 | mOnCameraListener.onPreview(data, size, mCameraID); 97 | } 98 | mBufferQueue.offer(data); 99 | mFrameHelper.printFPS(); 100 | } 101 | 102 | @Override 103 | public void over() { 104 | mVideo.destroy(); 105 | mBufferQueue.clear(); 106 | } 107 | 108 | public void addCallbackBuffer(byte[] data) { 109 | mBufferQueue.offer(data); 110 | } 111 | 112 | public void startPreview() { 113 | isPreviewStart = true; 114 | } 115 | 116 | public void stopPreview() { 117 | isPreviewStart = false; 118 | } 119 | 120 | public void setOnCameraListener(OnCameraListener l) { 121 | mOnCameraListener = l; 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/image/ImageConverter.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.image; 2 | 3 | import android.graphics.Bitmap; 4 | 5 | public class ImageConverter { 6 | private final String TAG = this.getClass().getSimpleName(); 7 | 8 | public final static int CP_MJPEG = 0xA000; 9 | public final static int CP_PNG = 0xB000; 10 | public final static int CP_RGBA8888 = 1; 11 | public final static int CP_RGB565 = 4; 12 | public final static int CP_RGBA4444 = 7; 13 | public final static int CP_PAF_NV21 = 0x802; 14 | public final static int CP_PAF_NV12 = 0x801; 15 | public final static int CP_PAF_YUYV = 0x501; 16 | public final static int CP_PAF_I420 = 0x601; 17 | public final static int CP_PAF_BGR24 = 0x201; 18 | 19 | 20 | private native long image_init(int width, int height, int format); 21 | private native int image_convert(long handler, Bitmap bitmap, byte[] data); 22 | private native int image_uninit(long handler); 23 | 24 | private long handle; 25 | 26 | static { 27 | System.loadLibrary("image"); 28 | } 29 | 30 | public ImageConverter() { 31 | // TODO Auto-generated constructor stub 32 | handle = -1; 33 | } 34 | 35 | /** 36 | * 37 | * @param width convert image width 38 | * @param height convert image height 39 | * @param format target to convert. 40 | * @return success is true. 41 | */ 42 | public boolean initial(int width, int height, int format) { 43 | handle = image_init(width, height, format); 44 | return handle != -1; 45 | } 46 | 47 | public boolean convert(Bitmap src, byte[] data) { 48 | return 0 == image_convert(handle, src, data); 49 | } 50 | 51 | public void destroy() { 52 | if (handle != -1) { 53 | image_uninit(handle); 54 | } 55 | } 56 | 57 | public static int calcImageSize(int w, int h, int format) { 58 | switch (format) { 59 | case CP_PAF_NV21 : 60 | case CP_PAF_NV12: 61 | return w * h * 3 / 2; 62 | case CP_PAF_YUYV: 63 | case CP_RGB565: 64 | case CP_RGBA4444: 65 | return w * h * 2; 66 | case CP_RGBA8888 : 67 | case CP_MJPEG: 68 | return w * h * 4; 69 | default :; 70 | } 71 | return 0; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/AbsLoop.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java; 2 | 3 | /** 4 | * Created by gqj3375 on 2015/12/17. 5 | */ 6 | public abstract class AbsLoop extends Thread { 7 | volatile Thread mBlinker = this; 8 | 9 | abstract public void setup(); 10 | 11 | abstract public void loop(); 12 | 13 | abstract public void over(); 14 | 15 | @Override 16 | public void run() { 17 | Thread thisThread = Thread.currentThread(); 18 | setup(); 19 | while (mBlinker == thisThread) { 20 | loop(); 21 | } 22 | over(); 23 | } 24 | 25 | public void break_loop() { 26 | mBlinker = null; 27 | } 28 | 29 | public void shutdown() { 30 | break_loop(); 31 | try { 32 | if (this != Thread.currentThread()) { 33 | synchronized (this) { 34 | this.notifyAll(); 35 | } 36 | this.join(); 37 | } 38 | } catch (InterruptedException e) { 39 | e.printStackTrace(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/ExtByteArrayOutputStream.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | 5 | /** 6 | * Created by Guo on 2015/12/26. 7 | */ 8 | public class ExtByteArrayOutputStream extends ByteArrayOutputStream { 9 | 10 | public ExtByteArrayOutputStream() { 11 | super(); 12 | } 13 | 14 | public ExtByteArrayOutputStream(int size) { 15 | super(size); 16 | } 17 | 18 | public byte[] getByteArray() { 19 | return super.buf; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/ExtByteTools.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java; 2 | 3 | /** 4 | * Created by gqj3375 on 2017/4/28. 5 | */ 6 | 7 | public class ExtByteTools { 8 | 9 | public static byte[] convert_from_int(int val) { 10 | byte[] size = new byte[4]; 11 | size[0] = (byte)(val >> 24); 12 | size[1] = (byte)(val >> 16); 13 | size[2] = (byte)(val >> 8); 14 | size[3] = (byte)(val >> 0); 15 | return size; 16 | } 17 | public static int convert_to_int(byte[] val) { 18 | int size = 0; 19 | if (val.length >= 4) { 20 | size |= ((val[0] & 0xFF) << 24); 21 | size |= ((val[1] & 0xFF) << 16); 22 | size |= ((val[2] & 0xFF) << 8); 23 | size |= ((val[3] & 0xFF) << 0); 24 | } 25 | return size; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/ExtInputStream.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java; 2 | 3 | 4 | import java.io.BufferedInputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | 8 | /** 9 | * Created by gqj3375 on 2017/7/11. 10 | */ 11 | 12 | public class ExtInputStream extends BufferedInputStream { 13 | 14 | public ExtInputStream(InputStream in) { 15 | super(in); 16 | } 17 | 18 | public ExtInputStream(InputStream in, int size) { 19 | super(in, size); 20 | } 21 | 22 | public String readString() throws IOException { 23 | byte[] size = new byte[4]; 24 | if (read(size) > 0) { 25 | byte[] name = new byte[ExtByteTools.convert_to_int(size)]; 26 | if (name.length == read(name)) { 27 | return new String(name); 28 | } 29 | } 30 | return null; 31 | } 32 | 33 | public byte[] readBytes() throws IOException { 34 | byte[] size = new byte[4]; 35 | if (read(size) > 0) { 36 | byte[] data = new byte[ExtByteTools.convert_to_int(size)]; 37 | if (data.length == read(data)) { 38 | return data; 39 | } 40 | } 41 | return null; 42 | } 43 | 44 | public boolean readBytes(byte[] data) throws IOException { 45 | byte[] size = new byte[4]; 46 | if (read(size) > 0) { 47 | if (ExtByteTools.convert_to_int(size) == data.length) { 48 | if (data.length == read(data)) { 49 | return true; 50 | } 51 | } 52 | } 53 | return false; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/ExtOutputStream.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java; 2 | 3 | 4 | import java.io.BufferedOutputStream; 5 | import java.io.IOException; 6 | import java.io.OutputStream; 7 | 8 | /** 9 | * Created by gqj3375 on 2017/7/11. 10 | */ 11 | 12 | public class ExtOutputStream extends BufferedOutputStream { 13 | 14 | public ExtOutputStream(OutputStream out) { 15 | super(out); 16 | } 17 | 18 | public ExtOutputStream(OutputStream out, int size) { 19 | super(out, size); 20 | } 21 | 22 | public boolean writeString(String name) throws IOException { 23 | write(ExtByteTools.convert_from_int(name.getBytes().length)); 24 | write(name.getBytes()); 25 | return true; 26 | } 27 | 28 | public boolean writeBytes(byte[] data) throws IOException { 29 | write(ExtByteTools.convert_from_int(data.length)); 30 | write(data); 31 | return false; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/LRULinkedHashMap.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map; 5 | 6 | public class LRULinkedHashMap extends LinkedHashMap { 7 | /** 8 | * @Version InitVersion. 10000L 9 | */ 10 | private static final long serialVersionUID = 10000L; 11 | 12 | private int mMaxSize; 13 | 14 | public LRULinkedHashMap(int initialCapacity, float loadFactor, 15 | boolean accessOrder) { 16 | super(initialCapacity, loadFactor, accessOrder); 17 | // TODO Auto-generated constructor stub 18 | mMaxSize = initialCapacity; 19 | } 20 | 21 | @Override 22 | protected boolean removeEldestEntry(Map.Entry eldest) { 23 | // TODO Auto-generated method stub 24 | return size() > mMaxSize; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/NetWorkFile.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network; 2 | 3 | import java.io.File; 4 | 5 | /** 6 | * Created by gqj3375 on 2014/12/31. 7 | */ 8 | public class NetWorkFile { 9 | 10 | protected String mLocalDir; 11 | protected String mUrl; 12 | 13 | public NetWorkFile(String mLocalDir, String mUrl) { 14 | this.mLocalDir = mLocalDir; 15 | this.mUrl = mUrl; 16 | } 17 | 18 | public void setLocalDir(String mLocalDir) { 19 | this.mLocalDir = mLocalDir; 20 | } 21 | 22 | public void setUrl(String mUrl) { 23 | this.mUrl = mUrl; 24 | } 25 | 26 | /** 27 | * get file name from url. 28 | * @return remote file name 29 | */ 30 | public synchronized String getRemoteFileName() { 31 | String fileName = null; 32 | try { 33 | String temp[] = mUrl.replaceAll("////", "/").split("/"); 34 | if (temp.length > 1) { 35 | fileName = temp[temp.length - 1]; 36 | } 37 | } catch (Exception e) { 38 | e.printStackTrace(); 39 | } 40 | return fileName; 41 | } 42 | 43 | /** 44 | * get local file abstract path. 45 | * @return local file path 46 | */ 47 | public synchronized String getLocalFile() { 48 | String localFile = null; 49 | String fileName = getRemoteFileName(); 50 | if (mLocalDir == null) { 51 | return fileName; 52 | } 53 | if (mLocalDir.endsWith("/")) { 54 | localFile = mLocalDir + fileName; 55 | } else { 56 | localFile = mLocalDir + "/" + fileName; 57 | } 58 | return localFile; 59 | } 60 | 61 | public String createCacheFile() { 62 | String fileName = getRemoteFileName(); 63 | if (mLocalDir.endsWith("/")) { 64 | fileName = mLocalDir + fileName +"_cache"; 65 | } else { 66 | fileName = mLocalDir + "/" + fileName +"_cache"; 67 | } 68 | try { 69 | File dir = new File(mLocalDir); 70 | dir.mkdirs(); 71 | 72 | File file = new File(fileName); 73 | if (file.exists()) { 74 | file.delete(); 75 | } 76 | } catch (Exception e) { 77 | e.printStackTrace(); 78 | } 79 | return fileName; 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/http/Downloader.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.http; 2 | 3 | import com.guo.android_extend.java.network.NetWorkFile; 4 | 5 | import java.io.File; 6 | import java.io.FileOutputStream; 7 | import java.io.InputStream; 8 | import java.io.OutputStream; 9 | import java.net.HttpURLConnection; 10 | import java.net.URL; 11 | 12 | /** 13 | * Created by gqj3375 on 2014/12/31. 14 | */ 15 | public abstract class Downloader extends NetWorkFile implements Runnable { 16 | 17 | private T mID; 18 | private OnMonitoring mOnMonitoring; 19 | 20 | public static int DOWNLOAD_OK = 0; 21 | public static int DOWNLOAD_ERR_NET = 1; 22 | public static int DOWNLOAD_ERR_IO = 2; 23 | public static int DOWNLOAD_ERR_FILE = 3; 24 | public static int DOWNLOAD_ERR_USER = 4; 25 | /** 26 | * 27 | * @author gqj3375 28 | * 29 | */ 30 | protected interface OnMonitoring { 31 | /** 32 | * current view is need fresh with this bitmap. 33 | * @param isSuccess the view is update to set another bitmap. 34 | * @param content downloader object. 35 | */ 36 | public void onFinish(Downloader content, boolean isSuccess); 37 | } 38 | 39 | protected Downloader(String mLocalDir, String mUrl, T mID) { 40 | super(mLocalDir, mUrl); 41 | this.mID = mID; 42 | } 43 | 44 | public T getID() {return mID;} 45 | 46 | final public void setOnMonitoring(OnMonitoring mOnMonitoring) { 47 | this.mOnMonitoring = mOnMonitoring; 48 | } 49 | 50 | public void onTaskOver(Downloader context, int error) { 51 | if (mOnMonitoring != null) { 52 | mOnMonitoring.onFinish(this, true); 53 | } 54 | //TODO USERTASK. 55 | } 56 | 57 | public boolean onDownloadFinish(Downloader context, String cache) { 58 | //TODO USER TASK. 59 | return true; 60 | } 61 | 62 | public abstract void onDownloadUpdate(Downloader context, int percent); 63 | 64 | 65 | /* (non-Javadoc) 66 | * @see java.lang.Runnable#run() 67 | */ 68 | @Override 69 | public void run() { 70 | // TODO Auto-generated method stub 71 | String cache = createCacheFile(); 72 | try { 73 | URL url = new URL(mUrl); 74 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 75 | conn.setConnectTimeout(30000); 76 | conn.setReadTimeout(30000); 77 | conn.setInstanceFollowRedirects(true); 78 | 79 | int total = conn.getContentLength(); 80 | int cur = 0, pre = 0; 81 | InputStream is = conn.getInputStream(); 82 | File file = new File(cache); 83 | OutputStream os = new FileOutputStream(file); 84 | 85 | byte[] bytes = new byte[1024 * 1024]; 86 | int length = 0; 87 | try { 88 | while ((length = is.read(bytes, 0, 1024 * 1024)) != -1) { 89 | os.write(bytes, 0, length); 90 | cur += length; 91 | //update progress 92 | if (cur * 100 > (pre + 5) * total) { 93 | pre = cur * 100 / total; 94 | onDownloadUpdate(this, pre >= 100 ? 100 : pre); 95 | } 96 | Thread.sleep(10); 97 | } 98 | 99 | } catch (Exception e) { 100 | e.printStackTrace(); 101 | onTaskOver(this, DOWNLOAD_ERR_IO); 102 | return ; 103 | } finally { 104 | os.close(); 105 | is.close(); 106 | conn.disconnect(); 107 | } 108 | 109 | if (!onDownloadFinish(this, cache)) { 110 | onTaskOver(this, DOWNLOAD_ERR_USER); 111 | } else { 112 | if (!file.renameTo(new File(getLocalFile()))) { 113 | onTaskOver(this, DOWNLOAD_ERR_FILE); 114 | } else { 115 | onTaskOver(this, DOWNLOAD_OK); 116 | } 117 | } 118 | } catch (Exception ex) { 119 | ex.printStackTrace(); 120 | onTaskOver(this, DOWNLOAD_ERR_NET); 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/http/DownloaderManager.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.http; 2 | 3 | import android.util.Log; 4 | 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.concurrent.ExecutorService; 8 | import java.util.concurrent.Executors; 9 | 10 | /** 11 | * Created by gqj3375 on 2014/12/31. 12 | */ 13 | public class DownloaderManager implements Downloader.OnMonitoring { 14 | private String TAG = this.getClass().getSimpleName(); 15 | 16 | private ExecutorService mThreadPool; 17 | private int mMaxTask; 18 | private List mDataPool; 19 | 20 | public DownloaderManager() { 21 | this(5); 22 | } 23 | 24 | public DownloaderManager(int maxTask) { 25 | // TODO Auto-generated constructor stub 26 | mDataPool = new LinkedList(); 27 | mMaxTask = maxTask; 28 | mThreadPool = Executors.newFixedThreadPool(mMaxTask); 29 | } 30 | 31 | public boolean postDownload(Downloader downloader) { 32 | if (mThreadPool.isShutdown()) { 33 | Log.e(TAG, "already shutdown"); 34 | return false; 35 | } 36 | synchronized(mDataPool) { 37 | if (!mDataPool.contains(downloader.getID())) { 38 | downloader.setOnMonitoring(this); 39 | mDataPool.add(downloader.getID()); 40 | mThreadPool.execute(downloader); 41 | return true; 42 | } 43 | } 44 | return false; 45 | } 46 | 47 | /** 48 | * shutdown the thread. 49 | */ 50 | public void shutdown() { 51 | try { 52 | mDataPool.clear(); 53 | mThreadPool.shutdownNow(); 54 | } catch (Exception e) { 55 | // TODO Auto-generated catch block 56 | e.printStackTrace(); 57 | } 58 | } 59 | 60 | @Override 61 | public void onFinish(Downloader downloader, boolean isSuccess) { 62 | synchronized(mDataPool) { 63 | mDataPool.remove(downloader.getID()); 64 | } 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/socket/Data/AbsTransmitter.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.socket.Data; 2 | 3 | import com.guo.android_extend.java.network.socket.Transfer.Receiver; 4 | import com.guo.android_extend.java.network.socket.Transfer.Sender; 5 | 6 | import java.io.DataInputStream; 7 | import java.io.DataOutputStream; 8 | 9 | /** 10 | * Created by gqj3375 on 2016/3/21. 11 | */ 12 | public abstract class AbsTransmitter { 13 | private String TAG = this.getClass().getSimpleName(); 14 | 15 | public final static int TYPE_FILE = 0x70EF0000; 16 | public final static int TYPE_BYTE = 0x71EF0000; 17 | public final static int TYPE_BYTE_8B = 0x80EF0000; 18 | public final static int TYPE_BYTE_16B = 0x81EF0000; 19 | public final static int TYPE_BYTE_32B = 0x82EF0000; 20 | public final static int TYPE_BYTE_16K = 0x91EF0000; 21 | public final static int TYPE_BYTE_32K = 0x92EF0000; 22 | public final static int TYPE_BYTE_USER = 0x93EF0000; 23 | public final static int TYPE_END_CODE = 0xEADC0000; 24 | public final static int MAX_PACKAGE_SIZE = (1 << 24); //16M 25 | 26 | protected int mType; 27 | protected Sender.OnSenderListener mOnSenderListener; 28 | protected Receiver.OnReceiverListener mOnReceiverListener; 29 | 30 | public AbsTransmitter(int type) { 31 | mType = type; 32 | } 33 | 34 | public int getType() { 35 | return mType; 36 | } 37 | 38 | public void setOnSenderListener(Sender.OnSenderListener mOnSenderListener) { 39 | this.mOnSenderListener = mOnSenderListener; 40 | } 41 | 42 | public void setOnReceiverListener(Receiver.OnReceiverListener mOnReceiverListener) { 43 | this.mOnReceiverListener = mOnReceiverListener; 44 | } 45 | 46 | public static int big_to_sml(int val) { 47 | byte[] data = int_to_bytes_big(val); 48 | return bytes_to_int_sml(data); 49 | } 50 | 51 | public static int sml_to_big(int val) { 52 | byte[] data = int_to_bytes_sml(val); 53 | return bytes_to_int_big(data); 54 | } 55 | 56 | public static byte[] int_to_bytes_sml(int val) { 57 | byte[] data = new byte[4]; 58 | data[0] = (byte)((val >> 0) & 0xFF); 59 | data[1] = (byte)((val >> 8) & 0xFF); 60 | data[2] = (byte)((val >> 16) & 0xFF); 61 | data[3] = (byte)((val >> 24) & 0xFF); 62 | return data; 63 | } 64 | 65 | public static byte[] int_to_bytes_big(int val) { 66 | byte[] data = new byte[4]; 67 | data[0] = (byte)((val >> 24) & 0xFF); 68 | data[1] = (byte)((val >> 16) & 0xFF); 69 | data[2] = (byte)((val >> 8) & 0xFF); 70 | data[3] = (byte)((val >> 0) & 0xFF); 71 | return data; 72 | } 73 | 74 | public static byte[] short_to_bytes_big(short val) { 75 | byte[] data = new byte[2]; 76 | data[0] = (byte)((val >> 8) & 0xFF); 77 | data[1] = (byte)((val >> 0) & 0xFF); 78 | return data; 79 | } 80 | 81 | public static int bytes_to_int_big(byte[] val) { 82 | int data = 0; 83 | data |= ((val[0] & 0xFF)<< 24); 84 | data |= ((val[1] & 0xFF)<< 16); 85 | data |= ((val[2] & 0xFF)<< 8); 86 | data |= ((val[3] & 0xFF)<< 0); 87 | return data; 88 | } 89 | 90 | public static int bytes_to_int_big(byte[] val, int offset) { 91 | int data = 0; 92 | data |= ((val[offset + 0] & 0xFF)<< 24); 93 | data |= ((val[offset + 1] & 0xFF)<< 16); 94 | data |= ((val[offset + 2] & 0xFF)<< 8); 95 | data |= ((val[offset + 3] & 0xFF)<< 0); 96 | return data; 97 | } 98 | 99 | public static int bytes_to_int_sml(byte[] val) { 100 | int data = 0; 101 | data |= ((val[0] & 0xFF) << 0); 102 | data |= ((val[1] & 0xFF) << 8); 103 | data |= ((val[2] & 0xFF) << 16); 104 | data |= ((val[3] & 0xFF) << 24); 105 | return data; 106 | } 107 | 108 | public static int bytes_to_int_sml(byte[] val, int offset) { 109 | int data = 0; 110 | data |= ((val[offset + 0] & 0xFF) << 0); 111 | data |= ((val[offset + 1] & 0xFF) << 8); 112 | data |= ((val[offset + 2] & 0xFF) << 16); 113 | data |= ((val[offset + 3] & 0xFF) << 24); 114 | return data; 115 | } 116 | 117 | public abstract String getName(); 118 | 119 | public abstract int send(DataOutputStream stream, byte[] mBuffer); 120 | 121 | public abstract int recv(DataInputStream stream, byte[] mBuffer); 122 | 123 | /** 124 | * use for send 125 | * @return input stream 126 | */ 127 | public abstract DataInputStream getDataInputStream(); 128 | 129 | /** 130 | * use for recv 131 | * @return output stream 132 | */ 133 | public abstract DataOutputStream getDataOutputStream(); 134 | } 135 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/socket/Data/TransmitByte.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.socket.Data; 2 | 3 | import com.guo.android_extend.java.ExtByteArrayOutputStream; 4 | import com.guo.android_extend.java.network.socket.OnSocketListener; 5 | import com.guo.android_extend.tools.LogcatHelper; 6 | 7 | import java.io.BufferedInputStream; 8 | import java.io.BufferedOutputStream; 9 | import java.io.ByteArrayInputStream; 10 | import java.io.DataInputStream; 11 | import java.io.DataOutputStream; 12 | import java.io.IOException; 13 | 14 | /** 15 | * Created by gqj3375 on 2016/3/21. 16 | */ 17 | public class TransmitByte extends AbsTransmitter { 18 | private String TAG = this.getClass().getSimpleName(); 19 | 20 | protected byte[] mData; 21 | protected int mLength; 22 | 23 | //receive 24 | public TransmitByte() { 25 | super(TYPE_BYTE); 26 | mData = null; 27 | mLength = 0; 28 | } 29 | 30 | //send 31 | public TransmitByte(byte[] data, int length) { 32 | super(TYPE_BYTE); 33 | if (length > MAX_PACKAGE_SIZE) { 34 | throw new RuntimeException("byte data length is bigger than 16M"); 35 | } 36 | mLength = length; 37 | mData = new byte[mLength + 12]; 38 | System.arraycopy(int_to_bytes_big(getType()), 0, mData, 0, 4); //type 39 | System.arraycopy(int_to_bytes_big(mLength), 0, mData, 4, 4); //length 40 | System.arraycopy(data, 0, mData, 8, mLength); //data 41 | System.arraycopy(int_to_bytes_big(TYPE_END_CODE), 0, mData, 8 + mLength, 4); //end code 42 | } 43 | 44 | @Override 45 | public String getName() { 46 | return "byte"; 47 | } 48 | 49 | public byte[] getData() { 50 | return mData; 51 | } 52 | 53 | public int send(DataOutputStream stream, byte[] mBuffer) { 54 | int ret = OnSocketListener.ERROR_NONE; 55 | 56 | DataInputStream input = getDataInputStream(); 57 | if (input == null) { 58 | LogcatHelper.e(TAG, "loop: Bad object!"); 59 | return OnSocketListener.ERROR_OBJECT_UNKNOWN; 60 | } 61 | 62 | try { 63 | for (int size = 0, read = 0; size < mData.length; size += read) { 64 | read = input.read(mBuffer); 65 | stream.write(mBuffer, 0, read); 66 | if (mOnSenderListener != null) { 67 | mOnSenderListener.onSendProcess(this, size + read, mData.length); 68 | } 69 | } 70 | stream.flush(); 71 | } catch (Exception e) { 72 | LogcatHelper.e(TAG, "loop:" + e.getMessage()); 73 | ret = OnSocketListener.ERROR_SOCKET_TRANSFER; 74 | } 75 | 76 | try { 77 | input.close(); 78 | } catch (IOException e) { 79 | LogcatHelper.e(TAG, "loop:" + e.getMessage()); 80 | ret = OnSocketListener.ERROR_STREAM_CLOSE; 81 | } 82 | 83 | return ret; 84 | } 85 | 86 | public int recv(DataInputStream stream, byte[] mBuffer) { 87 | try { 88 | mLength = stream.readInt(); 89 | DataOutputStream output = this.getDataOutputStream(); 90 | for (int size = 0, read = 0; size < mLength; size += read) { 91 | read = stream.read(mBuffer, 0, Math.min((int) mLength - size, mBuffer.length)); 92 | output.write(mBuffer, 0, read); 93 | if (mOnReceiverListener != null) { 94 | mOnReceiverListener.onReceiveProcess(this, size, (int) mLength); 95 | } 96 | } 97 | if (stream.readInt() != TYPE_END_CODE) { 98 | throw new Exception("received end code error!"); 99 | } 100 | output.flush(); 101 | output.close(); 102 | //finish 103 | if (mOnReceiverListener != null) { 104 | mOnReceiverListener.onReceiveProcess(this, (int) mLength, (int) mLength); 105 | } 106 | } catch (Exception e) { 107 | LogcatHelper.e(TAG, "loop:" + e.getMessage()); 108 | if (mOnReceiverListener != null) { 109 | mOnReceiverListener.onException(OnSocketListener.ERROR_STREAM_CLOSE); 110 | } 111 | } 112 | return OnSocketListener.ERROR_NONE; 113 | } 114 | 115 | @Override 116 | public DataInputStream getDataInputStream() { 117 | return new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(mData, 0, mData.length))); 118 | } 119 | 120 | @Override 121 | public DataOutputStream getDataOutputStream() { 122 | ExtByteArrayOutputStream out = new ExtByteArrayOutputStream(mLength); 123 | mData = out.getByteArray(); 124 | return new DataOutputStream(new BufferedOutputStream(out)); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/socket/Data/TransmitFile.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.socket.Data; 2 | 3 | import com.guo.android_extend.java.network.NetWorkFile; 4 | import com.guo.android_extend.java.network.socket.OnSocketListener; 5 | import com.guo.android_extend.tools.LogcatHelper; 6 | 7 | import java.io.BufferedInputStream; 8 | import java.io.BufferedOutputStream; 9 | import java.io.DataInputStream; 10 | import java.io.DataOutputStream; 11 | import java.io.File; 12 | import java.io.FileInputStream; 13 | import java.io.FileNotFoundException; 14 | import java.io.FileOutputStream; 15 | import java.io.IOException; 16 | 17 | /** 18 | * Created by Guo on 2015/12/26. 19 | */ 20 | public class TransmitFile extends AbsTransmitter { 21 | private String TAG = this.getClass().getSimpleName(); 22 | 23 | NetWorkFile mNetWorkFile; 24 | String mName; 25 | 26 | byte[] mData; 27 | int mLength; 28 | 29 | //receive 30 | public TransmitFile(String local_dir, String remote_file) { 31 | super(TYPE_FILE); 32 | mNetWorkFile = new NetWorkFile(local_dir, remote_file); 33 | mName = null; 34 | mLength = 0; 35 | } 36 | 37 | //send 38 | public TransmitFile(String local_file) { 39 | super(TYPE_FILE); 40 | mNetWorkFile = new NetWorkFile(null, local_file); 41 | mName = local_file; 42 | long max = new File(mName).length(); 43 | if (max > Integer.MAX_VALUE) { 44 | throw new RuntimeException("file size not support!"); 45 | } 46 | mLength = (int)(max); 47 | mData = new byte[mName.getBytes().length + 10]; 48 | System.arraycopy(int_to_bytes_big(getType()), 0, mData, 0, 4); //type 49 | System.arraycopy(short_to_bytes_big((short) mName.getBytes().length), 0, mData, 4, 2); // name length 50 | System.arraycopy(mName.getBytes(), 0, mData, 6, mName.getBytes().length); //name 51 | System.arraycopy(int_to_bytes_big(mLength), 0, mData, mName.getBytes().length + 6, 4); //length 52 | } 53 | 54 | @Override 55 | public int send(DataOutputStream stream, byte[] mBuffer) { 56 | int ret = OnSocketListener.ERROR_NONE; 57 | 58 | DataInputStream input = getDataInputStream(); 59 | if (input == null) { 60 | LogcatHelper.e("TransmitInterface", "loop: Bad object!"); 61 | return OnSocketListener.ERROR_OBJECT_UNKNOWN; 62 | } 63 | 64 | try { 65 | //write head 66 | stream.write(mData); 67 | //write data 68 | for (int size = 0, read = 0; size < mLength; size += read) { 69 | read = input.read(mBuffer); 70 | stream.write(mBuffer, 0, read); 71 | if (mOnSenderListener != null) { 72 | mOnSenderListener.onSendProcess(this, size + read, mLength); 73 | } 74 | } 75 | stream.write(int_to_bytes_big(TYPE_END_CODE)); 76 | stream.flush(); 77 | } catch (Exception e) { 78 | LogcatHelper.e("TransmitInterface", "loop:" + e.getMessage()); 79 | ret = OnSocketListener.ERROR_SOCKET_TRANSFER; 80 | } 81 | 82 | try { 83 | input.close(); 84 | } catch (IOException e) { 85 | LogcatHelper.e("TransmitInterface", "loop:" + e.getMessage()); 86 | ret = OnSocketListener.ERROR_STREAM_CLOSE; 87 | } 88 | 89 | return ret; 90 | } 91 | 92 | @Override 93 | public int recv(DataInputStream stream, byte[] mBuffer) { 94 | try { 95 | String name = stream.readUTF(); 96 | mNetWorkFile.setUrl(name); 97 | mName = mNetWorkFile.getLocalFile(); 98 | 99 | int length = stream.readInt(); 100 | DataOutputStream output = this.getDataOutputStream(); 101 | for (int size = 0, read = 0; size < length; size += read) { 102 | read = stream.read(mBuffer, 0, Math.min((int) length - size, mBuffer.length)); 103 | output.write(mBuffer, 0, read); 104 | if (mOnReceiverListener != null) { 105 | mOnReceiverListener.onReceiveProcess(this, size, (int) length); 106 | } 107 | } 108 | if (stream.readInt() != TYPE_END_CODE) { 109 | throw new Exception("received end code error!"); 110 | } 111 | output.flush(); 112 | output.close(); 113 | //finish 114 | if (mOnReceiverListener != null) { 115 | mOnReceiverListener.onReceiveProcess(this, (int) length, (int) length); 116 | } 117 | } catch (Exception e) { 118 | LogcatHelper.e("", "loop:" + e.getMessage()); 119 | if (mOnReceiverListener != null) { 120 | mOnReceiverListener.onException(OnSocketListener.ERROR_STREAM_CLOSE); 121 | } 122 | } 123 | return OnSocketListener.ERROR_NONE; 124 | } 125 | 126 | public String getName() { 127 | return mName; 128 | } 129 | 130 | @Override 131 | public DataInputStream getDataInputStream() { 132 | FileInputStream fis = null; 133 | try { 134 | fis = new FileInputStream(new File(mName)); 135 | } catch (FileNotFoundException e) { 136 | e.printStackTrace(); 137 | return null; 138 | } 139 | return new DataInputStream(new BufferedInputStream(fis)); 140 | } 141 | 142 | @Override 143 | public DataOutputStream getDataOutputStream() { 144 | FileOutputStream fis = null; 145 | try { 146 | fis = new FileOutputStream(new File(mName)); 147 | } catch (FileNotFoundException e) { 148 | e.printStackTrace(); 149 | return null; 150 | } 151 | return new DataOutputStream(new BufferedOutputStream(fis)); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/socket/OnSocketListener.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.socket; 2 | 3 | import com.guo.android_extend.java.network.socket.Data.AbsTransmitter; 4 | 5 | import java.net.Socket; 6 | 7 | /** 8 | * Created by gqj3375 on 2015/12/18. 9 | */ 10 | public interface OnSocketListener { 11 | public static int ERROR_NONE = 0; 12 | public static int ERROR_CONNECT_REJECT = 1; 13 | public static int ERROR_CONNECT_EXCEPTION = 2; 14 | public static int ERROR_SOCKET_STREAM = 3; 15 | public static int ERROR_SOCKET_TRANSFER = 4; 16 | public static int ERROR_SOCKET_CLOSE = 5; 17 | public static int ERROR_OBJECT_UNKNOWN = 6; 18 | public static int ERROR_STREAM_CLOSE = 7; 19 | 20 | public static int EVENT_RECEIVER_CONNECTED = 1000; 21 | public static int EVENT_SENDER_CONNECTED = 1001; 22 | public static int EVENT_RECEIVER_DISCONNECTED = 1002; 23 | public static int EVENT_SENDER_DISCONNECTED = 1003; 24 | public static int EVENT_STOP_ACCEPT = 1004; 25 | 26 | public void onSocketException(int e); 27 | public void onSocketEvent(Socket socket, int e); 28 | 29 | public void onReceiveProcess(AbsTransmitter obj, int percent); 30 | public void onReceived(AbsTransmitter obj); 31 | public void onSendProcess(AbsTransmitter obj, int percent); 32 | public void onSended(AbsTransmitter obj); 33 | } 34 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/socket/SocketModule.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.socket; 2 | 3 | import java.io.File; 4 | 5 | /** 6 | * Created by gqj3375 on 2015/12/22. 7 | */ 8 | public class SocketModule { 9 | private String TAG = this.getClass().getSimpleName(); 10 | 11 | public static final int PORT = 4203; 12 | 13 | private String mLocalDir ; 14 | private String mIP; 15 | private int mPort; 16 | 17 | private OnSocketListener mOnSocketListener; 18 | private SocketServer mSocketServer; 19 | private SocketClient mSocketClient; 20 | 21 | public SocketModule(String local_dir, int port) { 22 | File file = new File(local_dir); 23 | file.mkdirs(); 24 | mLocalDir = local_dir; 25 | mOnSocketListener = null; 26 | mSocketServer = new SocketServer(mLocalDir, port); 27 | mSocketServer.setOnSocketListener(mOnSocketListener); 28 | mSocketServer.start(); 29 | mSocketClient = null; 30 | } 31 | 32 | public SocketModule(String local_dir) { 33 | this(local_dir, PORT); 34 | } 35 | 36 | public void connect(String ip) { 37 | connect(ip, PORT); 38 | } 39 | 40 | /** 41 | * connect to other client 42 | * @param ip target ip 43 | * @param port target port 44 | */ 45 | public void connect(String ip, int port) { 46 | mIP = ip; 47 | mPort = port; 48 | if (mSocketClient != null) { 49 | mSocketClient.shutdown(); 50 | } 51 | mSocketClient = new SocketClient(mLocalDir, ip, port); 52 | mSocketClient.setOnSocketListener(mOnSocketListener); 53 | mSocketClient.start(); 54 | } 55 | 56 | /** 57 | * close sender connect 58 | */ 59 | public void disconnect() { 60 | if (mSocketClient != null) { 61 | mSocketClient.shutdown(); 62 | } 63 | mSocketClient = null; 64 | } 65 | 66 | /** 67 | * 发送byte数据 68 | * @param data data 69 | * @param length length 70 | * @return ok or not 71 | */ 72 | public boolean send(byte[] data, int length) { 73 | if (mSocketClient != null) { 74 | return mSocketClient.send(data, length); 75 | } 76 | return false; 77 | } 78 | 79 | /** 80 | * 发送文件 81 | * @param file local file path. 82 | * @return ok or not 83 | */ 84 | public boolean send(String file) { 85 | if (mSocketClient != null) { 86 | return mSocketClient.send(file); 87 | } 88 | return false; 89 | } 90 | 91 | public void setOnSocketListener(OnSocketListener sl) { 92 | mOnSocketListener = sl; 93 | if (mSocketServer != null) { 94 | mSocketServer.setOnSocketListener(mOnSocketListener); 95 | } 96 | if (mSocketClient != null) { 97 | mSocketClient.setOnSocketListener(mOnSocketListener); 98 | } 99 | } 100 | 101 | public void destroy() { 102 | if (mSocketServer != null) { 103 | mSocketServer.shutdown(); 104 | } 105 | if (mSocketClient != null) { 106 | mSocketClient.shutdown(); 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/socket/Transfer/Receiver.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.socket.Transfer; 2 | 3 | import com.guo.android_extend.java.AbsLoop; 4 | import com.guo.android_extend.java.network.socket.OnSocketListener; 5 | import com.guo.android_extend.java.network.socket.Data.AbsTransmitter; 6 | import com.guo.android_extend.tools.LogcatHelper; 7 | 8 | import java.io.DataInputStream; 9 | import java.io.File; 10 | import java.net.Socket; 11 | 12 | /** 13 | * Created by gqj3375 on 2015/12/18. 14 | */ 15 | public class Receiver extends AbsLoop { 16 | private String TAG = this.getClass().getSimpleName(); 17 | 18 | private final static int BUFFER_LENGTH = 8192; 19 | 20 | private Socket mSocket; 21 | private DataInputStream mDataRead; 22 | private byte[] mBuffer; 23 | private String mLocalDir; 24 | private OnReceiverListener mOnReceiverListener; 25 | 26 | public interface OnReceiverListener { 27 | public void onException(int error); 28 | public AbsTransmitter onReceiveType(int type); 29 | public void onReceiveProcess(AbsTransmitter obj, int cur, int total); 30 | public void onReceiveInitial(Socket socket, DataInputStream dis); 31 | public void onReceiveDestroy(Socket socket); 32 | } 33 | 34 | public Receiver(String local_dir, Socket socket) { 35 | super(); 36 | mBuffer = new byte[BUFFER_LENGTH]; 37 | mLocalDir = local_dir; 38 | File file = new File(mLocalDir); 39 | file.mkdirs(); 40 | mSocket = socket; 41 | mOnReceiverListener = null; 42 | } 43 | 44 | public void setOnReceiverListener(OnReceiverListener l) { 45 | mOnReceiverListener = l; 46 | } 47 | 48 | @Override 49 | public void setup() { 50 | try { 51 | mDataRead = new DataInputStream(mSocket.getInputStream()); 52 | } catch (Exception e) { 53 | LogcatHelper.e(TAG, "setup:" + e.getMessage()); 54 | if (mOnReceiverListener != null) { 55 | mOnReceiverListener.onException(OnSocketListener.ERROR_SOCKET_STREAM); 56 | } 57 | return; 58 | } 59 | if (mOnReceiverListener != null) { 60 | mOnReceiverListener.onReceiveInitial(mSocket, mDataRead); 61 | } 62 | } 63 | 64 | @Override 65 | public void loop() { 66 | try { 67 | if (mDataRead.available() <= 0) { 68 | sleep(1); 69 | return ; 70 | } 71 | if (mOnReceiverListener != null) { 72 | AbsTransmitter object = mOnReceiverListener.onReceiveType(mDataRead.readInt()); 73 | object.setOnReceiverListener(mOnReceiverListener); 74 | int ex = object.recv(mDataRead, mBuffer); 75 | if (ex != OnSocketListener.ERROR_NONE) { 76 | mOnReceiverListener.onException(ex); 77 | } 78 | } else { 79 | LogcatHelper.e(TAG, "please set listener!"); 80 | } 81 | } catch (Exception e) { 82 | LogcatHelper.e(TAG, "loop:" + e.getMessage()); 83 | if (mOnReceiverListener != null) { 84 | mOnReceiverListener.onException(OnSocketListener.ERROR_SOCKET_TRANSFER); 85 | } 86 | } 87 | 88 | } 89 | 90 | @Override 91 | public void over() { 92 | if (mOnReceiverListener != null) { 93 | mOnReceiverListener.onReceiveDestroy(mSocket); 94 | } 95 | try { 96 | if (mDataRead != null) { 97 | mDataRead.close(); 98 | } 99 | } catch (Exception e) { 100 | LogcatHelper.e(TAG, "over:" + e.getMessage()); 101 | if (mOnReceiverListener != null) { 102 | mOnReceiverListener.onException(OnSocketListener.ERROR_SOCKET_CLOSE); 103 | } 104 | } 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/socket/Transfer/Sender.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.socket.Transfer; 2 | 3 | import com.guo.android_extend.java.AbsLoop; 4 | import com.guo.android_extend.java.network.socket.Data.AbsTransmitter; 5 | import com.guo.android_extend.java.network.socket.OnSocketListener; 6 | import com.guo.android_extend.tools.LogcatHelper; 7 | 8 | import java.io.DataOutputStream; 9 | import java.io.IOException; 10 | import java.net.Socket; 11 | import java.util.concurrent.BlockingQueue; 12 | import java.util.concurrent.LinkedBlockingQueue; 13 | 14 | /** 15 | * Created by Guo on 2015/12/26. 16 | */ 17 | public class Sender extends AbsLoop { 18 | private String TAG = this.getClass().getSimpleName(); 19 | 20 | private final static int BUFFER_LENGTH = 8192; 21 | private final static int QUEUE_MAX_SIZE = 8; 22 | 23 | private BlockingQueue mTaskQueue; 24 | private DataOutputStream mDataWrite; 25 | private Socket mSocket; 26 | private byte[] mBuffer; 27 | private OnSenderListener mOnSenderListener; 28 | 29 | public interface OnSenderListener { 30 | public void onException(int error); 31 | public void onSendProcess(AbsTransmitter obj, int cur, int total); 32 | public void onSendInitial(Socket socket, DataOutputStream dos); 33 | public void onSendDestroy(Socket socket); 34 | } 35 | 36 | public Sender(Socket mSocket, int max_queue) { 37 | this.mTaskQueue = new LinkedBlockingQueue(max_queue); 38 | this.mBuffer = new byte[BUFFER_LENGTH]; 39 | this.mSocket = mSocket; 40 | this.mOnSenderListener = null; 41 | } 42 | 43 | public Sender(Socket mSocket) { 44 | this(mSocket, QUEUE_MAX_SIZE); 45 | } 46 | 47 | /** 48 | * post object for transmit 49 | * @param object sender object. 50 | * @return success is true. 51 | */ 52 | public boolean post(AbsTransmitter object) { 53 | boolean success = mTaskQueue.offer(object); 54 | synchronized (this) { 55 | this.notifyAll(); 56 | } 57 | return success; 58 | } 59 | 60 | public void setOnSenderListener(OnSenderListener tl) { 61 | this.mOnSenderListener = tl; 62 | } 63 | 64 | @Override 65 | public void setup() { 66 | try { 67 | mDataWrite = new DataOutputStream(mSocket.getOutputStream()); 68 | } catch (Exception e) { 69 | LogcatHelper.e(TAG, "setup:" + e.getCause().getMessage()); 70 | if (mOnSenderListener != null) { 71 | mOnSenderListener.onException(OnSocketListener.ERROR_SOCKET_STREAM); 72 | } 73 | return; 74 | } 75 | if (mOnSenderListener != null) { 76 | mOnSenderListener.onSendInitial(mSocket, mDataWrite); 77 | } 78 | } 79 | 80 | @Override 81 | public void loop() { 82 | AbsTransmitter data = mTaskQueue.poll(); 83 | if (data != null) { 84 | data.setOnSenderListener(mOnSenderListener); 85 | int ex = data.send(mDataWrite, mBuffer); 86 | if (ex != OnSocketListener.ERROR_NONE) { 87 | if (mOnSenderListener != null) { 88 | mOnSenderListener.onException(ex); 89 | } 90 | } 91 | } else { 92 | synchronized (this) { 93 | try { 94 | wait(); 95 | } catch (InterruptedException e) { 96 | e.printStackTrace(); 97 | } 98 | } 99 | } 100 | } 101 | 102 | @Override 103 | public void over() { 104 | if (mOnSenderListener != null) { 105 | mOnSenderListener.onSendDestroy(mSocket); 106 | } 107 | try { 108 | mDataWrite.close(); 109 | } catch (IOException e) { 110 | LogcatHelper.e(TAG, "over:" + e.getMessage()); 111 | if (mOnSenderListener != null) { 112 | mOnSenderListener.onException(OnSocketListener.ERROR_SOCKET_CLOSE); 113 | } 114 | } 115 | } 116 | } -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/udp/UDPDataProtocol.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.udp; 2 | 3 | /** 4 | * Created by gqj3375 on 2015/12/22. 5 | */ 6 | public interface UDPDataProtocol { 7 | public byte[] packaged(); 8 | public void parsed(String ip, byte[] data, int size); 9 | } 10 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/java/network/udp/UDPModule.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.java.network.udp; 2 | 3 | import com.guo.android_extend.tools.LogcatHelper; 4 | 5 | import java.net.InetAddress; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * Created by gqj3375 on 2015/12/17. 11 | */ 12 | public class UDPModule implements UDPDataProtocol { 13 | private String TAG = this.getClass().getSimpleName(); 14 | 15 | private List mDevices; 16 | private UDPTransponder mUDPTransponder; 17 | private OnUDPListener mOnClientListener; 18 | 19 | private String mName; 20 | private String mLocalMac; 21 | 22 | public class Device { 23 | public String mName; 24 | public String mIP; 25 | public String mMAC; 26 | 27 | public Device(String mName, String mIP, String mMAC) { 28 | this.mName = mName; 29 | this.mIP = mIP; 30 | this.mMAC = mMAC; 31 | } 32 | } 33 | 34 | public interface OnUDPListener { 35 | public void onReceiveDevice(List list, String name, String ip); 36 | } 37 | 38 | public UDPModule(InetAddress inetAddress, boolean isBroadcast, String mac, String name) { 39 | this(inetAddress, isBroadcast, mac, name, 1000); 40 | } 41 | 42 | public UDPModule(InetAddress inetAddress, boolean isBroadcast, String mac, String name, int time) { 43 | mName = name; 44 | mUDPTransponder = new UDPTransponder(inetAddress, isBroadcast); 45 | mUDPTransponder.setUDPDataProtocol(this); 46 | mUDPTransponder.startReceiver(); 47 | mUDPTransponder.startDeliver(time); 48 | mDevices = new ArrayList(); 49 | mLocalMac = mac; 50 | } 51 | 52 | public List getResult() { 53 | return mDevices; 54 | } 55 | 56 | public void clear() { 57 | mDevices.clear(); 58 | } 59 | 60 | public void destroy() { 61 | mUDPTransponder.stopDeliver(); 62 | mUDPTransponder.stopReceiver(); 63 | } 64 | 65 | public void setOnUDPListener(OnUDPListener osl) { 66 | mOnClientListener = osl; 67 | } 68 | 69 | @Override 70 | public byte[] packaged() { 71 | String ret = mName + "#" + mLocalMac; 72 | return ret.getBytes(); 73 | } 74 | 75 | @Override 76 | public void parsed(String ip, byte[] data, int size) { 77 | String info = new String(data, 0, size); 78 | String[] list = info.split("#"); 79 | String mac, name; 80 | if (list.length > 1) { 81 | name = list[0]; 82 | mac = list[1]; 83 | } else { 84 | LogcatHelper.e(TAG, "parser error!" + info + ",ip=" + ip); 85 | return ; 86 | } 87 | boolean update = true; 88 | for (Device device : mDevices) { 89 | if (device.mMAC.equals(mac)) { 90 | update = false; 91 | break; 92 | } 93 | } 94 | if (update && !mac.equals(mLocalMac)) { 95 | LogcatHelper.d(TAG, "Device:" + name + "[" + mac + "]" + "<" + ip + ">"); 96 | mDevices.add(new Device(name, ip, mac)); 97 | if (mOnClientListener != null) { 98 | mOnClientListener.onReceiveDevice(mDevices, name, ip); 99 | } 100 | } 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/tools/AssetsHelper.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.tools; 2 | 3 | import android.content.Context; 4 | import android.os.Environment; 5 | import android.util.Log; 6 | 7 | import java.io.BufferedInputStream; 8 | import java.io.BufferedOutputStream; 9 | import java.io.File; 10 | import java.io.FileInputStream; 11 | import java.io.FileOutputStream; 12 | import java.io.InputStream; 13 | import java.io.OutputStream; 14 | import java.util.zip.ZipEntry; 15 | import java.util.zip.ZipFile; 16 | import java.util.zip.ZipInputStream; 17 | 18 | /** 19 | * Created by gqj3375 on 2017/2/23. 20 | */ 21 | 22 | public class AssetsHelper { 23 | 24 | public static boolean zipDecodeToDisk(Context context, String assetsFile, String path_in_sdcard) { 25 | try { 26 | if (copyToDisk(context, assetsFile, path_in_sdcard)) { 27 | String PATH_LOGCAT; 28 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 29 | PATH_LOGCAT = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + path_in_sdcard; 30 | } else { 31 | PATH_LOGCAT = context.getFilesDir().getAbsolutePath() + File.separator + path_in_sdcard; 32 | } 33 | File file = new File(PATH_LOGCAT + File.separator + assetsFile); 34 | ZipFile zipFile = new ZipFile(file); 35 | ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file)); 36 | ZipEntry zipEntry = null; 37 | while ((zipEntry = zipInputStream.getNextEntry()) != null) { 38 | String fileName = zipEntry.getName(); 39 | File temp = new File(PATH_LOGCAT + File.separator + fileName); 40 | if (! temp.getParentFile().exists()) { 41 | temp.getParentFile().mkdirs(); 42 | } 43 | OutputStream os = new FileOutputStream(temp); 44 | BufferedOutputStream Bout = new BufferedOutputStream(os); 45 | InputStream is = zipFile.getInputStream(zipEntry); 46 | BufferedInputStream Bin = new BufferedInputStream(is); 47 | int len = 0; 48 | while ((len = Bin.read()) != -1) { 49 | Bout.write(len); 50 | } 51 | Bout.close(); 52 | os.close(); 53 | Bin.close(); 54 | is.close(); 55 | } 56 | zipInputStream.close(); 57 | return true; 58 | } 59 | } catch (Exception e) { 60 | e.printStackTrace(); 61 | } 62 | return false; 63 | } 64 | 65 | public static boolean copyToDisk(Context context, String fileName, String path_in_sdcard) { 66 | try { 67 | String PATH_LOGCAT; 68 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 69 | PATH_LOGCAT = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + path_in_sdcard; 70 | } else { 71 | PATH_LOGCAT = context.getFilesDir().getAbsolutePath() + File.separator + path_in_sdcard; 72 | } 73 | File file = new File(PATH_LOGCAT); 74 | if (!file.exists()) { 75 | file.mkdirs(); 76 | } 77 | 78 | file = new File(PATH_LOGCAT + File.separator + fileName); 79 | if (!file.exists()) { 80 | if (!file.createNewFile()) { 81 | Log.e("AssetsHelper", "createNewFile FAIL!"); 82 | } 83 | } 84 | FileOutputStream fout = new FileOutputStream(file); 85 | InputStream fin = context.getResources().getAssets().open(fileName); 86 | int length = fin.available(); 87 | byte[] buffer = new byte[length]; 88 | fin.read(buffer); 89 | fout.write(buffer); 90 | fin.close(); 91 | fout.flush(); 92 | fout.close(); 93 | return true; 94 | } catch (Exception e) { 95 | e.printStackTrace(); 96 | return false; 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/tools/DumpHelper.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.tools; 2 | 3 | import com.guo.android_extend.java.AbsLoop; 4 | 5 | import java.io.File; 6 | import java.io.FileNotFoundException; 7 | import java.io.FileOutputStream; 8 | import java.io.IOException; 9 | import java.util.concurrent.LinkedBlockingQueue; 10 | 11 | /** 12 | * dump camera preview to sdcard. 13 | * use LinkedBlockingQueue 14 | * Created by gqj3375 on 2017/7/19. 15 | */ 16 | 17 | public class DumpHelper extends AbsLoop { 18 | private String tag; 19 | private String path; 20 | private String suffix; 21 | private LinkedBlockingQueue writeQueue; 22 | private FileOutputStream out; 23 | 24 | public interface DataInterface { 25 | public byte[] convert(); 26 | } 27 | 28 | private class FinishData implements DataInterface { 29 | 30 | @Override 31 | public byte[] convert() { 32 | return null; 33 | } 34 | } 35 | 36 | /** 37 | * 38 | * @param tag file name tag. 39 | * @param path path in sdcard. 40 | * @param suffix file suffix. 41 | */ 42 | public DumpHelper(String tag, String path, String suffix) { 43 | this.tag = tag; 44 | this.path = path; 45 | this.suffix = suffix; 46 | } 47 | 48 | @Override 49 | public void setup() { 50 | writeQueue = new LinkedBlockingQueue(); 51 | try { 52 | out = new FileOutputStream(new File(path, tag + "." + suffix)); 53 | } catch (FileNotFoundException e) { 54 | // TODO Auto-generated catch block 55 | e.printStackTrace(); 56 | } 57 | } 58 | 59 | public boolean write(DataInterface data) { 60 | if (writeQueue != null) { 61 | try { 62 | writeQueue.put(data); 63 | return true; 64 | } catch (InterruptedException e) { 65 | e.printStackTrace(); 66 | } 67 | } 68 | return false; 69 | } 70 | 71 | @Override 72 | public void loop() { 73 | DataInterface data = null; 74 | try { 75 | data = writeQueue.take(); 76 | } catch (InterruptedException e) { 77 | e.printStackTrace(); 78 | } 79 | if (data != null) { 80 | try { 81 | byte[] real = data.convert(); 82 | if (real != null) { 83 | out.write(real); 84 | } else { // if real data is null, it's last data. should be close this thread. 85 | break_loop(); 86 | } 87 | } catch (IOException e) { 88 | e.printStackTrace(); 89 | } 90 | } 91 | } 92 | 93 | @Override 94 | public void over() { 95 | try { 96 | out.close(); 97 | } catch (IOException e) { 98 | e.printStackTrace(); 99 | } 100 | } 101 | 102 | public void shutdown(boolean force) { 103 | if (force) { 104 | try { 105 | writeQueue.put(new FinishData()); 106 | } catch (InterruptedException e) { 107 | e.printStackTrace(); 108 | } 109 | super.shutdown(); 110 | } else { 111 | try { 112 | writeQueue.put(new FinishData()); 113 | if (this != Thread.currentThread()) { 114 | synchronized (this) { 115 | this.notifyAll(); 116 | } 117 | this.join(); 118 | } 119 | } catch (InterruptedException e) { 120 | e.printStackTrace(); 121 | } 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/tools/FrameHelper.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.tools; 2 | 3 | import android.util.Log; 4 | 5 | public class FrameHelper { 6 | private final String TAG = this.getClass().getSimpleName(); 7 | 8 | //FOR DEBUG 9 | private long mStartTime; 10 | private long mFrames; 11 | private boolean isShow; 12 | 13 | public FrameHelper() { 14 | // TODO Auto-generated constructor stub 15 | isShow = true; 16 | reset(); 17 | } 18 | 19 | public void printFPS() { 20 | if (isShow) { 21 | if (mStartTime == 0) { 22 | mStartTime = System.currentTimeMillis(); 23 | mFrames = 0; 24 | } else { 25 | mFrames++; 26 | if (mFrames >= 10) { 27 | Log.e(TAG, "FPS = " + (int)(1000.0*mFrames/(System.currentTimeMillis() - mStartTime))); 28 | mStartTime = 0; 29 | } 30 | } 31 | } 32 | } 33 | 34 | public void reset() { 35 | mStartTime = 0; 36 | mFrames = 0; 37 | } 38 | 39 | public void enable(boolean show) { 40 | if (isShow) { 41 | reset(); 42 | } 43 | this.isShow = show; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/tools/LogcatHelper.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.tools; 2 | 3 | import android.content.Context; 4 | import android.os.Environment; 5 | import android.util.Log; 6 | 7 | import java.io.BufferedReader; 8 | import java.io.File; 9 | import java.io.FileNotFoundException; 10 | import java.io.FileOutputStream; 11 | import java.io.IOException; 12 | import java.io.InputStreamReader; 13 | import java.text.SimpleDateFormat; 14 | import java.util.Date; 15 | 16 | 17 | public class LogcatHelper { 18 | 19 | private static LogcatHelper INSTANCE = null; 20 | private static String PATH_LOGCAT; 21 | private LogDumper mLogDumper = null; 22 | private int mPId; 23 | private Context mContext; 24 | private boolean isInitial; 25 | 26 | public static void d(String tag, String content) { 27 | Log.d(tag, content); 28 | } 29 | 30 | public static void e(String tag, String content) { 31 | Log.e(tag, content); 32 | } 33 | 34 | public static void v(String tag, String content) { 35 | Log.v(tag, content); 36 | } 37 | 38 | /** 39 | * initial log dir. 40 | * @param context the application/or activity context. 41 | * @param subPath dir in sdcard. 42 | */ 43 | public void setupLogPath(Context context, String subPath) { 44 | if (!isInitial) { 45 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 46 | PATH_LOGCAT = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + subPath; 47 | } else { 48 | PATH_LOGCAT = context.getFilesDir().getAbsolutePath() + File.separator + subPath; 49 | } 50 | File file = new File(PATH_LOGCAT); 51 | if (!file.exists()) { 52 | file.mkdirs(); 53 | } 54 | isInitial = true; 55 | } 56 | } 57 | 58 | public static LogcatHelper getInstance(Context context) { 59 | if (INSTANCE == null) { 60 | INSTANCE = new LogcatHelper(context); 61 | } 62 | return INSTANCE; 63 | } 64 | 65 | private LogcatHelper(Context context) { 66 | mContext = context; 67 | mPId = android.os.Process.myPid(); 68 | isInitial = false; 69 | } 70 | 71 | public void start() { 72 | setupLogPath(mContext, mContext.getPackageName()); 73 | if (mLogDumper == null) { 74 | mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT); 75 | } 76 | mLogDumper.start(); 77 | } 78 | 79 | public void stop() { 80 | if (mLogDumper != null) { 81 | mLogDumper.stopLogs(); 82 | mLogDumper = null; 83 | } 84 | } 85 | 86 | private class LogDumper extends Thread { 87 | 88 | private Process logcatProc; 89 | private BufferedReader mReader = null; 90 | private boolean mRunning = true; 91 | String cmds = null; 92 | private String mPID; 93 | private FileOutputStream out = null; 94 | 95 | public LogDumper(String pid, String dir) { 96 | mPID = pid; 97 | try { 98 | out = new FileOutputStream(new File(dir, "dump-" + System.currentTimeMillis() + ".log")); 99 | } catch (FileNotFoundException e) { 100 | // TODO Auto-generated catch block 101 | e.printStackTrace(); 102 | } 103 | 104 | /** 105 | * 106 | * 日志等级:*:v , *:d , *:w , *:e , *:f , *:s 107 | * 108 | * 显示当前mPID程序的 E和W等级的日志. 109 | * 110 | * */ 111 | 112 | // cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; 113 | // cmds = "logcat | grep \"(" + mPID + ")\"";//打印所有日志信息 114 | // cmds = "logcat -s way";//打印标签过滤信息 115 | cmds = "logcat *:e *:i *:d | grep \"(" + mPID + ")\""; 116 | 117 | } 118 | 119 | public void stopLogs() { 120 | mRunning = false; 121 | } 122 | 123 | @Override 124 | public void run() { 125 | try { 126 | logcatProc = Runtime.getRuntime().exec(cmds); 127 | mReader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), 1024); 128 | String line = null; 129 | while (mRunning && (line = mReader.readLine()) != null) { 130 | if (!mRunning) { 131 | break; 132 | } 133 | if (line.length() == 0) { 134 | continue; 135 | } 136 | if (out != null && line.contains(mPID)) { 137 | SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 138 | String date1 = format1.format(new Date(System.currentTimeMillis())); 139 | out.write((date1 + " " + line + "\n").getBytes()); 140 | } 141 | } 142 | 143 | } catch (IOException e) { 144 | e.printStackTrace(); 145 | } finally { 146 | if (logcatProc != null) { 147 | logcatProc.destroy(); 148 | logcatProc = null; 149 | } 150 | if (mReader != null) { 151 | try { 152 | mReader.close(); 153 | mReader = null; 154 | } catch (IOException e) { 155 | e.printStackTrace(); 156 | } 157 | } 158 | if (out != null) { 159 | try { 160 | out.close(); 161 | } catch (IOException e) { 162 | e.printStackTrace(); 163 | } 164 | out = null; 165 | } 166 | 167 | } 168 | 169 | } 170 | 171 | } 172 | 173 | } 174 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/tools/SensorHelper.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.tools; 2 | 3 | import android.content.Context; 4 | import android.hardware.Sensor; 5 | import android.hardware.SensorEventListener; 6 | import android.hardware.SensorManager; 7 | 8 | import static android.content.Context.SENSOR_SERVICE; 9 | 10 | /** 11 | * Created by gqj3375 on 2017/1/10. 12 | */ 13 | 14 | public class SensorHelper { 15 | private SensorManager sensorManager; 16 | 17 | public SensorHelper(SensorManager sensorManager) { 18 | this.sensorManager = sensorManager; 19 | } 20 | 21 | public SensorHelper(Context context) { 22 | sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE); 23 | } 24 | 25 | public void register(int type, SensorEventListener listener) { 26 | Sensor sensor = sensorManager.getDefaultSensor(type); 27 | sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME); 28 | } 29 | 30 | public void unregister(SensorEventListener listener) { 31 | sensorManager.unregisterListener(listener); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/CameraFrameData.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | /** 4 | * Created by gqj3375 on 2017/7/14. 5 | */ 6 | 7 | public class CameraFrameData { 8 | //image data. 9 | byte[] mData; 10 | int mWidth, mHeight, mFormat; 11 | 12 | //user data 13 | Object mParams; 14 | //timestamp 15 | long mTimeStamp; 16 | 17 | public CameraFrameData(int w, int h, int f, int size) { 18 | mWidth = w; 19 | mHeight = h; 20 | mFormat = f; 21 | mData = new byte[size]; 22 | mParams = null; 23 | mTimeStamp = System.nanoTime(); 24 | } 25 | 26 | public byte[] getData() { 27 | return mData; 28 | } 29 | 30 | public void setData(byte[] mData) { 31 | this.mData = mData; 32 | } 33 | 34 | public Object getParams() { 35 | return mParams; 36 | } 37 | 38 | public void setParams(Object mParams) { 39 | this.mParams = mParams; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/CameraGLSurfaceView.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | import android.content.Context; 4 | import android.graphics.ImageFormat; 5 | import android.opengl.GLSurfaceView; 6 | import android.util.AttributeSet; 7 | import android.util.Log; 8 | 9 | import com.guo.android_extend.GLES2Render; 10 | import com.guo.android_extend.image.ImageConverter; 11 | 12 | import java.util.concurrent.BlockingQueue; 13 | import java.util.concurrent.LinkedBlockingQueue; 14 | 15 | import javax.microedition.khronos.egl.EGLConfig; 16 | import javax.microedition.khronos.opengles.GL10; 17 | 18 | /** 19 | * create by gqjjqg,. 20 | * easy to use opengl surface.. 21 | */ 22 | 23 | public class CameraGLSurfaceView extends ExtGLSurfaceView implements GLSurfaceView.Renderer { 24 | private final String TAG = this.getClass().getSimpleName(); 25 | 26 | private int mWidth, mHeight, mFormat, mRenderFormat; 27 | private int mDegree; 28 | private int mMirror; 29 | private boolean mDebugFPS; 30 | private boolean mConfigSuccess = false; 31 | 32 | private BlockingQueue mImageRenderBuffers; 33 | private GLES2Render mGLES2Render; 34 | private OnRenderListener mOnRenderListener; 35 | private OnDrawListener mOnDrawListener; 36 | 37 | public interface OnDrawListener{ 38 | public void onDrawOverlap(GLES2Render render); 39 | } 40 | 41 | public interface OnRenderListener { 42 | public void onBeforeRender(CameraFrameData data); 43 | public void onAfterRender(CameraFrameData data); 44 | } 45 | 46 | public CameraGLSurfaceView(Context context, AttributeSet attrs) { 47 | super(context, attrs); 48 | // TODO Auto-generated constructor stub 49 | onCreate(); 50 | } 51 | 52 | public CameraGLSurfaceView(Context context) { 53 | super(context); 54 | // TODO Auto-generated constructor stub 55 | onCreate(); 56 | } 57 | 58 | private void onCreate() { 59 | if (isInEditMode()) { 60 | return; 61 | } 62 | setEGLContextClientVersion(2); 63 | setRenderer(this); 64 | setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 65 | setZOrderMediaOverlay(true); 66 | mImageRenderBuffers = new LinkedBlockingQueue<>(); 67 | } 68 | 69 | @Override 70 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 71 | Log.d(TAG,"onSurfaceCreated"); 72 | } 73 | 74 | @Override 75 | public void onSurfaceChanged(GL10 gl, int width, int height) { 76 | Log.d(TAG,"onSurfaceChanged"); 77 | if (mGLES2Render == null && mConfigSuccess) { 78 | mGLES2Render = new GLES2Render(mMirror, mDegree, mRenderFormat, mDebugFPS); 79 | } 80 | if (mGLES2Render != null) { 81 | mGLES2Render.setViewPort(width, height); 82 | mGLES2Render.setViewDisplay(mMirror, mDegree); 83 | } 84 | } 85 | 86 | @Override 87 | public void onDrawFrame(GL10 gl) { 88 | CameraFrameData data = mImageRenderBuffers.poll(); 89 | if (data != null) { 90 | byte[] buffer = data.mData; 91 | if (mOnRenderListener != null) { 92 | mOnRenderListener.onBeforeRender(data); 93 | } 94 | if (mGLES2Render != null) { 95 | mGLES2Render.render(buffer, mWidth, mHeight); 96 | } 97 | if (mOnRenderListener != null) { 98 | mOnRenderListener.onAfterRender(data); 99 | } 100 | } 101 | if (mOnDrawListener != null) { 102 | mOnDrawListener.onDrawOverlap(mGLES2Render); 103 | } 104 | } 105 | 106 | public void requestRender(CameraFrameData data) { 107 | if (!mImageRenderBuffers.offer(data)) { 108 | Log.e(TAG, "RENDER QUEUE FULL!"); 109 | } else { 110 | requestRender(); 111 | } 112 | } 113 | 114 | public void setOnDrawListener(OnDrawListener lis) { 115 | mOnDrawListener = lis; 116 | } 117 | 118 | public void setOnRenderListener(OnRenderListener lis) { 119 | mOnRenderListener = lis; 120 | } 121 | 122 | public boolean setImageConfig(int width, int height, int format) { 123 | mWidth = width; 124 | mHeight = height; 125 | mFormat = format; 126 | switch(format) { 127 | case ImageFormat.NV21 : mRenderFormat = ImageConverter.CP_PAF_NV21; mConfigSuccess = true; break; 128 | case ImageFormat.RGB_565 : mRenderFormat = ImageConverter.CP_RGB565; mConfigSuccess = true; break; 129 | default: 130 | Log.e(TAG, "Current camera preview format = " + format + ", render is not support!"); 131 | mConfigSuccess = false; 132 | } 133 | return mConfigSuccess; 134 | } 135 | 136 | public void setRenderConfig(int degree, int mirror) { 137 | mDegree = degree; 138 | mMirror = mirror; 139 | if (mGLES2Render != null) { 140 | mGLES2Render.setViewDisplay(mMirror, degree); 141 | } 142 | } 143 | 144 | @Override 145 | public boolean OnOrientationChanged(int degree, int offset, int flag) { 146 | if (mGLES2Render != null) { 147 | mGLES2Render.setViewDisplay(mMirror, degree); 148 | } 149 | return super.OnOrientationChanged(degree, offset, flag); 150 | } 151 | 152 | public GLES2Render getGLES2Render() { 153 | return mGLES2Render; 154 | } 155 | 156 | public void debug_print_fps(boolean show) { 157 | mDebugFPS = show; 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/ExtGLSurfaceView.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Handler; 6 | import android.util.AttributeSet; 7 | import android.util.Log; 8 | import android.view.MotionEvent; 9 | import android.view.animation.Animation; 10 | import android.view.animation.RotateAnimation; 11 | 12 | import com.guo.android_extend.widget.controller.TouchController; 13 | 14 | /** 15 | * create by gqjjqg,. 16 | * easy to use opengl surface.. 17 | */ 18 | 19 | public class ExtGLSurfaceView extends GLSurfaceView implements ExtOrientationDetector.OnOrientationListener { 20 | private final String TAG = this.getClass().getSimpleName(); 21 | 22 | private Handler mHandler; 23 | 24 | /** 25 | * animation during time. 26 | */ 27 | private final int ANIMATION_TIME = RotateRunable.ANIMATION_TIME; 28 | 29 | private int mCurDegree; 30 | 31 | private double mAspectRatio; 32 | private boolean mFitMaxArea; 33 | 34 | /** 35 | * for touch point rotate. 36 | */ 37 | private TouchController mTouchController; 38 | 39 | /** 40 | * for dispatch touch event process. 41 | */ 42 | private TouchController.OnDispatchTouchEventListener mOnDispatchTouchEventListener; 43 | 44 | public ExtGLSurfaceView(Context context, AttributeSet attrs) { 45 | super(context, attrs); 46 | // TODO Auto-generated constructor stub 47 | onCreate(); 48 | } 49 | 50 | public ExtGLSurfaceView(Context context) { 51 | super(context); 52 | // TODO Auto-generated constructor stub 53 | onCreate(); 54 | } 55 | 56 | private void onCreate() { 57 | mAspectRatio = 0.0; 58 | mCurDegree = 0; 59 | mHandler = new Handler(); 60 | mTouchController = new TouchController(); 61 | } 62 | 63 | public void setAutoFitMax(boolean enable) { 64 | mFitMaxArea = enable; 65 | } 66 | 67 | public void setAspectRatio(double ratio) { 68 | if (mAspectRatio != ratio) { 69 | mAspectRatio = ratio; 70 | requestLayout(); 71 | } 72 | } 73 | 74 | public void setAspectRatio(int width, int height) { 75 | double ratio = ((double)width / (double)height); 76 | if (mAspectRatio != ratio) { 77 | mAspectRatio = ratio; 78 | requestLayout(); 79 | } 80 | } 81 | 82 | /* (non-Javadoc) 83 | * @see android.view.SurfaceView#onMeasure(int, int) 84 | */ 85 | @Override 86 | protected void onMeasure(int widthSpec, int heightSpec) { 87 | // TODO Auto-generated method stub 88 | int width = MeasureSpec.getSize(widthSpec); 89 | int height = MeasureSpec.getSize(heightSpec); 90 | 91 | if (mAspectRatio != 0) { 92 | int w1 = width, h1 = height; 93 | if (w1 > h1 * mAspectRatio) { 94 | w1 = (int) (h1 * mAspectRatio + .5); 95 | } else { 96 | h1 = (int) (w1 / mAspectRatio + .5); 97 | } 98 | if (mFitMaxArea) { 99 | int w2 = width, h2 = height; 100 | double ratio = 1.0 / mAspectRatio; 101 | if (w2 > h2 * ratio) { 102 | w2 = (int) (h2 * ratio + .5); 103 | } else { 104 | h2 = (int) (w2 / ratio + .5); 105 | } 106 | if (w1 * h1 > w2 * h2) { 107 | width = w1; 108 | height = h1; 109 | } else { 110 | width = w2; 111 | height = h2; 112 | } 113 | } else { 114 | width = w1; 115 | height = h1; 116 | } 117 | } 118 | 119 | widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); 120 | heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 121 | 122 | super.onMeasure(widthSpec, heightSpec); 123 | } 124 | 125 | 126 | @Override 127 | public boolean OnOrientationChanged(int degree, int offset, int flag) { 128 | if (flag != ExtOrientationDetector.ROTATE_FORCE_REDO) { 129 | if (!this.isShown()) { 130 | Log.i(TAG, "Not Shown!"); 131 | return false; 132 | } 133 | } 134 | 135 | Animation animation = new RotateAnimation(offset, 0, 136 | Animation.RELATIVE_TO_SELF, 0.5f, 137 | Animation.RELATIVE_TO_SELF, 0.5f); 138 | animation.setDuration(ANIMATION_TIME); 139 | animation.setFillAfter(true); 140 | mHandler.post(new RotateRunable(animation, this, degree)); 141 | 142 | mCurDegree = degree; 143 | return true; 144 | } 145 | 146 | /* (non-Javadoc) 147 | * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent) 148 | */ 149 | @Override 150 | public boolean dispatchTouchEvent(MotionEvent ev) { 151 | MotionEvent newEvent = ev; 152 | if (mTouchController != null) { 153 | newEvent = mTouchController.obtainTouchEvent(ev, this.getWidth(), this.getHeight(), mCurDegree); 154 | } 155 | // TODO Auto-generated method stub 156 | if (mOnDispatchTouchEventListener != null) { 157 | mOnDispatchTouchEventListener.onDispatchTouchEvent(this, ev); 158 | } 159 | return super.dispatchTouchEvent(newEvent); 160 | } 161 | 162 | public void setOnDispatchTouchEventListener(TouchController.OnDispatchTouchEventListener listener) { 163 | mOnDispatchTouchEventListener = listener; 164 | } 165 | 166 | @Override 167 | public int getCurrentOrientationDegree() { 168 | return mCurDegree; 169 | } 170 | 171 | } 172 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/ExtImageButton.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.os.Handler; 6 | import android.util.AttributeSet; 7 | import android.util.Log; 8 | import android.view.View; 9 | import android.view.animation.Animation; 10 | import android.view.animation.Animation.AnimationListener; 11 | import android.view.animation.RotateAnimation; 12 | import android.widget.ImageButton; 13 | 14 | import com.guo.android_extend.widget.ExtOrientationDetector.OnOrientationListener; 15 | 16 | /** 17 | * @author gqj3375 18 | * @see ExtImageButton 19 | * 20 | * rotatable and scalable. 21 | * background will not rotate and scale . 22 | */ 23 | 24 | public class ExtImageButton extends ImageButton implements OnOrientationListener, AnimationListener { 25 | private final String TAG = this.getClass().toString(); 26 | 27 | private Handler mHandler; 28 | 29 | /** 30 | * animation during time. 31 | */ 32 | private final int ANIMATION_TIME = RotateRunable.ANIMATION_TIME; 33 | 34 | /** 35 | * for animation . 36 | */ 37 | private int mCurDegree; 38 | 39 | /** 40 | * for scale. 41 | */ 42 | private float scaleX, scaleY; 43 | 44 | public ExtImageButton(Context context, AttributeSet attrs, 45 | int defStyle) { 46 | super(context, attrs, defStyle); 47 | // TODO Auto-generated constructor stub 48 | preCreate(context); 49 | } 50 | 51 | public ExtImageButton(Context context, AttributeSet attrs) { 52 | super(context, attrs); 53 | // TODO Auto-generated constructor stub 54 | preCreate(context); 55 | } 56 | 57 | public ExtImageButton(Context context) { 58 | super(context); 59 | // TODO Auto-generated constructor stub 60 | preCreate(context); 61 | } 62 | 63 | private void preCreate(Context context) { 64 | mHandler = new Handler(); 65 | mCurDegree = 0; 66 | scaleX = 1.0f; 67 | scaleY = 1.0f; 68 | } 69 | 70 | @Override 71 | public boolean OnOrientationChanged(int degree, int offset, int flag) { 72 | // TODO Auto-generated method stub 73 | if (flag != ExtOrientationDetector.ROTATE_FORCE_REDO) { 74 | if (!this.isShown()) { 75 | Log.i(TAG, "Not Shown!"); 76 | return false; 77 | } 78 | } 79 | 80 | Animation animation = new RotateAnimation (offset, 0, 81 | Animation.RELATIVE_TO_SELF, 0.5f, 82 | Animation.RELATIVE_TO_SELF, 0.5f); 83 | animation.setDuration(ANIMATION_TIME); 84 | animation.setFillAfter(true); 85 | mHandler.post(new RotateRunable(animation, this, degree)); 86 | 87 | mCurDegree = degree; 88 | return true; 89 | } 90 | 91 | @Override 92 | public int getCurrentOrientationDegree() { 93 | // TODO Auto-generated method stub 94 | return mCurDegree; 95 | } 96 | 97 | @Override 98 | public void onAnimationStart(Animation animation) { 99 | // TODO Auto-generated method stub 100 | this.setVisibility(View.GONE); 101 | this.setEnabled(false); 102 | } 103 | 104 | @Override 105 | public void onAnimationEnd(Animation animation) { 106 | // TODO Auto-generated method stub 107 | this.setVisibility(View.VISIBLE); 108 | this.setEnabled(true); 109 | } 110 | 111 | @Override 112 | public void onAnimationRepeat(Animation animation) { 113 | // TODO Auto-generated method stub 114 | 115 | } 116 | 117 | /* (non-Javadoc) 118 | * @see android.widget.ImageView#onDraw(android.graphics.Canvas) 119 | */ 120 | @Override 121 | protected void onDraw(Canvas canvas) { 122 | // TODO Auto-generated method stub 123 | canvas.save(); 124 | canvas.scale(scaleX, scaleY, this.getWidth() / 2f, this.getHeight() / 2f); 125 | canvas.rotate(-mCurDegree, this.getWidth() / 2f, this.getHeight() / 2f); 126 | super.onDraw(canvas); 127 | canvas.restore(); 128 | } 129 | 130 | /** 131 | * set scale percent. 132 | * @param sx scale x 133 | * @param sy scale y 134 | */ 135 | public void setScale(float sx, float sy) { 136 | scaleX = sx; 137 | scaleY = sy; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/ExtImageView.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.PointF; 6 | import android.graphics.RectF; 7 | import android.os.Handler; 8 | import android.util.AttributeSet; 9 | import android.util.Log; 10 | import android.view.View; 11 | import android.view.animation.Animation; 12 | import android.view.animation.Animation.AnimationListener; 13 | import android.view.animation.RotateAnimation; 14 | import android.widget.ImageView; 15 | 16 | import com.guo.android_extend.widget.ExtOrientationDetector.OnOrientationListener; 17 | import com.guo.android_extend.widget.controller.AbstractController; 18 | import com.guo.android_extend.widget.controller.AbstractController.ControllerListener; 19 | 20 | /** 21 | * @author gqj3375 22 | * @see ExtImageButton 23 | * 24 | * rotatable and scalable. 25 | * background will not rotate and scale . 26 | */ 27 | 28 | public class ExtImageView extends ImageView implements ControllerListener, OnOrientationListener, AnimationListener { 29 | private final String TAG = this.getClass().toString(); 30 | 31 | private Handler mHandler; 32 | 33 | /** 34 | * animation during time. 35 | */ 36 | private final int ANIMATION_TIME = RotateRunable.ANIMATION_TIME; 37 | 38 | /** 39 | * for animation . 40 | */ 41 | private int mCurDegree; 42 | private int mPreDegree; 43 | 44 | /** 45 | * for scale. 46 | */ 47 | private float scaleX, scaleY; 48 | private boolean isEnableScale; 49 | /** 50 | * for touchable. 51 | */ 52 | private AbstractController mImageCtrl; 53 | 54 | public ExtImageView(Context context, AttributeSet attrs, 55 | int defStyle) { 56 | super(context, attrs, defStyle); 57 | // TODO Auto-generated constructor stub 58 | preCreate(context); 59 | } 60 | 61 | public ExtImageView(Context context, AttributeSet attrs) { 62 | super(context, attrs); 63 | // TODO Auto-generated constructor stub 64 | preCreate(context); 65 | } 66 | 67 | public ExtImageView(Context context) { 68 | super(context); 69 | // TODO Auto-generated constructor stub 70 | preCreate(context); 71 | } 72 | 73 | private void preCreate(Context context) { 74 | mHandler = new Handler(); 75 | mCurDegree = 0; 76 | mPreDegree = -90; 77 | scaleX = 1.0f; 78 | scaleY = 1.0f; 79 | isEnableScale = true; 80 | 81 | mImageCtrl = null; 82 | 83 | } 84 | 85 | @Override 86 | public boolean OnOrientationChanged(int degree, int offset, int flag) { 87 | // TODO Auto-generated method stub 88 | if (flag != ExtOrientationDetector.ROTATE_FORCE_REDO) { 89 | if (!this.isShown()) { 90 | Log.i(TAG, "Not Shown!"); 91 | return false; 92 | } 93 | } 94 | 95 | Animation animation = new RotateAnimation (offset, 0, 96 | Animation.RELATIVE_TO_SELF, 0.5f, 97 | Animation.RELATIVE_TO_SELF, 0.5f); 98 | animation.setDuration(ANIMATION_TIME); 99 | animation.setFillAfter(true); 100 | mHandler.post(new RotateRunable(animation, this, degree)); 101 | 102 | mCurDegree = degree; 103 | return true; 104 | } 105 | 106 | @Override 107 | public int getCurrentOrientationDegree() { 108 | // TODO Auto-generated method stub 109 | return mCurDegree; 110 | } 111 | 112 | @Override 113 | public void onAnimationStart(Animation animation) { 114 | // TODO Auto-generated method stub 115 | this.setVisibility(View.GONE); 116 | this.setEnabled(false); 117 | } 118 | 119 | @Override 120 | public void onAnimationEnd(Animation animation) { 121 | // TODO Auto-generated method stub 122 | this.setVisibility(View.VISIBLE); 123 | this.setEnabled(true); 124 | } 125 | 126 | @Override 127 | public void onAnimationRepeat(Animation animation) { 128 | // TODO Auto-generated method stub 129 | 130 | } 131 | 132 | /* (non-Javadoc) 133 | * @see android.widget.ImageView#onDraw(android.graphics.Canvas) 134 | */ 135 | @Override 136 | protected void onDraw(Canvas canvas) { 137 | // TODO Auto-generated method stub 138 | if (mImageCtrl != null && mCurDegree != mPreDegree) { 139 | //if rotate 90 degree , exchange the width and height, reinit the controller. 140 | if (mCurDegree == 0 || mCurDegree == 180) { 141 | mImageCtrl.initialize( getDrawable().getBounds().width(), 142 | getDrawable().getBounds().height(), 143 | canvas.getWidth(), 144 | canvas.getHeight()); 145 | } else { 146 | mImageCtrl.initialize(getDrawable().getBounds().height(), 147 | getDrawable().getBounds().width(), 148 | canvas.getWidth(), 149 | canvas.getHeight()); 150 | } 151 | mPreDegree = mCurDegree; 152 | } 153 | 154 | if (mImageCtrl != null) { 155 | mImageCtrl.beforeDraw(canvas); 156 | } 157 | 158 | canvas.save(); 159 | canvas.scale(scaleX, scaleY, this.getWidth() / 2f, this.getHeight() / 2f); 160 | canvas.rotate(-mCurDegree, this.getWidth() / 2f, this.getHeight() / 2f); 161 | super.onDraw(canvas); 162 | canvas.restore(); 163 | 164 | if (mImageCtrl != null) { 165 | mImageCtrl.afterDraw(canvas); 166 | } 167 | } 168 | 169 | @Override 170 | public PointF getCenterPoint() { 171 | // TODO Auto-generated method stub 172 | RectF bounds = new RectF(); 173 | bounds.set(this.getDrawable().getBounds()); 174 | this.getImageMatrix().mapRect(bounds); 175 | return new PointF(bounds.centerX(), bounds.centerY()); 176 | } 177 | 178 | /** 179 | * @param isEnable enable scale. 180 | */ 181 | public void enableScale(boolean isEnable) { 182 | isEnableScale = isEnable; 183 | } 184 | 185 | /** 186 | * set scale percent. 187 | * @param sx scale x 188 | * @param sy scale y 189 | * @return set ok is true. 190 | */ 191 | public boolean setScale(float sx, float sy) { 192 | if (isEnableScale) { 193 | scaleX = sx; 194 | scaleY = sy; 195 | return true; 196 | } 197 | return false; 198 | } 199 | 200 | /** 201 | * @return the mImageCtrl 202 | */ 203 | public AbstractController getImageCtrl() { 204 | return mImageCtrl; 205 | } 206 | 207 | /** 208 | * @param mImageCtrl the mImageCtrl to set 209 | */ 210 | public void setImageCtrl(AbstractController mImageCtrl) { 211 | this.mImageCtrl = mImageCtrl; 212 | this.setOnTouchListener(mImageCtrl); 213 | } 214 | 215 | } 216 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/ExtRelativeLayout.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.os.Handler; 6 | import android.util.AttributeSet; 7 | import android.util.Log; 8 | import android.view.MotionEvent; 9 | import android.view.View; 10 | import android.view.animation.Animation; 11 | import android.view.animation.Animation.AnimationListener; 12 | import android.view.animation.RotateAnimation; 13 | import android.widget.RelativeLayout; 14 | 15 | import com.guo.android_extend.widget.ExtOrientationDetector.OnOrientationListener; 16 | import com.guo.android_extend.widget.controller.TouchController; 17 | 18 | /** 19 | * @author gqj3375 20 | * @see ExtImageView 21 | */ 22 | 23 | public class ExtRelativeLayout extends RelativeLayout implements OnOrientationListener, AnimationListener { 24 | private final String TAG = this.getClass().toString(); 25 | 26 | private Handler mHandler; 27 | 28 | /** 29 | * animation during time. 30 | */ 31 | private final int ANIMATION_TIME = RotateRunable.ANIMATION_TIME; 32 | 33 | /** 34 | * for animation . 35 | */ 36 | private int mCurDegree; 37 | /** 38 | * for scale. 39 | */ 40 | private float scaleX, scaleY; 41 | 42 | /** 43 | * for touch point rotate. 44 | */ 45 | private TouchController mTouchController; 46 | 47 | /** 48 | * for dispatch touch event process. 49 | */ 50 | private TouchController.OnDispatchTouchEventListener mOnDispatchTouchEventListener; 51 | 52 | public ExtRelativeLayout(Context context, AttributeSet attrs, 53 | int defStyle) { 54 | super(context, attrs, defStyle); 55 | // TODO Auto-generated constructor stub 56 | preCreate(context); 57 | } 58 | 59 | public ExtRelativeLayout(Context context, AttributeSet attrs) { 60 | super(context, attrs); 61 | // TODO Auto-generated constructor stub 62 | preCreate(context); 63 | } 64 | 65 | public ExtRelativeLayout(Context context) { 66 | super(context); 67 | // TODO Auto-generated constructor stub 68 | preCreate(context); 69 | } 70 | 71 | private void preCreate(Context context) { 72 | if (isInEditMode()) { 73 | return; 74 | } 75 | mOnDispatchTouchEventListener = null; 76 | mTouchController = new TouchController(); 77 | mHandler = new Handler(); 78 | mCurDegree = 0; 79 | scaleX = 1.0f; 80 | scaleY = 1.0f; 81 | } 82 | 83 | @Override 84 | public boolean OnOrientationChanged(int degree, int offset, int flag) { 85 | // TODO Auto-generated method stub 86 | if (flag != ExtOrientationDetector.ROTATE_FORCE_REDO) { 87 | if (!this.isShown()) { 88 | Log.i(TAG, "Not Shown!"); 89 | return false; 90 | } 91 | } 92 | 93 | Animation animation = new RotateAnimation (offset, 0, 94 | Animation.RELATIVE_TO_SELF, 0.5f, 95 | Animation.RELATIVE_TO_SELF, 0.5f); 96 | animation.setDuration(ANIMATION_TIME); 97 | animation.setFillAfter(true); 98 | mHandler.post(new RotateRunable(animation, this, degree)); 99 | 100 | mCurDegree = degree; 101 | return true; 102 | } 103 | 104 | @Override 105 | public void onAnimationStart(Animation animation) { 106 | // TODO Auto-generated method stub 107 | this.setVisibility(View.GONE); 108 | this.setEnabled(false); 109 | } 110 | 111 | @Override 112 | public void onAnimationEnd(Animation animation) { 113 | // TODO Auto-generated method stub 114 | this.setVisibility(View.VISIBLE); 115 | this.setEnabled(true); 116 | } 117 | 118 | @Override 119 | public void onAnimationRepeat(Animation animation) { 120 | // TODO Auto-generated method stub 121 | 122 | } 123 | 124 | /* (non-Javadoc) 125 | * @see android.widget.ImageView#onDraw(android.graphics.Canvas) 126 | */ 127 | @Override 128 | protected void onDraw(Canvas canvas) { 129 | // TODO Auto-generated method stub 130 | super.onDraw(canvas); 131 | canvas.scale(scaleX, scaleY, this.getWidth() / 2f, this.getHeight() / 2f); 132 | canvas.rotate(-mCurDegree, this.getWidth() / 2f, this.getHeight() / 2f); 133 | } 134 | 135 | @Override 136 | public int getCurrentOrientationDegree() { 137 | // TODO Auto-generated method stub 138 | return mCurDegree; 139 | } 140 | 141 | /* (non-Javadoc) 142 | * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent) 143 | */ 144 | @Override 145 | public boolean dispatchTouchEvent(MotionEvent ev) { 146 | MotionEvent newEvent = ev; 147 | if (mTouchController != null) { 148 | newEvent = mTouchController.obtainTouchEvent(ev, this.getWidth(), this.getHeight(), mCurDegree); 149 | } 150 | // TODO Auto-generated method stub 151 | if (mOnDispatchTouchEventListener != null) { 152 | mOnDispatchTouchEventListener.onDispatchTouchEvent(this, ev); 153 | } 154 | return super.dispatchTouchEvent(newEvent); 155 | } 156 | 157 | /** 158 | * set scale percent. 159 | * @param sx scale x 160 | * @param sy scale y 161 | */ 162 | public void setScale(float sx, float sy) { 163 | scaleX = sx; 164 | scaleY = sy; 165 | } 166 | 167 | public void setTouchControllerListener(TouchController controller) { 168 | mTouchController = controller; 169 | } 170 | 171 | public void setOnDispatchTouchEventListener(TouchController.OnDispatchTouchEventListener listener) { 172 | mOnDispatchTouchEventListener = listener; 173 | } 174 | 175 | } 176 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/ExtSurfaceView.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | import android.content.Context; 4 | import android.os.Handler; 5 | import android.util.AttributeSet; 6 | import android.util.Log; 7 | import android.view.MotionEvent; 8 | import android.view.SurfaceView; 9 | import android.view.animation.Animation; 10 | import android.view.animation.RotateAnimation; 11 | 12 | import com.guo.android_extend.widget.controller.TouchController; 13 | 14 | /** 15 | * create by gqjjqg,. 16 | * easy to use opengl surface.. 17 | */ 18 | 19 | public class ExtSurfaceView extends SurfaceView implements ExtOrientationDetector.OnOrientationListener { 20 | private final String TAG = this.getClass().getSimpleName(); 21 | 22 | private Handler mHandler; 23 | 24 | /** 25 | * animation during time. 26 | */ 27 | private final int ANIMATION_TIME = RotateRunable.ANIMATION_TIME; 28 | 29 | private int mCurDegree; 30 | 31 | private double mAspectRatio; 32 | private boolean mFitMaxArea; 33 | /** 34 | * for touch point rotate. 35 | */ 36 | private TouchController mTouchController; 37 | 38 | /** 39 | * for dispatch touch event process. 40 | */ 41 | private TouchController.OnDispatchTouchEventListener mOnDispatchTouchEventListener; 42 | 43 | public ExtSurfaceView(Context context, AttributeSet attrs) { 44 | super(context, attrs); 45 | // TODO Auto-generated constructor stub 46 | onCreate(); 47 | } 48 | 49 | public ExtSurfaceView(Context context) { 50 | super(context); 51 | // TODO Auto-generated constructor stub 52 | onCreate(); 53 | } 54 | 55 | private void onCreate() { 56 | mAspectRatio = 0.0; 57 | mHandler = new Handler(); 58 | mTouchController = new TouchController(); 59 | } 60 | 61 | public void setAutoFitMax(boolean enable) { 62 | mFitMaxArea = enable; 63 | } 64 | 65 | public void setAspectRatio(double ratio) { 66 | if (mAspectRatio != ratio) { 67 | mAspectRatio = ratio; 68 | requestLayout(); 69 | } 70 | } 71 | 72 | public void setAspectRatio(int width, int height) { 73 | double ratio = ((double)width / (double)height); 74 | if (mAspectRatio != ratio) { 75 | mAspectRatio = ratio; 76 | requestLayout(); 77 | } 78 | } 79 | 80 | /* (non-Javadoc) 81 | * @see android.view.SurfaceView#onMeasure(int, int) 82 | */ 83 | @Override 84 | protected void onMeasure(int widthSpec, int heightSpec) { 85 | // TODO Auto-generated method stub 86 | int width = MeasureSpec.getSize(widthSpec); 87 | int height = MeasureSpec.getSize(heightSpec); 88 | 89 | if (mAspectRatio != 0) { 90 | int w1 = width, h1 = height; 91 | if (w1 > h1 * mAspectRatio) { 92 | w1 = (int) (h1 * mAspectRatio + .5); 93 | } else { 94 | h1 = (int) (w1 / mAspectRatio + .5); 95 | } 96 | if (mFitMaxArea) { 97 | int w2 = width, h2 = height; 98 | double ratio = 1.0 / mAspectRatio; 99 | if (w2 > h2 * ratio) { 100 | w2 = (int) (h2 * ratio + .5); 101 | } else { 102 | h2 = (int) (w2 / ratio + .5); 103 | } 104 | if (w1 * h1 > w2 * h2) { 105 | width = w1; 106 | height = h1; 107 | } else { 108 | width = w2; 109 | height = h2; 110 | } 111 | } else { 112 | width = w1; 113 | height = h1; 114 | } 115 | } 116 | 117 | widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); 118 | heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 119 | 120 | super.onMeasure(widthSpec, heightSpec); 121 | } 122 | 123 | @Override 124 | public boolean OnOrientationChanged(int degree, int offset, int flag) { 125 | if (flag != ExtOrientationDetector.ROTATE_FORCE_REDO) { 126 | if (!this.isShown()) { 127 | Log.i(TAG, "Not Shown!"); 128 | return false; 129 | } 130 | } 131 | 132 | Animation animation = new RotateAnimation(offset, 0, 133 | Animation.RELATIVE_TO_SELF, 0.5f, 134 | Animation.RELATIVE_TO_SELF, 0.5f); 135 | animation.setDuration(ANIMATION_TIME); 136 | animation.setFillAfter(true); 137 | mHandler.post(new RotateRunable(animation, this, degree)); 138 | 139 | mCurDegree = degree; 140 | return true; 141 | } 142 | 143 | @Override 144 | public int getCurrentOrientationDegree() { 145 | return mCurDegree; 146 | } 147 | 148 | /* (non-Javadoc) 149 | * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent) 150 | */ 151 | @Override 152 | public boolean dispatchTouchEvent(MotionEvent ev) { 153 | MotionEvent newEvent = ev; 154 | if (mTouchController != null) { 155 | newEvent = mTouchController.obtainTouchEvent(ev, this.getWidth(), this.getHeight(), mCurDegree); 156 | } 157 | // TODO Auto-generated method stub 158 | if (mOnDispatchTouchEventListener != null) { 159 | mOnDispatchTouchEventListener.onDispatchTouchEvent(this, ev); 160 | } 161 | return super.dispatchTouchEvent(newEvent); 162 | } 163 | 164 | public void setOnDispatchTouchEventListener(TouchController.OnDispatchTouchEventListener listener) { 165 | mOnDispatchTouchEventListener = listener; 166 | } 167 | 168 | } 169 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/GestureDetector.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | import android.content.Context; 4 | import android.util.Log; 5 | import android.view.MotionEvent; 6 | import android.view.View; 7 | import android.view.ViewConfiguration; 8 | 9 | /** 10 | * Created by gqj3375 on 2017/2/27. 11 | */ 12 | 13 | public class GestureDetector { 14 | private final String TAG = this.getClass().toString(); 15 | private float sx, sy; 16 | private float mx, my; 17 | private float ex, ey; 18 | private float distance; 19 | private int touchMode; 20 | private int count_scale; 21 | private int count_translate; 22 | private GestureListener mGestureListener; 23 | 24 | public static int TOUCHMODE_ROTATE_RESTART = -2; 25 | public static int TOUCHMODE_DOUBLE_PRE = -1; 26 | public static int TOUCHMODE_NONE = 0; 27 | public static int TOUCHMODE_ROTATE = 1; 28 | public static int TOUCHMODE_DOUBLE_SCALE = 2; 29 | public static int TOUCHMODE_DOUBLE_MOVE = 3; 30 | 31 | private int MAX_DISTANCE_MOVE; 32 | private int MAX_DETECTED_COUNT; 33 | 34 | public interface GestureListener { 35 | public void single_drag_prepare(float x, float y); 36 | public void single_drag_process(float x, float y); 37 | 38 | public void double_scale_prepare(float scale); 39 | public void double_scale_process(float scale); 40 | 41 | public void double_drag_prepare(float x, float y); 42 | public void double_drag_process(float x, float y); 43 | } 44 | 45 | public GestureDetector(Context context) { 46 | mGestureListener = null; 47 | final ViewConfiguration configuration = ViewConfiguration.get(context); 48 | MAX_DISTANCE_MOVE = configuration.getScaledTouchSlop(); 49 | MAX_DETECTED_COUNT = 3; 50 | Log.i(TAG, "MAX_DISTANCE_MOVE = " + MAX_DISTANCE_MOVE); 51 | } 52 | 53 | public void setGestureListener(GestureListener gestureListener) { 54 | mGestureListener = gestureListener; 55 | } 56 | 57 | public void onTouch(View view, MotionEvent event) { 58 | switch (event.getAction() & MotionEvent.ACTION_MASK) { 59 | case MotionEvent.ACTION_DOWN: 60 | sx = event.getX(0); 61 | sy = event.getY(0); 62 | touchMode = TOUCHMODE_ROTATE; 63 | //start rotate 64 | if (mGestureListener != null) { 65 | mGestureListener.single_drag_prepare(sx, sy); 66 | } 67 | count_scale = MAX_DETECTED_COUNT; 68 | count_translate = MAX_DETECTED_COUNT; 69 | break; 70 | case MotionEvent.ACTION_POINTER_DOWN: 71 | sx = event.getX(0); 72 | sy = event.getY(0); 73 | ex = event.getX(1); 74 | ey = event.getY(1); 75 | mx = (sx + ex) / 2.0f; 76 | my = (sy + ey) / 2.0f; 77 | distance = (float)Math.hypot(ex - sx, ey - sy); 78 | touchMode = TOUCHMODE_DOUBLE_PRE; 79 | break; 80 | case MotionEvent.ACTION_UP: 81 | touchMode = TOUCHMODE_NONE; 82 | break; 83 | case MotionEvent.ACTION_POINTER_UP: 84 | touchMode = TOUCHMODE_ROTATE_RESTART; 85 | break; 86 | case MotionEvent.ACTION_MOVE: 87 | if (touchMode == TOUCHMODE_ROTATE) { 88 | // ROTATE. 89 | if (mGestureListener != null) { 90 | mGestureListener.single_drag_process(event.getX(0), event.getY(0)); 91 | } 92 | } else if (touchMode == TOUCHMODE_DOUBLE_PRE) { 93 | if (event.getPointerCount() == 2) { 94 | float temp = (float) Math.hypot(event.getX(0) - event.getX(1), event.getY(0) - event.getY(1)); 95 | float offset1 = Math.abs(distance - temp); 96 | if (offset1 > MAX_DISTANCE_MOVE) { 97 | count_scale--; 98 | distance = temp; 99 | } 100 | 101 | float tx = (event.getX(0) + event.getX(1)) / 2; 102 | float ty = (event.getY(0) + event.getY(1)) / 2; 103 | float offset2 = (float) Math.hypot(mx - tx, my - ty); 104 | if (offset2 > MAX_DISTANCE_MOVE) { 105 | count_translate--; 106 | mx = tx; 107 | my = ty; 108 | } 109 | 110 | if (count_scale == 0) { 111 | touchMode = TOUCHMODE_DOUBLE_SCALE; 112 | //start scale 113 | if (mGestureListener != null) { 114 | mGestureListener.double_scale_prepare(1.0f); 115 | } 116 | } 117 | 118 | if (count_translate == 0) { 119 | touchMode = TOUCHMODE_DOUBLE_MOVE; 120 | //start move 121 | if (mGestureListener != null) { 122 | mGestureListener.double_drag_prepare(mx, my); 123 | } 124 | } 125 | } 126 | } else if (touchMode == TOUCHMODE_ROTATE_RESTART) { 127 | if (mGestureListener != null) { 128 | mGestureListener.single_drag_prepare(event.getX(0), event.getY(0)); 129 | mGestureListener.single_drag_process(event.getX(0), event.getY(0)); 130 | } 131 | touchMode = TOUCHMODE_ROTATE; 132 | } else if (touchMode == TOUCHMODE_DOUBLE_SCALE) { 133 | float temp = (float)Math.hypot(event.getX(0) - event.getX(1), event.getY(0) - event.getY(1)); 134 | float scale = (temp / distance); 135 | if (mGestureListener != null) { 136 | mGestureListener.double_scale_process(scale); 137 | } 138 | } else if (touchMode == TOUCHMODE_DOUBLE_MOVE) { 139 | if (mGestureListener != null) { 140 | float tx = (event.getX(0) + event.getX(1)) / 2; 141 | float ty = (event.getY(0) + event.getY(1)) / 2; 142 | mGestureListener.double_drag_process(tx, ty); 143 | } 144 | } 145 | break; 146 | default:; 147 | } 148 | } 149 | 150 | } 151 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/RotateRunable.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | import android.view.View; 4 | import android.view.animation.Animation; 5 | 6 | /** 7 | * @author qijiang.guo 8 | * @see com.guo.android_extend.widget.ExtImageButton 9 | */ 10 | public class RotateRunable implements Runnable { 11 | /** 12 | * DEFAULT ANIMATION TIME. 13 | */ 14 | static final int ANIMATION_TIME = 300; 15 | 16 | /** 17 | * rotate animation. 18 | */ 19 | private Animation mAnimation; 20 | 21 | /** 22 | * target view. 23 | */ 24 | private View mContextView; 25 | 26 | /** 27 | * @deprecated param relayout not working. 28 | * 29 | * @param animation animation 30 | * @param v the animation view. 31 | * @param degree rotate degree. 32 | * @param relayout not working. 33 | */ 34 | public RotateRunable(Animation animation, View v, int degree, boolean relayout) { 35 | super(); 36 | // TODO Auto-generated constructor stub 37 | mAnimation = animation; 38 | mContextView = v; 39 | } 40 | 41 | public RotateRunable(Animation animation, View v, int degree) { 42 | super(); 43 | // TODO Auto-generated constructor stub 44 | mAnimation = animation; 45 | mContextView = v; 46 | } 47 | 48 | @Override 49 | public void run() { 50 | // TODO Auto-generated method stub 51 | mContextView.startAnimation(mAnimation); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/VerticalSeekBar.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.util.AttributeSet; 6 | import android.view.MotionEvent; 7 | import android.widget.SeekBar; 8 | 9 | 10 | public class VerticalSeekBar extends SeekBar { 11 | public VerticalSeekBar(Context context) { 12 | super(context); 13 | } 14 | 15 | public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) { 16 | super(context, attrs, defStyle); 17 | } 18 | 19 | public VerticalSeekBar(Context context, AttributeSet attrs) { 20 | super(context, attrs); 21 | } 22 | 23 | /* (non-Javadoc) 24 | * @see android.widget.AbsSeekBar#onSizeChanged(int, int, int, int) 25 | */ 26 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 27 | super.onSizeChanged(h, w, oldh, oldw); 28 | } 29 | 30 | /* (non-Javadoc) 31 | * @see android.widget.AbsSeekBar#onMeasure(int, int) 32 | */ 33 | @Override 34 | protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 35 | super.onMeasure(heightMeasureSpec, widthMeasureSpec); 36 | setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); 37 | /** 38 | * when the layout is changed. 39 | * we also should changed the thumb's position 40 | */ 41 | super.onSizeChanged(getMeasuredHeight(), getMeasuredWidth(), 0, 0); 42 | } 43 | 44 | /* (non-Javadoc) 45 | * @see android.widget.AbsSeekBar#onDraw(android.graphics.Canvas) 46 | */ 47 | protected void onDraw(Canvas c) { 48 | c.rotate(-90); 49 | c.translate(-getHeight(),0); 50 | 51 | super.onDraw(c); 52 | } 53 | 54 | /* (non-Javadoc) 55 | * @see android.widget.AbsSeekBar#onTouchEvent(android.view.MotionEvent) 56 | */ 57 | @Override 58 | public boolean onTouchEvent(MotionEvent event) { 59 | if (!isEnabled()) { 60 | return false; 61 | } 62 | 63 | switch (event.getAction()) { 64 | case MotionEvent.ACTION_DOWN: 65 | case MotionEvent.ACTION_MOVE: 66 | case MotionEvent.ACTION_UP: 67 | int i = getMax() - (int) (getMax() * event.getY() / getHeight()); 68 | setProgress(i); 69 | break; 70 | 71 | case MotionEvent.ACTION_CANCEL: 72 | break; 73 | } 74 | return true; 75 | } 76 | 77 | /* (non-Javadoc) 78 | * @see android.widget.ProgressBar#setProgress(int) 79 | */ 80 | @Override 81 | public synchronized void setProgress(int progress) { 82 | // TODO Auto-generated method stub 83 | super.setProgress(progress); 84 | onSizeChanged(getWidth(), getHeight(), 0, 0); 85 | } 86 | 87 | /* (non-Javadoc) 88 | * @see android.widget.AbsSeekBar#setMax(int) 89 | */ 90 | @Override 91 | public synchronized void setMax(int max) { 92 | // TODO Auto-generated method stub 93 | super.setMax(max); 94 | onSizeChanged(getWidth(), getHeight(), 0, 0); 95 | } 96 | } -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/controller/AbstractController.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget.controller; 2 | 3 | import android.graphics.Canvas; 4 | import android.graphics.PointF; 5 | import android.view.MotionEvent; 6 | import android.view.View.OnTouchListener; 7 | 8 | public abstract class AbstractController implements OnTouchListener { 9 | 10 | protected ControllerListener mListener; 11 | 12 | protected PointF mCurPointDown; 13 | protected PointF mCurPointMidd; 14 | 15 | protected float mPreDistance; 16 | protected float mPreDegree; 17 | 18 | protected final float PRECISION = 0.001F; 19 | 20 | public interface ControllerListener { 21 | public void invalidate(); 22 | public PointF getCenterPoint(); 23 | } 24 | 25 | public AbstractController(ControllerListener mListener) { 26 | // TODO Auto-generated constructor stub 27 | this.mListener = mListener; 28 | mCurPointDown = new PointF(); 29 | mCurPointMidd = new PointF(); 30 | mPreDistance = 0F; 31 | mPreDegree = 0F; 32 | } 33 | 34 | public abstract void initialize(float imageWidth, float imageHeight, float worldWidth, float worldHeight); 35 | 36 | public abstract void beforeDraw(Canvas canvas); 37 | 38 | public abstract void afterDraw(Canvas canvas); 39 | 40 | /** 41 | * @param event the motion event. 42 | * @return distance between point 0 and point 1 43 | */ 44 | protected float getDistance(MotionEvent event) { 45 | float x = event.getX(0) - event.getX(1); 46 | float y = event.getY(0) - event.getY(1); 47 | return (float) Math.sqrt(x * x + y * y); 48 | } 49 | 50 | /** 51 | * @param event motion event. 52 | * @return get rotation between point 0 and point 1 53 | */ 54 | protected float getRotation(MotionEvent event) { 55 | double radians = Math.atan2((event.getY(0) - event.getY(1)), (event.getX(0) - event.getX(1))); 56 | return (float) Math.toDegrees(radians); 57 | } 58 | 59 | /** 60 | * @param A point a 61 | * @param B point b 62 | * @return get rotation between point 0 and point 1 63 | */ 64 | protected float getRotation(PointF A, PointF B) { 65 | double radians = Math.atan2((A.y - B.y), (A.x - B.x)); 66 | return (float) Math.toDegrees(radians); 67 | } 68 | 69 | /** 70 | * 71 | * @param A point a 72 | * @param B point b 73 | * @param Center point center. 74 | * @return the degree of a center b. 75 | */ 76 | protected float getRotation(PointF A, PointF B, PointF Center) { 77 | PointF VCA = new PointF(A.x - Center.x, A.y - Center.y); 78 | PointF VCB = new PointF(B.x - Center.x, B.y - Center.y); 79 | float AXB = VCA.x * VCB.y - VCA.y * VCB.x; 80 | double ABS_VA = Math.hypot(VCA.x, VCA.y); 81 | double ABS_VB = Math.hypot(VCB.x, VCB.y); 82 | double radians = Math.asin( AXB / (ABS_VA * ABS_VB) ); 83 | return (float) Math.toDegrees(radians); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /android-extend/src/main/src/com/guo/android_extend/widget/controller/TouchController.java: -------------------------------------------------------------------------------- 1 | package com.guo.android_extend.widget.controller; 2 | 3 | import android.annotation.TargetApi; 4 | import android.graphics.PointF; 5 | import android.os.Build; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | 9 | /** 10 | * Created by gqj3375 on 2017/1/10. 11 | */ 12 | 13 | public class TouchController { 14 | 15 | public interface OnDispatchTouchEventListener { 16 | public void onDispatchTouchEvent(View v, MotionEvent ev); 17 | } 18 | 19 | /** 20 | * @param A 21 | * @param B center point 22 | * @param degree 23 | * @return 24 | */ 25 | private PointF rotatePoint(PointF A, PointF B, float degree) { 26 | float radian = (float) Math.toRadians(degree); 27 | float cos = (float) Math.cos(radian); 28 | float sin = (float) Math.sin(radian); 29 | float x = (float) ((A.x - B.x)* cos +(A.y - B.y) * sin + B.x); 30 | float y = (float) (-(A.x - B.x)* sin + (A.y - B.y) * cos + B.y); 31 | return new PointF(x, y); 32 | } 33 | 34 | @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 35 | public MotionEvent obtainTouchEvent(MotionEvent ev, int view_w, int view_h, int cur_degree) { 36 | MotionEvent newEvent; 37 | if (false) { // suport one touch 38 | PointF newPoint = rotatePoint(new PointF(ev.getX(), ev.getY()), 39 | new PointF(view_w / 2F, view_h / 2F), -cur_degree); 40 | newEvent = MotionEvent.obtain(ev.getDownTime(), 41 | ev.getEventTime(), ev.getAction(), newPoint.x, newPoint.y, 42 | ev.getPressure(), ev.getSize(), ev.getMetaState(), 43 | ev.getXPrecision(), ev.getYPrecision(), ev.getDeviceId(), 44 | ev.getEdgeFlags()); 45 | } else { 46 | MotionEvent.PointerProperties[] preo = new MotionEvent.PointerProperties[ev.getPointerCount()]; 47 | MotionEvent.PointerCoords[] coor = new MotionEvent.PointerCoords[ev.getPointerCount()]; 48 | for (int i = 0; i < ev.getPointerCount(); i++) { 49 | preo[i] = new MotionEvent.PointerProperties(); 50 | coor[i] = new MotionEvent.PointerCoords(); 51 | ev.getPointerProperties(i, preo[i]); 52 | ev.getPointerCoords(i, coor[i]); 53 | PointF newPoint = rotatePoint(new PointF(coor[i].x, coor[i].y), 54 | new PointF(view_w / 2F, view_h / 2F), -cur_degree); 55 | coor[i].x = newPoint.x; 56 | coor[i].y = newPoint.y; 57 | } 58 | newEvent = MotionEvent.obtain(ev.getDownTime(), ev.getEventTime(), ev.getAction(), ev.getPointerCount(), preo, 59 | coor, ev.getMetaState(), ev.getButtonState(), ev.getXPrecision(), ev.getYPrecision(), ev.getDeviceId(), 60 | ev.getEdgeFlags(), ev.getSource(), ev.getFlags()); 61 | } 62 | 63 | return newEvent; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | repositories { 4 | google() 5 | mavenCentral() 6 | } 7 | dependencies { 8 | classpath "com.android.tools.build:gradle:4.2.0+" 9 | // NOTE: Do not place your application dependencies here; they belong 10 | // in the individual module build.gradle files 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | google() 17 | mavenCentral() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app"s APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gqjjqg/android-extend/e4ca8dee85bc1aeb7168551ba837737b35eaf463/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Oct 04 00:40:12 CST 2021 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /jitpack.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - openjdk11 -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = "Samples" 2 | include ':samples' 3 | include ':android-extend' 4 | --------------------------------------------------------------------------------