diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd45b12 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*.iml +.gradle +/local.properties +/.idea/caches/build_file_checksums.ser +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..09556ab --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..37a7509 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..0af46ce --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,33 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 27 + defaultConfig { + applicationId "mohammadaminha.com.widgets_package" + minSdkVersion 17 + targetSdkVersion 27 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.android.support:support-v4:27.1.1' + implementation 'com.android.support:design:27.1.1' + implementation 'com.android.support:recyclerview-v7:27.1.1' + implementation 'com.github.hotchemi:khronos:0.9.0' + implementation 'com.android.support:cardview-v7:27.1.1' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'com.android.support.test:runner:0.5' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' + implementation project(':widgets') +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/mohammadaminha/com/widgets_package/ExampleInstrumentedTest.java b/app/src/androidTest/java/mohammadaminha/com/widgets_package/ExampleInstrumentedTest.java new file mode 100644 index 0000000..2dd160d --- /dev/null +++ b/app/src/androidTest/java/mohammadaminha/com/widgets_package/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package mohammadaminha.com.widgets_package; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("mohammadaminha.com.widgets_package", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..8e11841 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/assets/font/BYekan.ttf b/app/src/main/assets/font/BYekan.ttf new file mode 100644 index 0000000..168a908 Binary files /dev/null and b/app/src/main/assets/font/BYekan.ttf differ diff --git a/app/src/main/java/mohammadaminha/com/widgets_package/G.java b/app/src/main/java/mohammadaminha/com/widgets_package/G.java new file mode 100644 index 0000000..6097cee --- /dev/null +++ b/app/src/main/java/mohammadaminha/com/widgets_package/G.java @@ -0,0 +1,14 @@ +package mohammadaminha.com.widgets_package; + +import android.app.Application; + +import mohammadaminha.com.widgets.Util; + +public class G extends Application { + @Override + public void onCreate() { + super.onCreate(); + new Util("font/BYekan.ttf", getApplicationContext()); + } + +} diff --git a/app/src/main/java/mohammadaminha/com/widgets_package/MainActivity.java b/app/src/main/java/mohammadaminha/com/widgets_package/MainActivity.java new file mode 100644 index 0000000..db8e8d2 --- /dev/null +++ b/app/src/main/java/mohammadaminha/com/widgets_package/MainActivity.java @@ -0,0 +1,13 @@ +package mohammadaminha.com.widgets_package; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + } +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..7c22d2b --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..eca70cf --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..eca70cf --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..898f3ed Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000..dffca36 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..64ba76f Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000..dae5e08 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..e5ed465 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000..14ed0af Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..b0907ca Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..d8ae031 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..2c18de9 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..beed3cd Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..69b2233 --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #008577 + #00574B + #D81B60 + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..711e60d --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Widgets_Package + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml new file mode 100644 index 0000000..5885930 --- /dev/null +++ b/app/src/main/res/values/styles.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/app/src/test/java/mohammadaminha/com/widgets_package/ExampleUnitTest.java b/app/src/test/java/mohammadaminha/com/widgets_package/ExampleUnitTest.java new file mode 100644 index 0000000..f499593 --- /dev/null +++ b/app/src/test/java/mohammadaminha/com/widgets_package/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package mohammadaminha.com.widgets_package; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..4e8009d --- /dev/null +++ b/build.gradle @@ -0,0 +1,27 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + + repositories { + google() + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.2.0' + + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + google() + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..1487463 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,14 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx1536m +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true + diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..f6b961f Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..9a4163a --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..cccdd3d --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..f955316 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..55c2069 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +include ':app', ':widgets' diff --git a/widgets/.gitignore b/widgets/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/widgets/.gitignore @@ -0,0 +1 @@ +/build diff --git a/widgets/build.gradle b/widgets/build.gradle new file mode 100644 index 0000000..ac08554 --- /dev/null +++ b/widgets/build.gradle @@ -0,0 +1,39 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 27 + + + + defaultConfig { + minSdkVersion 17 + targetSdkVersion 27 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + + implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.android.support:support-v4:27.1.1' + implementation 'com.android.support:design:27.1.1' + implementation 'com.android.support:recyclerview-v7:27.1.1' + implementation 'com.github.hotchemi:khronos:0.9.0' + implementation 'com.android.support:cardview-v7:27.1.1' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'com.android.support.test:runner:0.5' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' +} diff --git a/widgets/proguard-rules.pro b/widgets/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/widgets/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/widgets/src/androidTest/java/mohammadaminha/com/widgets/ExampleInstrumentedTest.java b/widgets/src/androidTest/java/mohammadaminha/com/widgets/ExampleInstrumentedTest.java new file mode 100644 index 0000000..906797e --- /dev/null +++ b/widgets/src/androidTest/java/mohammadaminha/com/widgets/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("mohammadaminha.com.widgets.test", appContext.getPackageName()); + } +} diff --git a/widgets/src/main/AndroidManifest.xml b/widgets/src/main/AndroidManifest.xml new file mode 100644 index 0000000..b54e73e --- /dev/null +++ b/widgets/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Button.java b/widgets/src/main/java/mohammadaminha/com/widgets/Button.java new file mode 100644 index 0000000..e01a792 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Button.java @@ -0,0 +1,36 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.util.AttributeSet; + +/** + * Created by aj on 2/1/2018. + */ + +public class Button extends android.support.v7.widget.AppCompatButton{ + + + public Button(Context context) { + super(context); + + setTf(context); + } + + + public Button(Context context, AttributeSet attrs) { + super(context, attrs); + setTf(context); + } + + public Button(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setTf(context); + } + + + private void setTf(Context context) { + setTypeface(Util.getTypeFace()); + } + + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/CardView.java b/widgets/src/main/java/mohammadaminha/com/widgets/CardView.java new file mode 100644 index 0000000..54e7edb --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/CardView.java @@ -0,0 +1,34 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.AttributeSet; + +public class CardView extends android.support.v7.widget.CardView { + public CardView(@NonNull Context context) { + super(context); + Customizer(context); + } + + public CardView(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + + Customizer(context); + } + + public CardView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + Customizer(context); + + } + + private void Customizer(Context context) { + setLayoutDirection(LAYOUT_DIRECTION_RTL); + setTextDirection(TEXT_DIRECTION_RTL); + setPadding(6, 6, 6, 6); + setCardElevation(12f); + setUseCompatPadding(true); + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/CheckBox.java b/widgets/src/main/java/mohammadaminha/com/widgets/CheckBox.java new file mode 100644 index 0000000..09c47a2 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/CheckBox.java @@ -0,0 +1,48 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.content.res.ColorStateList; +import android.support.v4.content.ContextCompat; +import android.support.v4.widget.CompoundButtonCompat; +import android.util.AttributeSet; + +/** + * Created by aj on 2/1/2018. + */ + +public class CheckBox extends android.support.v7.widget.AppCompatCheckBox { + + + public CheckBox(Context context) { + super(context); + setTf(context); + } + + public CheckBox(Context context, AttributeSet attrs) { + super(context, attrs); + setTf(context); + } + + public CheckBox(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setTf(context); + } + + private void setTf(Context context) { + + ColorStateList colorStateList = new ColorStateList( + new int[][]{ + new int[]{-android.R.attr.state_checked}, // unchecked + new int[]{android.R.attr.state_checked} , // checked + }, + new int[]{ + ContextCompat.getColor(context,R.color.GrayColor), //unchecked color + ContextCompat.getColor(context,R.color.YellowColor), //checked color + } + ); + CompoundButtonCompat.setButtonTintList(this,colorStateList); + setTypeface(Util.getTypeFace()); + } + + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Coordinator.java b/widgets/src/main/java/mohammadaminha/com/widgets/Coordinator.java new file mode 100644 index 0000000..3354dc8 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Coordinator.java @@ -0,0 +1,25 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.support.design.widget.CoordinatorLayout; +import android.util.AttributeSet; + +public class Coordinator extends CoordinatorLayout { + public Coordinator(Context context) { + super(context); + Customizer(); + } + + public Coordinator(Context context, AttributeSet attrs) { + super(context, attrs); + Customizer(); + } + + public Coordinator(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + Customizer(); + } + private void Customizer(){ + setLayoutDirection(LAYOUT_DIRECTION_RTL); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/CurrencyEditText.java b/widgets/src/main/java/mohammadaminha/com/widgets/CurrencyEditText.java new file mode 100644 index 0000000..c465cb7 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/CurrencyEditText.java @@ -0,0 +1,179 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.graphics.Rect; +import android.text.Editable; +import android.text.InputFilter; +import android.text.InputType; +import android.text.TextWatcher; +import android.util.AttributeSet; +import android.widget.EditText; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.Locale; + +/** + * Created by PhanVanLinh on 25/07/2017. + * phanvanlinh.94vn@gmail.com + *

+ * Some note
+ *

  • Always use locale US instead of default to make DecimalFormat work well in all language
  • + */ +public class CurrencyEditText extends android.support.v7.widget.AppCompatEditText { + private static String prefix = ""; + private static final int MAX_LENGTH = 30; + private static final int MAX_DECIMAL = 3; + private CurrencyTextWatcher currencyTextWatcher = new CurrencyTextWatcher(this, prefix); + + + public CurrencyEditText(Context context) { + this(context, null); + setTf(context); + } + + public CurrencyEditText(Context context, AttributeSet attrs) { + this(context, attrs, android.support.v7.appcompat.R.attr.editTextStyle); + setTf(context); + } + + public CurrencyEditText(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + this.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); + this.setHint(prefix); + this.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_LENGTH)}); + setTf(context); + } + + @Override + protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { + super.onFocusChanged(focused, direction, previouslyFocusedRect); + if (focused) { + this.addTextChangedListener(currencyTextWatcher); + } else { + this.removeTextChangedListener(currencyTextWatcher); + } + handleCaseCurrencyEmpty(focused); + } + + /** + * When currency empty
    + * + When focus EditText, set the default text = prefix (ex: VND)
    + * + When EditText lose focus, set the default text = "", EditText will display hint (ex:VND) + */ + private void handleCaseCurrencyEmpty(boolean focused) { + if (focused) { + if (getText().toString().isEmpty()) { + setText(prefix); + } + } else { + if (getText().toString().equals(prefix)) { + setText(""); + } + } + } + + private static class CurrencyTextWatcher implements TextWatcher { + private final EditText editText; + private String previousCleanString; + private String prefix; + + CurrencyTextWatcher(EditText editText, String prefix) { + this.editText = editText; + this.prefix = prefix; + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + // do nothing + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + // do nothing + } + + @Override + public void afterTextChanged(Editable editable) { + String str = editable.toString(); + if (str.length() < prefix.length()) { + editText.setText(prefix); + editText.setSelection(prefix.length()); + return; + } + if (str.equals(prefix)) { + return; + } + // cleanString this the string which not contain prefix and , + String cleanString = str.replace(prefix, "").replaceAll("[,]", ""); + // for prevent afterTextChanged recursive call + if (cleanString.equals(previousCleanString) || cleanString.isEmpty()) { + return; + } + previousCleanString = cleanString; + + String formattedString; + if (cleanString.contains(".")) { + formattedString = formatDecimal(cleanString); + } else { + formattedString = formatInteger(cleanString); + } + editText.removeTextChangedListener(this); // Remove listener + editText.setText(formattedString); + handleSelection(); + editText.addTextChangedListener(this); // Add back the listener + } + + private String formatInteger(String str) { + BigDecimal parsed = new BigDecimal(str); + DecimalFormat formatter = + new DecimalFormat(prefix + "#,###", new DecimalFormatSymbols(Locale.US)); + return formatter.format(parsed); + } + + private String formatDecimal(String str) { + if (str.equals(".")) { + return prefix + "."; + } + BigDecimal parsed = new BigDecimal(str); + // example pattern VND #,###.00 + DecimalFormat formatter = new DecimalFormat(prefix + "#,###." + + getDecimalPattern(str), + new DecimalFormatSymbols(Locale.US)); + formatter.setRoundingMode(RoundingMode.DOWN); + return formatter.format(parsed); + } + + /** + * It will return suitable pattern for format decimal + * For example: 10.2 -> return 0 | 10.23 -> return 00, | 10.235 -> return 000 + */ + private String getDecimalPattern(String str) { + int decimalCount = str.length() - str.indexOf(".") - 1; + StringBuilder decimalPattern = new StringBuilder(); + for (int i = 0; i < decimalCount && i < MAX_DECIMAL; i++) { + decimalPattern.append("0"); + } + return decimalPattern.toString(); + } + + private void handleSelection() { + if (editText.getText().length() <= MAX_LENGTH) { + editText.setSelection(editText.getText().length()); + } else { + editText.setSelection(MAX_LENGTH); + } + } + } + + + private void setTf(Context context) { + + setTypeface(Util.getTypeFace()); + } + + + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/CustomTextView/JustifiedTextView.java b/widgets/src/main/java/mohammadaminha/com/widgets/CustomTextView/JustifiedTextView.java new file mode 100644 index 0000000..27c50e6 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/CustomTextView/JustifiedTextView.java @@ -0,0 +1,351 @@ +package mohammadaminha.com.widgets.CustomTextView; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint.Align; +import android.graphics.Rect; +import android.graphics.Typeface; +import android.text.TextPaint; +import android.util.AttributeSet; +import android.util.TypedValue; +import android.view.View; +import android.view.ViewTreeObserver; +import android.view.ViewTreeObserver.OnGlobalLayoutListener; + +import java.util.ArrayList; +import java.util.List; + +public class JustifiedTextView extends View { + + /** + * when we want to draw text after view created to avoid loop in drawing we use this boolean + */ + private boolean hasTextBeenDrown = false; + private Context mContext; + private TextPaint textPaint; + private int lineSpace = 0; + private int lineHeight; + private int textAreaWidth; + private int measuredViewHeight, measuredViewWidth; + private String text; + private List lineList = new ArrayList<>(); + + public JustifiedTextView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + constructor(context, attrs); + } + + public JustifiedTextView(Context context, AttributeSet attrs) { + super(context, attrs); + constructor(context, attrs); + } + + public JustifiedTextView(Context context) { + super(context); + constructor(context, null); + + } + + private void constructor(Context context, AttributeSet attrs) { + + mContext = context; + XmlToClassAttribHandler mXmlParser = new XmlToClassAttribHandler(mContext, attrs); + initTextPaint(); + + if (attrs != null) { + String text; + int textColor; + int textSize; + int textSizeUnit; + + text = mXmlParser.getTextValue(); + textColor = mXmlParser.getColorValue(); + textSize = mXmlParser.getTextSize(); + textSizeUnit = mXmlParser.gettextSizeUnit(); + + + setText(text); + setTextColor(textColor); + if (textSizeUnit == -1) + setTextSize(textSize); + else + setTextSize(textSizeUnit, textSize); + +// setText(XmlToClassAttribHandler.GetAttributeStringValue(mContext, attrs, namespace, key, "")); + + } + + ViewTreeObserver observer = getViewTreeObserver(); + + + observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { + + @Override + public void onGlobalLayout() { + + if (hasTextBeenDrown) + return; + hasTextBeenDrown = true; + setTextAreaWidth(getWidth() - (getPaddingLeft() + getPaddingRight())); + calculate(); + + } + + + }); + + } + + private void calculate() { + setLineHeight(getTextPaint()); + lineList.clear(); + lineList = divideOriginalTextToStringLineList(getText()); + setMeasuredDimentions(lineList.size(), getLineHeight(), getLineSpace()); + measure(getMeasuredViewWidth(), getMeasuredViewHeight()); + } + + private void initTextPaint() { + textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); + textPaint.setTextAlign(Align.RIGHT); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + if (getMeasuredViewWidth() > 0) { + requestLayout(); + setMeasuredDimension(getMeasuredViewWidth(), getMeasuredViewHeight()); + } else { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + invalidate(); + } + + @Override + protected void onDraw(Canvas canvas) { + + int rowIndex = getPaddingTop(); + int colIndex ; + if (getAlignment() == Align.RIGHT) + colIndex = getPaddingLeft() + getTextAreaWidth(); + else + colIndex = getPaddingLeft(); + + for (int i = 0; i < lineList.size(); i++) { + rowIndex += getLineHeight() + getLineSpace(); + + canvas.drawText(lineList.get(i), colIndex, rowIndex, getTextPaint()); + } + + } + + + private List divideOriginalTextToStringLineList(String originalText) { + + List listStringLine = new ArrayList<>(); + + String line = ""; + float textWidth; + + String[] listParageraphes = originalText.split("\n"); + + for (String listParageraphe : listParageraphes) { + String[] arrayWords = listParageraphe.split(" "); + + for (int i = 0; i < arrayWords.length; i++) { + + line += arrayWords[i] + " "; + textWidth = getTextPaint().measureText(line); + + //if text width is equal to textAreaWidth then just add it to ListStringLine + if (getTextAreaWidth() == textWidth) { + + listStringLine.add(line); + line = "";//make line clear + continue; + } + //else if text width excite textAreaWidth then remove last word and justify the StringLine + else if (getTextAreaWidth() < textWidth) { + + int lastWordCount = arrayWords[i].length(); + + //remove last word that cause line width to excite textAreaWidth + line = line.substring(0, line.length() - lastWordCount - 1); + + // if line is empty then should be skipped + if (line.trim().length() == 0) + continue; + + //and then we need to justify line + line = justifyTextLine(textPaint, line.trim(), getTextAreaWidth()); + + listStringLine.add(line); + line = ""; + i--; + continue; + } + + //if we are now at last line of paragraph then just add it + if (i == arrayWords.length - 1) { + listStringLine.add(line); + line = ""; + } + } + } + + return listStringLine; + + } + + private String justifyTextLine(TextPaint textPaint, String lineString, int textAreaWidth) { + + int gapIndex = 0; + + float lineWidth = textPaint.measureText(lineString); + + while (lineWidth < textAreaWidth && lineWidth > 0) { + + gapIndex = lineString.indexOf(" ", gapIndex + 2); + if (gapIndex == -1) { + gapIndex = 0; + gapIndex = lineString.indexOf(" ", gapIndex + 1); + if (gapIndex == -1) + return lineString; + } + + lineString = lineString.substring(0, gapIndex) + " " + lineString.substring(gapIndex + 1, lineString.length()); + + lineWidth = textPaint.measureText(lineString); + } + return lineString; + } + + private void setLineHeight(TextPaint textPaint) { + + Rect bounds = new Rect(); + String sampleStr = ""; + textPaint.getTextBounds(sampleStr, 0, sampleStr.length(), bounds); + + setLineHeight(bounds.height()); + + } + + private void setMeasuredDimentions(int lineListSize, int lineHeigth, int lineSpace) { + int mHeight = lineListSize * (lineHeigth + lineSpace) + lineSpace; + + mHeight += getPaddingRight() + getPaddingLeft(); + + setMeasuredViewHeight(mHeight); + + setMeasuredViewWidth(getWidth()); + } + + + private int getTextAreaWidth() { + return textAreaWidth; + } + + private void setTextAreaWidth(int textAreaWidth) { + this.textAreaWidth = textAreaWidth; + } + + private int getLineHeight() { + return lineHeight; + } + + private void setLineHeight(int lineHeight) { + this.lineHeight = lineHeight; + } + + private int getMeasuredViewHeight() { + return measuredViewHeight; + } + + private void setMeasuredViewHeight(int measuredViewHeight) { + this.measuredViewHeight = measuredViewHeight; + } + + private int getMeasuredViewWidth() { + return measuredViewWidth; + } + + private void setMeasuredViewWidth(int measuredViewWidth) { + this.measuredViewWidth = measuredViewWidth; + } + + private String getText() { + return text; + } + + public void setText(int resid) { + setText(mContext.getResources().getString(resid)); + } + + private void setText(String text) { + this.text = text; + calculate(); + invalidate(); + } + + public Typeface getTypeFace() { + return getTextPaint().getTypeface(); + } + + public void setTypeFace(Typeface typeFace) { + getTextPaint().setTypeface(typeFace); + } + + public float getTextSize() { + return getTextPaint().getTextSize(); + } + + private void setTextSize(float textSize) { + getTextPaint().setTextSize(textSize); + calculate(); + invalidate(); + } + + private void setTextSize(int unit, float textSize) { + textSize = TypedValue.applyDimension(unit, textSize, mContext.getResources().getDisplayMetrics()); + setTextSize(textSize); + } + + private TextPaint getTextPaint() { + return textPaint; + } + + public void setTextPaint(TextPaint textPaint) { + this.textPaint = textPaint; + } + + public void setLineSpacing(int lineSpace) { + this.lineSpace = lineSpace; + invalidate(); + } + + /*** + * @return text color + */ + public int getTextColor() { + return getTextPaint().getColor(); + } + + private void setTextColor(int textColor) { + getTextPaint().setColor(textColor); + invalidate(); + } + + private int getLineSpace() { + return lineSpace; + } + + private Align getAlignment() { + return getTextPaint().getTextAlign(); + } + + public void setAlignment(Align align) { + getTextPaint().setTextAlign(align); + invalidate(); + } + + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/CustomTextView/XmlToClassAttribHandler.java b/widgets/src/main/java/mohammadaminha/com/widgets/CustomTextView/XmlToClassAttribHandler.java new file mode 100644 index 0000000..64dbf7e --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/CustomTextView/XmlToClassAttribHandler.java @@ -0,0 +1,131 @@ +package mohammadaminha.com.widgets.CustomTextView; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Color; +import android.util.AttributeSet; +import android.util.TypedValue; + +class XmlToClassAttribHandler { + private final String KEY_TEXT_SIZE = "textSize"; + private final Resources mRes; + private final Context mContext; + private final AttributeSet mAttributeSet; + private final String namespace = "http://noghteh.ir"; + + public XmlToClassAttribHandler(Context context, AttributeSet attributeSet) { + mContext = context; + mRes = mContext.getResources(); + mAttributeSet = attributeSet; + } + + public String getTextValue() { + + String KEY_TEXT = "text"; + String value = mAttributeSet.getAttributeValue(namespace, KEY_TEXT); + + if (value == null) + return ""; + + if (value.length() > 1 && + value.charAt(0) == '@' && + value.contains("@string/")) { + int resId = mRes.getIdentifier(mContext.getPackageName() + ":" + value.substring(1), null, null); + value = mRes.getString(resId); + } + + return value; + + } + + public int getColorValue() { + + String KEY_TEXT_COLOR = "textColor"; + String value = mAttributeSet.getAttributeValue(namespace, KEY_TEXT_COLOR); + + int color = Color.BLACK; + + if (value == null) + return color; + + if (value.length() > 1 && + value.charAt(0) == '@' && + value.contains("@color/")) { + int resId = mRes.getIdentifier(mContext.getPackageName() + ":" + value.substring(1), null, null); + color = mRes.getColor(resId); + + return color; + } + + + try { + color = Color.parseColor(value); + } catch (Exception e) { + return Color.BLACK; + } + + + return color; + } + + + public int getTextSize() { + int textSize = 12; + + String value = mAttributeSet.getAttributeValue(namespace, KEY_TEXT_SIZE); + + if (value == null) + return textSize; + + if (value.length() > 1 && + value.charAt(0) == '@' && + value.contains("@dimen/")) { + int resId = mRes.getIdentifier(mContext.getPackageName() + ":" + value.substring(1), null, null); + textSize = mRes.getDimensionPixelSize(resId); + + return textSize; + } + + try { + textSize = Integer.parseInt(value.substring(0, value.length() - 2)); + } catch (Exception e) { + return 12; + } + + return textSize; + } + + + public int gettextSizeUnit() { + + String value = mAttributeSet.getAttributeValue(namespace, KEY_TEXT_SIZE); + + if (value == null) + return TypedValue.COMPLEX_UNIT_SP; + + try { + String type = value.substring(value.length() - 2, value.length()); + + switch (type) { + case "dp": + return TypedValue.COMPLEX_UNIT_DIP; + case "sp": + return TypedValue.COMPLEX_UNIT_SP; + case "pt": + return TypedValue.COMPLEX_UNIT_PT; + case "mm": + return TypedValue.COMPLEX_UNIT_MM; + case "in": + return TypedValue.COMPLEX_UNIT_IN; + case "px": + return TypedValue.COMPLEX_UNIT_PX; + } + + } catch (Exception e) { + return -1; + } + + return -1; + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/CustomTypefaceSpan.java b/widgets/src/main/java/mohammadaminha/com/widgets/CustomTypefaceSpan.java new file mode 100644 index 0000000..6a99404 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/CustomTypefaceSpan.java @@ -0,0 +1,50 @@ +package mohammadaminha.com.widgets; + +import android.graphics.Paint; +import android.graphics.Typeface; +import android.text.TextPaint; +import android.text.style.TypefaceSpan; + +/** + * Created by amin on 1/18/18. + */ + +public class CustomTypefaceSpan extends TypefaceSpan { + private final Typeface newType; + + public CustomTypefaceSpan(String family, Typeface type) { + super(family); + newType = type; + } + + @Override + public void updateDrawState(TextPaint ds) { + applyCustomTypeFace(ds, newType); + } + + @Override + public void updateMeasureState(TextPaint paint) { + applyCustomTypeFace(paint, newType); + } + + private static void applyCustomTypeFace(Paint paint, Typeface tf) { + int oldStyle; + Typeface old = paint.getTypeface(); + if (old == null) { + oldStyle = 0; + } else { + oldStyle = old.getStyle(); + } + + int fake = oldStyle & ~tf.getStyle(); + if ((fake & Typeface.BOLD) != 0) { + paint.setFakeBoldText(true); + } + + if ((fake & Typeface.ITALIC) != 0) { + paint.setTextSkewX(-0.25f); + } + + paint.setTypeface(Util.getTypeFace()); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/CustomViewPager/AutoScrollViewPager.java b/widgets/src/main/java/mohammadaminha/com/widgets/CustomViewPager/AutoScrollViewPager.java new file mode 100644 index 0000000..3f7026d --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/CustomViewPager/AutoScrollViewPager.java @@ -0,0 +1,311 @@ +package mohammadaminha.com.widgets.CustomViewPager; + +import android.content.Context; +import android.os.Handler; +import android.os.Message; +import android.support.v4.view.MotionEventCompat; +import android.support.v4.view.PagerAdapter; +import android.support.v4.view.ViewPager; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.animation.Interpolator; + +import java.lang.reflect.Field; + +public class AutoScrollViewPager extends ViewPager { + + private static final int DEFAULT_INTERVAL = 4500; + + private static final int LEFT = 0; + private static final int RIGHT = 1; + + /** do nothing when sliding at the last or first item **/ + private static final int SLIDE_BORDER_MODE_NONE = 0; + /** cycle when sliding at the last or first item **/ + private static final int SLIDE_BORDER_MODE_CYCLE = 1; + /** deliver event to parent when sliding at the last or first item **/ + private static final int SLIDE_BORDER_MODE_TO_PARENT = 2; + + /** auto scroll time in milliseconds, default is {@link #DEFAULT_INTERVAL} **/ + private long interval = DEFAULT_INTERVAL; + /** auto scroll direction, default is {@link #RIGHT} **/ + private int direction = RIGHT; + /** whether automatic cycle when auto scroll reaching the last or first item, default is true **/ + private boolean isCycle = true; + /** whether stop auto scroll when touching, default is true **/ + private boolean stopScrollWhenTouch = true; + /** how to process when sliding at the last or first item, default is {@link #SLIDE_BORDER_MODE_NONE} **/ + private int slideBorderMode = SLIDE_BORDER_MODE_NONE; + /** whether animating when auto scroll at the last or first item **/ + private boolean isBorderAnimation = true; + /** scroll factor for auto scroll animation, default is 1.0 **/ + private double autoScrollFactor = 1.0; + /** scroll factor for swipe scroll animation, default is 1.0 **/ + private double swipeScrollFactor = 1.0; + + private Handler handler; + private boolean isAutoScroll = false; + private boolean isStopByTouch = false; + private float touchX = 0f; + private float downX = 0f; + private CustomDurationScroller scroller = null; + + private static final int SCROLL_WHAT = 0; + + public AutoScrollViewPager(Context paramContext) { + super(paramContext); + init(); + } + + public AutoScrollViewPager(Context paramContext, AttributeSet paramAttributeSet) { + super(paramContext, paramAttributeSet); + init(); + } + + private void init() { + handler = new MyHandler(); + setViewPagerScroller(); + } + + /** + * start auto scroll, first scroll delay time is {@link #getInterval()} + */ + public void startAutoScroll() { + isAutoScroll = true; + sendScrollMessage((long) (interval + scroller.getDuration()/ autoScrollFactor * swipeScrollFactor)); + } + + /** + * start auto scroll + * + * @param delayTimeInMills first scroll delay time + */ + public void startAutoScroll(int delayTimeInMills) { + isAutoScroll = true; + sendScrollMessage(delayTimeInMills); + } + + /** + * stop auto scroll + */ + private void stopAutoScroll() { + isAutoScroll = false; + handler.removeMessages(SCROLL_WHAT); + } + + /** + * set the factor by which the duration of sliding animation will change while swiping + */ + public void setSwipeScrollDurationFactor(double scrollFactor) { + swipeScrollFactor = scrollFactor; + } + + /** + * set the factor by which the duration of sliding animation will change while auto scrolling + */ + public void setAutoScrollDurationFactor(double scrollFactor) { + autoScrollFactor = scrollFactor; + } + + private void sendScrollMessage(long delayTimeInMills) { + handler.removeMessages(SCROLL_WHAT); + handler.sendEmptyMessageDelayed(SCROLL_WHAT, delayTimeInMills); + } + + private void setViewPagerScroller() { + try { + Field scrollerField = ViewPager.class.getDeclaredField("mScroller"); + scrollerField.setAccessible(true); + Field interpolatorField = ViewPager.class.getDeclaredField("sInterpolator"); + interpolatorField.setAccessible(true); + + scroller = new CustomDurationScroller(getContext(), (Interpolator)interpolatorField.get(null)); + scrollerField.set(this, scroller); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * scroll only once + */ + private void scrollOnce() { + PagerAdapter adapter = getAdapter(); + int currentItem = getCurrentItem(); + int totalCount; + if (adapter == null || (totalCount = adapter.getCount()) <= 1) { + return; + } + + int nextItem = (direction == LEFT) ? --currentItem : ++currentItem; + if (nextItem < 0) { + if (isCycle) { + setCurrentItem(totalCount - 1, isBorderAnimation); + } + } else if (nextItem == totalCount) { + if (isCycle) { + setCurrentItem(0, isBorderAnimation); + } + } else { + setCurrentItem(nextItem, true); + } + } + + /** + *
      + * if stopScrollWhenTouch is true + *
    • if event is down, stop auto scroll.
    • + *
    • if event is up, start auto scroll again.
    • + *
    + */ + @Override + public boolean dispatchTouchEvent(MotionEvent ev) { + int action = MotionEventCompat.getActionMasked(ev); + + if (stopScrollWhenTouch) { + if ((action == MotionEvent.ACTION_DOWN) && isAutoScroll) { + isStopByTouch = true; + stopAutoScroll(); + } else if (ev.getAction() == MotionEvent.ACTION_UP && isStopByTouch) { + startAutoScroll(); + } + } + + if (slideBorderMode == SLIDE_BORDER_MODE_TO_PARENT || slideBorderMode == SLIDE_BORDER_MODE_CYCLE) { + touchX = ev.getX(); + if (ev.getAction() == MotionEvent.ACTION_DOWN) { + downX = touchX; + } + int currentItem = getCurrentItem(); + PagerAdapter adapter = getAdapter(); + int pageCount = adapter == null ? 0 : adapter.getCount(); + if ((currentItem == 0 && downX <= touchX) || (currentItem == pageCount - 1 && downX >= touchX)) { + if (slideBorderMode == SLIDE_BORDER_MODE_TO_PARENT) { + getParent().requestDisallowInterceptTouchEvent(false); + } else { + if (pageCount > 1) { + setCurrentItem(pageCount - currentItem - 1, isBorderAnimation); + } + getParent().requestDisallowInterceptTouchEvent(true); + } + return super.dispatchTouchEvent(ev); + } + } + getParent().requestDisallowInterceptTouchEvent(true); + + return super.dispatchTouchEvent(ev); + } + + private class MyHandler extends Handler { + + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + + switch (msg.what) { + case SCROLL_WHAT: + scroller.setScrollDurationFactor(autoScrollFactor); + scrollOnce(); + scroller.setScrollDurationFactor(swipeScrollFactor); + sendScrollMessage(interval + scroller.getDuration()); + default: + break; + } + } + } + + /** + * get auto scroll time in milliseconds, default is {@link #DEFAULT_INTERVAL} + * + * @return the interval + */ + public long getInterval() { + return interval; + } + + /** + * set auto scroll time in milliseconds, default is {@link #DEFAULT_INTERVAL} + * + * @param interval the interval to set + */ + public void setInterval(long interval) { + this.interval = interval; + } + + /** + * get auto scroll direction + * + * @return {@link #LEFT} or {@link #RIGHT}, default is {@link #RIGHT} + */ + public int getDirection() { + return (direction == LEFT) ? LEFT : RIGHT; + } + + /** + * set auto scroll direction + * + * @param direction {@link #LEFT} or {@link #RIGHT}, default is {@link #RIGHT} + */ + public void setDirection(int direction) { + this.direction = direction; + } + + /** + * whether automatic cycle when auto scroll reaching the last or first item, default is true + * + * @return the isCycle + */ + public boolean isCycle() { + return isCycle; + } + + /** + * set whether automatic cycle when auto scroll reaching the last or first item, default is true + * + * @param isCycle the isCycle to set + */ + public void setCycle(boolean isCycle) { + this.isCycle = isCycle; + } + + /** + * whether stop auto scroll when touching, default is true + * + * @return the stopScrollWhenTouch + */ + public boolean isStopScrollWhenTouch() { + return stopScrollWhenTouch; + } + + public void setStopScrollWhenTouch(boolean stopScrollWhenTouch) { + this.stopScrollWhenTouch = stopScrollWhenTouch; + } + + /** + * get how to process when sliding at the last or first item + * + * @return the slideBorderMode {@link #SLIDE_BORDER_MODE_NONE}, {@link #SLIDE_BORDER_MODE_TO_PARENT}, + * {@link #SLIDE_BORDER_MODE_CYCLE}, default is {@link #SLIDE_BORDER_MODE_NONE} + */ + public int getSlideBorderMode() { + return slideBorderMode; + } + + /** + * set how to process when sliding at the last or first item + * + * @param slideBorderMode {@link #SLIDE_BORDER_MODE_NONE}, {@link #SLIDE_BORDER_MODE_TO_PARENT}, + * {@link #SLIDE_BORDER_MODE_CYCLE}, default is {@link #SLIDE_BORDER_MODE_NONE} + */ + public void setSlideBorderMode(int slideBorderMode) { + this.slideBorderMode = slideBorderMode; + } + + public boolean isBorderAnimation() { + return isBorderAnimation; + } + + public void setBorderAnimation(boolean isBorderAnimation) { + this.isBorderAnimation = isBorderAnimation; + } +} \ No newline at end of file diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/CustomViewPager/CustomDurationScroller.java b/widgets/src/main/java/mohammadaminha/com/widgets/CustomViewPager/CustomDurationScroller.java new file mode 100644 index 0000000..ab1e141 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/CustomViewPager/CustomDurationScroller.java @@ -0,0 +1,32 @@ +package mohammadaminha.com.widgets.CustomViewPager; + + +import android.content.Context; +import android.view.animation.Interpolator; +import android.widget.Scroller; + +class CustomDurationScroller extends Scroller { + private double scrollFactor = 1; + + public CustomDurationScroller(Context context) { + super(context); + } + + public CustomDurationScroller(Context context, Interpolator interpolator) { + super(context, interpolator); + } + + // @SuppressLint("NewApi") + // public CustomDurationScroller(Context context, Interpolator interpolator, boolean flywheel){ + // super(context, interpolator, flywheel); + // } + + public void setScrollDurationFactor(double scrollFactor) { + this.scrollFactor = scrollFactor; + } + + @Override + public void startScroll(int startX, int startY, int dx, int dy, int duration) { + super.startScroll(startX, startY, dx, dy, (int)(duration * scrollFactor)); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/AccessibleLinearLayout.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/AccessibleLinearLayout.java new file mode 100644 index 0000000..4839a9c --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/AccessibleLinearLayout.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityNodeInfo; +import android.widget.Button; +import android.widget.LinearLayout; + +/** + * Fake Button class, used so TextViews can announce themselves as Buttons, for accessibility. + */ +public class AccessibleLinearLayout extends LinearLayout { + + public AccessibleLinearLayout(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + public void onInitializeAccessibilityEvent(AccessibilityEvent event) { + super.onInitializeAccessibilityEvent(event); + event.setClassName(Button.class.getName()); + } + + @Override + public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { + super.onInitializeAccessibilityNodeInfo(info); + info.setClassName(Button.class.getName()); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/AccessibleTextView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/AccessibleTextView.java new file mode 100644 index 0000000..8229860 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/AccessibleTextView.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityNodeInfo; +import android.widget.Button; + +import mohammadaminha.com.widgets.TextView; +import mohammadaminha.com.widgets.Util; + +/** + * Fake Button class, used so TextViews can announce themselves as Buttons, for accessibility. + */ +public class AccessibleTextView extends TextView { + + public AccessibleTextView(Context context, AttributeSet attrs) { + super(context, attrs); + setTypeface(context); + } + + @Override + public void onInitializeAccessibilityEvent(AccessibilityEvent event) { + super.onInitializeAccessibilityEvent(event); + event.setClassName(Button.class.getName()); + } + + @Override + public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { + super.onInitializeAccessibilityNodeInfo(info); + info.setClassName(Button.class.getName()); + } + + + private void setTypeface(Context context) { + + setTypeface(Util.getTypeFace()); + } + + + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/HapticFeedbackController.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/HapticFeedbackController.java new file mode 100644 index 0000000..0ddecb2 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/HapticFeedbackController.java @@ -0,0 +1,74 @@ +package mohammadaminha.com.widgets.Date_Picker; + +import android.app.Service; +import android.content.Context; +import android.database.ContentObserver; +import android.net.Uri; +import android.os.SystemClock; +import android.os.Vibrator; +import android.provider.Settings; + +/** + * A simple utility class to handle haptic feedback. + */ +public class HapticFeedbackController { + private static final int VIBRATE_DELAY_MS = 125; + private static final int VIBRATE_LENGTH_MS = 5; + + private static boolean checkGlobalSetting(Context context) { + return Settings.System.getInt(context.getContentResolver(), + Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 1; + } + + private final Context mContext; + private final ContentObserver mContentObserver; + + private Vibrator mVibrator; + private boolean mIsGloballyEnabled; + private long mLastVibrate; + + public HapticFeedbackController(Context context) { + mContext = context; + mContentObserver = new ContentObserver(null) { + @Override + public void onChange(boolean selfChange) { + mIsGloballyEnabled = checkGlobalSetting(mContext); + } + }; + } + + /** + * Call to setup the controller. + */ + public void start() { + mVibrator = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE); + + // Setup a listener for changes in haptic feedback settings + mIsGloballyEnabled = checkGlobalSetting(mContext); + Uri uri = Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED); + mContext.getContentResolver().registerContentObserver(uri, false, mContentObserver); + } + + /** + * Call this when you don't need the controller anymore. + */ + public void stop() { + mVibrator = null; + mContext.getContentResolver().unregisterContentObserver(mContentObserver); + } + + /** + * Try to vibrate. To prevent this becoming a single continuous vibration, nothing will + * happen if we have vibrated very recently. + */ + public void tryVibrate() { + if (mVibrator != null && mIsGloballyEnabled) { + long now = SystemClock.uptimeMillis(); + // We want to try to vibrate each individual tick discretely. + if (now - mLastVibrate >= VIBRATE_DELAY_MS) { + mVibrator.vibrate(VIBRATE_LENGTH_MS); + mLastVibrate = now; + } + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/TypefaceHelper.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/TypefaceHelper.java new file mode 100644 index 0000000..f3581d5 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/TypefaceHelper.java @@ -0,0 +1,39 @@ +package mohammadaminha.com.widgets.Date_Picker; + + +import android.content.Context; +import android.graphics.Typeface; +import android.support.v4.util.SimpleArrayMap; + +/* + Each call to Typeface.createFromAsset will load a new instance of the typeface into memory, + and this memory is not consistently get garbage collected + http://code.google.com/p/android/issues/detail?id=9904 + (It states released but even on Lollipop you can see the typefaces accumulate even after + multiple GC passes) + You can detect this by running: + adb shell dumpsys meminfo com.your.packagenage + You will see output like: + Asset Allocations + zip:/data/app/com.your.packagenage-1.apk:/assets/Roboto-Medium.ttf: 125K + zip:/data/app/com.your.packagenage-1.apk:/assets/Roboto-Medium.ttf: 125K + zip:/data/app/com.your.packagenage-1.apk:/assets/Roboto-Medium.ttf: 125K + zip:/data/app/com.your.packagenage-1.apk:/assets/Roboto-Regular.ttf: 123K + zip:/data/app/com.your.packagenage-1.apk:/assets/Roboto-Medium.ttf: 125K +*/ +public class TypefaceHelper { + + private static final SimpleArrayMap cache = new SimpleArrayMap<>(); + + public static Typeface get(Context c, String name) { + synchronized (cache) { + if (!cache.containsKey(name)) { + Typeface t = Typeface.createFromAsset( + c.getAssets(), String.format("font/BYekan.ttf", name)); + cache.put(name, t); + return t; + } + return cache.get(name); + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/Utils.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/Utils.java new file mode 100644 index 0000000..8af0bb3 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/Utils.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker; + +import android.animation.Keyframe; +import android.animation.ObjectAnimator; +import android.animation.PropertyValuesHolder; +import android.annotation.SuppressLint; +import android.content.res.Resources; +import android.os.Build; +import android.util.TypedValue; +import android.view.View; + +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendarUtils; + +/** + * Utility helper functions for time and date pickers. + */ +public class Utils { + + //public static final int MONDAY_BEFORE_JULIAN_EPOCH = Time.EPOCH_JULIAN_DAY - 3; + private static final int PULSE_ANIMATOR_DURATION = 544; + + // Alpha level for time picker selection. + public static final int SELECTED_ALPHA = 255; + public static final int SELECTED_ALPHA_THEME_DARK = 255; + // Alpha level for fully opaque. + public static final int FULL_ALPHA = 255; + + private static boolean isJellybeanOrLater() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; + } + + /** + * Try to speak the specified text, for accessibility. Only available on JB or later. + * @param text Text to announce. + */ + @SuppressLint("NewApi") + public static void tryAccessibilityAnnounce(View view, CharSequence text) { + if (isJellybeanOrLater() && view != null && text != null) { + view.announceForAccessibility(text); + } + } + + public static int getDaysInMonth(int month, int year) { + if (month < 6) { + return 31; + } else if (month < 11) { + return 30; + } else { + if (PersianCalendarUtils.isPersianLeapYear(year)) return 30; + else return 29; + } + } + + /** + * Render an animator to pulsate a view in place. + * @param labelToAnimate the view to pulsate. + * @return The animator object. Use .start() to begin. + */ + public static ObjectAnimator getPulseAnimator(View labelToAnimate, float decreaseRatio, + float increaseRatio) { + Keyframe k0 = Keyframe.ofFloat(0f, 1f); + Keyframe k1 = Keyframe.ofFloat(0.275f, decreaseRatio); + Keyframe k2 = Keyframe.ofFloat(0.69f, increaseRatio); + Keyframe k3 = Keyframe.ofFloat(1f, 1f); + + PropertyValuesHolder scaleX = PropertyValuesHolder.ofKeyframe("scaleX", k0, k1, k2, k3); + PropertyValuesHolder scaleY = PropertyValuesHolder.ofKeyframe("scaleY", k0, k1, k2, k3); + ObjectAnimator pulseAnimator = + ObjectAnimator.ofPropertyValuesHolder(labelToAnimate, scaleX, scaleY); + pulseAnimator.setDuration(PULSE_ANIMATOR_DURATION); + + return pulseAnimator; + } + + /** + * Convert Dp to Pixel + */ + @SuppressWarnings("unused") + public static int dpToPx(float dp, Resources resources){ + float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics()); + return (int) px; + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/AccessibleDateAnimator.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/AccessibleDateAnimator.java new file mode 100644 index 0000000..1dfe83d --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/AccessibleDateAnimator.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.accessibility.AccessibilityEvent; +import android.widget.ViewAnimator; + +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; + +public class AccessibleDateAnimator extends ViewAnimator { + private long mDateMillis; + + public AccessibleDateAnimator(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public void setDateMillis(long dateMillis) { + mDateMillis = dateMillis; + } + + /** + * Announce the currently-selected date when launched. + */ + @Override + public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { + if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { + // Clear the event's current text so that only the current date will be spoken. + event.getText().clear(); + PersianCalendar mPersianCalendar = new PersianCalendar(); + mPersianCalendar.setTimeInMillis(mDateMillis); + String dateString = LanguageUtils.getPersianNumbers( + mPersianCalendar.getPersianMonthName() + " " + + mPersianCalendar.getPersianYear() + ); + event.getText().add(dateString); + return true; + } + return super.dispatchPopulateAccessibilityEvent(event); + } +} \ No newline at end of file diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/DatePickerController.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/DatePickerController.java new file mode 100644 index 0000000..91960b8 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/DatePickerController.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; + +/** + * Controller class to communicate among the various components of the date picker dialog. + */ +public interface DatePickerController { + + void onYearSelected(int year); + + void onDayOfMonthSelected(int year, int month, int day); + + void registerOnDateChangedListener(DatePickerDialog.OnDateChangedListener listener); + + void unregisterOnDateChangedListener(DatePickerDialog.OnDateChangedListener listener); + + MonthAdapter.CalendarDay getSelectedDay(); + + boolean isThemeDark(); + + PersianCalendar[] getHighlightedDays(); + + PersianCalendar[] getSelectableDays(); + + int getFirstDayOfWeek(); + + int getMinYear(); + + int getMaxYear(); + + PersianCalendar getMinDate(); + + PersianCalendar getMaxDate(); + + void tryVibrate(); +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/DatePickerDialog.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/DatePickerDialog.java new file mode 100644 index 0000000..a2a0faf --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/DatePickerDialog.java @@ -0,0 +1,635 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.animation.ObjectAnimator; +import android.app.Activity; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.content.res.Resources; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.view.animation.AlphaAnimation; +import android.view.animation.Animation; +import android.widget.LinearLayout; + +import java.util.Arrays; +import java.util.Calendar; +import java.util.HashSet; + +import mohammadaminha.com.widgets.Button; +import mohammadaminha.com.widgets.Date_Picker.AccessibleTextView; +import mohammadaminha.com.widgets.Date_Picker.HapticFeedbackController; +import mohammadaminha.com.widgets.Date_Picker.TypefaceHelper; +import mohammadaminha.com.widgets.Date_Picker.Utils; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; +import mohammadaminha.com.widgets.R; +import mohammadaminha.com.widgets.TextView; + +/** + * Dialog allowing users to select a date. + */ +public class DatePickerDialog extends DialogFragment implements + OnClickListener, DatePickerController { + + private static final String TAG = "DatePickerDialog"; + + private static final int UNINITIALIZED = -1; + private static final int MONTH_AND_DAY_VIEW = 0; + private static final int YEAR_VIEW = 1; + + private static final String KEY_SELECTED_YEAR = "year"; + private static final String KEY_SELECTED_MONTH = "month"; + private static final String KEY_SELECTED_DAY = "day"; + private static final String KEY_LIST_POSITION = "list_position"; + private static final String KEY_WEEK_START = "week_start"; + private static final String KEY_YEAR_START = "year_start"; + private static final String KEY_YEAR_END = "year_end"; + private static final String KEY_CURRENT_VIEW = "current_view"; + private static final String KEY_LIST_POSITION_OFFSET = "list_position_offset"; + private static final String KEY_MIN_DATE = "min_date"; + private static final String KEY_MAX_DATE = "max_date"; + private static final String KEY_HIGHLIGHTED_DAYS = "highlighted_days"; + private static final String KEY_SELECTABLE_DAYS = "selectable_days"; + private static final String KEY_THEME_DARK = "theme_dark"; + + private static final int DEFAULT_START_YEAR = 1250; + private static final int DEFAULT_END_YEAR = 1550; + + private static final int ANIMATION_DURATION = 300; + private static final int ANIMATION_DELAY = 500; + + private PersianCalendar mPersianCalendar = new PersianCalendar(); + private OnDateSetListener mCallBack; + private HashSet mListeners = new HashSet<>(); + private DialogInterface.OnCancelListener mOnCancelListener; + private DialogInterface.OnDismissListener mOnDismissListener; + + private AccessibleDateAnimator mAnimator; + + private TextView mDayOfWeekView; + private LinearLayout mMonthAndDayView; + private TextView mSelectedMonthTextView, mSelectedDayTextView; + private AccessibleTextView mYearView; + private DayPickerView mDayPickerView; + private YearPickerView mYearPickerView; + + private int mCurrentView = UNINITIALIZED; + + private int mWeekStart = PersianCalendar.SATURDAY; + private int mMinYear = DEFAULT_START_YEAR; + private int mMaxYear = DEFAULT_END_YEAR; + private PersianCalendar mMinDate; + private PersianCalendar mMaxDate; + private PersianCalendar[] highlightedDays; + private PersianCalendar[] selectableDays; + private boolean mThemeDark; + + private HapticFeedbackController mHapticFeedbackController; + + private boolean mDelayAnimation = true; + + // Accessibility strings. + private String mDayPickerDescription; + private String mSelectDay; + private String mYearPickerDescription; + private String mSelectYear; + + /** + * The callback used to indicate the user is done filling in the date. + */ + public interface OnDateSetListener { + + /** + * @param view The view associated with this listener. + * @param year The year that was set. + * @param monthOfYear The month that was set (0-11) for compatibility + * with {@link Calendar}. + * @param dayOfMonth The day of the month that was set. + */ + void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth); + } + + /** + * The callback used to notify other date picker components of a change in selected date. + */ + public interface OnDateChangedListener { + + void onDateChanged(); + } + + + public DatePickerDialog() { + // Empty constructor required for dialog fragment. + } + + /** + * @param callBack How the parent is notified that the date is set. + * @param year The initial year of the dialog. + * @param monthOfYear The initial month of the dialog. + * @param dayOfMonth The initial day of the dialog. + */ + public static DatePickerDialog newInstance(OnDateSetListener callBack, int year, + int monthOfYear, + int dayOfMonth) { + DatePickerDialog ret = new DatePickerDialog(); + ret.initialize(callBack, year, monthOfYear, dayOfMonth); + return ret; + } + + private void initialize(OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { + mCallBack = callBack; + mPersianCalendar.setPersianDate(year, monthOfYear, dayOfMonth); + mThemeDark = false; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + final Activity activity = getActivity(); + activity.getWindow().setSoftInputMode( + WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); + if (savedInstanceState != null) { + mPersianCalendar.setPersianDate( + savedInstanceState.getInt(KEY_SELECTED_YEAR), + savedInstanceState.getInt(KEY_SELECTED_MONTH), + savedInstanceState.getInt(KEY_SELECTED_DAY) + ); + } + } + + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putInt(KEY_SELECTED_YEAR, mPersianCalendar.getPersianYear()); + outState.putInt(KEY_SELECTED_MONTH, mPersianCalendar.getPersianMonth()); + outState.putInt(KEY_SELECTED_DAY, mPersianCalendar.getPersianDay()); + outState.putInt(KEY_WEEK_START, mWeekStart); + outState.putInt(KEY_YEAR_START, mMinYear); + outState.putInt(KEY_YEAR_END, mMaxYear); + outState.putInt(KEY_CURRENT_VIEW, mCurrentView); + int listPosition = -1; + if (mCurrentView == MONTH_AND_DAY_VIEW) { + listPosition = mDayPickerView.getMostVisiblePosition(); + } else if (mCurrentView == YEAR_VIEW) { + listPosition = mYearPickerView.getFirstVisiblePosition(); + outState.putInt(KEY_LIST_POSITION_OFFSET, mYearPickerView.getFirstPositionOffset()); + } + outState.putInt(KEY_LIST_POSITION, listPosition); + outState.putSerializable(KEY_MIN_DATE, mMinDate); + outState.putSerializable(KEY_MAX_DATE, mMaxDate); + outState.putSerializable(KEY_HIGHLIGHTED_DAYS, highlightedDays); + outState.putSerializable(KEY_SELECTABLE_DAYS, selectableDays); + outState.putBoolean(KEY_THEME_DARK, mThemeDark); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + Log.d(TAG, "onCreateView: "); + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + + View view = inflater.inflate(R.layout.mdtp_date_picker_dialog, null); + + mDayOfWeekView = view.findViewById(R.id.date_picker_header); + mMonthAndDayView = view.findViewById(R.id.date_picker_month_and_day); + mMonthAndDayView.setOnClickListener(this); + mSelectedMonthTextView = view.findViewById(R.id.date_picker_month); + mSelectedDayTextView = view.findViewById(R.id.date_picker_day); + mYearView = view.findViewById(R.id.date_picker_year); + mYearView.setOnClickListener(this); + + int listPosition = -1; + int listPositionOffset = 0; + int currentView = MONTH_AND_DAY_VIEW; + if (savedInstanceState != null) { + mWeekStart = savedInstanceState.getInt(KEY_WEEK_START); + mMinYear = savedInstanceState.getInt(KEY_YEAR_START); + mMaxYear = savedInstanceState.getInt(KEY_YEAR_END); + currentView = savedInstanceState.getInt(KEY_CURRENT_VIEW); + listPosition = savedInstanceState.getInt(KEY_LIST_POSITION); + listPositionOffset = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET); + mMinDate = (PersianCalendar) savedInstanceState.getSerializable(KEY_MIN_DATE); + mMaxDate = (PersianCalendar) savedInstanceState.getSerializable(KEY_MAX_DATE); + highlightedDays = (PersianCalendar[]) savedInstanceState.getSerializable(KEY_HIGHLIGHTED_DAYS); + selectableDays = (PersianCalendar[]) savedInstanceState.getSerializable(KEY_SELECTABLE_DAYS); + mThemeDark = savedInstanceState.getBoolean(KEY_THEME_DARK); + } + + final Activity activity = getActivity(); + mDayPickerView = new SimpleDayPickerView(activity, this); + mYearPickerView = new YearPickerView(activity, this); + + Resources res = getResources(); + mDayPickerDescription = res.getString(R.string.mdtp_day_picker_description); + mSelectDay = res.getString(R.string.mdtp_select_day); + mYearPickerDescription = res.getString(R.string.mdtp_year_picker_description); + mSelectYear = res.getString(R.string.mdtp_select_year); + + int bgColorResource = mThemeDark ? R.color.mdtp_date_picker_view_animator_dark_theme : R.color.mdtp_date_picker_view_animator; + view.setBackgroundColor(activity.getResources().getColor(bgColorResource)); + + mAnimator = view.findViewById(R.id.animator); + mAnimator.addView(mDayPickerView); + mAnimator.addView(mYearPickerView); + mAnimator.setDateMillis(mPersianCalendar.getTimeInMillis()); + // TODO: Replace with animation decided upon by the design team. + Animation animation = new AlphaAnimation(0.0f, 1.0f); + animation.setDuration(ANIMATION_DURATION); + mAnimator.setInAnimation(animation); + // TODO: Replace with animation decided upon by the design team. + Animation animation2 = new AlphaAnimation(1.0f, 0.0f); + animation2.setDuration(ANIMATION_DURATION); + mAnimator.setOutAnimation(animation2); + + Button okButton = view.findViewById(R.id.ok); + okButton.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + tryVibrate(); + if (mCallBack != null) { + mCallBack.onDateSet(DatePickerDialog.this, mPersianCalendar.getPersianYear(), + mPersianCalendar.getPersianMonth(), mPersianCalendar.getPersianDay()); + } + dismiss(); + } + }); + okButton.setTypeface(TypefaceHelper.get(activity, "Roboto-Medium")); + + Button cancelButton = view.findViewById(R.id.cancel); + cancelButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + tryVibrate(); + getDialog().cancel(); + } + }); + cancelButton.setTypeface(TypefaceHelper.get(activity, "Roboto-Medium")); + cancelButton.setVisibility(isCancelable() ? View.VISIBLE : View.GONE); + + updateDisplay(false); + setCurrentView(currentView); + + if (listPosition != -1) { + if (currentView == MONTH_AND_DAY_VIEW) { + mDayPickerView.postSetSelection(listPosition); + } else if (currentView == YEAR_VIEW) { + mYearPickerView.postSetSelectionFromTop(listPosition, listPositionOffset); + } + } + + mHapticFeedbackController = new HapticFeedbackController(activity); + return view; + } + + @Override + public void onResume() { + super.onResume(); + mHapticFeedbackController.start(); + } + + @Override + public void onPause() { + super.onPause(); + mHapticFeedbackController.stop(); + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + if (mOnCancelListener != null) mOnCancelListener.onCancel(dialog); + } + + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + if (mOnDismissListener != null) mOnDismissListener.onDismiss(dialog); + } + + private void setCurrentView(final int viewIndex) { + + switch (viewIndex) { + case MONTH_AND_DAY_VIEW: + ObjectAnimator pulseAnimator = Utils.getPulseAnimator(mMonthAndDayView, 0.9f, + 1.05f); + if (mDelayAnimation) { + pulseAnimator.setStartDelay(ANIMATION_DELAY); + mDelayAnimation = false; + } + mDayPickerView.onDateChanged(); + if (mCurrentView != viewIndex) { + mMonthAndDayView.setSelected(true); + mYearView.setSelected(false); + mAnimator.setDisplayedChild(MONTH_AND_DAY_VIEW); + mCurrentView = viewIndex; + } + pulseAnimator.start(); + + String dayString = LanguageUtils.getPersianNumbers(mPersianCalendar.getPersianLongDate()); + mAnimator.setContentDescription(mDayPickerDescription + ": " + dayString); + Utils.tryAccessibilityAnnounce(mAnimator, mSelectDay); + break; + case YEAR_VIEW: + pulseAnimator = Utils.getPulseAnimator(mYearView, 0.85f, 1.1f); + if (mDelayAnimation) { + pulseAnimator.setStartDelay(ANIMATION_DELAY); + mDelayAnimation = false; + } + mYearPickerView.onDateChanged(); + if (mCurrentView != viewIndex) { + mMonthAndDayView.setSelected(false); + mYearView.setSelected(true); + mAnimator.setDisplayedChild(YEAR_VIEW); + mCurrentView = viewIndex; + } + pulseAnimator.start(); + + String yearString = LanguageUtils. + getPersianNumbers(String.valueOf(mPersianCalendar.getPersianYear())); + mAnimator.setContentDescription(mYearPickerDescription + ": " + yearString); + Utils.tryAccessibilityAnnounce(mAnimator, mSelectYear); + break; + } + } + + private void updateDisplay(boolean announce) { + if (mDayOfWeekView != null) { + mDayOfWeekView.setText(mPersianCalendar.getPersianWeekDayName()); + } + + mSelectedMonthTextView.setText(LanguageUtils. + getPersianNumbers(mPersianCalendar.getPersianMonthName())); + mSelectedDayTextView.setText(LanguageUtils. + getPersianNumbers(String.valueOf(mPersianCalendar.getPersianDay()))); + mYearView.setText(LanguageUtils. + getPersianNumbers(String.valueOf(mPersianCalendar.getPersianYear()))); + + // Accessibility. + long millis = mPersianCalendar.getTimeInMillis(); + mAnimator.setDateMillis(millis); + String monthAndDayText = LanguageUtils.getPersianNumbers( + mPersianCalendar.getPersianMonthName() + " " + + mPersianCalendar.getPersianDay() + ); + mMonthAndDayView.setContentDescription(monthAndDayText); + + if (announce) { + String fullDateText = LanguageUtils. + getPersianNumbers(mPersianCalendar.getPersianLongDate()); + Utils.tryAccessibilityAnnounce(mAnimator, fullDateText); + } + } + + /** + * Set whether the dark theme should be used + * + * @param themeDark true if the dark theme should be used, false if the default theme should be used + */ + public void setThemeDark(boolean themeDark) { + mThemeDark = themeDark; + } + + /** + * Returns true when the dark theme should be used + * + * @return true if the dark theme should be used, false if the default theme should be used + */ + @Override + public boolean isThemeDark() { + return mThemeDark; + } + + @SuppressWarnings("unused") + public void setFirstDayOfWeek(int startOfWeek) { + if (startOfWeek < Calendar.SUNDAY || startOfWeek > Calendar.SATURDAY) { + throw new IllegalArgumentException("Value must be between Calendar.SUNDAY and " + + "Calendar.SATURDAY"); + } + mWeekStart = startOfWeek; + if (mDayPickerView != null) { + mDayPickerView.onChange(); + } + } + + @SuppressWarnings("unused") + public void setYearRange(int startYear, int endYear) { + if (endYear < startYear) { + throw new IllegalArgumentException("Year end must be larger than or equal to year start"); + } + + mMinYear = startYear; + mMaxYear = endYear; + if (mDayPickerView != null) { + mDayPickerView.onChange(); + } + } + + /** + * Sets the minimal date supported by this DatePicker. Dates before (but not including) the + * specified date will be disallowed from being selected. + * + * @param calendar a Calendar object set to the year, month, day desired as the mindate. + */ + @SuppressWarnings("unused") + public void setMinDate(PersianCalendar calendar) { + mMinDate = calendar; + + if (mDayPickerView != null) { + mDayPickerView.onChange(); + } + } + + /** + * @return The minimal date supported by this DatePicker. Null if it has not been set. + */ + @Override + public PersianCalendar getMinDate() { + return mMinDate; + } + + /** + * Sets the minimal date supported by this DatePicker. Dates after (but not including) the + * specified date will be disallowed from being selected. + * + * @param calendar a Calendar object set to the year, month, day desired as the maxdate. + */ + @SuppressWarnings("unused") + public void setMaxDate(PersianCalendar calendar) { + mMaxDate = calendar; + + if (mDayPickerView != null) { + mDayPickerView.onChange(); + } + } + + /** + * @return The maximal date supported by this DatePicker. Null if it has not been set. + */ + @Override + public PersianCalendar getMaxDate() { + return mMaxDate; + } + + /** + * Sets an array of dates which should be highlighted when the picker is drawn + * + * @param highlightedDays an Array of Calendar objects containing the dates to be highlighted + */ + @SuppressWarnings("unused") + public void setHighlightedDays(PersianCalendar[] highlightedDays) { + // Sort the array to optimize searching over it later on + Arrays.sort(highlightedDays); + this.highlightedDays = highlightedDays; + } + + /** + * @return The list of dates, as Calendar Objects, which should be highlighted. null is no dates should be highlighted + */ + @Override + public PersianCalendar[] getHighlightedDays() { + return highlightedDays; + } + + /** + * Set's a list of days which are the only valid selections. + * Setting this value will take precedence over using setMinDate() and setMaxDate() + * + * @param selectableDays an Array of Calendar Objects containing the selectable dates + */ + @SuppressWarnings("unused") + public void setSelectableDays(PersianCalendar[] selectableDays) { + // Sort the array to optimize searching over it later on + Arrays.sort(selectableDays); + this.selectableDays = selectableDays; + } + + /** + * @return an Array of Calendar objects containing the list with selectable items. null if no restriction is set + */ + @Override + public PersianCalendar[] getSelectableDays() { + return selectableDays; + } + + @SuppressWarnings("unused") + public void setOnDateSetListener(OnDateSetListener listener) { + mCallBack = listener; + } + + @SuppressWarnings("unused") + public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) { + mOnCancelListener = onCancelListener; + } + + @SuppressWarnings("unused") + public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) { + mOnDismissListener = onDismissListener; + } + + // If the newly selected month / year does not contain the currently selected day number, + // change the selected day number to the last day of the selected month or year. + // e.g. Switching from Mar to Apr when Mar 31 is selected -> Apr 30 + // e.g. Switching from 2012 to 2013 when Feb 29, 2012 is selected -> Feb 28, 2013 + private void adjustDayInMonthIfNeeded(int month, int year) { +// int day = mPersianCalendar.getPersianDay(); +// int daysInMonth = Utils.getDaysInMonth(month, year); +// if (day > daysInMonth) { +// mPersianCalendar.setPersianDate(Persian); +// } TODO + } + + @Override + public void onClick(View v) { + tryVibrate(); + if (v.getId() == R.id.date_picker_year) { + setCurrentView(YEAR_VIEW); + } else if (v.getId() == R.id.date_picker_month_and_day) { + setCurrentView(MONTH_AND_DAY_VIEW); + } + } + + @Override + public void onYearSelected(int year) { + adjustDayInMonthIfNeeded(mPersianCalendar.getPersianMonth(), year); + mPersianCalendar.setPersianDate(year, mPersianCalendar.getPersianMonth(), + mPersianCalendar.getPersianDay()); + updatePickers(); + setCurrentView(MONTH_AND_DAY_VIEW); + updateDisplay(true); + } + + @Override + public void onDayOfMonthSelected(int year, int month, int day) { + mPersianCalendar.setPersianDate(year, month, day); + updatePickers(); + updateDisplay(true); + } + + private void updatePickers() { + for (OnDateChangedListener listener : mListeners) listener.onDateChanged(); + } + + + @Override + public MonthAdapter.CalendarDay getSelectedDay() { + return new MonthAdapter.CalendarDay(mPersianCalendar); + } + + @Override + public int getMinYear() { + if (selectableDays != null) return selectableDays[0].getPersianYear(); + // Ensure no years can be selected outside of the given minimum date + return mMinDate != null && mMinDate.getPersianYear() > mMinYear ? mMinDate.getPersianYear() : mMinYear; + } + + @Override + public int getMaxYear() { + if (selectableDays != null) + return selectableDays[selectableDays.length - 1].getPersianYear(); + // Ensure no years can be selected outside of the given maximum date + return mMaxDate != null && mMaxDate.getPersianYear() < mMaxYear ? mMaxDate.getPersianYear() : mMaxYear; + } + + @Override + public int getFirstDayOfWeek() { + return mWeekStart; + } + + @Override + public void registerOnDateChangedListener(OnDateChangedListener listener) { + mListeners.add(listener); + } + + @Override + public void unregisterOnDateChangedListener(OnDateChangedListener listener) { + mListeners.remove(listener); + } + + @Override + public void tryVibrate() { + mHapticFeedbackController.tryVibrate(); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/DayPickerView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/DayPickerView.java new file mode 100644 index 0000000..9809175 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/DayPickerView.java @@ -0,0 +1,508 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.support.annotation.NonNull; +import android.util.AttributeSet; +import android.util.Log; +import android.view.View; +import android.view.ViewConfiguration; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityNodeInfo; +import android.widget.AbsListView; +import android.widget.AbsListView.OnScrollListener; +import android.widget.ListView; + +import mohammadaminha.com.widgets.Date_Picker.Utils; +import mohammadaminha.com.widgets.Date_Picker.date.DatePickerDialog.OnDateChangedListener; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; + +/** + * This displays a list of months in a calendar format with selectable days. + */ +public abstract class DayPickerView extends ListView implements OnScrollListener, + OnDateChangedListener { + + private static final String TAG = "MonthFragment"; + + // Affects when the month selection will change while scrolling up + protected static final int SCROLL_HYST_WEEKS = 2; + // How long the GoTo fling animation should last + private static final int GOTO_SCROLL_DURATION = 250; + // How long to wait after receiving an onScrollStateChanged notification + // before acting on it + private static final int SCROLL_CHANGE_DELAY = 40; + // The number of days to display in each week + public static final int DAYS_PER_WEEK = 7; + private static final int LIST_TOP_OFFSET = -1; // so that the top line will be + // under the separator + // You can override these numbers to get a different appearance + protected int mNumWeeks = 6; + protected boolean mShowWeekNumber = false; + protected int mDaysPerWeek = 7; +// + // These affect the scroll speed and feel +private float mFriction = 1.0f; + + private Context mContext; + private Handler mHandler; + + // highlighted time + private MonthAdapter.CalendarDay mSelectedDay = new MonthAdapter.CalendarDay(); + private MonthAdapter mAdapter; + + private MonthAdapter.CalendarDay mTempDay = new MonthAdapter.CalendarDay(); + + // When the week starts; numbered like Time. (e.g. SUNDAY=0). + protected int mFirstDayOfWeek; + // The last name announced by accessibility + protected CharSequence mPrevMonthName; + // which month should be displayed/highlighted [0-11] + private int mCurrentMonthDisplayed; + // used for tracking during a scroll + private long mPreviousScrollPosition; + // used for tracking what state listview is in + private int mPreviousScrollState = OnScrollListener.SCROLL_STATE_IDLE; + // used for tracking what state listview is in + private int mCurrentScrollState = OnScrollListener.SCROLL_STATE_IDLE; + + private DatePickerController mController; + private boolean mPerformingScroll; + + public DayPickerView(Context context, AttributeSet attrs) { + super(context, attrs); + init(context); + } + + DayPickerView(Context context, DatePickerController controller) { + super(context); + init(context); + setController(controller); + } + + private void setController(DatePickerController controller) { + mController = controller; + mController.registerOnDateChangedListener(this); + refreshAdapter(); + onDateChanged(); + } + + private void init(Context context) { + mHandler = new Handler(); + setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + setDrawSelectorOnTop(false); + + mContext = context; + setUpListView(); + } + + public void onChange() { + refreshAdapter(); + } + + /** + * Creates a new adapter if necessary and sets up its parameters. Override + * this method to provide a custom adapter. + */ + private void refreshAdapter() { + if (mAdapter == null) { + mAdapter = createMonthAdapter(getContext(), mController); + } else { + mAdapter.setSelectedDay(mSelectedDay); + } + // refresh the view with the new parameters + setAdapter(mAdapter); + } + + protected abstract MonthAdapter createMonthAdapter(Context context, + DatePickerController controller); + + /* + * Sets all the required fields for the list view. Override this method to + * set a different list view behavior. + */ + private void setUpListView() { + // Transparent background on scroll + setCacheColorHint(0); + // No dividers + setDivider(null); + // Items are clickable + setItemsCanFocus(true); + // The thumb gets in the way, so disable it + setFastScrollEnabled(false); + setVerticalScrollBarEnabled(false); + setOnScrollListener(this); + setFadingEdgeLength(0); + // Make the scrolling behavior nicer + setFriction(ViewConfiguration.getScrollFriction() * mFriction); + } + + /** + * This moves to the specified time in the view. If the time is not already + * in range it will move the list so that the first of the month containing + * the time is at the top of the view. If the new time is already in view + * the list will not be scrolled unless forceScroll is true. This time may + * optionally be highlighted as selected as well. + * + * @param day The day to move to + * @param animate Whether to scroll to the given time or just redraw at the + * new location + * @param setSelected Whether to set the given time as selected + * @param forceScroll Whether to recenter even if the time is already + * visible + * @return Whether or not the view animated to the new location + */ + private void goTo(MonthAdapter.CalendarDay day, boolean animate, boolean setSelected, boolean forceScroll) { + + // Set the selected day + if (setSelected) { + mSelectedDay.set(day); + } + + mTempDay.set(day); + final int position = (day.year - mController.getMinYear()) + * MonthAdapter.MONTHS_IN_YEAR + day.month; + + View child; + int i = 0; + int top ; + // Find a child that's completely in the view + do { + child = getChildAt(i++); + if (child == null) { + break; + } + top = child.getTop(); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "child at " + (i - 1) + " has top " + top); + } + } while (top < 0); + + // Compute the first and last position visible + int selectedPosition; + if (child != null) { + selectedPosition = getPositionForView(child); + } else { + selectedPosition = 0; + } + + if (setSelected) { + mAdapter.setSelectedDay(mSelectedDay); + } + + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "GoTo position " + position); + } + // Check if the selected day is now outside of our visible range + // and if so scroll to the month that contains it + if (position != selectedPosition || forceScroll) { + setMonthDisplayed(mTempDay); + mPreviousScrollState = OnScrollListener.SCROLL_STATE_FLING; + if (animate) { + smoothScrollToPositionFromTop( + position, LIST_TOP_OFFSET, GOTO_SCROLL_DURATION); + } else { + postSetSelection(position); + } + } else if (setSelected) { + setMonthDisplayed(mSelectedDay); + } + } + + public void postSetSelection(final int position) { + clearFocus(); + post(new Runnable() { + + @Override + public void run() { + setSelection(position); + } + }); + onScrollStateChanged(this, OnScrollListener.SCROLL_STATE_IDLE); + } + + /** + * Updates the title and selected month if the view has moved to a new + * month. + */ + @Override + public void onScroll( + AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { + MonthView child = (MonthView) view.getChildAt(0); + if (child == null) { + return; + } + + // Figure out where we are + mPreviousScrollPosition = (long) (view.getFirstVisiblePosition() * child.getHeight() - child.getBottom()); + mPreviousScrollState = mCurrentScrollState; + } + + /** + * Sets the month displayed at the top of this view based on time. Override + * to add custom events when the title is changed. + */ + private void setMonthDisplayed(MonthAdapter.CalendarDay date) { + mCurrentMonthDisplayed = date.month; + invalidateViews(); + } + + @Override + public void onScrollStateChanged(AbsListView view, int scrollState) { + // use a post to prevent re-entering onScrollStateChanged before it + // exits + mScrollStateChangedRunnable.doScrollStateChange(view, scrollState); + } + + private ScrollStateRunnable mScrollStateChangedRunnable = new ScrollStateRunnable(); + + protected class ScrollStateRunnable implements Runnable { + private int mNewState; + + /** + * Sets up the runnable with a short delay in case the scroll state + * immediately changes again. + * + * @param view The list view that changed state + * @param scrollState The new state it changed to + */ + public void doScrollStateChange(AbsListView view, int scrollState) { + mHandler.removeCallbacks(this); + mNewState = scrollState; + mHandler.postDelayed(this, SCROLL_CHANGE_DELAY); + } + + @Override + public void run() { + mCurrentScrollState = mNewState; + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, + "new scroll state: " + mNewState + " old state: " + mPreviousScrollState); + } + // Fix the position after a scroll or a fling ends + if (mNewState == OnScrollListener.SCROLL_STATE_IDLE + && mPreviousScrollState != OnScrollListener.SCROLL_STATE_IDLE + && mPreviousScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { + mPreviousScrollState = mNewState; + int i = 0; + View child = getChildAt(i); + while (child != null && child.getBottom() <= 0) { + child = getChildAt(++i); + } + if (child == null) { + // The view is no longer visible, just return + return; + } + int firstPosition = getFirstVisiblePosition(); + int lastPosition = getLastVisiblePosition(); + boolean scroll = firstPosition != 0 && lastPosition != getCount() - 1; + final int top = child.getTop(); + final int bottom = child.getBottom(); + final int midpoint = getHeight() / 2; + if (scroll && top < LIST_TOP_OFFSET) { + if (bottom > midpoint) { + smoothScrollBy(top, GOTO_SCROLL_DURATION); + } else { + smoothScrollBy(bottom, GOTO_SCROLL_DURATION); + } + } + } else { + mPreviousScrollState = mNewState; + } + } + } + + /** + * Gets the position of the view that is most prominently displayed within the list view. + */ + public int getMostVisiblePosition() { + final int firstPosition = getFirstVisiblePosition(); + final int height = getHeight(); + + int maxDisplayedHeight = 0; + int mostVisibleIndex = 0; + int i=0; + int bottom = 0; + while (bottom < height) { + View child = getChildAt(i); + if (child == null) { + break; + } + bottom = child.getBottom(); + int displayedHeight = Math.min(bottom, height) - Math.max(0, child.getTop()); + if (displayedHeight > maxDisplayedHeight) { + mostVisibleIndex = i; + maxDisplayedHeight = displayedHeight; + } + i++; + } + return firstPosition + mostVisibleIndex; + } + + @Override + public void onDateChanged() { + goTo(mController.getSelectedDay(), false, true, true); + } + + /** + * Attempts to return the date that has accessibility focus. + * + * @return The date that has accessibility focus, or {@code null} if no date + * has focus. + */ + private MonthAdapter.CalendarDay findAccessibilityFocus() { + final int childCount = getChildCount(); + for (int i = 0; i < childCount; i++) { + final View child = getChildAt(i); + if (child instanceof MonthView) { + final MonthAdapter.CalendarDay focus = ((MonthView) child).getAccessibilityFocus(); + if (focus != null) { + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) { + // Clear focus to avoid ListView bug in Jelly Bean MR1. + ((MonthView) child).clearAccessibilityFocus(); + } + return focus; + } + } + } + + return null; + } + + /** + * Attempts to restore accessibility focus to a given date. No-op if + * {@code day} is {@code null}. + * + * @param day The date that should receive accessibility focus + * @return {@code true} if focus was restored + */ + private void restoreAccessibilityFocus(MonthAdapter.CalendarDay day) { + if (day == null) { + return; + } + + final int childCount = getChildCount(); + for (int i = 0; i < childCount; i++) { + final View child = getChildAt(i); + if (child instanceof MonthView) { + if (((MonthView) child).restoreAccessibilityFocus(day)) { + return; + } + } + } + + } + + @Override + protected void layoutChildren() { + final MonthAdapter.CalendarDay focusedDay = findAccessibilityFocus(); + super.layoutChildren(); + if (mPerformingScroll) { + mPerformingScroll = false; + } else { + restoreAccessibilityFocus(focusedDay); + } + } + + @Override + public void onInitializeAccessibilityEvent(@NonNull AccessibilityEvent event) { + super.onInitializeAccessibilityEvent(event); + event.setItemCount(-1); + } + + private static String getMonthAndYearString(MonthAdapter.CalendarDay day) { + PersianCalendar mPersianCalendar = new PersianCalendar(); + mPersianCalendar.setPersianDate(day.year, day.month, day.day); + + String sbuf = ""; + sbuf += mPersianCalendar.getPersianMonthName(); + sbuf += " "; + sbuf += mPersianCalendar.getPersianYear(); + return sbuf; + } + + /** + * Necessary for accessibility, to ensure we support "scrolling" forward and backward + * in the month list. + */ + @Override + @SuppressWarnings("deprecation") + public void onInitializeAccessibilityNodeInfo(@NonNull AccessibilityNodeInfo info) { + super.onInitializeAccessibilityNodeInfo(info); + if(Build.VERSION.SDK_INT >= 21) { + info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD); + info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD); + } + else { + info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); + info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); + } + } + + /** + * When scroll forward/backward events are received, announce the newly scrolled-to month. + */ + @SuppressLint("NewApi") + @Override + public boolean performAccessibilityAction(int action, Bundle arguments) { + if (action != AccessibilityNodeInfo.ACTION_SCROLL_FORWARD && + action != AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) { + return super.performAccessibilityAction(action, arguments); + } + + // Figure out what month is showing. + int firstVisiblePosition = getFirstVisiblePosition(); + int month = firstVisiblePosition % 12; + int year = firstVisiblePosition / 12 + mController.getMinYear(); + MonthAdapter.CalendarDay day = new MonthAdapter.CalendarDay(year, month, 1); + + // Scroll either forward or backward one month. + if (action == AccessibilityNodeInfo.ACTION_SCROLL_FORWARD) { + day.month++; + if (day.month == 12) { + day.month = 0; + day.year++; + } + } else if (action == AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) { + View firstVisibleView = getChildAt(0); + // If the view is fully visible, jump one month back. Otherwise, we'll just jump + // to the first day of first visible month. + if (firstVisibleView != null && firstVisibleView.getTop() >= -1) { + // There's an off-by-one somewhere, so the top of the first visible item will + // actually be -1 when it's at the exact top. + day.month--; + if (day.month == -1) { + day.month = 11; + day.year--; + } + } + } + + // Go to that month. + Utils.tryAccessibilityAnnounce(this, + LanguageUtils.getPersianNumbers(getMonthAndYearString(day))); + goTo(day, true, false, true); + mPerformingScroll = true; + return true; + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/MonthAdapter.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/MonthAdapter.java new file mode 100644 index 0000000..d464a8e --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/MonthAdapter.java @@ -0,0 +1,226 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AbsListView.LayoutParams; +import android.widget.BaseAdapter; + +import java.util.HashMap; + +import mohammadaminha.com.widgets.Date_Picker.date.MonthView.OnDayClickListener; +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; + +/** + * An adapter for a list of {@link MonthView} items. + */ +public abstract class MonthAdapter extends BaseAdapter implements OnDayClickListener { + + private static final String TAG = "SimpleMonthAdapter"; + + private Context mContext; + final DatePickerController mController; + + private CalendarDay mSelectedDay; + + protected static int WEEK_7_OVERHANG_HEIGHT = 7; + static final int MONTHS_IN_YEAR = 12; + + /** + * A convenience class to represent a specific date. + */ + public static class CalendarDay { + private PersianCalendar mPersianCalendar; + int year; + int month; + int day; + + public CalendarDay() { + setTime(System.currentTimeMillis()); + } + + public CalendarDay(long timeInMillis) { + setTime(timeInMillis); + } + + public CalendarDay(PersianCalendar calendar) { + year = calendar.getPersianYear(); + month = calendar.getPersianMonth(); + day = calendar.getPersianDay(); + } + + public CalendarDay(int year, int month, int day) { + setDay(year, month, day); + } + + public void set(CalendarDay date) { + year = date.year; + month = date.month; + day = date.day; + } + + public void setDay(int year, int month, int day) { + this.year = year; + this.month = month; + this.day = day; + } + + private void setTime(long timeInMillis) { + if (mPersianCalendar == null) { + mPersianCalendar = new PersianCalendar(); + } + mPersianCalendar.setTimeInMillis(timeInMillis); + month = mPersianCalendar.getPersianMonth(); + year = mPersianCalendar.getPersianYear(); + day = mPersianCalendar.getPersianDay(); + } + + public int getYear() { + return year; + } + + public int getMonth() { + return month; + } + + public int getDay() { + return day; + } + } + + MonthAdapter(Context context, + DatePickerController controller) { + mContext = context; + mController = controller; + init(); + setSelectedDay(mController.getSelectedDay()); + } + + /** + * Updates the selected day and related parameters. + * + * @param day The day to highlight + */ + public void setSelectedDay(CalendarDay day) { + mSelectedDay = day; + notifyDataSetChanged(); + } + + public CalendarDay getSelectedDay() { + return mSelectedDay; + } + + /** + * Set up the gesture detector and selected time + */ + private void init() { + mSelectedDay = new CalendarDay(System.currentTimeMillis()); + } + + @Override + public int getCount() { + return ((mController.getMaxYear() - mController.getMinYear()) + 1) * MONTHS_IN_YEAR; + } + + @Override + public Object getItem(int position) { + return null; + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public boolean hasStableIds() { + return true; + } + + @SuppressLint("NewApi") + @SuppressWarnings("unchecked") + @Override + public View getView(int position, View convertView, ViewGroup parent) { + MonthView v; + HashMap drawingParams = null; + if (convertView != null) { + v = (MonthView) convertView; + // We store the drawing parameters in the view so it can be recycled + drawingParams = (HashMap) v.getTag(); + } else { + v = createMonthView(mContext); + // Set up the new view + LayoutParams params = new LayoutParams( + LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + v.setLayoutParams(params); + v.setClickable(true); + v.setOnDayClickListener(this); + } + if (drawingParams == null) { + drawingParams = new HashMap<>(); + } + drawingParams.clear(); + + final int month = position % MONTHS_IN_YEAR; + final int year = position / MONTHS_IN_YEAR + mController.getMinYear(); + + int selectedDay = -1; + if (isSelectedDayInMonth(year, month)) { + selectedDay = mSelectedDay.day; + } + + // Invokes requestLayout() to ensure that the recycled view is set with the appropriate + // height/number of weeks before being displayed. + v.reuse(); + + drawingParams.put(MonthView.VIEW_PARAMS_SELECTED_DAY, selectedDay); + drawingParams.put(MonthView.VIEW_PARAMS_YEAR, year); + drawingParams.put(MonthView.VIEW_PARAMS_MONTH, month); + drawingParams.put(MonthView.VIEW_PARAMS_WEEK_START, mController.getFirstDayOfWeek()); + v.setMonthParams(drawingParams); + v.invalidate(); + return v; + } + + protected abstract MonthView createMonthView(Context context); + + private boolean isSelectedDayInMonth(int year, int month) { + return mSelectedDay.year == year && mSelectedDay.month == month; + } + + + @Override + public void onDayClick(MonthView view, CalendarDay day) { + if (day != null) { + onDayTapped(day); + } + } + + /** + * Maintains the same hour/min/sec but moves the day to the tapped day. + * + * @param day The day that was tapped + */ + private void onDayTapped(CalendarDay day) { + mController.tryVibrate(); + mController.onDayOfMonthSelected(day.year, day.month, day.day); + setSelectedDay(day); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/MonthView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/MonthView.java new file mode 100644 index 0000000..dcbaaf3 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/MonthView.java @@ -0,0 +1,828 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.Paint.Style; +import android.graphics.Rect; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.view.ViewCompat; +import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; +import android.support.v4.widget.ExploreByTouchHelper; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityNodeInfo; + +import java.security.InvalidParameterException; +import java.util.Calendar; +import java.util.HashMap; +import java.util.List; + +import mohammadaminha.com.widgets.Date_Picker.TypefaceHelper; +import mohammadaminha.com.widgets.Date_Picker.Utils; +import mohammadaminha.com.widgets.Date_Picker.date.MonthAdapter.CalendarDay; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; +import mohammadaminha.com.widgets.R; +import mohammadaminha.com.widgets.Util; + +/** + * A calendar-like view displaying a specified month and the appropriate selectable day numbers + * within the specified month. + */ +public abstract class MonthView extends View { + private static final String TAG = "MonthView"; + + /** + * This sets the height of this week in pixels + */ + private static final String VIEW_PARAMS_HEIGHT = "height"; + /** + * This specifies the position (or weeks since the epoch) of this week. + */ + public static final String VIEW_PARAMS_MONTH = "month"; + /** + * This specifies the position (or weeks since the epoch) of this week. + */ + public static final String VIEW_PARAMS_YEAR = "year"; + /** + * This sets one of the days in this view as selected {@link Calendar#SUNDAY} + * through {@link Calendar#SATURDAY}. + */ + public static final String VIEW_PARAMS_SELECTED_DAY = "selected_day"; + /** + * Which day the week should start on. {@link Calendar#SUNDAY} through + * {@link Calendar#SATURDAY}. + */ + public static final String VIEW_PARAMS_WEEK_START = "week_start"; + /** + * How many days to display at a time. Days will be displayed starting with + * {@link #mWeekStart}. + */ + public static final String VIEW_PARAMS_NUM_DAYS = "num_days"; + /** + * Which month is currently in focus, as defined by {@link Calendar#MONTH} + * [0-11]. + */ + public static final String VIEW_PARAMS_FOCUS_MONTH = "focus_month"; + /** + * If this month should display week numbers. false if 0, true otherwise. + */ + public static final String VIEW_PARAMS_SHOW_WK_NUM = "show_wk_num"; + + private static final int DEFAULT_HEIGHT = 32; + private static final int MIN_HEIGHT = 10; + private static final int DEFAULT_SELECTED_DAY = -1; + private static final int DEFAULT_WEEK_START = Calendar.SATURDAY; + private static final int DEFAULT_NUM_DAYS = 7; + protected static final int DEFAULT_SHOW_WK_NUM = 0; + protected static final int DEFAULT_FOCUS_MONTH = -1; + private static final int DEFAULT_NUM_ROWS = 6; + private static final int MAX_NUM_ROWS = 6; + + private static final int SELECTED_CIRCLE_ALPHA = 255; + + private static final int DAY_SEPARATOR_WIDTH = 1; + static int MINI_DAY_NUMBER_TEXT_SIZE; + private static int MONTH_LABEL_TEXT_SIZE; + private static int MONTH_DAY_LABEL_TEXT_SIZE; + private static int MONTH_HEADER_SIZE; + static int DAY_SELECTED_CIRCLE_SIZE; + + // used for scaling to the device density + protected static float mScale = 0; + + private DatePickerController mController; + + // affects the padding on the sides of this view + private int mEdgePadding = 0; + + + Paint mMonthNumPaint; + private Paint mMonthTitlePaint; + Paint mSelectedCirclePaint; + private Paint mMonthDayLabelPaint; + + private StringBuilder mStringBuilder; + + // The Julian day of the first day displayed by this item + protected int mFirstJulianDay = -1; + // The month of the first day in this week + protected int mFirstMonth = -1; + // The month of the last day in this week + protected int mLastMonth = -1; + + private int mMonth; + + private int mYear; + // Quick reference to the width of this view, matches parent + private int mWidth; + // The height this view should draw at in pixels, set by height param + private int mRowHeight = DEFAULT_HEIGHT; + // If this view contains the today + boolean mHasToday = false; + // Which day is selected [0-6] or -1 if no day is selected + int mSelectedDay = -1; + // Which day is today [0-6] or -1 if no day is today + int mToday = DEFAULT_SELECTED_DAY; + // Which day of the week to start on [0-6] + private int mWeekStart = DEFAULT_WEEK_START; + // How many days to display + private int mNumDays = DEFAULT_NUM_DAYS; + // The number of days + a spot for week number if it is displayed + private int mNumCells = mNumDays; + // The left edge of the selected day + protected int mSelectedLeft = -1; + // The right edge of the selected day + protected int mSelectedRight = -1; + + private PersianCalendar mPersianCalendar; + private PersianCalendar mDayLabelCalendar; + private MonthViewTouchHelper mTouchHelper; + + private int mNumRows = DEFAULT_NUM_ROWS; + + // Optional listener for handling day click actions + private OnDayClickListener mOnDayClickListener; + + // Whether to prevent setting the accessibility delegate + private boolean mLockAccessibilityDelegate; + + final int mDayTextColor; + final int mSelectedDayTextColor; + private int mMonthDayTextColor; + final int mTodayNumberColor; + final int mHighlightedDayTextColor; + final int mDisabledDayTextColor; + private int mMonthTitleColor; + + public MonthView(Context context) { + this(context, null, null); + } + + MonthView(Context context, AttributeSet attr, DatePickerController controller) { + super(context, attr); + mController = controller; + Resources res = context.getResources(); + + mDayLabelCalendar = new PersianCalendar(); + mPersianCalendar = new PersianCalendar(); + + boolean darkTheme = mController != null && mController.isThemeDark(); + if (darkTheme) { + mDayTextColor = res.getColor(R.color.mdtp_date_picker_text_normal_dark_theme); + mMonthDayTextColor = res.getColor(R.color.mdtp_date_picker_month_day_dark_theme); + mDisabledDayTextColor = res.getColor(R.color.mdtp_date_picker_text_disabled_dark_theme); + mHighlightedDayTextColor = res.getColor(R.color.mdtp_date_picker_text_highlighted_dark_theme); + } else { + mDayTextColor = res.getColor(R.color.mdtp_date_picker_text_normal); + mMonthDayTextColor = res.getColor(R.color.mdtp_date_picker_month_day); + mDisabledDayTextColor = res.getColor(R.color.mdtp_date_picker_text_disabled); + mHighlightedDayTextColor = res.getColor(R.color.mdtp_date_picker_text_highlighted); + } + mSelectedDayTextColor = res.getColor(R.color.mdtp_white); + mTodayNumberColor = res.getColor(R.color.mdtp_accent_color); + mMonthTitleColor = res.getColor(R.color.mdtp_white); + + mStringBuilder = new StringBuilder(50); + + MINI_DAY_NUMBER_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_day_number_size); + MONTH_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_label_size); + MONTH_DAY_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_day_label_text_size); + MONTH_HEADER_SIZE = res.getDimensionPixelOffset(R.dimen.mdtp_month_list_item_header_height); + DAY_SELECTED_CIRCLE_SIZE = res + .getDimensionPixelSize(R.dimen.mdtp_day_number_select_circle_radius); + + mRowHeight = (res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height) + - getMonthHeaderSize()) / MAX_NUM_ROWS; + + // Set up accessibility components. + mTouchHelper = getMonthViewTouchHelper(); + ViewCompat.setAccessibilityDelegate(this, mTouchHelper); + ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES); + mLockAccessibilityDelegate = true; + + // Sets up any standard paints that will be used + initView(); + } + + public void setDatePickerController(DatePickerController controller) { + mController = controller; + } + + private MonthViewTouchHelper getMonthViewTouchHelper() { + return new MonthViewTouchHelper(this); + } + + @Override + public void setAccessibilityDelegate(AccessibilityDelegate delegate) { + // Workaround for a JB MR1 issue where accessibility delegates on + // top-level ListView items are overwritten. + if (!mLockAccessibilityDelegate) { + super.setAccessibilityDelegate(delegate); + } + } + + public void setOnDayClickListener(OnDayClickListener listener) { + mOnDayClickListener = listener; + } + + @Override + public boolean dispatchHoverEvent(@NonNull MotionEvent event) { + // First right-of-refusal goes the touch exploration helper. + return mTouchHelper.dispatchHoverEvent(event) || super.dispatchHoverEvent(event); + } + + @Override + public boolean onTouchEvent(@NonNull MotionEvent event) { + switch (event.getAction()) { + case MotionEvent.ACTION_UP: + final int day = getDayFromLocation(event.getX(), event.getY()); + if (day >= 0) { + onDayClick(day); + } + break; + } + return true; + } + + /** + * Sets up the text and style properties for painting. Override this if you + * want to use a different paint. + */ + private void initView() { + mMonthTitlePaint = new Paint(); + mMonthTitlePaint.setFakeBoldText(true); + mMonthTitlePaint.setAntiAlias(true); + mMonthTitlePaint.setTextSize(MONTH_LABEL_TEXT_SIZE); + + mMonthTitlePaint.setTypeface(Util.getTypeFace()); + mMonthTitlePaint.setColor(mDayTextColor); + mMonthTitlePaint.setTextAlign(Align.CENTER); + mMonthTitlePaint.setStyle(Style.FILL); + + mSelectedCirclePaint = new Paint(); + mSelectedCirclePaint.setFakeBoldText(true); + mSelectedCirclePaint.setAntiAlias(true); + mSelectedCirclePaint.setColor(mTodayNumberColor); + mSelectedCirclePaint.setTextAlign(Align.CENTER); + mSelectedCirclePaint.setStyle(Style.FILL); + mSelectedCirclePaint.setAlpha(SELECTED_CIRCLE_ALPHA); + + mMonthDayLabelPaint = new Paint(); + mMonthDayLabelPaint.setAntiAlias(true); + mMonthDayLabelPaint.setTextSize(MONTH_DAY_LABEL_TEXT_SIZE); + mMonthDayLabelPaint.setColor(mMonthDayTextColor); + mMonthDayLabelPaint.setTypeface(TypefaceHelper.get(getContext(), "Roboto-Medium")); + mMonthDayLabelPaint.setStyle(Style.FILL); + mMonthDayLabelPaint.setTextAlign(Align.CENTER); + mMonthDayLabelPaint.setFakeBoldText(true); + + mMonthNumPaint = new Paint(); + mMonthNumPaint.setAntiAlias(true); + mMonthNumPaint.setTextSize(MINI_DAY_NUMBER_TEXT_SIZE); + mMonthNumPaint.setStyle(Style.FILL); + mMonthNumPaint.setTextAlign(Align.CENTER); + mMonthNumPaint.setFakeBoldText(false); + } + + @Override + protected void onDraw(Canvas canvas) { + drawMonthTitle(canvas); + drawMonthDayLabels(canvas); + drawMonthNums(canvas); + } + + private int mDayOfWeekStart = 0; + + /** + * Sets all the parameters for displaying this week. The only required + * parameter is the week number. Other parameters have a default value and + * will only update if a new value is included, except for focus month, + * which will always default to no focus month if no value is passed in. See + * {@link #VIEW_PARAMS_HEIGHT} for more info on parameters. + * + * @param params A map of the new parameters, see + * {@link #VIEW_PARAMS_HEIGHT} + */ + public void setMonthParams(HashMap params) { + if (!params.containsKey(VIEW_PARAMS_MONTH) && !params.containsKey(VIEW_PARAMS_YEAR)) { + throw new InvalidParameterException("You must specify month and year for this view"); + } + setTag(params); + // We keep the current value for any params not present + if (params.containsKey(VIEW_PARAMS_HEIGHT)) { + mRowHeight = params.get(VIEW_PARAMS_HEIGHT); + if (mRowHeight < MIN_HEIGHT) { + mRowHeight = MIN_HEIGHT; + } + } + if (params.containsKey(VIEW_PARAMS_SELECTED_DAY)) { + mSelectedDay = params.get(VIEW_PARAMS_SELECTED_DAY); + } + + // Allocate space for caching the day numbers and focus values + mMonth = params.get(VIEW_PARAMS_MONTH); + mYear = params.get(VIEW_PARAMS_YEAR); + + // Figure out what day today is + //final Time today = new Time(Time.getCurrentTimezone()); + //today.setToNow(); + final PersianCalendar today = new PersianCalendar(); + mHasToday = false; + mToday = -1; + + mPersianCalendar.setPersianDate(mYear, mMonth, 1); + mDayOfWeekStart = mPersianCalendar.get(Calendar.DAY_OF_WEEK); + + if (params.containsKey(VIEW_PARAMS_WEEK_START)) { + mWeekStart = params.get(VIEW_PARAMS_WEEK_START); + } else { + mWeekStart = Calendar.SATURDAY; + } + + mNumCells = Utils.getDaysInMonth(mMonth, mYear); + for (int i = 0; i < mNumCells; i++) { + final int day = i + 1; + if (sameDay(day, today)) { + mHasToday = true; + mToday = day; + } + } + mNumRows = calculateNumRows(); + + // Invalidate cached accessibility information. + mTouchHelper.invalidateRoot(); + } + + public void setSelectedDay(int day) { + mSelectedDay = day; + } + + public void reuse() { + mNumRows = DEFAULT_NUM_ROWS; + requestLayout(); + } + + private int calculateNumRows() { + int offset = findDayOffset(); + int dividend = (offset + mNumCells) / mNumDays; + int remainder = (offset + mNumCells) % mNumDays; + return (dividend + (remainder > 0 ? 1 : 0)); + } + + private boolean sameDay(int day, PersianCalendar today) { + return mYear == today.getPersianYear() && + mMonth == today.getPersianMonth() && + day == today.getPersianDay(); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mRowHeight * mNumRows + + getMonthHeaderSize() + 5); + } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + mWidth = w; + + // Invalidate cached accessibility information. + mTouchHelper.invalidateRoot(); + } + + public int getMonth() { + return mMonth; + } + + public int getYear() { + return mYear; + } + + /** + * A wrapper to the MonthHeaderSize to allow override it in children + */ + private int getMonthHeaderSize() { + return MONTH_HEADER_SIZE; + } + + private String getMonthAndYearString() { + mStringBuilder.setLength(0); + return LanguageUtils.getPersianNumbers( + mPersianCalendar.getPersianMonthName() + " " + mPersianCalendar.getPersianYear()); + } + + private void drawMonthTitle(Canvas canvas) { + int x = (mWidth + 2 * mEdgePadding) / 2; + int y = (getMonthHeaderSize() - MONTH_DAY_LABEL_TEXT_SIZE) / 2; + canvas.drawText(getMonthAndYearString(), x, y, mMonthTitlePaint); + } + + private void drawMonthDayLabels(Canvas canvas) { + int y = getMonthHeaderSize() - (MONTH_DAY_LABEL_TEXT_SIZE / 2); + int dayWidthHalf = (mWidth - mEdgePadding * 2) / (mNumDays * 2); + + for (int i = 0; i < mNumDays; i++) { + int calendarDay = (i + mWeekStart) % mNumDays; + int x = (2 * i + 1) * dayWidthHalf + mEdgePadding; + mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay); + String localWeekDisplayName = mDayLabelCalendar.getPersianWeekDayName(); // TODO: RTLize + String weekString = localWeekDisplayName.substring(0, 1); + canvas.drawText(weekString, x, y, mMonthDayLabelPaint); + } + } + + /** + * Draws the week and month day numbers for this week. Override this method + * if you need different placement. + * + * @param canvas The canvas to draw on + */ + private void drawMonthNums(Canvas canvas) { + int y = (((mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2) - DAY_SEPARATOR_WIDTH) + + getMonthHeaderSize(); + final float dayWidthHalf = (mWidth - mEdgePadding * 2) / (mNumDays * 2.0f); + int j = findDayOffset(); + for (int dayNumber = 1; dayNumber <= mNumCells; dayNumber++) { + final int x = (int) ((2 * j + 1) * dayWidthHalf + mEdgePadding); + + int yRelativeToDay = (mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2 - DAY_SEPARATOR_WIDTH; + + final int startX = (int) (x - dayWidthHalf); + final int stopX = (int) (x + dayWidthHalf); + final int startY = y - yRelativeToDay; + final int stopY = startY + mRowHeight; + + drawMonthDay(canvas, mYear, mMonth, dayNumber, x, y, startX, stopX, startY, stopY); + + j++; + if (j == mNumDays) { + j = 0; + y += mRowHeight; + } + } + } + + /** + * This method should draw the month day. Implemented by sub-classes to allow customization. + * + * @param canvas The canvas to draw on + * @param year The year of this month day + * @param month The month of this month day + * @param day The day number of this month day + * @param x The default x position to draw the day number + * @param y The default y position to draw the day number + * @param startX The left boundary of the day number rect + * @param stopX The right boundary of the day number rect + * @param startY The top boundary of the day number rect + * @param stopY The bottom boundary of the day number rect + */ + protected abstract void drawMonthDay(Canvas canvas, int year, int month, int day, + int x, int y, int startX, int stopX, int startY, int stopY); + + private int findDayOffset() { + return (mDayOfWeekStart < mWeekStart ? (mDayOfWeekStart + mNumDays) : mDayOfWeekStart) + - mWeekStart; + } + + + /** + * Calculates the day that the given x position is in, accounting for week + * number. Returns the day or -1 if the position wasn't in a day. + * + * @param x The x position of the touch event + * @return The day number, or -1 if the position wasn't in a day + */ + private int getDayFromLocation(float x, float y) { + final int day = getInternalDayFromLocation(x, y); + if (day < 1 || day > mNumCells) { + return -1; + } + return day; + } + + /** + * Calculates the day that the given x position is in, accounting for week + * number. + * + * @param x The x position of the touch event + * @return The day number + */ + private int getInternalDayFromLocation(float x, float y) { + int dayStart = mEdgePadding; + if (x < dayStart || x > mWidth - mEdgePadding) { + return -1; + } + // Selection is (x - start) / (pixels/day) == (x -s) * day / pixels + int row = (int) (y - getMonthHeaderSize()) / mRowHeight; + int column = (int) ((x - dayStart) * mNumDays / (mWidth - dayStart - mEdgePadding)); + + int day = column - findDayOffset() + 1; + day += row * mNumDays; + return day; + } + + /** + * Called when the user clicks on a day. Handles callbacks to the + * {@link OnDayClickListener} if one is set. + *

    + * If the day is out of the range set by minDate and/or maxDate, this is a no-op. + * + * @param day The day that was clicked + */ + private void onDayClick(int day) { + // If the min / max date are set, only process the click if it's a valid selection. + if (isOutOfRange(mYear, mMonth, day)) { + return; + } + + + if (mOnDayClickListener != null) { + mOnDayClickListener.onDayClick(this, new CalendarDay(mYear, mMonth, day)); + } + + // This is a no-op if accessibility is turned off. + mTouchHelper.sendEventForVirtualView(day, AccessibilityEvent.TYPE_VIEW_CLICKED); + } + + /** + * @return true if the specified year/month/day are within the selectable days or the range set by minDate and maxDate. + * If one or either have not been set, they are considered as Integer.MIN_VALUE and + * Integer.MAX_VALUE. + */ + boolean isOutOfRange(int year, int month, int day) { + if (mController.getSelectableDays() != null) { + return !isSelectable(year, month, day); + } + + if (isBeforeMin(year, month, day)) { + return true; + } else if (isAfterMax(year, month, day)) { + return true; + } + + return false; + } + + private boolean isSelectable(int year, int month, int day) { + PersianCalendar[] selectableDays = mController.getSelectableDays(); + for (PersianCalendar c : selectableDays) { + if (year < c.getPersianYear()) break; + if (year > c.getPersianYear()) continue; + if (month < c.getPersianMonth()) break; + if (month > c.getPersianMonth()) continue; + if (day < c.getPersianDay()) break; + if (day > c.getPersianDay()) continue; + return true; + } + return false; + } + + private boolean isBeforeMin(int year, int month, int day) { + if (mController == null) { + return false; + } + PersianCalendar minDate = mController.getMinDate(); + if (minDate == null) { + return false; + } + + if (year < minDate.getPersianYear()) { + return true; + } else if (year > minDate.getPersianYear()) { + return false; + } + + if (month < minDate.getPersianMonth()) { + return true; + } else if (month > minDate.getPersianMonth()) { + return false; + } + + return day < minDate.getPersianDay(); + } + + private boolean isAfterMax(int year, int month, int day) { + if (mController == null) { + return false; + } + PersianCalendar maxDate = mController.getMaxDate(); + if (maxDate == null) { + return false; + } + + if (year > maxDate.getPersianYear()) { + return true; + } else if (year < maxDate.getPersianYear()) { + return false; + } + + if (month > maxDate.getPersianMonth()) { + return true; + } else if (month < maxDate.getPersianMonth()) { + return false; + } + + return day > maxDate.getPersianMonth(); + } + + /** + * @param year + * @param month + * @param day + * @return true if the given date should be highlighted + */ + boolean isHighlighted(int year, int month, int day) { + PersianCalendar[] highlightedDays = mController.getHighlightedDays(); + if (highlightedDays == null) return false; + for (PersianCalendar c : highlightedDays) { + if (year < c.getPersianYear()) break; + if (year > c.getPersianYear()) continue; + if (month < c.getPersianMonth()) break; + if (month > c.getPersianMonth()) continue; + if (day < c.getPersianDay()) break; + if (day > c.getPersianDay()) continue; + return true; + } + return false; + } + + /** + * @return The date that has accessibility focus, or {@code null} if no date + * has focus + */ + public CalendarDay getAccessibilityFocus() { + final int day = mTouchHelper.getFocusedVirtualView(); + if (day >= 0) { + return new CalendarDay(mYear, mMonth, day); + } + return null; + } + + /** + * Clears accessibility focus within the view. No-op if the view does not + * contain accessibility focus. + */ + public void clearAccessibilityFocus() { + mTouchHelper.clearFocusedVirtualView(); + } + + /** + * Attempts to restore accessibility focus to the specified date. + * + * @param day The date which should receive focus + * @return {@code false} if the date is not valid for this month view, or + * {@code true} if the date received focus + */ + public boolean restoreAccessibilityFocus(CalendarDay day) { + if ((day.year != mYear) || (day.month != mMonth) || (day.day > mNumCells)) { + return false; + } + mTouchHelper.setFocusedVirtualView(day.day); + return true; + } + + /** + * Provides a virtual view hierarchy for interfacing with an accessibility + * service. + */ + protected class MonthViewTouchHelper extends ExploreByTouchHelper { + + private Rect mTempRect = new Rect(); + private PersianCalendar mTempCalendar = new PersianCalendar(); + + public MonthViewTouchHelper(View host) { + super(host); + } + + public void setFocusedVirtualView(int virtualViewId) { + getAccessibilityNodeProvider(MonthView.this).performAction( + virtualViewId, AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS, null); + } + + public void clearFocusedVirtualView() { + final int focusedVirtualView = getFocusedVirtualView(); + if (focusedVirtualView != ExploreByTouchHelper.INVALID_ID) { + getAccessibilityNodeProvider(MonthView.this).performAction( + focusedVirtualView, + AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS, + null); + } + } + + @Override + protected int getVirtualViewAt(float x, float y) { + final int day = getDayFromLocation(x, y); + if (day >= 0) { + return day; + } + return ExploreByTouchHelper.INVALID_ID; + } + + @Override + protected void getVisibleVirtualViews(List virtualViewIds) { + for (int day = 1; day <= mNumCells; day++) { + virtualViewIds.add(day); + } + } + + @Override + protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) { + event.setContentDescription(getItemDescription(virtualViewId)); + } + + @Override + protected void onPopulateNodeForVirtualView(int virtualViewId, + AccessibilityNodeInfoCompat node) { + getItemBounds(virtualViewId, mTempRect); + + node.setContentDescription(getItemDescription(virtualViewId)); + node.setBoundsInParent(mTempRect); + node.addAction(AccessibilityNodeInfo.ACTION_CLICK); + + if (virtualViewId == mSelectedDay) { + node.setSelected(true); + } + + } + + @Override + protected boolean onPerformActionForVirtualView(int virtualViewId, int action, + Bundle arguments) { + switch (action) { + case AccessibilityNodeInfo.ACTION_CLICK: + onDayClick(virtualViewId); + return true; + } + + return false; + } + + /** + * Calculates the bounding rectangle of a given time object. + * + * @param day The day to calculate bounds for + * @param rect The rectangle in which to store the bounds + */ + void getItemBounds(int day, Rect rect) { + final int offsetX = mEdgePadding; + final int offsetY = getMonthHeaderSize(); + final int cellHeight = mRowHeight; + final int cellWidth = ((mWidth - (2 * mEdgePadding)) / mNumDays); + final int index = ((day - 1) + findDayOffset()); + final int row = (index / mNumDays); + final int column = (index % mNumDays); + final int x = (offsetX + (column * cellWidth)); + final int y = (offsetY + (row * cellHeight)); + + rect.set(x, y, (x + cellWidth), (y + cellHeight)); + } + + /** + * Generates a description for a given time object. Since this + * description will be spoken, the components are ordered by descending + * specificity as DAY MONTH YEAR. + * + * @param day The day to generate a description for + * @return A description of the time object + */ + CharSequence getItemDescription(int day) { + mTempCalendar.setPersianDate(mYear, mMonth, day); + final String date = LanguageUtils.getPersianNumbers(mTempCalendar.getPersianLongDate()); + + if (day == mSelectedDay) { + return getContext().getString(R.string.mdtp_item_is_selected, date); + } + + return date; + } + } + + /** + * Handles callbacks when the user clicks on a time object. + */ + public interface OnDayClickListener { + void onDayClick(MonthView view, CalendarDay day); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/SimpleDayPickerView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/SimpleDayPickerView.java new file mode 100644 index 0000000..0a4988e --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/SimpleDayPickerView.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.content.Context; +import android.util.AttributeSet; + +/** + * A DayPickerView customized for {@link SimpleMonthAdapter} + */ +public class SimpleDayPickerView extends DayPickerView { + + public SimpleDayPickerView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public SimpleDayPickerView(Context context, DatePickerController controller) { + super(context, controller); + } + + @Override + public MonthAdapter createMonthAdapter(Context context, DatePickerController controller) { + return new SimpleMonthAdapter(context, controller); + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/SimpleMonthAdapter.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/SimpleMonthAdapter.java new file mode 100644 index 0000000..67f8a79 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/SimpleMonthAdapter.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.content.Context; + +/** + * An adapter for a list of {@link SimpleMonthView} items. + */ +public class SimpleMonthAdapter extends MonthAdapter { + + public SimpleMonthAdapter(Context context, DatePickerController controller) { + super(context, controller); + } + + @Override + public MonthView createMonthView(Context context) { + return new SimpleMonthView(context, null, mController); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/SimpleMonthView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/SimpleMonthView.java new file mode 100644 index 0000000..3fdadd0 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/SimpleMonthView.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Typeface; +import android.util.AttributeSet; + +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; + +public class SimpleMonthView extends MonthView { + + public SimpleMonthView(Context context, AttributeSet attr, DatePickerController controller) { + super(context, attr, controller); + } + + @Override + public void drawMonthDay(Canvas canvas, int year, int month, int day, + int x, int y, int startX, int stopX, int startY, int stopY) { + if (mSelectedDay == day) { + canvas.drawCircle(x , y - (MINI_DAY_NUMBER_TEXT_SIZE / 3), DAY_SELECTED_CIRCLE_SIZE, + mSelectedCirclePaint); + } + + if(isHighlighted(year, month, day)) { + mMonthNumPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); + } + else { + mMonthNumPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)); + } + + // If we have a mindate or maxdate, gray out the day number if it's outside the range. + if (isOutOfRange(year, month, day)) { + mMonthNumPaint.setColor(mDisabledDayTextColor); + } + else if (mSelectedDay == day) { + mMonthNumPaint.setColor(mSelectedDayTextColor); + } else if (mHasToday && mToday == day) { + mMonthNumPaint.setColor(mTodayNumberColor); + } else { + mMonthNumPaint.setColor(isHighlighted(year, month, day) ? mHighlightedDayTextColor : mDayTextColor); + } + + canvas.drawText(LanguageUtils. + getPersianNumbers(String.format("%d", day)), x, y, mMonthNumPaint); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/TextViewWithCircularIndicator.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/TextViewWithCircularIndicator.java new file mode 100644 index 0000000..33696c5 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/TextViewWithCircularIndicator.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.Paint.Style; +import android.support.annotation.NonNull; +import android.util.AttributeSet; + +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.R; + +/** + * A text view which, when pressed or activated, displays a colored circle around the text. + */ +public class TextViewWithCircularIndicator extends android.support.v7.widget.AppCompatTextView { + + private static final int SELECTED_CIRCLE_ALPHA = 255; + + private Paint mCirclePaint = new Paint(); + + private int mCircleColor; + private String mItemIsSelectedText; + + private boolean mDrawCircle; + + public TextViewWithCircularIndicator(Context context, AttributeSet attrs) { + super(context, attrs); + Resources res = context.getResources(); + mCircleColor = res.getColor(R.color.mdtp_accent_color); + int mRadius = res.getDimensionPixelOffset(R.dimen.mdtp_month_select_circle_radius); + mItemIsSelectedText = context.getResources().getString(R.string.mdtp_item_is_selected); + + init(); + } + + private void init() { + mCirclePaint.setFakeBoldText(true); + mCirclePaint.setAntiAlias(true); + mCirclePaint.setColor(mCircleColor); + mCirclePaint.setTextAlign(Align.CENTER); + mCirclePaint.setStyle(Style.FILL); + mCirclePaint.setAlpha(SELECTED_CIRCLE_ALPHA); + } + + public void drawIndicator(boolean drawCircle) { + mDrawCircle = drawCircle; + } + + @Override + public void onDraw(@NonNull Canvas canvas) { + if (mDrawCircle) { + final int width = getWidth(); + final int height = getHeight(); + int radius = Math.min(width, height) / 2; + canvas.drawCircle(width / 2, height / 2, radius, mCirclePaint); + } + setSelected(mDrawCircle); + super.onDraw(canvas); + } + + @Override + public CharSequence getContentDescription() { + String itemText = LanguageUtils.getPersianNumbers(getText().toString()); + if (mDrawCircle) { + return String.format(mItemIsSelectedText, itemText); + } else { + return itemText; + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/YearPickerView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/YearPickerView.java new file mode 100644 index 0000000..0a74c30 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/date/YearPickerView.java @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.date; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.drawable.StateListDrawable; +import android.support.annotation.NonNull; +import android.view.View; +import android.view.ViewGroup; +import android.view.accessibility.AccessibilityEvent; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +import java.util.ArrayList; +import java.util.List; + +import mohammadaminha.com.widgets.Date_Picker.date.DatePickerDialog.OnDateChangedListener; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.R; + +/** + * Displays a selectable list of years. + */ +public class YearPickerView extends ListView implements OnItemClickListener, OnDateChangedListener { + private static final String TAG = "YearPickerView"; + + private DatePickerController mController; + private YearAdapter mAdapter; + private int mViewSize; + private int mChildSize; + private TextViewWithCircularIndicator mSelectedView; + + /** + * @param context + */ + public YearPickerView(Context context, DatePickerController controller) { + super(context); + mController = controller; + mController.registerOnDateChangedListener(this); + ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, + LayoutParams.WRAP_CONTENT); + setLayoutParams(frame); + Resources res = context.getResources(); + mViewSize = res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height); + mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height); + setVerticalFadingEdgeEnabled(true); + setFadingEdgeLength(mChildSize / 3); + init(context); + setOnItemClickListener(this); + setSelector(new StateListDrawable()); + setDividerHeight(0); + onDateChanged(); + } + + private void init(Context context) { + ArrayList years = new ArrayList<>(); + for (int year = mController.getMinYear(); year <= mController.getMaxYear(); year++) { + years.add(String.format("%d", year)); + } + years = LanguageUtils.getPersianNumbers(years); + mAdapter = new YearAdapter(context, R.layout.mdtp_year_label_text_view, years); + setAdapter(mAdapter); + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + mController.tryVibrate(); + TextViewWithCircularIndicator clickedView = (TextViewWithCircularIndicator) view; + if (clickedView != null) { + if (clickedView != mSelectedView) { + if (mSelectedView != null) { + mSelectedView.drawIndicator(false); + mSelectedView.requestLayout(); + } + clickedView.drawIndicator(true); + clickedView.requestLayout(); + mSelectedView = clickedView; + } + mController.onYearSelected(getYearFromTextView(clickedView)); + mAdapter.notifyDataSetChanged(); + } + } + + private static int getYearFromTextView(TextViewWithCircularIndicator view) { + return Integer.valueOf(LanguageUtils.getLatinNumbers(view.getText().toString())); + } + + private class YearAdapter extends ArrayAdapter { + + public YearAdapter(Context context, int resource, List objects) { + super(context, resource, objects); + } + + @NonNull + @Override + public View getView(int position, View convertView, @NonNull ViewGroup parent) { + TextViewWithCircularIndicator v = (TextViewWithCircularIndicator) + super.getView(position, convertView, parent); + v.requestLayout(); + int year = getYearFromTextView(v); + boolean selected = mController.getSelectedDay().year == year; + v.drawIndicator(selected); + if (selected) { + mSelectedView = v; + } + return v; + } + } + + private void postSetSelectionCentered(final int position) { + postSetSelectionFromTop(position, mViewSize / 2 - mChildSize / 2); + } + + public void postSetSelectionFromTop(final int position, final int offset) { + post(new Runnable() { + + @Override + public void run() { + setSelectionFromTop(position, offset); + requestLayout(); + } + }); + } + + public int getFirstPositionOffset() { + final View firstChild = getChildAt(0); + if (firstChild == null) { + return 0; + } + return firstChild.getTop(); + } + + @Override + public void onDateChanged() { + mAdapter.notifyDataSetChanged(); + postSetSelectionCentered(mController.getSelectedDay().year - mController.getMinYear()); + } + + @Override + public void onInitializeAccessibilityEvent(AccessibilityEvent event) { + super.onInitializeAccessibilityEvent(event); + if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) { + event.setFromIndex(0); + event.setToIndex(0); + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/DatePickerController.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/DatePickerController.java new file mode 100644 index 0000000..58ec6bc --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/DatePickerController.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.multidate; + +import java.util.ArrayList; + +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; + +/** + * Controller class to communicate among the various components of the date picker dialog. + */ +public interface DatePickerController { + + void onYearSelected(int year); + + void onDaysOfMonthSelected(ArrayList selectedDays); + + void registerOnDateChangedListener(MultiDatePickerDialog.OnDateChangedListener listener); + + void unregisterOnDateChangedListener(MultiDatePickerDialog.OnDateChangedListener listener); + + ArrayList getSelectedDays(); + + void setSelectedDays(ArrayList selectedDays); + + boolean isThemeDark(); + + PersianCalendar[] getHighlightedDays(); + + PersianCalendar[] getSelectableDays(); + + int getFirstDayOfWeek(); + + int getMinYear(); + + int getMaxYear(); + + int getSelectedYear(); + + PersianCalendar getMinDate(); + + PersianCalendar getMaxDate(); + + void tryVibrate(); +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/DayPickerView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/DayPickerView.java new file mode 100644 index 0000000..b5133d6 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/DayPickerView.java @@ -0,0 +1,512 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.multidate; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.support.annotation.NonNull; +import android.util.AttributeSet; +import android.util.Log; +import android.view.View; +import android.view.ViewConfiguration; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityNodeInfo; +import android.widget.AbsListView; +import android.widget.AbsListView.OnScrollListener; +import android.widget.ListView; + +import mohammadaminha.com.widgets.Date_Picker.Utils; +import mohammadaminha.com.widgets.Date_Picker.multidate.MultiDatePickerDialog.OnDateChangedListener; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; + +/** + * This displays a list of months in a calendar format with selectable days. + */ +public abstract class DayPickerView extends ListView implements OnScrollListener, + OnDateChangedListener { + + private static final String TAG = "MonthFragment"; + + // Affects when the month selection will change while scrolling up + protected static final int SCROLL_HYST_WEEKS = 2; + // How long the GoTo fling animation should last + private static final int GOTO_SCROLL_DURATION = 250; + // How long to wait after receiving an onScrollStateChanged notification + // before acting on it + private static final int SCROLL_CHANGE_DELAY = 40; + // The number of days to display in each week + public static final int DAYS_PER_WEEK = 7; + private static final int LIST_TOP_OFFSET = -1; // so that the top line will be + // under the separator + // You can override these numbers to get a different appearance + protected int mNumWeeks = 6; + protected boolean mShowWeekNumber = false; + protected int mDaysPerWeek = 7; + // + // These affect the scroll speed and feel + private float mFriction = 1.0f; + + private Context mContext; + private Handler mHandler; + + // highlighted time + private MonthAdapter.CalendarDay mSelectedDay = new MonthAdapter.CalendarDay(); + private MonthAdapter mAdapter; + + private MonthAdapter.CalendarDay mTempDay = new MonthAdapter.CalendarDay(); + + // When the week starts; numbered like Time. (e.g. SUNDAY=0). + protected int mFirstDayOfWeek; + // The last name announced by accessibility + protected CharSequence mPrevMonthName; + // which month should be displayed/highlighted [0-11] + private int mCurrentMonthDisplayed; + // used for tracking during a scroll + private long mPreviousScrollPosition; + // used for tracking what state listview is in + private int mPreviousScrollState = OnScrollListener.SCROLL_STATE_IDLE; + // used for tracking what state listview is in + private int mCurrentScrollState = OnScrollListener.SCROLL_STATE_IDLE; + + private DatePickerController mController; + private boolean mPerformingScroll; + + public DayPickerView(Context context, AttributeSet attrs) { + super(context, attrs); + init(context); + } + + DayPickerView(Context context, DatePickerController controller) { + super(context); + init(context); + setController(controller); + } + + private void setController(DatePickerController controller) { + mController = controller; + mController.registerOnDateChangedListener(this); + refreshAdapter(); + onDateChanged(); + } + + private void init(Context context) { + mHandler = new Handler(); + setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + setDrawSelectorOnTop(false); + + mContext = context; + setUpListView(); + } + + public void onChange() { + refreshAdapter(); + } + + /** + * Creates a new adapter if necessary and sets up its parameters. Override + * this method to provide a custom adapter. + */ + private void refreshAdapter() { + if (mAdapter == null) { + mAdapter = createMonthAdapter(getContext(), mController); +// } else { +// mAdapter.setSelectedDay(mSelectedDay); +// } + // refresh the view with the new parameters + setAdapter(mAdapter); + } + } + + protected abstract MonthAdapter createMonthAdapter(Context context, + DatePickerController controller); + + /* + * Sets all the required fields for the list view. Override this method to + * set a different list view behavior. + */ + private void setUpListView() { + // Transparent background on scroll + setCacheColorHint(0); + // No dividers + setDivider(null); + // Items are clickable + setItemsCanFocus(true); + // The thumb gets in the way, so disable it + setFastScrollEnabled(false); + setVerticalScrollBarEnabled(false); + setOnScrollListener(this); + setFadingEdgeLength(0); + // Make the scrolling behavior nicer + setFriction(ViewConfiguration.getScrollFriction() * mFriction); + } + + /** + * This moves to the specified time in the view. If the time is not already + * in range it will move the list so that the first of the month containing + * the time is at the top of the view. If the new time is already in view + * the list will not be scrolled unless forceScroll is true. This time may + * optionally be highlighted as selected as well. + * + * @param day The day to move to + * @param animate Whether to scroll to the given time or just redraw at the + * new location + * @param setSelected Whether to set the given time as selected + * @param forceScroll Whether to recenter even if the time is already + * visible + * @return Whether or not the view animated to the new location + */ + private void goTo(MonthAdapter.CalendarDay day, boolean animate, boolean setSelected, boolean forceScroll) { + + // Set the selected day + if (setSelected) { + mSelectedDay.set(day); + } + + mTempDay.set(day); + final int position = (day.year - mController.getMinYear()) + * MonthAdapter.MONTHS_IN_YEAR + day.month; + + View child; + int i = 0; + int top ; + // Find a child that's completely in the view + do { + child = getChildAt(i++); + if (child == null) { + break; + } + top = child.getTop(); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "child at " + (i - 1) + " has top " + top); + } + } while (top < 0); + + // Compute the first and last position visible + int selectedPosition; + if (child != null) { + selectedPosition = getPositionForView(child); + } else { + selectedPosition = 0; + } + + /*if (setSelected) { + mAdapter.setSelectedDay(mSelectedDay); + }*/ + + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "GoTo position " + position); + } + // Check if the selected day is now outside of our visible range + // and if so scroll to the month that contains it + if (position != selectedPosition || forceScroll) { + setMonthDisplayed(mTempDay); + mPreviousScrollState = OnScrollListener.SCROLL_STATE_FLING; + if (animate) { + smoothScrollToPositionFromTop( + position, LIST_TOP_OFFSET, GOTO_SCROLL_DURATION); + } else { + postSetSelection(position); + } + } else if (setSelected) { + setMonthDisplayed(mSelectedDay); + } + } + + public void postSetSelection(final int position) { + clearFocus(); + post(new Runnable() { + + @Override + public void run() { + setSelection(position); + } + }); + onScrollStateChanged(this, OnScrollListener.SCROLL_STATE_IDLE); + } + + /** + * Updates the title and selected month if the view has moved to a new + * month. + */ + @Override + public void onScroll( + AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { + MonthView child = (MonthView) view.getChildAt(0); + if (child == null) { + return; + } + + // Figure out where we are + mPreviousScrollPosition = (long) (view.getFirstVisiblePosition() * child.getHeight() - child.getBottom()); + mPreviousScrollState = mCurrentScrollState; + } + + /** + * Sets the month displayed at the top of this view based on time. Override + * to add custom events when the title is changed. + */ + private void setMonthDisplayed(MonthAdapter.CalendarDay date) { + mCurrentMonthDisplayed = date.month; + invalidateViews(); + } + + @Override + public void onScrollStateChanged(AbsListView view, int scrollState) { + // use a post to prevent re-entering onScrollStateChanged before it + // exits + mScrollStateChangedRunnable.doScrollStateChange(view, scrollState); + } + + private ScrollStateRunnable mScrollStateChangedRunnable = new ScrollStateRunnable(); + + protected class ScrollStateRunnable implements Runnable { + private int mNewState; + + /** + * Sets up the runnable with a short delay in case the scroll state + * immediately changes again. + * + * @param view The list view that changed state + * @param scrollState The new state it changed to + */ + public void doScrollStateChange(AbsListView view, int scrollState) { + mHandler.removeCallbacks(this); + mNewState = scrollState; + mHandler.postDelayed(this, SCROLL_CHANGE_DELAY); + } + + @Override + public void run() { + mCurrentScrollState = mNewState; + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, + "new scroll state: " + mNewState + " old state: " + mPreviousScrollState); + } + // Fix the position after a scroll or a fling ends + if (mNewState == OnScrollListener.SCROLL_STATE_IDLE + && mPreviousScrollState != OnScrollListener.SCROLL_STATE_IDLE + && mPreviousScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { + mPreviousScrollState = mNewState; + int i = 0; + View child = getChildAt(i); + while (child != null && child.getBottom() <= 0) { + child = getChildAt(++i); + } + if (child == null) { + // The view is no longer visible, just return + return; + } + int firstPosition = getFirstVisiblePosition(); + int lastPosition = getLastVisiblePosition(); + boolean scroll = firstPosition != 0 && lastPosition != getCount() - 1; + final int top = child.getTop(); + final int bottom = child.getBottom(); + final int midpoint = getHeight() / 2; + if (scroll && top < LIST_TOP_OFFSET) { + if (bottom > midpoint) { + smoothScrollBy(top, GOTO_SCROLL_DURATION); + } else { + smoothScrollBy(bottom, GOTO_SCROLL_DURATION); + } + } + } else { + mPreviousScrollState = mNewState; + } + } + } + + /** + * Gets the position of the view that is most prominently displayed within the list view. + */ + public int getMostVisiblePosition() { + final int firstPosition = getFirstVisiblePosition(); + final int height = getHeight(); + + int maxDisplayedHeight = 0; + int mostVisibleIndex = 0; + int i = 0; + int bottom = 0; + while (bottom < height) { + View child = getChildAt(i); + if (child == null) { + break; + } + bottom = child.getBottom(); + int displayedHeight = Math.min(bottom, height) - Math.max(0, child.getTop()); + if (displayedHeight > maxDisplayedHeight) { + mostVisibleIndex = i; + maxDisplayedHeight = displayedHeight; + } + i++; + } + return firstPosition + mostVisibleIndex; + } + + @Override + public void onDateChanged() { + PersianCalendar persianCalendar = new PersianCalendar(mController.getSelectedDays() + .get(mController.getSelectedDays().size() - 1).getTimeInMillis()); + persianCalendar.setPersianDate(mController.getSelectedYear() + , persianCalendar.getPersianMonth(), persianCalendar.getPersianDay()); + goTo(new MonthAdapter.CalendarDay(persianCalendar), false, true, true); + } + + /** + * Attempts to return the date that has accessibility focus. + * + * @return The date that has accessibility focus, or {@code null} if no date + * has focus. + */ + private MonthAdapter.CalendarDay findAccessibilityFocus() { + final int childCount = getChildCount(); + for (int i = 0; i < childCount; i++) { + final View child = getChildAt(i); + if (child instanceof MonthView) { + final MonthAdapter.CalendarDay focus = ((MonthView) child).getAccessibilityFocus(); + if (focus != null) { + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) { + // Clear focus to avoid ListView bug in Jelly Bean MR1. + ((MonthView) child).clearAccessibilityFocus(); + } + return focus; + } + } + } + + return null; + } + + /** + * Attempts to restore accessibility focus to a given date. No-op if + * {@code day} is {@code null}. + * + * @param day The date that should receive accessibility focus + * @return {@code true} if focus was restored + */ + private void restoreAccessibilityFocus(MonthAdapter.CalendarDay day) { + if (day == null) { + return; + } + + final int childCount = getChildCount(); + for (int i = 0; i < childCount; i++) { + final View child = getChildAt(i); + if (child instanceof MonthView) { + if (((MonthView) child).restoreAccessibilityFocus(day)) { + return; + } + } + } + + } + + @Override + protected void layoutChildren() { + final MonthAdapter.CalendarDay focusedDay = findAccessibilityFocus(); + super.layoutChildren(); + if (mPerformingScroll) { + mPerformingScroll = false; + } else { + restoreAccessibilityFocus(focusedDay); + } + } + + @Override + public void onInitializeAccessibilityEvent(@NonNull AccessibilityEvent event) { + super.onInitializeAccessibilityEvent(event); + event.setItemCount(-1); + } + + private static String getMonthAndYearString(MonthAdapter.CalendarDay day) { + PersianCalendar mPersianCalendar = new PersianCalendar(); + mPersianCalendar.setPersianDate(day.year, day.month, day.day); + + String sbuf = ""; + sbuf += mPersianCalendar.getPersianMonthName(); + sbuf += " "; + sbuf += mPersianCalendar.getPersianYear(); + return sbuf; + } + + /** + * Necessary for accessibility, to ensure we support "scrolling" forward and backward + * in the month list. + */ + @Override + @SuppressWarnings("deprecation") + public void onInitializeAccessibilityNodeInfo(@NonNull AccessibilityNodeInfo info) { + super.onInitializeAccessibilityNodeInfo(info); + if (Build.VERSION.SDK_INT >= 21) { + info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD); + info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD); + } else { + info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); + info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); + } + } + + /** + * When scroll forward/backward events are received, announce the newly scrolled-to month. + */ + @SuppressLint("NewApi") + @Override + public boolean performAccessibilityAction(int action, Bundle arguments) { + if (action != AccessibilityNodeInfo.ACTION_SCROLL_FORWARD && + action != AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) { + return super.performAccessibilityAction(action, arguments); + } + + // Figure out what month is showing. + int firstVisiblePosition = getFirstVisiblePosition(); + int month = firstVisiblePosition % 12; + int year = firstVisiblePosition / 12 + mController.getMinYear(); + MonthAdapter.CalendarDay day = new MonthAdapter.CalendarDay(year, month, 1); + + // Scroll either forward or backward one month. + if (action == AccessibilityNodeInfo.ACTION_SCROLL_FORWARD) { + day.month++; + if (day.month == 12) { + day.month = 0; + day.year++; + } + } else if (action == AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) { + View firstVisibleView = getChildAt(0); + // If the view is fully visible, jump one month back. Otherwise, we'll just jump + // to the first day of first visible month. + if (firstVisibleView != null && firstVisibleView.getTop() >= -1) { + // There's an off-by-one somewhere, so the top of the first visible item will + // actually be -1 when it's at the exact top. + day.month--; + if (day.month == -1) { + day.month = 11; + day.year--; + } + } + } + + // Go to that month. + Utils.tryAccessibilityAnnounce(this, + LanguageUtils.getPersianNumbers(getMonthAndYearString(day))); + goTo(day, true, false, true); + mPerformingScroll = true; + return true; + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/MonthAdapter.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/MonthAdapter.java new file mode 100644 index 0000000..4fb7319 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/MonthAdapter.java @@ -0,0 +1,247 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.multidate; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AbsListView.LayoutParams; +import android.widget.BaseAdapter; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; + +import mohammadaminha.com.widgets.Date_Picker.multidate.MonthView.OnDayClickListener; +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; + +/** + * An adapter for a list of {@link MonthView} items. + */ +public abstract class MonthAdapter extends BaseAdapter implements OnDayClickListener { + + private static final String TAG = "SimpleMonthAdapter"; + + private Context mContext; + final DatePickerController mController; + + private ArrayList mSelectedDays; + + protected static int WEEK_7_OVERHANG_HEIGHT = 7; + static final int MONTHS_IN_YEAR = 12; + + /** + * A convenience class to represent a specific date. + */ + public static class CalendarDay { + private PersianCalendar mPersianCalendar; + int year; + int month; + int day; + + public CalendarDay() { + setTime(System.currentTimeMillis()); + } + + public CalendarDay(long timeInMillis) { + setTime(timeInMillis); + } + + public CalendarDay(PersianCalendar calendar) { + year = calendar.getPersianYear(); + month = calendar.getPersianMonth(); + day = calendar.getPersianDay(); + } + + public CalendarDay(int year, int month, int day) { + setDay(year, month, day); + } + + public void set(CalendarDay date) { + year = date.year; + month = date.month; + day = date.day; + } + + public void setDay(int year, int month, int day) { + this.year = year; + this.month = month; + this.day = day; + } + + private void setTime(long timeInMillis) { + if (mPersianCalendar == null) { + mPersianCalendar = new PersianCalendar(); + } + mPersianCalendar.setTimeInMillis(timeInMillis); + month = mPersianCalendar.getPersianMonth(); + year = mPersianCalendar.getPersianYear(); + day = mPersianCalendar.getPersianDay(); + } + + public boolean same(CalendarDay date) { + return date.day == day && date.year == year && date.month == month; + } + + public int getYear() { + return year; + } + + public int getMonth() { + return month; + } + + public int getDay() { + return day; + } + + public PersianCalendar getPersianCalendar() { + if (mPersianCalendar != null) + return mPersianCalendar; + else { + mPersianCalendar = new PersianCalendar(); + mPersianCalendar.setPersianDate(year, month, day); + return mPersianCalendar; + } + } + } + + MonthAdapter(Context context, + DatePickerController controller) { + mContext = context; + mController = controller; + mSelectedDays = mController.getSelectedDays(); + /*ArrayList persianCalendars = mController.getSelectedDays(); + for (PersianCalendar row : persianCalendars) + mSelectedDays.add(new CalendarDay(row));*/ + } + + @Override + public int getCount() { + return ((mController.getMaxYear() - mController.getMinYear()) + 1) * MONTHS_IN_YEAR; + } + + @Override + public Object getItem(int position) { + return null; + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public boolean hasStableIds() { + return true; + } + + @SuppressLint("NewApi") + @SuppressWarnings("unchecked") + @Override + public View getView(int position, View convertView, ViewGroup parent) { + MonthView v; + HashMap drawingParams = null; + if (convertView != null) { + v = (MonthView) convertView; + // We store the drawing parameters in the view so it can be recycled + drawingParams = (HashMap) v.getTag(); + } else { + v = createMonthView(mContext); + // Set up the new view + LayoutParams params = new LayoutParams( + LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + v.setLayoutParams(params); + v.setClickable(true); + v.setOnDayClickListener(this); + } + if (drawingParams == null) { + drawingParams = new HashMap<>(); + } + drawingParams.clear(); + + final int month = position % MONTHS_IN_YEAR; + final int year = position / MONTHS_IN_YEAR + mController.getMinYear(); + + ArrayList days = new ArrayList<>(); + for (PersianCalendar persianCalendar : mSelectedDays) + if (isSelectedDayInMonth(new CalendarDay(persianCalendar), year, month)) + days.add(persianCalendar.getPersianDay()); + + + // Invokes requestLayout() to ensure that the recycled view is set with the appropriate + // height/number of weeks before being displayed. + v.reuse(); + + drawingParams.put(MonthView.VIEW_PARAMS_SELECTED_DAYS, days); + drawingParams.put(MonthView.VIEW_PARAMS_YEAR, year); + drawingParams.put(MonthView.VIEW_PARAMS_MONTH, month); + drawingParams.put(MonthView.VIEW_PARAMS_WEEK_START, mController.getFirstDayOfWeek()); + v.setMonthParams(drawingParams); + v.invalidate(); + return v; + } + + protected abstract MonthView createMonthView(Context context); + + private boolean isSelectedDayInMonth(CalendarDay selectedDay, int year, int month) { + return selectedDay.year == year && selectedDay.month == month; + } + + + @Override + public void onDayClick(MonthView view, CalendarDay day) { + if (day != null) { + onDayTapped(day); + } + } + + /** + * Maintains the same hour/min/sec but moves the day to the tapped day. + * + * @param day The day that was tapped + */ + private void onDayTapped(CalendarDay day) { + mController.tryVibrate(); + notifySelectedDays(day); + notifyDataSetChanged(); + mController.onDaysOfMonthSelected(mSelectedDays); + } + + private void notifySelectedDays(CalendarDay day) { + PersianCalendar toRemove = null; + for (PersianCalendar calendarDay : mSelectedDays) + if (day.same(new CalendarDay(calendarDay))) { + toRemove = calendarDay; + break; + } + + if (mSelectedDays.size() > 1 && toRemove != null) + mSelectedDays.remove(toRemove); + else { + mSelectedDays.add(day.getPersianCalendar()); + Collections.sort(mSelectedDays, new Comparator() { + @Override + public int compare(PersianCalendar o1, PersianCalendar o2) { + return o1.getTimeInMillis() > o2.getTimeInMillis() ? 1 : 0; + } + }); + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/MonthView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/MonthView.java new file mode 100644 index 0000000..0bbbeba --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/MonthView.java @@ -0,0 +1,836 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.multidate; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.Paint.Style; +import android.graphics.Rect; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.view.ViewCompat; +import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; +import android.support.v4.widget.ExploreByTouchHelper; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityNodeInfo; + +import java.security.InvalidParameterException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.List; + +import mohammadaminha.com.widgets.Date_Picker.TypefaceHelper; +import mohammadaminha.com.widgets.Date_Picker.Utils; +import mohammadaminha.com.widgets.Date_Picker.multidate.MonthAdapter.CalendarDay; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; +import mohammadaminha.com.widgets.R; +import mohammadaminha.com.widgets.Util; + +/** + * A calendar-like view displaying a specified month and the appropriate selectable day numbers + * within the specified month. + */ +public abstract class MonthView extends View { + private static final String TAG = "MonthView"; + + /** + * This sets the height of this week in pixels + */ + private static final String VIEW_PARAMS_HEIGHT = "height"; + /** + * This specifies the position (or weeks since the epoch) of this week. + */ + public static final String VIEW_PARAMS_MONTH = "month"; + /** + * This specifies the position (or weeks since the epoch) of this week. + */ + public static final String VIEW_PARAMS_YEAR = "year"; + /** + * This sets one of the days in this view as selected {@link Calendar#SUNDAY} + * through {@link Calendar#SATURDAY}. + */ + private static final String VIEW_PARAMS_SELECTED_DAY = "selected_day"; + public static final String VIEW_PARAMS_SELECTED_DAYS = "selected_days"; + /** + * Which day the week should start on. {@link Calendar#SUNDAY} through + * {@link Calendar#SATURDAY}. + */ + public static final String VIEW_PARAMS_WEEK_START = "week_start"; + /** + * How many days to display at a time. Days will be displayed starting with + * {@link #mWeekStart}. + */ + public static final String VIEW_PARAMS_NUM_DAYS = "num_days"; + /** + * Which month is currently in focus, as defined by {@link Calendar#MONTH} + * [0-11]. + */ + public static final String VIEW_PARAMS_FOCUS_MONTH = "focus_month"; + /** + * If this month should display week numbers. false if 0, true otherwise. + */ + public static final String VIEW_PARAMS_SHOW_WK_NUM = "show_wk_num"; + + private static final int DEFAULT_HEIGHT = 32; + private static final int MIN_HEIGHT = 10; + private static final int DEFAULT_SELECTED_DAY = -1; + private static final int DEFAULT_WEEK_START = Calendar.SATURDAY; + private static final int DEFAULT_NUM_DAYS = 7; + protected static final int DEFAULT_SHOW_WK_NUM = 0; + protected static final int DEFAULT_FOCUS_MONTH = -1; + private static final int DEFAULT_NUM_ROWS = 6; + private static final int MAX_NUM_ROWS = 6; + + private static final int SELECTED_CIRCLE_ALPHA = 255; + + private static final int DAY_SEPARATOR_WIDTH = 1; + static int MINI_DAY_NUMBER_TEXT_SIZE; + private static int MONTH_LABEL_TEXT_SIZE; + private static int MONTH_DAY_LABEL_TEXT_SIZE; + private static int MONTH_HEADER_SIZE; + static int DAY_SELECTED_CIRCLE_SIZE; + + // used for scaling to the device density + protected static float mScale = 0; + + private DatePickerController mController; + + // affects the padding on the sides of this view + private int mEdgePadding = 0; + + + Paint mMonthNumPaint; + private Paint mMonthTitlePaint; + Paint mSelectedCirclePaint; + private Paint mMonthDayLabelPaint; + + private StringBuilder mStringBuilder; + + // The Julian day of the first day displayed by this item + protected int mFirstJulianDay = -1; + // The month of the first day in this week + protected int mFirstMonth = -1; + // The month of the last day in this week + protected int mLastMonth = -1; + + private int mMonth; + + private int mYear; + // Quick reference to the width of this view, matches parent + private int mWidth; + // The height this view should draw at in pixels, set by height param + private int mRowHeight = DEFAULT_HEIGHT; + // If this view contains the today + boolean mHasToday = false; + // Which day is selected [0-6] or -1 if no day is selected + private int mSelectedDay = -1; + ArrayList mSelectedDays = new ArrayList<>(); + // Which day is today [0-6] or -1 if no day is today + int mToday = DEFAULT_SELECTED_DAY; + // Which day of the week to start on [0-6] + private int mWeekStart = DEFAULT_WEEK_START; + // How many days to display + private int mNumDays = DEFAULT_NUM_DAYS; + // The number of days + a spot for week number if it is displayed + private int mNumCells = mNumDays; + // The left edge of the selected day + protected int mSelectedLeft = -1; + // The right edge of the selected day + protected int mSelectedRight = -1; + + private PersianCalendar mPersianCalendar; + private PersianCalendar mDayLabelCalendar; + private MonthViewTouchHelper mTouchHelper; + + private int mNumRows = DEFAULT_NUM_ROWS; + + // Optional listener for handling day click actions + private OnDayClickListener mOnDayClickListener; + + // Whether to prevent setting the accessibility delegate + private boolean mLockAccessibilityDelegate; + + final int mDayTextColor; + final int mSelectedDayTextColor; + private int mMonthDayTextColor; + final int mTodayNumberColor; + final int mHighlightedDayTextColor; + final int mDisabledDayTextColor; + private int mMonthTitleColor; + + public MonthView(Context context) { + this(context, null, null); + } + + MonthView(Context context, AttributeSet attr, DatePickerController controller) { + super(context, attr); + mController = controller; + Resources res = context.getResources(); + + mDayLabelCalendar = new PersianCalendar(); + mPersianCalendar = new PersianCalendar(); + + + boolean darkTheme = mController != null && mController.isThemeDark(); + if(darkTheme) { + mDayTextColor = res.getColor(R.color.mdtp_date_picker_text_normal_dark_theme); + mMonthDayTextColor = res.getColor(R.color.mdtp_date_picker_month_day_dark_theme); + mDisabledDayTextColor = res.getColor(R.color.mdtp_date_picker_text_disabled_dark_theme); + mHighlightedDayTextColor = res.getColor(R.color.mdtp_date_picker_text_highlighted_dark_theme); + } + else { + mDayTextColor = res.getColor(R.color.mdtp_date_picker_text_normal); + mMonthDayTextColor = res.getColor(R.color.mdtp_date_picker_month_day); + mDisabledDayTextColor = res.getColor(R.color.mdtp_date_picker_text_disabled); + mHighlightedDayTextColor = res.getColor(R.color.mdtp_date_picker_text_highlighted); + } + mSelectedDayTextColor = res.getColor(R.color.mdtp_white); + mTodayNumberColor = res.getColor(R.color.mdtp_accent_color); + mMonthTitleColor = res.getColor(R.color.mdtp_white); + + mStringBuilder = new StringBuilder(50); + + MINI_DAY_NUMBER_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_day_number_size); + MONTH_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_label_size); + MONTH_DAY_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_day_label_text_size); + MONTH_HEADER_SIZE = res.getDimensionPixelOffset(R.dimen.mdtp_month_list_item_header_height); + DAY_SELECTED_CIRCLE_SIZE = res + .getDimensionPixelSize(R.dimen.mdtp_day_number_select_circle_radius); + + mRowHeight = (res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height) + - getMonthHeaderSize()) / MAX_NUM_ROWS; + + // Set up accessibility components. + mTouchHelper = getMonthViewTouchHelper(); + ViewCompat.setAccessibilityDelegate(this, mTouchHelper); + ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES); + mLockAccessibilityDelegate = true; + + // Sets up any standard paints that will be used + initView(); + } + + public void setDatePickerController(DatePickerController controller) { + mController = controller; + } + + private MonthViewTouchHelper getMonthViewTouchHelper() { + return new MonthViewTouchHelper(this); + } + + @Override + public void setAccessibilityDelegate(AccessibilityDelegate delegate) { + // Workaround for a JB MR1 issue where accessibility delegates on + // top-level ListView items are overwritten. + if (!mLockAccessibilityDelegate) { + super.setAccessibilityDelegate(delegate); + } + } + + public void setOnDayClickListener(OnDayClickListener listener) { + mOnDayClickListener = listener; + } + + @Override + public boolean dispatchHoverEvent(@NonNull MotionEvent event) { + // First right-of-refusal goes the touch exploration helper. + return mTouchHelper.dispatchHoverEvent(event) || super.dispatchHoverEvent(event); + } + + @Override + public boolean onTouchEvent(@NonNull MotionEvent event) { + switch (event.getAction()) { + case MotionEvent.ACTION_UP: + final int day = getDayFromLocation(event.getX(), event.getY()); + if (day >= 0) { + onDayClick(day); + } + break; + } + return true; + } + + /** + * Sets up the text and style properties for painting. Override this if you + * want to use a different paint. + */ + private void initView() { + mMonthTitlePaint = new Paint(); + mMonthTitlePaint.setFakeBoldText(true); + mMonthTitlePaint.setAntiAlias(true); + mMonthTitlePaint.setTextSize(MONTH_LABEL_TEXT_SIZE); + + mMonthTitlePaint.setTypeface(Util.getTypeFace()); + mMonthTitlePaint.setColor(mDayTextColor); + mMonthTitlePaint.setTextAlign(Align.CENTER); + mMonthTitlePaint.setStyle(Style.FILL); + + mSelectedCirclePaint = new Paint(); + mSelectedCirclePaint.setFakeBoldText(true); + mSelectedCirclePaint.setAntiAlias(true); + mSelectedCirclePaint.setColor(mTodayNumberColor); + mSelectedCirclePaint.setTextAlign(Align.CENTER); + mSelectedCirclePaint.setStyle(Style.FILL); + mSelectedCirclePaint.setAlpha(SELECTED_CIRCLE_ALPHA); + + mMonthDayLabelPaint = new Paint(); + mMonthDayLabelPaint.setAntiAlias(true); + mMonthDayLabelPaint.setTextSize(MONTH_DAY_LABEL_TEXT_SIZE); + mMonthDayLabelPaint.setColor(mMonthDayTextColor); + mMonthDayLabelPaint.setTypeface(TypefaceHelper.get(getContext(),"Roboto-Medium")); + mMonthDayLabelPaint.setStyle(Style.FILL); + mMonthDayLabelPaint.setTextAlign(Align.CENTER); + mMonthDayLabelPaint.setFakeBoldText(true); + + mMonthNumPaint = new Paint(); + mMonthNumPaint.setAntiAlias(true); + mMonthNumPaint.setTextSize(MINI_DAY_NUMBER_TEXT_SIZE); + mMonthNumPaint.setStyle(Style.FILL); + mMonthNumPaint.setTextAlign(Align.CENTER); + mMonthNumPaint.setFakeBoldText(false); + } + + @Override + protected void onDraw(Canvas canvas) { + drawMonthTitle(canvas); + drawMonthDayLabels(canvas); + drawMonthNums(canvas); + } + + private int mDayOfWeekStart = 0; + + /** + * Sets all the parameters for displaying this week. The only required + * parameter is the week number. Other parameters have a default value and + * will only update if a new value is included, except for focus month, + * which will always default to no focus month if no value is passed in. See + * {@link #VIEW_PARAMS_HEIGHT} for more info on parameters. + * + * @param params A map of the new parameters, see + * {@link #VIEW_PARAMS_HEIGHT} + */ + public void setMonthParams(HashMap params) { + if (!params.containsKey(VIEW_PARAMS_MONTH) && !params.containsKey(VIEW_PARAMS_YEAR)) { + throw new InvalidParameterException("You must specify month and year for this view"); + } + setTag(params); + // We keep the current value for any params not present + if (params.containsKey(VIEW_PARAMS_HEIGHT)) { + mRowHeight = (int) params.get(VIEW_PARAMS_HEIGHT); + if (mRowHeight < MIN_HEIGHT) { + mRowHeight = MIN_HEIGHT; + } + } + if (params.containsKey(VIEW_PARAMS_SELECTED_DAY)) { + mSelectedDay = (int) params.get(VIEW_PARAMS_SELECTED_DAY); + } + if (params.containsKey(VIEW_PARAMS_SELECTED_DAYS)) { + mSelectedDays = (ArrayList) params.get(VIEW_PARAMS_SELECTED_DAYS); + } + // Allocate space for caching the day numbers and focus values + mMonth = (int) params.get(VIEW_PARAMS_MONTH); + mYear = (int) params.get(VIEW_PARAMS_YEAR); + + // Figure out what day today is + //final Time today = new Time(Time.getCurrentTimezone()); + //today.setToNow(); + final PersianCalendar today = new PersianCalendar(); + mHasToday = false; + mToday = -1; + + mPersianCalendar.setPersianDate(mYear, mMonth, 1); + mDayOfWeekStart = mPersianCalendar.get(Calendar.DAY_OF_WEEK); + + if (params.containsKey(VIEW_PARAMS_WEEK_START)) { + mWeekStart = (int) params.get(VIEW_PARAMS_WEEK_START); + } else { + mWeekStart = Calendar.SATURDAY; + } + + mNumCells = Utils.getDaysInMonth(mMonth, mYear); + for (int i = 0; i < mNumCells; i++) { + final int day = i + 1; + if (sameDay(day, today)) { + mHasToday = true; + mToday = day; + } + } + mNumRows = calculateNumRows(); + + // Invalidate cached accessibility information. + mTouchHelper.invalidateRoot(); + } + + public void setSelectedDay(int day) { + mSelectedDay = day; + } + + public void reuse() { + mNumRows = DEFAULT_NUM_ROWS; + requestLayout(); + } + + private int calculateNumRows() { + int offset = findDayOffset(); + int dividend = (offset + mNumCells) / mNumDays; + int remainder = (offset + mNumCells) % mNumDays; + return (dividend + (remainder > 0 ? 1 : 0)); + } + + private boolean sameDay(int day, PersianCalendar today) { + return mYear == today.getPersianYear() && + mMonth == today.getPersianMonth() && + day == today.getPersianDay(); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mRowHeight * mNumRows + + getMonthHeaderSize() + 5); + } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + mWidth = w; + + // Invalidate cached accessibility information. + mTouchHelper.invalidateRoot(); + } + + public int getMonth() { + return mMonth; + } + + public int getYear() { + return mYear; + } + + /** + * A wrapper to the MonthHeaderSize to allow override it in children + */ + private int getMonthHeaderSize() { + return MONTH_HEADER_SIZE; + } + + private String getMonthAndYearString() { + mStringBuilder.setLength(0); + return LanguageUtils.getPersianNumbers( + mPersianCalendar.getPersianMonthName() + " " + mPersianCalendar.getPersianYear()); + } + + private void drawMonthTitle(Canvas canvas) { + int x = (mWidth + 2 * mEdgePadding) / 2; + int y = (getMonthHeaderSize() - MONTH_DAY_LABEL_TEXT_SIZE) / 2; + canvas.drawText(getMonthAndYearString(), x, y, mMonthTitlePaint); + } + + private void drawMonthDayLabels(Canvas canvas) { + int y = getMonthHeaderSize() - (MONTH_DAY_LABEL_TEXT_SIZE / 2); + int dayWidthHalf = (mWidth - mEdgePadding * 2) / (mNumDays * 2); + + for (int i = 0; i < mNumDays; i++) { + int calendarDay = (i + mWeekStart) % mNumDays; + int x = (2 * i + 1) * dayWidthHalf + mEdgePadding; + mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay); + String localWeekDisplayName = mDayLabelCalendar.getPersianWeekDayName(); // TODO: RTLize + String weekString = localWeekDisplayName.substring(0, 1); + canvas.drawText(weekString, x, y, mMonthDayLabelPaint); + } + } + + /** + * Draws the week and month day numbers for this week. Override this method + * if you need different placement. + * + * @param canvas The canvas to draw on + */ + private void drawMonthNums(Canvas canvas) { + int y = (((mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2) - DAY_SEPARATOR_WIDTH) + + getMonthHeaderSize(); + final float dayWidthHalf = (mWidth - mEdgePadding * 2) / (mNumDays * 2.0f); + int j = findDayOffset(); + for (int dayNumber = 1; dayNumber <= mNumCells; dayNumber++) { + final int x = (int)((2 * j + 1) * dayWidthHalf + mEdgePadding); + + int yRelativeToDay = (mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2 - DAY_SEPARATOR_WIDTH; + + final int startX = (int)(x - dayWidthHalf); + final int stopX = (int)(x + dayWidthHalf); + final int startY = y - yRelativeToDay; + final int stopY = startY + mRowHeight; + + drawMonthDay(canvas, mYear, mMonth, dayNumber, x, y, startX, stopX, startY, stopY); + + j++; + if (j == mNumDays) { + j = 0; + y += mRowHeight; + } + } + } + + /** + * This method should draw the month day. Implemented by sub-classes to allow customization. + * + * @param canvas The canvas to draw on + * @param year The year of this month day + * @param month The month of this month day + * @param day The day number of this month day + * @param x The default x position to draw the day number + * @param y The default y position to draw the day number + * @param startX The left boundary of the day number rect + * @param stopX The right boundary of the day number rect + * @param startY The top boundary of the day number rect + * @param stopY The bottom boundary of the day number rect + */ + protected abstract void drawMonthDay(Canvas canvas, int year, int month, int day, + int x, int y, int startX, int stopX, int startY, int stopY); + + private int findDayOffset() { + return (mDayOfWeekStart < mWeekStart ? (mDayOfWeekStart + mNumDays) : mDayOfWeekStart) + - mWeekStart; + } + + + /** + * Calculates the day that the given x position is in, accounting for week + * number. Returns the day or -1 if the position wasn't in a day. + * + * @param x The x position of the touch event + * @return The day number, or -1 if the position wasn't in a day + */ + private int getDayFromLocation(float x, float y) { + final int day = getInternalDayFromLocation(x, y); + if (day < 1 || day > mNumCells) { + return -1; + } + return day; + } + + /** + * Calculates the day that the given x position is in, accounting for week + * number. + * + * @param x The x position of the touch event + * @return The day number + */ + private int getInternalDayFromLocation(float x, float y) { + int dayStart = mEdgePadding; + if (x < dayStart || x > mWidth - mEdgePadding) { + return -1; + } + // Selection is (x - start) / (pixels/day) == (x -s) * day / pixels + int row = (int) (y - getMonthHeaderSize()) / mRowHeight; + int column = (int) ((x - dayStart) * mNumDays / (mWidth - dayStart - mEdgePadding)); + + int day = column - findDayOffset() + 1; + day += row * mNumDays; + return day; + } + + /** + * Called when the user clicks on a day. Handles callbacks to the + * {@link OnDayClickListener} if one is set. + *

    + * If the day is out of the range set by minDate and/or maxDate, this is a no-op. + * + * @param day The day that was clicked + */ + private void onDayClick(int day) { + // If the min / max date are set, only process the click if it's a valid selection. + if (isOutOfRange(mYear, mMonth, day)) { + return; + } + + + if (mOnDayClickListener != null) { + mOnDayClickListener.onDayClick(this, new CalendarDay(mYear, mMonth, day)); + } + + // This is a no-op if accessibility is turned off. + mTouchHelper.sendEventForVirtualView(day, AccessibilityEvent.TYPE_VIEW_CLICKED); + } + + /** + * @return true if the specified year/month/day are within the selectable days or the range set by minDate and maxDate. + * If one or either have not been set, they are considered as Integer.MIN_VALUE and + * Integer.MAX_VALUE. + */ + boolean isOutOfRange(int year, int month, int day) { + if (mController.getSelectableDays() != null) { + return !isSelectable(year, month, day); + } + + if (isBeforeMin(year, month, day)) { + return true; + } + else if (isAfterMax(year, month, day)) { + return true; + } + + return false; + } + + private boolean isSelectable(int year, int month, int day) { + PersianCalendar[] selectableDays = mController.getSelectableDays(); + for (PersianCalendar c : selectableDays) { + if(year < c.getPersianYear()) break; + if(year > c.getPersianYear()) continue; + if(month < c.getPersianMonth()) break; + if(month > c.getPersianMonth()) continue; + if(day < c.getPersianDay()) break; + if(day > c.getPersianDay()) continue; + return true; + } + return false; + } + + private boolean isBeforeMin(int year, int month, int day) { + if (mController == null) { + return false; + } + PersianCalendar minDate = mController.getMinDate(); + if (minDate == null) { + return false; + } + + if (year < minDate.getPersianYear()) { + return true; + } else if (year > minDate.getPersianYear()) { + return false; + } + + if (month < minDate.getPersianMonth()) { + return true; + } else if (month > minDate.getPersianMonth()) { + return false; + } + + return day < minDate.getPersianDay(); + } + + private boolean isAfterMax(int year, int month, int day) { + if (mController == null) { + return false; + } + PersianCalendar maxDate = mController.getMaxDate(); + if (maxDate == null) { + return false; + } + + if (year > maxDate.getPersianYear()) { + return true; + } else if (year < maxDate.getPersianYear()) { + return false; + } + + if (month > maxDate.getPersianMonth()) { + return true; + } else if (month < maxDate.getPersianMonth()) { + return false; + } + + return day > maxDate.getPersianMonth(); + } + + /** + * @param year + * @param month + * @param day + * @return true if the given date should be highlighted + */ + boolean isHighlighted(int year, int month, int day) { + PersianCalendar[] highlightedDays = mController.getHighlightedDays(); + if(highlightedDays == null) return false; + for (PersianCalendar c : highlightedDays) { + if(year < c.getPersianYear()) break; + if(year > c.getPersianYear()) continue; + if(month < c.getPersianMonth()) break; + if(month > c.getPersianMonth()) continue; + if(day < c.getPersianDay()) break; + if(day > c.getPersianDay()) continue; + return true; + } + return false; + } + + /** + * @return The date that has accessibility focus, or {@code null} if no date + * has focus + */ + public CalendarDay getAccessibilityFocus() { + final int day = mTouchHelper.getFocusedVirtualView(); + if (day >= 0) { + return new CalendarDay(mYear, mMonth, day); + } + return null; + } + + /** + * Clears accessibility focus within the view. No-op if the view does not + * contain accessibility focus. + */ + public void clearAccessibilityFocus() { + mTouchHelper.clearFocusedVirtualView(); + } + + /** + * Attempts to restore accessibility focus to the specified date. + * + * @param day The date which should receive focus + * @return {@code false} if the date is not valid for this month view, or + * {@code true} if the date received focus + */ + public boolean restoreAccessibilityFocus(CalendarDay day) { + if ((day.year != mYear) || (day.month != mMonth) || (day.day > mNumCells)) { + return false; + } + mTouchHelper.setFocusedVirtualView(day.day); + return true; + } + + /** + * Provides a virtual view hierarchy for interfacing with an accessibility + * service. + */ + protected class MonthViewTouchHelper extends ExploreByTouchHelper { + + private Rect mTempRect = new Rect(); + private PersianCalendar mTempCalendar = new PersianCalendar(); + + public MonthViewTouchHelper(View host) { + super(host); + } + + public void setFocusedVirtualView(int virtualViewId) { + getAccessibilityNodeProvider(MonthView.this).performAction( + virtualViewId, AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS, null); + } + + public void clearFocusedVirtualView() { + final int focusedVirtualView = getFocusedVirtualView(); + if (focusedVirtualView != ExploreByTouchHelper.INVALID_ID) { + getAccessibilityNodeProvider(MonthView.this).performAction( + focusedVirtualView, + AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS, + null); + } + } + + @Override + protected int getVirtualViewAt(float x, float y) { + final int day = getDayFromLocation(x, y); + if (day >= 0) { + return day; + } + return ExploreByTouchHelper.INVALID_ID; + } + + @Override + protected void getVisibleVirtualViews(List virtualViewIds) { + for (int day = 1; day <= mNumCells; day++) { + virtualViewIds.add(day); + } + } + + @Override + protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) { + event.setContentDescription(getItemDescription(virtualViewId)); + } + + @Override + protected void onPopulateNodeForVirtualView(int virtualViewId, + AccessibilityNodeInfoCompat node) { + getItemBounds(virtualViewId, mTempRect); + + node.setContentDescription(getItemDescription(virtualViewId)); + node.setBoundsInParent(mTempRect); + node.addAction(AccessibilityNodeInfo.ACTION_CLICK); + + if (virtualViewId == mSelectedDay) { + node.setSelected(true); + } + + } + + @Override + protected boolean onPerformActionForVirtualView(int virtualViewId, int action, + Bundle arguments) { + switch (action) { + case AccessibilityNodeInfo.ACTION_CLICK: + onDayClick(virtualViewId); + return true; + } + + return false; + } + + /** + * Calculates the bounding rectangle of a given time object. + * + * @param day The day to calculate bounds for + * @param rect The rectangle in which to store the bounds + */ + void getItemBounds(int day, Rect rect) { + final int offsetX = mEdgePadding; + final int offsetY = getMonthHeaderSize(); + final int cellHeight = mRowHeight; + final int cellWidth = ((mWidth - (2 * mEdgePadding)) / mNumDays); + final int index = ((day - 1) + findDayOffset()); + final int row = (index / mNumDays); + final int column = (index % mNumDays); + final int x = (offsetX + (column * cellWidth)); + final int y = (offsetY + (row * cellHeight)); + + rect.set(x, y, (x + cellWidth), (y + cellHeight)); + } + + /** + * Generates a description for a given time object. Since this + * description will be spoken, the components are ordered by descending + * specificity as DAY MONTH YEAR. + * + * @param day The day to generate a description for + * @return A description of the time object + */ + CharSequence getItemDescription(int day) { + mTempCalendar.setPersianDate(mYear, mMonth, day); + final String date = LanguageUtils.getPersianNumbers(mTempCalendar.getPersianLongDate()); + + if (day == mSelectedDay) { + return getContext().getString(R.string.mdtp_item_is_selected, date); + } + + return date; + } + } + + /** + * Handles callbacks when the user clicks on a time object. + */ + interface OnDayClickListener { + void onDayClick(MonthView view, CalendarDay day); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/MultiDatePickerDialog.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/MultiDatePickerDialog.java new file mode 100644 index 0000000..3c051cf --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/MultiDatePickerDialog.java @@ -0,0 +1,650 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.multidate; + +import android.animation.ObjectAnimator; +import android.app.Activity; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.content.res.Resources; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.view.animation.AlphaAnimation; +import android.view.animation.Animation; +import android.widget.LinearLayout; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.HashSet; + +import mohammadaminha.com.widgets.Button; +import mohammadaminha.com.widgets.Date_Picker.HapticFeedbackController; +import mohammadaminha.com.widgets.Date_Picker.TypefaceHelper; +import mohammadaminha.com.widgets.Date_Picker.Utils; +import mohammadaminha.com.widgets.Date_Picker.date.AccessibleDateAnimator; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.Date_Picker.utils.PersianCalendar; +import mohammadaminha.com.widgets.R; +import mohammadaminha.com.widgets.TextView; + +/** + * Dialog allowing users to select a date. + */ +public class MultiDatePickerDialog extends DialogFragment implements + OnClickListener, DatePickerController { + + private static final String TAG = "MultiDatePickerDialog"; + + private static final int UNINITIALIZED = -1; + private static final int MONTH_AND_DAY_VIEW = 0; + private static final int YEAR_VIEW = 1; + + private static final String KEY_SELECTED_DAYS = "selectedDays"; + private static final String KEY_LIST_POSITION = "list_position"; + private static final String KEY_WEEK_START = "week_start"; + private static final String KEY_YEAR_START = "year_start"; + private static final String KEY_YEAR_END = "year_end"; + private static final String KEY_CURRENT_VIEW = "current_view"; + private static final String KEY_LIST_POSITION_OFFSET = "list_position_offset"; + private static final String KEY_MIN_DATE = "min_date"; + private static final String KEY_MAX_DATE = "max_date"; + private static final String KEY_SELECTED_YEAR = "selected_year"; + private static final String KEY_HIGHLIGHTED_DAYS = "highlighted_days"; + private static final String KEY_SELECTABLE_DAYS = "selectable_days"; + private static final String KEY_THEME_DARK = "theme_dark"; + + private static final int DEFAULT_START_YEAR = 1350; + private static final int DEFAULT_END_YEAR = 1450; + + private static final int ANIMATION_DURATION = 300; + private static final int ANIMATION_DELAY = 500; + + private ArrayList mSelectedDaysCalendars = new ArrayList<>(); + private OnDateSetListener mCallBack; + private HashSet mListeners = new HashSet<>(); + private DialogInterface.OnCancelListener mOnCancelListener; + private DialogInterface.OnDismissListener mOnDismissListener; + + private AccessibleDateAnimator mAnimator; + + private TextView mDayOfWeekView; + private LinearLayout mMonthAndDayView; + private TextView mSelectedMonthTextView; + private TextView mSelectedDayTextView; + private TextView mYearView; + private DayPickerView mDayPickerView; + private YearPickerView mYearPickerView; + + private int mCurrentView = UNINITIALIZED; + + private int mWeekStart = PersianCalendar.SATURDAY; + private int mMinYear = DEFAULT_START_YEAR; + private int mMaxYear = DEFAULT_END_YEAR; + private int mSelectedYear; + private PersianCalendar mMinDate; + private PersianCalendar mMaxDate; + private PersianCalendar[] highlightedDays; + private PersianCalendar[] selectableDays; + private boolean mThemeDark; + + private HapticFeedbackController mHapticFeedbackController; + + private boolean mDelayAnimation = true; + + // Accessibility strings. + private String mDayPickerDescription; + private String mSelectDay; + private String mYearPickerDescription; + private String mSelectYear; + + /** + * The callback used to indicate the user is done filling in the date. + */ + public interface OnDateSetListener { + + /** + * @param view The view associated with this listener. + * @param selectedDays List of days that have been selected. + */ + void onDateSet(MultiDatePickerDialog view, ArrayList selectedDays); + } + + /** + * The callback used to notify other date picker components of a change in selected date. + */ + public interface OnDateChangedListener { + + void onDateChanged(); + } + + + public MultiDatePickerDialog() { + // Empty constructor required for dialog fragment. + } + + /** + * @param callBack How the parent is notified that the date is set. + * @param selectedDays Selected days shown on date picker. + */ + public static MultiDatePickerDialog newInstance(OnDateSetListener callBack, @Nullable ArrayList selectedDays) { + MultiDatePickerDialog ret = new MultiDatePickerDialog(); + ret.initialize(callBack, selectedDays); + return ret; + } + + private void initialize(OnDateSetListener callBack, @Nullable ArrayList selectedDays) { + mCallBack = callBack; + if (selectedDays != null) { + setSelectedDays(selectedDays); + } else { + mSelectedDaysCalendars.add(new PersianCalendar(System.currentTimeMillis())); + } + mSelectedYear = mSelectedDaysCalendars.get(mSelectedDaysCalendars.size() - 1).getPersianYear(); + mThemeDark = false; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + final Activity activity = getActivity(); + activity.getWindow().setSoftInputMode( + WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); + if (savedInstanceState != null) { + mSelectedDaysCalendars.clear(); + mSelectedDaysCalendars.addAll((ArrayList) savedInstanceState.getSerializable(KEY_SELECTED_DAYS)); + } + } + + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putSerializable(KEY_SELECTED_DAYS, mSelectedDaysCalendars); + outState.putInt(KEY_WEEK_START, mWeekStart); + outState.putInt(KEY_YEAR_START, mMinYear); + outState.putInt(KEY_YEAR_END, mMaxYear); + outState.putInt(KEY_CURRENT_VIEW, mCurrentView); + int listPosition = -1; + if (mCurrentView == MONTH_AND_DAY_VIEW) { + listPosition = mDayPickerView.getMostVisiblePosition(); + } else if (mCurrentView == YEAR_VIEW) { + listPosition = mYearPickerView.getFirstVisiblePosition(); + outState.putInt(KEY_LIST_POSITION_OFFSET, mYearPickerView.getFirstPositionOffset()); + } + outState.putInt(KEY_LIST_POSITION, listPosition); + outState.putSerializable(KEY_MIN_DATE, mMinDate); + outState.putSerializable(KEY_MAX_DATE, mMaxDate); + outState.putSerializable(KEY_SELECTED_YEAR, mSelectedYear); + outState.putSerializable(KEY_HIGHLIGHTED_DAYS, highlightedDays); + outState.putSerializable(KEY_SELECTABLE_DAYS, selectableDays); + outState.putBoolean(KEY_THEME_DARK, mThemeDark); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + Log.d(TAG, "onCreateView: "); + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + + View view = inflater.inflate(R.layout.mdtp_date_picker_dialog, null); + + mDayOfWeekView = view.findViewById(R.id.date_picker_header); + mMonthAndDayView = view.findViewById(R.id.date_picker_month_and_day); + mMonthAndDayView.setOnClickListener(this); + mSelectedMonthTextView = view.findViewById(R.id.date_picker_month); + mSelectedDayTextView = view.findViewById(R.id.date_picker_day); + mYearView = view.findViewById(R.id.date_picker_year); + mYearView.setOnClickListener(this); + + int listPosition = -1; + int listPositionOffset = 0; + int currentView = MONTH_AND_DAY_VIEW; + if (savedInstanceState != null) { + mWeekStart = savedInstanceState.getInt(KEY_WEEK_START); + mMinYear = savedInstanceState.getInt(KEY_YEAR_START); + mMaxYear = savedInstanceState.getInt(KEY_YEAR_END); + mSelectedYear = savedInstanceState.getInt(KEY_SELECTED_YEAR); + currentView = savedInstanceState.getInt(KEY_CURRENT_VIEW); + listPosition = savedInstanceState.getInt(KEY_LIST_POSITION); + listPositionOffset = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET); + mMinDate = (PersianCalendar) savedInstanceState.getSerializable(KEY_MIN_DATE); + mMaxDate = (PersianCalendar) savedInstanceState.getSerializable(KEY_MAX_DATE); + highlightedDays = (PersianCalendar[]) savedInstanceState.getSerializable(KEY_HIGHLIGHTED_DAYS); + selectableDays = (PersianCalendar[]) savedInstanceState.getSerializable(KEY_SELECTABLE_DAYS); + mThemeDark = savedInstanceState.getBoolean(KEY_THEME_DARK); + } + + final Activity activity = getActivity(); + mDayPickerView = new SimpleDayPickerView(activity, this); + mYearPickerView = new YearPickerView(activity, this); + + Resources res = getResources(); + mDayPickerDescription = res.getString(R.string.mdtp_day_picker_description); + mSelectDay = res.getString(R.string.mdtp_select_day); + mYearPickerDescription = res.getString(R.string.mdtp_year_picker_description); + mSelectYear = res.getString(R.string.mdtp_select_year); + + int bgColorResource = mThemeDark ? R.color.mdtp_date_picker_view_animator_dark_theme : R.color.mdtp_date_picker_view_animator; + view.setBackgroundColor(activity.getResources().getColor(bgColorResource)); + + mAnimator = view.findViewById(R.id.animator); + mAnimator.addView(mDayPickerView); + mAnimator.addView(mYearPickerView); + mAnimator.setDateMillis(mSelectedDaysCalendars.get(mSelectedDaysCalendars.size() - 1).getTimeInMillis()); + // TODO: Replace with animation decided upon by the design team. + Animation animation = new AlphaAnimation(0.0f, 1.0f); + animation.setDuration(ANIMATION_DURATION); + mAnimator.setInAnimation(animation); + // TODO: Replace with animation decided upon by the design team. + Animation animation2 = new AlphaAnimation(1.0f, 0.0f); + animation2.setDuration(ANIMATION_DURATION); + mAnimator.setOutAnimation(animation2); + + Button okButton = view.findViewById(R.id.ok); + okButton.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + tryVibrate(); + if (mCallBack != null) { + mCallBack.onDateSet(MultiDatePickerDialog.this, mSelectedDaysCalendars); + } + dismiss(); + } + }); + okButton.setTypeface(TypefaceHelper.get(activity, "Roboto-Medium")); + + Button cancelButton = view.findViewById(R.id.cancel); + cancelButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + tryVibrate(); + getDialog().cancel(); + } + }); + cancelButton.setTypeface(TypefaceHelper.get(activity, "Roboto-Medium")); + cancelButton.setVisibility(isCancelable() ? View.VISIBLE : View.GONE); + + updateDisplay(false); + setCurrentView(currentView); + + if (listPosition != -1) { + if (currentView == MONTH_AND_DAY_VIEW) { + mDayPickerView.postSetSelection(listPosition); + } else if (currentView == YEAR_VIEW) { + mYearPickerView.postSetSelectionFromTop(listPosition, listPositionOffset); + } + } + + mHapticFeedbackController = new HapticFeedbackController(activity); + return view; + } + + @Override + public void onResume() { + super.onResume(); + mHapticFeedbackController.start(); + } + + @Override + public void onPause() { + super.onPause(); + mHapticFeedbackController.stop(); + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + if (mOnCancelListener != null) mOnCancelListener.onCancel(dialog); + } + + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + if (mOnDismissListener != null) mOnDismissListener.onDismiss(dialog); + } + + private void setCurrentView(final int viewIndex) { + + switch (viewIndex) { + case MONTH_AND_DAY_VIEW: + ObjectAnimator pulseAnimator = Utils.getPulseAnimator(mMonthAndDayView, 0.9f, + 1.05f); + if (mDelayAnimation) { + pulseAnimator.setStartDelay(ANIMATION_DELAY); + mDelayAnimation = false; + } + mDayPickerView.onDateChanged(); + if (mCurrentView != viewIndex) { + mMonthAndDayView.setSelected(true); + mYearView.setSelected(false); + mAnimator.setDisplayedChild(MONTH_AND_DAY_VIEW); + mCurrentView = viewIndex; + } + pulseAnimator.start(); + + String dayString = LanguageUtils.getPersianNumbers(mSelectedDaysCalendars.get(mSelectedDaysCalendars.size() - 1).getPersianLongDate()); + mAnimator.setContentDescription(mDayPickerDescription + ": " + dayString); + Utils.tryAccessibilityAnnounce(mAnimator, mSelectDay); + break; + case YEAR_VIEW: + pulseAnimator = Utils.getPulseAnimator(mYearView, 0.85f, 1.1f); + if (mDelayAnimation) { + pulseAnimator.setStartDelay(ANIMATION_DELAY); + mDelayAnimation = false; + } + mYearPickerView.onDateChanged(); + if (mCurrentView != viewIndex) { + mMonthAndDayView.setSelected(false); + mYearView.setSelected(true); + mAnimator.setDisplayedChild(YEAR_VIEW); + mCurrentView = viewIndex; + } + pulseAnimator.start(); + + String yearString = LanguageUtils. + getPersianNumbers(String.valueOf(mSelectedDaysCalendars.get(mSelectedDaysCalendars.size() - 1).getPersianYear())); + mAnimator.setContentDescription(mYearPickerDescription + ": " + yearString); + Utils.tryAccessibilityAnnounce(mAnimator, mSelectYear); + break; + } + } + + private void updateDisplay(boolean announce) { + if (mSelectedDaysCalendars.size() == 0) + return; + PersianCalendar target = mSelectedDaysCalendars.get(mSelectedDaysCalendars.size() - 1); + if (mDayOfWeekView != null) { + mDayOfWeekView.setText(target.getPersianWeekDayName()); + } + + mSelectedMonthTextView.setText(LanguageUtils. + getPersianNumbers(target.getPersianMonthName())); + mSelectedDayTextView.setText(LanguageUtils. + getPersianNumbers(String.valueOf(target.getPersianDay()))); + mYearView.setText(LanguageUtils. + getPersianNumbers(String.valueOf(mSelectedYear))); + + // Accessibility. + long millis = target.getTimeInMillis(); + mAnimator.setDateMillis(millis); + String monthAndDayText = LanguageUtils.getPersianNumbers( + target.getPersianMonthName() + " " + + target.getPersianDay() + ); + mMonthAndDayView.setContentDescription(monthAndDayText); + + if (announce) { + String fullDateText = LanguageUtils. + getPersianNumbers(target.getPersianLongDate()); + Utils.tryAccessibilityAnnounce(mAnimator, fullDateText); + } + } + + /** + * Set whether the dark theme should be used + * + * @param themeDark true if the dark theme should be used, false if the default theme should be used + */ + public void setThemeDark(boolean themeDark) { + mThemeDark = themeDark; + } + + /** + * Returns true when the dark theme should be used + * + * @return true if the dark theme should be used, false if the default theme should be used + */ + @Override + public boolean isThemeDark() { + return mThemeDark; + } + + @SuppressWarnings("unused") + public void setFirstDayOfWeek(int startOfWeek) { + if (startOfWeek < Calendar.SUNDAY || startOfWeek > Calendar.SATURDAY) { + throw new IllegalArgumentException("Value must be between Calendar.SUNDAY and " + + "Calendar.SATURDAY"); + } + mWeekStart = startOfWeek; + if (mDayPickerView != null) { + mDayPickerView.onChange(); + } + } + + @SuppressWarnings("unused") + public void setYearRange(int startYear, int endYear) { + if (endYear < startYear) { + throw new IllegalArgumentException("Year end must be larger than or equal to year start"); + } + + mMinYear = startYear; + mMaxYear = endYear; + if (mDayPickerView != null) { + mDayPickerView.onChange(); + } + } + + /** + * Sets the minimal date supported by this DatePicker. Dates before (but not including) the + * specified date will be disallowed from being selected. + * + * @param calendar a Calendar object set to the year, month, day desired as the mindate. + */ + @SuppressWarnings("unused") + public void setMinDate(PersianCalendar calendar) { + mMinDate = calendar; + + if (mDayPickerView != null) { + mDayPickerView.onChange(); + } + } + + /** + * @return The minimal date supported by this DatePicker. Null if it has not been set. + */ + @Override + public PersianCalendar getMinDate() { + return mMinDate; + } + + /** + * Sets the minimal date supported by this DatePicker. Dates after (but not including) the + * specified date will be disallowed from being selected. + * + * @param calendar a Calendar object set to the year, month, day desired as the maxdate. + */ + @SuppressWarnings("unused") + public void setMaxDate(PersianCalendar calendar) { + mMaxDate = calendar; + + if (mDayPickerView != null) { + mDayPickerView.onChange(); + } + } + + /** + * @return The maximal date supported by this DatePicker. Null if it has not been set. + */ + @Override + public PersianCalendar getMaxDate() { + return mMaxDate; + } + + /** + * Sets an array of dates which should be highlighted when the picker is drawn + * + * @param highlightedDays an Array of Calendar objects containing the dates to be highlighted + */ + @SuppressWarnings("unused") + public void setHighlightedDays(PersianCalendar[] highlightedDays) { + // Sort the array to optimize searching over it later on + Arrays.sort(highlightedDays); + this.highlightedDays = highlightedDays; + } + + /** + * @return The list of dates, as Calendar Objects, which should be highlighted. null is no dates should be highlighted + */ + @Override + public PersianCalendar[] getHighlightedDays() { + return highlightedDays; + } + + /** + * Set's a list of days which are the only valid selections. + * Setting this value will take precedence over using setMinDate() and setMaxDate() + * + * @param selectableDays an Array of Calendar Objects containing the selectable dates + */ + @SuppressWarnings("unused") + public void setSelectableDays(PersianCalendar[] selectableDays) { + // Sort the array to optimize searching over it later on + Arrays.sort(selectableDays); + this.selectableDays = selectableDays; + } + + /** + * @return an Array of Calendar objects containing the list with selectable items. null if no restriction is set + */ + @Override + public PersianCalendar[] getSelectableDays() { + return selectableDays; + } + + @SuppressWarnings("unused") + public void setOnDateSetListener(OnDateSetListener listener) { + mCallBack = listener; + } + + @SuppressWarnings("unused") + public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) { + mOnCancelListener = onCancelListener; + } + + @SuppressWarnings("unused") + public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) { + mOnDismissListener = onDismissListener; + } + + // If the newly selected month / year does not contain the currently selected day number, + // change the selected day number to the last day of the selected month or year. + // e.g. Switching from Mar to Apr when Mar 31 is selected -> Apr 30 + // e.g. Switching from 2012 to 2013 when Feb 29, 2012 is selected -> Feb 28, 2013 + private void adjustDayInMonthIfNeeded(int month, int year) { +// int day = mPersianCalendar.getPersianDay(); +// int daysInMonth = Utils.getDaysInMonth(month, year); +// if (day > daysInMonth) { +// mPersianCalendar.setPersianDate(Persian); +// } TODO + } + + @Override + public void onClick(View v) { + tryVibrate(); + if (v.getId() == R.id.date_picker_year) { + setCurrentView(YEAR_VIEW); + } else if (v.getId() == R.id.date_picker_month_and_day) { + setCurrentView(MONTH_AND_DAY_VIEW); + } + } + + @Override + public void onYearSelected(int year) { + mSelectedYear = year; + adjustDayInMonthIfNeeded(mSelectedDaysCalendars.get(mSelectedDaysCalendars.size() - 1).getPersianMonth(), year); + if (mSelectedDaysCalendars.size() == 1) + mSelectedDaysCalendars.get(mSelectedDaysCalendars.size() - 1).setPersianDate(year + , mSelectedDaysCalendars.get(mSelectedDaysCalendars.size() - 1).getPersianMonth(), + mSelectedDaysCalendars.get(mSelectedDaysCalendars.size() - 1).getPersianDay()); + updatePickers(); + setCurrentView(MONTH_AND_DAY_VIEW); + updateDisplay(true); + } + + @Override + public void onDaysOfMonthSelected(ArrayList selectedDays) { + //setSelectedDays(selectedDays); + mSelectedYear = selectedDays.get(selectedDays.size() - 1).getPersianYear(); + updatePickers(); + updateDisplay(true); + } + + private void updatePickers() { + for (OnDateChangedListener listener : mListeners) listener.onDateChanged(); + } + + + @Override + public ArrayList getSelectedDays() { + return mSelectedDaysCalendars; + } + + @Override + public void setSelectedDays(ArrayList selectedDays) { + mSelectedDaysCalendars.clear(); + mSelectedDaysCalendars.addAll(selectedDays); + } + + @Override + public int getMinYear() { + if (selectableDays != null) return selectableDays[0].getPersianYear(); + // Ensure no years can be selected outside of the given minimum date + return mMinDate != null && mMinDate.getPersianYear() > mMinYear ? mMinDate.getPersianYear() : mMinYear; + } + + @Override + public int getMaxYear() { + if (selectableDays != null) + return selectableDays[selectableDays.length - 1].getPersianYear(); + // Ensure no years can be selected outside of the given maximum date + return mMaxDate != null && mMaxDate.getPersianYear() < mMaxYear ? mMaxDate.getPersianYear() : mMaxYear; + } + + @Override + public int getSelectedYear() { + return mSelectedYear; + } + + @Override + public int getFirstDayOfWeek() { + return mWeekStart; + } + + @Override + public void registerOnDateChangedListener(OnDateChangedListener listener) { + mListeners.add(listener); + } + + @Override + public void unregisterOnDateChangedListener(OnDateChangedListener listener) { + mListeners.remove(listener); + } + + @Override + public void tryVibrate() { + mHapticFeedbackController.tryVibrate(); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/SimpleDayPickerView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/SimpleDayPickerView.java new file mode 100644 index 0000000..1bb3d50 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/SimpleDayPickerView.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.multidate; + +import android.content.Context; +import android.util.AttributeSet; + +/** + * A DayPickerView customized for {@link SimpleMonthAdapter} + */ +public class SimpleDayPickerView extends DayPickerView { + + public SimpleDayPickerView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public SimpleDayPickerView(Context context, DatePickerController controller) { + super(context, controller); + } + + @Override + public MonthAdapter createMonthAdapter(Context context, DatePickerController controller) { + return new SimpleMonthAdapter(context, controller); + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/SimpleMonthAdapter.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/SimpleMonthAdapter.java new file mode 100644 index 0000000..ded4e4d --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/SimpleMonthAdapter.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.multidate; + +import android.content.Context; + +/** + * An adapter for a list of {@link SimpleMonthView} items. + */ +public class SimpleMonthAdapter extends MonthAdapter { + + public SimpleMonthAdapter(Context context, DatePickerController controller) { + super(context, controller); + } + + @Override + public MonthView createMonthView(Context context) { + return new SimpleMonthView(context, null, mController); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/SimpleMonthView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/SimpleMonthView.java new file mode 100644 index 0000000..fd3bed7 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/SimpleMonthView.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.multidate; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Typeface; +import android.util.AttributeSet; + +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; + +public class SimpleMonthView extends MonthView { + + public SimpleMonthView(Context context, AttributeSet attr, DatePickerController controller) { + super(context, attr, controller); + } + + @Override + public void drawMonthDay(Canvas canvas, int year, int month, int day, + int x, int y, int startX, int stopX, int startY, int stopY) { + boolean flag = false; + for (int selectedDays : mSelectedDays) { + if (day == selectedDays) { + canvas.drawCircle(x, y - (MINI_DAY_NUMBER_TEXT_SIZE / 3), DAY_SELECTED_CIRCLE_SIZE, + mSelectedCirclePaint); + flag = true; + break; + } + } + + if (isHighlighted(year, month, day)) { + mMonthNumPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); + } else { + mMonthNumPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)); + } + + // If we have a mindate or maxdate, gray out the day number if it's outside the range. + if (isOutOfRange(year, month, day)) { + mMonthNumPaint.setColor(mDisabledDayTextColor); + } + else if (flag) { + mMonthNumPaint.setColor(mSelectedDayTextColor); + } + else if (mHasToday && mToday == day) { + mMonthNumPaint.setColor(mTodayNumberColor); + } else { + mMonthNumPaint.setColor(isHighlighted(year, month, day) ? mHighlightedDayTextColor : mDayTextColor); + } + + canvas.drawText(LanguageUtils. + getPersianNumbers(String.format("%d", day)), x, y, mMonthNumPaint); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/YearPickerView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/YearPickerView.java new file mode 100644 index 0000000..e8b9ade --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/multidate/YearPickerView.java @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.multidate; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.drawable.StateListDrawable; +import android.support.annotation.NonNull; +import android.view.View; +import android.view.ViewGroup; +import android.view.accessibility.AccessibilityEvent; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +import java.util.ArrayList; +import java.util.List; + +import mohammadaminha.com.widgets.Date_Picker.date.TextViewWithCircularIndicator; +import mohammadaminha.com.widgets.Date_Picker.multidate.MultiDatePickerDialog.OnDateChangedListener; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.R; + +/** + * Displays a selectable list of years. + */ +public class YearPickerView extends ListView implements OnItemClickListener, OnDateChangedListener { + private static final String TAG = "YearPickerView"; + + private DatePickerController mController; + private YearAdapter mAdapter; + private int mViewSize; + private int mChildSize; + private TextViewWithCircularIndicator mSelectedView; + + /** + * @param context + */ + public YearPickerView(Context context, DatePickerController controller) { + super(context); + mController = controller; + mController.registerOnDateChangedListener(this); + ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, + LayoutParams.WRAP_CONTENT); + setLayoutParams(frame); + Resources res = context.getResources(); + mViewSize = res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height); + mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height); + setVerticalFadingEdgeEnabled(true); + setFadingEdgeLength(mChildSize / 3); + init(context); + setOnItemClickListener(this); + setSelector(new StateListDrawable()); + setDividerHeight(0); + onDateChanged(); + } + + private void init(Context context) { + ArrayList years = new ArrayList<>(); + for (int year = mController.getMinYear(); year <= mController.getMaxYear(); year++) { + years.add(String.format("%d", year)); + } + years = LanguageUtils.getPersianNumbers(years); + mAdapter = new YearAdapter(context, R.layout.mdtp_year_label_text_view, years); + setAdapter(mAdapter); + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + mController.tryVibrate(); + TextViewWithCircularIndicator clickedView = (TextViewWithCircularIndicator) view; + if (clickedView != null) { + if (clickedView != mSelectedView) { + if (mSelectedView != null) { + mSelectedView.drawIndicator(false); + mSelectedView.requestLayout(); + } + clickedView.drawIndicator(true); + clickedView.requestLayout(); + mSelectedView = clickedView; + } + mController.onYearSelected(getYearFromTextView(clickedView)); + mAdapter.notifyDataSetChanged(); + } + } + + private static int getYearFromTextView(TextViewWithCircularIndicator view) { + return Integer.valueOf(LanguageUtils.getLatinNumbers(view.getText().toString())); + } + + private class YearAdapter extends ArrayAdapter { + + public YearAdapter(Context context, int resource, List objects) { + super(context, resource, objects); + } + + @NonNull + @Override + public View getView(int position, View convertView, @NonNull ViewGroup parent) { + TextViewWithCircularIndicator v = (TextViewWithCircularIndicator) + super.getView(position, convertView, parent); + v.requestLayout(); + int year = getYearFromTextView(v); + boolean selected = mController.getSelectedYear() == year; + v.drawIndicator(selected); + if (selected) { + mSelectedView = v; + } + return v; + } + } + + private void postSetSelectionCentered(final int position) { + postSetSelectionFromTop(position, mViewSize / 2 - mChildSize / 2); + } + + public void postSetSelectionFromTop(final int position, final int offset) { + post(new Runnable() { + + @Override + public void run() { + setSelectionFromTop(position, offset); + requestLayout(); + } + }); + } + + public int getFirstPositionOffset() { + final View firstChild = getChildAt(0); + if (firstChild == null) { + return 0; + } + return firstChild.getTop(); + } + + @Override + public void onDateChanged() { + mAdapter.notifyDataSetChanged(); + postSetSelectionCentered(mController.getSelectedYear() - mController.getMinYear()); + } + + @Override + public void onInitializeAccessibilityEvent(AccessibilityEvent event) { + super.onInitializeAccessibilityEvent(event); + if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) { + event.setFromIndex(0); + event.setToIndex(0); + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/AmPmCirclesView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/AmPmCirclesView.java new file mode 100644 index 0000000..7597f23 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/AmPmCirclesView.java @@ -0,0 +1,218 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.time; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.util.Log; +import android.view.View; + +import mohammadaminha.com.widgets.Date_Picker.Utils; +import mohammadaminha.com.widgets.R; +import mohammadaminha.com.widgets.Util; + +/** + * Draw the two smaller AM and PM circles next to where the larger circle will be. + */ +public class AmPmCirclesView extends View { + private static final String TAG = "AmPmCirclesView"; + + // Alpha level for selected circle. + private static final int SELECTED_ALPHA = Utils.SELECTED_ALPHA; + private static final int SELECTED_ALPHA_THEME_DARK = Utils.SELECTED_ALPHA_THEME_DARK; + + private Paint mPaint = new Paint(); + private int mSelectedAlpha; + private int mTouchedColor; + private int mUnselectedColor; + private int mAmPmTextColor; + private int mAmPmSelectedTextColor; + private int mSelectedColor; + private float mCircleRadiusMultiplier; + private float mAmPmCircleRadiusMultiplier; + private String mAmText; + private String mPmText; + private boolean mIsInitialized; + + private static final int AM = TimePickerDialog.AM; + private static final int PM = TimePickerDialog.PM; + + private boolean mDrawValuesReady; + private int mAmPmCircleRadius; + private int mAmXCenter; + private int mPmXCenter; + private int mAmPmYCenter; + private int mAmOrPm; + private int mAmOrPmPressed; + + public AmPmCirclesView(Context context) { + super(context); + mIsInitialized = false; + } + + public void initialize(Context context, int amOrPm) { + if (mIsInitialized) { + Log.e(TAG, "AmPmCirclesView may only be initialized once."); + return; + } + + Resources res = context.getResources(); + mUnselectedColor = res.getColor(R.color.mdtp_white); + mSelectedColor = res.getColor(R.color.mdtp_accent_color); + mTouchedColor = res.getColor(R.color.mdtp_accent_color_dark); + mAmPmTextColor = res.getColor(R.color.mdtp_ampm_text_color); + mAmPmSelectedTextColor = res.getColor(R.color.mdtp_white); + mSelectedAlpha = SELECTED_ALPHA; + + mPaint.setTypeface(Util.getTypeFace()); + mPaint.setAntiAlias(true); + mPaint.setTextAlign(Align.CENTER); + + mCircleRadiusMultiplier = + Float.parseFloat(res.getString(R.string.mdtp_circle_radius_multiplier)); + mAmPmCircleRadiusMultiplier = + Float.parseFloat(res.getString(R.string.mdtp_ampm_circle_radius_multiplier)); + mAmText = "قبل‌ازظهر"; + mPmText = "بعدازظهر"; + + setAmOrPm(amOrPm); + mAmOrPmPressed = -1; + + mIsInitialized = true; + } + + /* package */ void setTheme(Context context, boolean themeDark) { + Resources res = context.getResources(); + if (themeDark) { + mUnselectedColor = res.getColor(R.color.mdtp_circle_background_dark_theme); + mSelectedColor = res.getColor(R.color.mdtp_red); + mAmPmTextColor = res.getColor(R.color.mdtp_white); + mSelectedAlpha = SELECTED_ALPHA_THEME_DARK; + } else { + mUnselectedColor = res.getColor(R.color.mdtp_white); + mSelectedColor = res.getColor(R.color.mdtp_accent_color); + mAmPmTextColor = res.getColor(R.color.mdtp_ampm_text_color); + mSelectedAlpha = SELECTED_ALPHA; + } + } + + public void setAmOrPm(int amOrPm) { + mAmOrPm = amOrPm; + } + + public void setAmOrPmPressed(int amOrPmPressed) { + mAmOrPmPressed = amOrPmPressed; + } + + /** + * Calculate whether the coordinates are touching the AM or PM circle. + */ + public int getIsTouchingAmOrPm(float xCoord, float yCoord) { + if (!mDrawValuesReady) { + return -1; + } + + int squaredYDistance = (int) ((yCoord - mAmPmYCenter)*(yCoord - mAmPmYCenter)); + + int distanceToAmCenter = + (int) Math.sqrt((xCoord - mAmXCenter)*(xCoord - mAmXCenter) + squaredYDistance); + if (distanceToAmCenter <= mAmPmCircleRadius) { + return AM; + } + + int distanceToPmCenter = + (int) Math.sqrt((xCoord - mPmXCenter)*(xCoord - mPmXCenter) + squaredYDistance); + if (distanceToPmCenter <= mAmPmCircleRadius) { + return PM; + } + + // Neither was close enough. + return -1; + } + + @Override + public void onDraw(Canvas canvas) { + int viewWidth = getWidth(); + if (viewWidth == 0 || !mIsInitialized) { + return; + } + + if (!mDrawValuesReady) { + int layoutXCenter = getWidth() / 2; + int layoutYCenter = getHeight() / 2; + int circleRadius = + (int) (Math.min(layoutXCenter, layoutYCenter) * mCircleRadiusMultiplier); + mAmPmCircleRadius = (int) (circleRadius * mAmPmCircleRadiusMultiplier); + layoutYCenter += mAmPmCircleRadius*0.75; + int textSize = mAmPmCircleRadius * 3 / 4; + mPaint.setTextSize(textSize); + + // Line up the vertical center of the AM/PM circles with the bottom of the main circle. + mAmPmYCenter = layoutYCenter - mAmPmCircleRadius / 2 + circleRadius; + // Line up the horizontal edges of the AM/PM circles with the horizontal edges + // of the main circle. + mAmXCenter = layoutXCenter - circleRadius + mAmPmCircleRadius; + mPmXCenter = layoutXCenter + circleRadius - mAmPmCircleRadius; + + mDrawValuesReady = true; + } + + // We'll need to draw either a lighter blue (for selection), a darker blue (for touching) + // or white (for not selected). + int amColor = mUnselectedColor; + int amAlpha = 255; + int amTextColor = mAmPmTextColor; + int pmColor = mUnselectedColor; + int pmAlpha = 255; + int pmTextColor = mAmPmTextColor; + + if (mAmOrPm == AM) { + amColor = mSelectedColor; + amAlpha = mSelectedAlpha; + amTextColor = mAmPmSelectedTextColor; + } else if (mAmOrPm == PM) { + pmColor = mSelectedColor; + pmAlpha = mSelectedAlpha; + pmTextColor = mAmPmSelectedTextColor; + } + if (mAmOrPmPressed == AM) { + amColor = mTouchedColor; + amAlpha = mSelectedAlpha; + } else if (mAmOrPmPressed == PM) { + pmColor = mTouchedColor; + pmAlpha = mSelectedAlpha; + } + + // Draw the two circles. + mPaint.setColor(amColor); + mPaint.setAlpha(amAlpha); + canvas.drawCircle(mAmXCenter, mAmPmYCenter, mAmPmCircleRadius, mPaint); + mPaint.setColor(pmColor); + mPaint.setAlpha(pmAlpha); + canvas.drawCircle(mPmXCenter, mAmPmYCenter, mAmPmCircleRadius, mPaint); + + // Draw the AM/PM texts on top. + mPaint.setColor(amTextColor); + int textYCenter = mAmPmYCenter - (int) (mPaint.descent() + mPaint.ascent()) / 2; + canvas.drawText(mAmText, mAmXCenter, textYCenter, mPaint); + mPaint.setColor(pmTextColor); + canvas.drawText(mPmText, mPmXCenter, textYCenter, mPaint); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/CircleView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/CircleView.java new file mode 100644 index 0000000..b9c1571 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/CircleView.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.time; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.util.Log; +import android.view.View; + +import mohammadaminha.com.widgets.R; + +/** + * Draws a simple white circle on which the numbers will be drawn. + */ +public class CircleView extends View { + private static final String TAG = "CircleView"; + + private Paint mPaint = new Paint(); + private boolean mIs24HourMode; + private int mCircleColor; + private int mDotColor; + private float mCircleRadiusMultiplier; + private float mAmPmCircleRadiusMultiplier; + private boolean mIsInitialized; + + private boolean mDrawValuesReady; + private int mXCenter; + private int mYCenter; + private int mCircleRadius; + + public CircleView(Context context) { + super(context); + + Resources res = context.getResources(); + mCircleColor = res.getColor(R.color.mdtp_circle_color); + mDotColor = res.getColor(R.color.mdtp_numbers_text_color); + mPaint.setAntiAlias(true); + + mIsInitialized = false; + } + + public void initialize(Context context, boolean is24HourMode) { + if (mIsInitialized) { + Log.e(TAG, "CircleView may only be initialized once."); + return; + } + + Resources res = context.getResources(); + mIs24HourMode = is24HourMode; + if (is24HourMode) { + mCircleRadiusMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_circle_radius_multiplier_24HourMode)); + } else { + mCircleRadiusMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_circle_radius_multiplier)); + mAmPmCircleRadiusMultiplier = + Float.parseFloat(res.getString(R.string.mdtp_ampm_circle_radius_multiplier)); + } + + mIsInitialized = true; + } + + /* package */ void setTheme(Context context, boolean dark) { + Resources res = context.getResources(); + if (dark) { + mCircleColor = res.getColor(R.color.mdtp_circle_background_dark_theme); + mDotColor = res.getColor(R.color.mdtp_white); + } else { + mCircleColor = res.getColor(R.color.mdtp_circle_color); + mDotColor = res.getColor(R.color.mdtp_numbers_text_color); + } + } + + + @Override + public void onDraw(Canvas canvas) { + int viewWidth = getWidth(); + if (viewWidth == 0 || !mIsInitialized) { + return; + } + + if (!mDrawValuesReady) { + mXCenter = getWidth() / 2; + mYCenter = getHeight() / 2; + mCircleRadius = (int) (Math.min(mXCenter, mYCenter) * mCircleRadiusMultiplier); + + if (!mIs24HourMode) { + // We'll need to draw the AM/PM circles, so the main circle will need to have + // a slightly higher center. To keep the entire view centered vertically, we'll + // have to push it up by half the radius of the AM/PM circles. + int amPmCircleRadius = (int) (mCircleRadius * mAmPmCircleRadiusMultiplier); + mYCenter -= amPmCircleRadius*0.75; + } + + mDrawValuesReady = true; + } + + // Draw the white circle. + mPaint.setColor(mCircleColor); + canvas.drawCircle(mXCenter, mYCenter, mCircleRadius, mPaint); + + // Draw a small black circle in the center. + mPaint.setColor(mDotColor); + canvas.drawCircle(mXCenter, mYCenter, 4, mPaint); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/RadialPickerLayout.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/RadialPickerLayout.java new file mode 100644 index 0000000..306a6b6 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/RadialPickerLayout.java @@ -0,0 +1,861 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.time; + +import android.animation.AnimatorSet; +import android.animation.ObjectAnimator; +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.res.Resources; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.support.annotation.NonNull; +import android.text.format.DateUtils; +import android.util.AttributeSet; +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; +import android.view.View.OnTouchListener; +import android.view.ViewConfiguration; +import android.view.ViewGroup; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityManager; +import android.view.accessibility.AccessibilityNodeInfo; +import android.widget.FrameLayout; + +import java.util.Calendar; + +import mohammadaminha.com.widgets.Date_Picker.HapticFeedbackController; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.R; + +/** + * The primary layout to hold the circular picker, and the am/pm buttons. This view will measure + * itself to end up as a square. It also handles touches to be passed in to views that need to know + * when they'd been touched. + */ +public class RadialPickerLayout extends FrameLayout implements OnTouchListener { + private static final String TAG = "RadialPickerLayout"; + + private int TOUCH_SLOP; + private int TAP_TIMEOUT; + + private static final int VISIBLE_DEGREES_STEP_SIZE = 30; + private static final int HOUR_VALUE_TO_DEGREES_STEP_SIZE = VISIBLE_DEGREES_STEP_SIZE; + private static final int MINUTE_VALUE_TO_DEGREES_STEP_SIZE = 6; + private static final int HOUR_INDEX = TimePickerDialog.HOUR_INDEX; + private static final int MINUTE_INDEX = TimePickerDialog.MINUTE_INDEX; + private static final int AMPM_INDEX = TimePickerDialog.AMPM_INDEX; + private static final int ENABLE_PICKER_INDEX = TimePickerDialog.ENABLE_PICKER_INDEX; + private static final int AM = TimePickerDialog.AM; + private static final int PM = TimePickerDialog.PM; + + private int mLastValueSelected; + + private HapticFeedbackController mHapticFeedbackController; + private OnValueSelectedListener mListener; + private boolean mTimeInitialized; + private int mCurrentHoursOfDay; + private int mCurrentMinutes; + private boolean mIs24HourMode; + private boolean mHideAmPm; + private int mCurrentItemShowing; + + private CircleView mCircleView; + private AmPmCirclesView mAmPmCirclesView; + private RadialTextsView mHourRadialTextsView; + private RadialTextsView mMinuteRadialTextsView; + private RadialSelectorView mHourRadialSelectorView; + private RadialSelectorView mMinuteRadialSelectorView; + private View mGrayBox; + + private int[] mSnapPrefer30sMap; + private boolean mInputEnabled; + private int mIsTouchingAmOrPm = -1; + private boolean mDoingMove; + private boolean mDoingTouch; + private int mDownDegrees; + private float mDownX; + private float mDownY; + private AccessibilityManager mAccessibilityManager; + + private AnimatorSet mTransition; + private Handler mHandler = new Handler(); + + public interface OnValueSelectedListener { + void onValueSelected(int pickerIndex, int newValue, boolean autoAdvance); + } + + public RadialPickerLayout(Context context, AttributeSet attrs) { + super(context, attrs); + + setOnTouchListener(this); + ViewConfiguration vc = ViewConfiguration.get(context); + TOUCH_SLOP = vc.getScaledTouchSlop(); + TAP_TIMEOUT = ViewConfiguration.getTapTimeout(); + mDoingMove = false; + + mCircleView = new CircleView(context); + addView(mCircleView); + + mAmPmCirclesView = new AmPmCirclesView(context); + addView(mAmPmCirclesView); + + mHourRadialSelectorView = new RadialSelectorView(context); + addView(mHourRadialSelectorView); + mMinuteRadialSelectorView = new RadialSelectorView(context); + addView(mMinuteRadialSelectorView); + + mHourRadialTextsView = new RadialTextsView(context); + addView(mHourRadialTextsView); + mMinuteRadialTextsView = new RadialTextsView(context); + addView(mMinuteRadialTextsView); + + // Prepare mapping to snap touchable degrees to selectable degrees. + preparePrefer30sMap(); + + mLastValueSelected = -1; + + mInputEnabled = true; + + mGrayBox = new View(context); + mGrayBox.setLayoutParams(new ViewGroup.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + mGrayBox.setBackgroundColor(getResources().getColor(R.color.mdtp_transparent_black)); + mGrayBox.setVisibility(View.INVISIBLE); + addView(mGrayBox); + + mAccessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE); + + mTimeInitialized = false; + } + + /** + @Override + public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int measuredWidth = MeasureSpec.getSize(widthMeasureSpec); + int widthMode = MeasureSpec.getMode(widthMeasureSpec); + int measuredHeight = MeasureSpec.getSize(heightMeasureSpec); + int heightMode = MeasureSpec.getMode(heightMeasureSpec); + int minDimension = Math.min(measuredWidth, measuredHeight); + + super.onMeasure(MeasureSpec.makeMeasureSpec(minDimension, widthMode), + MeasureSpec.makeMeasureSpec(minDimension, heightMode)); + } + **/ + + public void setOnValueSelectedListener(OnValueSelectedListener listener) { + mListener = listener; + } + + /** + * Initialize the Layout with starting values. + * @param context + * @param initialHoursOfDay + * @param initialMinutes + * @param is24HourMode + */ + public void initialize(Context context, HapticFeedbackController hapticFeedbackController, + int initialHoursOfDay, int initialMinutes, boolean is24HourMode) { + if (mTimeInitialized) { + Log.e(TAG, "Time has already been initialized."); + return; + } + + mHapticFeedbackController = hapticFeedbackController; + mIs24HourMode = is24HourMode; + mHideAmPm = mAccessibilityManager.isTouchExplorationEnabled() || mIs24HourMode; + + // Initialize the circle and AM/PM circles if applicable. + mCircleView.initialize(context, mHideAmPm); + mCircleView.invalidate(); + if (!mHideAmPm) { + mAmPmCirclesView.initialize(context, initialHoursOfDay < 12? AM : PM); + mAmPmCirclesView.invalidate(); + } + + // Initialize the hours and minutes numbers. + Resources res = context.getResources(); + int[] hours = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + int[] hours_24 = {0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; + int[] minutes = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55}; + String[] hoursTexts = new String[12]; + String[] innerHoursTexts = new String[12]; + String[] minutesTexts = new String[12]; + for (int i = 0; i < 12; i++) { + hoursTexts[i] = LanguageUtils.getPersianNumbers( + is24HourMode? String.format("%02d", hours_24[i]) : String.format("%d", hours[i]) + ); + innerHoursTexts[i] = LanguageUtils.getPersianNumbers(String.format("%d", hours[i])); + minutesTexts[i] = LanguageUtils.getPersianNumbers(String.format("%02d", minutes[i])); + } + mHourRadialTextsView.initialize(res, + hoursTexts, (is24HourMode? innerHoursTexts : null), mHideAmPm, true); + mHourRadialTextsView.setSelection(is24HourMode ? initialHoursOfDay : initialHoursOfDay % 12); + mHourRadialTextsView.invalidate(); + mMinuteRadialTextsView.initialize(res, minutesTexts, null, mHideAmPm, false); + mMinuteRadialTextsView.setSelection(initialMinutes); + mMinuteRadialTextsView.invalidate(); + + // Initialize the currently-selected hour and minute. + setValueForItem(HOUR_INDEX, initialHoursOfDay); + setValueForItem(MINUTE_INDEX, initialMinutes); + int hourDegrees = (initialHoursOfDay % 12) * HOUR_VALUE_TO_DEGREES_STEP_SIZE; + mHourRadialSelectorView.initialize(context, mHideAmPm, is24HourMode, true, + hourDegrees, isHourInnerCircle(initialHoursOfDay)); + int minuteDegrees = initialMinutes * MINUTE_VALUE_TO_DEGREES_STEP_SIZE; + mMinuteRadialSelectorView.initialize(context, mHideAmPm, false, false, + minuteDegrees, false); + + mTimeInitialized = true; + } + + /* package */ void setTheme(Context context, boolean themeDark) { + mCircleView.setTheme(context, themeDark); + mAmPmCirclesView.setTheme(context, themeDark); + mHourRadialTextsView.setTheme(context, themeDark); + mMinuteRadialTextsView.setTheme(context, themeDark); + mHourRadialSelectorView.setTheme(context, themeDark); + mMinuteRadialSelectorView.setTheme(context, themeDark); + } + + public void setTime(int hours, int minutes) { + setItem(HOUR_INDEX, hours); + setItem(MINUTE_INDEX, minutes); + } + + /** + * Set either the hour or the minute. Will set the internal value, and set the selection. + */ + private void setItem(int index, int value) { + if (index == HOUR_INDEX) { + setValueForItem(HOUR_INDEX, value); + int hourDegrees = (value % 12) * HOUR_VALUE_TO_DEGREES_STEP_SIZE; + mHourRadialSelectorView.setSelection(hourDegrees, isHourInnerCircle(value), false); + mHourRadialSelectorView.invalidate(); + mHourRadialTextsView.setSelection(value); + mHourRadialTextsView.invalidate(); + } else if (index == MINUTE_INDEX) { + setValueForItem(MINUTE_INDEX, value); + int minuteDegrees = value * MINUTE_VALUE_TO_DEGREES_STEP_SIZE; + mMinuteRadialSelectorView.setSelection(minuteDegrees, false, false); + mMinuteRadialSelectorView.invalidate(); + mMinuteRadialTextsView.setSelection(value); + mHourRadialTextsView.invalidate(); + } + } + + /** + * Check if a given hour appears in the outer circle or the inner circle + * @return true if the hour is in the inner circle, false if it's in the outer circle. + */ + private boolean isHourInnerCircle(int hourOfDay) { + // We'll have the 00 hours on the outside circle. + return mIs24HourMode && (hourOfDay <= 12 && hourOfDay != 0); + } + + public int getHours() { + return mCurrentHoursOfDay; + } + + public int getMinutes() { + return mCurrentMinutes; + } + + /** + * If the hours are showing, return the current hour. If the minutes are showing, return the + * current minute. + */ + private int getCurrentlyShowingValue() { + int currentIndex = getCurrentItemShowing(); + if (currentIndex == HOUR_INDEX) { + return mCurrentHoursOfDay; + } else if (currentIndex == MINUTE_INDEX) { + return mCurrentMinutes; + } else { + return -1; + } + } + + public int getIsCurrentlyAmOrPm() { + if (mCurrentHoursOfDay < 12) { + return AM; + } else if (mCurrentHoursOfDay < 24) { + return PM; + } + return -1; + } + + /** + * Set the internal value for the hour, minute, or AM/PM. + */ + private void setValueForItem(int index, int value) { + if (index == HOUR_INDEX) { + mCurrentHoursOfDay = value; + } else if (index == MINUTE_INDEX){ + mCurrentMinutes = value; + } else if (index == AMPM_INDEX) { + if (value == AM) { + mCurrentHoursOfDay = mCurrentHoursOfDay % 12; + } else if (value == PM) { + mCurrentHoursOfDay = (mCurrentHoursOfDay % 12) + 12; + } + } + } + + /** + * Set the internal value as either AM or PM, and update the AM/PM circle displays. + * @param amOrPm + */ + public void setAmOrPm(int amOrPm) { + mAmPmCirclesView.setAmOrPm(amOrPm); + mAmPmCirclesView.invalidate(); + setValueForItem(AMPM_INDEX, amOrPm); + } + + /** + * Split up the 360 degrees of the circle among the 60 selectable values. Assigns a larger + * selectable area to each of the 12 visible values, such that the ratio of space apportioned + * to a visible value : space apportioned to a non-visible value will be 14 : 4. + * E.g. the output of 30 degrees should have a higher range of input associated with it than + * the output of 24 degrees, because 30 degrees corresponds to a visible number on the clock + * circle (5 on the minutes, 1 or 13 on the hours). + */ + private void preparePrefer30sMap() { + // We'll split up the visible output and the non-visible output such that each visible + // output will correspond to a range of 14 associated input degrees, and each non-visible + // output will correspond to a range of 4 associate input degrees, so visible numbers + // are more than 3 times easier to get than non-visible numbers: + // {354-359,0-7}:0, {8-11}:6, {12-15}:12, {16-19}:18, {20-23}:24, {24-37}:30, etc. + // + // If an output of 30 degrees should correspond to a range of 14 associated degrees, then + // we'll need any input between 24 - 37 to snap to 30. Working out from there, 20-23 should + // snap to 24, while 38-41 should snap to 36. This is somewhat counter-intuitive, that you + // can be touching 36 degrees but have the selection snapped to 30 degrees; however, this + // inconsistency isn't noticeable at such fine-grained degrees, and it affords us the + // ability to aggressively prefer the visible values by a factor of more than 3:1, which + // greatly contributes to the selectability of these values. + + // Our input will be 0 through 360. + mSnapPrefer30sMap = new int[361]; + + // The first output is 0, and each following output will increment by 6 {0, 6, 12, ...}. + int snappedOutputDegrees = 0; + // Count of how many inputs we've designated to the specified output. + int count = 1; + // How many input we expect for a specified output. This will be 14 for output divisible + // by 30, and 4 for the remaining output. We'll special case the outputs of 0 and 360, so + // the caller can decide which they need. + int expectedCount = 8; + // Iterate through the input. + for (int degrees = 0; degrees < 361; degrees++) { + // Save the input-output mapping. + mSnapPrefer30sMap[degrees] = snappedOutputDegrees; + // If this is the last input for the specified output, calculate the next output and + // the next expected count. + if (count == expectedCount) { + snappedOutputDegrees += 6; + if (snappedOutputDegrees == 360) { + expectedCount = 7; + } else if (snappedOutputDegrees % 30 == 0) { + expectedCount = 14; + } else { + expectedCount = 4; + } + count = 1; + } else { + count++; + } + } + } + + /** + * Returns mapping of any input degrees (0 to 360) to one of 60 selectable output degrees, + * where the degrees corresponding to visible numbers (i.e. those divisible by 30) will be + * weighted heavier than the degrees corresponding to non-visible numbers. + * See {@link #preparePrefer30sMap()} documentation for the rationale and generation of the + * mapping. + */ + private int snapPrefer30s(int degrees) { + if (mSnapPrefer30sMap == null) { + return -1; + } + return mSnapPrefer30sMap[degrees]; + } + + /** + * Returns mapping of any input degrees (0 to 360) to one of 12 visible output degrees (all + * multiples of 30), where the input will be "snapped" to the closest visible degrees. + * @param degrees The input degrees + * @param forceHigherOrLower The output may be forced to either the higher or lower step, or may + * be allowed to snap to whichever is closer. Use 1 to force strictly higher, -1 to force + * strictly lower, and 0 to snap to the closer one. + * @return output degrees, will be a multiple of 30 + */ + private static int snapOnly30s(int degrees, int forceHigherOrLower) { + int stepSize = HOUR_VALUE_TO_DEGREES_STEP_SIZE; + int floor = (degrees / stepSize) * stepSize; + int ceiling = floor + stepSize; + if (forceHigherOrLower == 1) { + degrees = ceiling; + } else if (forceHigherOrLower == -1) { + if (degrees == floor) { + floor -= stepSize; + } + degrees = floor; + } else { + if ((degrees - floor) < (ceiling - degrees)) { + degrees = floor; + } else { + degrees = ceiling; + } + } + return degrees; + } + + /** + * For the currently showing view (either hours or minutes), re-calculate the position for the + * selector, and redraw it at that position. The input degrees will be snapped to a selectable + * value. The text representing the currently selected value will be redrawn if required. + * @param degrees Degrees which should be selected. + * @param isInnerCircle Whether the selection should be in the inner circle; will be ignored + * if there is no inner circle. + * @param forceToVisibleValue Even if the currently-showing circle allows for fine-grained + * selection (i.e. minutes), force the selection to one of the visibly-showing values. + * @param forceDrawDot The dot in the circle will generally only be shown when the selection + * is on non-visible values, but use this to force the dot to be shown. + * @return The value that was selected, i.e. 0-23 for hours, 0-59 for minutes. + */ + private int reselectSelector(int degrees, boolean isInnerCircle, + boolean forceToVisibleValue, boolean forceDrawDot) { + if (degrees == -1) { + return -1; + } + int currentShowing = getCurrentItemShowing(); + + int stepSize; + boolean allowFineGrained = !forceToVisibleValue && (currentShowing == MINUTE_INDEX); + if (allowFineGrained) { + degrees = snapPrefer30s(degrees); + } else { + degrees = snapOnly30s(degrees, 0); + } + + RadialSelectorView radialSelectorView; + if (currentShowing == HOUR_INDEX) { + radialSelectorView = mHourRadialSelectorView; + stepSize = HOUR_VALUE_TO_DEGREES_STEP_SIZE; + } else { + radialSelectorView = mMinuteRadialSelectorView; + stepSize = MINUTE_VALUE_TO_DEGREES_STEP_SIZE; + } + radialSelectorView.setSelection(degrees, isInnerCircle, forceDrawDot); + radialSelectorView.invalidate(); + + + if (currentShowing == HOUR_INDEX) { + if (mIs24HourMode) { + if (degrees == 0 && isInnerCircle) { + degrees = 360; + } else if (degrees == 360 && !isInnerCircle) { + degrees = 0; + } + } else if (degrees == 0) { + degrees = 360; + } + } else if (degrees == 360 && currentShowing == MINUTE_INDEX) { + degrees = 0; + } + + int value = degrees / stepSize; + + if (currentShowing == HOUR_INDEX && mIs24HourMode && !isInnerCircle && degrees != 0) { + value += 12; + } + + // Redraw the text if necessary + if(getCurrentItemShowing() == HOUR_INDEX) { + mHourRadialTextsView.setSelection(value); + mHourRadialTextsView.invalidate(); + } else if(getCurrentItemShowing() == MINUTE_INDEX) { + mMinuteRadialTextsView.setSelection(value); + mMinuteRadialTextsView.invalidate(); + } + + return value; + } + + /** + * Calculate the degrees within the circle that corresponds to the specified coordinates, if + * the coordinates are within the range that will trigger a selection. + * @param pointX The x coordinate. + * @param pointY The y coordinate. + * @param forceLegal Force the selection to be legal, regardless of how far the coordinates are + * from the actual numbers. + * @param isInnerCircle If the selection may be in the inner circle, pass in a size-1 boolean + * array here, inside which the value will be true if the selection is in the inner circle, + * and false if in the outer circle. + * @return Degrees from 0 to 360, if the selection was within the legal range. -1 if not. + */ + private int getDegreesFromCoords(float pointX, float pointY, boolean forceLegal, + final Boolean[] isInnerCircle) { + int currentItem = getCurrentItemShowing(); + if (currentItem == HOUR_INDEX) { + return mHourRadialSelectorView.getDegreesFromCoords( + pointX, pointY, forceLegal, isInnerCircle); + } else if (currentItem == MINUTE_INDEX) { + return mMinuteRadialSelectorView.getDegreesFromCoords( + pointX, pointY, forceLegal, isInnerCircle); + } else { + return -1; + } + } + + /** + * Get the item (hours or minutes) that is currently showing. + */ + public int getCurrentItemShowing() { + if (mCurrentItemShowing != HOUR_INDEX && mCurrentItemShowing != MINUTE_INDEX) { + Log.e(TAG, "Current item showing was unfortunately set to "+mCurrentItemShowing); + return -1; + } + return mCurrentItemShowing; + } + + /** + * Set either minutes or hours as showing. + * @param animate True to animate the transition, false to show with no animation. + */ + public void setCurrentItemShowing(int index, boolean animate) { + if (index != HOUR_INDEX && index != MINUTE_INDEX) { + Log.e(TAG, "TimePicker does not support view at index "+index); + return; + } + + int lastIndex = getCurrentItemShowing(); + mCurrentItemShowing = index; + + if (animate && (index != lastIndex)) { + ObjectAnimator[] anims = new ObjectAnimator[4]; + if (index == MINUTE_INDEX) { + anims[0] = mHourRadialTextsView.getDisappearAnimator(); + anims[1] = mHourRadialSelectorView.getDisappearAnimator(); + anims[2] = mMinuteRadialTextsView.getReappearAnimator(); + anims[3] = mMinuteRadialSelectorView.getReappearAnimator(); + } else if (index == HOUR_INDEX){ + anims[0] = mHourRadialTextsView.getReappearAnimator(); + anims[1] = mHourRadialSelectorView.getReappearAnimator(); + anims[2] = mMinuteRadialTextsView.getDisappearAnimator(); + anims[3] = mMinuteRadialSelectorView.getDisappearAnimator(); + } + + if (mTransition != null && mTransition.isRunning()) { + mTransition.end(); + } + mTransition = new AnimatorSet(); + mTransition.playTogether(anims); + mTransition.start(); + } else { + int hourAlpha = (index == HOUR_INDEX) ? 255 : 0; + int minuteAlpha = (index == MINUTE_INDEX) ? 255 : 0; + mHourRadialTextsView.setAlpha(hourAlpha); + mHourRadialSelectorView.setAlpha(hourAlpha); + mMinuteRadialTextsView.setAlpha(minuteAlpha); + mMinuteRadialSelectorView.setAlpha(minuteAlpha); + } + + } + + @Override + public boolean onTouch(View v, MotionEvent event) { + final float eventX = event.getX(); + final float eventY = event.getY(); + int degrees; + int value; + final Boolean[] isInnerCircle = new Boolean[1]; + isInnerCircle[0] = false; + + switch(event.getAction()) { + case MotionEvent.ACTION_DOWN: + if (!mInputEnabled) { + return true; + } + + mDownX = eventX; + mDownY = eventY; + + mLastValueSelected = -1; + mDoingMove = false; + mDoingTouch = true; + // If we're showing the AM/PM, check to see if the user is touching it. + if (!mHideAmPm) { + mIsTouchingAmOrPm = mAmPmCirclesView.getIsTouchingAmOrPm(eventX, eventY); + } else { + mIsTouchingAmOrPm = -1; + } + if (mIsTouchingAmOrPm == AM || mIsTouchingAmOrPm == PM) { + // If the touch is on AM or PM, set it as "touched" after the TAP_TIMEOUT + // in case the user moves their finger quickly. + mHapticFeedbackController.tryVibrate(); + mDownDegrees = -1; + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + mAmPmCirclesView.setAmOrPmPressed(mIsTouchingAmOrPm); + mAmPmCirclesView.invalidate(); + } + }, TAP_TIMEOUT); + } else { + // If we're in accessibility mode, force the touch to be legal. Otherwise, + // it will only register within the given touch target zone. + boolean forceLegal = mAccessibilityManager.isTouchExplorationEnabled(); + // Calculate the degrees that is currently being touched. + mDownDegrees = getDegreesFromCoords(eventX, eventY, forceLegal, isInnerCircle); + if (mDownDegrees != -1) { + // If it's a legal touch, set that number as "selected" after the + // TAP_TIMEOUT in case the user moves their finger quickly. + mHapticFeedbackController.tryVibrate(); + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + mDoingMove = true; + int value = reselectSelector(mDownDegrees, isInnerCircle[0], + false, true); + mLastValueSelected = value; + mListener.onValueSelected(getCurrentItemShowing(), value, false); + } + }, TAP_TIMEOUT); + } + } + return true; + case MotionEvent.ACTION_MOVE: + if (!mInputEnabled) { + // We shouldn't be in this state, because input is disabled. + Log.e(TAG, "Input was disabled, but received ACTION_MOVE."); + return true; + } + + float dY = Math.abs(eventY - mDownY); + float dX = Math.abs(eventX - mDownX); + + if (!mDoingMove && dX <= TOUCH_SLOP && dY <= TOUCH_SLOP) { + // Hasn't registered down yet, just slight, accidental movement of finger. + break; + } + + // If we're in the middle of touching down on AM or PM, check if we still are. + // If so, no-op. If not, remove its pressed state. Either way, no need to check + // for touches on the other circle. + if (mIsTouchingAmOrPm == AM || mIsTouchingAmOrPm == PM) { + mHandler.removeCallbacksAndMessages(null); + int isTouchingAmOrPm = mAmPmCirclesView.getIsTouchingAmOrPm(eventX, eventY); + if (isTouchingAmOrPm != mIsTouchingAmOrPm) { + mAmPmCirclesView.setAmOrPmPressed(-1); + mAmPmCirclesView.invalidate(); + mIsTouchingAmOrPm = -1; + } + break; + } + + if (mDownDegrees == -1) { + // Original down was illegal, so no movement will register. + break; + } + + // We're doing a move along the circle, so move the selection as appropriate. + mDoingMove = true; + mHandler.removeCallbacksAndMessages(null); + degrees = getDegreesFromCoords(eventX, eventY, true, isInnerCircle); + if (degrees != -1) { + value = reselectSelector(degrees, isInnerCircle[0], false, true); + if (value != mLastValueSelected) { + mHapticFeedbackController.tryVibrate(); + mLastValueSelected = value; + mListener.onValueSelected(getCurrentItemShowing(), value, false); + } + } + return true; + case MotionEvent.ACTION_UP: + if (!mInputEnabled) { + // If our touch input was disabled, tell the listener to re-enable us. + Log.d(TAG, "Input was disabled, but received ACTION_UP."); + mListener.onValueSelected(ENABLE_PICKER_INDEX, 1, false); + return true; + } + + mHandler.removeCallbacksAndMessages(null); + mDoingTouch = false; + + // If we're touching AM or PM, set it as selected, and tell the listener. + if (mIsTouchingAmOrPm == AM || mIsTouchingAmOrPm == PM) { + int isTouchingAmOrPm = mAmPmCirclesView.getIsTouchingAmOrPm(eventX, eventY); + mAmPmCirclesView.setAmOrPmPressed(-1); + mAmPmCirclesView.invalidate(); + + if (isTouchingAmOrPm == mIsTouchingAmOrPm) { + mAmPmCirclesView.setAmOrPm(isTouchingAmOrPm); + if (getIsCurrentlyAmOrPm() != isTouchingAmOrPm) { + mListener.onValueSelected(AMPM_INDEX, mIsTouchingAmOrPm, false); + setValueForItem(AMPM_INDEX, isTouchingAmOrPm); + } + } + mIsTouchingAmOrPm = -1; + break; + } + + // If we have a legal degrees selected, set the value and tell the listener. + if (mDownDegrees != -1) { + degrees = getDegreesFromCoords(eventX, eventY, mDoingMove, isInnerCircle); + if (degrees != -1) { + value = reselectSelector(degrees, isInnerCircle[0], !mDoingMove, false); + + if (getCurrentItemShowing() == HOUR_INDEX && !mIs24HourMode) { + int amOrPm = getIsCurrentlyAmOrPm(); + if (amOrPm == AM && value == 12) { + value = 0; + } else if (amOrPm == PM && value != 12) { + value += 12; + } + } + setValueForItem(getCurrentItemShowing(), value); + mListener.onValueSelected(getCurrentItemShowing(), value, true); + } + } + mDoingMove = false; + return true; + default: + break; + } + return false; + } + + /** + * Set touch input as enabled or disabled, for use with keyboard mode. + */ + public boolean trySettingInputEnabled(boolean inputEnabled) { + if (mDoingTouch && !inputEnabled) { + // If we're trying to disable input, but we're in the middle of a touch event, + // we'll allow the touch event to continue before disabling input. + return false; + } + + mInputEnabled = inputEnabled; + mGrayBox.setVisibility(inputEnabled? View.INVISIBLE : View.VISIBLE); + return true; + } + + /** + * Necessary for accessibility, to ensure we support "scrolling" forward and backward + * in the circle. + */ + @Override + @SuppressWarnings("deprecation") + public void onInitializeAccessibilityNodeInfo(@NonNull AccessibilityNodeInfo info) { + super.onInitializeAccessibilityNodeInfo(info); + if(Build.VERSION.SDK_INT >= 21) { + info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD); + info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD); + } + else { + info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); + info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); + } + } + + /** + * Announce the currently-selected time when launched. + */ + @Override + public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { + if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { + // Clear the event's current text so that only the current time will be spoken. + event.getText().clear(); + Calendar time = Calendar.getInstance(); + time.set(Calendar.HOUR, getHours()); + time.set(Calendar.MINUTE, getMinutes()); + long millis = time.getTimeInMillis(); + int flags = DateUtils.FORMAT_SHOW_TIME; + if (mIs24HourMode) { + flags |= DateUtils.FORMAT_24HOUR; + } + String timeString = LanguageUtils.getPersianNumbers( + DateUtils.formatDateTime(getContext(), millis, flags)); //TODO: Changed Here. + event.getText().add(timeString); + return true; + } + return super.dispatchPopulateAccessibilityEvent(event); + } + + /** + * When scroll forward/backward events are received, jump the time to the higher/lower + * discrete, visible value on the circle. + */ + @SuppressLint("NewApi") + @Override + public boolean performAccessibilityAction(int action, Bundle arguments) { + if (super.performAccessibilityAction(action, arguments)) { + return true; + } + + int changeMultiplier = 0; + if (action == AccessibilityNodeInfo.ACTION_SCROLL_FORWARD) { + changeMultiplier = 1; + } else if (action == AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) { + changeMultiplier = -1; + } + if (changeMultiplier != 0) { + int value = getCurrentlyShowingValue(); + int stepSize = 0; + int currentItemShowing = getCurrentItemShowing(); + if (currentItemShowing == HOUR_INDEX) { + stepSize = HOUR_VALUE_TO_DEGREES_STEP_SIZE; + value %= 12; + } else if (currentItemShowing == MINUTE_INDEX) { + stepSize = MINUTE_VALUE_TO_DEGREES_STEP_SIZE; + } + + int degrees = value * stepSize; + degrees = snapOnly30s(degrees, changeMultiplier); + value = degrees / stepSize; + int maxValue ; + int minValue = 0; + if (currentItemShowing == HOUR_INDEX) { + if (mIs24HourMode) { + maxValue = 23; + } else { + maxValue = 12; + minValue = 1; + } + } else { + maxValue = 55; + } + if (value > maxValue) { + // If we scrolled forward past the highest number, wrap around to the lowest. + value = minValue; + } else if (value < minValue) { + // If we scrolled backward past the lowest number, wrap around to the highest. + value = maxValue; + } + setItem(currentItemShowing, value); + mListener.onValueSelected(currentItemShowing, value, false); + return true; + } + + return false; + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/RadialSelectorView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/RadialSelectorView.java new file mode 100644 index 0000000..f787ab6 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/RadialSelectorView.java @@ -0,0 +1,398 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.time; + +import android.animation.Keyframe; +import android.animation.ObjectAnimator; +import android.animation.PropertyValuesHolder; +import android.animation.ValueAnimator; +import android.animation.ValueAnimator.AnimatorUpdateListener; +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.util.Log; +import android.view.View; + +import mohammadaminha.com.widgets.Date_Picker.Utils; +import mohammadaminha.com.widgets.R; + +/** + * View to show what number is selected. This will draw a blue circle over the number, with a blue + * line coming from the center of the main circle to the edge of the blue selection. + */ +public class RadialSelectorView extends View { + private static final String TAG = "RadialSelectorView"; + + // Alpha level for selected circle. + private static final int SELECTED_ALPHA = Utils.SELECTED_ALPHA; + private static final int SELECTED_ALPHA_THEME_DARK = Utils.SELECTED_ALPHA_THEME_DARK; + // Alpha level for the line. + private static final int FULL_ALPHA = Utils.FULL_ALPHA; + + private Paint mPaint = new Paint(); + + private boolean mIsInitialized; + private boolean mDrawValuesReady; + + private float mCircleRadiusMultiplier; + private float mAmPmCircleRadiusMultiplier; + private float mInnerNumbersRadiusMultiplier; + private float mOuterNumbersRadiusMultiplier; + private float mNumbersRadiusMultiplier; + private float mSelectionRadiusMultiplier; + private float mAnimationRadiusMultiplier; + private boolean mIs24HourMode; + private boolean mHasInnerCircle; + private int mSelectionAlpha; + + private int mXCenter; + private int mYCenter; + private int mCircleRadius; + private float mTransitionMidRadiusMultiplier; + private float mTransitionEndRadiusMultiplier; + private int mLineLength; + private int mSelectionRadius; + private InvalidateUpdateListener mInvalidateUpdateListener; + + private int mSelectionDegrees; + private double mSelectionRadians; + private boolean mForceDrawDot; + + public RadialSelectorView(Context context) { + super(context); + mIsInitialized = false; + } + + /** + * Initialize this selector with the state of the picker. + * @param context Current context. + * @param is24HourMode Whether the selector is in 24-hour mode, which will tell us + * whether the circle's center is moved up slightly to make room for the AM/PM circles. + * @param hasInnerCircle Whether we have both an inner and an outer circle of numbers + * that may be selected. Should be true for 24-hour mode in the hours circle. + * @param disappearsOut Whether the numbers' animation will have them disappearing out + * or disappearing in. + * @param selectionDegrees The initial degrees to be selected. + * @param isInnerCircle Whether the initial selection is in the inner or outer circle. + * Will be ignored when hasInnerCircle is false. + */ + public void initialize(Context context, boolean is24HourMode, boolean hasInnerCircle, + boolean disappearsOut, int selectionDegrees, boolean isInnerCircle) { + if (mIsInitialized) { + Log.e(TAG, "This RadialSelectorView may only be initialized once."); + return; + } + + Resources res = context.getResources(); + + int accentColor = res.getColor(R.color.mdtp_accent_color); + mPaint.setColor(accentColor); + mPaint.setAntiAlias(true); + mSelectionAlpha = SELECTED_ALPHA; + + // Calculate values for the circle radius size. + mIs24HourMode = is24HourMode; + if (is24HourMode) { + mCircleRadiusMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_circle_radius_multiplier_24HourMode)); + } else { + mCircleRadiusMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_circle_radius_multiplier)); + mAmPmCircleRadiusMultiplier = + Float.parseFloat(res.getString(R.string.mdtp_ampm_circle_radius_multiplier)); + } + + // Calculate values for the radius size(s) of the numbers circle(s). + mHasInnerCircle = hasInnerCircle; + if (hasInnerCircle) { + mInnerNumbersRadiusMultiplier = + Float.parseFloat(res.getString(R.string.mdtp_numbers_radius_multiplier_inner)); + mOuterNumbersRadiusMultiplier = + Float.parseFloat(res.getString(R.string.mdtp_numbers_radius_multiplier_outer)); + } else { + mNumbersRadiusMultiplier = + Float.parseFloat(res.getString(R.string.mdtp_numbers_radius_multiplier_normal)); + } + mSelectionRadiusMultiplier = + Float.parseFloat(res.getString(R.string.mdtp_selection_radius_multiplier)); + + // Calculate values for the transition mid-way states. + mAnimationRadiusMultiplier = 1; + mTransitionMidRadiusMultiplier = 1f + (0.05f * (disappearsOut? -1 : 1)); + mTransitionEndRadiusMultiplier = 1f + (0.3f * (disappearsOut? 1 : -1)); + mInvalidateUpdateListener = new InvalidateUpdateListener(); + + setSelection(selectionDegrees, isInnerCircle, false); + mIsInitialized = true; + } + + /* package */ void setTheme(Context context, boolean themeDark) { + Resources res = context.getResources(); + int color; + if (themeDark) { + color = res.getColor(R.color.mdtp_red); + mSelectionAlpha = SELECTED_ALPHA_THEME_DARK; + } else { + color = res.getColor(R.color.mdtp_accent_color); + mSelectionAlpha = SELECTED_ALPHA; + } + mPaint.setColor(color); + } + + /** + * Set the selection. + * @param selectionDegrees The degrees to be selected. + * @param isInnerCircle Whether the selection should be in the inner circle or outer. Will be + * ignored if hasInnerCircle was initialized to false. + * @param forceDrawDot Whether to force the dot in the center of the selection circle to be + * drawn. If false, the dot will be drawn only when the degrees is not a multiple of 30, i.e. + * the selection is not on a visible number. + */ + public void setSelection(int selectionDegrees, boolean isInnerCircle, boolean forceDrawDot) { + mSelectionDegrees = selectionDegrees; + mSelectionRadians = selectionDegrees * Math.PI / 180; + mForceDrawDot = forceDrawDot; + + if (mHasInnerCircle) { + if (isInnerCircle) { + mNumbersRadiusMultiplier = mInnerNumbersRadiusMultiplier; + } else { + mNumbersRadiusMultiplier = mOuterNumbersRadiusMultiplier; + } + } + } + + /** + * Allows for smoother animations. + */ + @Override + public boolean hasOverlappingRendering() { + return false; + } + + /** + * Set the multiplier for the radius. Will be used during animations to move in/out. + */ + public void setAnimationRadiusMultiplier(float animationRadiusMultiplier) { + mAnimationRadiusMultiplier = animationRadiusMultiplier; + } + + public int getDegreesFromCoords(float pointX, float pointY, boolean forceLegal, + final Boolean[] isInnerCircle) { + if (!mDrawValuesReady) { + return -1; + } + + double hypotenuse = Math.sqrt( + (pointY - mYCenter)*(pointY - mYCenter) + + (pointX - mXCenter)*(pointX - mXCenter)); + // Check if we're outside the range + if (mHasInnerCircle) { + if (forceLegal) { + // If we're told to force the coordinates to be legal, we'll set the isInnerCircle + // boolean based based off whichever number the coordinates are closer to. + int innerNumberRadius = (int) (mCircleRadius * mInnerNumbersRadiusMultiplier); + int distanceToInnerNumber = (int) Math.abs(hypotenuse - innerNumberRadius); + int outerNumberRadius = (int) (mCircleRadius * mOuterNumbersRadiusMultiplier); + int distanceToOuterNumber = (int) Math.abs(hypotenuse - outerNumberRadius); + + isInnerCircle[0] = (distanceToInnerNumber <= distanceToOuterNumber); + } else { + // Otherwise, if we're close enough to either number (with the space between the + // two allotted equally), set the isInnerCircle boolean as the closer one. + // appropriately, but otherwise return -1. + int minAllowedHypotenuseForInnerNumber = + (int) (mCircleRadius * mInnerNumbersRadiusMultiplier) - mSelectionRadius; + int maxAllowedHypotenuseForOuterNumber = + (int) (mCircleRadius * mOuterNumbersRadiusMultiplier) + mSelectionRadius; + int halfwayHypotenusePoint = (int) (mCircleRadius * + ((mOuterNumbersRadiusMultiplier + mInnerNumbersRadiusMultiplier) / 2)); + + if (hypotenuse >= minAllowedHypotenuseForInnerNumber && + hypotenuse <= halfwayHypotenusePoint) { + isInnerCircle[0] = true; + } else if (hypotenuse <= maxAllowedHypotenuseForOuterNumber && + hypotenuse >= halfwayHypotenusePoint) { + isInnerCircle[0] = false; + } else { + return -1; + } + } + } else { + // If there's just one circle, we'll need to return -1 if: + // we're not told to force the coordinates to be legal, and + // the coordinates' distance to the number is within the allowed distance. + if (!forceLegal) { + int distanceToNumber = (int) Math.abs(hypotenuse - mLineLength); + // The max allowed distance will be defined as the distance from the center of the + // number to the edge of the circle. + int maxAllowedDistance = (int) (mCircleRadius * (1 - mNumbersRadiusMultiplier)); + if (distanceToNumber > maxAllowedDistance) { + return -1; + } + } + } + + + float opposite = Math.abs(pointY - mYCenter); + double radians = Math.asin(opposite / hypotenuse); + int degrees = (int) (radians * 180 / Math.PI); + + // Now we have to translate to the correct quadrant. + boolean rightSide = (pointX > mXCenter); + boolean topSide = (pointY < mYCenter); + if (rightSide && topSide) { + degrees = 90 - degrees; + } else if (rightSide && !topSide) { + degrees = 90 + degrees; + } else if (!rightSide && !topSide) { + degrees = 270 - degrees; + } else if (!rightSide && topSide) { + degrees = 270 + degrees; + } + return degrees; + } + + @Override + public void onDraw(Canvas canvas) { + int viewWidth = getWidth(); + if (viewWidth == 0 || !mIsInitialized) { + return; + } + + if (!mDrawValuesReady) { + mXCenter = getWidth() / 2; + mYCenter = getHeight() / 2; + mCircleRadius = (int) (Math.min(mXCenter, mYCenter) * mCircleRadiusMultiplier); + + if (!mIs24HourMode) { + // We'll need to draw the AM/PM circles, so the main circle will need to have + // a slightly higher center. To keep the entire view centered vertically, we'll + // have to push it up by half the radius of the AM/PM circles. + int amPmCircleRadius = (int) (mCircleRadius * mAmPmCircleRadiusMultiplier); + mYCenter -= amPmCircleRadius *0.75; + } + + mSelectionRadius = (int) (mCircleRadius * mSelectionRadiusMultiplier); + + mDrawValuesReady = true; + } + + // Calculate the current radius at which to place the selection circle. + mLineLength = (int) (mCircleRadius * mNumbersRadiusMultiplier * mAnimationRadiusMultiplier); + int pointX = mXCenter + (int) (mLineLength * Math.sin(mSelectionRadians)); + int pointY = mYCenter - (int) (mLineLength * Math.cos(mSelectionRadians)); + + // Draw the selection circle. + mPaint.setAlpha(mSelectionAlpha); + canvas.drawCircle(pointX, pointY, mSelectionRadius, mPaint); + + if (mForceDrawDot | mSelectionDegrees % 30 != 0) { + // We're not on a direct tick (or we've been told to draw the dot anyway). + mPaint.setAlpha(FULL_ALPHA); + canvas.drawCircle(pointX, pointY, (mSelectionRadius * 2 / 7), mPaint); + } else { + // We're not drawing the dot, so shorten the line to only go as far as the edge of the + // selection circle. + int lineLength = mLineLength; + lineLength -= mSelectionRadius; + pointX = mXCenter + (int) (lineLength * Math.sin(mSelectionRadians)); + pointY = mYCenter - (int) (lineLength * Math.cos(mSelectionRadians)); + } + + // Draw the line from the center of the circle. + mPaint.setAlpha(255); + mPaint.setStrokeWidth(1); + canvas.drawLine(mXCenter, mYCenter, pointX, pointY, mPaint); + } + + public ObjectAnimator getDisappearAnimator() { + if (!mIsInitialized || !mDrawValuesReady) { + Log.e(TAG, "RadialSelectorView was not ready for animation."); + return null; + } + + Keyframe kf0, kf1, kf2; + float midwayPoint = 0.2f; + int duration = 500; + + kf0 = Keyframe.ofFloat(0f, 1); + kf1 = Keyframe.ofFloat(midwayPoint, mTransitionMidRadiusMultiplier); + kf2 = Keyframe.ofFloat(1f, mTransitionEndRadiusMultiplier); + PropertyValuesHolder radiusDisappear = PropertyValuesHolder.ofKeyframe( + "animationRadiusMultiplier", kf0, kf1, kf2); + + kf0 = Keyframe.ofFloat(0f, 1f); + kf1 = Keyframe.ofFloat(1f, 0f); + PropertyValuesHolder fadeOut = PropertyValuesHolder.ofKeyframe("alpha", kf0, kf1); + + ObjectAnimator disappearAnimator = ObjectAnimator.ofPropertyValuesHolder( + this, radiusDisappear, fadeOut).setDuration(duration); + disappearAnimator.addUpdateListener(mInvalidateUpdateListener); + + return disappearAnimator; + } + + public ObjectAnimator getReappearAnimator() { + if (!mIsInitialized || !mDrawValuesReady) { + Log.e(TAG, "RadialSelectorView was not ready for animation."); + return null; + } + + Keyframe kf0, kf1, kf2, kf3; + float midwayPoint = 0.2f; + int duration = 500; + + // The time points are half of what they would normally be, because this animation is + // staggered against the disappear so they happen seamlessly. The reappear starts + // halfway into the disappear. + float delayMultiplier = 0.25f; + float transitionDurationMultiplier = 1f; + float totalDurationMultiplier = transitionDurationMultiplier + delayMultiplier; + int totalDuration = (int) (duration * totalDurationMultiplier); + float delayPoint = (delayMultiplier * duration) / totalDuration; + midwayPoint = 1 - (midwayPoint * (1 - delayPoint)); + + kf0 = Keyframe.ofFloat(0f, mTransitionEndRadiusMultiplier); + kf1 = Keyframe.ofFloat(delayPoint, mTransitionEndRadiusMultiplier); + kf2 = Keyframe.ofFloat(midwayPoint, mTransitionMidRadiusMultiplier); + kf3 = Keyframe.ofFloat(1f, 1); + PropertyValuesHolder radiusReappear = PropertyValuesHolder.ofKeyframe( + "animationRadiusMultiplier", kf0, kf1, kf2, kf3); + + kf0 = Keyframe.ofFloat(0f, 0f); + kf1 = Keyframe.ofFloat(delayPoint, 0f); + kf2 = Keyframe.ofFloat(1f, 1f); + PropertyValuesHolder fadeIn = PropertyValuesHolder.ofKeyframe("alpha", kf0, kf1, kf2); + + ObjectAnimator reappearAnimator = ObjectAnimator.ofPropertyValuesHolder( + this, radiusReappear, fadeIn).setDuration(totalDuration); + reappearAnimator.addUpdateListener(mInvalidateUpdateListener); + return reappearAnimator; + } + + /** + * We'll need to invalidate during the animation. + */ + private class InvalidateUpdateListener implements AnimatorUpdateListener { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + RadialSelectorView.this.invalidate(); + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/RadialTextsView.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/RadialTextsView.java new file mode 100644 index 0000000..3d44951 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/RadialTextsView.java @@ -0,0 +1,380 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.Date_Picker.time; + +import android.animation.Keyframe; +import android.animation.ObjectAnimator; +import android.animation.PropertyValuesHolder; +import android.animation.ValueAnimator; +import android.animation.ValueAnimator.AnimatorUpdateListener; +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.Typeface; +import android.util.Log; +import android.view.View; + +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.R; +import mohammadaminha.com.widgets.Util; + +/** + * A view to show a series of numbers in a circular pattern. + */ +public class RadialTextsView extends View { + private static String TAG = "RadialTextsView"; + + private Paint mPaint = new Paint(); + private Paint mSelectedPaint = new Paint(); + + private boolean mDrawValuesReady; + private boolean mIsInitialized; + + private int selection = -1; + + private Typeface mTypefaceLight; + private Typeface mTypefaceRegular; + private String[] mTexts; + private String[] mInnerTexts; + private boolean mIs24HourMode; + private boolean mHasInnerCircle; + private float mCircleRadiusMultiplier; + private float mAmPmCircleRadiusMultiplier; + private float mNumbersRadiusMultiplier; + private float mInnerNumbersRadiusMultiplier; + private float mTextSizeMultiplier; + private float mInnerTextSizeMultiplier; + + private int mXCenter; + private int mYCenter; + private float mCircleRadius; + private boolean mTextGridValuesDirty; + private float mTextSize; + private float mInnerTextSize; + private float[] mTextGridHeights; + private float[] mTextGridWidths; + private float[] mInnerTextGridHeights; + private float[] mInnerTextGridWidths; + + private float mAnimationRadiusMultiplier; + private float mTransitionMidRadiusMultiplier; + private float mTransitionEndRadiusMultiplier; + private ObjectAnimator mDisappearAnimator; + private ObjectAnimator mReappearAnimator; + private InvalidateUpdateListener mInvalidateUpdateListener; + + public RadialTextsView(Context context) { + super(context); + mIsInitialized = false; + } + + public void initialize(Resources res, String[] texts, String[] innerTexts, + boolean is24HourMode, boolean disappearsOut) { + if (mIsInitialized) { + Log.e(TAG, "This RadialTextsView may only be initialized once."); + return; + } + + // Set up the paint. + int numbersTextColor = res.getColor(R.color.mdtp_numbers_text_color); + mPaint.setColor(numbersTextColor); + + mTypefaceLight = Util.getTypeFace(); + mTypefaceRegular = Util.getTypeFace(); + mPaint.setAntiAlias(true); + mPaint.setTextAlign(Align.CENTER); + + // Set up the selected paint + int selectedTextColor = res.getColor(R.color.YellowLight); + mSelectedPaint.setColor(selectedTextColor); + mSelectedPaint.setAntiAlias(true); + mSelectedPaint.setTextAlign(Align.CENTER); + + mTexts = texts; + mInnerTexts = innerTexts; + mIs24HourMode = is24HourMode; + mHasInnerCircle = (innerTexts != null); + + // Calculate the radius for the main circle. + if (is24HourMode) { + mCircleRadiusMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_circle_radius_multiplier_24HourMode)); + } else { + mCircleRadiusMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_circle_radius_multiplier)); + mAmPmCircleRadiusMultiplier = + Float.parseFloat(res.getString(R.string.mdtp_ampm_circle_radius_multiplier)); + } + + // Initialize the widths and heights of the grid, and calculate the values for the numbers. + mTextGridHeights = new float[7]; + mTextGridWidths = new float[7]; + if (mHasInnerCircle) { + mNumbersRadiusMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_numbers_radius_multiplier_outer)); + mTextSizeMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_text_size_multiplier_outer)); + mInnerNumbersRadiusMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_numbers_radius_multiplier_inner)); + mInnerTextSizeMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_text_size_multiplier_inner)); + + mInnerTextGridHeights = new float[7]; + mInnerTextGridWidths = new float[7]; + } else { + mNumbersRadiusMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_numbers_radius_multiplier_normal)); + mTextSizeMultiplier = Float.parseFloat( + res.getString(R.string.mdtp_text_size_multiplier_normal)); + } + + mAnimationRadiusMultiplier = 1; + mTransitionMidRadiusMultiplier = 1f + (0.05f * (disappearsOut ? -1 : 1)); + mTransitionEndRadiusMultiplier = 1f + (0.3f * (disappearsOut ? 1 : -1)); + mInvalidateUpdateListener = new InvalidateUpdateListener(); + + mTextGridValuesDirty = true; + mIsInitialized = true; + } + + /* package */ void setTheme(Context context, boolean themeDark) { + Resources res = context.getResources(); + int textColor; + if (themeDark) { + textColor = res.getColor(R.color.YellowLight); + } else { + textColor = res.getColor(R.color.mdtp_numbers_text_color); + } + mPaint.setColor(textColor); + } + + /** + * Set the value of the selected text. Depending on the theme this will be rendered differently + * + * @param selection The text which is currently selected + */ + void setSelection(int selection) { + this.selection = selection; + } + + /** + * Allows for smoother animation. + */ + @Override + public boolean hasOverlappingRendering() { + return false; + } + + /** + * Used by the animation to move the numbers in and out. + */ + @SuppressWarnings("unused") + public void setAnimationRadiusMultiplier(float animationRadiusMultiplier) { + mAnimationRadiusMultiplier = animationRadiusMultiplier; + mTextGridValuesDirty = true; + } + + @Override + public void onDraw(Canvas canvas) { + int viewWidth = getWidth(); + if (viewWidth == 0 || !mIsInitialized) { + return; + } + + if (!mDrawValuesReady) { + mXCenter = getWidth() / 2; + mYCenter = getHeight() / 2; + mCircleRadius = Math.min(mXCenter, mYCenter) * mCircleRadiusMultiplier; + if (!mIs24HourMode) { + // We'll need to draw the AM/PM circles, so the main circle will need to have + // a slightly higher center. To keep the entire view centered vertically, we'll + // have to push it up by half the radius of the AM/PM circles. + float amPmCircleRadius = mCircleRadius * mAmPmCircleRadiusMultiplier; + mYCenter -= amPmCircleRadius * 0.75; + } + + mTextSize = mCircleRadius * mTextSizeMultiplier; + if (mHasInnerCircle) { + mInnerTextSize = mCircleRadius * mInnerTextSizeMultiplier; + } + + // Because the text positions will be static, pre-render the animations. + renderAnimations(); + + mTextGridValuesDirty = true; + mDrawValuesReady = true; + } + + // Calculate the text positions, but only if they've changed since the last onDraw. + if (mTextGridValuesDirty) { + float numbersRadius = + mCircleRadius * mNumbersRadiusMultiplier * mAnimationRadiusMultiplier; + + // Calculate the positions for the 12 numbers in the main circle. + calculateGridSizes(numbersRadius, mXCenter, mYCenter, + mTextSize, mTextGridHeights, mTextGridWidths); + if (mHasInnerCircle) { + // If we have an inner circle, calculate those positions too. + float innerNumbersRadius = + mCircleRadius * mInnerNumbersRadiusMultiplier * mAnimationRadiusMultiplier; + calculateGridSizes(innerNumbersRadius, mXCenter, mYCenter, + mInnerTextSize, mInnerTextGridHeights, mInnerTextGridWidths); + } + mTextGridValuesDirty = false; + } + + // Draw the texts in the pre-calculated positions. + drawTexts(canvas, mTextSize, mTypefaceLight, mTexts, mTextGridWidths, mTextGridHeights); + if (mHasInnerCircle) { + drawTexts(canvas, mInnerTextSize, mTypefaceRegular, mInnerTexts, + mInnerTextGridWidths, mInnerTextGridHeights); + } + } + + /** + * Using the trigonometric Unit Circle, calculate the positions that the text will need to be + * drawn at based on the specified circle radius. Place the values in the textGridHeights and + * textGridWidths parameters. + */ + private void calculateGridSizes(float numbersRadius, float xCenter, float yCenter, + float textSize, float[] textGridHeights, float[] textGridWidths) { + /* + * The numbers need to be drawn in a 7x7 grid, representing the points on the Unit Circle. + */ + // cos(30) = a / r => r * cos(30) = a => r * √3/2 = a + float offset2 = numbersRadius * ((float) Math.sqrt(3)) / 2f; + // sin(30) = o / r => r * sin(30) = o => r / 2 = a + float offset3 = numbersRadius / 2f; + mPaint.setTextSize(textSize); + mSelectedPaint.setTextSize(textSize); + // We'll need yTextBase to be slightly lower to account for the text's baseline. + yCenter -= (mPaint.descent() + mPaint.ascent()) / 2; + + textGridHeights[0] = yCenter - numbersRadius; + textGridWidths[0] = xCenter - numbersRadius; + textGridHeights[1] = yCenter - offset2; + textGridWidths[1] = xCenter - offset2; + textGridHeights[2] = yCenter - offset3; + textGridWidths[2] = xCenter - offset3; + textGridHeights[3] = yCenter; + textGridWidths[3] = xCenter; + textGridHeights[4] = yCenter + offset3; + textGridWidths[4] = xCenter + offset3; + textGridHeights[5] = yCenter + offset2; + textGridWidths[5] = xCenter + offset2; + textGridHeights[6] = yCenter + numbersRadius; + textGridWidths[6] = xCenter + numbersRadius; + } + + /** + * Draw the 12 text values at the positions specified by the textGrid parameters. + */ + private void drawTexts(Canvas canvas, float textSize, Typeface typeface, String[] texts, + float[] textGridWidths, float[] textGridHeights) { + mPaint.setTextSize(textSize); + mPaint.setTypeface(typeface); + LanguageUtils.getPersianNumbers(texts); + canvas.drawText(texts[0], textGridWidths[3], textGridHeights[0], Integer.parseInt(texts[0]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[1], textGridWidths[4], textGridHeights[1], Integer.parseInt(texts[1]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[2], textGridWidths[5], textGridHeights[2], Integer.parseInt(texts[2]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[3], textGridWidths[6], textGridHeights[3], Integer.parseInt(texts[3]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[4], textGridWidths[5], textGridHeights[4], Integer.parseInt(texts[4]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[5], textGridWidths[4], textGridHeights[5], Integer.parseInt(texts[5]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[6], textGridWidths[3], textGridHeights[6], Integer.parseInt(texts[6]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[7], textGridWidths[2], textGridHeights[5], Integer.parseInt(texts[7]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[8], textGridWidths[1], textGridHeights[4], Integer.parseInt(texts[8]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[9], textGridWidths[0], textGridHeights[3], Integer.parseInt(texts[9]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[10], textGridWidths[1], textGridHeights[2], Integer.parseInt(texts[10]) == selection ? mSelectedPaint : mPaint); + canvas.drawText(texts[11], textGridWidths[2], textGridHeights[1], Integer.parseInt(texts[11]) == selection ? mSelectedPaint : mPaint); + } + + /** + * Render the animations for appearing and disappearing. + */ + private void renderAnimations() { + Keyframe kf0, kf1, kf2, kf3; + float midwayPoint = 0.2f; + int duration = 500; + + // Set up animator for disappearing. + kf0 = Keyframe.ofFloat(0f, 1); + kf1 = Keyframe.ofFloat(midwayPoint, mTransitionMidRadiusMultiplier); + kf2 = Keyframe.ofFloat(1f, mTransitionEndRadiusMultiplier); + PropertyValuesHolder radiusDisappear = PropertyValuesHolder.ofKeyframe( + "animationRadiusMultiplier", kf0, kf1, kf2); + + kf0 = Keyframe.ofFloat(0f, 1f); + kf1 = Keyframe.ofFloat(1f, 0f); + PropertyValuesHolder fadeOut = PropertyValuesHolder.ofKeyframe("alpha", kf0, kf1); + + mDisappearAnimator = ObjectAnimator.ofPropertyValuesHolder( + this, radiusDisappear, fadeOut).setDuration(duration); + mDisappearAnimator.addUpdateListener(mInvalidateUpdateListener); + + + // Set up animator for reappearing. + float delayMultiplier = 0.25f; + float transitionDurationMultiplier = 1f; + float totalDurationMultiplier = transitionDurationMultiplier + delayMultiplier; + int totalDuration = (int) (duration * totalDurationMultiplier); + float delayPoint = (delayMultiplier * duration) / totalDuration; + midwayPoint = 1 - (midwayPoint * (1 - delayPoint)); + + kf0 = Keyframe.ofFloat(0f, mTransitionEndRadiusMultiplier); + kf1 = Keyframe.ofFloat(delayPoint, mTransitionEndRadiusMultiplier); + kf2 = Keyframe.ofFloat(midwayPoint, mTransitionMidRadiusMultiplier); + kf3 = Keyframe.ofFloat(1f, 1); + PropertyValuesHolder radiusReappear = PropertyValuesHolder.ofKeyframe( + "animationRadiusMultiplier", kf0, kf1, kf2, kf3); + + kf0 = Keyframe.ofFloat(0f, 0f); + kf1 = Keyframe.ofFloat(delayPoint, 0f); + kf2 = Keyframe.ofFloat(1f, 1f); + PropertyValuesHolder fadeIn = PropertyValuesHolder.ofKeyframe("alpha", kf0, kf1, kf2); + + mReappearAnimator = ObjectAnimator.ofPropertyValuesHolder( + this, radiusReappear, fadeIn).setDuration(totalDuration); + mReappearAnimator.addUpdateListener(mInvalidateUpdateListener); + } + + public ObjectAnimator getDisappearAnimator() { + if (!mIsInitialized || !mDrawValuesReady || mDisappearAnimator == null) { + Log.e(TAG, "RadialTextView was not ready for animation."); + return null; + } + + return mDisappearAnimator; + } + + public ObjectAnimator getReappearAnimator() { + if (!mIsInitialized || !mDrawValuesReady || mReappearAnimator == null) { + Log.e(TAG, "RadialTextView was not ready for animation."); + return null; + } + + return mReappearAnimator; + } + + private class InvalidateUpdateListener implements AnimatorUpdateListener { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + RadialTextsView.this.invalidate(); + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/TimePickerDialog.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/TimePickerDialog.java new file mode 100644 index 0000000..c0ac49d --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/time/TimePickerDialog.java @@ -0,0 +1,1046 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package mohammadaminha.com.widgets.Date_Picker.time; + +import android.animation.ObjectAnimator; +import android.app.ActionBar.LayoutParams; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.content.res.ColorStateList; +import android.content.res.Resources; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.util.Log; +import android.view.KeyCharacterMap; +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.View.OnKeyListener; +import android.view.ViewGroup; +import android.view.Window; +import android.widget.RelativeLayout; + +import java.util.ArrayList; +import java.util.Locale; + +import mohammadaminha.com.widgets.Button; +import mohammadaminha.com.widgets.Date_Picker.HapticFeedbackController; +import mohammadaminha.com.widgets.Date_Picker.TypefaceHelper; +import mohammadaminha.com.widgets.Date_Picker.Utils; +import mohammadaminha.com.widgets.Date_Picker.time.RadialPickerLayout.OnValueSelectedListener; +import mohammadaminha.com.widgets.Date_Picker.utils.LanguageUtils; +import mohammadaminha.com.widgets.R; +import mohammadaminha.com.widgets.TextView; + +/** + * Dialog to set a time. + */ +public class TimePickerDialog extends DialogFragment implements OnValueSelectedListener{ + private static final String TAG = "TimePickerDialog"; + + private static final String KEY_HOUR_OF_DAY = "hour_of_day"; + private static final String KEY_MINUTE = "minute"; + private static final String KEY_IS_24_HOUR_VIEW = "is_24_hour_view"; + private static final String KEY_TITLE = "dialog_title"; + private static final String KEY_CURRENT_ITEM_SHOWING = "current_item_showing"; + private static final String KEY_IN_KB_MODE = "in_kb_mode"; + private static final String KEY_TYPED_TIMES = "typed_times"; + private static final String KEY_DARK_THEME = "dark_theme"; + + public static final int HOUR_INDEX = 0; + public static final int MINUTE_INDEX = 1; + // NOT a real index for the purpose of what's showing. + public static final int AMPM_INDEX = 2; + // Also NOT a real index, just used for keyboard mode. + public static final int ENABLE_PICKER_INDEX = 3; + public static final int AM = 0; + public static final int PM = 1; + + // Delay before starting the pulse animation, in ms. + private static final int PULSE_ANIMATOR_DELAY = 300; + + private OnTimeSetListener mCallback; + private DialogInterface.OnCancelListener mOnCancelListener; + private DialogInterface.OnDismissListener mOnDismissListener; + + private HapticFeedbackController mHapticFeedbackController; + + private Button mOkButton; + private TextView mHourView; + private TextView mHourSpaceView; + private TextView mMinuteView; + private TextView mMinuteSpaceView; + private TextView mAmPmTextView; + private View mAmPmHitspace; + private RadialPickerLayout mTimePicker; + + private int mSelectedColor; + private int mUnselectedColor; + private String mAmText; + private String mPmText; + + private boolean mAllowAutoAdvance; + private int mInitialHourOfDay; + private int mInitialMinute; + private boolean mIs24HourMode; + private String mTitle; + private boolean mThemeDark; + + // For hardware IME input. + private char mPlaceholderText; + private String mDoublePlaceholderText; + private String mDeletedKeyFormat; + private boolean mInKbMode; + private ArrayList mTypedTimes; + private Node mLegalTimesTree; + private int mAmKeyCode; + private int mPmKeyCode; + + // Accessibility strings. + private String mHourPickerDescription; + private String mSelectHours; + private String mMinutePickerDescription; + private String mSelectMinutes; + + /** + * The callback interface used to indicate the user is done filling in + * the time (they clicked on the 'Set' button). + */ + public interface OnTimeSetListener { + + /** + * @param view The view associated with this listener. + * @param hourOfDay The hour that was set. + * @param minute The minute that was set. + */ + void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute); + } + + public TimePickerDialog() { + // Empty constructor required for dialog fragment. + } + + /** + public TimePickerDialog(Context context, int theme, OnTimeSetListener callback, + int hourOfDay, int minute, boolean is24HourMode) { + // Empty constructor required for dialog fragment. + } + **/ + + public static TimePickerDialog newInstance(OnTimeSetListener callback, + int hourOfDay, int minute, boolean is24HourMode) { + TimePickerDialog ret = new TimePickerDialog(); + ret.initialize(callback, hourOfDay, minute, is24HourMode); + return ret; + } + + private void initialize(OnTimeSetListener callback, + int hourOfDay, int minute, boolean is24HourMode) { + mCallback = callback; + + mInitialHourOfDay = hourOfDay; + mInitialMinute = minute; + mIs24HourMode = is24HourMode; + mInKbMode = false; + mTitle = ""; + mThemeDark = false; + } + + /** + * Set a title. NOTE: this will only take effect with the next onCreateView + */ + public void setTitle(String title) { + mTitle = title; + } + + public String getTitle() { + return mTitle; + } + + /** + * Set a dark or light theme. NOTE: this will only take effect for the next onCreateView. + */ + public void setThemeDark(boolean dark) { + mThemeDark = dark; + } + + public boolean isThemeDark() { + return mThemeDark; + } + + public void setOnTimeSetListener(OnTimeSetListener callback) { + mCallback = callback; + } + + public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) { + mOnCancelListener = onCancelListener; + } + + public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) { + mOnDismissListener = onDismissListener; + } + + public void setStartTime(int hourOfDay, int minute) { + mInitialHourOfDay = hourOfDay; + mInitialMinute = minute; + mInKbMode = false; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (savedInstanceState != null && savedInstanceState.containsKey(KEY_HOUR_OF_DAY) + && savedInstanceState.containsKey(KEY_MINUTE) + && savedInstanceState.containsKey(KEY_IS_24_HOUR_VIEW)) { + mInitialHourOfDay = savedInstanceState.getInt(KEY_HOUR_OF_DAY); + mInitialMinute = savedInstanceState.getInt(KEY_MINUTE); + mIs24HourMode = savedInstanceState.getBoolean(KEY_IS_24_HOUR_VIEW); + mInKbMode = savedInstanceState.getBoolean(KEY_IN_KB_MODE); + mTitle = savedInstanceState.getString(KEY_TITLE); + mThemeDark = savedInstanceState.getBoolean(KEY_DARK_THEME); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + + View view = inflater.inflate(R.layout.mdtp_time_picker_dialog, null); + KeyboardListener keyboardListener = new KeyboardListener(); + view.findViewById(R.id.time_picker_dialog).setOnKeyListener(keyboardListener); + + Resources res = getResources(); + mHourPickerDescription = res.getString(R.string.mdtp_hour_picker_description); + mSelectHours = res.getString(R.string.mdtp_select_hours); + mMinutePickerDescription = res.getString(R.string.mdtp_minute_picker_description); + mSelectMinutes = res.getString(R.string.mdtp_select_minutes); + mSelectedColor = res.getColor(R.color.mdtp_white); + mUnselectedColor = res.getColor(R.color.mdtp_white); + + mHourView = view.findViewById(R.id.hours); + mHourView.setOnKeyListener(keyboardListener); + mHourSpaceView = view.findViewById(R.id.hour_space); + mMinuteSpaceView = view.findViewById(R.id.minutes_space); + mMinuteView = view.findViewById(R.id.minutes); + mMinuteView.setOnKeyListener(keyboardListener); + mAmPmTextView = view.findViewById(R.id.ampm_label); + mAmPmTextView.setOnKeyListener(keyboardListener); + mAmText = "قبل‌ازظهر"; + mPmText = "بعدازظهر"; + + mHapticFeedbackController = new HapticFeedbackController(getActivity()); + + mTimePicker = view.findViewById(R.id.time_picker); + mTimePicker.setOnValueSelectedListener(this); + mTimePicker.setOnKeyListener(keyboardListener); + mTimePicker.initialize(getActivity(), mHapticFeedbackController, mInitialHourOfDay, + mInitialMinute, mIs24HourMode); + + int currentItemShowing = HOUR_INDEX; + if (savedInstanceState != null && + savedInstanceState.containsKey(KEY_CURRENT_ITEM_SHOWING)) { + currentItemShowing = savedInstanceState.getInt(KEY_CURRENT_ITEM_SHOWING); + } + setCurrentItemShowing(currentItemShowing, false, true, true); + mTimePicker.invalidate(); + + mHourView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + setCurrentItemShowing(HOUR_INDEX, true, false, true); + tryVibrate(); + } + }); + mMinuteView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + setCurrentItemShowing(MINUTE_INDEX, true, false, true); + tryVibrate(); + } + }); + + mOkButton = view.findViewById(R.id.ok); + mOkButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + if (mInKbMode && isTypedTimeFullyLegal()) { + finishKbMode(false); + } else { + tryVibrate(); + } + if (mCallback != null) { + mCallback.onTimeSet(mTimePicker, + mTimePicker.getHours(), mTimePicker.getMinutes()); + } + dismiss(); + } + }); + mOkButton.setOnKeyListener(keyboardListener); + mOkButton.setTypeface(TypefaceHelper.get(getDialog().getContext(),"Roboto-Medium")); + + Button mCancelButton = view.findViewById(R.id.cancel); + mCancelButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + tryVibrate(); + getDialog().cancel(); + } + }); + mCancelButton.setTypeface(TypefaceHelper.get(getDialog().getContext(),"Roboto-Medium")); + mCancelButton.setVisibility(isCancelable() ? View.VISIBLE : View.GONE); + + // Enable or disable the AM/PM view. + mAmPmHitspace = view.findViewById(R.id.ampm_hitspace); + if (mIs24HourMode) { + mAmPmTextView.setVisibility(View.GONE); + + RelativeLayout.LayoutParams paramsSeparator = new RelativeLayout.LayoutParams( + LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + paramsSeparator.addRule(RelativeLayout.CENTER_IN_PARENT); + TextView separatorView = view.findViewById(R.id.separator); + separatorView.setLayoutParams(paramsSeparator); + } else { + mAmPmTextView.setVisibility(View.VISIBLE); + updateAmPmDisplay(mInitialHourOfDay < 12? AM : PM); + mAmPmHitspace.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + tryVibrate(); + int amOrPm = mTimePicker.getIsCurrentlyAmOrPm(); + if (amOrPm == AM) { + amOrPm = PM; + } else if (amOrPm == PM){ + amOrPm = AM; + } + updateAmPmDisplay(amOrPm); + mTimePicker.setAmOrPm(amOrPm); + } + }); + } + + mAllowAutoAdvance = true; + setHour(mInitialHourOfDay, true); + setMinute(mInitialMinute); + + // Set up for keyboard mode. + mDoublePlaceholderText = res.getString(R.string.mdtp_time_placeholder); + mDeletedKeyFormat = res.getString(R.string.mdtp_deleted_key); + mPlaceholderText = mDoublePlaceholderText.charAt(0); + mAmKeyCode = mPmKeyCode = -1; + generateLegalTimesTree(); + if (mInKbMode) { + mTypedTimes = savedInstanceState.getIntegerArrayList(KEY_TYPED_TIMES); + tryStartingKbMode(-1); + mHourView.invalidate(); + } else if (mTypedTimes == null) { + mTypedTimes = new ArrayList<>(); + } + + // Set the title (if any) + TextView timePickerHeader = view.findViewById(R.id.time_picker_header); + if (!mTitle.isEmpty()) { + timePickerHeader.setVisibility(TextView.VISIBLE); + timePickerHeader.setText(mTitle); // TODO + } + + // Set the theme at the end so that the initialize()s above don't counteract the theme. + mTimePicker.setTheme(getActivity().getApplicationContext(), mThemeDark); + // Prepare some colors to use. + int white = res.getColor(R.color.mdtp_white); + int accent = res.getColor(R.color.mdtp_white); + int circleBackground = res.getColor(R.color.mdtp_circle_background); + int line = res.getColor(R.color.mdtp_white); + int timeDisplay = res.getColor(R.color.mdtp_numbers_text_color); + ColorStateList doneTextColor = res.getColorStateList(R.color.mdtp_done_text_color); + int doneBackground = R.drawable.mdtp_done_background_color; + int backgroundColor = res.getColor(R.color.mdtp_background_color); + int darkBackgroundColor = res.getColor(R.color.mdtp_light_gray); + + int darkGray = res.getColor(R.color.mdtp_dark_gray); + int lightGray = res.getColor(R.color.mdtp_light_gray); + int darkLine = res.getColor(R.color.mdtp_line_dark); + ColorStateList darkDoneTextColor = res.getColorStateList(R.color.mdtp_done_text_color_dark); + int darkDoneBackground = R.drawable.mdtp_done_background_color_dark; + + // Set the colors for each view based on the theme. + //view.findViewById(R.id.time_display_background).setBackgroundColor(mThemeDark? darkGray : accent); + //view.findViewById(R.id.time_display).setBackgroundColor(mThemeDark? darkGray : white); + //((TextView) view.findViewById(R.id.separator)).setTextColor(mThemeDark? white : timeDisplay); + //((TextView) view.findViewById(R.id.ampm_label)).setTextColor(mThemeDark? white : timeDisplay); + //view.findViewById(R.id.line).setBackgroundColor(mThemeDark? darkLine : line); + //mOkButton.setTextColor(mThemeDark? darkDoneTextColor : doneTextColor); + mTimePicker.setBackgroundColor(mThemeDark? lightGray : circleBackground); + view.findViewById(R.id.time_picker_dialog).setBackgroundColor(mThemeDark ? darkBackgroundColor : backgroundColor); + //mOkButton.setBackgroundResource(mThemeDark? darkDoneBackground : doneBackground); + return view; + } + + @Override + public void onResume() { + super.onResume(); + mHapticFeedbackController.start(); + } + + @Override + public void onPause() { + super.onPause(); + mHapticFeedbackController.stop(); + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + if(mOnCancelListener != null) mOnCancelListener.onCancel(dialog); + } + + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + if(mOnDismissListener != null) mOnDismissListener.onDismiss(dialog); + } + + private void tryVibrate() { + mHapticFeedbackController.tryVibrate(); + } + + private void updateAmPmDisplay(int amOrPm) { + if (amOrPm == AM) { + mAmPmTextView.setText(mAmText); + Utils.tryAccessibilityAnnounce(mTimePicker, mAmText); + mAmPmHitspace.setContentDescription(mAmText); + } else if (amOrPm == PM){ + mAmPmTextView.setText(mPmText); + Utils.tryAccessibilityAnnounce(mTimePicker, mPmText); + mAmPmHitspace.setContentDescription(mPmText); + } else { + mAmPmTextView.setText(mDoublePlaceholderText); + } + } + + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + if (mTimePicker != null) { + outState.putInt(KEY_HOUR_OF_DAY, mTimePicker.getHours()); + outState.putInt(KEY_MINUTE, mTimePicker.getMinutes()); + outState.putBoolean(KEY_IS_24_HOUR_VIEW, mIs24HourMode); + outState.putInt(KEY_CURRENT_ITEM_SHOWING, mTimePicker.getCurrentItemShowing()); + outState.putBoolean(KEY_IN_KB_MODE, mInKbMode); + if (mInKbMode) { + outState.putIntegerArrayList(KEY_TYPED_TIMES, mTypedTimes); + } + outState.putString(KEY_TITLE, mTitle); + outState.putBoolean(KEY_DARK_THEME, mThemeDark); + } + } + + /** + * Called by the picker for updating the header display. + */ + @Override + public void onValueSelected(int pickerIndex, int newValue, boolean autoAdvance) { + if (pickerIndex == HOUR_INDEX) { + setHour(newValue, false); + String announcement = String.format("%d", newValue); + if (mAllowAutoAdvance && autoAdvance) { + setCurrentItemShowing(MINUTE_INDEX, true, true, false); + announcement += ". " + mSelectMinutes; + } else { + mTimePicker.setContentDescription(mHourPickerDescription + ": " + newValue); + } + + Utils.tryAccessibilityAnnounce(mTimePicker, announcement); + } else if (pickerIndex == MINUTE_INDEX){ + setMinute(newValue); + mTimePicker.setContentDescription(mMinutePickerDescription + ": " + newValue); + } else if (pickerIndex == AMPM_INDEX) { + updateAmPmDisplay(newValue); + } else if (pickerIndex == ENABLE_PICKER_INDEX) { + if (!isTypedTimeFullyLegal()) { + mTypedTimes.clear(); + } + finishKbMode(true); + } + } + + private void setHour(int value, boolean announce) { + String format; + if (mIs24HourMode) { + format = "%02d"; + } else { + format = "%d"; + value = value % 12; + if (value == 0) { + value = 12; + } + } + + String text = LanguageUtils.getPersianNumbers(String.format(format, value)); + mHourView.setText(text); + mHourSpaceView.setText(text); + if (announce) { + Utils.tryAccessibilityAnnounce(mTimePicker, text); + } + } + + private void setMinute(int value) { + if (value == 60) { + value = 0; + } + CharSequence text = LanguageUtils.getPersianNumbers(String.format(Locale.getDefault(), "%02d", value)); + Utils.tryAccessibilityAnnounce(mTimePicker, text); + mMinuteView.setText(text); + mMinuteSpaceView.setText(text); + } + + // Show either Hours or Minutes. + private void setCurrentItemShowing(int index, boolean animateCircle, boolean delayLabelAnimate, + boolean announce) { + mTimePicker.setCurrentItemShowing(index, animateCircle); + + TextView labelToAnimate; + if (index == HOUR_INDEX) { + int hours = mTimePicker.getHours(); + if (!mIs24HourMode) { + hours = hours % 12; + } + mTimePicker.setContentDescription(mHourPickerDescription + ": " + hours); + if (announce) { + Utils.tryAccessibilityAnnounce(mTimePicker, mSelectHours); + } + labelToAnimate = mHourView; + } else { + int minutes = mTimePicker.getMinutes(); + mTimePicker.setContentDescription(mMinutePickerDescription + ": " + minutes); + if (announce) { + Utils.tryAccessibilityAnnounce(mTimePicker, mSelectMinutes); + } + labelToAnimate = mMinuteView; + } + + int hourColor = (index == HOUR_INDEX)? mSelectedColor : mUnselectedColor; + int minuteColor = (index == MINUTE_INDEX)? mSelectedColor : mUnselectedColor; + mHourView.setTextColor(hourColor); + mMinuteView.setTextColor(minuteColor); + + ObjectAnimator pulseAnimator = Utils.getPulseAnimator(labelToAnimate, 0.85f, 1.1f); + if (delayLabelAnimate) { + pulseAnimator.setStartDelay(PULSE_ANIMATOR_DELAY); + } + pulseAnimator.start(); + } + + /** + * For keyboard mode, processes key events. + * @param keyCode the pressed key. + * @return true if the key was successfully processed, false otherwise. + */ + private boolean processKeyUp(int keyCode) { + if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) { + if(isCancelable()) dismiss(); + return true; + } else if (keyCode == KeyEvent.KEYCODE_TAB) { + if(mInKbMode) { + if (isTypedTimeFullyLegal()) { + finishKbMode(true); + } + return true; + } + } else if (keyCode == KeyEvent.KEYCODE_ENTER) { + if (mInKbMode) { + if (!isTypedTimeFullyLegal()) { + return true; + } + finishKbMode(false); + } + if (mCallback != null) { + mCallback.onTimeSet(mTimePicker, + mTimePicker.getHours(), mTimePicker.getMinutes()); + } + dismiss(); + return true; + } else if (keyCode == KeyEvent.KEYCODE_DEL) { + if (mInKbMode) { + if (!mTypedTimes.isEmpty()) { + int deleted = deleteLastTypedKey(); + String deletedKeyStr; + if (deleted == getAmOrPmKeyCode(AM)) { + deletedKeyStr = mAmText; + } else if (deleted == getAmOrPmKeyCode(PM)) { + deletedKeyStr = mPmText; + } else { + deletedKeyStr = String.format("%d", getValFromKeyCode(deleted)); //TODO + } + Utils.tryAccessibilityAnnounce(mTimePicker, + String.format(mDeletedKeyFormat, deletedKeyStr)); + updateDisplay(true); + } + } + } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1 + || keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3 + || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5 + || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7 + || keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9 + || (!mIs24HourMode && + (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) { + if (!mInKbMode) { + if (mTimePicker == null) { + // Something's wrong, because time picker should definitely not be null. + Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null."); + return true; + } + mTypedTimes.clear(); + tryStartingKbMode(keyCode); + return true; + } + // We're already in keyboard mode. + if (addKeyIfLegal(keyCode)) { + updateDisplay(false); + } + return true; + } + return false; + } + + /** + * Try to start keyboard mode with the specified key, as long as the timepicker is not in the + * middle of a touch-event. + * @param keyCode The key to use as the first press. Keyboard mode will not be started if the + * key is not legal to start with. Or, pass in -1 to get into keyboard mode without a starting + * key. + */ + private void tryStartingKbMode(int keyCode) { + if (mTimePicker.trySettingInputEnabled(false) && + (keyCode == -1 || addKeyIfLegal(keyCode))) { + mInKbMode = true; + mOkButton.setEnabled(false); + updateDisplay(false); + } + } + + private boolean addKeyIfLegal(int keyCode) { + // If we're in 24hour mode, we'll need to check if the input is full. If in AM/PM mode, + // we'll need to see if AM/PM have been typed. + if ((mIs24HourMode && mTypedTimes.size() == 4) || + (!mIs24HourMode && isTypedTimeFullyLegal())) { + return false; + } + + mTypedTimes.add(keyCode); //TODO + if (!isTypedTimeLegalSoFar()) { + deleteLastTypedKey(); + return false; + } + + int val = getValFromKeyCode(keyCode); + Utils.tryAccessibilityAnnounce(mTimePicker, String.format("%d", val)); + // Automatically fill in 0's if AM or PM was legally entered. + if (isTypedTimeFullyLegal()) { + if (!mIs24HourMode && mTypedTimes.size() <= 3) { + mTypedTimes.add(mTypedTimes.size() - 1, KeyEvent.KEYCODE_0); + mTypedTimes.add(mTypedTimes.size() - 1, KeyEvent.KEYCODE_0); + } + mOkButton.setEnabled(true); + } + + return true; + } + + /** + * Traverse the tree to see if the keys that have been typed so far are legal as is, + * or may become legal as more keys are typed (excluding backspace). + */ + private boolean isTypedTimeLegalSoFar() { + Node node = mLegalTimesTree; + for (int keyCode : mTypedTimes) { + node = node.canReach(keyCode); + if (node == null) { + return false; + } + } + return true; + } + + /** + * Check if the time that has been typed so far is completely legal, as is. + */ + private boolean isTypedTimeFullyLegal() { + if (mIs24HourMode) { + // For 24-hour mode, the time is legal if the hours and minutes are each legal. Note: + // getEnteredTime() will ONLY call isTypedTimeFullyLegal() when NOT in 24hour mode. + int[] values = getEnteredTime(null); + return (values[0] >= 0 && values[1] >= 0 && values[1] < 60); + } else { + // For AM/PM mode, the time is legal if it contains an AM or PM, as those can only be + // legally added at specific times based on the tree's algorithm. + return (mTypedTimes.contains(getAmOrPmKeyCode(AM)) || + mTypedTimes.contains(getAmOrPmKeyCode(PM))); + } + } + + private int deleteLastTypedKey() { + int deleted = mTypedTimes.remove(mTypedTimes.size() - 1); + if (!isTypedTimeFullyLegal()) { + mOkButton.setEnabled(false); + } + return deleted; + } + + /** + * Get out of keyboard mode. If there is nothing in typedTimes, revert to TimePicker's time. + * @param updateDisplays If true, update the displays with the relevant time. + */ + private void finishKbMode(boolean updateDisplays) { + mInKbMode = false; + if (!mTypedTimes.isEmpty()) { + int values[] = getEnteredTime(null); + mTimePicker.setTime(values[0], values[1]); + if (!mIs24HourMode) { + mTimePicker.setAmOrPm(values[2]); + } + mTypedTimes.clear(); + } + if (updateDisplays) { + updateDisplay(false); + mTimePicker.trySettingInputEnabled(true); + } + } + + /** + * Update the hours, minutes, and AM/PM displays with the typed times. If the typedTimes is + * empty, either show an empty display (filled with the placeholder text), or update from the + * timepicker's values. + * @param allowEmptyDisplay if true, then if the typedTimes is empty, use the placeholder text. + * Otherwise, revert to the timepicker's values. + */ + private void updateDisplay(boolean allowEmptyDisplay) { + if (!allowEmptyDisplay && mTypedTimes.isEmpty()) { + int hour = mTimePicker.getHours(); + int minute = mTimePicker.getMinutes(); + setHour(hour, true); + setMinute(minute); + if (!mIs24HourMode) { + updateAmPmDisplay(hour < 12? AM : PM); + } + setCurrentItemShowing(mTimePicker.getCurrentItemShowing(), true, true, true); + mOkButton.setEnabled(true); + } else { + Boolean[] enteredZeros = {false, false}; + int[] values = getEnteredTime(enteredZeros); + String hourFormat = enteredZeros[0]? "%02d" : "%2d"; + String minuteFormat = (enteredZeros[1])? "%02d" : "%2d"; + String hourStr = (values[0] == -1)? mDoublePlaceholderText : + String.format(hourFormat, values[0]).replace(' ', mPlaceholderText); + String minuteStr = (values[1] == -1)? mDoublePlaceholderText : + String.format(minuteFormat, values[1]).replace(' ', mPlaceholderText); + mHourView.setText(LanguageUtils.getPersianNumbers(hourStr)); + mHourSpaceView.setText(LanguageUtils.getPersianNumbers(hourStr)); + mHourView.setTextColor(mUnselectedColor); + mMinuteView.setText(LanguageUtils.getPersianNumbers(minuteStr)); + mMinuteSpaceView.setText(LanguageUtils.getPersianNumbers(minuteStr)); + mMinuteView.setTextColor(mUnselectedColor); + if (!mIs24HourMode) { + updateAmPmDisplay(values[2]); + } + } + } + + private static int getValFromKeyCode(int keyCode) { + switch (keyCode) { + case KeyEvent.KEYCODE_0: + return 0; + case KeyEvent.KEYCODE_1: + return 1; + case KeyEvent.KEYCODE_2: + return 2; + case KeyEvent.KEYCODE_3: + return 3; + case KeyEvent.KEYCODE_4: + return 4; + case KeyEvent.KEYCODE_5: + return 5; + case KeyEvent.KEYCODE_6: + return 6; + case KeyEvent.KEYCODE_7: + return 7; + case KeyEvent.KEYCODE_8: + return 8; + case KeyEvent.KEYCODE_9: + return 9; + default: + return -1; + } + } + + /** + * Get the currently-entered time, as integer values of the hours and minutes typed. + * @param enteredZeros A size-2 boolean array, which the caller should initialize, and which + * may then be used for the caller to know whether zeros had been explicitly entered as either + * hours of minutes. This is helpful for deciding whether to show the dashes, or actual 0's. + * @return A size-3 int array. The first value will be the hours, the second value will be the + * minutes, and the third will be either TimePickerDialog.AM or TimePickerDialog.PM. + */ + private int[] getEnteredTime(Boolean[] enteredZeros) { + int amOrPm = -1; + int startIndex = 1; + if (!mIs24HourMode && isTypedTimeFullyLegal()) { + int keyCode = mTypedTimes.get(mTypedTimes.size() - 1); + if (keyCode == getAmOrPmKeyCode(AM)) { + amOrPm = AM; + } else if (keyCode == getAmOrPmKeyCode(PM)){ + amOrPm = PM; + } + startIndex = 2; + } + int minute = -1; + int hour = -1; + for (int i = startIndex; i <= mTypedTimes.size(); i++) { + int val = getValFromKeyCode(mTypedTimes.get(mTypedTimes.size() - i)); + if (i == startIndex) { + minute = val; + } else if (i == startIndex+1) { + minute += 10*val; + if (enteredZeros != null && val == 0) { + enteredZeros[1] = true; + } + } else if (i == startIndex+2) { + hour = val; + } else if (i == startIndex+3) { + hour += 10*val; + if (enteredZeros != null && val == 0) { + enteredZeros[0] = true; + } + } + } + + return new int[] {hour, minute, amOrPm}; + } + + /** + * Get the keycode value for AM and PM in the current language. + */ + private int getAmOrPmKeyCode(int amOrPm) { + // Cache the codes. + if (mAmKeyCode == -1 || mPmKeyCode == -1) { + // Find the first character in the AM/PM text that is unique. + KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); + char amChar; + char pmChar; + for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) { + amChar = "AM".toLowerCase(Locale.getDefault()).charAt(i); + pmChar = "PM".toLowerCase(Locale.getDefault()).charAt(i); + if (amChar != pmChar) { + KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar}); + // There should be 4 events: a down and up for both AM and PM. + if (events != null && events.length == 4) { + mAmKeyCode = events[0].getKeyCode(); + mPmKeyCode = events[2].getKeyCode(); + } else { + Log.e(TAG, "Unable to find keycodes for AM and PM."); + } + break; + } + } + } + if (amOrPm == AM) { + return mAmKeyCode; + } else if (amOrPm == PM) { + return mPmKeyCode; + } + + return -1; + } + + /** + * Create a tree for deciding what keys can legally be typed. + */ + private void generateLegalTimesTree() { + // Create a quick cache of numbers to their keycodes. + int k0 = KeyEvent.KEYCODE_0; + int k1 = KeyEvent.KEYCODE_1; + int k2 = KeyEvent.KEYCODE_2; + int k3 = KeyEvent.KEYCODE_3; + int k4 = KeyEvent.KEYCODE_4; + int k5 = KeyEvent.KEYCODE_5; + int k6 = KeyEvent.KEYCODE_6; + int k7 = KeyEvent.KEYCODE_7; + int k8 = KeyEvent.KEYCODE_8; + int k9 = KeyEvent.KEYCODE_9; + + // The root of the tree doesn't contain any numbers. + mLegalTimesTree = new Node(); + if (mIs24HourMode) { + // We'll be re-using these nodes, so we'll save them. + Node minuteFirstDigit = new Node(k0, k1, k2, k3, k4, k5); + Node minuteSecondDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9); + // The first digit must be followed by the second digit. + minuteFirstDigit.addChild(minuteSecondDigit); + + // The first digit may be 0-1. + Node firstDigit = new Node(k0, k1); + mLegalTimesTree.addChild(firstDigit); + + // When the first digit is 0-1, the second digit may be 0-5. + Node secondDigit = new Node(k0, k1, k2, k3, k4, k5); + firstDigit.addChild(secondDigit); + // We may now be followed by the first minute digit. E.g. 00:09, 15:58. + secondDigit.addChild(minuteFirstDigit); + + // When the first digit is 0-1, and the second digit is 0-5, the third digit may be 6-9. + Node thirdDigit = new Node(k6, k7, k8, k9); + // The time must now be finished. E.g. 0:55, 1:08. + secondDigit.addChild(thirdDigit); + + // When the first digit is 0-1, the second digit may be 6-9. + secondDigit = new Node(k6, k7, k8, k9); + firstDigit.addChild(secondDigit); + // We must now be followed by the first minute digit. E.g. 06:50, 18:20. + secondDigit.addChild(minuteFirstDigit); + + // The first digit may be 2. + firstDigit = new Node(k2); + mLegalTimesTree.addChild(firstDigit); + + // When the first digit is 2, the second digit may be 0-3. + secondDigit = new Node(k0, k1, k2, k3); + firstDigit.addChild(secondDigit); + // We must now be followed by the first minute digit. E.g. 20:50, 23:09. + secondDigit.addChild(minuteFirstDigit); + + // When the first digit is 2, the second digit may be 4-5. + secondDigit = new Node(k4, k5); + firstDigit.addChild(secondDigit); + // We must now be followd by the last minute digit. E.g. 2:40, 2:53. + secondDigit.addChild(minuteSecondDigit); + + // The first digit may be 3-9. + firstDigit = new Node(k3, k4, k5, k6, k7, k8, k9); + mLegalTimesTree.addChild(firstDigit); + // We must now be followed by the first minute digit. E.g. 3:57, 8:12. + firstDigit.addChild(minuteFirstDigit); + } else { + // We'll need to use the AM/PM node a lot. + // Set up AM and PM to respond to "a" and "p". + Node ampm = new Node(getAmOrPmKeyCode(AM), getAmOrPmKeyCode(PM)); + + // The first hour digit may be 1. + Node firstDigit = new Node(k1); + mLegalTimesTree.addChild(firstDigit); + // We'll allow quick input of on-the-hour times. E.g. 1pm. + firstDigit.addChild(ampm); + + // When the first digit is 1, the second digit may be 0-2. + Node secondDigit = new Node(k0, k1, k2); + firstDigit.addChild(secondDigit); + // Also for quick input of on-the-hour times. E.g. 10pm, 12am. + secondDigit.addChild(ampm); + + // When the first digit is 1, and the second digit is 0-2, the third digit may be 0-5. + Node thirdDigit = new Node(k0, k1, k2, k3, k4, k5); + secondDigit.addChild(thirdDigit); + // The time may be finished now. E.g. 1:02pm, 1:25am. + thirdDigit.addChild(ampm); + + // When the first digit is 1, the second digit is 0-2, and the third digit is 0-5, + // the fourth digit may be 0-9. + Node fourthDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9); + thirdDigit.addChild(fourthDigit); + // The time must be finished now. E.g. 10:49am, 12:40pm. + fourthDigit.addChild(ampm); + + // When the first digit is 1, and the second digit is 0-2, the third digit may be 6-9. + thirdDigit = new Node(k6, k7, k8, k9); + secondDigit.addChild(thirdDigit); + // The time must be finished now. E.g. 1:08am, 1:26pm. + thirdDigit.addChild(ampm); + + // When the first digit is 1, the second digit may be 3-5. + secondDigit = new Node(k3, k4, k5); + firstDigit.addChild(secondDigit); + + // When the first digit is 1, and the second digit is 3-5, the third digit may be 0-9. + thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9); + secondDigit.addChild(thirdDigit); + // The time must be finished now. E.g. 1:39am, 1:50pm. + thirdDigit.addChild(ampm); + + // The hour digit may be 2-9. + firstDigit = new Node(k2, k3, k4, k5, k6, k7, k8, k9); + mLegalTimesTree.addChild(firstDigit); + // We'll allow quick input of on-the-hour-times. E.g. 2am, 5pm. + firstDigit.addChild(ampm); + + // When the first digit is 2-9, the second digit may be 0-5. + secondDigit = new Node(k0, k1, k2, k3, k4, k5); + firstDigit.addChild(secondDigit); + + // When the first digit is 2-9, and the second digit is 0-5, the third digit may be 0-9. + thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9); + secondDigit.addChild(thirdDigit); + // The time must be finished now. E.g. 2:57am, 9:30pm. + thirdDigit.addChild(ampm); + } + } + + /** + * Simple node class to be used for traversal to check for legal times. + * mLegalKeys represents the keys that can be typed to get to the node. + * mChildren are the children that can be reached from this node. + */ + private static class Node { + private int[] mLegalKeys; + private ArrayList mChildren; + + public Node(int... legalKeys) { + mLegalKeys = legalKeys; + mChildren = new ArrayList<>(); + } + + public void addChild(Node child) { + mChildren.add(child); + } + + public boolean containsKey(int key) { + for (int mLegalKey : mLegalKeys) { + if (mLegalKey == key) { + return true; + } + } + return false; + } + + public Node canReach(int key) { + if (mChildren == null) { + return null; + } + for (Node child : mChildren) { + if (child.containsKey(key)) { + return child; + } + } + return null; + } + } + + private class KeyboardListener implements OnKeyListener { + @Override + public boolean onKey(View v, int keyCode, KeyEvent event) { + return event.getAction() == KeyEvent.ACTION_UP && processKeyUp(keyCode); + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/LanguageUtils.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/LanguageUtils.java new file mode 100644 index 0000000..1acd7a3 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/LanguageUtils.java @@ -0,0 +1,48 @@ +package mohammadaminha.com.widgets.Date_Picker.utils; + +import java.util.ArrayList; + +public class LanguageUtils { + + public static String getPersianNumbers(String string) { + string = string.replace("0", "۰"); + string = string.replace("1", "١"); + string = string.replace("2", "۲"); + string = string.replace("3", "۳"); + string = string.replace("4", "۴"); + string = string.replace("5", "۵"); + string = string.replace("6", "۶"); + string = string.replace("7", "۷"); + string = string.replace("8", "۸"); + string = string.replace("9", "۹"); + return string; + } + + public static void getPersianNumbers(String[] strings) { + for (int i=0; i getPersianNumbers(ArrayList strings) { + for (int i=0; i Persian(Shamsi) calendar + *

    + *

    + *

    + * The calendar consists of 12 months, the first six of which are 31 days, the + * next five 30 days, and the final month 29 days in a normal year and 30 days + * in a leap year. + *

    + *

    + * As one of the few calendars designed in the era of accurate positional + * astronomy, the Persian calendar uses a very complex leap year structure which + * makes it the most accurate solar calendar in use today. Years are grouped + * into cycles which begin with four normal years after which every fourth + * subsequent year in the cycle is a leap year. Cycles are grouped into grand + * cycles of either 128 years (composed of cycles of 29, 33, 33, and 33 years) + * or 132 years, containing cycles of of 29, 33, 33, and 37 years. A great grand + * cycle is composed of 21 consecutive 128 year grand cycles and a final 132 + * grand cycle, for a total of 2820 years. The pattern of normal and leap years + * which began in 1925 will not repeat until the year 4745! + *

    + *

    Each 2820 year great grand cycle contains 2137 normal years of 365 days + * and 683 leap years of 366 days, with the average year length over the great + * grand cycle of 365.24219852. So close is this to the actual solar tropical + * year of 365.24219878 days that the Persian calendar accumulates an error of + * one day only every 3.8 million years. As a purely solar calendar, months are + * not synchronized with the phases of the Moon.

    + *

    + *

    + *

    + *

    + * PersianCalendar by extending Default GregorianCalendar + * provides capabilities such as: + *

    + *

    + *

    + *

    + *

  • you can set the date in Persian by setPersianDate(persianYear, + * persianMonth, persianDay) and get the Gregorian date or vice versa
  • + *

    + *

    + *
  • determine is the current date is Leap year in persian calendar or not by + * IsPersianLeapYear()
  • + *

    + *

    + *
  • getPersian short and long Date String getPersianShortDate() and + * getPersianLongDate you also can set delimiter to assign delimiter of returned + * dateString
  • + *

    + *

    + *
  • Parse string based on assigned delimiter
  • + *

    + *

    + *

    + *

    + *

    + *

    + *

    + * Example + *

    + *

    + *

    + *

    + *

    + * {@code
    + *       PersianCalendar persianCal = new PersianCalendar();
    + *       System.out.println(persianCal.getPersianShortDate());
    + *
    + *       persianCal.set(1982, Calendar.MAY, 22);
    + *       System.out.println(persianCal.getPersianShortDate());
    + *
    + *       persianCal.setDelimiter(" , ");
    + *       persianCal.parse("1361 , 03 , 01");
    + *       System.out.println(persianCal.getPersianShortDate());
    + *
    + *       persianCal.setPersianDate(1361, 3, 1);
    + *       System.out.println(persianCal.getPersianLongDate());
    + *       System.out.println(persianCal.getTime());
    + *
    + *       persianCal.addPersianDate(Calendar.MONTH, 33);
    + *       persianCal.addPersianDate(Calendar.YEAR, 5);
    + *       persianCal.addPersianDate(Calendar.DATE, 50);
    + *
    + * }
    + *
    + * 
    + * @author Morteza  contact: Mortezaadi@gmail.com
    + * @version 1.1
    + */
    +public class PersianCalendar extends GregorianCalendar {
    +
    +	private static final long serialVersionUID = 5541422440580682494L;
    +
    +	private int persianYear;
    +	private int persianMonth;
    +	private int persianDay;
    +	// use to seperate PersianDate's field and also Parse the DateString based
    +	// on this delimiter
    +	private String delimiter = "/";
    +
    +	private long convertToMilis(long julianDate) {
    +		return PersianCalendarConstants.MILLIS_JULIAN_EPOCH + julianDate * PersianCalendarConstants.MILLIS_OF_A_DAY
    +				+ PersianCalendarUtils.ceil(getTimeInMillis() - PersianCalendarConstants.MILLIS_JULIAN_EPOCH, PersianCalendarConstants.MILLIS_OF_A_DAY);
    +	}
    +
    +	/**
    +	 * default constructor
    +	 * 

    + * most of the time we don't care about TimeZone when we persisting Date or + * doing some calculation on date. Default TimeZone was set to + * "GMT" in order to make developer to work more convenient with + * the library; however you can change the TimeZone as you do in + * GregorianCalendar by calling setTimeZone() + */ + public PersianCalendar(long millis) { + setTimeInMillis(millis); + } + + /** + * default constructor + *

    + * most of the time we don't care about TimeZone when we persisting Date or + * doing some calculation on date. Default TimeZone was set to + * "GMT" in order to make developer to work more convenient with + * the library; however you can change the TimeZone as you do in + * GregorianCalendar by calling setTimeZone() + */ + public PersianCalendar() { + setTimeZone(TimeZone.getTimeZone("Iran")); + } + + /** + * Calculate persian date from current Date and populates the corresponding + * fields(persianYear, persianMonth, persianDay) + */ + private void calculatePersianDate() { + long julianDate = ((long) Math.floor((getTimeInMillis() - PersianCalendarConstants.MILLIS_JULIAN_EPOCH)) / PersianCalendarConstants.MILLIS_OF_A_DAY); + long PersianRowDate = PersianCalendarUtils.julianToPersian(julianDate); + long year = PersianRowDate >> 16; + int month = (int) (PersianRowDate & 0xff00) >> 8; + int day = (int) (PersianRowDate & 0xff); + this.persianYear = (int) (year > 0 ? year : year - 1); + this.persianMonth = month; + this.persianDay = day; + } + + /** + * Determines if the given year is a leap year in persian calendar. Returns + * true if the given year is a leap year. + * + * @return boolean + */ + public boolean isPersianLeapYear() { + // calculatePersianDate(); + return PersianCalendarUtils.isPersianLeapYear(this.persianYear); + } + + /** + * set the persian date it converts PersianDate to the Julian and assigned + * equivalent milliseconds to the instance + * + * @param persianYear + * @param persianMonth + * @param persianDay + */ + public void setPersianDate(int persianYear, int persianMonth, int persianDay) { + persianMonth += 1; // TODO + this.persianYear = persianYear; + this.persianMonth = persianMonth; + this.persianDay = persianDay; + setTimeInMillis(convertToMilis(PersianCalendarUtils.persianToJulian(this.persianYear > 0 ? this.persianYear : this.persianYear + 1, this.persianMonth - 1, this.persianDay))); + } + + public int getPersianYear() { + // calculatePersianDate(); + return this.persianYear; + } + + /** + * @return int persian month number + */ + public int getPersianMonth() { + // calculatePersianDate(); + return this.persianMonth; + } + + /** + * @return String persian month name + */ + public String getPersianMonthName() { + // calculatePersianDate(); + return PersianCalendarConstants.persianMonthNames[this.persianMonth]; + } + + /** + * @return int Persian day in month + */ + public int getPersianDay() { + // calculatePersianDate(); + return this.persianDay; + } + + /** + * @return String Name of the day in week + */ + public String getPersianWeekDayName() { + switch (get(DAY_OF_WEEK)) { + case SATURDAY: + return PersianCalendarConstants.persianWeekDays[0]; + case SUNDAY: + return PersianCalendarConstants.persianWeekDays[1]; + case MONDAY: + return PersianCalendarConstants.persianWeekDays[2]; + case TUESDAY: + return PersianCalendarConstants.persianWeekDays[3]; + case WEDNESDAY: + return PersianCalendarConstants.persianWeekDays[4]; + case THURSDAY: + return PersianCalendarConstants.persianWeekDays[5]; + default: + return PersianCalendarConstants.persianWeekDays[6]; + } + + } + + /** + * @return String of Persian Date ex: شنبه 01 خرداد 1361 + */ + public String getPersianLongDate() { + return getPersianWeekDayName() + " " + this.persianDay + " " + getPersianMonthName() + " " + this.persianYear; + } + + public String getPersianLongDateAndTime() { + return getPersianLongDate() + " ساعت " + get(HOUR_OF_DAY) + ":" + get(MINUTE) + ":" + get(SECOND); + } + + /** + * @return String of persian date formatted by + * 'YYYY[delimiter]mm[delimiter]dd' default delimiter is '/' + */ + private String getPersianShortDate() { + // calculatePersianDate(); + return "" + formatToMilitary(this.persianYear) + delimiter + formatToMilitary(getPersianMonth() + 1) + delimiter + formatToMilitary(this.persianDay); + } + + public String getPersianShortDateTime() { + return + //formatToMilitary(this.persianYear) + delimiter + + //formatToMilitary(getPersianMonth() + 1) + delimiter + + //formatToMilitary(this.persianDay) + " " + + (this.persianYear) + delimiter + + (getPersianMonth() + 1) + delimiter + + (this.persianDay) + " " + + formatToMilitary(this.get(HOUR_OF_DAY)) + + ":" + + formatToMilitary(get(MINUTE)); + } + + public String getPersianHour() { + return + formatToMilitary(get(HOUR_OF_DAY)) + + ":" + + formatToMilitary(get(MINUTE)); + } + + public String formatToMilitary(int i) { + + return (i < 10) ? "0" + i : String.valueOf(i); + } + + /** + * add specific amout of fields to the current date for now doesnt handle + * before 1 farvardin hejri (before epoch) + * + * @param field + * @param amount

    +	 *                                                          Usage:
    +	 *                                                          {@code
    +	 *                                                          addPersianDate(Calendar.YEAR, 2);
    +	 *                                                          addPersianDate(Calendar.MONTH, 3);
    +	 *                                                          }
    +	 *                                                         
    + *

    + * u can also use Calendar.HOUR_OF_DAY,Calendar.MINUTE, + * Calendar.SECOND, Calendar.MILLISECOND etc + */ + // + public void addPersianDate(int field, int amount) { + if (amount == 0) { + return; // Do nothing! + } + + if (field < 0 || field >= ZONE_OFFSET) { + throw new IllegalArgumentException(); + } + + if (field == YEAR) { + setPersianDate(this.persianYear + amount, getPersianMonth() + 1, this.persianDay); + return; + } else if (field == MONTH) { + setPersianDate(this.persianYear + ((getPersianMonth() + 1 + amount) / 12), (getPersianMonth() + 1 + amount) % 12, this.persianDay); + return; + } + add(field, amount); + calculatePersianDate(); + } + + /** + *

    +	 *    use {@link PersianDateParser} to parse string
    +	 *    and get the Persian Date.
    +	 * 
    + * + * @param dateString + * @see PersianDateParser + */ + public void parse(String dateString) { + PersianCalendar p = new PersianDateParser(dateString, delimiter).getPersianDate(); + setPersianDate(p.getPersianYear(), p.getPersianMonth(), p.getPersianDay()); + } + + public String getDelimiter() { + return delimiter; + } + + /** + * assign delimiter to use as a separator of date fields. + * + * @param delimiter + */ + public void setDelimiter(String delimiter) { + this.delimiter = delimiter; + } + + @Override + public String toString() { + String str = super.toString(); + return str.substring(0, str.length() - 1) + ",PersianDate=" + getPersianShortDate() + "]"; + } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public void set(int field, int value) { + super.set(field, value); + calculatePersianDate(); + } + + @Override + public void setTimeInMillis(long millis) { + super.setTimeInMillis(millis); + calculatePersianDate(); + } + + @Override + public void setTimeZone(TimeZone zone) { + super.setTimeZone(zone); + calculatePersianDate(); + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/PersianCalendarConstants.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/PersianCalendarConstants.java new file mode 100644 index 0000000..a13208c --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/PersianCalendarConstants.java @@ -0,0 +1,46 @@ +package mohammadaminha.com.widgets.Date_Picker.utils; + +/** + * + * @author Morteza contact: Mortezaadi@gmail.com + * @version 1.0 + */ +class PersianCalendarConstants { + + // 00:00:00 UTC (Gregorian) Julian day 0, + // 0 milliseconds since 1970-01-01 + public static final long MILLIS_JULIAN_EPOCH = -210866803200000L; + // Milliseconds of a day calculated by 24L(hours) * 60L(minutes) * + // 60L(seconds) * 1000L(mili); + public static final long MILLIS_OF_A_DAY = 86400000L; + + /** + * The JDN of 1 Farvardin 1; Equivalent to March 19, 622 A.D. + */ + public static final long PERSIAN_EPOCH = 1948321; + + public static final String[] persianMonthNames = { "\u0641\u0631\u0648\u0631\u062f\u06cc\u0646", // Farvardin + "\u0627\u0631\u062f\u06cc\u0628\u0647\u0634\u062a", // Ordibehesht + "\u062e\u0631\u062f\u0627\u062f", // Khordad + "\u062a\u06cc\u0631", // Tir + "\u0645\u0631\u062f\u0627\u062f", // Mordad + "\u0634\u0647\u0631\u06cc\u0648\u0631", // Shahrivar + "\u0645\u0647\u0631", // Mehr + "\u0622\u0628\u0627\u0646", // Aban + "\u0622\u0630\u0631", // Azar + "\u062f\u06cc", // Dey + "\u0628\u0647\u0645\u0646", // Bahman + "\u0627\u0633\u0641\u0646\u062f" // Esfand + }; + + public static final String[] persianWeekDays = { "\u0634\u0646\u0628\u0647", // Shanbeh + "\u06cc\u06a9\u200c\u0634\u0646\u0628\u0647", // Yekshanbeh + "\u062f\u0648\u0634\u0646\u0628\u0647", // Doshanbeh + "\u0633\u0647\u200c\u0634\u0646\u0628\u0647", // Sehshanbeh + "\u0686\u0647\u0627\u0631\u0634\u0646\u0628\u0647", // Chaharshanbeh + "\u067e\u0646\u062c\u200c\u0634\u0646\u0628\u0647", // Panjshanbeh + "\u062c\u0645\u0639\u0647" // jome + }; + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/PersianCalendarUtils.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/PersianCalendarUtils.java new file mode 100644 index 0000000..b54a56e --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/PersianCalendarUtils.java @@ -0,0 +1,80 @@ +package mohammadaminha.com.widgets.Date_Picker.utils; + +/** + * algorithms for converting Julian days to the Persian calendar, and vice versa + * are adopted from couprie.nl written in + * VB. The algorithms is not exactly the same as its original. I've done some + * minor changes in the sake of performances and corrected some bugs. + * + * @author Morteza contact: Mortezaadi@gmail.com + * @version 1.0 + * + */ +public class PersianCalendarUtils { + + /** + * Converts a provided Persian (Shamsi) date to the Julian Day Number (i.e. + * the number of days since January 1 in the year 4713 BC). Since the + * Persian calendar is a highly regular calendar, converting to and from a + * Julian Day Number is not as difficult as it looks. Basically it's a + * mather of dividing, rounding and multiplying. This routine uses Julian + * Day Number 1948321 as focal point, since that Julian Day Number + * corresponds with 1 Farvardin (1) 1. + * + * @param year + * int persian year + * @param month + * int persian month + * @param day + * int persian day + * @return long + */ + public static long persianToJulian(long year, int month, int day) { + return 365L * ((ceil(year - 474L, 2820D) + 474L) - 1L) + ((long) Math.floor((682L * (ceil(year - 474L, 2820D) + 474L) - 110L) / 2816D)) + (PersianCalendarConstants.PERSIAN_EPOCH - 1L) + 1029983L + * ((long) Math.floor((year - 474L) / 2820D)) + (month < 7 ? 31 * month : 30 * month + 6) + day; + } + + /** + * Calculate whether current year is Leap year in persian or not + * + * @return boolean + */ + public static boolean isPersianLeapYear(int persianYear) { + return PersianCalendarUtils.ceil((38D + (PersianCalendarUtils.ceil(persianYear - 474L, 2820L) + 474L)) * 682D, 2816D) < 682L; + } + + /** + * Converts a provided Julian Day Number (i.e. the number of days since + * January 1 in the year 4713 BC) to the Persian (Shamsi) date. Since the + * Persian calendar is a highly regular calendar, converting to and from a + * Julian Day Number is not as difficult as it looks. Basically it's a + * mather of dividing, rounding and multiplying. + * + * @param julianDate + * @return long + */ + public static long julianToPersian(long julianDate) { + long persianEpochInJulian = julianDate - persianToJulian(475L, 0, 1); + long cyear = ceil(persianEpochInJulian, 1029983D); + long ycycle = cyear != 1029982L ? ((long) Math.floor((2816D * (double) cyear + 1031337D) / 1028522D)) : 2820L; + long year = 474L + 2820L * ((long) Math.floor(persianEpochInJulian / 1029983D)) + ycycle; + long aux = (1L + julianDate) - persianToJulian(year, 0, 1); + int month = (int) (aux > 186L ? Math.ceil((double) (aux - 6L) / 30D) - 1 : Math.ceil((double) aux / 31D) - 1); + int day = (int) (julianDate - (persianToJulian(year, month, 1) - 1L)); + return (year << 16) | (month << 8) | day; + } + + /** + * Ceil function in original algorithm + * + * @param double1 + * @param double2 + * @return long + */ + public static long ceil(double double1, double double2) { + return (long) (double1 - double2 * Math.floor(double1 / double2)); + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/PersianDateParser.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/PersianDateParser.java new file mode 100644 index 0000000..208699a --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/PersianDateParser.java @@ -0,0 +1,162 @@ +package mohammadaminha.com.widgets.Date_Picker.utils; + +/** + * Parses text from the beginning of the given string to produce a + * PersianCalendar. + * + *

    + * See the {@link #getPersianDate()} method for more information on date + * parsing. + * + *

    + *                Example
    + *                     
    + *  {@code
    + *    PersianCalendar pCal =
    + *     new PersianDateParser("1361/3/1").getPersianDate();             
    + *  }
    + * 
    + * + * @author Morteza contact: Mortezaadi@gmail.com + * @version 1.0 + */ +class PersianDateParser { + + private String dateString; + private String delimiter = "/"; + + /** + *
    +	 * construct parser with date string assigned
    +	 * the default delimiter is '/'.
    +	 * 
    +	 * To assign deferment delimiter use:
    +	 * {@link #PersianDateParser(String dateString, String delimiter)}
    +	 * 
    +	 *                     Example
    +	 *                     
    +	 *  {@code
    +	 *    PersianCalendar pCal =
    +	 *     new PersianDateParser("1361/3/1").getPersianDate();             
    +	 *  }
    +	 * 
    + * + * @param dateString + */ + private PersianDateParser(String dateString) { + this.dateString = dateString; + } + + /** + *
    +	 * construct parser with date string assigned
    +	 * the default delimiter is '/'. with this constructor
    +	 * you can set different delimiter to parse the date
    +	 * based on this delimiter.
    +	 * see also:
    +	 * {@link #PersianDateParser(String dateString)}
    +	 * 
    +	 *                     Example
    +	 *                     
    +	 *  {@code
    +	 *    PersianCalendar pCal =
    +	 *     new PersianDateParser("1361-3-1","-").getPersianDate();             
    +	 *  }
    +	 * 
    + * + * @param dateString + * @param delimiter + */ + public PersianDateParser(String dateString, String delimiter) { + this(dateString); + this.delimiter = delimiter; + } + + /** + * Produce the PersianCalendar object from given DateString throws Exception + * if couldn't parse the text. + * + * @return PersianCalendar object + * @exception RuntimeException + */ + public PersianCalendar getPersianDate() { + + checkDateStringInitialValidation(); + + String tokens[] = splitDateString(normalizeDateString(dateString)); + int year = Integer.parseInt(tokens[0]); + int month = Integer.parseInt(tokens[1]); + int day = Integer.parseInt(tokens[2]); + + checkPersianDateValidation(year, month, day); + + PersianCalendar pCal = new PersianCalendar(); + pCal.setPersianDate(year, month, day); + + return pCal; + } + + /** + * validate the given date + * + * @param year + * @param month + * @param day + */ + private void checkPersianDateValidation(int year, int month, int day) { + if (year < 1) + throw new RuntimeException("year is not valid"); + if (month < 1 || month > 12) + throw new RuntimeException("month is not valid"); + if (day < 1 || day > 31) + throw new RuntimeException("day is not valid"); + if (month > 6 && day == 31) + throw new RuntimeException("day is not valid"); + if (month == 12 && day == 30 && !PersianCalendarUtils.isPersianLeapYear(year)) + throw new RuntimeException("day is not valid " + year + " is not a leap year"); + } + + /** + * planned for further calculation before parsing the text + * + * @param dateString + * @return + */ + private String normalizeDateString(String dateString) { + // dateString = dateString.replace("-", delimiter); + return dateString; + } + + private String[] splitDateString(String dateString) { + String tokens[] = dateString.split(delimiter); + if (tokens.length != 3) + throw new RuntimeException("wrong date:" + dateString + " is not a Persian Date or can not be parsed"); + return tokens; + } + + private void checkDateStringInitialValidation() { + if (dateString == null) + throw new RuntimeException("input didn't assing please use setDateString()"); + // if(dateString.length()>10) + // throw new RuntimeException("wrong date:" + dateString + + // " is not a Persian Date or can not be parsed" ); + } + + public String getDateString() { + return dateString; + } + + public void setDateString(String dateString) { + this.dateString = dateString; + } + + public String getDelimiter() { + return delimiter; + } + + public void setDelimiter(String delimiter) { + this.delimiter = delimiter; + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/TimeZones.java b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/TimeZones.java new file mode 100644 index 0000000..30e9c86 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Date_Picker/utils/TimeZones.java @@ -0,0 +1,161 @@ +package mohammadaminha.com.widgets.Date_Picker.utils; + +import java.util.TimeZone; + +/** + * This is simply all the available TimeZones from java.util.TimeZone as type + * safe enum + */ +public enum TimeZones { + DEFAULT(TimeZone.getDefault()), + // default JDK 1.4.2 time zones + ACT(TimeZone.getTimeZone("ACT")), AET(TimeZone.getTimeZone("AET")), AFRICA_ABIDJAN(TimeZone.getTimeZone("Africa/Abidjan")), AFRICA_ACCRA(TimeZone.getTimeZone("Africa/Accra")), AFRICA_ADDIS_ABABA(TimeZone + .getTimeZone("Africa/Addis_Ababa")), AFRICA_ALGIERS(TimeZone.getTimeZone("Africa/Algiers")), AFRICA_ASMERA(TimeZone.getTimeZone("Africa/Asmera")), AFRICA_BAMAKO(TimeZone.getTimeZone("Africa/Bamako")), AFRICA_BANGUI(TimeZone + .getTimeZone("Africa/Bangui")), AFRICA_BANJUL(TimeZone.getTimeZone("Africa/Banjul")), AFRICA_BISSAU(TimeZone.getTimeZone("Africa/Bissau")), AFRICA_BLANTYRE(TimeZone.getTimeZone("Africa/Blantyre")), AFRICA_BRAZZAVILLE(TimeZone + .getTimeZone("Africa/Brazzaville")), AFRICA_BUJUMBURA(TimeZone.getTimeZone("Africa/Bujumbura")), AFRICA_CAIRO(TimeZone.getTimeZone("Africa/Cairo")), AFRICA_CASABLANCA(TimeZone.getTimeZone("Africa/Casablanca")), AFRICA_CEUTA( + TimeZone.getTimeZone("Africa/Ceuta")), AFRICA_CONAKRY(TimeZone.getTimeZone("Africa/Conakry")), AFRICA_DAKAR(TimeZone.getTimeZone("Africa/Dakar")), AFRICA_DAR_ES_SALAAM(TimeZone.getTimeZone("Africa/Dar_es_Salaam")), AFRICA_DJIBOUTI( + TimeZone.getTimeZone("Africa/Djibouti")), AFRICA_DOUALA(TimeZone.getTimeZone("Africa/Douala")), AFRICA_EL_AAIUN(TimeZone.getTimeZone("Africa/El_Aaiun")), AFRICA_FREETOWN(TimeZone.getTimeZone("Africa/Freetown")), AFRICA_GABORONE( + TimeZone.getTimeZone("Africa/Gaborone")), AFRICA_HARARE(TimeZone.getTimeZone("Africa/Harare")), AFRICA_JOHANNESBURG(TimeZone.getTimeZone("Africa/Johannesburg")), AFRICA_KAMPALA(TimeZone.getTimeZone("Africa/Kampala")), AFRICA_KHARTOUM( + TimeZone.getTimeZone("Africa/Khartoum")), AFRICA_KIGALI(TimeZone.getTimeZone("Africa/Kigali")), AFRICA_KINSHASA(TimeZone.getTimeZone("Africa/Kinshasa")), AFRICA_LAGOS(TimeZone.getTimeZone("Africa/Lagos")), AFRICA_LIBREVILLE( + TimeZone.getTimeZone("Africa/Libreville")), AFRICA_LOME(TimeZone.getTimeZone("Africa/Lome")), AFRICA_LUANDA(TimeZone.getTimeZone("Africa/Luanda")), AFRICA_LUBUMBASHI(TimeZone.getTimeZone("Africa/Lubumbashi")), AFRICA_LUSAKA( + TimeZone.getTimeZone("Africa/Lusaka")), AFRICA_MALABO(TimeZone.getTimeZone("Africa/Malabo")), AFRICA_MAPUTO(TimeZone.getTimeZone("Africa/Maputo")), AFRICA_MASERU(TimeZone.getTimeZone("Africa/Maseru")), AFRICA_MBABANE(TimeZone + .getTimeZone("Africa/Mbabane")), AFRICA_MOGADISHU(TimeZone.getTimeZone("Africa/Mogadishu")), AFRICA_MONROVIA(TimeZone.getTimeZone("Africa/Monrovia")), AFRICA_NAIROBI(TimeZone.getTimeZone("Africa/Nairobi")), AFRICA_NDJAMENA( + TimeZone.getTimeZone("Africa/Ndjamena")), AFRICA_NIAMEY(TimeZone.getTimeZone("Africa/Niamey")), AFRICA_NOUAKCHOTT(TimeZone.getTimeZone("Africa/Nouakchott")), AFRICA_OUAGADOUGOU(TimeZone.getTimeZone("Africa/Ouagadougou")), AFRICA_PORTO_NOVO( + TimeZone.getTimeZone("Africa/Porto-Novo")), AFRICA_SAO_TOME(TimeZone.getTimeZone("Africa/Sao_Tome")), AFRICA_TIMBUKTU(TimeZone.getTimeZone("Africa/Timbuktu")), AFRICA_TRIPOLI(TimeZone.getTimeZone("Africa/Tripoli")), AFRICA_TUNIS( + TimeZone.getTimeZone("Africa/Tunis")), AFRICA_WINDHOEK(TimeZone.getTimeZone("Africa/Windhoek")), AGT(TimeZone.getTimeZone("AGT")), AMERICA_ADAK(TimeZone.getTimeZone("America/Adak")), AMERICA_ANCHORAGE(TimeZone + .getTimeZone("America/Anchorage")), AMERICA_ANGUILLA(TimeZone.getTimeZone("America/Anguilla")), AMERICA_ANTIGUA(TimeZone.getTimeZone("America/Antigua")), AMERICA_ARAGUAINA(TimeZone.getTimeZone("America/Araguaina")), AMERICA_ARUBA( + TimeZone.getTimeZone("America/Aruba")), AMERICA_ASUNCION(TimeZone.getTimeZone("America/Asuncion")), AMERICA_ATKA(TimeZone.getTimeZone("America/Atka")), AMERICA_BARBADOS(TimeZone.getTimeZone("America/Barbados")), AMERICA_BELEM( + TimeZone.getTimeZone("America/Belem")), AMERICA_BELIZE(TimeZone.getTimeZone("America/Belize")), AMERICA_BOA_VISTA(TimeZone.getTimeZone("America/Boa_Vista")), AMERICA_BOGOTA(TimeZone.getTimeZone("America/Bogota")), AMERICA_BOISE( + TimeZone.getTimeZone("America/Boise")), AMERICA_BUENOS_AIRES(TimeZone.getTimeZone("America/Buenos_Aires")), AMERICA_CAMBRIDGE_BAY(TimeZone.getTimeZone("America/Cambridge_Bay")), AMERICA_CANCUN(TimeZone + .getTimeZone("America/Cancun")), AMERICA_CARACAS(TimeZone.getTimeZone("America/Caracas")), AMERICA_CATAMARCA(TimeZone.getTimeZone("America/Catamarca")), AMERICA_CAYENNE(TimeZone.getTimeZone("America/Cayenne")), AMERICA_CAYMAN( + TimeZone.getTimeZone("America/Cayman")), AMERICA_CHICAGO(TimeZone.getTimeZone("America/Chicago")), AMERICA_CHIHUAHUA(TimeZone.getTimeZone("America/Chihuahua")), AMERICA_CORDOBA(TimeZone.getTimeZone("America/Cordoba")), AMERICA_COSTA_RICA( + TimeZone.getTimeZone("America/Costa_Rica")), AMERICA_CUIABA(TimeZone.getTimeZone("America/Cuiaba")), AMERICA_CURACAO(TimeZone.getTimeZone("America/Curacao")), AMERICA_DANMARKSHAVN(TimeZone.getTimeZone("America/Danmarkshavn")), AMERICA_DAWSON( + TimeZone.getTimeZone("America/Dawson")), AMERICA_DAWSON_CREEK(TimeZone.getTimeZone("America/Dawson_Creek")), AMERICA_DENVER(TimeZone.getTimeZone("America/Denver")), AMERICA_DETROIT(TimeZone.getTimeZone("America/Detroit")), AMERICA_DOMINICA( + TimeZone.getTimeZone("America/Dominica")), AMERICA_EDMONTON(TimeZone.getTimeZone("America/Edmonton")), AMERICA_EIRUNEPE(TimeZone.getTimeZone("America/Eirunepe")), AMERICA_EL_SALVADOR(TimeZone.getTimeZone("America/El_Salvador")), AMERICA_ENSENADA( + TimeZone.getTimeZone("America/Ensenada")), AMERICA_FORT_WAYNE(TimeZone.getTimeZone("America/Fort_Wayne")), AMERICA_FORTALEZA(TimeZone.getTimeZone("America/Fortaleza")), AMERICA_GLACE_BAY(TimeZone + .getTimeZone("America/Glace_Bay")), AMERICA_GODTHAB(TimeZone.getTimeZone("America/Godthab")), AMERICA_GOOSE_BAY(TimeZone.getTimeZone("America/Goose_Bay")), AMERICA_GRAND_TURK(TimeZone.getTimeZone("America/Grand_Turk")), AMERICA_GRENADA( + TimeZone.getTimeZone("America/Grenada")), AMERICA_GUADELOUPE(TimeZone.getTimeZone("America/Guadeloupe")), AMERICA_GUATEMALA(TimeZone.getTimeZone("America/Guatemala")), AMERICA_GUAYAQUIL(TimeZone.getTimeZone("America/Guayaquil")), AMERICA_GUYANA( + TimeZone.getTimeZone("America/Guyana")), AMERICA_HALIFAX(TimeZone.getTimeZone("America/Halifax")), AMERICA_HAVANA(TimeZone.getTimeZone("America/Havana")), AMERICA_HERMOSILLO(TimeZone.getTimeZone("America/Hermosillo")), AMERICA_INDIANA_INDIANAPOLIS( + TimeZone.getTimeZone("America/Indiana/Indianapolis")), AMERICA_INDIANA_KNOX(TimeZone.getTimeZone("America/Indiana/Knox")), AMERICA_INDIANA_MARENGO(TimeZone.getTimeZone("America/Indiana/Marengo")), AMERICA_INDIANA_VEVAY(TimeZone + .getTimeZone("America/Indiana/Vevay")), AMERICA_INDIANAPOLIS(TimeZone.getTimeZone("America/Indianapolis")), AMERICA_INUVIK(TimeZone.getTimeZone("America/Inuvik")), AMERICA_IQALUIT(TimeZone.getTimeZone("America/Iqaluit")), AMERICA_JAMAICA( + TimeZone.getTimeZone("America/Jamaica")), AMERICA_JUJUY(TimeZone.getTimeZone("America/Jujuy")), AMERICA_JUNEAU(TimeZone.getTimeZone("America/Juneau")), AMERICA_KENTUCKY_LOUISVILLE(TimeZone + .getTimeZone("America/Kentucky/Louisville")), AMERICA_KENTUCKY_MONTICELLO(TimeZone.getTimeZone("America/Kentucky/Monticello")), AMERICA_KNOX_IN(TimeZone.getTimeZone("America/Knox_IN")), AMERICA_LA_PAZ(TimeZone + .getTimeZone("America/La_Paz")), AMERICA_LIMA(TimeZone.getTimeZone("America/Lima")), AMERICA_LOS_ANGELES(TimeZone.getTimeZone("America/Los_Angeles")), AMERICA_LOUISVILLE(TimeZone.getTimeZone("America/Louisville")), AMERICA_MACEIO( + TimeZone.getTimeZone("America/Maceio")), AMERICA_MANAGUA(TimeZone.getTimeZone("America/Managua")), AMERICA_MANAUS(TimeZone.getTimeZone("America/Manaus")), AMERICA_MARTINIQUE(TimeZone.getTimeZone("America/Martinique")), AMERICA_MAZATLAN( + TimeZone.getTimeZone("America/Mazatlan")), AMERICA_MENDOZA(TimeZone.getTimeZone("America/Mendoza")), AMERICA_MENOMINEE(TimeZone.getTimeZone("America/Menominee")), AMERICA_MERIDA(TimeZone.getTimeZone("America/Merida")), AMERICA_MEXICO_CITY( + TimeZone.getTimeZone("America/Mexico_City")), AMERICA_MIQUELON(TimeZone.getTimeZone("America/Miquelon")), AMERICA_MONTERREY(TimeZone.getTimeZone("America/Monterrey")), AMERICA_MONTEVIDEO(TimeZone + .getTimeZone("America/Montevideo")), AMERICA_MONTREAL(TimeZone.getTimeZone("America/Montreal")), AMERICA_MONTSERRAT(TimeZone.getTimeZone("America/Montserrat")), AMERICA_NASSAU(TimeZone.getTimeZone("America/Nassau")), AMERICA_NEW_YORK( + TimeZone.getTimeZone("America/New_York")), AMERICA_NIPIGON(TimeZone.getTimeZone("America/Nipigon")), AMERICA_NOME(TimeZone.getTimeZone("America/Nome")), AMERICA_NORONHA(TimeZone.getTimeZone("America/Noronha")), AMERICA_NORTH_DAKOTA_CENTER( + TimeZone.getTimeZone("America/North_Dakota/Center")), AMERICA_PANAMA(TimeZone.getTimeZone("America/Panama")), AMERICA_PANGNIRTUNG(TimeZone.getTimeZone("America/Pangnirtung")), AMERICA_PARAMARIBO(TimeZone + .getTimeZone("America/Paramaribo")), AMERICA_PHOENIX(TimeZone.getTimeZone("America/Phoenix")), AMERICA_PORT_AU_PRINCE(TimeZone.getTimeZone("America/Port-au-Prince")), AMERICA_PORT_OF_SPAIN(TimeZone + .getTimeZone("America/Port_of_Spain")), AMERICA_PORTO_ACRE(TimeZone.getTimeZone("America/Porto_Acre")), AMERICA_PORTO_VELHO(TimeZone.getTimeZone("America/Porto_Velho")), AMERICA_PUERTO_RICO(TimeZone + .getTimeZone("America/Puerto_Rico")), AMERICA_RAINY_RIVER(TimeZone.getTimeZone("America/Rainy_River")), AMERICA_RANKIN_INLET(TimeZone.getTimeZone("America/Rankin_Inlet")), AMERICA_RECIFE(TimeZone.getTimeZone("America/Recife")), AMERICA_REGINA( + TimeZone.getTimeZone("America/Regina")), AMERICA_RIO_BRANCO(TimeZone.getTimeZone("America/Rio_Branco")), AMERICA_ROSARIO(TimeZone.getTimeZone("America/Rosario")), AMERICA_SANTIAGO(TimeZone.getTimeZone("America/Santiago")), AMERICA_SANTO_DOMINGO( + TimeZone.getTimeZone("America/Santo_Domingo")), AMERICA_SAO_PAULO(TimeZone.getTimeZone("America/Sao_Paulo")), AMERICA_SCORESBYSUND(TimeZone.getTimeZone("America/Scoresbysund")), AMERICA_SHIPROCK(TimeZone + .getTimeZone("America/Shiprock")), AMERICA_ST_JOHNS(TimeZone.getTimeZone("America/St_Johns")), AMERICA_ST_KITTS(TimeZone.getTimeZone("America/St_Kitts")), AMERICA_ST_LUCIA(TimeZone.getTimeZone("America/St_Lucia")), AMERICA_ST_THOMAS( + TimeZone.getTimeZone("America/St_Thomas")), AMERICA_ST_VINCENT(TimeZone.getTimeZone("America/St_Vincent")), AMERICA_SWIFT_CURRENT(TimeZone.getTimeZone("America/Swift_Current")), AMERICA_TEGUCIGALPA(TimeZone + .getTimeZone("America/Tegucigalpa")), AMERICA_THULE(TimeZone.getTimeZone("America/Thule")), AMERICA_THUNDER_BAY(TimeZone.getTimeZone("America/Thunder_Bay")), AMERICA_TIJUANA(TimeZone.getTimeZone("America/Tijuana")), AMERICA_TORTOLA( + TimeZone.getTimeZone("America/Tortola")), AMERICA_VANCOUVER(TimeZone.getTimeZone("America/Vancouver")), AMERICA_VIRGIN(TimeZone.getTimeZone("America/Virgin")), AMERICA_WHITEHORSE(TimeZone.getTimeZone("America/Whitehorse")), AMERICA_WINNIPEG( + TimeZone.getTimeZone("America/Winnipeg")), AMERICA_YAKUTAT(TimeZone.getTimeZone("America/Yakutat")), AMERICA_YELLOWKNIFE(TimeZone.getTimeZone("America/Yellowknife")), ANTARCTICA_CASEY(TimeZone.getTimeZone("Antarctica/Casey")), ANTARCTICA_DAVIS( + TimeZone.getTimeZone("Antarctica/Davis")), ANTARCTICA_DUMONTDURVILLE(TimeZone.getTimeZone("Antarctica/DumontDUrville")), ANTARCTICA_MAWSON(TimeZone.getTimeZone("Antarctica/Mawson")), ANTARCTICA_MCMURDO(TimeZone + .getTimeZone("Antarctica/McMurdo")), ANTARCTICA_PALMER(TimeZone.getTimeZone("Antarctica/Palmer")), ANTARCTICA_ROTHERA(TimeZone.getTimeZone("Antarctica/Rothera")), ANTARCTICA_SOUTH_POLE(TimeZone + .getTimeZone("Antarctica/South_Pole")), ANTARCTICA_SYOWA(TimeZone.getTimeZone("Antarctica/Syowa")), ANTARCTICA_VOSTOK(TimeZone.getTimeZone("Antarctica/Vostok")), ARCTIC_LONGYEARBYEN(TimeZone.getTimeZone("Arctic/Longyearbyen")), ART( + TimeZone.getTimeZone("ART")), ASIA_ADEN(TimeZone.getTimeZone("Asia/Aden")), ASIA_ALMATY(TimeZone.getTimeZone("Asia/Almaty")), ASIA_AMMAN(TimeZone.getTimeZone("Asia/Amman")), ASIA_ANADYR(TimeZone.getTimeZone("Asia/Anadyr")), ASIA_AQTAU( + TimeZone.getTimeZone("Asia/Aqtau")), ASIA_AQTOBE(TimeZone.getTimeZone("Asia/Aqtobe")), ASIA_ASHGABAT(TimeZone.getTimeZone("Asia/Ashgabat")), ASIA_ASHKHABAD(TimeZone.getTimeZone("Asia/Ashkhabad")), ASIA_BAGHDAD(TimeZone + .getTimeZone("Asia/Baghdad")), ASIA_BAHRAIN(TimeZone.getTimeZone("Asia/Bahrain")), ASIA_BAKU(TimeZone.getTimeZone("Asia/Baku")), ASIA_BANGKOK(TimeZone.getTimeZone("Asia/Bangkok")), ASIA_BEIRUT(TimeZone + .getTimeZone("Asia/Beirut")), ASIA_BISHKEK(TimeZone.getTimeZone("Asia/Bishkek")), ASIA_BRUNEI(TimeZone.getTimeZone("Asia/Brunei")), ASIA_CALCUTTA(TimeZone.getTimeZone("Asia/Calcutta")), ASIA_CHOIBALSAN(TimeZone + .getTimeZone("Asia/Choibalsan")), ASIA_CHONGQING(TimeZone.getTimeZone("Asia/Chongqing")), ASIA_CHUNGKING(TimeZone.getTimeZone("Asia/Chungking")), ASIA_COLOMBO(TimeZone.getTimeZone("Asia/Colombo")), ASIA_DACCA(TimeZone + .getTimeZone("Asia/Dacca")), ASIA_DAMASCUS(TimeZone.getTimeZone("Asia/Damascus")), ASIA_DHAKA(TimeZone.getTimeZone("Asia/Dhaka")), ASIA_DILI(TimeZone.getTimeZone("Asia/Dili")), ASIA_DUBAI(TimeZone.getTimeZone("Asia/Dubai")), ASIA_DUSHANBE( + TimeZone.getTimeZone("Asia/Dushanbe")), ASIA_GAZA(TimeZone.getTimeZone("Asia/Gaza")), ASIA_HARBIN(TimeZone.getTimeZone("Asia/Harbin")), ASIA_HONG_KONG(TimeZone.getTimeZone("Asia/Hong_Kong")), ASIA_HOVD(TimeZone + .getTimeZone("Asia/Hovd")), ASIA_IRKUTSK(TimeZone.getTimeZone("Asia/Irkutsk")), ASIA_ISTANBUL(TimeZone.getTimeZone("Asia/Istanbul")), ASIA_JAKARTA(TimeZone.getTimeZone("Asia/Jakarta")), ASIA_JAYAPURA(TimeZone + .getTimeZone("Asia/Jayapura")), ASIA_JERUSALEM(TimeZone.getTimeZone("Asia/Jerusalem")), ASIA_KABUL(TimeZone.getTimeZone("Asia/Kabul")), ASIA_KAMCHATKA(TimeZone.getTimeZone("Asia/Kamchatka")), ASIA_KARACHI(TimeZone + .getTimeZone("Asia/Karachi")), ASIA_KASHGAR(TimeZone.getTimeZone("Asia/Kashgar")), ASIA_KATMANDU(TimeZone.getTimeZone("Asia/Katmandu")), ASIA_KRASNOYARSK(TimeZone.getTimeZone("Asia/Krasnoyarsk")), ASIA_KUALA_LUMPUR(TimeZone + .getTimeZone("Asia/Kuala_Lumpur")), ASIA_KUCHING(TimeZone.getTimeZone("Asia/Kuching")), ASIA_KUWAIT(TimeZone.getTimeZone("Asia/Kuwait")), ASIA_MACAO(TimeZone.getTimeZone("Asia/Macao")), ASIA_MACAU(TimeZone + .getTimeZone("Asia/Macau")), ASIA_MAGADAN(TimeZone.getTimeZone("Asia/Magadan")), ASIA_MAKASSAR(TimeZone.getTimeZone("Asia/Makassar")), ASIA_MANILA(TimeZone.getTimeZone("Asia/Manila")), ASIA_MUSCAT(TimeZone + .getTimeZone("Asia/Muscat")), ASIA_NICOSIA(TimeZone.getTimeZone("Asia/Nicosia")), ASIA_NOVOSIBIRSK(TimeZone.getTimeZone("Asia/Novosibirsk")), ASIA_OMSK(TimeZone.getTimeZone("Asia/Omsk")), ASIA_ORAL(TimeZone + .getTimeZone("Asia/Oral")), ASIA_PHNOM_PENH(TimeZone.getTimeZone("Asia/Phnom_Penh")), ASIA_PONTIANAK(TimeZone.getTimeZone("Asia/Pontianak")), ASIA_PYONGYANG(TimeZone.getTimeZone("Asia/Pyongyang")), ASIA_QATAR(TimeZone + .getTimeZone("Asia/Qatar")), ASIA_QYZYLORDA(TimeZone.getTimeZone("Asia/Qyzylorda")), ASIA_RANGOON(TimeZone.getTimeZone("Asia/Rangoon")), ASIA_RIYADH(TimeZone.getTimeZone("Asia/Riyadh")), ASIA_RIYADH87(TimeZone + .getTimeZone("Asia/Riyadh87")), ASIA_RIYADH88(TimeZone.getTimeZone("Asia/Riyadh88")), ASIA_RIYADH89(TimeZone.getTimeZone("Asia/Riyadh89")), ASIA_SAIGON(TimeZone.getTimeZone("Asia/Saigon")), ASIA_SAKHALIN(TimeZone + .getTimeZone("Asia/Sakhalin")), ASIA_SAMARKAND(TimeZone.getTimeZone("Asia/Samarkand")), ASIA_SEOUL(TimeZone.getTimeZone("Asia/Seoul")), ASIA_SHANGHAI(TimeZone.getTimeZone("Asia/Shanghai")), ASIA_SINGAPORE(TimeZone + .getTimeZone("Asia/Singapore")), ASIA_TAIPEI(TimeZone.getTimeZone("Asia/Taipei")), ASIA_TASHKENT(TimeZone.getTimeZone("Asia/Tashkent")), ASIA_TBILISI(TimeZone.getTimeZone("Asia/Tbilisi")), ASIA_TEHRAN(TimeZone + .getTimeZone("Asia/Tehran")), ASIA_TEL_AVIV(TimeZone.getTimeZone("Asia/Tel_Aviv")), ASIA_THIMBU(TimeZone.getTimeZone("Asia/Thimbu")), ASIA_THIMPHU(TimeZone.getTimeZone("Asia/Thimphu")), ASIA_TOKYO(TimeZone + .getTimeZone("Asia/Tokyo")), ASIA_UJUNG_PANDANG(TimeZone.getTimeZone("Asia/Ujung_Pandang")), ASIA_ULAANBAATAR(TimeZone.getTimeZone("Asia/Ulaanbaatar")), ASIA_ULAN_BATOR(TimeZone.getTimeZone("Asia/Ulan_Bator")), ASIA_URUMQI( + TimeZone.getTimeZone("Asia/Urumqi")), ASIA_VIENTIANE(TimeZone.getTimeZone("Asia/Vientiane")), ASIA_VLADIVOSTOK(TimeZone.getTimeZone("Asia/Vladivostok")), ASIA_YAKUTSK(TimeZone.getTimeZone("Asia/Yakutsk")), ASIA_YEKATERINBURG( + TimeZone.getTimeZone("Asia/Yekaterinburg")), ASIA_YEREVAN(TimeZone.getTimeZone("Asia/Yerevan")), AST(TimeZone.getTimeZone("AST")), ATLANTIC_AZORES(TimeZone.getTimeZone("Atlantic/Azores")), ATLANTIC_BERMUDA(TimeZone + .getTimeZone("Atlantic/Bermuda")), ATLANTIC_CANARY(TimeZone.getTimeZone("Atlantic/Canary")), ATLANTIC_CAPE_VERDE(TimeZone.getTimeZone("Atlantic/Cape_Verde")), ATLANTIC_FAEROE(TimeZone.getTimeZone("Atlantic/Faeroe")), ATLANTIC_JAN_MAYEN( + TimeZone.getTimeZone("Atlantic/Jan_Mayen")), ATLANTIC_MADEIRA(TimeZone.getTimeZone("Atlantic/Madeira")), ATLANTIC_REYKJAVIK(TimeZone.getTimeZone("Atlantic/Reykjavik")), ATLANTIC_SOUTH_GEORGIA(TimeZone + .getTimeZone("Atlantic/South_Georgia")), ATLANTIC_ST_HELENA(TimeZone.getTimeZone("Atlantic/St_Helena")), ATLANTIC_STANLEY(TimeZone.getTimeZone("Atlantic/Stanley")), AUSTRALIA_ACT(TimeZone.getTimeZone("Australia/ACT")), AUSTRALIA_ADELAIDE( + TimeZone.getTimeZone("Australia/Adelaide")), AUSTRALIA_BRISBANE(TimeZone.getTimeZone("Australia/Brisbane")), AUSTRALIA_BROKEN_HILL(TimeZone.getTimeZone("Australia/Broken_Hill")), AUSTRALIA_CANBERRA(TimeZone + .getTimeZone("Australia/Canberra")), AUSTRALIA_DARWIN(TimeZone.getTimeZone("Australia/Darwin")), AUSTRALIA_HOBART(TimeZone.getTimeZone("Australia/Hobart")), AUSTRALIA_LHI(TimeZone.getTimeZone("Australia/LHI")), AUSTRALIA_LINDEMAN( + TimeZone.getTimeZone("Australia/Lindeman")), AUSTRALIA_LORD_HOWE(TimeZone.getTimeZone("Australia/Lord_Howe")), AUSTRALIA_MELBOURNE(TimeZone.getTimeZone("Australia/Melbourne")), AUSTRALIA_NORTH(TimeZone + .getTimeZone("Australia/North")), AUSTRALIA_NSW(TimeZone.getTimeZone("Australia/NSW")), AUSTRALIA_PERTH(TimeZone.getTimeZone("Australia/Perth")), AUSTRALIA_QUEENSLAND(TimeZone.getTimeZone("Australia/Queensland")), AUSTRALIA_SOUTH( + TimeZone.getTimeZone("Australia/South")), AUSTRALIA_SYDNEY(TimeZone.getTimeZone("Australia/Sydney")), AUSTRALIA_TASMANIA(TimeZone.getTimeZone("Australia/Tasmania")), AUSTRALIA_VICTORIA(TimeZone.getTimeZone("Australia/Victoria")), AUSTRALIA_WEST( + TimeZone.getTimeZone("Australia/West")), AUSTRALIA_YANCOWINNA(TimeZone.getTimeZone("Australia/Yancowinna")), BET(TimeZone.getTimeZone("BET")), BRAZIL_ACRE(TimeZone.getTimeZone("Brazil/Acre")), BRAZIL_DENORONHA(TimeZone + .getTimeZone("Brazil/DeNoronha")), BRAZIL_EAST(TimeZone.getTimeZone("Brazil/East")), BRAZIL_WEST(TimeZone.getTimeZone("Brazil/West")), BST(TimeZone.getTimeZone("BST")), CANADA_ATLANTIC(TimeZone.getTimeZone("Canada/Atlantic")), CANADA_CENTRAL( + TimeZone.getTimeZone("Canada/Central")), CANADA_EAST_SASKATCHEWAN(TimeZone.getTimeZone("Canada/East-Saskatchewan")), CANADA_EASTERN(TimeZone.getTimeZone("Canada/Eastern")), CANADA_MOUNTAIN(TimeZone + .getTimeZone("Canada/Mountain")), CANADA_NEWFOUNDLAND(TimeZone.getTimeZone("Canada/Newfoundland")), CANADA_PACIFIC(TimeZone.getTimeZone("Canada/Pacific")), CANADA_SASKATCHEWAN(TimeZone.getTimeZone("Canada/Saskatchewan")), CANADA_YUKON( + TimeZone.getTimeZone("Canada/Yukon")), CAT(TimeZone.getTimeZone("CAT")), CET(TimeZone.getTimeZone("CET")), CHILE_CONTINENTAL(TimeZone.getTimeZone("Chile/Continental")), CHILE_EASTERISLAND(TimeZone + .getTimeZone("Chile/EasterIsland")), CNT(TimeZone.getTimeZone("CNT")), CST(TimeZone.getTimeZone("CST")), CST6CDT(TimeZone.getTimeZone("CST6CDT")), CTT(TimeZone.getTimeZone("CTT")), CUBA(TimeZone.getTimeZone("Cuba")), EAT( + TimeZone.getTimeZone("EAT")), ECT(TimeZone.getTimeZone("ECT")), EET(TimeZone.getTimeZone("EET")), EGYPT(TimeZone.getTimeZone("Egypt")), EIRE(TimeZone.getTimeZone("Eire")), EST(TimeZone.getTimeZone("EST")), EST5EDT(TimeZone + .getTimeZone("EST5EDT")), ETC_GMT(TimeZone.getTimeZone("Etc/GMT")), ETC_GMT_PLUS_0(TimeZone.getTimeZone("Etc/GMT+0")), ETC_GMT_PLUS_1(TimeZone.getTimeZone("Etc/GMT+1")), ETC_GMT_PLUS_10(TimeZone.getTimeZone("Etc/GMT+10")), ETC_GMT_PLUS_11( + TimeZone.getTimeZone("Etc/GMT+11")), ETC_GMT_PLUS_12(TimeZone.getTimeZone("Etc/GMT+12")), ETC_GMT_PLUS_2(TimeZone.getTimeZone("Etc/GMT+2")), ETC_GMT_PLUS_3(TimeZone.getTimeZone("Etc/GMT+3")), ETC_GMT_PLUS_4(TimeZone + .getTimeZone("Etc/GMT+4")), ETC_GMT_PLUS_5(TimeZone.getTimeZone("Etc/GMT+5")), ETC_GMT_PLUS_6(TimeZone.getTimeZone("Etc/GMT+6")), ETC_GMT_PLUS_7(TimeZone.getTimeZone("Etc/GMT+7")), ETC_GMT_PLUS_8(TimeZone + .getTimeZone("Etc/GMT+8")), ETC_GMT_PLUS_9(TimeZone.getTimeZone("Etc/GMT+9")), ETC_GMT_MINUS_0(TimeZone.getTimeZone("Etc/GMT-0")), ETC_GMT_MINUS_1(TimeZone.getTimeZone("Etc/GMT-1")), ETC_GMT_MINUS_10(TimeZone + .getTimeZone("Etc/GMT-10")), ETC_GMT_MINUS_11(TimeZone.getTimeZone("Etc/GMT-11")), ETC_GMT_MINUS_12(TimeZone.getTimeZone("Etc/GMT-12")), ETC_GMT_MINUS_13(TimeZone.getTimeZone("Etc/GMT-13")), ETC_GMT_MINUS_14(TimeZone + .getTimeZone("Etc/GMT-14")), ETC_GMT_MINUS_2(TimeZone.getTimeZone("Etc/GMT-2")), ETC_GMT_MINUS_3(TimeZone.getTimeZone("Etc/GMT-3")), ETC_GMT_MINUS_4(TimeZone.getTimeZone("Etc/GMT-4")), ETC_GMT_MINUS_5(TimeZone + .getTimeZone("Etc/GMT-5")), ETC_GMT_MINUS_6(TimeZone.getTimeZone("Etc/GMT-6")), ETC_GMT_MINUS_7(TimeZone.getTimeZone("Etc/GMT-7")), ETC_GMT_MINUS_8(TimeZone.getTimeZone("Etc/GMT-8")), ETC_GMT_MINUS_9(TimeZone + .getTimeZone("Etc/GMT-9")), ETC_GMT0(TimeZone.getTimeZone("Etc/GMT0")), ETC_GREENWICH(TimeZone.getTimeZone("Etc/Greenwich")), ETC_UCT(TimeZone.getTimeZone("Etc/UCT")), ETC_UNIVERSAL(TimeZone.getTimeZone("Etc/Universal")), ETC_UTC( + TimeZone.getTimeZone("Etc/UTC")), ETC_ZULU(TimeZone.getTimeZone("Etc/Zulu")), EUROPE_AMSTERDAM(TimeZone.getTimeZone("Europe/Amsterdam")), EUROPE_ANDORRA(TimeZone.getTimeZone("Europe/Andorra")), EUROPE_ATHENS(TimeZone + .getTimeZone("Europe/Athens")), EUROPE_BELFAST(TimeZone.getTimeZone("Europe/Belfast")), EUROPE_BELGRADE(TimeZone.getTimeZone("Europe/Belgrade")), EUROPE_BERLIN(TimeZone.getTimeZone("Europe/Berlin")), EUROPE_BRATISLAVA(TimeZone + .getTimeZone("Europe/Bratislava")), EUROPE_BRUSSELS(TimeZone.getTimeZone("Europe/Brussels")), EUROPE_BUCHAREST(TimeZone.getTimeZone("Europe/Bucharest")), EUROPE_BUDAPEST(TimeZone.getTimeZone("Europe/Budapest")), EUROPE_CHISINAU( + TimeZone.getTimeZone("Europe/Chisinau")), EUROPE_COPENHAGEN(TimeZone.getTimeZone("Europe/Copenhagen")), EUROPE_DUBLIN(TimeZone.getTimeZone("Europe/Dublin")), EUROPE_GIBRALTAR(TimeZone.getTimeZone("Europe/Gibraltar")), EUROPE_HELSINKI( + TimeZone.getTimeZone("Europe/Helsinki")), EUROPE_ISTANBUL(TimeZone.getTimeZone("Europe/Istanbul")), EUROPE_KALININGRAD(TimeZone.getTimeZone("Europe/Kaliningrad")), EUROPE_KIEV(TimeZone.getTimeZone("Europe/Kiev")), EUROPE_LISBON( + TimeZone.getTimeZone("Europe/Lisbon")), EUROPE_LJUBLJANA(TimeZone.getTimeZone("Europe/Ljubljana")), EUROPE_LONDON(TimeZone.getTimeZone("Europe/London")), EUROPE_LUXEMBOURG(TimeZone.getTimeZone("Europe/Luxembourg")), EUROPE_MADRID( + TimeZone.getTimeZone("Europe/Madrid")), EUROPE_MALTA(TimeZone.getTimeZone("Europe/Malta")), EUROPE_MINSK(TimeZone.getTimeZone("Europe/Minsk")), EUROPE_MONACO(TimeZone.getTimeZone("Europe/Monaco")), EUROPE_MOSCOW(TimeZone + .getTimeZone("Europe/Moscow")), EUROPE_NICOSIA(TimeZone.getTimeZone("Europe/Nicosia")), EUROPE_OSLO(TimeZone.getTimeZone("Europe/Oslo")), EUROPE_PARIS(TimeZone.getTimeZone("Europe/Paris")), EUROPE_PRAGUE(TimeZone + .getTimeZone("Europe/Prague")), EUROPE_RIGA(TimeZone.getTimeZone("Europe/Riga")), EUROPE_ROME(TimeZone.getTimeZone("Europe/Rome")), EUROPE_SAMARA(TimeZone.getTimeZone("Europe/Samara")), EUROPE_SAN_MARINO(TimeZone + .getTimeZone("Europe/San_Marino")), EUROPE_SARAJEVO(TimeZone.getTimeZone("Europe/Sarajevo")), EUROPE_SIMFEROPOL(TimeZone.getTimeZone("Europe/Simferopol")), EUROPE_SKOPJE(TimeZone.getTimeZone("Europe/Skopje")), EUROPE_SOFIA( + TimeZone.getTimeZone("Europe/Sofia")), EUROPE_STOCKHOLM(TimeZone.getTimeZone("Europe/Stockholm")), EUROPE_TALLINN(TimeZone.getTimeZone("Europe/Tallinn")), EUROPE_TIRANE(TimeZone.getTimeZone("Europe/Tirane")), EUROPE_TIRASPOL( + TimeZone.getTimeZone("Europe/Tiraspol")), EUROPE_UZHGOROD(TimeZone.getTimeZone("Europe/Uzhgorod")), EUROPE_VADUZ(TimeZone.getTimeZone("Europe/Vaduz")), EUROPE_VATICAN(TimeZone.getTimeZone("Europe/Vatican")), EUROPE_VIENNA( + TimeZone.getTimeZone("Europe/Vienna")), EUROPE_VILNIUS(TimeZone.getTimeZone("Europe/Vilnius")), EUROPE_WARSAW(TimeZone.getTimeZone("Europe/Warsaw")), EUROPE_ZAGREB(TimeZone.getTimeZone("Europe/Zagreb")), EUROPE_ZAPOROZHYE( + TimeZone.getTimeZone("Europe/Zaporozhye")), EUROPE_ZURICH(TimeZone.getTimeZone("Europe/Zurich")), GB(TimeZone.getTimeZone("GB")), GB_EIRE(TimeZone.getTimeZone("GB-Eire")), GMT(TimeZone.getTimeZone("GMT")), GMT0(TimeZone + .getTimeZone("GMT0")), GREENWICH(TimeZone.getTimeZone("Greenwich")), HONGKONG(TimeZone.getTimeZone("Hongkong")), HST(TimeZone.getTimeZone("HST")), ICELAND(TimeZone.getTimeZone("Iceland")), IET(TimeZone.getTimeZone("IET")), INDIAN_ANTANANARIVO( + TimeZone.getTimeZone("Indian/Antananarivo")), INDIAN_CHAGOS(TimeZone.getTimeZone("Indian/Chagos")), INDIAN_CHRISTMAS(TimeZone.getTimeZone("Indian/Christmas")), INDIAN_COCOS(TimeZone.getTimeZone("Indian/Cocos")), INDIAN_COMORO( + TimeZone.getTimeZone("Indian/Comoro")), INDIAN_KERGUELEN(TimeZone.getTimeZone("Indian/Kerguelen")), INDIAN_MAHE(TimeZone.getTimeZone("Indian/Mahe")), INDIAN_MALDIVES(TimeZone.getTimeZone("Indian/Maldives")), INDIAN_MAURITIUS( + TimeZone.getTimeZone("Indian/Mauritius")), INDIAN_MAYOTTE(TimeZone.getTimeZone("Indian/Mayotte")), INDIAN_REUNION(TimeZone.getTimeZone("Indian/Reunion")), IRAN(TimeZone.getTimeZone("Iran")), ISRAEL(TimeZone + .getTimeZone("Israel")), IST(TimeZone.getTimeZone("IST")), JAMAICA(TimeZone.getTimeZone("Jamaica")), JAPAN(TimeZone.getTimeZone("Japan")), JST(TimeZone.getTimeZone("JST")), KWAJALEIN(TimeZone.getTimeZone("Kwajalein")), LIBYA( + TimeZone.getTimeZone("Libya")), MET(TimeZone.getTimeZone("MET")), MEXICO_BAJANORTE(TimeZone.getTimeZone("Mexico/BajaNorte")), MEXICO_BAJASUR(TimeZone.getTimeZone("Mexico/BajaSur")), MEXICO_GENERAL(TimeZone + .getTimeZone("Mexico/General")), MIDEAST_RIYADH87(TimeZone.getTimeZone("Mideast/Riyadh87")), MIDEAST_RIYADH88(TimeZone.getTimeZone("Mideast/Riyadh88")), MIDEAST_RIYADH89(TimeZone.getTimeZone("Mideast/Riyadh89")), MIT(TimeZone + .getTimeZone("MIT")), MST(TimeZone.getTimeZone("MST")), MST7MDT(TimeZone.getTimeZone("MST7MDT")), NAVAJO(TimeZone.getTimeZone("Navajo")), NET(TimeZone.getTimeZone("NET")), NST(TimeZone.getTimeZone("NST")), NZ(TimeZone + .getTimeZone("NZ")), NZ_CHAT(TimeZone.getTimeZone("NZ-CHAT")), PACIFIC_APIA(TimeZone.getTimeZone("Pacific/Apia")), PACIFIC_AUCKLAND(TimeZone.getTimeZone("Pacific/Auckland")), PACIFIC_CHATHAM(TimeZone + .getTimeZone("Pacific/Chatham")), PACIFIC_EASTER(TimeZone.getTimeZone("Pacific/Easter")), PACIFIC_EFATE(TimeZone.getTimeZone("Pacific/Efate")), PACIFIC_ENDERBURY(TimeZone.getTimeZone("Pacific/Enderbury")), PACIFIC_FAKAOFO( + TimeZone.getTimeZone("Pacific/Fakaofo")), PACIFIC_FIJI(TimeZone.getTimeZone("Pacific/Fiji")), PACIFIC_FUNAFUTI(TimeZone.getTimeZone("Pacific/Funafuti")), PACIFIC_GALAPAGOS(TimeZone.getTimeZone("Pacific/Galapagos")), PACIFIC_GAMBIER( + TimeZone.getTimeZone("Pacific/Gambier")), PACIFIC_GUADALCANAL(TimeZone.getTimeZone("Pacific/Guadalcanal")), PACIFIC_GUAM(TimeZone.getTimeZone("Pacific/Guam")), PACIFIC_HONOLULU(TimeZone.getTimeZone("Pacific/Honolulu")), PACIFIC_JOHNSTON( + TimeZone.getTimeZone("Pacific/Johnston")), PACIFIC_KIRITIMATI(TimeZone.getTimeZone("Pacific/Kiritimati")), PACIFIC_KOSRAE(TimeZone.getTimeZone("Pacific/Kosrae")), PACIFIC_KWAJALEIN(TimeZone.getTimeZone("Pacific/Kwajalein")), PACIFIC_MAJURO( + TimeZone.getTimeZone("Pacific/Majuro")), PACIFIC_MARQUESAS(TimeZone.getTimeZone("Pacific/Marquesas")), PACIFIC_MIDWAY(TimeZone.getTimeZone("Pacific/Midway")), PACIFIC_NAURU(TimeZone.getTimeZone("Pacific/Nauru")), PACIFIC_NIUE( + TimeZone.getTimeZone("Pacific/Niue")), PACIFIC_NORFOLK(TimeZone.getTimeZone("Pacific/Norfolk")), PACIFIC_NOUMEA(TimeZone.getTimeZone("Pacific/Noumea")), PACIFIC_PAGO_PAGO(TimeZone.getTimeZone("Pacific/Pago_Pago")), PACIFIC_PALAU( + TimeZone.getTimeZone("Pacific/Palau")), PACIFIC_PITCAIRN(TimeZone.getTimeZone("Pacific/Pitcairn")), PACIFIC_PONAPE(TimeZone.getTimeZone("Pacific/Ponape")), PACIFIC_PORT_MORESBY(TimeZone.getTimeZone("Pacific/Port_Moresby")), PACIFIC_RAROTONGA( + TimeZone.getTimeZone("Pacific/Rarotonga")), PACIFIC_SAIPAN(TimeZone.getTimeZone("Pacific/Saipan")), PACIFIC_SAMOA(TimeZone.getTimeZone("Pacific/Samoa")), PACIFIC_TAHITI(TimeZone.getTimeZone("Pacific/Tahiti")), PACIFIC_TARAWA( + TimeZone.getTimeZone("Pacific/Tarawa")), PACIFIC_TONGATAPU(TimeZone.getTimeZone("Pacific/Tongatapu")), PACIFIC_TRUK(TimeZone.getTimeZone("Pacific/Truk")), PACIFIC_WAKE(TimeZone.getTimeZone("Pacific/Wake")), PACIFIC_WALLIS( + TimeZone.getTimeZone("Pacific/Wallis")), PACIFIC_YAP(TimeZone.getTimeZone("Pacific/Yap")), PLT(TimeZone.getTimeZone("PLT")), PNT(TimeZone.getTimeZone("PNT")), POLAND(TimeZone.getTimeZone("Poland")), PORTUGAL(TimeZone + .getTimeZone("Portugal")), PRC(TimeZone.getTimeZone("PRC")), PRT(TimeZone.getTimeZone("PRT")), PST(TimeZone.getTimeZone("PST")), PST8PDT(TimeZone.getTimeZone("PST8PDT")), ROK(TimeZone.getTimeZone("ROK")), SINGAPORE(TimeZone + .getTimeZone("Singapore")), SST(TimeZone.getTimeZone("SST")), SYSTEMV_AST4(TimeZone.getTimeZone("SystemV/AST4")), SYSTEMV_AST4ADT(TimeZone.getTimeZone("SystemV/AST4ADT")), SYSTEMV_CST6(TimeZone.getTimeZone("SystemV/CST6")), SYSTEMV_CST6CDT( + TimeZone.getTimeZone("SystemV/CST6CDT")), SYSTEMV_EST5(TimeZone.getTimeZone("SystemV/EST5")), SYSTEMV_EST5EDT(TimeZone.getTimeZone("SystemV/EST5EDT")), SYSTEMV_HST10(TimeZone.getTimeZone("SystemV/HST10")), SYSTEMV_MST7(TimeZone + .getTimeZone("SystemV/MST7")), SYSTEMV_MST7MDT(TimeZone.getTimeZone("SystemV/MST7MDT")), SYSTEMV_PST8(TimeZone.getTimeZone("SystemV/PST8")), SYSTEMV_PST8PDT(TimeZone.getTimeZone("SystemV/PST8PDT")), SYSTEMV_YST9(TimeZone + .getTimeZone("SystemV/YST9")), SYSTEMV_YST9YDT(TimeZone.getTimeZone("SystemV/YST9YDT")), TURKEY(TimeZone.getTimeZone("Turkey")), UCT(TimeZone.getTimeZone("UCT")), UNIVERSAL(TimeZone.getTimeZone("Universal")), US_ALASKA(TimeZone + .getTimeZone("US/Alaska")), US_ALEUTIAN(TimeZone.getTimeZone("US/Aleutian")), US_ARIZONA(TimeZone.getTimeZone("US/Arizona")), US_CENTRAL(TimeZone.getTimeZone("US/Central")), US_EAST_INDIANA(TimeZone + .getTimeZone("US/East-Indiana")), US_EASTERN(TimeZone.getTimeZone("US/Eastern")), US_HAWAII(TimeZone.getTimeZone("US/Hawaii")), US_INDIANA_STARKE(TimeZone.getTimeZone("US/Indiana-Starke")), US_MICHIGAN(TimeZone + .getTimeZone("US/Michigan")), US_MOUNTAIN(TimeZone.getTimeZone("US/Mountain")), US_PACIFIC(TimeZone.getTimeZone("US/Pacific")), US_PACIFIC_NEW(TimeZone.getTimeZone("US/Pacific-New")), US_SAMOA(TimeZone.getTimeZone("US/Samoa")), UTC( + TimeZone.getTimeZone("UTC")), VST(TimeZone.getTimeZone("VST")), W_SU(TimeZone.getTimeZone("W-SU")), WET(TimeZone.getTimeZone("WET")), ZULU(TimeZone.getTimeZone("Zulu")); + + private TimeZone tz; + + TimeZones(final TimeZone tz) { + this.tz = tz; + } + + public final TimeZone getTimeZone() { + return tz; + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/EditText.java b/widgets/src/main/java/mohammadaminha/com/widgets/EditText.java new file mode 100644 index 0000000..46ce436 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/EditText.java @@ -0,0 +1,36 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.support.v7.widget.AppCompatEditText; +import android.util.AttributeSet; + +/** + * Created by aj on 1/30/2018. + */ + +public class EditText extends AppCompatEditText { + + + + public EditText(Context context) { + super(context); + setTf(context); + } + + public EditText(Context context, AttributeSet attrs) { + super(context, attrs); + setTf(context); + } + + public EditText(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setTf(context); + } + + private void setTf(Context context) { + + setTypeface(Util.getTypeFace()); + } + + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/ExpendableLayout/ExpandableLayout.java b/widgets/src/main/java/mohammadaminha/com/widgets/ExpendableLayout/ExpandableLayout.java new file mode 100644 index 0000000..3627503 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/ExpendableLayout/ExpandableLayout.java @@ -0,0 +1,328 @@ +package mohammadaminha.com.widgets.ExpendableLayout; + +import android.animation.Animator; +import android.animation.ValueAnimator; +import android.content.Context; +import android.content.res.Configuration; +import android.content.res.TypedArray; +import android.os.Bundle; +import android.os.Parcelable; +import android.util.AttributeSet; +import android.view.View; +import android.view.animation.Interpolator; +import android.widget.FrameLayout; +import android.widget.LinearLayout; + +import mohammadaminha.com.widgets.ExpendableLayout.util.FastOutSlowInInterpolator; +import mohammadaminha.com.widgets.R; + +import static mohammadaminha.com.widgets.ExpendableLayout.ExpandableLayout.State.COLLAPSED; +import static mohammadaminha.com.widgets.ExpendableLayout.ExpandableLayout.State.COLLAPSING; +import static mohammadaminha.com.widgets.ExpendableLayout.ExpandableLayout.State.EXPANDED; +import static mohammadaminha.com.widgets.ExpendableLayout.ExpandableLayout.State.EXPANDING; + + +public class ExpandableLayout extends FrameLayout { + public interface State { + int COLLAPSED = 0; + int COLLAPSING = 1; + int EXPANDING = 2; + int EXPANDED = 3; + } + + public static final String KEY_SUPER_STATE = "super_state"; + public static final String KEY_EXPANSION = "expansion"; + + public static final int HORIZONTAL = 0; + public static final int VERTICAL = 1; + + private static final int DEFAULT_DURATION = 300; + + private int duration = DEFAULT_DURATION; + private float parallax; + private float expansion; + private int orientation; + private int state; + + private Interpolator interpolator = new FastOutSlowInInterpolator(); + private ValueAnimator animator; + + private OnExpansionUpdateListener listener; + + public ExpandableLayout(Context context) { + this(context, null); + } + + public ExpandableLayout(Context context, AttributeSet attrs) { + super(context, attrs); + + if (attrs != null) { + TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ExpandableLayout); + duration = a.getInt(R.styleable.ExpandableLayout_el_duration, DEFAULT_DURATION); + expansion = a.getBoolean(R.styleable.ExpandableLayout_el_expanded, false) ? 1 : 0; + orientation = a.getInt(R.styleable.ExpandableLayout_android_orientation, VERTICAL); + parallax = a.getFloat(R.styleable.ExpandableLayout_el_parallax, 1); + a.recycle(); + + state = expansion == 0 ? COLLAPSED : EXPANDED; + setParallax(parallax); + } + } + + @Override + protected Parcelable onSaveInstanceState() { + Parcelable superState = super.onSaveInstanceState(); + Bundle bundle = new Bundle(); + + expansion = isExpanded() ? 1 : 0; + + bundle.putFloat(KEY_EXPANSION, expansion); + bundle.putParcelable(KEY_SUPER_STATE, superState); + + return bundle; + } + + @Override + protected void onRestoreInstanceState(Parcelable parcelable) { + Bundle bundle = (Bundle) parcelable; + expansion = bundle.getFloat(KEY_EXPANSION); + state = expansion == 1 ? EXPANDED : COLLAPSED; + Parcelable superState = bundle.getParcelable(KEY_SUPER_STATE); + + super.onRestoreInstanceState(superState); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + int width = getMeasuredWidth(); + int height = getMeasuredHeight(); + + int size = orientation == LinearLayout.HORIZONTAL ? width : height; + + setVisibility(expansion == 0 && size == 0 ? GONE : VISIBLE); + + int expansionDelta = size - Math.round(size * expansion); + if (parallax > 0) { + float parallaxDelta = expansionDelta * parallax; + for (int i = 0; i < getChildCount(); i++) { + View child = getChildAt(i); + if (orientation == HORIZONTAL) { + int direction = -1; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && getLayoutDirection() == LAYOUT_DIRECTION_RTL) { + direction = 1; + } + child.setTranslationX(direction * parallaxDelta); + } else { + child.setTranslationY(-parallaxDelta); + } + } + } + + if (orientation == HORIZONTAL) { + setMeasuredDimension(width - expansionDelta, height); + } else { + setMeasuredDimension(width, height - expansionDelta); + } + } + + @Override + protected void onConfigurationChanged(Configuration newConfig) { + if (animator != null) { + animator.cancel(); + } + super.onConfigurationChanged(newConfig); + } + + /** + * Get expansion state + * + * @return one of {@link State} + */ + public int getState() { + return state; + } + + public boolean isExpanded() { + return state == EXPANDING || state == EXPANDED; + } + + public void toggle() { + toggle(true); + } + + public void toggle(boolean animate) { + if (isExpanded()) { + collapse(animate); + } else { + expand(animate); + } + } + + public void expand() { + expand(true); + } + + public void expand(boolean animate) { + setExpanded(true, animate); + } + + public void collapse() { + collapse(true); + } + + public void collapse(boolean animate) { + setExpanded(false, animate); + } + + /** + * Convenience method - same as calling setExpanded(expanded, true) + */ + public void setExpanded(boolean expand) { + setExpanded(expand, true); + } + + public void setExpanded(boolean expand, boolean animate) { + if (expand == isExpanded()) { + return; + } + + int targetExpansion = expand ? 1 : 0; + if (animate) { + animateSize(targetExpansion); + } else { + setExpansion(targetExpansion); + } + } + + public int getDuration() { + return duration; + } + + public void setInterpolator(Interpolator interpolator) { + this.interpolator = interpolator; + } + + public void setDuration(int duration) { + this.duration = duration; + } + + public float getExpansion() { + return expansion; + } + + public void setExpansion(float expansion) { + if (this.expansion == expansion) { + return; + } + + // Infer state from previous value + float delta = expansion - this.expansion; + if (expansion == 0) { + state = COLLAPSED; + } else if (expansion == 1) { + state = EXPANDED; + } else if (delta < 0) { + state = COLLAPSING; + } else if (delta > 0) { + state = EXPANDING; + } + + setVisibility(state == COLLAPSED ? GONE : VISIBLE); + this.expansion = expansion; + requestLayout(); + + if (listener != null) { + listener.onExpansionUpdate(expansion, state); + } + } + + public float getParallax() { + return parallax; + } + + public void setParallax(float parallax) { + // Make sure parallax is between 0 and 1 + parallax = Math.min(1, Math.max(0, parallax)); + this.parallax = parallax; + } + + public int getOrientation() { + return orientation; + } + + public void setOrientation(int orientation) { + if (orientation < 0 || orientation > 1) { + throw new IllegalArgumentException("Orientation must be either 0 (horizontal) or 1 (vertical)"); + } + this.orientation = orientation; + } + + public void setOnExpansionUpdateListener(OnExpansionUpdateListener listener) { + this.listener = listener; + } + + private void animateSize(int targetExpansion) { + if (animator != null) { + animator.cancel(); + animator = null; + } + + animator = ValueAnimator.ofFloat(expansion, targetExpansion); + animator.setInterpolator(interpolator); + animator.setDuration(duration); + + animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator valueAnimator) { + setExpansion((float) valueAnimator.getAnimatedValue()); + } + }); + + animator.addListener(new ExpansionListener(targetExpansion)); + + animator.start(); + } + + public interface OnExpansionUpdateListener { + /** + * Callback for expansion updates + * + * @param expansionFraction Value between 0 (collapsed) and 1 (expanded) representing the the expansion progress + * @param state One of {@link State} repesenting the current expansion state + */ + void onExpansionUpdate(float expansionFraction, int state); + } + + private class ExpansionListener implements Animator.AnimatorListener { + private int targetExpansion; + private boolean canceled; + + public ExpansionListener(int targetExpansion) { + this.targetExpansion = targetExpansion; + } + + @Override + public void onAnimationStart(Animator animation) { + state = targetExpansion == 0 ? COLLAPSING : EXPANDING; + } + + @Override + public void onAnimationEnd(Animator animation) { + if (!canceled) { + state = targetExpansion == 0 ? COLLAPSED : EXPANDED; + setExpansion(targetExpansion); + } + } + + @Override + public void onAnimationCancel(Animator animation) { + canceled = true; + } + + @Override + public void onAnimationRepeat(Animator animation) { + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/ExpendableLayout/util/FastOutSlowInInterpolator.java b/widgets/src/main/java/mohammadaminha/com/widgets/ExpendableLayout/util/FastOutSlowInInterpolator.java new file mode 100644 index 0000000..5d304f5 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/ExpendableLayout/util/FastOutSlowInInterpolator.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.ExpendableLayout.util; + +/** + * Interpolator corresponding to {@link android.R.interpolator#fast_out_slow_in}. + * + * Uses a lookup table for the Bezier curve from (0,0) to (1,1) with control points: + * P0 (0, 0) + * P1 (0.4, 0) + * P2 (0.2, 1.0) + * P3 (1.0, 1.0) + */ +public class FastOutSlowInInterpolator extends LookupTableInterpolator { + + /** + * Lookup table values sampled with x at regular intervals between 0 and 1 for a total of + * 201 points. + */ + private static final float[] VALUES = new float[] { + 0.0000f, 0.0001f, 0.0002f, 0.0005f, 0.0009f, 0.0014f, 0.0020f, + 0.0027f, 0.0036f, 0.0046f, 0.0058f, 0.0071f, 0.0085f, 0.0101f, + 0.0118f, 0.0137f, 0.0158f, 0.0180f, 0.0205f, 0.0231f, 0.0259f, + 0.0289f, 0.0321f, 0.0355f, 0.0391f, 0.0430f, 0.0471f, 0.0514f, + 0.0560f, 0.0608f, 0.0660f, 0.0714f, 0.0771f, 0.0830f, 0.0893f, + 0.0959f, 0.1029f, 0.1101f, 0.1177f, 0.1257f, 0.1339f, 0.1426f, + 0.1516f, 0.1610f, 0.1707f, 0.1808f, 0.1913f, 0.2021f, 0.2133f, + 0.2248f, 0.2366f, 0.2487f, 0.2611f, 0.2738f, 0.2867f, 0.2998f, + 0.3131f, 0.3265f, 0.3400f, 0.3536f, 0.3673f, 0.3810f, 0.3946f, + 0.4082f, 0.4217f, 0.4352f, 0.4485f, 0.4616f, 0.4746f, 0.4874f, + 0.5000f, 0.5124f, 0.5246f, 0.5365f, 0.5482f, 0.5597f, 0.5710f, + 0.5820f, 0.5928f, 0.6033f, 0.6136f, 0.6237f, 0.6335f, 0.6431f, + 0.6525f, 0.6616f, 0.6706f, 0.6793f, 0.6878f, 0.6961f, 0.7043f, + 0.7122f, 0.7199f, 0.7275f, 0.7349f, 0.7421f, 0.7491f, 0.7559f, + 0.7626f, 0.7692f, 0.7756f, 0.7818f, 0.7879f, 0.7938f, 0.7996f, + 0.8053f, 0.8108f, 0.8162f, 0.8215f, 0.8266f, 0.8317f, 0.8366f, + 0.8414f, 0.8461f, 0.8507f, 0.8551f, 0.8595f, 0.8638f, 0.8679f, + 0.8720f, 0.8760f, 0.8798f, 0.8836f, 0.8873f, 0.8909f, 0.8945f, + 0.8979f, 0.9013f, 0.9046f, 0.9078f, 0.9109f, 0.9139f, 0.9169f, + 0.9198f, 0.9227f, 0.9254f, 0.9281f, 0.9307f, 0.9333f, 0.9358f, + 0.9382f, 0.9406f, 0.9429f, 0.9452f, 0.9474f, 0.9495f, 0.9516f, + 0.9536f, 0.9556f, 0.9575f, 0.9594f, 0.9612f, 0.9629f, 0.9646f, + 0.9663f, 0.9679f, 0.9695f, 0.9710f, 0.9725f, 0.9739f, 0.9753f, + 0.9766f, 0.9779f, 0.9791f, 0.9803f, 0.9815f, 0.9826f, 0.9837f, + 0.9848f, 0.9858f, 0.9867f, 0.9877f, 0.9885f, 0.9894f, 0.9902f, + 0.9910f, 0.9917f, 0.9924f, 0.9931f, 0.9937f, 0.9944f, 0.9949f, + 0.9955f, 0.9960f, 0.9964f, 0.9969f, 0.9973f, 0.9977f, 0.9980f, + 0.9984f, 0.9986f, 0.9989f, 0.9991f, 0.9993f, 0.9995f, 0.9997f, + 0.9998f, 0.9999f, 0.9999f, 1.0000f, 1.0000f + }; + + public FastOutSlowInInterpolator() { + super(VALUES); + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/ExpendableLayout/util/LookupTableInterpolator.java b/widgets/src/main/java/mohammadaminha/com/widgets/ExpendableLayout/util/LookupTableInterpolator.java new file mode 100644 index 0000000..593a539 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/ExpendableLayout/util/LookupTableInterpolator.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mohammadaminha.com.widgets.ExpendableLayout.util; + +import android.view.animation.Interpolator; + +/** + * An {@link Interpolator} that uses a lookup table to compute an interpolation based on a + * given input. + */ +abstract class LookupTableInterpolator implements Interpolator { + + private final float[] mValues; + private final float mStepSize; + + public LookupTableInterpolator(float[] values) { + mValues = values; + mStepSize = 1f / (mValues.length - 1); + } + + @Override + public float getInterpolation(float input) { + if (input >= 1.0f) { + return 1.0f; + } + if (input <= 0f) { + return 0f; + } + + // Calculate index - We use min with length - 2 to avoid IndexOutOfBoundsException when + // we lerp (linearly interpolate) in the return statement + int position = Math.min((int) (input * (mValues.length - 1)), mValues.length - 2); + + // Calculate values to account for small offsets as the lookup table has discrete values + float quantized = position * mStepSize; + float diff = input - quantized; + float weight = diff / mStepSize; + + // Linearly interpolate between the table values + return mValues[position] + weight * (mValues[position + 1] - mValues[position]); + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/GridSpacingItemDecoration.java b/widgets/src/main/java/mohammadaminha/com/widgets/GridSpacingItemDecoration.java new file mode 100644 index 0000000..93305fa --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/GridSpacingItemDecoration.java @@ -0,0 +1,50 @@ +package mohammadaminha.com.widgets; +import android.graphics.Rect; +import android.support.v7.widget.RecyclerView; +import android.view.View; + +public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { + + private int spanCount; + private int spacing; + private boolean includeEdge; + private int headerNum; + + public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge, int headerNum) { + this.spanCount = spanCount; + this.spacing = spacing; + this.includeEdge = includeEdge; + this.headerNum = headerNum; + } + + @Override + public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { + int position = parent.getChildAdapterPosition(view) - headerNum; // item position + + if (position >= 0) { + int column = position % spanCount; // item column + + if (includeEdge) { + outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing) + outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing) + + if (position < spanCount) { // top edge + outRect.top = spacing; + } + outRect.bottom = spacing; // item bottom + } else { + outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing) + outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing) + if (position >= spanCount) { + outRect.top = spacing; // item top + } + } + } else { + outRect.left = 0; + outRect.right = 0; + outRect.top = 0; + outRect.bottom = 0; + } + } +} + diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/ImageViewTouch.java b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/ImageViewTouch.java new file mode 100644 index 0000000..73d870f --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/ImageViewTouch.java @@ -0,0 +1,404 @@ +package mohammadaminha.com.widgets.ImageViewZoom; + +import android.annotation.TargetApi; +import android.content.Context; +import android.graphics.Rect; +import android.graphics.RectF; +import android.os.Build; +import android.os.SystemClock; +import android.util.AttributeSet; +import android.util.Log; +import android.view.GestureDetector; +import android.view.GestureDetector.OnGestureListener; +import android.view.MotionEvent; +import android.view.ScaleGestureDetector; +import android.view.ScaleGestureDetector.OnScaleGestureListener; +import android.view.ViewConfiguration; + +public class ImageViewTouch extends ImageViewTouchBase { + private static final float SCROLL_DELTA_THRESHOLD = 1.0f; + /** + * minimum time between a scale event and a valid fling event + */ + private static final long MIN_FLING_DELTA_TIME = 150; + private float mScaleFactor; + private ScaleGestureDetector mScaleDetector; + private GestureDetector mGestureDetector; + private int mTouchSlop; + private int mDoubleTapDirection; + private OnGestureListener mGestureListener; + private OnScaleGestureListener mScaleListener; + private boolean mDoubleTapEnabled = true; + private boolean mScaleEnabled = true; + private boolean mScrollEnabled = true; + private OnImageViewTouchDoubleTapListener mDoubleTapListener; + private OnImageViewTouchSingleTapListener mSingleTapListener; + + public ImageViewTouch(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public ImageViewTouch(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + @Override + protected void init(Context context, AttributeSet attrs, int defStyle) { + super.init(context, attrs, defStyle); + mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); + mGestureListener = getGestureListener(); + mScaleListener = getScaleListener(); + + mScaleDetector = new ScaleGestureDetector(getContext(), mScaleListener); + mGestureDetector = new GestureDetector(getContext(), mGestureListener, null, true); + mDoubleTapDirection = 1; + setQuickScaleEnabled(false); + } + + @TargetApi(19) + private void setQuickScaleEnabled(boolean value) { + if (Build.VERSION.SDK_INT >= 19) { + mScaleDetector.setQuickScaleEnabled(value); + } + } + + @TargetApi(19) + @SuppressWarnings("unused") + public boolean getQuickScaleEnabled() { + + + return Build.VERSION.SDK_INT >= 19 && mScaleDetector.isQuickScaleEnabled(); + + } + + @SuppressWarnings("unused") + public float getScaleFactor() { + return mScaleFactor; + } + + public void setDoubleTapListener(OnImageViewTouchDoubleTapListener listener) { + mDoubleTapListener = listener; + } + + public void setSingleTapListener(OnImageViewTouchSingleTapListener listener) { + mSingleTapListener = listener; + } + + public void setDoubleTapEnabled(boolean value) { + mDoubleTapEnabled = value; + } + + public void setScaleEnabled(boolean value) { + mScaleEnabled = value; + } + + public void setScrollEnabled(boolean value) { + mScrollEnabled = value; + } + + public boolean getDoubleTapEnabled() { + return mDoubleTapEnabled; + } + + private OnGestureListener getGestureListener() { + return new GestureListener(); + } + + private OnScaleGestureListener getScaleListener() { + return new ScaleListener(); + } + + @Override + protected void onLayoutChanged(final int left, final int top, final int right, final int bottom) { + super.onLayoutChanged(left, top, right, bottom); + Log.v(TAG, "min: " + getMinScale() + ", max: " + getMaxScale() + ", result: " + (getMaxScale() - getMinScale()) / 2f); + mScaleFactor = ((getMaxScale() - getMinScale()) / 2f) + 0.5f; + } + + private long mPointerUpTime; + + @Override + public boolean onTouchEvent(MotionEvent event) { + if (getBitmapChanged()) { + return false; + } + + final int action = event.getActionMasked(); + + if (action == MotionEvent.ACTION_POINTER_UP) { + mPointerUpTime = event.getEventTime(); + } + + mScaleDetector.onTouchEvent(event); + + if (!mScaleDetector.isInProgress()) { + mGestureDetector.onTouchEvent(event); + } + + switch (action) { + case MotionEvent.ACTION_UP: + return onUp(event); + default: + break; + } + return true; + } + + @Override + protected void onZoomAnimationCompleted(float scale) { + + if (DEBUG) { + Log.d(TAG, "onZoomAnimationCompleted. scale: " + scale + ", minZoom: " + getMinScale()); + } + + if (scale < getMinScale()) { + zoomTo(getMinScale(), 50); + } + } + + private float onDoubleTapPost(float scale, final float maxZoom, final float minScale) { + if ((scale + mScaleFactor) <= maxZoom) { + return scale + mScaleFactor; + } else { + return minScale; + } + } + + private boolean onSingleTapConfirmed(MotionEvent e) { + return true; + } + + private boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { + if (canScroll()) { + return false; + } + mUserScaled = true; + scrollBy(-distanceX, -distanceY); + invalidate(); + return true; + } + + private boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + if (canScroll()) { + return false; + } + + if (DEBUG) { + Log.i(TAG, "onFling"); + } + + if (Math.abs(velocityX) > (mMinFlingVelocity * 4) || Math.abs(velocityY) > (mMinFlingVelocity * 4)) { + if (DEBUG) { + Log.v(TAG, "velocity: " + velocityY); + Log.v(TAG, "diff: " + (e2.getY() - e1.getY())); + } + + final float scale = Math.min(Math.max(2f, getScale() / 2), 3.f); + + float scaledDistanceX = ((velocityX) / mMaxFlingVelocity) * (getWidth() * scale); + float scaledDistanceY = ((velocityY) / mMaxFlingVelocity) * (getHeight() * scale); + + if (DEBUG) { + Log.v(TAG, "scale: " + getScale() + ", scale_final: " + scale); + Log.v(TAG, "scaledDistanceX: " + scaledDistanceX); + Log.v(TAG, "scaledDistanceY: " + scaledDistanceY); + } + + mUserScaled = true; + + double total = Math.sqrt(Math.pow(scaledDistanceX, 2) + Math.pow(scaledDistanceY, 2)); + + scrollBy(scaledDistanceX, scaledDistanceY, (long) Math.min(Math.max(300, total / 5), 800)); + + postInvalidate(); + return true; + } + return false; + } + + private boolean onDown(MotionEvent e) { + return !getBitmapChanged(); + } + + private boolean onUp(MotionEvent e) { + if (getBitmapChanged()) { + return false; + } + if (getScale() < getMinScale()) { + zoomTo(getMinScale(), 50); + } + return true; + } + + private boolean onSingleTapUp(MotionEvent e) { + return !getBitmapChanged(); + } + + private boolean canScroll() { + if (getScale() > 1) { + return false; + } + RectF bitmapRect = getBitmapRect(); + return mViewPort.contains(bitmapRect); + } + + /** + * Determines whether this ImageViewTouch can be scrolled. + * + * @param direction - positive direction value means scroll from right to left, + * negative value means scroll from left to right + * @return true if there is some more place to scroll, false - otherwise. + */ + @SuppressWarnings("unused") + public boolean canScroll(int direction) { + RectF bitmapRect = getBitmapRect(); + updateRect(bitmapRect, mScrollPoint); + Rect imageViewRect = new Rect(); + getGlobalVisibleRect(imageViewRect); + + if (null == bitmapRect) { + return false; + } + + if (bitmapRect.right >= imageViewRect.right) { + if (direction < 0) { + return Math.abs(bitmapRect.right - imageViewRect.right) > SCROLL_DELTA_THRESHOLD; + } + } + + double bitmapScrollRectDelta = Math.abs(bitmapRect.left - mScrollPoint.x); + return bitmapScrollRectDelta > SCROLL_DELTA_THRESHOLD; + } + + public class GestureListener extends GestureDetector.SimpleOnGestureListener { + @Override + public boolean onSingleTapConfirmed(MotionEvent e) { + + if (null != mSingleTapListener) { + mSingleTapListener.onSingleTapConfirmed(); + } + + return ImageViewTouch.this.onSingleTapConfirmed(e); + } + + @Override + public boolean onDoubleTap(MotionEvent e) { + if (DEBUG) { + Log.i(TAG, "onDoubleTap. double tap enabled? " + mDoubleTapEnabled); + } + if (mDoubleTapEnabled) { + if (Build.VERSION.SDK_INT >= 19) { + if (mScaleDetector.isQuickScaleEnabled()) { + return true; + } + } + + mUserScaled = true; + + float scale = getScale(); + float targetScale; + targetScale = onDoubleTapPost(scale, getMaxScale(), getMinScale()); + targetScale = Math.min(getMaxScale(), Math.max(targetScale, getMinScale())); + zoomTo(targetScale, e.getX(), e.getY(), mDefaultAnimationDuration); + + } + + if (null != mDoubleTapListener) { + mDoubleTapListener.onDoubleTap(); + } + + return super.onDoubleTap(e); + } + + @Override + public void onLongPress(MotionEvent e) { + if (isLongClickable()) { + if (!mScaleDetector.isInProgress()) { + setPressed(true); + performLongClick(); + } + } + } + + @Override + public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { + return mScrollEnabled && !(e1 == null || e2 == null) && !(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) && !mScaleDetector.isInProgress() && ImageViewTouch.this.onScroll(e1, e2, distanceX, distanceY); + + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + if (!mScrollEnabled) { + return false; + } + if (e1 == null || e2 == null) { + return false; + } + if (e1.getPointerCount() > 1 || e2.getPointerCount() > 1) { + return false; + } + if (mScaleDetector.isInProgress()) { + return false; + } + + final long delta = (SystemClock.uptimeMillis() - mPointerUpTime); + + // prevent fling happening just + // after a quick pinch to zoom + return delta > MIN_FLING_DELTA_TIME && ImageViewTouch.this.onFling(e1, e2, velocityX, velocityY); + + } + + @Override + public boolean onSingleTapUp(MotionEvent e) { + return ImageViewTouch.this.onSingleTapUp(e); + } + + @Override + public boolean onDown(MotionEvent e) { + if (DEBUG) { + Log.i(TAG, "onDown"); + } + stopAllAnimations(); + + return ImageViewTouch.this.onDown(e); + } + } + + public class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { + boolean mScaled = false; + + @Override + public boolean onScale(ScaleGestureDetector detector) { + float span = detector.getCurrentSpan() - detector.getPreviousSpan(); + float targetScale = getScale() * detector.getScaleFactor(); + + if (mScaleEnabled) { + if (mScaled && span != 0) { + mUserScaled = true; + targetScale = Math.min(getMaxScale(), Math.max(targetScale, getMinScale() - MIN_SCALE_DIFF)); + zoomTo(targetScale, detector.getFocusX(), detector.getFocusY()); + mDoubleTapDirection = 1; + invalidate(); + return true; + } + + // This is to prevent a glitch the first time + // image is scaled. + if (!mScaled) { + mScaled = true; + } + } + return true; + } + + } + + public interface OnImageViewTouchDoubleTapListener { + void onDoubleTap(); + } + + public interface OnImageViewTouchSingleTapListener { + void onSingleTapConfirmed(); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/ImageViewTouchBase.java b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/ImageViewTouchBase.java new file mode 100644 index 0000000..0aaeb30 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/ImageViewTouchBase.java @@ -0,0 +1,957 @@ +package mohammadaminha.com.widgets.ImageViewZoom; + +import android.animation.Animator; +import android.animation.AnimatorSet; +import android.animation.ValueAnimator; +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.res.Configuration; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Matrix; +import android.graphics.PointF; +import android.graphics.RectF; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.support.annotation.RequiresApi; +import android.util.AttributeSet; +import android.util.Log; +import android.view.ViewConfiguration; +import android.view.animation.DecelerateInterpolator; +import android.widget.ImageView; + +import mohammadaminha.com.widgets.BuildConfig; +import mohammadaminha.com.widgets.ImageViewZoom.graphics.FastBitmapDrawable; +import mohammadaminha.com.widgets.ImageViewZoom.utils.IDisposable; + + +/** + * Base View to manage image zoom/scrool/pinch operations + * + * @author alessandro + */ +public abstract class ImageViewTouchBase extends android.support.v7.widget.AppCompatImageView implements IDisposable { + public static final String VERSION = BuildConfig.VERSION_NAME; + static final float MIN_SCALE_DIFF = 0.1f; + + public interface OnDrawableChangeListener { + void onDrawableChanged(Drawable drawable); + } + + public interface OnLayoutChangeListener { + /** + * Callback invoked when the layout bounds changed + */ + void onLayoutChanged(boolean changed, int left, int top, int right, int bottom); + } + + /** + * Use this to change the {@link ImageViewTouchBase#setDisplayType(DisplayType)} of + * this View + * + * @author alessandro + */ + public enum DisplayType { + /** + * Image is not scaled by default + */ + NONE, + /** + * Image will be always presented using this view's bounds + */ + FIT_TO_SCREEN, + /** + * Image will be scaled only if bigger than the bounds of this view + */ + FIT_IF_BIGGER + } + + static final String TAG = "ImageViewTouchBase"; + @SuppressWarnings("checkstyle:staticvariablename") + static final boolean DEBUG = false; + private static final float ZOOM_INVALID = -1f; + private final Matrix mBaseMatrix = new Matrix(); + private Matrix mSuppMatrix = new Matrix(); + private Matrix mNextMatrix; + private Runnable mLayoutRunnable = null; + boolean mUserScaled = false; + private float mMaxZoom = ZOOM_INVALID; + private float mMinZoom = ZOOM_INVALID; + // true when min and max zoom are explicitly defined + private boolean mMaxZoomDefined; + private boolean mMinZoomDefined; + private final Matrix mDisplayMatrix = new Matrix(); + private final float[] mMatrixValues = new float[9]; + private DisplayType mScaleType = DisplayType.FIT_IF_BIGGER; + private boolean mScaleTypeChanged; + private boolean mBitmapChanged; + int mDefaultAnimationDuration; + int mMinFlingVelocity; + int mMaxFlingVelocity; + private final PointF mCenter = new PointF(); + private final RectF mBitmapRect = new RectF(); + private final RectF mBitmapRectTmp = new RectF(); + private final RectF mCenterRect = new RectF(); + final PointF mScrollPoint = new PointF(); + final RectF mViewPort = new RectF(); + private final RectF mViewPortOld = new RectF(); + private Animator mCurrentAnimation; + private OnDrawableChangeListener mDrawableChangeListener; + private OnLayoutChangeListener mOnLayoutChangeListener; + + public ImageViewTouchBase(Context context) { + this(context, null); + } + + public ImageViewTouchBase(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public ImageViewTouchBase(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(context, attrs, defStyle); + } + + boolean getBitmapChanged() { + return mBitmapChanged; + } + + public void setOnDrawableChangedListener(OnDrawableChangeListener listener) { + mDrawableChangeListener = listener; + } + + public void setOnLayoutChangeListener(OnLayoutChangeListener listener) { + mOnLayoutChangeListener = listener; + } + + void init(Context context, AttributeSet attrs, int defStyle) { + ViewConfiguration configuration = ViewConfiguration.get(context); + mMinFlingVelocity = configuration.getScaledMinimumFlingVelocity(); + mMaxFlingVelocity = configuration.getScaledMaximumFlingVelocity(); + mDefaultAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime); + setScaleType(ScaleType.MATRIX); + } + + /** + * Clear the current drawable + */ + private void clear() { + setImageBitmap(null); + } + + public void setDisplayType(DisplayType type) { + if (type != mScaleType) { + if (DEBUG) { + Log.i(TAG, "setDisplayType: " + type); + } + mUserScaled = false; + mScaleType = type; + mScaleTypeChanged = true; + requestLayout(); + } + } + + private DisplayType getDisplayType() { + return mScaleType; + } + + protected void setMinScale(float value) { + if (DEBUG) { + Log.d(TAG, "setMinZoom: " + value); + } + + mMinZoom = value; + } + + protected void setMaxScale(float value) { + if (DEBUG) { + Log.d(TAG, "setMaxZoom: " + value); + } + mMaxZoom = value; + } + + private void onViewPortChanged(float left, float top, float right, float bottom) { + mViewPort.set(left, top, right, bottom); + mCenter.x = mViewPort.centerX(); + mCenter.y = mViewPort.centerY(); + } + + @SuppressWarnings("checkstyle:cyclomaticcomplexity") + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + if (DEBUG) { + Log.e(TAG, "onLayout: " + changed + ", bitmapChanged: " + mBitmapChanged + ", scaleChanged: " + mScaleTypeChanged); + } + + float deltaX = 0; + float deltaY = 0; + + if (changed) { + mViewPortOld.set(mViewPort); + onViewPortChanged(left, top, right, bottom); + + deltaX = mViewPort.width() - mViewPortOld.width(); + deltaY = mViewPort.height() - mViewPortOld.height(); + } + + super.onLayout(changed, left, top, right, bottom); + + Runnable r = mLayoutRunnable; + + if (r != null) { + mLayoutRunnable = null; + r.run(); + } + + final Drawable drawable = getDrawable(); + + if (drawable != null) { + + if (changed || mScaleTypeChanged || mBitmapChanged) { + + if (mBitmapChanged) { + mUserScaled = false; + mBaseMatrix.reset(); + if (!mMinZoomDefined) { + mMinZoom = ZOOM_INVALID; + } + if (!mMaxZoomDefined) { + mMaxZoom = ZOOM_INVALID; + } + } + + float scale = 1; + + // retrieve the old values + float oldDefaultScale = getDefaultScale(getDisplayType()); + float oldMatrixScale = getScale(mBaseMatrix); + float oldScale = getScale(); + float oldMinScale = Math.min(1f, 1f / oldMatrixScale); + + getProperBaseMatrix(drawable, mBaseMatrix, mViewPort); + + float newMatrixScale = getScale(mBaseMatrix); + + if (DEBUG) { + Log.d(TAG, "old matrix scale: " + oldMatrixScale); + Log.d(TAG, "new matrix scale: " + newMatrixScale); + Log.d(TAG, "old min scale: " + oldMinScale); + Log.d(TAG, "old scale: " + oldScale); + } + + // 1. bitmap changed or scaletype changed + if (mBitmapChanged || mScaleTypeChanged) { + + if (DEBUG) { + Log.d(TAG, "display type: " + getDisplayType()); + Log.d(TAG, "newMatrix: " + mNextMatrix); + } + + if (mNextMatrix != null) { + mSuppMatrix.set(mNextMatrix); + mNextMatrix = null; + scale = getScale(); + } else { + mSuppMatrix.reset(); + scale = getDefaultScale(getDisplayType()); + } + + setImageMatrix(getImageViewMatrix()); + + if (scale != getScale()) { + if (DEBUG) { + Log.v(TAG, "scale != getScale: " + scale + " != " + getScale()); + } + zoomTo(scale); + } + + } else if (changed) { + + // 2. layout size changed + + if (!mMinZoomDefined) { + mMinZoom = ZOOM_INVALID; + } + if (!mMaxZoomDefined) { + mMaxZoom = ZOOM_INVALID; + } + + setImageMatrix(getImageViewMatrix()); + postTranslate(-deltaX, -deltaY); + + if (!mUserScaled) { + scale = getDefaultScale(getDisplayType()); + if (DEBUG) { + Log.v(TAG, "!userScaled. scale=" + scale); + } + zoomTo(scale); + } else { + if (Math.abs(oldScale - oldMinScale) > MIN_SCALE_DIFF) { + scale = (oldMatrixScale / newMatrixScale) * oldScale; + } + if (DEBUG) { + Log.v(TAG, "userScaled. scale=" + scale); + } + zoomTo(scale); + } + + if (DEBUG) { + Log.d(TAG, "old min scale: " + oldDefaultScale); + Log.d(TAG, "old scale: " + oldScale); + Log.d(TAG, "new scale: " + scale); + } + } + + if (scale > getMaxScale() || scale < getMinScale()) { + // if current scale if outside the min/max bounds + // then restore the correct scale + zoomTo(scale); + } + + center(true, true); + + if (mBitmapChanged) { + onDrawableChanged(drawable); + } + if (changed || mBitmapChanged || mScaleTypeChanged) { + onLayoutChanged(left, top, right, bottom); + } + + if (mScaleTypeChanged) { + mScaleTypeChanged = false; + } + if (mBitmapChanged) { + mBitmapChanged = false; + } + + if (DEBUG) { + Log.d(TAG, "scale: " + getScale() + ", minScale: " + getMinScale() + ", maxScale: " + getMaxScale()); + } + } + } else { + // drawable is null + if (mBitmapChanged) { + onDrawableChanged(drawable); + } + if (changed || mBitmapChanged || mScaleTypeChanged) { + onLayoutChanged(left, top, right, bottom); + } + + if (mBitmapChanged) { + mBitmapChanged = false; + } + if (mScaleTypeChanged) { + mScaleTypeChanged = false; + } + } + } + + @Override + protected void onConfigurationChanged(final Configuration newConfig) { + super.onConfigurationChanged(newConfig); + + if (DEBUG) { + Log.i( + TAG, + "onConfigurationChanged. scale: " + getScale() + ", minScale: " + getMinScale() + ", mUserScaled: " + mUserScaled + ); + } + + if (mUserScaled) { + mUserScaled = Math.abs(getScale() - getMinScale()) > MIN_SCALE_DIFF; + } + + if (DEBUG) { + Log.v(TAG, "mUserScaled: " + mUserScaled); + } + } + + /** + * Restore the original display + */ + public void resetDisplay() { + mBitmapChanged = true; + requestLayout(); + } + + public void resetMatrix() { + if (DEBUG) { + Log.i(TAG, "resetMatrix"); + } + mSuppMatrix = new Matrix(); + + float scale = getDefaultScale(getDisplayType()); + setImageMatrix(getImageViewMatrix()); + + if (DEBUG) { + Log.d(TAG, "default scale: " + scale + ", scale: " + getScale()); + } + + if (scale != getScale()) { + zoomTo(scale); + } + + postInvalidate(); + } + + private float getDefaultScale(DisplayType type) { + if (type == DisplayType.FIT_TO_SCREEN) { + // always fit to screen + return 1f; + } else if (type == DisplayType.FIT_IF_BIGGER) { + // normal scale if smaller, fit to screen otherwise + return Math.min(1f, 1f / getScale(mBaseMatrix)); + } else { + // no scale + return 1f / getScale(mBaseMatrix); + } + } + + @Override + public void setImageResource(int resId) { + setImageDrawable(getContext().getResources().getDrawable(resId)); + } + + /** + * {@inheritDoc} Set the new image to display and reset the internal matrix. + * + * @param bitmap the {@link Bitmap} to display + * @see {@link ImageView#setImageBitmap(Bitmap)} + */ + @Override + public void setImageBitmap(final Bitmap bitmap) { + setImageBitmap(bitmap, null, ZOOM_INVALID, ZOOM_INVALID); + } + + private void setImageBitmap(final Bitmap bitmap, Matrix matrix, float minZoom, float maxZoom) { + if (bitmap != null) { + setImageDrawable(new FastBitmapDrawable(bitmap), matrix, minZoom, maxZoom); + } else { + setImageDrawable(null, matrix, minZoom, maxZoom); + } + } + + @Override + public void setImageDrawable(Drawable drawable) { + setImageDrawable(drawable, null, ZOOM_INVALID, ZOOM_INVALID); + } + + /** + * Note: if the scaleType is FitToScreen then min_zoom must be <= 1 and max_zoom must be >= 1 + * + * @param drawable the new drawable + * @param initialMatrix the optional initial display matrix + * @param minZoom the optional minimum scale, pass {@link #ZOOM_INVALID} to use the default min_zoom + * @param maxZoom the optional maximum scale, pass {@link #ZOOM_INVALID} to use the default max_zoom + */ + private void setImageDrawable(final Drawable drawable, final Matrix initialMatrix, final float minZoom, final float maxZoom) { + final int viewWidth = getWidth(); + + if (viewWidth <= 0) { + mLayoutRunnable = new Runnable() { + @Override + public void run() { + setImageDrawable(drawable, initialMatrix, minZoom, maxZoom); + } + }; + return; + } + setImageDrawableInternal(drawable, initialMatrix, minZoom, maxZoom); + } + + private void setImageDrawableInternal(final Drawable drawable, final Matrix initialMatrix, float minZoom, float maxZoom) { + mBaseMatrix.reset(); + super.setImageDrawable(drawable); + + if (minZoom != ZOOM_INVALID && maxZoom != ZOOM_INVALID) { + minZoom = Math.min(minZoom, maxZoom); + maxZoom = Math.max(minZoom, maxZoom); + + mMinZoom = minZoom; + mMaxZoom = maxZoom; + + mMinZoomDefined = true; + mMaxZoomDefined = true; + + if (getDisplayType() == DisplayType.FIT_TO_SCREEN || getDisplayType() == DisplayType.FIT_IF_BIGGER) { + + if (mMinZoom >= 1) { + mMinZoomDefined = false; + mMinZoom = ZOOM_INVALID; + } + + if (mMaxZoom <= 1) { + mMaxZoomDefined = true; + mMaxZoom = ZOOM_INVALID; + } + } + } else { + mMinZoom = ZOOM_INVALID; + mMaxZoom = ZOOM_INVALID; + + mMinZoomDefined = false; + mMaxZoomDefined = false; + } + + if (initialMatrix != null) { + mNextMatrix = new Matrix(initialMatrix); + } + if (DEBUG) { + Log.v(TAG, "mMinZoom: " + mMinZoom + ", mMaxZoom: " + mMaxZoom); + } + + mBitmapChanged = true; + updateDrawable(drawable); + requestLayout(); + } + + private void updateDrawable(Drawable newDrawable) { + if (null != newDrawable) { + mBitmapRect.set(0, 0, newDrawable.getIntrinsicWidth(), newDrawable.getIntrinsicHeight()); + } else { + mBitmapRect.setEmpty(); + } + } + + private void onDrawableChanged(final Drawable drawable) { + if (DEBUG) { + Log.i(TAG, "onDrawableChanged"); + Log.v(TAG, "scale: " + getScale() + ", minScale: " + getMinScale()); + } + fireOnDrawableChangeListener(drawable); + } + + private void fireOnLayoutChangeListener(int left, int top, int right, int bottom) { + if (null != mOnLayoutChangeListener) { + mOnLayoutChangeListener.onLayoutChanged(true, left, top, right, bottom); + } + } + + private void fireOnDrawableChangeListener(Drawable drawable) { + if (null != mDrawableChangeListener) { + mDrawableChangeListener.onDrawableChanged(drawable); + } + } + + void onLayoutChanged(int left, int top, int right, int bottom) { + if (DEBUG) { + Log.i(TAG, "onLayoutChanged"); + } + fireOnLayoutChangeListener(left, top, right, bottom); + } + + private float computeMaxZoom() { + final Drawable drawable = getDrawable(); + if (drawable == null) { + return 1f; + } + float fw = mBitmapRect.width() / mViewPort.width(); + float fh = mBitmapRect.height() / mViewPort.height(); + float scale = Math.max(fw, fh) * 4; + + if (DEBUG) { + Log.i(TAG, "computeMaxZoom: " + scale); + } + return scale; + } + + private float computeMinZoom() { + if (DEBUG) { + Log.i(TAG, "computeMinZoom"); + } + + final Drawable drawable = getDrawable(); + if (drawable == null) { + return 1f; + } + + float scale = getScale(mBaseMatrix); + + scale = Math.min(1f, 1f / scale); + if (DEBUG) { + Log.i(TAG, "computeMinZoom: " + scale); + } + + return scale; + } + + float getMaxScale() { + if (mMaxZoom == ZOOM_INVALID) { + mMaxZoom = computeMaxZoom(); + } + return mMaxZoom; + } + + float getMinScale() { + if (DEBUG) { + Log.i(TAG, "getMinScale, mMinZoom: " + mMinZoom); + } + + if (mMinZoom == ZOOM_INVALID) { + mMinZoom = computeMinZoom(); + } + + if (DEBUG) { + Log.v(TAG, "mMinZoom: " + mMinZoom); + } + + return mMinZoom; + } + + private Matrix getImageViewMatrix() { + return getImageViewMatrix(mSuppMatrix); + } + + private Matrix getImageViewMatrix(Matrix supportMatrix) { + mDisplayMatrix.set(mBaseMatrix); + mDisplayMatrix.postConcat(supportMatrix); + return mDisplayMatrix; + } + + @Override + public void setImageMatrix(Matrix matrix) { + Matrix current = getImageMatrix(); + boolean needUpdate = false; + + if (matrix == null && !current.isIdentity() || matrix != null && !current.equals(matrix)) { + needUpdate = true; + } + + super.setImageMatrix(matrix); + if (needUpdate) { + onImageMatrixChanged(); + } + } + + private void onImageMatrixChanged() { + } + + /** + * Returns the current image display matrix.
    + * This matrix can be used in the next call to the {@link #setImageDrawable(Drawable, Matrix, float, float)} to restore the same + * view state of the previous {@link Bitmap}.
    + * Example: + *

    + *

    +     * Matrix currentMatrix = mImageView.getDisplayMatrix();
    +     * mImageView.setImageBitmap( newBitmap, currentMatrix, ZOOM_INVALID, ZOOM_INVALID );
    +     * 
    + * + * @return the current support matrix + */ + public Matrix getDisplayMatrix() { + return new Matrix(mSuppMatrix); + } + + private void getProperBaseMatrix(Drawable drawable, Matrix matrix, RectF rect) { + float w = mBitmapRect.width(); + float h = mBitmapRect.height(); + float widthScale, heightScale; + + matrix.reset(); + + widthScale = rect.width() / w; + heightScale = rect.height() / h; + float scale = Math.min(widthScale, heightScale); + matrix.postScale(scale, scale); + matrix.postTranslate(rect.left, rect.top); + + float tw = (rect.width() - w * scale) / 2.0f; + float th = (rect.height() - h * scale) / 2.0f; + matrix.postTranslate(tw, th); + printMatrix(matrix); + } + + private float getValue(Matrix matrix, int whichValue) { + matrix.getValues(mMatrixValues); + return mMatrixValues[whichValue]; + } + + private void printMatrix(Matrix matrix) { + float scalex = getValue(matrix, Matrix.MSCALE_X); + float scaley = getValue(matrix, Matrix.MSCALE_Y); + float tx = getValue(matrix, Matrix.MTRANS_X); + float ty = getValue(matrix, Matrix.MTRANS_Y); + Log.d(TAG, "matrix: { x: " + tx + ", y: " + ty + ", scalex: " + scalex + ", scaley: " + scaley + " }"); + } + + RectF getBitmapRect() { + return getBitmapRect(mSuppMatrix); + } + + private RectF getBitmapRect(Matrix supportMatrix) { + Matrix m = getImageViewMatrix(supportMatrix); + m.mapRect(mBitmapRectTmp, mBitmapRect); + return mBitmapRectTmp; + } + + private float getScale(Matrix matrix) { + return getValue(matrix, Matrix.MSCALE_X); + } + + @SuppressLint("Override") + public float getRotation() { + return 0; + } + + float getScale() { + return getScale(mSuppMatrix); + } + + public float getBaseScale() { + return getScale(mBaseMatrix); + } + + private void center(boolean horizontal, boolean vertical) { + final Drawable drawable = getDrawable(); + if (drawable == null) { + return; + } + + RectF rect = getCenter(mSuppMatrix, horizontal, vertical); + + if (rect.left != 0 || rect.top != 0) { + postTranslate(rect.left, rect.top); + } + } + + private RectF getCenter(Matrix supportMatrix, boolean horizontal, boolean vertical) { + final Drawable drawable = getDrawable(); + + if (drawable == null) { + return new RectF(0, 0, 0, 0); + } + + mCenterRect.set(0, 0, 0, 0); + RectF rect = getBitmapRect(supportMatrix); + float height = rect.height(); + float width = rect.width(); + float deltaX = 0, deltaY = 0; + if (vertical) { + if (height < mViewPort.height()) { + deltaY = (mViewPort.height() - height) / 2 - (rect.top - mViewPort.top); + } else if (rect.top > mViewPort.top) { + deltaY = -(rect.top - mViewPort.top); + } else if (rect.bottom < mViewPort.bottom) { + deltaY = mViewPort.bottom - rect.bottom; + } + } + if (horizontal) { + if (width < mViewPort.width()) { + deltaX = (mViewPort.width() - width) / 2 - (rect.left - mViewPort.left); + } else if (rect.left > mViewPort.left) { + deltaX = -(rect.left - mViewPort.left); + } else if (rect.right < mViewPort.right) { + deltaX = mViewPort.right - rect.right; + } + } + mCenterRect.set(deltaX, deltaY, 0, 0); + return mCenterRect; + } + + private void postTranslate(float deltaX, float deltaY) { + if (deltaX != 0 || deltaY != 0) { + mSuppMatrix.postTranslate(deltaX, deltaY); + setImageMatrix(getImageViewMatrix()); + } + } + + private void postScale(float scale, float centerX, float centerY) { + mSuppMatrix.postScale(scale, scale, centerX, centerY); + setImageMatrix(getImageViewMatrix()); + } + + private PointF getCenter() { + return mCenter; + } + + private void zoomTo(float scale) { + if (DEBUG) { + Log.i(TAG, "zoomTo: " + scale); + } + + if (scale > getMaxScale()) { + scale = getMaxScale(); + } + if (scale < getMinScale()) { + scale = getMinScale(); + } + + if (DEBUG) { + Log.d(TAG, "sanitized scale: " + scale); + } + + PointF center = getCenter(); + zoomTo(scale, center.x, center.y); + } + + /** + * Scale to the target scale + * + * @param scale the target zoom + * @param durationMs the animation duration + */ + void zoomTo(float scale, long durationMs) { + PointF center = getCenter(); + zoomTo(scale, center.x, center.y, durationMs); + } + + void zoomTo(float scale, float centerX, float centerY) { + if (scale > getMaxScale()) { + scale = getMaxScale(); + } + + float oldScale = getScale(); + float deltaScale = scale / oldScale; + postScale(deltaScale, centerX, centerY); + onZoom(getScale()); + center(true, true); + } + + @SuppressWarnings("unused") + protected void onZoom(float scale) { + } + + @SuppressWarnings("unused") + protected void onZoomAnimationCompleted(float scale) { + } + + void scrollBy(float x, float y) { + panBy(x, y); + } + + private void panBy(double dx, double dy) { + RectF rect = getBitmapRect(); + mScrollPoint.set((float) dx, (float) dy); + updateRect(rect, mScrollPoint); + + if (mScrollPoint.x != 0 || mScrollPoint.y != 0) { + postTranslate(mScrollPoint.x, mScrollPoint.y); + center(true, true); + } + } + + void updateRect(RectF bitmapRect, PointF scrollRect) { + + } + + void stopAllAnimations() { + if (null != mCurrentAnimation) { + mCurrentAnimation.cancel(); + mCurrentAnimation = null; + } + } + + void scrollBy(float distanceX, float distanceY, final long durationMs) { + final ValueAnimator anim1 = ValueAnimator.ofFloat(0, distanceX).setDuration(durationMs); + final ValueAnimator anim2 = ValueAnimator.ofFloat(0, distanceY).setDuration(durationMs); + + stopAllAnimations(); + + mCurrentAnimation = new AnimatorSet(); + ((AnimatorSet) mCurrentAnimation).playTogether( + anim1, anim2 + ); + + mCurrentAnimation.setDuration(durationMs); + mCurrentAnimation.setInterpolator(new DecelerateInterpolator()); + mCurrentAnimation.start(); + + anim2.addUpdateListener( + new ValueAnimator.AnimatorUpdateListener() { + float oldValueX = 0; + float oldValueY = 0; + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + @Override + public void onAnimationUpdate(final ValueAnimator animation) { + float valueX = (Float) anim1.getAnimatedValue(); + float valueY = (Float) anim2.getAnimatedValue(); + panBy(valueX - oldValueX, valueY - oldValueY); + oldValueX = valueX; + oldValueY = valueY; + postInvalidateOnAnimation(); + } + } + ); + + mCurrentAnimation.addListener( + new Animator.AnimatorListener() { + @Override + public void onAnimationStart(final Animator animation) { + + } + + @Override + public void onAnimationEnd(final Animator animation) { + RectF centerRect = getCenter(mSuppMatrix, true, true); + if (centerRect.left != 0 || centerRect.top != 0) { + scrollBy(centerRect.left, centerRect.top); + } + } + + @Override + public void onAnimationCancel(final Animator animation) { + + } + + @Override + public void onAnimationRepeat(final Animator animation) { + + } + } + ); + } + + void zoomTo(float scale, float centerX, float centerY, final long durationMs) { + if (scale > getMaxScale()) { + scale = getMaxScale(); + } + + final float oldScale = getScale(); + + Matrix m = new Matrix(mSuppMatrix); + m.postScale(scale, scale, centerX, centerY); + RectF rect = getCenter(m, true, true); + + final float finalScale = scale; + final float destX = centerX + rect.left * scale; + final float destY = centerY + rect.top * scale; + + stopAllAnimations(); + + ValueAnimator animation = ValueAnimator.ofFloat(oldScale, finalScale); + animation.setDuration(durationMs); + animation.setInterpolator(new DecelerateInterpolator(1.0f)); + animation.addUpdateListener( + new ValueAnimator.AnimatorUpdateListener() { + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + @Override + public void onAnimationUpdate(final ValueAnimator animation) { + float value = (Float) animation.getAnimatedValue(); + zoomTo(value, destX, destY); + postInvalidateOnAnimation(); + } + } + ); + animation.start(); + } + + @Override + public void dispose() { + clear(); + } + + @Override + protected void onDraw(final Canvas canvas) { + + if (getScaleType() == ScaleType.FIT_XY) { + final Drawable drawable = getDrawable(); + if (null != drawable) { + drawable.draw(canvas); + } + } else { + super.onDraw(canvas); + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/graphics/FastBitmapDrawable.java b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/graphics/FastBitmapDrawable.java new file mode 100644 index 0000000..1880c5b --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/graphics/FastBitmapDrawable.java @@ -0,0 +1,110 @@ +package mohammadaminha.com.widgets.ImageViewZoom.graphics; + +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.ColorFilter; +import android.graphics.Paint; +import android.graphics.PixelFormat; +import android.graphics.Rect; +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; + +import java.io.InputStream; + +/** + * Fast bitmap drawable. Does not support states. it only + * support alpha and colormatrix + * + * @author alessandro + */ +public class FastBitmapDrawable extends Drawable implements IBitmapDrawable { + private Bitmap mBitmap; + private final Paint mPaint; + private final int mIntrinsicWidth; + private final int mIntrinsicHeight; + + public FastBitmapDrawable(Bitmap b) { + mBitmap = b; + if (null != mBitmap) { + mIntrinsicWidth = mBitmap.getWidth(); + mIntrinsicHeight = mBitmap.getHeight(); + } else { + mIntrinsicWidth = 0; + mIntrinsicHeight = 0; + } + mPaint = new Paint(); + mPaint.setDither(true); + mPaint.setFilterBitmap(true); + } + + public void setBitmap(Bitmap bitmap) { + mBitmap = bitmap; + } + + public FastBitmapDrawable(Resources res, InputStream is) { + this(BitmapFactory.decodeStream(is)); + } + + @Override + public void draw(@NonNull Canvas canvas) { + if (null != mBitmap && !mBitmap.isRecycled()) { + final Rect bounds = getBounds(); + if (!bounds.isEmpty()) { + canvas.drawBitmap(mBitmap, null, bounds, mPaint); + } else { + canvas.drawBitmap(mBitmap, 0f, 0f, mPaint); + } + } + } + + @Override + public int getOpacity() { + return PixelFormat.TRANSLUCENT; + } + + @Override + public void setAlpha(int alpha) { + mPaint.setAlpha(alpha); + } + + @Override + public void setColorFilter(ColorFilter cf) { + mPaint.setColorFilter(cf); + } + + @Override + public int getIntrinsicWidth() { + return mIntrinsicWidth; + } + + @Override + public int getIntrinsicHeight() { + return mIntrinsicHeight; + } + + @Override + public int getMinimumWidth() { + return mIntrinsicWidth; + } + + @Override + public int getMinimumHeight() { + return mIntrinsicHeight; + } + + public void setAntiAlias(boolean value) { + mPaint.setAntiAlias(value); + invalidateSelf(); + } + + @Override + public Bitmap getBitmap() { + return mBitmap; + } + + public Paint getPaint() { + return mPaint; + } +} \ No newline at end of file diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/graphics/IBitmapDrawable.java b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/graphics/IBitmapDrawable.java new file mode 100644 index 0000000..b211470 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/graphics/IBitmapDrawable.java @@ -0,0 +1,14 @@ +package mohammadaminha.com.widgets.ImageViewZoom.graphics; + +import android.graphics.Bitmap; + + +/** + * Base interface used in the {@link ImageViewTouchBase} view + * + * @author alessandro + */ +interface IBitmapDrawable { + + Bitmap getBitmap(); +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/utils/IDisposable.java b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/utils/IDisposable.java new file mode 100644 index 0000000..49b284c --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/ImageViewZoom/utils/IDisposable.java @@ -0,0 +1,6 @@ +package mohammadaminha.com.widgets.ImageViewZoom.utils; + +public interface IDisposable { + + void dispose(); +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/JalaliCalendar.java b/widgets/src/main/java/mohammadaminha/com/widgets/JalaliCalendar.java new file mode 100644 index 0000000..160e453 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/JalaliCalendar.java @@ -0,0 +1,418 @@ +package mohammadaminha.com.widgets; + +import java.util.Calendar; +import java.util.GregorianCalendar; + +public class JalaliCalendar { + private int year, month, day; + + /** + * Today Jalali Date + */ + public JalaliCalendar() { + fromGregorian(new GregorianCalendar()); + } + + /** + * Create a ir.huri.jcal.JalaliCalendar object + * + * @param year Jalali Year + * @param month Jalali Month + * @param day Jalali Day + */ + private JalaliCalendar(int year, int month, int day) { + set(year, month, day); + } + + + /** + * Create a ir.huri.jcal.JalaliCalendar object from gregorian calendar + * + * @param gc gregorian calendar object + */ + public JalaliCalendar(GregorianCalendar gc) { + fromGregorian(gc); + } + + /** + * Convert current jalali date to gregorian date + * + * @return date converted gregorianDate + */ + private GregorianCalendar toGregorian() { + int julianDay = toJulianDay(); + return julianDayToGregorianCalendar(julianDay); + } + + /** + * set date from gregorian date + * + * @param gc input gregorian calendar + */ + private void fromGregorian(GregorianCalendar gc) { + int jd = gregorianToJulianDayNumber(gc); + fromJulianDay(jd); + } + + /** + * @return yesterday date + */ + public JalaliCalendar getYesterday() { + return getDateByDiff(-1); + } + + /** + * @return tomorrow date + */ + public JalaliCalendar getTomorrow() { + return getDateByDiff(1); + } + + /** + * get Jalali date by day difference + * + * @param diff number of day diffrents + * @return jalali calendar diffحزن + */ + private JalaliCalendar getDateByDiff(int diff) { + GregorianCalendar gc = toGregorian(); + gc.add(Calendar.DAY_OF_MONTH, diff); + return new JalaliCalendar(gc); + } + + /** + * @return day Of Week + */ + private int getDayOfWeek() { + return toGregorian().get(Calendar.DAY_OF_WEEK); + } + + /** + * @return get first day of week + */ + public int getFirstDayOfWeek() { + return toGregorian().getFirstDayOfWeek(); + } + + /** + * @return day name + */ + private String getDayOfWeekString() { + switch (getDayOfWeek()) { + case 1: + return "یک‌شنبه"; + case 2: + return "دوشنبه"; + case 3: + return "سه‌شنبه"; + case 4: + return "چهارشنبه"; + case 5: + return "پنجشنبه"; + case 6: + return "جمعه"; + case 7: + return "شنبه"; + default: + return "نامعلوم"; + } + } + + /** + * @return month name + */ + private String getMonthString() { + switch (getMonth()) { + case 1: + return "فروردین"; + case 2: + return "اردیبهشت"; + case 3: + return "خرداد"; + case 4: + return "تیر"; + case 5: + return "مرداد"; + case 6: + return "شهریور"; + case 7: + return "مهر"; + case 8: + return "آبان"; + case 9: + return "آذر"; + case 10: + return "دی"; + case 11: + return "بهمن"; + case 12: + return "اسفند"; + default: + return "نامعلوم"; + } + } + + + /** + * get String with the following format : + * یکشنبه ۱۲ آبان + * + * @return String format + */ + + public String getDayOfWeekDayMonthString() { + return getDayOfWeekString() + " " + getDay() + " " + getMonthString(); + } + + /** + * @return return whether this year is a jalali leap year + */ + private boolean isLeap() { + return getLeapFactor(getYear()) == 0; + } + + public int getYearLength() { + return isLeap() ? 366 : 365; + } + + public int getMonthLength() { + if (getMonth() < 7) { + return 31; + } else if (getMonth() < 12) { + return 30; + } else if (getMonth() == 12) { + if (isLeap()) + return 30; + else + return 29; + } + return 0; + } + + private int getDay() { + return day; + } + + private int getMonth() { + return month; + } + + private int getYear() { + return year; + } + + private void setMonth(int month) { + this.month = month; + } + + private void setYear(int year) { + this.year = year; + } + + private void setDay(int day) { + this.day = day; + } + + private void set(int year, int month, int day) { + setYear(year); + setMonth(month); + setDay(day); + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + JalaliCalendar that = (JalaliCalendar) o; + + return year == that.year && month == that.month && day == that.day; + } + + private int gregorianToJulianDayNumber(GregorianCalendar gc) { + int gregorianYear = gc.get(GregorianCalendar.YEAR); + int gregorianMonth = gc.get(GregorianCalendar.MONTH) + 1; + int gregorianDay = gc.get(GregorianCalendar.DAY_OF_MONTH); + + return (((1461 * (gregorianYear + 4800 + (gregorianMonth - 14) / 12)) / 4 + + (367 * (gregorianMonth - 2 - 12 * ((gregorianMonth - 14) / 12))) / 12 + - (3 * ((gregorianYear + 4900 + (gregorianMonth - 14) / 12) / 100)) / 4 + gregorianDay + - 32075) - (gregorianYear + 100100 + (gregorianMonth - 8) / 6) / 100 * 3 / 4 + 752); + } + + private int julianToJulianDayNumber(JulianCalendar jc) { + int julianYear = jc.getYear(); + int julianMonth = jc.getMonth(); + int JulianDay = jc.getDay(); + + return (1461 * (julianYear + 4800 + (julianMonth - 14) / 12)) / 4 + + (367 * (julianMonth - 2 - 12 * ((julianMonth - 14) / 12))) / 12 + - (3 * ((julianYear + 4900 + (julianMonth - 14) / 12) / 100)) / 4 + JulianDay + - 32075; + } + + private GregorianCalendar julianDayToGregorianCalendar(int JulianDayNumber) { + + int j = 4 * JulianDayNumber + 139361631 + (4 * JulianDayNumber + 183187720) / 146097 * 3 / 4 * 4 - 3908; + int i = (j % 1461) / 4 * 5 + 308; + + int gregorianDay = (i % 153) / 5 + 1; + int gregorianMonth = ((i / 153) % 12) + 1; + int gregorianYear = j / 1461 - 100100 + (8 - gregorianMonth) / 6; + + return new GregorianCalendar(gregorianYear, gregorianMonth - 1, gregorianDay); + } + + private void fromJulianDay(int JulianDayNumber) { + GregorianCalendar gc = julianDayToGregorianCalendar(JulianDayNumber); + int gregorianYear = gc.get(GregorianCalendar.YEAR); + + int jalaliYear, jalaliMonth, jalaliDay; + + jalaliYear = gregorianYear - 621; + + GregorianCalendar gregorianFirstFarvardin = new JalaliCalendar(jalaliYear, 1, 1).getGregorianFirstFarvardin(); + int JulianDayFarvardinFirst = gregorianToJulianDayNumber(gregorianFirstFarvardin); + int diffFromFarvardinFirst = JulianDayNumber - JulianDayFarvardinFirst; + + + if (diffFromFarvardinFirst >= 0) { + if (diffFromFarvardinFirst <= 185) { + jalaliMonth = 1 + diffFromFarvardinFirst / 31; + jalaliDay = (diffFromFarvardinFirst % 31) + 1; + set(jalaliYear, jalaliMonth, jalaliDay); + return; + } else { + diffFromFarvardinFirst = diffFromFarvardinFirst - 186; + } + } else { + diffFromFarvardinFirst = diffFromFarvardinFirst + 179; + if (getLeapFactor(jalaliYear) == 1) + diffFromFarvardinFirst = diffFromFarvardinFirst + 1; + jalaliYear -= 1; + } + + + jalaliMonth = 7 + diffFromFarvardinFirst / 30; + jalaliDay = (diffFromFarvardinFirst % 30) + 1; + set(jalaliYear, jalaliMonth, jalaliDay); + } + + private int toJulianDay() { + int jalaliMonth = getMonth(); + int jalaliDay = getDay(); + + GregorianCalendar gregorianFirstFarvardin = getGregorianFirstFarvardin(); + int gregorianYear = gregorianFirstFarvardin.get(Calendar.YEAR); + int gregorianMonth = gregorianFirstFarvardin.get(Calendar.MONTH) + 1; + int gregorianDay = gregorianFirstFarvardin.get(Calendar.DAY_OF_MONTH); + + JulianCalendar julianFirstFarvardin = new JulianCalendar(gregorianYear, gregorianMonth, gregorianDay); + + return julianToJulianDayNumber(julianFirstFarvardin) + (jalaliMonth - 1) * 31 - jalaliMonth / 7 * (jalaliMonth - 7) + + jalaliDay - 1; + } + + + private GregorianCalendar getGregorianFirstFarvardin() { + int marchDay = 0; + int[] breaks = {-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, + 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178}; + + int jalaliYear = getYear(); + int gregorianYear = jalaliYear + 621; + int jalaliLeap = -14; + int jp = breaks[0]; + + int jump ; + for (int j = 1; j <= 19; j++) { + int jm = breaks[j]; + jump = jm - jp; + if (jalaliYear < jm) { + int N = jalaliYear - jp; + jalaliLeap = jalaliLeap + N / 33 * 8 + (N % 33 + 3) / 4; + + if ((jump % 33) == 4 && (jump - N) == 4) + jalaliLeap = jalaliLeap + 1; + + int GregorianLeap = (gregorianYear / 4) - (gregorianYear / 100 + 1) * 3 / 4 - 150; + + marchDay = 20 + (jalaliLeap - GregorianLeap); + + if ((jump - N) < 6) + N = N - jump + (jump + 4) / 33 * 33; + + break; + } + + jalaliLeap = jalaliLeap + jump / 33 * 8 + (jump % 33) / 4; + jp = jm; + } + + return new GregorianCalendar(gregorianYear, 2, marchDay); + } + + private int getLeapFactor(int jalaliYear) { + int leap = 0; + int[] breaks = {-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, + 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178}; + + int jp = breaks[0]; + + int jump ; + for (int j = 1; j <= 19; j++) { + int jm = breaks[j]; + jump = jm - jp; + if (jalaliYear < jm) { + int N = jalaliYear - jp; + + if ((jump - N) < 6) + N = N - jump + (jump + 4) / 33 * 33; + + leap = ((((N + 1) % 33) - 1) % 4); + + if (leap == -1) + leap = 4; + + break; + } + + jp = jm; + } + + return leap; + } + + @Override + public String toString() { + return String.format("%04d-%02d-%02d", getYear(), getMonth(), getDay()); + } + + + private class JulianCalendar { + final int year; + final int month; + final int day; + + public JulianCalendar(int year, int month, int day) { + this.year = year; + this.month = month; + this.day = day; + } + + public int getYear() { + return year; + } + + public int getMonth() { + return month; + } + + public int getDay() { + return day; + } + } + + +} \ No newline at end of file diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/JalaliCalendar1.java b/widgets/src/main/java/mohammadaminha/com/widgets/JalaliCalendar1.java new file mode 100644 index 0000000..c1f5104 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/JalaliCalendar1.java @@ -0,0 +1,802 @@ +package mohammadaminha.com.widgets; + +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.Locale; +import java.util.TimeZone; + +public class JalaliCalendar1 extends Calendar { + private static final int[] gregorianDaysInMonth = {31, 28, 31, 30, 31, + 30, 31, 31, 30, 31, 30, 31}; + private static final int[] jalaliDaysInMonth = {31, 31, 31, 31, 31, 31, + 30, 30, 30, 30, 30, 29}; + + private static int FARVARDIN = 0; + public final static int ORDIBEHESHT = 1; + public final static int KHORDAD = 2; + public final static int TIR = 3; + public final static int MORDAD = 4; + public final static int SHAHRIVAR = 5; + public final static int MEHR = 6; + public final static int ABAN = 7; + public final static int AZAR = 8; + public final static int DEY = 9; + public final static int BAHMAN = 10; + private static int ESFAND = 11; + + private static TimeZone timeZone = TimeZone.getDefault(); + private static boolean isTimeSeted = false; + + private static final int ONE_SECOND = 1000; + private static final int ONE_MINUTE = 60 * ONE_SECOND; + private static final int ONE_HOUR = 60 * ONE_MINUTE; + private static final long ONE_DAY = 24 * ONE_HOUR; + private static final int BCE = 0; + private static final int CE = 1; + public static final int AD = 1; + private GregorianCalendar cal; + + private static final int[] MIN_VALUES = { + BCE, // ERA + 1, // YEAR + FARVARDIN, // MONTH + 1, // WEEK_OF_YEAR + 0, // WEEK_OF_MONTH + 1, // DAY_OF_MONTH + 1, // DAY_OF_YEAR + SATURDAY, // DAY_OF_WEEK + 1, // DAY_OF_WEEK_IN_MONTH + AM, // AM_PM + 0, // HOUR + 0, // HOUR_OF_DAY + 0, // MINUTE + 0, // SECOND + 0, // MILLISECOND + -13 * ONE_HOUR, // ZONE_OFFSET (UNIX compatibility) + 0 // DST_OFFSET + }; + + private static final int[] LEAST_MAX_VALUES = { + CE, // ERA + 292269054, // YEAR + ESFAND, // MONTH + 52, // WEEK_OF_YEAR + 4, // WEEK_OF_MONTH + 28, // DAY_OF_MONTH + 365, // DAY_OF_YEAR + FRIDAY, // DAY_OF_WEEK + 4, // DAY_OF_WEEK_IN + PM, // AM_PM + 11, // HOUR + 23, // HOUR_OF_DAY + 59, // MINUTE + 59, // SECOND + 999, // MILLISECOND + 14 * ONE_HOUR, // ZONE_OFFSET + 20 * ONE_MINUTE // DST_OFFSET (historical least maximum) + }; + + private static final int[] MAX_VALUES = { + CE, // ERA + 292278994, // YEAR + ESFAND, // MONTH + 53, // WEEK_OF_YEAR + 6, // WEEK_OF_MONTH + 31, // DAY_OF_MONTH + 366, // DAY_OF_YEAR + FRIDAY, // DAY_OF_WEEK + 6, // DAY_OF_WEEK_IN + PM, // AM_PM + 11, // HOUR + 23, // HOUR_OF_DAY + 59, // MINUTE + 59, // SECOND + 999, // MILLISECOND + 14 * ONE_HOUR, // ZONE_OFFSET + 2 * ONE_HOUR // DST_OFFSET (double summer time) + }; + + public JalaliCalendar1() { + this(TimeZone.getDefault(), Locale.getDefault()); + } + + public JalaliCalendar1(TimeZone zone) { + this(zone, Locale.getDefault()); + } + + public JalaliCalendar1(Locale aLocale) { + this(TimeZone.getDefault(), aLocale); + } + + private JalaliCalendar1(TimeZone zone, Locale aLocale) { + + super(zone, aLocale); + timeZone = zone; + Calendar calendar = Calendar.getInstance(zone, aLocale); + + YearMonthDate yearMonthDate = new YearMonthDate(calendar.get(YEAR), calendar.get(MONTH), calendar.get(DATE)); + yearMonthDate = gregorianToJalali(yearMonthDate); + set(yearMonthDate.getYear(), yearMonthDate.getMonth(), yearMonthDate.getDate()); + complete(); + + } + + public JalaliCalendar1(int year, int month, int dayOfMonth) { + this(year, month, dayOfMonth, 0, 0, 0, 0); + } + + public JalaliCalendar1(int year, int month, int dayOfMonth, int hourOfDay, + int minute) { + this(year, month, dayOfMonth, hourOfDay, minute, 0, 0); + } + + public JalaliCalendar1(int year, int month, int dayOfMonth, int hourOfDay, + int minute, int second) { + this(year, month, dayOfMonth, hourOfDay, minute, second, 0); + } + + private JalaliCalendar1(int year, int month, int dayOfMonth, + int hourOfDay, int minute, int second, int millis) { + super(); + + this.set(YEAR, year); + this.set(MONTH, month); + this.set(DAY_OF_MONTH, dayOfMonth); + + if (hourOfDay >= 12 && hourOfDay <= 23) { + + this.set(AM_PM, PM); + this.set(HOUR, hourOfDay - 12); + } else { + this.set(HOUR, hourOfDay); + this.set(AM_PM, AM); + } + + this.set(HOUR_OF_DAY, hourOfDay); + this.set(MINUTE, minute); + this.set(SECOND, second); + + this.set(MILLISECOND, millis); + + YearMonthDate yearMonthDate = jalaliToGregorian(new YearMonthDate(fields[1], fields[2], fields[5])); + cal = new GregorianCalendar(yearMonthDate.getYear(), yearMonthDate.getMonth(), yearMonthDate.getDate(), hourOfDay, + minute, second); + time = cal.getTimeInMillis(); + + isTimeSeted = true; + } + + + private static YearMonthDate gregorianToJalali(YearMonthDate gregorian) { + + if (gregorian.getMonth() > 11 || gregorian.getMonth() < -11) { + throw new IllegalArgumentException(); + } + int jalaliYear; + int jalaliMonth; + int jalaliDay; + + int gregorianDayNo, jalaliDayNo; + int jalaliNP; + int i; + + gregorian.setYear(gregorian.getYear() - 1600); + gregorian.setDate(gregorian.getDate() - 1); + + gregorianDayNo = 365 * gregorian.getYear() + (int) Math.floor((gregorian.getYear() + 3) / 4) + - (int) Math.floor((gregorian.getYear() + 99) / 100) + + (int) Math.floor((gregorian.getYear() + 399) / 400); + for (i = 0; i < gregorian.getMonth(); ++i) { + gregorianDayNo += gregorianDaysInMonth[i]; + } + + if (gregorian.getMonth() > 1 && ((gregorian.getYear() % 4 == 0 && gregorian.getYear() % 100 != 0) + || (gregorian.getYear() % 400 == 0))) { + ++gregorianDayNo; + } + + gregorianDayNo += gregorian.getDate(); + + jalaliDayNo = gregorianDayNo - 79; + + jalaliNP = (int) Math.floor(jalaliDayNo / 12053); + jalaliDayNo = jalaliDayNo % 12053; + + jalaliYear = 979 + 33 * jalaliNP + 4 * jalaliDayNo / 1461; + jalaliDayNo = jalaliDayNo % 1461; + + if (jalaliDayNo >= 366) { + jalaliYear += (int) Math.floor((jalaliDayNo - 1) / 365); + jalaliDayNo = (jalaliDayNo - 1) % 365; + } + + for (i = 0; i < 11 && jalaliDayNo >= jalaliDaysInMonth[i]; ++i) { + jalaliDayNo -= jalaliDaysInMonth[i]; + } + jalaliMonth = i; + jalaliDay = jalaliDayNo + 1; + + return new YearMonthDate(jalaliYear, jalaliMonth, jalaliDay); + } + + + private static YearMonthDate jalaliToGregorian(YearMonthDate jalali) { + if (jalali.getMonth() > 11 || jalali.getMonth() < -11) { + throw new IllegalArgumentException(); + } + + int gregorianYear; + int gregorianMonth; + int gregorianDay; + + int gregorianDayNo, jalaliDayNo; + int leap; + + int i; + jalali.setYear(jalali.getYear() - 979); + jalali.setDate(jalali.getDate() - 1); + + jalaliDayNo = 365 * jalali.getYear() + jalali.getYear() / 33 * 8 + + (int) Math.floor(((jalali.getYear() % 33) + 3) / 4); + for (i = 0; i < jalali.getMonth(); ++i) { + jalaliDayNo += jalaliDaysInMonth[i]; + } + + jalaliDayNo += jalali.getDate(); + + gregorianDayNo = jalaliDayNo + 79; + + gregorianYear = 1600 + 400 * (int) Math.floor(gregorianDayNo / 146097); /* 146097 = 365*400 + 400/4 - 400/100 + 400/400 */ + gregorianDayNo = gregorianDayNo % 146097; + + leap = 1; + if (gregorianDayNo >= 36525) /* 36525 = 365*100 + 100/4 */ { + gregorianDayNo--; + gregorianYear += 100 * (int) Math.floor(gregorianDayNo / 36524); /* 36524 = 365*100 + 100/4 - 100/100 */ + gregorianDayNo = gregorianDayNo % 36524; + + if (gregorianDayNo >= 365) { + gregorianDayNo++; + } else { + leap = 0; + } + } + + gregorianYear += 4 * (int) Math.floor(gregorianDayNo / 1461); /* 1461 = 365*4 + 4/4 */ + gregorianDayNo = gregorianDayNo % 1461; + + if (gregorianDayNo >= 366) { + leap = 0; + + gregorianDayNo--; + gregorianYear += (int) Math.floor(gregorianDayNo / 365); + gregorianDayNo = gregorianDayNo % 365; + } + + for (i = 0; gregorianDayNo >= gregorianDaysInMonth[i] + ((i == 1 && leap == 1) ? i : 0); i++) { + gregorianDayNo -= gregorianDaysInMonth[i] + ((i == 1 && leap == 1) ? i : 0); + } + gregorianMonth = i; + gregorianDay = gregorianDayNo + 1; + + return new YearMonthDate(gregorianYear, gregorianMonth, gregorianDay); + + } + + private static int weekOfYear(int dayOfYear, int year) { + switch (dayOfWeek(JalaliCalendar1.jalaliToGregorian(new YearMonthDate(year, 0, 1)))) { + case 2: + dayOfYear++; + break; + case 3: + dayOfYear += 2; + break; + case 4: + dayOfYear += 3; + break; + case 5: + dayOfYear += 4; + break; + case 6: + dayOfYear += 5; + break; + case 7: + dayOfYear--; + break; + } + dayOfYear = (int) Math.floor(dayOfYear / 7); + return dayOfYear + 1; + } + + private static int dayOfWeek(YearMonthDate yearMonthDate) { + + Calendar cal = new GregorianCalendar(yearMonthDate.getYear(), yearMonthDate.getMonth(), yearMonthDate.getDate()); + return cal.get(DAY_OF_WEEK); + + } + + private static boolean isLeepYear(int year) { + //Algorithm from www.wikipedia.com + return (year % 33 == 1 || year % 33 == 5 || year % 33 == 9 || year % 33 == 13 || + year % 33 == 17 || year % 33 == 22 || year % 33 == 26 || year % 33 == 30); + } + + @Override + protected void computeTime() { + + if (!isTimeSet && !isTimeSeted) { + Calendar cal = GregorianCalendar.getInstance(timeZone); + if (!isSet(HOUR_OF_DAY)) { + super.set(HOUR_OF_DAY, cal.get(HOUR_OF_DAY)); + } + if (!isSet(HOUR)) { + super.set(HOUR, cal.get(HOUR)); + } + if (!isSet(MINUTE)) { + super.set(MINUTE, cal.get(MINUTE)); + } + if (!isSet(SECOND)) { + super.set(SECOND, cal.get(SECOND)); + } + if (!isSet(MILLISECOND)) { + super.set(MILLISECOND, cal.get(MILLISECOND)); + } + if (!isSet(ZONE_OFFSET)) { + super.set(ZONE_OFFSET, cal.get(ZONE_OFFSET)); + } + if (!isSet(DST_OFFSET)) { + super.set(DST_OFFSET, cal.get(DST_OFFSET)); + } + if (!isSet(AM_PM)) { + super.set(AM_PM, cal.get(AM_PM)); + } + + if (internalGet(HOUR_OF_DAY) >= 12 && internalGet(HOUR_OF_DAY) <= 23) { + super.set(AM_PM, PM); + super.set(HOUR, internalGet(HOUR_OF_DAY) - 12); + } else { + super.set(HOUR, internalGet(HOUR_OF_DAY)); + super.set(AM_PM, AM); + } + + YearMonthDate yearMonthDate = jalaliToGregorian(new YearMonthDate(internalGet(YEAR), internalGet(MONTH), internalGet(DAY_OF_MONTH))); + cal.set(yearMonthDate.getYear(), yearMonthDate.getMonth(), yearMonthDate.getDate() + , internalGet(HOUR_OF_DAY), internalGet(MINUTE), internalGet(SECOND)); + time = cal.getTimeInMillis(); + + } else if (!isTimeSet && isTimeSeted) { + if (internalGet(HOUR_OF_DAY) >= 12 && internalGet(HOUR_OF_DAY) <= 23) { + super.set(AM_PM, PM); + super.set(HOUR, internalGet(HOUR_OF_DAY) - 12); + } else { + super.set(HOUR, internalGet(HOUR_OF_DAY)); + super.set(AM_PM, AM); + } + cal = new GregorianCalendar(); + super.set(ZONE_OFFSET, timeZone.getRawOffset()); + super.set(DST_OFFSET, timeZone.getDSTSavings()); + YearMonthDate yearMonthDate = jalaliToGregorian(new YearMonthDate(internalGet(YEAR), internalGet(MONTH), internalGet(DAY_OF_MONTH))); + cal.set(yearMonthDate.getYear(), yearMonthDate.getMonth(), yearMonthDate.getDate(), internalGet(HOUR_OF_DAY), + internalGet(MINUTE), internalGet(SECOND)); + time = cal.getTimeInMillis(); + } + } + + public void set(int field, int value) { + switch (field) { + case DATE: { + super.set(field, 0); + add(field, value); + break; + } + case MONTH: { + if (value > 11) { + super.set(field, 11); + add(field, value - 11); + } else if (value < 0) { + super.set(field, 0); + add(field, value); + } else { + super.set(field, value); + } + break; + } + case DAY_OF_YEAR: { + if (isSet(YEAR) && isSet(MONTH) && isSet(DAY_OF_MONTH)) { + super.set(YEAR, internalGet(YEAR)); + super.set(MONTH, 0); + super.set(DATE, 0); + add(field, value); + } else { + super.set(field, value); + } + break; + } + case WEEK_OF_YEAR: { + if (isSet(YEAR) && isSet(MONTH) && isSet(DAY_OF_MONTH)) { + add(field, value - get(WEEK_OF_YEAR)); + } else { + super.set(field, value); + } + break; + } + case WEEK_OF_MONTH: { + if (isSet(YEAR) && isSet(MONTH) && isSet(DAY_OF_MONTH)) { + add(field, value - get(WEEK_OF_MONTH)); + } else { + super.set(field, value); + } + break; + } + case DAY_OF_WEEK: { + if (isSet(YEAR) && isSet(MONTH) && isSet(DAY_OF_MONTH)) { + add(DAY_OF_WEEK, value % 7 - get(DAY_OF_WEEK)); + } else { + super.set(field, value); + } + break; + } + case HOUR_OF_DAY: + case HOUR: + case MINUTE: + case SECOND: + case MILLISECOND: + case ZONE_OFFSET: + case DST_OFFSET: { + if (isSet(YEAR) && isSet(MONTH) && isSet(DATE) && isSet(HOUR) && isSet(HOUR_OF_DAY) && + isSet(MINUTE) && isSet(SECOND) && isSet(MILLISECOND)) { + cal = new GregorianCalendar(); + YearMonthDate yearMonthDate = jalaliToGregorian(new YearMonthDate(internalGet(YEAR), internalGet(MONTH), internalGet(DATE))); + cal.set(yearMonthDate.getYear(), yearMonthDate.getMonth(), yearMonthDate.getDate(), internalGet(HOUR_OF_DAY), internalGet(MINUTE), + internalGet(SECOND)); + cal.set(field, value); + yearMonthDate = gregorianToJalali(new YearMonthDate(cal.get(YEAR), cal.get(MONTH), cal.get(DATE))); + super.set(YEAR, yearMonthDate.getYear()); + super.set(MONTH, yearMonthDate.getMonth()); + super.set(DATE, yearMonthDate.getDate()); + super.set(HOUR_OF_DAY, cal.get(HOUR_OF_DAY)); + super.set(MINUTE, cal.get(MINUTE)); + super.set(SECOND, cal.get(SECOND)); + + } else { + super.set(field, value); + } + break; + } + + + default: { + super.set(field, value); + } + } + } + + @Override + protected void computeFields() { + boolean temp = isTimeSet; + if (!areFieldsSet) { + setMinimalDaysInFirstWeek(1); + setFirstDayOfWeek(7); + + //Day_Of_Year + int dayOfYear = 0; + int index = 0; + + while (index < fields[2]) { + dayOfYear += jalaliDaysInMonth[index++]; + } + dayOfYear += fields[5]; + super.set(DAY_OF_YEAR, dayOfYear); + //*** + + //Day_of_Week + super.set(DAY_OF_WEEK, dayOfWeek(jalaliToGregorian(new YearMonthDate(fields[1], fields[2], fields[5])))); + //*** + + //Day_Of_Week_In_Month + if (0 < fields[5] && fields[5] < 8) { + super.set(DAY_OF_WEEK_IN_MONTH, 1); + } + + if (7 < fields[5] && fields[5] < 15) { + super.set(DAY_OF_WEEK_IN_MONTH, 2); + } + + if (14 < fields[5] && fields[5] < 22) { + super.set(DAY_OF_WEEK_IN_MONTH, 3); + } + + if (21 < fields[5] && fields[5] < 29) { + super.set(DAY_OF_WEEK_IN_MONTH, 4); + } + + if (28 < fields[5] && fields[5] < 32) { + super.set(DAY_OF_WEEK_IN_MONTH, 5); + } + //*** + + //Week_Of_Year + super.set(WEEK_OF_YEAR, weekOfYear(fields[6], fields[1])); + //*** + + //Week_Of_Month + super.set(WEEK_OF_MONTH, weekOfYear(fields[6], fields[1]) - weekOfYear(fields[6] - fields[5], fields[1]) + 1); + // + + isTimeSet = temp; + } + } + + @Override + public void add(int field, int amount) { + + if (field == MONTH) { + amount += get(MONTH); + add(YEAR, amount / 12); + super.set(MONTH, amount % 12); + if (get(DAY_OF_MONTH) > jalaliDaysInMonth[amount % 12]) { + super.set(DAY_OF_MONTH, jalaliDaysInMonth[amount % 12]); + if (get(MONTH) == 11 && isLeepYear(get(YEAR))) { + super.set(DAY_OF_MONTH, 30); + } + } + complete(); + + } else if (field == YEAR) { + + super.set(YEAR, get(YEAR) + amount); + if (get(DAY_OF_MONTH) == 30 && get(MONTH) == 11 && !isLeepYear(get(YEAR))) { + super.set(DAY_OF_MONTH, 29); + } + + complete(); + } else { + YearMonthDate yearMonthDate = jalaliToGregorian(new YearMonthDate(get(YEAR), get(MONTH), get(DATE))); + Calendar gc = new GregorianCalendar(yearMonthDate.getYear(), yearMonthDate.getMonth(), yearMonthDate.getDate(), + get(HOUR_OF_DAY), get(MINUTE), get(SECOND)); + gc.add(field, amount); + yearMonthDate = gregorianToJalali(new YearMonthDate(gc.get(YEAR), gc.get(MONTH), gc.get(DATE))); + super.set(YEAR, yearMonthDate.getYear()); + super.set(MONTH, yearMonthDate.getMonth()); + super.set(DATE, yearMonthDate.getDate()); + super.set(HOUR_OF_DAY, gc.get(HOUR_OF_DAY)); + super.set(MINUTE, gc.get(MINUTE)); + super.set(SECOND, gc.get(SECOND)); + complete(); + } + + } + + @Override + public void roll(int field, boolean up) { + roll(field, up ? +1 : -1); + } + + @Override + public void roll(int field, int amount) { + if (amount == 0) { + return; + } + + if (field < 0 || field >= ZONE_OFFSET) { + throw new IllegalArgumentException(); + } + + complete(); + + switch (field) { + case AM_PM: { + if (amount % 2 != 0) { + if (internalGet(AM_PM) == AM) { + fields[AM_PM] = PM; + } else { + fields[AM_PM] = AM; + } + if (get(AM_PM) == AM) { + super.set(HOUR_OF_DAY, get(HOUR)); + } else { + super.set(HOUR_OF_DAY, get(HOUR) + 12); + } + } + break; + } + case YEAR: { + super.set(YEAR, internalGet(YEAR) + amount); + if (internalGet(MONTH) == 11 && internalGet(DAY_OF_MONTH) == 30 && !isLeepYear(internalGet(YEAR))) { + super.set(DAY_OF_MONTH, 29); + } + break; + } + case MINUTE: { + int unit = 60; + int m = (internalGet(MINUTE) + amount) % unit; + if (m < 0) { + m += unit; + } + super.set(MINUTE, m); + break; + } + case SECOND: { + int unit = 60; + int s = (internalGet(SECOND) + amount) % unit; + if (s < 0) { + s += unit; + } + super.set(SECOND, s); + break; + } + case MILLISECOND: { + int unit = 1000; + int ms = (internalGet(MILLISECOND) + amount) % unit; + if (ms < 0) { + ms += unit; + } + super.set(MILLISECOND, ms); + break; + } + + case HOUR: { + super.set(HOUR, (internalGet(HOUR) + amount) % 12); + if (internalGet(HOUR) < 0) { + fields[HOUR] += 12; + } + if (internalGet(AM_PM) == AM) { + super.set(HOUR_OF_DAY, internalGet(HOUR)); + } else { + super.set(HOUR_OF_DAY, internalGet(HOUR) + 12); + } + + break; + } + case HOUR_OF_DAY: { + fields[HOUR_OF_DAY] = (internalGet(HOUR_OF_DAY) + amount) % 24; + if (internalGet(HOUR_OF_DAY) < 0) { + fields[HOUR_OF_DAY] += 24; + } + if (internalGet(HOUR_OF_DAY) < 12) { + fields[AM_PM] = AM; + fields[HOUR] = internalGet(HOUR_OF_DAY); + } else { + fields[AM_PM] = PM; + fields[HOUR] = internalGet(HOUR_OF_DAY) - 12; + } + + } + case MONTH: { + int mon = (internalGet(MONTH) + amount) % 12; + if (mon < 0) { + mon += 12; + } + super.set(MONTH, mon); + + int monthLen = jalaliDaysInMonth[mon]; + if (internalGet(MONTH) == 11 && isLeepYear(internalGet(YEAR))) { + monthLen = 30; + } + if (internalGet(DAY_OF_MONTH) > monthLen) { + super.set(DAY_OF_MONTH, monthLen); + } + break; + } + case DAY_OF_MONTH: { + int unit = 0; + if (0 <= get(MONTH) && get(MONTH) <= 5) { + unit = 31; + } + if (6 <= get(MONTH) && get(MONTH) <= 10) { + unit = 30; + } + if (get(MONTH) == 11) { + if (isLeepYear(get(YEAR))) { + unit = 30; + } else { + unit = 29; + } + } + int d = (get(DAY_OF_MONTH) + amount) % unit; + if (d < 0) { + d += unit; + } + super.set(DAY_OF_MONTH, d); + break; + + } + case WEEK_OF_YEAR: { + break; + } + case DAY_OF_YEAR: { + int unit = (isLeepYear(internalGet(YEAR)) ? 366 : 365); + int dayOfYear = (internalGet(DAY_OF_YEAR) + amount) % unit; + dayOfYear = (dayOfYear > 0) ? dayOfYear : dayOfYear + unit; + int month = 0, temp = 0; + while (dayOfYear > temp) { + temp += jalaliDaysInMonth[month++]; + } + super.set(MONTH, --month); + super.set(DAY_OF_MONTH, jalaliDaysInMonth[internalGet(MONTH)] - (temp - dayOfYear)); + break; + } + case DAY_OF_WEEK: { + int index = amount % 7; + if (index < 0) { + index += 7; + } + int i = 0; + while (i != index) { + if (internalGet(DAY_OF_WEEK) == FRIDAY) { + add(DAY_OF_MONTH, -6); + } else { + add(DAY_OF_MONTH, +1); + } + i++; + } + break; + } + + default: + throw new IllegalArgumentException(); + } + + } + + @Override + public int getMinimum(int field) { + return MIN_VALUES[field]; + } + + @Override + public int getMaximum(int field) { + return MAX_VALUES[field]; + } + + @Override + public int getGreatestMinimum(int field) { + return MIN_VALUES[field]; + } + + @Override + public int getLeastMaximum(int field) { + return LEAST_MAX_VALUES[field]; + } + + public static class YearMonthDate { + + public YearMonthDate(int year, int month, int date) { + this.year = year; + this.month = month; + this.date = date; + } + + private int year; + private int month; + private int date; + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public int getMonth() { + return month; + } + + public void setMonth(int month) { + this.month = month; + } + + public int getDate() { + return date; + } + + public void setDate(int date) { + this.date = date; + } + + public String toString() { + return getYear() + "/" + getMonth() + "/" + getDate(); + } + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/RadioButton.java b/widgets/src/main/java/mohammadaminha/com/widgets/RadioButton.java new file mode 100644 index 0000000..10b79dc --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/RadioButton.java @@ -0,0 +1,35 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.util.AttributeSet; + +/** + * Created by amin on 2/21/18. + */ + +public class RadioButton extends android.support.v7.widget.AppCompatRadioButton { + + public RadioButton(Context context) { + super(context); + } + + public RadioButton(Context context, AttributeSet attrs) { + super(context, attrs); + setTf(context); + } + + public RadioButton(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setTf(context); + } + + + + private void setTf(Context context) { + + setTypeface(Util.getTypeFace()); + } + + +} + diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Spinner/OnSpinerItemClick.java b/widgets/src/main/java/mohammadaminha/com/widgets/Spinner/OnSpinerItemClick.java new file mode 100644 index 0000000..c30086b --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Spinner/OnSpinerItemClick.java @@ -0,0 +1,9 @@ +package mohammadaminha.com.widgets.Spinner; + + +import org.json.JSONException; + +public interface OnSpinerItemClick +{ + void onClick(String item, int position) throws JSONException; +} \ No newline at end of file diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Switch.java b/widgets/src/main/java/mohammadaminha/com/widgets/Switch.java new file mode 100644 index 0000000..45b1f3f --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Switch.java @@ -0,0 +1,33 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.util.AttributeSet; + +/** + * Created by aj on 2/6/2018. + */ + +public class Switch extends android.widget.Switch { + + public Switch(Context context) { + super(context); + setTf(context); + } + + public Switch(Context context, AttributeSet attrs) { + super(context, attrs); + setTf(context); + } + + public Switch(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setTf(context); + } + private void setTf(Context context) { + + setTypeface(Util.getTypeFace()); + + } + + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/TextInputLayout.java b/widgets/src/main/java/mohammadaminha/com/widgets/TextInputLayout.java new file mode 100644 index 0000000..196a7cf --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/TextInputLayout.java @@ -0,0 +1,34 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.util.AttributeSet; + +/** + * Created by aj on 2/5/2018. + */ + +public class TextInputLayout extends android.support.design.widget.TextInputLayout { + + + public TextInputLayout(Context context) { + super(context); + setTf(context); + } + + public TextInputLayout(Context context, AttributeSet attrs) { + super(context, attrs); + setTf(context); + } + + public TextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setTf(context); + } + + private void setTf(Context context) { + + setTypeface(Util.getTypeFace()); + } + + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/TextView.java b/widgets/src/main/java/mohammadaminha/com/widgets/TextView.java new file mode 100644 index 0000000..baa2a57 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/TextView.java @@ -0,0 +1,41 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.util.AttributeSet; + +/** + * Created by aj on 1/30/2018. + */ + +public class TextView extends android.support.v7.widget.AppCompatTextView { + + + public TextView(Context context) { + super(context); + setTf(context); + } + + public TextView(Context context, AttributeSet attrs) { + super(context, attrs); + setTf(context); + } + + public TextView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setTf(context); + } + + private void setTf(Context context) { + + setTypeface(Util.getTypeFace()); + + } + + + + public void setText(Context context) { + } + + public void setText() { + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/ToolbarCustomizer.java b/widgets/src/main/java/mohammadaminha/com/widgets/ToolbarCustomizer.java new file mode 100644 index 0000000..8ef21ec --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/ToolbarCustomizer.java @@ -0,0 +1,28 @@ +package mohammadaminha.com.widgets; + +import android.graphics.Color; +import android.graphics.PorterDuff; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.view.View; + +/** + * Created by pc3 on 4/17/2018. + */ + +public class ToolbarCustomizer { + public static void Toolbar(final AppCompatActivity activity, Toolbar toolbar) { + activity.setSupportActionBar(toolbar); + activity.getSupportActionBar().setHomeButtonEnabled(true); + activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true); + activity.getSupportActionBar().setDisplayShowTitleEnabled(false); + + toolbar.getNavigationIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP); + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + activity.finish(); + } + }); + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/Util.java b/widgets/src/main/java/mohammadaminha/com/widgets/Util.java new file mode 100644 index 0000000..e71b56c --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/Util.java @@ -0,0 +1,28 @@ +package mohammadaminha.com.widgets; + +import android.content.Context; +import android.graphics.Typeface; + +public class Util { + private static String fontAddress = ""; + private static Typeface typeFace; + private static Context context; + + public Util(String address, Context cnx) { + fontAddress = address; + context = cnx; + typeFace = Typeface.createFromAsset(context.getAssets(), Util.getAddress()); + } + + public static String getAddress() { + return fontAddress; + } + + public static Typeface getTypeFace() { + return typeFace; + } + + public static Context getContext() { + return context; + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/cToast.java b/widgets/src/main/java/mohammadaminha/com/widgets/cToast.java new file mode 100644 index 0000000..49ee9b5 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/cToast.java @@ -0,0 +1,40 @@ +package mohammadaminha.com.widgets; + +import android.app.Activity; +import android.content.Context; +import android.os.Build; +import android.support.annotation.RequiresApi; +import android.widget.LinearLayout; +import android.widget.Toast; + +/** + * Created by amin on 1/18/18. + */ + +public class cToast { + /** + * برای تعیین مدت زمان نمایش پیام + * + * @param ToastLengh 0_Toast.LENGTH_SHORT + * 1_Toast.LENGTH_LONG + */ + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) + public static void show(Activity activity, String message, int ToastLengh) { + Toast toast = Toast.makeText(activity, message, ToastLengh); + LinearLayout toastLayout = (LinearLayout) toast.getView(); + android.widget.TextView toastTV = (android.widget.TextView) toastLayout.getChildAt(0); + toastTV.setText(message); + toastTV.setTypeface(Util.getTypeFace()); + toast.show(); + } + + public static void show(Context context, String message, int ToastLengh) { + Toast toast = Toast.makeText(context, message, ToastLengh); + LinearLayout toastLayout = (LinearLayout) toast.getView(); + android.widget.TextView toastTV = (android.widget.TextView) toastLayout.getChildAt(0); + toastTV.setText(message); + toastTV.setTypeface(Util.getTypeFace()); + toast.show(); + } + +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/particleview/LineEvaluator.java b/widgets/src/main/java/mohammadaminha/com/widgets/particleview/LineEvaluator.java new file mode 100644 index 0000000..d5429b5 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/particleview/LineEvaluator.java @@ -0,0 +1,19 @@ +package mohammadaminha.com.widgets.particleview; + +import android.animation.TypeEvaluator; + +/** + * 作者: 巴掌 on 16/8/27 12:06 + * Github: https://github.com/JeasonWong + */ +public class LineEvaluator implements TypeEvaluator { + + @Override + public Particle evaluate(float fraction, Particle startValue, Particle endValue) { + Particle particle = new Particle(); + particle.x = startValue.x + (endValue.x - startValue.x) * fraction; + particle.y = startValue.y + (endValue.y - startValue.y) * fraction; + particle.radius = startValue.radius + (endValue.radius - startValue.radius) * fraction; + return particle; + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/particleview/Particle.java b/widgets/src/main/java/mohammadaminha/com/widgets/particleview/Particle.java new file mode 100644 index 0000000..8b264f9 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/particleview/Particle.java @@ -0,0 +1,21 @@ +package mohammadaminha.com.widgets.particleview; + +/** + * 作者: 巴掌 on 16/8/27 12:07 + * Github: https://github.com/JeasonWong + */ +public class Particle { + + public float x; + public float y; + public float radius; + + public Particle() { + } + + public Particle(float x, float y, float radius) { + this.x = x; + this.y = y; + this.radius = radius; + } +} diff --git a/widgets/src/main/java/mohammadaminha/com/widgets/particleview/ParticleView.java b/widgets/src/main/java/mohammadaminha/com/widgets/particleview/ParticleView.java new file mode 100644 index 0000000..a5a3959 --- /dev/null +++ b/widgets/src/main/java/mohammadaminha/com/widgets/particleview/ParticleView.java @@ -0,0 +1,385 @@ +package mohammadaminha.com.widgets.particleview; + +import android.animation.Animator; +import android.animation.AnimatorSet; +import android.animation.ValueAnimator; +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.LinearGradient; +import android.graphics.Paint; +import android.graphics.PointF; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.Shader; +import android.graphics.Typeface; +import android.util.AttributeSet; +import android.view.View; + +import java.util.ArrayList; +import java.util.Collection; + +import mohammadaminha.com.widgets.R; +import mohammadaminha.com.widgets.Util; + +/** + * 作者: 巴掌 on 16/8/27 11:29 + * Github: https://github.com/JeasonWong + */ +public class ParticleView extends View { + + private final int STATUS_MOTIONLESS = 0; + private final int STATUS_PARTICLE_GATHER = 1; + private final int STATUS_TEXT_MOVING = 2; + + private final int ROW_NUM = 10; + private final int COLUMN_NUM = 10; + + private final int DEFAULT_MAX_TEXT_SIZE = sp2px(80); + private final int DEFAULT_MIN_TEXT_SIZE = sp2px(30); + + public final int DEFAULT_TEXT_ANIM_TIME = 1000; + public final int DEFAULT_SPREAD_ANIM_TIME = 300; + public final int DEFAULT_HOST_TEXT_ANIM_TIME = 800; + + public Paint mHostTextPaint; + private Paint mParticleTextPaint; + private Paint mCirclePaint; + private Paint mHostBgPaint; + private int mWidth, mHeight; + + private Particle[][] mParticles = new Particle[ROW_NUM][COLUMN_NUM]; + private Particle[][] mMinParticles = new Particle[ROW_NUM][COLUMN_NUM]; + + //背景色 + private int mBgColor; + //粒子色 + private int mParticleColor; + //默认粒子文案大小 + private int mParticleTextSize = DEFAULT_MIN_TEXT_SIZE; + + private int mStatus = STATUS_MOTIONLESS; + + private ParticleAnimListener mParticleAnimListener; + + //粒子文案 + private String mParticleText; + //主文案 + public String mHostText; + //扩散宽度 + private float mSpreadWidth; + //Host文字展现宽度 + private float mHostRectWidth; + //粒子文案的x坐标 + private float mParticleTextX; + //Host文字的x坐标 + private float mHostTextX; + + //Text anim time in milliseconds + private int mTextAnimTime; + //Spread anim time in milliseconds + private int mSpreadAnimTime; + //HostText anim time in milliseconds + private int mHostTextAnimTime; + + private PointF mStartMaxP, mEndMaxP; + private PointF mStartMinP, mEndMinP; + Typeface bold; + public ParticleView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public ParticleView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + initView(attrs); + } + + private void initView(AttributeSet attrs) { + + bold = Typeface.create(Util.getTypeFace(), Typeface.BOLD); + + TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.ParticleView); + mHostText = null == typeArray.getString(R.styleable.ParticleView_pv_host_text) ? "" : typeArray.getString(R.styleable.ParticleView_pv_host_text); + mParticleText = null == typeArray.getString(R.styleable.ParticleView_pv_particle_text) ? "" : typeArray.getString(R.styleable.ParticleView_pv_particle_text); + mParticleTextSize = (int) typeArray.getDimension(R.styleable.ParticleView_pv_particle_text_size, DEFAULT_MIN_TEXT_SIZE); + int hostTextSize = (int) typeArray.getDimension(R.styleable.ParticleView_pv_host_text_size, DEFAULT_MIN_TEXT_SIZE); + mBgColor = typeArray.getColor(R.styleable.ParticleView_pv_background_color, 0xFF0867AB); + mParticleColor = typeArray.getColor(R.styleable.ParticleView_pv_text_color, 0xFFCEF4FD); + mTextAnimTime = typeArray.getInt(R.styleable.ParticleView_pv_text_anim_time, DEFAULT_TEXT_ANIM_TIME); + mSpreadAnimTime = typeArray.getInt(R.styleable.ParticleView_pv_text_anim_time, DEFAULT_SPREAD_ANIM_TIME); + mHostTextAnimTime = typeArray.getInt(R.styleable.ParticleView_pv_text_anim_time, DEFAULT_HOST_TEXT_ANIM_TIME); + typeArray.recycle(); + + mHostTextPaint = new Paint(); + mHostTextPaint.setAntiAlias(true); + mHostTextPaint.setTextSize(hostTextSize); + + + mParticleTextPaint = new Paint(); + mParticleTextPaint.setAntiAlias(true); + mCirclePaint = new Paint(); + mCirclePaint.setAntiAlias(true); + mHostBgPaint = new Paint(); + mHostBgPaint.setAntiAlias(true); + mHostBgPaint.setTextSize(hostTextSize); + + mParticleTextPaint.setTextSize(mParticleTextSize); + mCirclePaint.setTextSize(mParticleTextSize); + + mParticleTextPaint.setColor(mBgColor); + mHostTextPaint.setColor(mBgColor); + mCirclePaint.setColor(mParticleColor); + mHostBgPaint.setColor(mParticleColor); + + mHostTextPaint.setTypeface(bold); + mHostBgPaint.setTypeface(bold); + mParticleTextPaint.setTypeface(bold); + + } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + mWidth = w; + mHeight = h; + + mStartMinP = new PointF(mWidth / 2 - getTextWidth(mParticleText, mParticleTextPaint) / 2f - dip2px(4), mHeight / 2 + getTextHeight(mHostText, mHostTextPaint) / 2 - getTextHeight(mParticleText, mParticleTextPaint) / 0.7f); + mEndMinP = new PointF(mWidth / 2 + getTextWidth(mParticleText, mParticleTextPaint) / 2f + dip2px(10), mHeight / 2 + getTextHeight(mHostText, mHostTextPaint) / 2); + + for (int i = 0; i < ROW_NUM; i++) { + for (int j = 0; j < COLUMN_NUM; j++) { + mMinParticles[i][j] = new Particle(mStartMinP.x + (mEndMinP.x - mStartMinP.x) / COLUMN_NUM * j, mStartMinP.y + (mEndMinP.y - mStartMinP.y) / ROW_NUM * i, dip2px(0.8f)); + } + } + + mStartMaxP = new PointF(mWidth / 2 - DEFAULT_MAX_TEXT_SIZE, mHeight / 2 - DEFAULT_MAX_TEXT_SIZE); + mEndMaxP = new PointF(mWidth / 2 + DEFAULT_MAX_TEXT_SIZE, mHeight / 2 + DEFAULT_MAX_TEXT_SIZE); + + for (int i = 0; i < ROW_NUM; i++) { + for (int j = 0; j < COLUMN_NUM; j++) { + mParticles[i][j] = new Particle(mStartMaxP.x + (mEndMaxP.x - mStartMaxP.x) / COLUMN_NUM * j, mStartMaxP.y + (mEndMaxP.y - mStartMaxP.y) / ROW_NUM * i, getTextWidth(mHostText + mParticleText, mParticleTextPaint) / (COLUMN_NUM * 1.8f)); + } + } + + Shader linearGradient = new LinearGradient(mWidth / 2 - getTextWidth(mParticleText, mCirclePaint) / 2f, + mHeight / 2 - getTextHeight(mParticleText, mCirclePaint) / 2, + mWidth / 2 - getTextWidth(mParticleText, mCirclePaint) / 2, + mHeight / 2 + getTextHeight(mParticleText, mCirclePaint) / 2, + new int[]{mParticleColor, Color.argb(120, getR(mParticleColor), getG(mParticleColor), getB(mParticleColor))}, null, Shader.TileMode.CLAMP); + mCirclePaint.setShader(linearGradient); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + if (mStatus == STATUS_PARTICLE_GATHER) { + for (int i = 0; i < ROW_NUM; i++) { + for (int j = 0; j < COLUMN_NUM; j++) { + canvas.drawCircle(mParticles[i][j].x, mParticles[i][j].y, mParticles[i][j].radius, mCirclePaint); + } + } + } + + if (mStatus == STATUS_TEXT_MOVING) { + canvas.drawText(mHostText, mHostTextX, mHeight / 2 + getTextHeight(mHostText, mHostBgPaint) / 2, mHostBgPaint); + canvas.drawRect(mHostTextX + mHostRectWidth, mHeight / 2 - getTextHeight(mHostText, mHostBgPaint) / 1.2f, mHostTextX + getTextWidth(mHostText, mHostTextPaint), mHeight / 2 + getTextHeight(mHostText, mHostBgPaint) / 1.2f, mHostTextPaint); + } + + if (mStatus == STATUS_PARTICLE_GATHER) { + canvas.drawRoundRect(new RectF(mWidth / 2 - mSpreadWidth, mStartMinP.y, mWidth / 2 + mSpreadWidth, mEndMinP.y), dip2px(2), dip2px(2), mHostBgPaint); + canvas.drawText(mParticleText, mWidth / 2 - getTextWidth(mParticleText, mParticleTextPaint) / 2, mStartMinP.y + (mEndMinP.y - mStartMinP.y) / 2 + getTextHeight(mParticleText, mParticleTextPaint) / 2, mParticleTextPaint); + } else if (mStatus == STATUS_TEXT_MOVING) { + canvas.drawRoundRect(new RectF(mParticleTextX - dip2px(4), mStartMinP.y, mParticleTextX + getTextWidth(mParticleText, mParticleTextPaint) + dip2px(4), mEndMinP.y), dip2px(2), dip2px(2), mHostBgPaint); + canvas.drawText(mParticleText, mParticleTextX, mStartMinP.y + (mEndMinP.y - mStartMinP.y) / 2 + getTextHeight(mParticleText, mParticleTextPaint) / 2, mParticleTextPaint); + } + + } + + private void startParticleAnim() { + + mStatus = STATUS_PARTICLE_GATHER; + + Collection animList = new ArrayList<>(); + + ValueAnimator textAnim = ValueAnimator.ofInt(DEFAULT_MAX_TEXT_SIZE, mParticleTextSize); + textAnim.setDuration((int) (mTextAnimTime * 0.8f)); + textAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator valueAnimator) { + int textSize = (int) valueAnimator.getAnimatedValue(); + mParticleTextPaint.setTextSize(textSize); + } + }); + animList.add(textAnim); + + for (int i = 0; i < ROW_NUM; i++) { + for (int j = 0; j < COLUMN_NUM; j++) { + final int tempI = i; + final int tempJ = j; + ValueAnimator animator = ValueAnimator.ofObject(new LineEvaluator(), mParticles[i][j], mMinParticles[i][j]); + animator.setDuration(mTextAnimTime + ((int) (mTextAnimTime * 0.02f)) * i + ((int) (mTextAnimTime * 0.03f)) * j); + animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mParticles[tempI][tempJ] = (Particle) animation.getAnimatedValue(); + if (tempI == ROW_NUM - 1 && tempJ == COLUMN_NUM - 1) { + invalidate(); + } + } + }); + animList.add(animator); + } + } + + AnimatorSet set = new AnimatorSet(); + set.playTogether(animList); + set.start(); + + set.addListener(new AnimListener() { + @Override + public void onAnimationEnd(Animator animation) { + startSpreadAnim(); + } + }); + + } + + private void startSpreadAnim() { + ValueAnimator animator = ValueAnimator.ofFloat(0, getTextWidth(mParticleText, mParticleTextPaint) / 2 + dip2px(4)); + animator.setDuration(mSpreadAnimTime); + animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mSpreadWidth = (float) animation.getAnimatedValue(); + invalidate(); + } + }); + animator.addListener(new AnimListener() { + @Override + public void onAnimationEnd(Animator animation) { + startHostTextAnim(); + } + }); + animator.start(); + } + + private void startHostTextAnim() { + mStatus = STATUS_TEXT_MOVING; + + Collection animList = new ArrayList<>(); + + ValueAnimator particleTextXAnim = ValueAnimator.ofFloat(mStartMinP.x + dip2px(4), mWidth / 2 - (getTextWidth(mHostText, mHostTextPaint) + getTextWidth(mParticleText, mParticleTextPaint)) / 2 + getTextWidth(mHostText, mHostTextPaint)); + particleTextXAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mParticleTextX = (float) animation.getAnimatedValue(); + } + }); + animList.add(particleTextXAnim); + + ValueAnimator animator = ValueAnimator.ofFloat(0, getTextWidth(mHostText, mHostTextPaint)); + animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mHostRectWidth = (float) animation.getAnimatedValue(); + } + }); + animList.add(animator); + + ValueAnimator hostTextXAnim = ValueAnimator.ofFloat(mStartMinP.x, mWidth / 2 - (getTextWidth(mHostText, mHostTextPaint) + getTextWidth(mParticleText, mParticleTextPaint) + dip2px(20)) / 2); + hostTextXAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mHostTextX = (float) animation.getAnimatedValue(); + invalidate(); + } + }); + animList.add(hostTextXAnim); + + AnimatorSet set = new AnimatorSet(); + set.playTogether(animList); + set.setDuration(mHostTextAnimTime); + set.addListener(new AnimListener() { + @Override + public void onAnimationEnd(Animator animation) { + if (null != mParticleAnimListener) { + mParticleAnimListener.onAnimationEnd(); + } + } + }); + set.start(); + + } + + public void startAnim() { + post(new Runnable() { + @Override + public void run() { + startParticleAnim(); + } + }); + } + + private abstract class AnimListener implements Animator.AnimatorListener { + @Override + public void onAnimationStart(Animator animation) { + + } + + @Override + public void onAnimationCancel(Animator animation) { + + } + + @Override + public void onAnimationRepeat(Animator animation) { + + } + } + + public void setOnParticleAnimListener(ParticleAnimListener particleAnimListener) { + mParticleAnimListener = particleAnimListener; + } + + public interface ParticleAnimListener { + void onAnimationEnd(); + } + + private int dip2px(float dipValue) { + final float scale = getContext().getResources().getDisplayMetrics().density; + return (int) (dipValue * scale + 0.5f); + } + + private int sp2px(float spValue) { + final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity; + return (int) (spValue * fontScale + 0.5f); + } + + private float getTextHeight(String text, Paint paint) { + Rect rect = new Rect(); + paint.getTextBounds(text, 0, text.length(), rect); + return rect.height() / 1.1f; + } + + private float getTextWidth(String text, Paint paint) { + return paint.measureText(text); + } + + private int getR(int color) { + int r = (color >> 16) & 0xFF; + return r; + } + + private int getG(int color) { + int g = (color >> 8) & 0xFF; + return g; + } + + private int getB(int color) { + int b = color & 0xFF; + return b; + } +} + diff --git a/widgets/src/main/res/color/mdtp_date_picker_selector.xml b/widgets/src/main/res/color/mdtp_date_picker_selector.xml new file mode 100644 index 0000000..15606d2 --- /dev/null +++ b/widgets/src/main/res/color/mdtp_date_picker_selector.xml @@ -0,0 +1,23 @@ + + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/color/mdtp_date_picker_year_selector.xml b/widgets/src/main/res/color/mdtp_date_picker_year_selector.xml new file mode 100644 index 0000000..eb2ce13 --- /dev/null +++ b/widgets/src/main/res/color/mdtp_date_picker_year_selector.xml @@ -0,0 +1,23 @@ + + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/color/mdtp_done_text_color.xml b/widgets/src/main/res/color/mdtp_done_text_color.xml new file mode 100644 index 0000000..7e99f97 --- /dev/null +++ b/widgets/src/main/res/color/mdtp_done_text_color.xml @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/color/mdtp_done_text_color_dark.xml b/widgets/src/main/res/color/mdtp_done_text_color_dark.xml new file mode 100644 index 0000000..1c16f23 --- /dev/null +++ b/widgets/src/main/res/color/mdtp_done_text_color_dark.xml @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/drawable/mdtp_done_background_color.xml b/widgets/src/main/res/drawable/mdtp_done_background_color.xml new file mode 100644 index 0000000..f301fcc --- /dev/null +++ b/widgets/src/main/res/drawable/mdtp_done_background_color.xml @@ -0,0 +1,25 @@ + + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/drawable/mdtp_done_background_color_dark.xml b/widgets/src/main/res/drawable/mdtp_done_background_color_dark.xml new file mode 100644 index 0000000..d494c14 --- /dev/null +++ b/widgets/src/main/res/drawable/mdtp_done_background_color_dark.xml @@ -0,0 +1,25 @@ + + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/drawable/mdtp_material_button_background.xml b/widgets/src/main/res/drawable/mdtp_material_button_background.xml new file mode 100644 index 0000000..f70c39c --- /dev/null +++ b/widgets/src/main/res/drawable/mdtp_material_button_background.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/drawable/mdtp_material_button_selected.xml b/widgets/src/main/res/drawable/mdtp_material_button_selected.xml new file mode 100644 index 0000000..1733e2d --- /dev/null +++ b/widgets/src/main/res/drawable/mdtp_material_button_selected.xml @@ -0,0 +1,13 @@ + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/layout/mdtp_date_picker_dialog.xml b/widgets/src/main/res/layout/mdtp_date_picker_dialog.xml new file mode 100644 index 0000000..04acb71 --- /dev/null +++ b/widgets/src/main/res/layout/mdtp_date_picker_dialog.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/layout/mdtp_date_picker_header_view.xml b/widgets/src/main/res/layout/mdtp_date_picker_header_view.xml new file mode 100644 index 0000000..517ecf7 --- /dev/null +++ b/widgets/src/main/res/layout/mdtp_date_picker_header_view.xml @@ -0,0 +1,26 @@ + + + diff --git a/widgets/src/main/res/layout/mdtp_date_picker_selected_date.xml b/widgets/src/main/res/layout/mdtp_date_picker_selected_date.xml new file mode 100644 index 0000000..c39a76b --- /dev/null +++ b/widgets/src/main/res/layout/mdtp_date_picker_selected_date.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/layout/mdtp_date_picker_view_animator.xml b/widgets/src/main/res/layout/mdtp_date_picker_view_animator.xml new file mode 100644 index 0000000..458d865 --- /dev/null +++ b/widgets/src/main/res/layout/mdtp_date_picker_view_animator.xml @@ -0,0 +1,21 @@ + + \ No newline at end of file diff --git a/widgets/src/main/res/layout/mdtp_done_button.xml b/widgets/src/main/res/layout/mdtp_done_button.xml new file mode 100644 index 0000000..c1582cd --- /dev/null +++ b/widgets/src/main/res/layout/mdtp_done_button.xml @@ -0,0 +1,41 @@ + + + + + + + diff --git a/widgets/src/main/res/layout/mdtp_time_header_label.xml b/widgets/src/main/res/layout/mdtp_time_header_label.xml new file mode 100644 index 0000000..efa46c9 --- /dev/null +++ b/widgets/src/main/res/layout/mdtp_time_header_label.xml @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + diff --git a/widgets/src/main/res/layout/mdtp_time_picker_dialog.xml b/widgets/src/main/res/layout/mdtp_time_picker_dialog.xml new file mode 100644 index 0000000..826ef7c --- /dev/null +++ b/widgets/src/main/res/layout/mdtp_time_picker_dialog.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + diff --git a/widgets/src/main/res/layout/mdtp_year_label_text_view.xml b/widgets/src/main/res/layout/mdtp_year_label_text_view.xml new file mode 100644 index 0000000..f0bd077 --- /dev/null +++ b/widgets/src/main/res/layout/mdtp_year_label_text_view.xml @@ -0,0 +1,23 @@ + + + diff --git a/widgets/src/main/res/values-v21/styles.xml b/widgets/src/main/res/values-v21/styles.xml new file mode 100644 index 0000000..8405313 --- /dev/null +++ b/widgets/src/main/res/values-v21/styles.xml @@ -0,0 +1,15 @@ + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/values/attrs.xml b/widgets/src/main/res/values/attrs.xml new file mode 100644 index 0000000..286873e --- /dev/null +++ b/widgets/src/main/res/values/attrs.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/widgets/src/main/res/values/colors.xml b/widgets/src/main/res/values/colors.xml new file mode 100644 index 0000000..2c377a8 --- /dev/null +++ b/widgets/src/main/res/values/colors.xml @@ -0,0 +1,76 @@ + + + + #9C27B0 + #9C27B0 + #a828be + #FF4081 + #cf33ea + #111 + #fff + #0f6afc + #f20a0a + #09f415 + #f5b504 + #eded80 + #b7afaf + #662e03 + #FFD700 + #FF565656 + #484848 + #235BD4 + #2EA4E4 + + + ///////////////Date Picker + + @color/YellowLight + @color/YellowLight + @color/YellowLight + @color/YellowLight + #cccccc + @color/mdtp_numbers_text_color + #ff212121 + #cccccc + #ff212121 + + #7f000000 + #76ffffff + #ff212121 + @color/mdtp_date_picker_text_normal + #ccc + #767676 + + + @color/BlackColor + @color/mdtp_white + #b4b4b4 + @color/WhiteColor + @color/mdtp_date_picker_text_normal_dark_theme + @color/colorAccent + @color/colorPrimaryDark + + @android:color/white + + @color/mdtp_accent_color + #33969696 + + + #525252 + + + @color/mdtp_accent_color + @color/mdtp_accent_color_dark + #424242 + #323232 + #808080 + #ffffff + #888888 + + + #444444 + #888888 + #888888 + + + diff --git a/widgets/src/main/res/values/dimens.xml b/widgets/src/main/res/values/dimens.xml new file mode 100644 index 0000000..3e65775 --- /dev/null +++ b/widgets/src/main/res/values/dimens.xml @@ -0,0 +1,61 @@ + + + 16dp + 16dp + 16dp + 16dp + 8dp + 12dp + 65dp + 5dp + + + + + ///////DatePicker + 0.82 + 0.85 + 0.16 + 0.22 + 0.81 + 0.60 + 0.83 + 0.12 + 0.11 + 0.08 + + 60sp + -30dp + 16sp + 6dip + 4dip + 96dip + 270dip + 300dip + + 270dp + 155dp + 190dp + 252dp + + 56dp + 12sp + 16dp + 45dp + 20dp + 50dp + 25dp + 12sp + 14sp + 12sp + 64dp + 22dp + + 48dp + 14sp + 64dp + 8dp + + 14sp + + diff --git a/widgets/src/main/res/values/strings.xml b/widgets/src/main/res/values/strings.xml new file mode 100644 index 0000000..b9103c5 --- /dev/null +++ b/widgets/src/main/res/values/strings.xml @@ -0,0 +1,713 @@ + + CustomWidgets + + تایید + بیخیال + + "لغزنده دایره‌ای ساعت" + + "لغزنده دایره‌ای دقیقه" + + "انتخاب ساعت" + + "انتخاب دقیقه" + + + Month grid of days + + Year list + + Select month and day + + Select year + + "%1$s انتخاب شد" + + "%1$s حذف شد" + + + -- + + : + + + sans-serif + + sans-serif + + + sans-serif + + + "https://bookdocs.ir/" + "https://bookdocs.ir/Doctors/public/images/patients/" + "https://bookdocs.ir/Doctors/public/images/doctors/" + + + جمعیت شناسی + جهانگردی + حسابداری + حسابداری مالیاتی + حشره شناسی كشاورزی + حفاظت اطلاعات + حفاظت و مرمت آثار تاریخی + حقوق + حقوق بین الملل عمومی + حقوق جزا و جرم شناسی + حقوق خصوصی + حقوق عمومی + حقوق قضایی + حقوق نفت و گاز + حمل و نقل ریلی + خط و سازه های ریلی + داروسازی + دامپزشكی + دریا + دریا ـ دریانوردی + دندانپزشكی + رادیولوژی دامپزشكی + راه آهن + روابط عمومی + روابط كار + روان شناسی اجتماعی + روان شناسی تربیتی + روان شناسی و آموزش كودكان استثنایی + روانشناسی + روزنامه نگاری + روزنامه نگاری علوم ارتباطات + ریاضی كاربردی - آنالیز عددی + ریاضی كاربردی - زیست ریاضی + ریاضی محض + ریاضیات و كاربردها + ریز زیست فناوری + زبان اسپانیایی + زبان ایتالیایی + زبان روسی + زبان شناسی + زبان و ادبیات آلمانی + زبان و ادبیات اردو + زبان و ادبیات ارمنی + زبان و ادبیات اسپانیایی + زبان و ادبیات انگلیسی + زبان و ادبیات تركی آذری + زبان و ادبیات تركی استانبولی + زبان و ادبیات عربی + زبان و ادبیات فارسی + زبان و ادبیات فرانسه + زبان و ادبیات كردی + زبان و ادبیات ژاپنی + زبان چینی + زبانهای باستانی ایران + زمین شناسی + زمین شناسی آب شناسی + زمین شناسی اقتصادی + زمین شناسی تكتونیك + زمین شناسی زیست محیطی + زمین شناسی سنگ شناسی رسوبی + زمین شناسی فسیل شناسی و چینه شناسی + زمین شناسی مهندسی + زمین شناسی نفت + زمین شناسی پترولوژی + زیست شناسی + زیست شناسی جانوری + زیست شناسی دریا + زیست شناسی سلولی و مولكولی + زیست شناسی ـ سلولی و تكوینی گیاهی + زیست شناسی ـ سلولی و ملكولی + زیست شناسی ـ سیستماتیك و بوم شناسی گیاهی + زیست شناسی ـ فیزیولوژی گیاهی + زیست شناسی ـ میكروبیولوژی + زیست شناسی ـ ژنتیك مولكولی + زیست شناسی محاسباتی بیوفیزیك + زیست شناسی گیاهی + زیست فناوری + زیست فناوری میكروبی + ساخت و تولید + سم شناسی + سنجش از دور و سامانه اطلاعات جغرافیایی + سنجش و اندازه گیری + سینما + شناسایی و مبارزه با علفهای هرز + شنوایی شناسی + شهرسازی + شهرسازی - انرژی شهرسازی + شیعه شناسی + شیلات ـ تولید و بهره برداری + شیلات ـ عمل آوری + شیمی + شیمی آزمایشگاهی + شیمی ـ شیمی آلی + شیمی ـ شیمی فیزیك + شیمی ـ شیمی معدنی + شیمی ـ شیمی پلیمر + شیمی كاربردی + شیمی محض + صنایع + صنایع خمیر و كاغذ + صنایع دستی + صنایع مبلمان + صنایع چوب و فرآورده های سلولزی + ضد تروریسم + طبیعت + طراحی شهری + طراحی صحنه + طراحی صنعتی + طراحی لباس + طراحی پارچه + طراحی پارچه و لباس + عكاسی + علم اطلاعات و دانش شناسی + علوم آزمایشگاهی + علوم آزمایشگاهی دامپزشكی + علوم ارتباطات اجتماعی + علوم اطلاعاتی + علوم اقتصادی + علوم انتظامی + علوم تربیتی + علوم تغذیه + علوم جانوری ـ بیوسیستماتیك جانوری + علوم جانوری ـ فیزیولوژی جانوری + علوم جنگل ـ جنگل شناسی + علوم جنگل ـ جنگل داری و مسائل اقتصادی + علوم جنگل ـ مهندسی جنگل + علوم دامی + علوم زمین + علوم سیاسی + علوم شناختی + علوم علف های هرز + علوم فنی امنیت + علوم قرآن و حدیث + علوم قضایی + علوم كامپیوتر + علوم محیط زیست + علوم مهندسی + علوم و آب + علوم و باغبانی + علوم و جنگل + علوم و خاك + علوم و شیلات + علوم و صنایع غذایی + علوم و صنایع غذایی كشاورزی) + علوم و فناوری نانو ـ نانوشیمی + علوم و فناوری نانو - نانوفیزیك + علوم و محیط زیست + علوم و مهندسی آب + علوم و مهندسی آب ـ آبیاری و زهكشی + علوم و مهندسی آب ـ سازه های آبی + علوم و مهندسی آب ـ منابع آب + علوم و مهندسی آب ـ هوا شناسی كشاورزی + علوم و مهندسی آبخیزداری + علوم و مهندسی باغبانی + علوم و مهندسی جنگل + علوم و مهندسی شیلات + علوم و مهندسی صنایع غذایی + علوم و مهندسی محیط زیست + علوم و مهندسی مرتع + علوم ورزشی + عمران + فارماكولوژی + فرش + فرهنگ و زبانهای باستانی + فرهنگ و معارف اسلامی + فضای سبز + فقه شافعی + فقه و حقوق اسلامی + فقه و حقوق امامی + فقه و حقوق حنفی + فقه و حقوق شافعی + فقه و مبانی حقوق اسلامی + فلسفه + فلسفه تعلیم و تربیت + فلسفه علم + فلسفه منطق + فلسفه و حكمت اسلامی + فلسفه و عرفان اسلامی + فلسفه و كلام اسلامی + فناوری اطلاعات سلامت + فناوری اطلاعات و ارتباطات + جوشكاری + عملیات پتروشیمی + فناوری تولید مثل در دامپزشكی + فناوری نانو ـ نانوالكتریك + فناوری نانو ـ نانومواد + فوتونیك + فوریتهای پزشكی + فیتوشیمی + فیزیك + فیزیك دریا + فیزیك مهندسی + فیزیوتراپی + فیزیولوژی + فیزیولوژی دامپزشكی + فیزیولوژی علوم جانوری ـ تكوینی + قارچ شناسی + قارچ شناسی دامپزشكی + كار آفرینی + كاردرمانی + كارگردانی تلویزیون + كامپیوتر + كتابت و نگارگری + كتابداری در شاخه پزشكی + كلینیكال پاتولوژی دامپزشكی + ماشین آلات دریایی + ماشینهای ریلی + ماشینهای صنایع غذایی + مامایی + مامایی و بیماریهای تولید مثل دام + مترجمی زبان آلمانی + مترجمی زبان انگلیسی + مترجمی زبان عربی + مترجمی زبان فرانسه + مجسمه سازی + برنامه ریزی شهری + مدیریت شهری + تربیت بدنی و علوم ورزشی + ریاضی + زبان آلمانی + زبان انگلیسی + زبان عربی + زبان فرانسه + زراعت و اصلاح نباتات + شیمی ـ بیوتكنولوژی و داروسازی + علوم اجتماعی + علوم جغرافیایی + علوم دام و طیور + علوم سیاسی و روابط بین الملل + فتونیك + مدیریت + مدیریت حاصلخیزی، زیست فناوری و منابع خاك + مدیریت كسب و كار و امور شهری + معماری + مهندسی برق + مهندسی شیمی + مهندسی عمران + مهندسی معماری كشتی + مهندسی مكانیك + مهندسی مواد و متالورژی + مهندسی هوافضا + هنرهای ساخت و معماری + هنرهای موسیقی + هنرهای نمایشی و سینما + هنرهای پژوهشی و صنایع دستی + ژئوفیزیك و هواشناسی + محیط زیست + محیط زیست ـ برنامه ریزی + مخابرات + مخابرات هواپیمایی + مددكاری اجتماعی + مدرسی معارف اسلامی + مدیریت اطلاعاتی + مدیریت امور بانكی + مدیریت امور گمركی + مدیریت بازرگانی + مدیریت بیمه + مدیریت بیمه اكو + مدیریت جهانگردی + مدیریت خدمات اجتماعی مددكاری اجتماعی + مدیریت خدمات بهداشتی درمانی + مدیریت دریایی + مدیریت دولتی + مدیریت راهبردی و آینده پژوهی + مدیریت صنعتی + مدیریت فرهنگی هنری + مدیریت فناوری اطلاعات + مدیریت قراردادهای بین المللی نفت و گاز + مدیریت كسب و كارهای كوچك + مدیریت كشاورزی + مدیریت مالی + مدیریت منابع خاك + مدیریت هتل داری گردشگری + مدیریت و بازرگانی دریایی + مدیریت و كنترل بیابان + مدیریت پروژه + مربیگری عقیدتی + مربیگری ورزشی + مردم شناسی + مرمت آثار تاریخی + مرمت بناهای تاریخی + مرمت و احیای ابنیه و بافتهای تاریخی + مشاوره + مطالعات ارتباطی و فناوری اطلاعات + مطالعات تعاونی های علوم اجتماعی + مطالعات جهان + مطالعات خانواده + مطالعات زنان + معارف اسلامی + معارف اسلامی ـ تبلیغ و ارتباطات + معارف اسلامی و ارشاد + معارف اسلامی و تاریخ + معارف اسلامی و حقوق + معارف اسلامی و علوم تربیتی + معارف اسلامی و كلام + معارف اسلامی و مدیریت + معدن + معماری داخلی + معماری سنتی + مكانیزاسیون كشاورزی + مكانیك + مكانیك بیوسیستم كشاورزی) + مهندسی ابزار دقیق و اتوماسیون در صنایع نفت + مهندسی اقتصاد كشاورزی + مهندسی ایمنی و بازرسی فنی + مهندسی برق ـ الكترونیك + مهندسی برق ـ قدرت + مهندسی برق ـ كنترل + مهندسی برق ـ مخابرات + مهندسی در سوانح طبیعی + مهندسی دریا + مهندسی سیستمهای انرژی + مهندسی شیمی ـ بهداشت، ایمنی و محیط زیست HSE) + مهندسی شیمی ـ بیوتكنولوژی + مهندسی شیمی ـ بیوتكنولوژی و داروسازی + مهندسی صنایع + مهندسی صنایع چوب و فرآورده های سلولزی + مهندسی طراحی محیط زیست + مهندسی عمران ـ حمل و نقل + مهندسی عمران ـ راه و ترابری + مهندسی عمران ـ زلزله + مهندسی عمران ـ سازه + مهندسی عمران ـ سواحل، بنادر و سازههای دریایی + مهندسی عمران ـ محیط زیست + مهندسی عمران ـ مدیریت ساخت + مهندسی عمران ـ مدیریت منابع آب + مهندسی عمران ـ مهندسی آب و سازه های هیدرولیكی + مهندسی عمران ـ ژئوتكنیك + مهندسی فضای سبز + مهندسی فناوری اطلاعات IT) + مهندسی كامپیوتر + مهندسی كامپیوتر ـ شبكه و رایانش + مهندسی كامپیوتر ـ معماری سیستم های كامپیوتری + مهندسی كامپیوتر ـ نرم افزار و الگوریتم + مهندسی كامپیوتر ـ هوش مصنوعی + مهندسی متابولیك - زراعت + مهندسی محیط زیست ـ آب و فاضلاب + مهندسی محیط زیست ـ آلودگی هوا + مهندسی محیط زیست ـ منابع آب + مهندسی محیط زیست ـ مواد زائد جامد + مهندسی معدن + مهندسی معدن ـ استخراج + مهندسی معدن ـ اكتشاف + مهندسی معدن ـ فرآوری مواد معدنی + مهندسی معدن ـ مكانیك سنگ + مهندسی مكانیزاسیون كشاورزی + مهندسی مكانیك بیوسیستم + مهندسی مكانیك ـ تبدیل انرژی + مهندسی مكانیك ـ دینامیك، كنترل و ارتعاشات + مهندسی مكانیك ـ ساخت و تولید + مهندسی مكانیك ـ مكانیك جامدات + مهندسی نساجی + مهندسی نساجی ـ تكنولوژی نساجی + مهندسی نساجی ـ شیمی نساجی و علوم الیاف + مهندسی نفت + مهندسی نفت - حفاری + مهندسی نفت ـ اكتشاف + مهندسی نقشه برداری + مهندسی نقشه برداری ـ سنجش از دور + مهندسی نقشه برداری ـ سیستم اطلاعات جغرافیایی GIS) + مهندسی نقشه برداری ـ فتوگرامتری + مهندسی نقشه برداری ـ ژئودزی + مهندسی هسته ای ـ راكتور + مهندسی هسته ای ـ كاربرد پرتوها + مهندسی هسته ای ـ پرتو پزشكی + مهندسی هسته ای ـ گداخت + مهندسی هوا فضا ـ آئرودینامیك + مهندسی هوا فضا ـ جلوبرندگی + مهندسی هوا فضا ـ دینامیك پرواز و كنترل + مهندسی هوا فضا ـ سازه های هوایی + مهندسی پزشكی ـ بیوالكتریك + مهندسی پزشكی ـ بیومتریال + مهندسی پزشكی ـ بیومكانیك + مهندسی پلیمر + مهندسی پلیمر ـ رنگ + مهندسی پلیمر ـ صنایع رنگ + مهندسی پلیمر ـ پلیمر + مواد و متالورژی + موزه + موسیقی نظامی + میكروبیولوژی + نانوفناوری ـ نانومواد + نساجی + نفت + نقاشی + نقشه برداری + نمایش عروسكی + نمونه گیری و آمار + نوازندگی موسیقی ایرانی + نوازندگی موسیقی جهانی + هتل داری + هنر اسلامی + هنر سفالگری + هنرهای تجسمی + هنرهای صناعی + هنرهای چند رسانه ای + هواشناسی + هوافضا + هوانوردی + هوشبری + ویروس شناسی + یادگیری فناورانه تكنولوژی آموزشی + پاتولوژی دامپزشكی + پالایش گاز + پرستاری + پزشكی + پلیمر + پژوهش هنر + پژوهشگری اجتماعی + پژوهشگری امنیت + چاپ + ژئوفیزیك ـ الكترومغناطیس + ژئوفیزیك ـ زلزله شناسی + ژئوفیزیك ـ لرزه شناسی + ژئوفیزیك ـ گرانی سنجی + ژئومورفولوژی + ژنتیك و به نژادی گیاهی + گرافیك + گفتار درمانی + گیاه پزشكی + آب و هواشناسی + آمار + آمار و سنجش آموزشی + آموزش ابتدایی + آموزش الهیات و معارف اسلامی + آموزش تاریخ + آموزش جغرافیا + آموزش راهنمایی و مشاوره + آموزش ریاضی + آموزش زبان آلمانی + آموزش زبان انگلیسی + آموزش زبان روسی + آموزش زبان فرانسه + آموزش زبان و ادبیات عربی + آموزش زبان و ادبیات فارسی + آموزش زیست شناسی + آموزش شیمی + آموزش علوم اجتماعی + آموزش علوم ورزشی + آموزش فیزیك + آموزش كودكان استثنایی + آموزش و پرورش ابتدایی + آناتومی و جنین شناسی + آهنگسازی + اتاق عمل + بهداشت و كنترل كیفی مواد غذایی + ادبیات نمایشی + ادیان و عرفان + ادیان و مذاهب + ارتباط تصویری + ارتباطات و فناوری اطلاعات + اشتغال + اعضای مصنوعی + اقتصاد + اقتصاد اسلامی + اقتصاد كار و بهره وری + اقتصاد كشاورزی + اقتصاد نفت و گاز + اقیانوس شناسی + اقیانوس شناسی فیزیكی + اكوهیدرولوژی + الكترونیك هواپیمایی + الكترونیك و مخابرات دریایی + الهیات ـ ادیان و عرفان + الهیات ـ تاریخ و تمدن ملل اسلامی + الهیات ـ علوم قرآن و حدیث + الهیات ـ فقه و مبانی حقوق اسلامی + الهیات ـ كلام + الهیات و معارف اسلامی فقه شافعی + امنیت اطلاعات + امنیت اقتصادی + امنیت بین الملل + امنیت نرم + امور اراضی + امور بانكی + امور تربیتی + امور دولتی + امور زراعی و باغی + امور فرهنگی + امور مالی و مالیاتی + انرژی + انیمیشن + انگل شناسی + انگل شناسی دامپزشكی + ایران شناسی + ایمنی + ایمنی شناسی + ایمنی شناسی دامپزشكی + ایمنی صنعتی + اپتیك و لیزر + اپیدمیولوژی + بازیگری + باستان سنجی شیمی ـ شیمی تجزیه + باستان شناسی + بافت شناسی + بافت شناسی دامپزشكی + باكتری شناسی + باكتری شناسی دامپزشكی + برق + برنامه ریزی آموزشی مدیریت آموزشی + برنامه ریزی اجتماعی و تعاون + برنامه ریزی درسی + بهداشت حرفه ای + بهداشت خوراك دام + بهداشت عمومی + بهداشت محیط + بهداشت مواد غذایی + بهداشت و بازرسی گوشت + بهداشت و بیماریهای آبزیان + بهداشت و بیماریهای پرندگان + بهره برداری راه اهن + بهینه سازی مصرف انرژی + بوم شناسی زراعی + بیماری شناسی گیاهی + بیمه + بینایی سنجی + بیهوشی و مراقبتهای ویژه دامپزشكی + بیوانفورماتیك + بیوتكنولوژی + بیوتكنولوژی كشاورزی + بیوشیمی + بیوشیمی بالینی + بیولوژی و آناتومی چوب + بیولوژی و كنترل ناقلین بیماریها + تاریخ + تاریخ اسلام + تاریخ ـ تاریخ اسلام + تاریخ ـ تاریخ ایران دوره اسلام + تاریخ ـ تاریخ ایران قبل ازاسلام + تاریخ و تمدن ملل اسلامی + تاریخ و فلسفه علم + تربیت بدنی ـ آسیب شناسی ورزشی + تربیت بدنی ـ بیومكانیك ورزشی + تربیت بدنی ـ رفتارحركتی + تربیت بدنی ـ فیزیولوژی ورزشی + تربیت بدنی ـ مدیریت ورزشی + تربیت مبلغ قرآن كریم + تربیت مروج سیاسی + تربیت معلم قرآن كریم + ترجمه + ترویج و آموزش كشاورزی پایدار + تعمیر و نگهداری هواپیما + تكثیر و پرورش آبزیان + تكنسین سلامت دهان + تكنسین پروتزهای دندانی + تكنولوژی آبیاری + تكنولوژی تولیدات دامی + تكنولوژی تولیدات گیاهی + تكنولوژی رادیولوژی دهان، فك و صورت + تكنولوژی صنایع غذایی + تكنولوژی ماشینهای كشاورزی + تكنولوژی محیط زیست + تكنولوژی مرتع و آبخیزداری + تكنولوژی مواد غذایی + تكنولوژی پرتو درمانی + تكنولوژی پرتوشناسی + تكنولوژی پزشكی هسته ای + تلویزیون و هنرهای دیجیتالی + توسعه روستایی + توسعه كشاورزی + تولید و بهره برداری ازگیاهان دارویی و معطر + تولید و فرآوری خرما + تولید و ژنتیك گیاهی + جامعه شناسی + جراحی دامپزشكی + جغرافیا + جغرافیا و برنامه ریزی روستایی + جغرافیا و برنامه ریزی شهری + جغرافیای سیاسی + علوم انسانی + علوم تجربی + ریاضی و فیزیک + + + + پزشک + کلینیک + داروخانه + آزمایشگاه + بیمارستان + فیزیوتراپی + تجهیزات پزشکی + + + + doctor + clinik + pharmacy + lab + hospital + physiotherapy + medical_equipment + + + + select_doctorsAdver + select_places + select_lab_hospitals + select_lab_hospitals + select_lab_hospitals + select_lab_hospitals + select_lab_hospitals + + + + 1//پزشک + 2//کلینیک + 1//داروخانه + 2آزمایشگاه// + 3//بیمارستان + 4//فیزیوتراپی + 5//تجحیزات پزشکی + + + + شرکت شبکه رفاه ایرانیان (سهامی خاص) در سال 1395 با هدف خدمت رسانی به شما هموطنان عزیز و ایجاد فضای مناسب برای کاریابی و اشتغال زایی راه اندازی گردید. + +
    +
    + اهداف و سیاست های کلی رفاه من +
    + + ارائه مرجعی کامل از اماکن تفریحی، ورزشی و گردشگری تخفیف دار در سطح کشور +
    + تسهیل در نوبت دهی غیر حضوری و آنلاین پزشکان متخصص و درمانگاه ها +
    + ارائه مرجع کامل و تخصصی کسب و کار و کاریابی +
    + فراهم نمودن بستری مناسب جهت بیشتر دیده شدن کسب و کار و فعالان اقتصادی +
    + ایجاد شرایط مطلوب جهت اشتغال زایی برای عزیزانی ک تابحال سابقه کار نداشته اند. +
    + توزیع یکنواخت کار بین متخصصین مشاغل +
    + قرار دادن افراد در تخصص های مرتبط با تجربه کاری و ایجاد بستر مناسب برای رشد و توسعه فردی +
    + فراهم نمودن فضای مناسب درجهت سرعت بخشیدن به امر استخدام و کاریابی در سالی که به نام " اقتصاد مقاومتی ، تولید و اشتغال " مزین گردیده است. +
    +

    + ]]> +
    + + + + چرخش + معکوس کردن + + انتخاب فایل + +
    diff --git a/widgets/src/main/res/values/styles.xml b/widgets/src/main/res/values/styles.xml new file mode 100644 index 0000000..9fe576b --- /dev/null +++ b/widgets/src/main/res/values/styles.xml @@ -0,0 +1,288 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + //DatePicker + + + + + + + + + + + + diff --git a/widgets/src/test/java/mohammadaminha/com/widgets/ExampleUnitTest.java b/widgets/src/test/java/mohammadaminha/com/widgets/ExampleUnitTest.java new file mode 100644 index 0000000..30ef48d --- /dev/null +++ b/widgets/src/test/java/mohammadaminha/com/widgets/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package mohammadaminha.com.widgets; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file