├── settings.gradle
├── samples
├── demo
│ ├── board.gif
│ ├── local.gif
│ ├── global.gif
│ ├── remote.gif
│ └── badge_example.png
├── res
│ ├── drawable-mdpi
│ │ ├── android_gray.png
│ │ └── ic_launcher.png
│ ├── values
│ │ └── strings.xml
│ └── layout
│ │ └── main.xml
├── build.gradle
├── src
│ └── zemin
│ │ └── notification
│ │ └── samples
│ │ ├── MainApplication.java
│ │ └── MainActivity.java
└── AndroidManifest.xml
├── core
├── res
│ ├── raw
│ │ └── fallbackring.ogg
│ ├── drawable-mdpi
│ │ ├── clear_button.png
│ │ └── divider_horizontal_dark.9.png
│ ├── values
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── layout
│ │ ├── notification_text_only.xml
│ │ ├── notification_board_footer.xml
│ │ ├── notification_simple_2.xml
│ │ ├── notification_simple.xml
│ │ ├── notification_large_icon.xml
│ │ ├── notification_full.xml
│ │ └── notification_board_row.xml
├── AndroidManifest.xml
├── src
│ └── zemin
│ │ └── notification
│ │ ├── CallbackNotFoundException.java
│ │ ├── Utils.java
│ │ ├── NotificationListener.java
│ │ ├── AnimationListener.java
│ │ ├── AnimatorListener.java
│ │ ├── AnimationFactory.java
│ │ ├── ViewWrapper.java
│ │ ├── GestureListener.java
│ │ ├── ChildViewManager.java
│ │ ├── NotificationLocal.java
│ │ ├── NotificationRemoteCallback.java
│ │ ├── NotificationViewCallback.java
│ │ ├── NotificationBoardCallback.java
│ │ ├── ViewSwitcherWrapper.java
│ │ ├── NotificationEffect.java
│ │ ├── NotificationRemote.java
│ │ ├── NotificationDelegater.java
│ │ ├── NotificationGlobal.java
│ │ ├── NotificationHandler.java
│ │ └── NotificationRootView.java
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── .gitignore
├── gradlew.bat
├── gradlew
├── README.md
└── LICENSE
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':core'
2 | include ':samples'
3 |
--------------------------------------------------------------------------------
/samples/demo/board.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/samples/demo/board.gif
--------------------------------------------------------------------------------
/samples/demo/local.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/samples/demo/local.gif
--------------------------------------------------------------------------------
/samples/demo/global.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/samples/demo/global.gif
--------------------------------------------------------------------------------
/samples/demo/remote.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/samples/demo/remote.gif
--------------------------------------------------------------------------------
/core/res/raw/fallbackring.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/core/res/raw/fallbackring.ogg
--------------------------------------------------------------------------------
/samples/demo/badge_example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/samples/demo/badge_example.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/core/res/drawable-mdpi/clear_button.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/core/res/drawable-mdpi/clear_button.png
--------------------------------------------------------------------------------
/samples/res/drawable-mdpi/android_gray.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/samples/res/drawable-mdpi/android_gray.png
--------------------------------------------------------------------------------
/samples/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/samples/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/core/res/drawable-mdpi/divider_horizontal_dark.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lamydev/Android-Notification/HEAD/core/res/drawable-mdpi/divider_horizontal_dark.9.png
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | COMPILE_SDK_VERSION=22
2 | BUILD_TOOLS_VERSION=22.0.0
3 | MIN_SDK_VERSION=11
4 | TARGET_SDK_VERSION=22
5 |
6 | PROJECT_VERSION_CODE=3
7 | PROJECT_VERSION_NAME=3.0
8 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=http\://services.gradle.org/distributions/gradle-2.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 | /*/build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
--------------------------------------------------------------------------------
/samples/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | dependencies {
4 | compile project(':core')
5 | compile 'com.android.support:appcompat-v7:22.0.0'
6 | }
7 |
8 | android {
9 | compileSdkVersion 22
10 | buildToolsVersion "22.0.0"
11 |
12 | defaultConfig {
13 | minSdkVersion 11
14 | targetSdkVersion 22
15 | versionCode 1
16 | versionName "1.0.0"
17 | }
18 |
19 | sourceSets {
20 | main {
21 | manifest.srcFile 'AndroidManifest.xml'
22 | java.srcDir 'src'
23 | res.srcDir 'res'
24 | assets.srcDir 'assets'
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/core/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/core/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/CallbackNotFoundException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | /**
20 | * RuntimeException to be thrown when callback not set.
21 | */
22 | public class CallbackNotFoundException extends RuntimeException {
23 |
24 | public CallbackNotFoundException(String msg) { super(msg); }
25 | }
26 |
--------------------------------------------------------------------------------
/core/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'android-library'
2 |
3 | repositories {
4 | mavenCentral()
5 | }
6 |
7 | dependencies {
8 | compile 'com.android.support:appcompat-v7:22.0.0'
9 | }
10 |
11 | project.group = 'com.github.lamydev'
12 |
13 | android {
14 |
15 | compileSdkVersion Integer.parseInt(project.COMPILE_SDK_VERSION)
16 | buildToolsVersion project.BUILD_TOOLS_VERSION
17 |
18 | defaultConfig {
19 | minSdkVersion Integer.parseInt(project.MIN_SDK_VERSION)
20 | targetSdkVersion Integer.parseInt(project.TARGET_SDK_VERSION)
21 | versionCode Integer.parseInt(project.PROJECT_VERSION_CODE)
22 | versionName project.PROJECT_VERSION_NAME
23 | }
24 |
25 | sourceSets {
26 | main {
27 | manifest.srcFile 'AndroidManifest.xml'
28 | java.srcDir 'src'
29 | res.srcDir 'res'
30 | assets.srcDir 'assets'
31 | }
32 | }
33 |
34 | lintOptions {
35 | abortOnError false
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/samples/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
21 | zemin-noti-samples
22 |
23 | remote
24 | local
25 | global
26 | board
27 | arrival
28 | total
29 |
30 | cancelAll
31 | dismiss
32 |
33 | 0
34 |
35 |
--------------------------------------------------------------------------------
/core/res/layout/notification_text_only.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
23 |
24 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/Utils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | /**
20 | * Utils
21 | */
22 | public class Utils {
23 |
24 | /**
25 | * Calculate the alpha value for a position offset.
26 | *
27 | * @param alphaStart
28 | * @param alphaEnd
29 | * @param posStart
30 | * @param posEnd
31 | * @param posOffset
32 | */
33 | public static float getAlphaForOffset(float alphaStart, float alphaEnd,
34 | float posStart, float posEnd, float posOffset) {
35 | return alphaStart + posOffset * (alphaEnd - alphaStart) / (posEnd - posStart);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/core/res/layout/notification_board_footer.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
24 |
25 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/samples/src/zemin/notification/samples/MainApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification.samples;
18 |
19 | import android.app.Application;
20 |
21 | import zemin.notification.NotificationDelegater;
22 |
23 | //
24 | public class MainApplication extends Application {
25 |
26 | @Override
27 | public void onCreate() {
28 | super.onCreate();
29 |
30 | initNotification();
31 | }
32 |
33 | private void initNotification() {
34 |
35 | NotificationDelegater.initialize(
36 | this,
37 | NotificationDelegater.LOCAL |
38 | NotificationDelegater.GLOBAL |
39 | NotificationDelegater.REMOTE);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/samples/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
22 |
23 |
28 |
29 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | /**
20 | * Listener used to propagate events indicating when notifications
21 | * are delivered and/or canceled.
22 | *
23 | * @see NotificationDelegater#addListener
24 | * @see NotificationDelegater#removeListener
25 | */
26 | public interface NotificationListener {
27 |
28 | /**
29 | * Called when a notification arrives.
30 | *
31 | * @param entry
32 | */
33 | void onArrival(NotificationEntry entry);
34 |
35 | /**
36 | * Called when a notification is canceled.
37 | *
38 | * @param entry
39 | */
40 | void onCancel(NotificationEntry entry);
41 |
42 | /**
43 | * Called when a notification is updated.
44 | *
45 | * @param entry
46 | */
47 | void onUpdate(NotificationEntry entry);
48 | }
49 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/AnimationListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.view.View;
20 | import android.view.animation.Animation;
21 |
22 | /**
23 | * A convenience class to extend when you only want to listen for a subset
24 | * of all animation states. This implements all methods in the
25 | * {@link android.view.animation.Animation#AnimationListener}.
26 | */
27 | public class AnimationListener implements Animation.AnimationListener {
28 |
29 | /**
30 | * {@inheritDoc}
31 | */
32 | @Override
33 | public void onAnimationStart(Animation animation) {
34 | }
35 |
36 | /**
37 | * {@inheritDoc}
38 | */
39 | @Override
40 | public void onAnimationEnd(Animation animation) {
41 | }
42 |
43 | /**
44 | * {@inheritDoc}
45 | */
46 | @Override
47 | public void onAnimationRepeat(Animation animation) {
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/AnimatorListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.animation.Animator;
20 | import android.view.View;
21 |
22 | /**
23 | * A convenience class to extend when you only want to listen for a subset
24 | * of all animator states. This implements all methods in the
25 | * {@link android.animation.Animator#AnimatorListener}.
26 | */
27 | public class AnimatorListener implements Animator.AnimatorListener {
28 |
29 | /**
30 | * {@inheritDoc}
31 | */
32 | @Override
33 | public void onAnimationStart(Animator animation) {
34 | }
35 |
36 | /**
37 | * {@inheritDoc}
38 | */
39 | @Override
40 | public void onAnimationEnd(Animator animation) {
41 | }
42 |
43 | /**
44 | * {@inheritDoc}
45 | */
46 | @Override
47 | public void onAnimationRepeat(Animator animation) {
48 | }
49 |
50 | /**
51 | * {@inheritDoc}
52 | */
53 | @Override
54 | public void onAnimationCancel(Animator animator) {
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/core/res/layout/notification_simple_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
30 |
31 |
37 |
38 |
45 |
46 |
53 |
54 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/core/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
21 |
22 |
23 |
24 |
30 |
31 |
37 |
38 |
42 |
43 |
51 |
52 |
59 |
60 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/AnimationFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.view.animation.AlphaAnimation;
20 | import android.view.animation.Animation;
21 | import android.view.animation.AnimationSet;
22 | import android.view.animation.TranslateAnimation;
23 |
24 | /**
25 | * Animation Factory
26 | */
27 | public class AnimationFactory {
28 |
29 | /**
30 | * Create push down animation for entering.
31 | *
32 | * @return Animation
33 | */
34 | public static Animation pushDownIn() {
35 | AnimationSet animationSet = new AnimationSet(true);
36 | animationSet.setFillAfter(true);
37 | animationSet.addAnimation(new TranslateAnimation(0, 0, -100, 0));
38 | animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
39 | return animationSet;
40 | }
41 |
42 | /**
43 | * Create push down animation for leaving.
44 | *
45 | * @return Animation
46 | */
47 | public static Animation pushDownOut() {
48 | AnimationSet animationSet = new AnimationSet(true);
49 | animationSet.setFillAfter(true);
50 | animationSet.addAnimation(new TranslateAnimation(0, 0, 0, 100));
51 | animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
52 | return animationSet;
53 | }
54 |
55 | /**
56 | * Create push up animation for entering.
57 | *
58 | * @return Animation
59 | */
60 | public static Animation pushUpIn() {
61 | AnimationSet animationSet = new AnimationSet(true);
62 | animationSet.setFillAfter(true);
63 | animationSet.addAnimation(new TranslateAnimation(0, 0, 100, 0));
64 | animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
65 | return animationSet;
66 | }
67 |
68 | /**
69 | * Create push up animation for leaving.
70 | *
71 | * @return Animation
72 | */
73 | public static Animation pushUpOut() {
74 | AnimationSet animationSet = new AnimationSet(true);
75 | animationSet.setFillAfter(true);
76 | animationSet.addAnimation(new TranslateAnimation(0, 0, 0, -100));
77 | animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
78 | return animationSet;
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/ViewWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.graphics.drawable.Drawable;
20 | import android.view.View;
21 | import android.widget.ImageView;
22 | import android.widget.TextView;
23 |
24 | /**
25 | *
26 | */
27 | public class ViewWrapper {
28 |
29 | private static final String TAG = "zemin.ViewWrapper";
30 | public static boolean DBG;
31 |
32 | public final String name;
33 | public View view;
34 |
35 | public ViewWrapper(String name) {
36 | this.name = name;
37 | }
38 |
39 | public void clear() {
40 | view = null;
41 | }
42 |
43 | public void reset() {
44 | }
45 |
46 | public boolean hasView() {
47 | return view != null;
48 | }
49 |
50 | public void setView(View view) {
51 | this.view = view;
52 | }
53 |
54 | public View getView() {
55 | return view;
56 | }
57 |
58 | public void show() {
59 | if (view != null) {
60 | view.setVisibility(View.VISIBLE);
61 | }
62 | }
63 |
64 | public void hide() {
65 | if (view != null) {
66 | view.setVisibility(View.INVISIBLE);
67 | }
68 | }
69 |
70 | public void setImageDrawable(Drawable drawable) {
71 | setImageDrawable(drawable, true);
72 | }
73 |
74 | // throws ClassCastException
75 | public void setImageDrawable(Drawable drawable, boolean animate) {
76 | if (view != null) {
77 | ((ImageView) view).setImageDrawable(drawable);
78 | }
79 | }
80 |
81 | public void setText(CharSequence text) {
82 | setText(text, true);
83 | }
84 |
85 | // throws ClassCastException
86 | public void setText(CharSequence text, boolean animate) {
87 | if (view != null) {
88 | ((TextView) view).setText(text);
89 | }
90 | }
91 |
92 | public void setTextSize(int size) {
93 | if (view != null) {
94 | ((TextView) view).setTextSize(size);
95 | }
96 | }
97 |
98 | public void setTextColor(int color) {
99 | if (view != null) {
100 | ((TextView) view).setTextColor(color);
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/GestureListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.view.GestureDetector;
20 | import android.view.MotionEvent;
21 |
22 | /**
23 | * A convenience class to extend when you only want to listen for a subset
24 | * of all gesture states. This implements all methods in the
25 | * {@link android.view.GestureDetector#OnGestureListener} and
26 | * {@link android.view.GestureDetector#OnDoubleTapListener}.
27 | *
28 | * Moreover, new APIs are also introduced:
29 | *
30 | * 1) void onUpOrCancel(MotionEvent event, boolean handled);
31 | */
32 | public class GestureListener implements GestureDetector.OnGestureListener,
33 | GestureDetector.OnDoubleTapListener {
34 |
35 | /**
36 | * Called when {@link MotionEvent#ACTION_UP} or {@link MotionEvent#ACTION_CANCEL} event occurs.
37 | *
38 | * @param event
39 | * @param handled
40 | */
41 | public void onUpOrCancel(MotionEvent event, boolean handled) {
42 | }
43 |
44 | /**
45 | * {@inheritDoc}
46 | */
47 | @Override
48 | public boolean onDown(MotionEvent event) {
49 | return false;
50 | }
51 |
52 | /**
53 | * {@inheritDoc}
54 | */
55 | @Override
56 | public void onShowPress(MotionEvent event) {
57 | }
58 |
59 | /**
60 | * {@inheritDoc}
61 | */
62 | @Override
63 | public boolean onSingleTapUp(MotionEvent event) {
64 | return false;
65 | }
66 |
67 | /**
68 | * {@inheritDoc}
69 | */
70 | @Override
71 | public boolean onSingleTapConfirmed(MotionEvent event) {
72 | return false;
73 | }
74 |
75 | /**
76 | * {@inheritDoc}
77 | */
78 | @Override
79 | public boolean onDoubleTap(MotionEvent event) {
80 | return false;
81 | }
82 |
83 | /**
84 | * {@inheritDoc}
85 | */
86 | @Override
87 | public boolean onDoubleTapEvent(MotionEvent event) {
88 | return false;
89 | }
90 |
91 | /**
92 | * {@inheritDoc}
93 | */
94 | @Override
95 | public void onLongPress(MotionEvent event) {
96 | }
97 |
98 | /**
99 | * {@inheritDoc}
100 | */
101 | @Override
102 | public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) {
103 | return false;
104 | }
105 |
106 | /**
107 | * {@inheritDoc}
108 | */
109 | @Override
110 | public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
111 | return false;
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/core/res/layout/notification_simple.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
30 |
31 |
37 |
41 |
45 |
46 |
47 |
53 |
57 |
61 |
62 |
63 |
69 |
73 |
77 |
78 |
79 |
86 |
90 |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/core/res/layout/notification_large_icon.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
28 |
29 |
39 |
45 |
50 |
54 |
58 |
59 |
60 |
65 |
69 |
73 |
74 |
75 |
76 |
80 |
84 |
88 |
89 |
90 |
91 |
99 |
103 |
107 |
108 |
109 |
--------------------------------------------------------------------------------
/core/res/layout/notification_full.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
29 |
30 |
38 |
43 |
47 |
51 |
52 |
53 |
59 |
63 |
67 |
68 |
69 |
75 |
79 |
83 |
84 |
85 |
86 |
96 |
100 |
105 |
110 |
111 |
112 |
113 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/ChildViewManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.graphics.drawable.Drawable;
20 | import android.view.View;
21 | import android.widget.ViewSwitcher;
22 |
23 | import android.support.v4.util.ArrayMap;
24 |
25 | import java.util.Collection;
26 |
27 | /**
28 | *
29 | */
30 | public class ChildViewManager {
31 |
32 | private static final String TAG = "zemin.ChildViewManager";
33 | public static boolean DBG;
34 |
35 | public void setView(String name, View view) {
36 | Holder holder = mHolders.get(name);
37 | if (holder == null && name != null) {
38 | holder = new Holder(name);
39 | mHolders.put(name, holder);
40 | }
41 | holder.set(view);
42 | }
43 |
44 | public ViewWrapper getViewWrapper(String name) {
45 | Holder holder = mHolders.get(name);
46 | return holder != null ? holder.curr() : null;
47 | }
48 |
49 | public void show(String name) {
50 | getViewWrapper(name).show();
51 | }
52 |
53 | public void hide(String name) {
54 | getViewWrapper(name).hide();
55 | }
56 |
57 | public void setImageDrawable(String name, Drawable drawable) {
58 | setImageDrawable(name, drawable, true);
59 | }
60 |
61 | public void setImageDrawable(String name, Drawable drawable, boolean animate) {
62 | ViewWrapper v = getViewWrapper(name);
63 | if (drawable != null) {
64 | v.show();
65 | v.setImageDrawable(drawable, animate);
66 | } else {
67 | v.hide();
68 | }
69 | }
70 |
71 | public void setText(String name, CharSequence text) {
72 | setText(name, text, true);
73 | }
74 |
75 | public void setText(String name, CharSequence text, boolean animate) {
76 | ViewWrapper v = getViewWrapper(name);
77 | if (text != null) {
78 | v.show();
79 | v.setText(text, animate);
80 | } else {
81 | v.hide();
82 | }
83 | }
84 |
85 | public void setTextSize(String name, int size) {
86 | getViewWrapper(name).setTextSize(size);
87 | }
88 |
89 | public void setTextColor(String name, int color) {
90 | getViewWrapper(name).setTextColor(color);
91 | }
92 |
93 | public void clear() {
94 | Collection holders = mHolders.values();
95 | for (Holder h : holders) {
96 | h.clear();
97 | }
98 | }
99 |
100 | public void reset() {
101 | Collection holders = mHolders.values();
102 | for (Holder h : holders) {
103 | h.reset();
104 | }
105 | }
106 |
107 | private final ArrayMap mHolders =
108 | new ArrayMap();
109 |
110 | private class Holder {
111 | final String name;
112 | ViewWrapper view;
113 | ViewSwitcherWrapper switcher;
114 | Holder(String name) { this.name = name; }
115 |
116 | ViewWrapper curr() {
117 | if (switcher != null && switcher.hasView()) {
118 | return switcher;
119 | } else if (view != null && view.hasView()) {
120 | return view;
121 | }
122 | return null;
123 | }
124 |
125 | void set(View v) {
126 | if (v instanceof ViewSwitcher) {
127 | if (switcher == null) {
128 | switcher = new ViewSwitcherWrapper(name);
129 | }
130 | switcher.setView(v);
131 | } else {
132 | if (view == null) {
133 | view = new ViewWrapper(name);
134 | }
135 | view.setView(v);
136 | }
137 | }
138 |
139 | void clear() {
140 | ViewWrapper curr = curr();
141 | if (curr != null) curr.clear();
142 | }
143 |
144 | void reset() {
145 | ViewWrapper curr = curr();
146 | if (curr != null) curr.reset();
147 | }
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationLocal.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.content.Context;
20 | import android.os.Looper;
21 | import android.util.Log;
22 | import android.view.View;
23 |
24 | import java.util.ArrayList;
25 |
26 | /**
27 | * In-layout notification view.
28 | */
29 | public class NotificationLocal extends NotificationHandler {
30 |
31 | public static final String SIMPLE_NAME = "Local";
32 | public static boolean DBG;
33 |
34 | /**
35 | * Implementaiton of {@link NotificationViewCallback} for in-layout {@link NotificationView}.
36 | */
37 | public static class ViewCallback extends NotificationViewCallback {
38 |
39 | @Override
40 | public int getContentViewDefaultLayoutId(NotificationView view) {
41 | return R.layout.notification_simple_2;
42 | }
43 |
44 | @Override
45 | public void onViewSetup(NotificationView view) {
46 | // nothing.
47 | }
48 | }
49 |
50 | private NotificationView mView;
51 |
52 | /* package */ NotificationLocal(Context context, Looper looper) {
53 | super(context, NotificationDelegater.LOCAL, looper);
54 | }
55 |
56 | /**
57 | * Set notification view.
58 | *
59 | * @param view
60 | */
61 | public void setView(NotificationView view) {
62 | view.initialize(this);
63 | mView = view;
64 | }
65 |
66 | /**
67 | * Set callback for notification view.
68 | *
69 | * @param cb
70 | */
71 | public void setViewCallback(NotificationViewCallback cb) {
72 | if (mView != null) {
73 | mView.setCallback(cb);
74 | }
75 | }
76 |
77 | /**
78 | * Enable/disable notification view.
79 | *
80 | * @param enable
81 | */
82 | public void setViewEnabled(boolean enable) {
83 | if (mView != null) {
84 | mView.setViewEnabled(enable);
85 | }
86 | }
87 |
88 | /**
89 | * Get notification view.
90 | *
91 | * @param NotificationView
92 | */
93 | public NotificationView getView() {
94 | return mView;
95 | }
96 |
97 | /**
98 | * Dismiss notification view.
99 | */
100 | public void dismissView() {
101 | if (mView != null) {
102 | mView.dismiss();
103 | }
104 | }
105 |
106 | @Override
107 | protected void onCancel(NotificationEntry entry) {
108 | if (mView != null) {
109 | mView.onCancel(entry);
110 | } else {
111 | onCancelFinished(entry);
112 | }
113 | }
114 |
115 | @Override
116 | protected void onCancelAll() {
117 | if (mView != null) {
118 | mView.onCancelAll();
119 | } else {
120 | onCancelAllFinished();
121 | }
122 | }
123 |
124 | @Override
125 | protected void onArrival(NotificationEntry entry) {
126 | if (mView == null) {
127 | Log.w(TAG, "NotificationView not found.");
128 | onSendIgnored(entry);
129 | return;
130 | }
131 |
132 | if (!mView.hasCallback()) {
133 | if (DBG) Log.v(TAG, "set default NotificationViewCallback.");
134 | mView.setCallback(new ViewCallback());
135 | }
136 |
137 | if (!mView.isViewEnabled()) {
138 | if (DBG) Log.v(TAG, "NotificationView is currently disabled.");
139 | onSendIgnored(entry);
140 | return;
141 | }
142 |
143 | mView.onArrival(entry);
144 | }
145 |
146 |
147 | @Override
148 | protected void onUpdate(NotificationEntry entry) {
149 | if (mView == null) {
150 | Log.w(TAG, "NotificationView not found.");
151 | onUpdateIgnored(entry);
152 | return;
153 | }
154 |
155 | if (!mView.isViewEnabled()) {
156 | if (DBG) Log.v(TAG, "NotificationView is currently disabled.");
157 | onUpdateIgnored(entry);
158 | return;
159 | }
160 |
161 | mView.onUpdate(entry);
162 | }
163 |
164 | @Override public String toSimpleString() { return SIMPLE_NAME; }
165 | @Override public String toString() { return SIMPLE_NAME; }
166 | }
167 |
--------------------------------------------------------------------------------
/core/res/layout/notification_board_row.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
25 |
26 |
36 |
42 |
50 |
58 |
59 |
60 |
67 |
68 |
78 |
79 |
85 |
86 |
90 |
91 |
100 |
101 |
110 |
111 |
120 |
121 |
122 |
123 |
124 |
131 |
132 |
133 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationRemoteCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.app.Notification;
20 | import android.app.PendingIntent;
21 | import android.content.Context;
22 | import android.content.Intent;
23 | import android.os.Bundle;
24 | import android.util.Log;
25 | import android.support.v4.app.NotificationCompat;
26 |
27 | import java.util.ArrayList;
28 |
29 | /**
30 | * Callback for {@link NotificationRemote}. This is the default implementation.
31 | */
32 | public class NotificationRemoteCallback {
33 |
34 | private static final String TAG = "zemin.NotificationRemoteCallback";
35 | public static boolean DBG;
36 |
37 | /**
38 | * Called only once after this callback is set.
39 | *
40 | * @param remote
41 | */
42 | public void onRemoteSetup(NotificationRemote remote) {
43 | if (DBG) Log.v(TAG, "onRemoteSetup");
44 | }
45 |
46 | /**
47 | * Create a new {@link android.app.Notification} object.
48 | *
49 | * @param remote
50 | * @param entry
51 | * @param layoutId
52 | * @return Notification
53 | */
54 | public Notification makeStatusBarNotification(NotificationRemote remote, NotificationEntry entry, int layoutId) {
55 | if (DBG) Log.v(TAG, "makeStatusBarNotification - " + entry.ID);
56 |
57 | final int entryId = entry.ID;
58 | final CharSequence title = entry.title;
59 | final CharSequence text = entry.text;
60 | CharSequence tickerText = entry.tickerText;
61 |
62 | NotificationCompat.Builder builder = remote.getStatusBarNotificationBuilder();
63 | if (entry.smallIconRes > 0) {
64 | builder.setSmallIcon(entry.smallIconRes);
65 | } else {
66 | Log.w(TAG, "***************** small icon not set.");
67 | }
68 |
69 | if (tickerText == null) {
70 | Log.w(TAG, "***************** tickerText not set.");
71 | tickerText = title + ": " + text;
72 | }
73 |
74 | if (entry.largeIconBitmap != null) {
75 | builder.setLargeIcon(entry.largeIconBitmap);
76 | }
77 |
78 | builder.setTicker(tickerText);
79 | builder.setContentTitle(title);
80 | builder.setContentText(text);
81 | builder.setShowWhen(entry.showWhen);
82 |
83 | if (entry.showWhen && entry.whenLong > 0) {
84 | builder.setWhen(entry.whenLong);
85 | }
86 |
87 | if (entry.progressMax != 0 || entry.progressIndeterminate) {
88 | builder.setProgress(entry.progressMax, entry.progress, entry.progressIndeterminate);
89 | }
90 |
91 | builder.setAutoCancel(entry.autoCancel);
92 | builder.setOngoing(entry.ongoing);
93 | builder.setDeleteIntent(remote.getDeleteIntent(entry));
94 | builder.setContentIntent(remote.getContentIntent(entry));
95 |
96 | if (entry.hasActions()) {
97 | ArrayList actions = entry.getActions();
98 | for (NotificationEntry.Action act : actions) {
99 | builder.addAction(act.icon, act.title, remote.getActionIntent(entry, act));
100 | }
101 | }
102 |
103 | if (entry.useSystemEffect) {
104 | int defaults = 0;
105 | if (entry.playRingtone) {
106 | if (entry.ringtoneUri != null) {
107 | builder.setSound(entry.ringtoneUri);
108 | } else {
109 | defaults |= Notification.DEFAULT_SOUND;
110 | }
111 | }
112 |
113 | if (entry.useVibration) {
114 | if (entry.vibratePattern != null) {
115 | builder.setVibrate(entry.vibratePattern);
116 | } else {
117 | defaults |= Notification.DEFAULT_VIBRATE;
118 | }
119 | }
120 |
121 | if (defaults != 0) {
122 | builder.setDefaults(defaults);
123 | }
124 | }
125 |
126 | return builder.build();
127 | }
128 |
129 | /**
130 | * Called when notification is clicked.
131 | *
132 | * @param remote
133 | * @param entry
134 | */
135 | public void onClickRemote(NotificationRemote remote, NotificationEntry entry) {
136 | if (DBG) Log.v(TAG, "onClickRemote - " + entry.ID);
137 | }
138 |
139 | /**
140 | * Called when notification is canceled.
141 | *
142 | * @param remote
143 | * @param entry
144 | */
145 | public void onCancelRemote(NotificationRemote remote, NotificationEntry entry) {
146 | if (DBG) Log.v(TAG, "onCancelRemote - " + entry.ID);
147 | }
148 |
149 | /**
150 | * Called when notification action view is clicked.
151 | *
152 | * @param remote
153 | * @param entry
154 | * @param act
155 | */
156 | public void onClickRemoteAction(NotificationRemote remote, NotificationEntry entry,
157 | NotificationEntry.Action act) {
158 | if (DBG) Log.v(TAG, "onClickRemoteAction - " + entry.ID + ", " + act);
159 | }
160 |
161 | /**
162 | * Called when receiving a broadcast.
163 | *
164 | * @param remote
165 | * @param entry
166 | * @param intent
167 | * @param intentAction
168 | */
169 | public void onReceive(NotificationRemote remote, NotificationEntry entry,
170 | Intent intent, String intentAction) {
171 | if (DBG) Log.d(TAG, "onReceive - " + entry.ID + ", " + intentAction);
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationViewCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.graphics.drawable.Drawable;
20 | import android.util.Log;
21 | import android.view.View;
22 | import android.view.ViewGroup;
23 |
24 | import java.util.Collection;
25 |
26 | /**
27 | * Callback for {@link NotificationView}. This is the default implementation.
28 | */
29 | public class NotificationViewCallback {
30 |
31 | private static final String TAG = "zemin.NotificationViewCallback";
32 | public static boolean DBG;
33 |
34 | public static final String ICON = "icon";
35 | public static final String TITLE = "title";
36 | public static final String TEXT = "text";
37 | public static final String WHEN = "when";
38 | // public static final int PROGRESS = 4;
39 | // add more..
40 |
41 | /**
42 | * Called only once after this callback is set.
43 | *
44 | * @param view
45 | */
46 | public void onViewSetup(NotificationView view) {
47 | if (DBG) Log.v(TAG, "onViewSetup");
48 |
49 | view.setCornerRadius(8.0f);
50 | view.setContentMargin(50, 50, 50, 50);
51 | view.setShadowEnabled(true);
52 | }
53 |
54 | /**
55 | * Called to get the default layoutId.
56 | *
57 | * @param view
58 | * @return int
59 | */
60 | public int getContentViewDefaultLayoutId(NotificationView view) {
61 | return R.layout.notification_full;
62 | }
63 |
64 | /**
65 | * Called when content view is changed. All child-views were cleared due the
66 | * change of content view. You need to re-setup the associated child-views.
67 | *
68 | * @param view
69 | * @param contentView
70 | * @param layoutId
71 | */
72 | public void onContentViewChanged(NotificationView view, View contentView, int layoutId) {
73 | if (DBG) Log.v(TAG, "onContentViewChanged");
74 |
75 | ChildViewManager mgr = view.getChildViewManager();
76 |
77 | if (layoutId == R.layout.notification_simple ||
78 | layoutId == R.layout.notification_large_icon ||
79 | layoutId == R.layout.notification_full) {
80 |
81 | view.setNotificationTransitionEnabled(false);
82 |
83 | mgr.setView(ICON, contentView.findViewById(R.id.switcher_icon));
84 | mgr.setView(TITLE, contentView.findViewById(R.id.switcher_title));
85 | mgr.setView(TEXT, contentView.findViewById(R.id.switcher_text));
86 | mgr.setView(WHEN, contentView.findViewById(R.id.switcher_when));
87 |
88 | } else if (layoutId == R.layout.notification_simple_2) {
89 |
90 | view.setNotificationTransitionEnabled(true);
91 |
92 | mgr.setView(ICON, contentView.findViewById(R.id.icon));
93 | mgr.setView(TITLE, contentView.findViewById(R.id.title));
94 | mgr.setView(TEXT, contentView.findViewById(R.id.text));
95 | mgr.setView(WHEN, contentView.findViewById(R.id.when));
96 | }
97 | }
98 |
99 | /**
100 | * Called when a notification is being displayed. This is the place to update
101 | * the user interface of child-views for the new notification.
102 | *
103 | * @param view
104 | * @param contentView
105 | * @param entry
106 | * @param layoutId
107 | */
108 | public void onShowNotification(NotificationView view, View contentView, NotificationEntry entry, int layoutId) {
109 | if (DBG) Log.v(TAG, "onShowNotification - " + entry.ID);
110 |
111 | final Drawable icon = entry.iconDrawable;
112 | final CharSequence title = entry.title;
113 | final CharSequence text = entry.text;
114 | final CharSequence when = entry.showWhen ? entry.whenFormatted : null;
115 |
116 | ChildViewManager mgr = view.getChildViewManager();
117 |
118 | if (layoutId == R.layout.notification_simple ||
119 | layoutId == R.layout.notification_large_icon ||
120 | layoutId == R.layout.notification_full) {
121 |
122 | boolean titleChanged = true;
123 | boolean contentChanged = view.isContentLayoutChanged();
124 | NotificationEntry lastEntry = view.getLastNotification();
125 |
126 | if (!contentChanged && title != null &&
127 | lastEntry != null && title.equals(lastEntry.title)) {
128 | titleChanged = false;
129 | }
130 |
131 | mgr.setImageDrawable(ICON, icon, titleChanged);
132 | mgr.setText(TITLE, title, titleChanged);
133 | mgr.setText(TEXT, text);
134 | mgr.setText(WHEN, when);
135 |
136 | } else if (layoutId == R.layout.notification_simple_2) {
137 |
138 | mgr.setImageDrawable(ICON, icon);
139 | mgr.setText(TITLE, title);
140 | mgr.setText(TEXT, text);
141 | mgr.setText(WHEN, when);
142 | }
143 | }
144 |
145 | /**
146 | * Called when a notification is being updated.
147 | *
148 | * @param view
149 | * @param contentView
150 | * @param entry
151 | * @param layoutId
152 | */
153 | public void onUpdateNotification(NotificationView view, View contentView, NotificationEntry entry, int layoutId) {
154 | if (DBG) Log.v(TAG, "onUpdateNotification - " + entry.ID);
155 |
156 | final Drawable icon = entry.iconDrawable;
157 | final CharSequence title = entry.title;
158 | final CharSequence text = entry.text;
159 | final CharSequence when = entry.showWhen ? entry.whenFormatted : null;
160 |
161 | ChildViewManager mgr = view.getChildViewManager();
162 |
163 | mgr.setImageDrawable(ICON, icon, false);
164 | mgr.setText(TITLE, title, false);
165 | mgr.setText(TEXT, text, false);
166 | mgr.setText(WHEN, when, false);
167 | }
168 |
169 | /**
170 | * Called when the view has been clicked.
171 | *
172 | * @param view
173 | * @param contentView
174 | * @param entry
175 | * @return boolean true, if handled.
176 | */
177 | public void onClickContentView(NotificationView view, View contentView, NotificationEntry entry) {
178 | if (DBG) Log.v(TAG, "onClickContentView - " + entry.ID);
179 | }
180 | }
181 |
--------------------------------------------------------------------------------
/samples/res/layout/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
24 |
25 |
32 |
37 |
40 |
45 |
52 |
59 |
60 |
61 |
65 |
70 |
77 |
84 |
91 |
92 |
93 |
97 |
102 |
109 |
116 |
123 |
124 |
125 |
129 |
134 |
140 |
147 |
148 |
149 |
150 |
154 |
161 |
162 |
163 |
164 |
173 |
174 |
175 |
176 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationBoardCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.util.Log;
20 | import android.view.LayoutInflater;
21 | import android.view.View;
22 | import android.view.ViewGroup;
23 | import android.widget.Button;
24 | import android.widget.ImageView;
25 | import android.widget.ProgressBar;
26 | import android.widget.TextView;
27 |
28 | import java.util.ArrayList;
29 |
30 | import zemin.notification.NotificationBoard.RowView;
31 | import zemin.notification.NotificationEntry.Action;
32 |
33 | /**
34 | * Callback for {@link NotificationBoard}. This is the default implementation.
35 | */
36 | public class NotificationBoardCallback {
37 |
38 | private static final String TAG = "zemin.NotificationBoardCallback";
39 | public static boolean DBG;
40 |
41 | /**
42 | * Called only once after this callback is set.
43 | *
44 | * @param board
45 | */
46 | public void onBoardSetup(NotificationBoard board) {
47 | if (DBG) Log.v(TAG, "onBoardSetup");
48 |
49 | LayoutInflater inflater = board.getInflater();
50 | View footer = inflater.inflate(R.layout.notification_board_footer, null, false);
51 | board.addFooterView(footer);
52 | board.setFooterHeight(200);
53 |
54 | // board.setHeaderHeight(150);
55 | board.setHeaderMargin(0, 150, 0, 0);
56 |
57 | board.setHeaderDivider(R.drawable.divider_horizontal_dark);
58 | board.setFooterDivider(R.drawable.divider_horizontal_dark);
59 | board.setRowDivider(R.drawable.divider_horizontal_dark);
60 |
61 | board.setBoardPadding(80, 0, 80, 0);
62 | board.setRowMargin(20, 0, 20, 0);
63 |
64 | board.setClearView(footer.findViewById(R.id.clear));
65 |
66 | board.addStateListener(new NotificationBoard.SimpleStateListener() {
67 |
68 | @Override
69 | public void onBoardTranslationY(NotificationBoard board, float y) {
70 | final float t = board.getBoardTranslationY();
71 | if (t == 0.0f) {
72 | board.showClearView(false);
73 | } else if (t == -board.getBoardHeight()) {
74 | board.showClearView(false);
75 | } else if (y == 0.0f) {
76 | board.showClearView(board.getNotificationCount() > 0);
77 | }
78 | }
79 |
80 | @Override
81 | public void onBoardEndOpen(NotificationBoard board) {
82 | board.showClearView(board.getNotificationCount() > 0);
83 | }
84 |
85 | @Override
86 | public void onBoardStartClose(NotificationBoard board) {
87 | board.showClearView(false);
88 | }
89 | });
90 | }
91 |
92 | /**
93 | * Called to instantiate a view being placed in the row view,
94 | * which is the user interface for the incoming notification.
95 | *
96 | * @param board
97 | * @param entry
98 | * @param inflater
99 | * @return View
100 | */
101 | public View makeRowView(NotificationBoard board, NotificationEntry entry, LayoutInflater inflater) {
102 |
103 | return inflater.inflate(R.layout.notification_board_row, null, false);
104 | }
105 |
106 | /**
107 | * Called when a row view is added to the board.
108 | *
109 | * @param board
110 | * @param rowView
111 | * @param entry
112 | */
113 | public void onRowViewAdded(NotificationBoard board, RowView rowView, NotificationEntry entry) {
114 | if (DBG) Log.v(TAG, "onRowViewAdded - " + entry.ID);
115 |
116 | if (entry.hasActions()) {
117 | ArrayList actions = entry.getActions();
118 | ViewGroup vg = (ViewGroup) rowView.findViewById(R.id.actions);
119 | vg.setVisibility(View.VISIBLE);
120 | vg = (ViewGroup) vg.getChildAt(0);
121 |
122 | final View.OnClickListener onClickListener = new View.OnClickListener() {
123 |
124 | @Override
125 | public void onClick(View view) {
126 | Action act = (Action) view.getTag();
127 | onClickActionView(act.entry, act, view);
128 | act.execute(view.getContext());
129 | }
130 | };
131 |
132 | for (int i = 0, count = actions.size(); i < count; i++) {
133 | Action act = actions.get(i);
134 | Button actionBtn = (Button) vg.getChildAt(i);
135 | actionBtn.setVisibility(View.VISIBLE);
136 | actionBtn.setTag(act);
137 | actionBtn.setText(act.title);
138 | actionBtn.setOnClickListener(onClickListener);
139 | actionBtn.setCompoundDrawablesWithIntrinsicBounds(act.icon, 0, 0, 0);
140 | }
141 | }
142 | }
143 |
144 | /**
145 | * Called when a row view is removed from the board.
146 | *
147 | * @param board
148 | * @param rowView
149 | * @param entry
150 | */
151 | public void onRowViewRemoved(NotificationBoard board, RowView rowView, NotificationEntry entry) {
152 | if (DBG) Log.v(TAG, "onRowViewRemoved - " + entry.ID);
153 | }
154 |
155 | /**
156 | * Called when a row view is being updated.
157 | *
158 | * @param board
159 | * @param rowView
160 | * @param entry
161 | */
162 | public void onRowViewUpdate(NotificationBoard board, RowView rowView, NotificationEntry entry) {
163 | if (DBG) Log.v(TAG, "onRowViewUpdate - " + entry.ID);
164 |
165 | ImageView iconView = (ImageView) rowView.findViewById(R.id.icon);
166 | TextView titleView = (TextView) rowView.findViewById(R.id.title);
167 | TextView textView = (TextView) rowView.findViewById(R.id.text);
168 | TextView whenView = (TextView) rowView.findViewById(R.id.when);
169 | ProgressBar bar = (ProgressBar) rowView.findViewById(R.id.progress);
170 |
171 | if (entry.iconDrawable != null) {
172 | iconView.setImageDrawable(entry.iconDrawable);
173 | } else if (entry.smallIconRes != 0) {
174 | iconView.setImageResource(entry.smallIconRes);
175 | } else if (entry.largeIconBitmap != null) {
176 | iconView.setImageBitmap(entry.largeIconBitmap);
177 | }
178 |
179 | titleView.setText(entry.title);
180 | textView.setText(entry.text);
181 |
182 | if (entry.showWhen) {
183 | whenView.setText(entry.whenFormatted);
184 | }
185 |
186 | if (entry.progressMax != 0 || entry.progressIndeterminate) {
187 | bar.setVisibility(View.VISIBLE);
188 | bar.setIndeterminate(entry.progressIndeterminate);
189 | if (!entry.progressIndeterminate) {
190 | bar.setMax(entry.progressMax);
191 | bar.setProgress(entry.progress);
192 | }
193 | } else {
194 | bar.setVisibility(View.GONE);
195 | }
196 |
197 | }
198 |
199 | /**
200 | * Called when a row view has been clicked.
201 | *
202 | * @param board
203 | * @param rowView
204 | * @param entry
205 | */
206 | public void onClickRowView(NotificationBoard board, RowView rowView, NotificationEntry entry) {
207 | if (DBG) Log.v(TAG, "onClickRowView - " + entry.ID);
208 | }
209 |
210 | /**
211 | * Called when the clear view has been clicked.
212 | *
213 | * @param board
214 | * @param clearView
215 | */
216 | public void onClickClearView(NotificationBoard board, View clearView) {
217 | if (DBG) Log.v(TAG, "onClickClearView");
218 | }
219 |
220 | public void onClickActionView(NotificationEntry entry, Action act, View actionView) {
221 | if (DBG) Log.v(TAG, "onClickActionView - " + entry.ID + ", " + act);
222 | }
223 | }
224 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/ViewSwitcherWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.graphics.drawable.Drawable;
20 | import android.os.Handler;
21 | import android.os.Looper;
22 | import android.os.Message;
23 | import android.view.View;
24 | import android.view.animation.Animation;
25 | import android.widget.ImageSwitcher;
26 | import android.widget.ImageView;
27 | import android.widget.TextSwitcher;
28 | import android.widget.TextView;
29 | import android.widget.ViewSwitcher;
30 |
31 | import java.lang.ref.WeakReference;
32 |
33 | /**
34 | *
35 | */
36 | public class ViewSwitcherWrapper extends ViewWrapper {
37 |
38 | private static final String TAG = "zemin.ViewSwitcherWrapper";
39 | public static boolean DBG;
40 |
41 | public static final int TRANSITION_TIME = 700;
42 |
43 | private Animation mInAnimation;
44 | private Animation mOutAnimation;
45 | private int mInDuration = TRANSITION_TIME;
46 | private int mOutDuration = TRANSITION_TIME;
47 |
48 | public ViewSwitcherWrapper(String name) {
49 | super(name);
50 | }
51 |
52 | public Animation getInAnimation() {
53 | return mInAnimation;
54 | }
55 |
56 | public Animation getOutAnimation() {
57 | return mOutAnimation;
58 | }
59 |
60 | public int getInDuration() {
61 | return mInDuration;
62 | }
63 |
64 | public int getOutDuration() {
65 | return mOutDuration;
66 | }
67 |
68 | public void setInAnimation(Animation animation, int duration) {
69 | mInAnimation = animation;
70 | mInDuration = duration;
71 | updateAnimation();
72 | }
73 |
74 | public void setOutAnimation(Animation animation, int duration) {
75 | mOutAnimation = animation;
76 | mOutDuration = duration;
77 | updateAnimation();
78 | }
79 |
80 | @Override
81 | public void reset() {
82 | super.reset();
83 | View view = getView();
84 | if (view == null) {
85 | return;
86 | }
87 |
88 | ViewSwitcher viewSwitcher = (ViewSwitcher) view;
89 | viewSwitcher.setAnimateFirstView(false);
90 | viewSwitcher.reset();
91 | }
92 |
93 | @Override
94 | public void setView(View view) {
95 | super.setView(view);
96 | updateAnimation();
97 | }
98 |
99 | @Override
100 | public void setImageDrawable(Drawable drawable, boolean animate) {
101 | View view = getView();
102 | if (view == null) {
103 | return;
104 | }
105 |
106 | ImageSwitcher imageSwitcher = (ImageSwitcher) view;
107 | if (animate) {
108 | imageSwitcher.setImageDrawable(drawable);
109 | } else {
110 | ImageView curr = (ImageView) imageSwitcher.getCurrentView();
111 | curr.setImageDrawable(drawable);
112 | }
113 | }
114 |
115 | @Override
116 | public void setText(CharSequence text, boolean animate) {
117 | View view = getView();
118 | if (view == null) {
119 | return;
120 | }
121 |
122 | TextSwitcher textSwitcher = (TextSwitcher) view;
123 | if (animate) {
124 | textSwitcher.setText(text);
125 | } else {
126 | TextView curr = (TextView) textSwitcher.getCurrentView();
127 | curr.setText(text);
128 | }
129 |
130 | //
131 | // waiting for the first layout of SWITCHER to be finished,
132 | // so that we can adjust its size according to its content.
133 | //
134 | // 100 ms
135 | //
136 | schedule(MSG_TEXT_VIEW_ADJUST_HEIGHT, 100);
137 | }
138 |
139 | @Override
140 | public void setTextSize(int size) {
141 | View view = getView();
142 | if (view == null) {
143 | return;
144 | }
145 |
146 | TextSwitcher textSwitcher = (TextSwitcher) view;
147 | for (int i = 0; i < 2; i++) {
148 | TextView titleView = (TextView) textSwitcher.getChildAt(i);
149 | titleView.setTextSize(size);
150 | }
151 | }
152 |
153 | @Override
154 | public void setTextColor(int color) {
155 | View view = getView();
156 | if (view == null) {
157 | return;
158 | }
159 |
160 | TextSwitcher textSwitcher = (TextSwitcher) view;
161 | for (int i = 0; i < 2; i++) {
162 | TextView titleView = (TextView) textSwitcher.getChildAt(i);
163 | titleView.setTextColor(color);
164 | }
165 | }
166 |
167 | private void updateAnimation() {
168 | View view = getView();
169 | if (view == null) {
170 | return;
171 | }
172 |
173 | ViewSwitcher viewSwitcher = (ViewSwitcher) view;
174 | if (viewSwitcher.getInAnimation() != mInAnimation || mInAnimation == null) {
175 | if (mInAnimation == null) {
176 | mInAnimation = AnimationFactory.pushDownIn();
177 | }
178 | if (viewSwitcher instanceof TextSwitcher) {
179 | mInAnimation.setAnimationListener(mTextViewInAnimationListener);
180 | }
181 | mInAnimation.setDuration(mInDuration);
182 | viewSwitcher.setInAnimation(mInAnimation);
183 | }
184 | if (viewSwitcher.getOutAnimation() != mOutAnimation || mOutAnimation == null) {
185 | if (mOutAnimation == null) {
186 | mOutAnimation = AnimationFactory.pushDownOut();
187 | }
188 | mOutAnimation.setDuration(mOutDuration);
189 | viewSwitcher.setOutAnimation(mOutAnimation);
190 | }
191 | }
192 |
193 | // TODO: a better way to wrap content of TextView.
194 | private void adjustTextViewHeight() {
195 | View view = getView();
196 | if (view == null) {
197 | return;
198 | }
199 |
200 | TextSwitcher textSwitcher = (TextSwitcher) view;
201 | TextView curr = (TextView) textSwitcher.getCurrentView();
202 | TextView next = (TextView) textSwitcher.getNextView();
203 | int currH = curr.getLineCount() * curr.getLineHeight();
204 | int nextH = next.getLineCount() * next.getLineHeight();
205 | if (currH != nextH) {
206 | curr.setHeight(currH);
207 | next.setHeight(currH);
208 | }
209 | }
210 |
211 | private final AnimationListener mTextViewInAnimationListener = new AnimationListener() {
212 |
213 | @Override
214 | public void onAnimationStart(Animation animation) {
215 | adjustTextViewHeight();
216 | }
217 | };
218 |
219 | // ------------------------------------------------
220 | // handler
221 |
222 | private static final int MSG_TEXT_VIEW_ADJUST_HEIGHT = 0;
223 |
224 | protected void handleMessage(Message msg) {
225 | switch (msg.what) {
226 | case MSG_TEXT_VIEW_ADJUST_HEIGHT:
227 | adjustTextViewHeight();
228 | break;
229 | }
230 | }
231 |
232 | private H mH;
233 |
234 | private void cancel(int what) {
235 | final H h = getHandler();
236 | if (what == -1)
237 | h.removeCallbacksAndMessages(null);
238 | else
239 | h.removeMessages(what);
240 | }
241 |
242 | private void schedule(int what, int delay) {
243 | final H h = getHandler();
244 | h.removeMessages(what);
245 | h.sendEmptyMessageDelayed(what, delay);
246 | }
247 |
248 | private void schedule(int what, int arg1, int arg2, Object obj, int delay) {
249 | final H h = getHandler();
250 | h.removeMessages(what);
251 | h.sendMessageDelayed(h.obtainMessage(what, arg1, arg2, obj), delay);
252 | }
253 |
254 | public H getHandler() { if (mH == null) mH = new H(this); return mH; }
255 |
256 | private static final class H extends Handler {
257 | private WeakReference mView;
258 | H(ViewSwitcherWrapper v) {
259 | super(Looper.getMainLooper());
260 | mView = new WeakReference(v);
261 | }
262 | @Override
263 | public void handleMessage(Message msg) {
264 | ViewSwitcherWrapper v = mView.get();
265 | if (v != null) v.handleMessage(msg);
266 | }
267 | }
268 | }
269 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationEffect.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.content.Context;
20 | import android.media.AudioManager;
21 | import android.media.Ringtone;
22 | import android.media.RingtoneManager;
23 | import android.net.Uri;
24 | import android.os.Build;
25 | import android.os.Vibrator;
26 | import android.util.Log;
27 |
28 | /**
29 | * Effects:
30 | * 1) ringtone
31 | * 2) vibration (require manifest permission {@link android.Manifest.permission#VIBRATE})
32 | */
33 | public class NotificationEffect {
34 |
35 | private static final String TAG = "zemin.NotificationEffect";
36 | public static boolean DBG;
37 |
38 | final Object mLock = new Object();
39 | protected Context mContext;
40 | protected boolean mEnabled = true;
41 | private int mConsumer;
42 |
43 | // ringtone
44 | protected AudioManager mAudioManager;
45 | protected Ringtone mRingtone;
46 | protected Uri mRingtoneUri;
47 | private boolean mRingtoneAuto = true;
48 | private boolean mRingtoneEnabled = true;
49 | private int mRingtoneRes;
50 |
51 | // vibrate
52 | protected Vibrator mVibrator;
53 | protected boolean mHasVibrator = true;
54 | protected boolean mIsVibrating;
55 | private boolean mVibratorAuto = true;
56 | private boolean mVibratorEnabled = true;
57 | private long mVibrateTime;
58 |
59 | public NotificationEffect(Context context) {
60 | mContext = context;
61 | }
62 |
63 | public boolean isEnabled() {
64 | return mEnabled;
65 | }
66 |
67 | public void setEnabled(boolean enable) {
68 | if (enable && !mEnabled) {
69 | if (DBG) Log.d(TAG, "Notification effect is now enabled.");
70 | mEnabled = true;
71 | } else if (!enable && mEnabled) {
72 | if (DBG) Log.d(TAG, "Notification effect is now disabled.");
73 | mEnabled = false;
74 | cancel();
75 | }
76 | }
77 |
78 | public void setConsumer(int consumer) {
79 | if (DBG) Log.d(TAG, "set consumer=" + consumer);
80 | mConsumer = consumer;
81 | }
82 |
83 | public int getConsumer() {
84 | return mConsumer;
85 | }
86 |
87 | public boolean isConsumer(int consumer) {
88 | return mConsumer == consumer;
89 | }
90 |
91 | public boolean isRingtoneEnabled() {
92 | return mRingtoneEnabled;
93 | }
94 |
95 | public void enableRingtone(boolean enable) {
96 | if (enable && !mRingtoneEnabled) {
97 | if (DBG) Log.d(TAG, "ringtone is now enabled.");
98 | mRingtoneEnabled = true;
99 | } else if (!enable && mRingtoneEnabled) {
100 | if (DBG) Log.d(TAG, "ringtone is now disabled.");
101 | mRingtoneEnabled = false;
102 | }
103 | }
104 |
105 | public void setRingtoneResource(int resId) {
106 | mRingtoneRes = resId;
107 | }
108 |
109 | public int getRingtoneResource() {
110 | return mRingtoneRes;
111 | }
112 |
113 | /**
114 | * if both "mRingtoneEnabled" and "mRingtoneAuto" are true,
115 | * ringtone will still be played even though {@link NotificationEntry} does not set its ringtone.
116 | */
117 | public void setRingtoneAuto(boolean auto) {
118 | mRingtoneAuto = auto;
119 | }
120 |
121 | public boolean isRingtoneAuto() {
122 | return mRingtoneAuto;
123 | }
124 |
125 | public boolean isVibratorEnabled() {
126 | return mVibratorEnabled;
127 | }
128 |
129 | public void enableVibrator(boolean enable) {
130 | if (enable && !mVibratorEnabled) {
131 | if (DBG) Log.d(TAG, "vibrator is now enabled.");
132 | mVibratorEnabled = true;
133 | } else if (!enable && mVibratorEnabled) {
134 | if (DBG) Log.d(TAG, "vibrator is now disabled.");
135 | mVibratorEnabled = false;
136 | }
137 | }
138 |
139 | public void setVibrateTime(long time) {
140 | if (time > 0L) mVibrateTime = time;
141 | }
142 |
143 | /**
144 | * if both "mVibratorEnabled" and "mVibratorAuto" are true,
145 | * vibrator will sill vibrate the device even though {@link NotificationEntry} does not set its vibrate time.
146 | */
147 | public void setVibratorAuto(boolean auto) {
148 | mVibratorAuto = auto;
149 | }
150 |
151 | public boolean isVibratorAuto() {
152 | return mVibratorAuto;
153 | }
154 |
155 | public void play(NotificationEntry entry) {
156 | ringtone(entry);
157 | vibrate(entry);
158 | }
159 |
160 | // ringtone
161 | public void ringtone(NotificationEntry entry) {
162 | if (!mEnabled) {
163 | Log.w(TAG, "failed to play ringtone. effect disabled.");
164 | return;
165 | }
166 | if (mRingtoneEnabled && mRingtoneAuto &&
167 | entry.playRingtone && entry.ringtoneUri == null) {
168 | // default ringtone
169 | if (DBG) Log.d(TAG, "[default] ringtone");
170 | entry.setRingtone(mContext, mRingtoneRes);
171 | }
172 | if (entry.playRingtone) {
173 | Uri ringtone = entry.ringtoneUri;
174 | if (ringtone == null) {
175 | Log.e(TAG, "ringtone uri not found.");
176 | return;
177 | }
178 |
179 | Ringtone r = null;
180 | if (mRingtone != null && mRingtoneUri != null && mRingtoneUri.equals(ringtone)) {
181 | r = mRingtone;
182 | } else {
183 | r = RingtoneManager.getRingtone(mContext, ringtone);
184 | mRingtone = r;
185 | mRingtoneUri = ringtone;
186 | }
187 |
188 | if (r == null) {
189 | Log.e(TAG, "ringtone not found.");
190 | return;
191 | }
192 |
193 | if (mAudioManager == null) {
194 | mAudioManager = (AudioManager)
195 | mContext.getSystemService(Context.AUDIO_SERVICE);
196 | }
197 |
198 | if (mAudioManager != null &&
199 | mAudioManager.getStreamVolume(r.getStreamType()) == 0) {
200 | Log.i(TAG, "volume muted. won't play any ringtone.");
201 | return;
202 | }
203 |
204 | if (DBG) Log.d(TAG, "ringtone - " + entry.ringtoneUri);
205 |
206 | r.play();
207 | }
208 | }
209 |
210 | // vibrate
211 | public void vibrate(NotificationEntry entry) {
212 | if (!mEnabled) {
213 | Log.w(TAG, "failed to vibrate. effect disabled.");
214 | return;
215 | }
216 | if (mHasVibrator) {
217 | if (mVibrator == null) {
218 | mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
219 | if (Build.VERSION.SDK_INT >= 11) {
220 | mHasVibrator = mVibrator != null ? mVibrator.hasVibrator() : false;
221 | }
222 | }
223 |
224 | if (!mHasVibrator) {
225 | Log.w(TAG, "don't have vibrator on this device.");
226 | return;
227 | }
228 |
229 | if (mVibratorEnabled && mVibratorAuto &&
230 | entry.useVibration && entry.vibrateTime <= 0 &&
231 | entry.vibratePattern == null) {
232 | // default vibration
233 | if (DBG) Log.d(TAG, "[default] vibrate - " + mVibrateTime + " ms");
234 | entry.setVibrate(mVibrateTime);
235 | }
236 |
237 | cancelVibration();
238 | if (entry.useVibration) {
239 | if (entry.vibratePattern != null) {
240 | if (DBG) Log.d(TAG, "vibrate - " + entry.vibratePattern);
241 | mVibrator.vibrate(entry.vibratePattern, entry.vibrateRepeat);
242 | mIsVibrating = true;
243 | } else if (entry.vibrateTime > 0) {
244 | if (DBG) Log.d(TAG, "vibrate - " + entry.vibrateTime);
245 | mVibrator.vibrate(entry.vibrateTime);
246 | mIsVibrating = true;
247 | } else {
248 | Log.e(TAG, "no vibrate.");
249 | }
250 | }
251 | }
252 | }
253 |
254 | public void cancel() {
255 | if (DBG) Log.d(TAG, "cancel");
256 | cancelRingtone();
257 | cancelVibration();
258 | mConsumer = 0;
259 | }
260 |
261 | public void cancelRingtone() {
262 | if (mRingtone != null && mRingtone.isPlaying()) {
263 | if (DBG) Log.d(TAG, "cancel ringtone");
264 | mRingtone.stop();
265 | }
266 | }
267 |
268 | public void cancelVibration() {
269 | if (mVibrator != null && mIsVibrating) {
270 | if (DBG) Log.d(TAG, "cancel vibration");
271 | mVibrator.cancel();
272 | mIsVibrating = false;
273 | }
274 | }
275 | }
276 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationRemote.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.app.Notification;
20 | import android.app.NotificationManager;
21 | import android.app.PendingIntent;
22 | import android.content.BroadcastReceiver;
23 | import android.content.Context;
24 | import android.content.Intent;
25 | import android.content.IntentFilter;
26 | import android.os.Bundle;
27 | import android.os.Looper;
28 | import android.util.Log;
29 |
30 | import android.support.v4.app.NotificationCompat;
31 |
32 | /**
33 | * Status-bar notification.
34 | */
35 | public class NotificationRemote extends NotificationHandler {
36 |
37 | public static final String SIMPLE_NAME = "Remote";
38 | public static boolean DBG;
39 |
40 | public static final String ACTION_CANCEL =
41 | "notification.intent.action.notificationremote.cancel";
42 |
43 | public static final String ACTION_CONTENT =
44 | "notification.intent.action.notificationremote.content";
45 |
46 | public static final String ACTION_ACTION =
47 | "notification.intent.action.notificationremote.action";
48 |
49 | public static final String KEY_ENTRY_ID = "key_entry_id";
50 | public static final String KEY_ACTION_ID = "key_action_id";
51 |
52 | private static int sID = 0;
53 |
54 | private NotificationRemoteCallback mCallback;
55 | private NotificationManager mManager;
56 | private NotificationCompat.Builder mBuilder;
57 | private Receiver mReceiver;
58 | private IntentFilter mFilter;
59 | private boolean mListening;
60 |
61 | /* package */ NotificationRemote(Context context, Looper looper) {
62 | super(context, NotificationDelegater.REMOTE, looper);
63 |
64 | mManager = (NotificationManager)
65 | context.getSystemService(Context.NOTIFICATION_SERVICE);
66 |
67 | mReceiver = new Receiver();
68 | }
69 |
70 | /**
71 | * Set callback.
72 | *
73 | * @param cb
74 | */
75 | public void setCallback(NotificationRemoteCallback cb) {
76 | if (mCallback != cb) {
77 | stopListening();
78 | mCallback = cb;
79 | }
80 | }
81 |
82 | /**
83 | * Add a new Intent action for BroadcastReceiver {@link #Receiver}.
84 | *
85 | * @param action
86 | */
87 | public void addAction(String action) {
88 | getFilter().addAction(action);
89 | }
90 |
91 | /**
92 | * Whether the given action is included in the filter.
93 | *
94 | * @param action
95 | */
96 | public boolean hasAction(String action) {
97 | return getFilter().hasAction(action);
98 | }
99 |
100 | /**
101 | * Generate Id for {@link android.app.PendingIntent}.
102 | *
103 | * @return int
104 | */
105 | public int genIdForPendingIntent() {
106 | return sID++;
107 | }
108 |
109 | /**
110 | * Get builder for {@link android.app.Notification}.
111 | *
112 | * @return NotificationCompat#Builder
113 | */
114 | public NotificationCompat.Builder getStatusBarNotificationBuilder() {
115 | if (mBuilder == null) {
116 | mBuilder = new NotificationCompat.Builder(mContext);
117 | }
118 | return mBuilder;
119 | }
120 |
121 | /**
122 | * Create an PendingIntent to execute when the notification is explicitly dismissed by the user.
123 | *
124 | * @see android.app.Notification#setDeleteIntent(PendingIntent)
125 | *
126 | * @param entry
127 | * @return PendingIntent
128 | */
129 | public PendingIntent getDeleteIntent(NotificationEntry entry) {
130 | Intent intent = new Intent(ACTION_CANCEL);
131 | intent.putExtra(KEY_ENTRY_ID, entry.ID);
132 | return PendingIntent.getBroadcast(mContext, genIdForPendingIntent(), intent, 0);
133 | }
134 |
135 | /**
136 | * Create an PendingIntent to execute when the notification is clicked by the user.
137 | *
138 | * @see android.app.Notification#setContentIntent(PendingIntent)
139 | *
140 | * @param entry
141 | * @return PedningIntent
142 | */
143 | public PendingIntent getContentIntent(NotificationEntry entry) {
144 | Intent intent = new Intent(ACTION_CONTENT);
145 | intent.putExtra(KEY_ENTRY_ID, entry.ID);
146 | return PendingIntent.getBroadcast(mContext, genIdForPendingIntent(), intent, 0);
147 | }
148 |
149 | /**
150 | * Create an PendingIntent to be fired when the notification action is invoked.
151 | *
152 | * @see android.app.Notification#addAction(int, CharSequence, PendingIntent)
153 | *
154 | * @param entry
155 | * @param act
156 | * @return PendingIntent
157 | */
158 | public PendingIntent getActionIntent(NotificationEntry entry, NotificationEntry.Action act) {
159 | Intent intent = new Intent(ACTION_ACTION);
160 | intent.putExtra(KEY_ENTRY_ID, entry.ID);
161 | intent.putExtra(KEY_ACTION_ID, entry.mActions.indexOf(act));
162 | return PendingIntent.getBroadcast(mContext, genIdForPendingIntent(), intent, 0);
163 | }
164 |
165 | @Override
166 | protected void onCancel(NotificationEntry entry) {
167 | mManager.cancel(entry.ID);
168 | onCancelFinished(entry);
169 | }
170 |
171 | @Override
172 | protected void onCancelAll() {
173 | mManager.cancelAll();
174 | onCancelAllFinished();
175 | }
176 |
177 | @Override
178 | protected void onArrival(NotificationEntry entry) {
179 |
180 | if (mCallback == null) {
181 | if (DBG) Log.v(TAG, "set default NotificationRemoteCallback");
182 | mCallback = new NotificationRemoteCallback();
183 | }
184 |
185 | if (!mListening) {
186 | mCallback.onRemoteSetup(this);
187 | startListening();
188 | }
189 |
190 | Notification n = mCallback.makeStatusBarNotification(this, entry, entry.layoutId);
191 | if (n == null) {
192 | Log.e(TAG, "failed to send remote notification. {null Notification}");
193 | onSendIgnored(entry);
194 | return;
195 | }
196 |
197 | mManager.notify(entry.tag, entry.ID, n);
198 |
199 | if (entry.useSystemEffect) {
200 | cancelEffect(entry);
201 | }
202 | onSendFinished(entry);
203 | }
204 |
205 | @Override
206 | protected void onUpdate(NotificationEntry entry) {
207 | onArrival(entry);
208 | }
209 |
210 | private void onCanceledRemotely(NotificationEntry entry) {
211 | reportCanceled(entry);
212 | }
213 |
214 | private void startListening() {
215 | if (!mListening) {
216 | IntentFilter filter = getFilter();
217 | filter.addAction(ACTION_CANCEL);
218 | filter.addAction(ACTION_CONTENT);
219 | filter.addAction(ACTION_ACTION);
220 | mContext.registerReceiver(mReceiver, filter, null, this);
221 | mListening = true;
222 | }
223 | }
224 |
225 | private void stopListening() {
226 | if (mListening) {
227 | mListening = false;
228 | mFilter = null;
229 | mContext.unregisterReceiver(mReceiver);
230 | }
231 | }
232 |
233 | private IntentFilter getFilter() {
234 | if (mFilter == null) {
235 | mFilter = new IntentFilter();
236 | }
237 | return mFilter;
238 | }
239 |
240 | private class Receiver extends BroadcastReceiver {
241 |
242 | @Override
243 | public void onReceive(Context context, Intent intent) {
244 |
245 | final String action = intent.getAction();
246 | final int entryId = intent.getIntExtra(KEY_ENTRY_ID, -1);
247 | final NotificationEntry entry = getNotification(entryId);
248 | if (DBG) Log.d(TAG, "onReceive - " + entryId + ", " + action);
249 |
250 | if (ACTION_CANCEL.equals(action)) {
251 | onCanceledRemotely(entry);
252 | mCallback.onCancelRemote(NotificationRemote.this, entry);
253 | } else if (ACTION_CONTENT.equals(action)) {
254 | entry.executeContentAction(mContext);
255 | mCallback.onClickRemote(NotificationRemote.this, entry);
256 | if (entry.autoCancel) {
257 | onCanceledRemotely(entry);
258 | }
259 | } else if (ACTION_ACTION.equals(action)) {
260 | final int actionId = intent.getIntExtra(KEY_ACTION_ID, -1);
261 | final NotificationEntry.Action act = entry.mActions.get(actionId);
262 | mCallback.onClickRemoteAction(NotificationRemote.this, entry, act);
263 | act.execute(mContext);
264 | } else {
265 | mCallback.onReceive(NotificationRemote.this, entry, intent, action);
266 | }
267 | }
268 | }
269 |
270 | @Override public String toSimpleString() { return SIMPLE_NAME; }
271 | @Override public String toString() { return SIMPLE_NAME; }
272 | }
273 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationDelegater.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.content.Context;
20 | import android.content.pm.ApplicationInfo;
21 | import android.os.HandlerThread;
22 | import android.os.Looper;
23 | import android.util.Log;
24 |
25 | import java.util.List;
26 |
27 | /**
28 | * Delegater
29 | */
30 | public class NotificationDelegater {
31 |
32 | private static final String TAG = "zemin.NotificationDelegater";
33 | public static boolean DBG;
34 |
35 | /**
36 | * Support local (in-layout) notifications.
37 | */
38 | public static final int LOCAL = 0x00000001;
39 |
40 | /**
41 | * Support global (floating) notifications.
42 | */
43 | public static final int GLOBAL = 0x00000002;
44 |
45 | /**
46 | * Support remote (status-bar) notifications.
47 | */
48 | public static final int REMOTE = 0x00000004;
49 |
50 | public static final int MASK = 0x0000000F;
51 |
52 |
53 | /**
54 | * Get the singleton instance.
55 | *
56 | * @return NotificationDelegater
57 | */
58 | public static NotificationDelegater getInstance() {
59 | synchronized (NotificationDelegater.class) {
60 | if (sSelf == null)
61 | sSelf = new NotificationDelegater();
62 | return sSelf;
63 | }
64 | }
65 |
66 | /**
67 | * Initialization.
68 | *
69 | * @see NotificationDelegater#LOCAL
70 | * @see NotificationDelegater#GLOBAL
71 | * @see NotificationDelegater#REMOTE
72 | *
73 | * @param context
74 | * @param components
75 | */
76 | public static void initialize(Context context, int components) {
77 | NotificationDelegater delegater = getInstance();
78 | if (delegater.center() != null)
79 | throw new IllegalStateException("NotificationDelegater already init.");
80 |
81 | delegater.setContext(context);
82 | delegater.initComponents(components);
83 | Log.i(TAG, "Notification delegater initialize");
84 | }
85 |
86 | /**
87 | * Whether notification is disabled globally.
88 | *
89 | * @return boolean
90 | */
91 | public boolean isEnabled() {
92 | return mEnabled;
93 | }
94 |
95 | /**
96 | * Enable/disable notification globally.
97 | *
98 | * @param enable
99 | */
100 | public void setEnabled(boolean enable) {
101 | if (enable && !mEnabled) {
102 | if (DBG) Log.d(TAG, "Notification is now enabled.");
103 | mEnabled = true;
104 | } else if (enable && !mEnabled) {
105 | if (DBG) Log.d(TAG, "Notification is now disabled.");
106 | mEnabled = false;
107 | cancelAll();
108 | }
109 | }
110 |
111 | /**
112 | * Send notification. If a notification with the same id has already been posted,
113 | * it will be replaced by the updated information.
114 | *
115 | * @param entry
116 | */
117 | public void send(NotificationEntry entry) {
118 | if (mEnabled) CENTER.send(entry);
119 | }
120 |
121 | /**
122 | * Cancel notification.
123 | *
124 | * @param entryId
125 | */
126 | public void cancel(int entryId) {
127 | if (mEnabled) CENTER.cancel(entryId);
128 | }
129 |
130 | /**
131 | * Cancel notification. Notifications having the same tag will all be canceled.
132 | *
133 | * @param tag
134 | */
135 | public void cancel(String tag) {
136 | if (mEnabled) CENTER.cancel(tag);
137 | }
138 |
139 | /**
140 | * Cancel notification.
141 | *
142 | * @param entry
143 | */
144 | public void cancel(NotificationEntry entry) {
145 | if (mEnabled) CENTER.cancel(entry);
146 | }
147 |
148 | /**
149 | * Cancel all notifications.
150 | */
151 | public void cancelAll() {
152 | if (mEnabled) CENTER.cancelAll();
153 | }
154 |
155 | /**
156 | * Whether any notifications exist.
157 | *
158 | * @return boolean
159 | */
160 | public boolean hasNotifications() {
161 | return CENTER.hasEntries();
162 | }
163 |
164 | /**
165 | * Whether the notification with id exists.
166 | *
167 | * @param entryId
168 | * @return boolean
169 | */
170 | public boolean hasNotification(int entryId) {
171 | return CENTER.hasEntry(entryId);
172 | }
173 |
174 | /**
175 | * Whether the notifications with tag exists.
176 | *
177 | * @param tag
178 | * @return boolean
179 | */
180 | public boolean hasNotifications(String tag) {
181 | return CENTER.hasEntries(tag);
182 | }
183 |
184 | /**
185 | * Get notification by its id.
186 | *
187 | * @param entryId
188 | * @return NotificationEntry
189 | */
190 | public NotificationEntry getNotification(int entryId) {
191 | return CENTER.getEntry(entryId);
192 | }
193 |
194 | /**
195 | * Get notification by its tag.
196 | *
197 | * @param tag
198 | * @return List
199 | */
200 | public List getNotifications(String tag) {
201 | return CENTER.getEntries(tag);
202 | }
203 |
204 | /**
205 | * Get notifications.
206 | *
207 | * @return List
208 | */
209 | public List getNotifications() {
210 | return CENTER.mActives.getEntries();
211 | }
212 |
213 | /**
214 | * Get current notification count.
215 | *
216 | * @param tag
217 | * @return int
218 | */
219 | public int getNotificationCount(String tag) {
220 | return CENTER.mActives.getEntryCount(tag);
221 | }
222 |
223 | /**
224 | * Get current notification count.
225 | *
226 | * @return int
227 | */
228 | public int getNotificationCount() {
229 | return CENTER.mActives.getEntryCount();
230 | }
231 |
232 | /**
233 | * Add listener whick will be invoked when a notification arrives or get canceled.
234 | *
235 | * @param listener
236 | */
237 | public void addListener(NotificationListener listener) {
238 | CENTER.addListener(listener);
239 | }
240 |
241 | /**
242 | * Remove listener.
243 | *
244 | * @param listener
245 | */
246 | public void removeListener(NotificationListener listener) {
247 | CENTER.removeListener(listener);
248 | }
249 |
250 | /**
251 | * Enable/disable notification effect globally.
252 | *
253 | * @param enable
254 | */
255 | public void enableEffect(boolean enable) {
256 | CENTER.effect().setEnabled(enable);
257 | }
258 |
259 | /**
260 | * Get the singleton object of NotificationEffect.
261 | *
262 | * @return NotificationEffect
263 | */
264 | public NotificationEffect effect() {
265 | return CENTER.effect();
266 | }
267 |
268 | /**
269 | * Get the singleton object of NotificationRemote.
270 | *
271 | * @return NotificationRemote
272 | */
273 | public NotificationRemote remote() {
274 | return (NotificationRemote) CENTER.get(REMOTE);
275 | }
276 |
277 | /**
278 | * Get the singleton object of NotificationLocal.
279 | *
280 | * @return NotificationLocal
281 | */
282 | public NotificationLocal local() {
283 | return (NotificationLocal) CENTER.get(LOCAL);
284 | }
285 |
286 | /**
287 | * Get the singleton object of NotificationGlobal.
288 | *
289 | * @return NotificationGlobal
290 | */
291 | public NotificationGlobal global() {
292 | return (NotificationGlobal) CENTER.get(GLOBAL);
293 | }
294 |
295 | /**
296 | * Enable/disable debug log.
297 | *
298 | * @param debug
299 | */
300 | public static void debug(boolean debug) {
301 | NotificationCenter.DBG =
302 | NotificationEffect.DBG =
303 | NotificationBuilder.DBG =
304 | NotificationEntry.DBG =
305 | NotificationHandler.DBG =
306 | NotificationRemote.DBG =
307 | NotificationRemoteCallback.DBG =
308 | NotificationLocal.DBG =
309 | NotificationGlobal.DBG =
310 | NotificationView.DBG =
311 | NotificationViewCallback.DBG =
312 | NotificationRootView.DBG =
313 | NotificationBoard.DBG =
314 | NotificationBoardCallback.DBG =
315 | ViewWrapper.DBG =
316 | ViewSwitcherWrapper.DBG =
317 | ChildViewManager.DBG =
318 | DBG = debug;
319 | }
320 |
321 | private Context mContext;
322 | private boolean mEnabled = true;
323 | private HandlerThread mHT;
324 | private NotificationCenter CENTER;
325 |
326 | /* package */ NotificationCenter center() { return CENTER; }
327 |
328 | private void setContext(Context context) {
329 | mContext = context;
330 | }
331 |
332 | private void initComponents(int components) {
333 | CENTER = new NotificationCenter(mContext);
334 |
335 | // effect
336 | NotificationEffect effect = CENTER.effect();
337 | effect.setRingtoneResource(R.raw.fallbackring);
338 | effect.setVibrateTime(300L);
339 |
340 | // remote
341 | if ((components & REMOTE) != 0) {
342 | CENTER.register(new NotificationRemote(mContext, getMyLooper()));
343 | }
344 |
345 | // local
346 | if ((components & LOCAL) != 0) {
347 | CENTER.register(new NotificationLocal(mContext, getMyLooper()));
348 | }
349 |
350 | // global
351 | if ((components & GLOBAL) != 0) {
352 | CENTER.register(new NotificationGlobal(mContext, getMyLooper()));
353 | }
354 |
355 | debug((mContext.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
356 | }
357 |
358 | private static NotificationDelegater sSelf;
359 | private NotificationDelegater() {}
360 |
361 | private Looper getMyLooper() {
362 | if (mHT == null) {
363 | mHT = new HandlerThread(TAG);
364 | mHT.start(); // non-stop
365 | }
366 | return mHT.getLooper();
367 | }
368 | }
369 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Android Notification Library
2 |
3 | A notification library for Android applications.
4 |
5 | By using this library, you can easily manage your notifications.
6 |
7 | * send remote/global notification when application is in background.
8 | * send local/global notification when application is in foreground.
9 | * register a `NotificationListener` for updating the badge number of notification count.
10 | * display a `NotificationBoard` with a list of current notifications.
11 | * ...
12 |
13 | ## Overview
14 |
15 | There are 3 major components:
16 |
17 | * Notification Local (managing in-layout notifications)
18 | * Notification Global (managing floating notifications)
19 | * Notification Remote (managing status-bar notifications)
20 |
21 | and 2 minor components:
22 |
23 | * Notification Board (holding a notification list)
24 | * Notification Effect (managing ringtone, vibration, ...)
25 |
26 | Each notification can have its own layout and background. If not specified, the default layout and background will be used.
27 |
28 | If a notification is sent to more than one component, its status is synchronized between those components.
29 | Thus, canceling a notification in one component will trigger the cancel event in other components.
30 |
31 | ## Demo
32 |
33 | Sample code is also available in this repository.
34 |
35 | #### Notification Local
36 |
37 | 
38 |
39 | #### Notification Global
40 |
41 | 
42 |
43 | #### Notification Board
44 |
45 | 
46 |
47 | #### Notification Remote
48 |
49 | 
50 |
51 | ## Programming Guide
52 |
53 | ### Compatibility
54 | * minSdkVersion: 11
55 |
56 | ### Gradle
57 |
58 | Android-Notification library is pushed to Maven Central as a AAR, so you just need to
59 | declare the following dependency to your `build.gradle`.
60 |
61 | ``` xml
62 | dependencies {
63 | compile 'com.github.lamydev:android-notification:3.0'
64 | }
65 | ```
66 |
67 | ### Initialization
68 |
69 | You need to tell `NotificationDelegater` the major components you want to use.
70 |
71 | ``` java
72 | NotificationDelegater.initialize(context, components);
73 | ```
74 |
75 | Available components:
76 |
77 | * NotificationDelegater.LOCAL
78 | * NotificationDelegater.GLOBAL
79 | * NotificationDelegater.REMOTE
80 |
81 | Once it has been inited, you can enable/disable a component at runtime.
82 |
83 | For example, to disable the local notification:
84 |
85 | ``` java
86 | NotificationLocal local = NotificationDelegater.getInstance().local();
87 | local.setEnabled(false);
88 | ```
89 |
90 | ### Notification Local (in-layout notifications)
91 |
92 | You need to add `NotificationView` to your layout.
93 |
94 | Otherwise, nothing will be presented if you try to send a local notification.
95 |
96 | ``` xml
97 |
101 | ```
102 |
103 | Then attach it to `NotificationLocal`.
104 |
105 | ``` java
106 | NotificationLocal local = NotificationDelegater.getInstance().local();
107 | NotificationView view = (NotificationView) findViewById(R.id.nv);
108 | local.setView(view);
109 | ```
110 |
111 | To send a local notification:
112 |
113 | ``` java
114 | NotificationBuilder.V1 builder = NotificationBuilder.local()
115 | .setIconDrawable(icon)
116 | .setTitle(title)
117 | .setText(text);
118 |
119 | NotificationDelegater delegater = NotificationDelegater.getInstance();
120 | delegater.send(builder.getNotification());
121 | ```
122 |
123 | #### Customization
124 |
125 | * Refer to the JavaDoc of `NotificationView`.
126 | * Refer to the JavaDoc of `NotificationGlobal`.
127 | * Implement `NotificationViewCallback` or `NotificationLocal#ViewCallback`.
128 | Pass an instance to `NotificationView#setCallback(NotificationViewCallback cb)`.
129 |
130 | ### Notification Global (floating notifications)
131 |
132 | For global notification, you don't need to attach any `NotificationView`.
133 | Instead, it will do that for you.
134 |
135 | Once it has been fired, it is displayed on top of any screen and remains visible for its specified duration, regardless of the visibility of you application's main screen.
136 |
137 | By default, all features are disabled. You need to manually enable them.
138 |
139 | ``` java
140 | NotificationGlobal global = NotificationDelegater.getInstance().global();
141 | global.setViewEnabled(true);
142 | ```
143 |
144 | To send a global notification:
145 |
146 | ``` java
147 | NotificationBuilder.V1 builder = NotificationBuilder.global()
148 | .setIconDrawable(icon)
149 | .setTitle(title)
150 | .setText(text);
151 |
152 | NotificationDelegater delegater = NotificationDelegater.getInstance();
153 | delegater.send(builder.getNotification());
154 | ```
155 |
156 | #### Customization
157 |
158 | * Refer to the JavaDoc of `NotificationView`.
159 | * Refer to the JavaDoc of `NotificationGlobal`.
160 | * Implement `NotificationViewCallback` or `NotificationGlobal#ViewCallback`.
161 | Pass an instance to `NotificationView#setCallback(NotificationViewCallback cb)`.
162 |
163 | ### Notification Board (notification list)
164 |
165 | A notification board shows a list of currently delivered notifications.
166 |
167 | Initially, the board is hidden. You can open it by:
168 |
169 | * invoking method: `NotificationBoard#open(boolean anim)`
170 | * user gesture: touches inside a rectangular area on the screen and performs a scroll down action.
171 |
172 | By default, user gesture is not supported since the touch area is not defined.
173 |
174 | To specify a touch area, you can call `NotificationBoard#setInitialTouchArea(int l, int t, int r, int b)`.
175 |
176 | #### Floating board
177 |
178 | It is managed by the `NotificationGlobal`, so the board feature needs to be enabled before you can use it.
179 |
180 | ``` java
181 | NotificationGlobal global = NotificationDelegater.getInstance().global();
182 | global.setBoardEnabled(true);
183 | ```
184 |
185 | In this case, the initial touch area is automatically set by `NotificationRootView`.
186 |
187 | * if `NotificationView` is displayed, the initial touch area of the board would be the same as the view's dimension.
188 | * if `NotificationView` is not displayed, the initial touch area would be reset to 0.
189 |
190 | Thus, the board can be opened only when the `NotificationView` is displayed, and the user scrolls it down.
191 |
192 | #### In-layout board
193 |
194 | You just need to simply add `NotificationBoard` to your layout.... without attaching it to any
195 | notification handler, such as `NotificationLocal`, `NotificationGlobal`.
196 |
197 | ``` xml
198 |
202 | ```
203 |
204 | #### Customization
205 |
206 | * Refer to the JavaDoc of `NotificationBoard`.
207 | * Implement `NotificationBoardCallback`.
208 | Pass an instance to `NotificationBoard#setCallback(NotificationBoardCallback cb)`.
209 |
210 | ### Notification Remote (status-bar notifications)
211 |
212 | This is eventually a StatusBar Notification.
213 |
214 | ``` java
215 | NotificationBuilder.V2 builder = NotificationBuilder.remote()
216 | .setSmallIconResource(icon)
217 | .setTicker(tickerText)
218 | .setTitle(title)
219 | .setText(text);
220 |
221 | NotificationDelegater delegater = NotificationDelegater.getInstance();
222 | delegater.send(builder.getNotification());
223 | ```
224 |
225 | #### Customization
226 |
227 | * Refer to the JavaDoc of `NotificationRemote`.
228 | * Implement `NotificationRemoteCallback`.
229 | Pass an instance to `NotificationRemote#setCallback`.
230 |
231 | ### Notification Listener
232 |
233 | Implement this interface to listen to overall Notification status.
234 |
235 | By default, all notifications will be delivered to all listeners.
236 |
237 | Here's an example.
238 |
239 | If you have a badge showing notification count, you may need register a `NotificationListener` to help keep it up-to-date.
240 |
241 | 
242 |
243 | ``` java
244 | public class MainActivity extends Activity {
245 |
246 | @Override
247 | protected void onResume() {
248 | super.onResume();
249 |
250 | NotificationDelegater.getInstance().addListener(mListener);
251 | }
252 |
253 | @Override
254 | protected void onPause() {
255 | super.onPause();
256 |
257 | NotificationDelegater.getInstance().removeListener(mListener);
258 | }
259 |
260 | private final NotificationListener mListener = new NotificationListener() {
261 |
262 | @Override
263 | public void onArrival(NotificationEntry entry) {
264 | updateNotificationCount();
265 | }
266 |
267 | @Override
268 | public void onCancel(NotificationEntry entry) {
269 | updateNotificationCount();
270 | }
271 | }
272 |
273 | private void updateNotificationCount() {
274 | final int count = NotificationDelegater.getInstance().getNotificationCount();
275 | // do something ...
276 | }
277 | }
278 | ```
279 |
280 | ### Notification Effect
281 |
282 | By default, notification effect is disabled for all components.
283 |
284 | To enable it for a specific component, for example, for global notification:
285 |
286 | ``` java
287 | NotificationGlobal global = NotificationDelegater.getInstance().global();
288 | global.enableEffect(true);
289 | ```
290 |
291 | Then you can play a ringtone when sending a global notification:
292 |
293 | ``` java
294 | NotificationBuilder.V1 builder = NotificationBuilder.global()
295 | .setIconDrawable(icon)
296 | .setTitle(title)
297 | .setText(text)
298 | .setPlayRingtone(true)
299 | .setRingtone(context, resId);
300 |
301 | NotificationDelegater delegater = NotificationDelegater.getInstance();
302 | delegater.send(builder.getNotification());
303 | ```
304 |
305 | ## Developers
306 | * Zemin Liu (lam2dev@gmail.com)
307 |
308 | Any questions, contributions, bug fixes, and patches are welcomed. ^\_^
309 |
310 | ## License
311 |
312 | ```
313 | Copyright (C) 2015 Zemin Liu
314 |
315 | Licensed under the Apache License, Version 2.0 (the "License");
316 | you may not use this file except in compliance with the License.
317 | You may obtain a copy of the License at
318 |
319 | http://www.apache.org/licenses/LICENSE-2.0
320 |
321 | Unless required by applicable law or agreed to in writing, software
322 | distributed under the License is distributed on an "AS IS" BASIS,
323 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
324 | See the License for the specific language governing permissions and
325 | limitations under the License.
326 | ```
327 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/samples/src/zemin/notification/samples/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification.samples;
18 |
19 | import android.app.Activity;
20 | import android.graphics.Color;
21 | import android.os.Bundle;
22 | import android.util.Log;
23 | import android.view.View;
24 | import android.view.ViewGroup;
25 | import android.widget.Button;
26 | import android.widget.TextView;
27 | import android.support.v7.app.ActionBarActivity;
28 |
29 | import zemin.notification.NotificationBoard;
30 | import zemin.notification.NotificationBoardCallback;
31 | import zemin.notification.NotificationBuilder;
32 | import zemin.notification.NotificationDelegater;
33 | import zemin.notification.NotificationEntry;
34 | import zemin.notification.NotificationGlobal;
35 | import zemin.notification.NotificationListener;
36 | import zemin.notification.NotificationLocal;
37 | import zemin.notification.NotificationRemote;
38 | import zemin.notification.NotificationView;
39 | import zemin.notification.NotificationViewCallback;
40 |
41 | //
42 | public class MainActivity extends ActionBarActivity {
43 |
44 | private static final String TAG = "zemin.Notification.samples";
45 | private static final boolean DBG = true;
46 |
47 | private NotificationDelegater mDelegater;
48 | private NotificationRemote mRemote;
49 | private NotificationLocal mLocal;
50 | private NotificationGlobal mGlobal;
51 |
52 | @Override
53 | protected void onCreate(Bundle savedInstanceState) {
54 | super.onCreate(savedInstanceState);
55 | initView();
56 |
57 | mDelegater = NotificationDelegater.getInstance();
58 | mRemote = mDelegater.remote();
59 | mLocal = mDelegater.local();
60 | mGlobal = mDelegater.global();
61 |
62 | // enable global view && board
63 | mGlobal.setViewEnabled(true);
64 | mGlobal.setBoardEnabled(true);
65 |
66 | // attach local NotificationView
67 | mLocal.setView((NotificationView) findViewById(R.id.nv));
68 | }
69 |
70 | @Override
71 | protected void onResume() {
72 | super.onResume();
73 |
74 | // add listener
75 | mDelegater.addListener(mNotificationListener);
76 |
77 | // update notification count
78 | mTvTotalCount.setText(String.valueOf(mDelegater.getNotificationCount()));
79 | }
80 |
81 | @Override
82 | protected void onPause() {
83 | super.onPause();
84 |
85 | // remove listener
86 | mDelegater.removeListener(mNotificationListener);
87 | }
88 |
89 | private final NotificationListener mNotificationListener = new NotificationListener() {
90 | @Override
91 | public void onArrival(NotificationEntry entry) {
92 | updateNotificationCount(entry);
93 | }
94 |
95 | @Override
96 | public void onCancel(NotificationEntry entry) {
97 | updateNotificationCount(entry);
98 | }
99 |
100 | @Override
101 | public void onUpdate(NotificationEntry entry) {
102 | }
103 | };
104 |
105 | private void updateNotificationCount(NotificationEntry entry) {
106 | if (entry.isSentToRemote()) {
107 | mTvRemoteCount.setText(String.valueOf(mRemote.getNotificationCount()));
108 | }
109 | if (entry.isSentToLocalView()) {
110 | mTvLocalCount.setText(String.valueOf(mLocal.getNotificationCount()));
111 | }
112 | if (entry.isSentToGlobalView()) {
113 | mTvGlobalCount.setText(String.valueOf(mGlobal.getNotificationCount()));
114 | }
115 | mTvTotalCount.setText(String.valueOf(mDelegater.getNotificationCount()));
116 | }
117 |
118 | private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
119 | public void onClick(View view) {
120 |
121 | switch (view.getId()) {
122 | case R.id.btn_remote_send: {
123 | // send to remote
124 | String title = getNextTitle();
125 | String text = getNextText();
126 |
127 | NotificationBuilder.V2 builder = NotificationBuilder.remote()
128 | .setSmallIconResource(R.drawable.ic_launcher)
129 | .setTicker(title + ": " + text)
130 | .setTitle(title)
131 | .addAction(zemin.notification.R.drawable.clear_button, "yes", null, null)
132 | .setText(text);
133 |
134 | mDelegater.send(builder.getNotification());
135 |
136 | } break;
137 |
138 | case R.id.btn_remote_cancel: {
139 | // cancel all remote
140 | mRemote.cancelAll();
141 | } break;
142 |
143 | case R.id.btn_local_send: {
144 | // send to local view
145 |
146 | NotificationBuilder.V1 builder = NotificationBuilder.local()
147 | .setBackgroundColor(getNextColor())
148 | .setLayoutId(getNextLayout())
149 | .setIconDrawable(getResources().getDrawable(R.drawable.ic_launcher))
150 | .addAction(R.drawable.android_gray, "gray", null)
151 | .addAction(R.drawable.android_gray, "android", null)
152 | .setTitle(getNextTitle())
153 | .setText(getNextText());
154 |
155 | mDelegater.send(builder.getNotification());
156 |
157 | } break;
158 |
159 | case R.id.btn_local_cancel: {
160 | // cancel all local
161 | mLocal.cancelAll();
162 | } break;
163 |
164 | case R.id.btn_local_dismiss: {
165 | // dismiss view
166 | mLocal.dismissView();
167 | } break;
168 |
169 | case R.id.btn_global_send: {
170 | // send to global view
171 |
172 | NotificationBuilder.V1 builder = NotificationBuilder.global()
173 | .setIconDrawable(getResources().getDrawable(R.drawable.ic_launcher))
174 | .setTitle(getNextTitle())
175 | .setText(getNextText());
176 |
177 | mDelegater.send(builder.getNotification());
178 |
179 | } break;
180 |
181 | case R.id.btn_global_cancel: {
182 | // cancel all global
183 | mGlobal.cancelAll();
184 | } break;
185 |
186 | case R.id.btn_global_dismiss: {
187 | // dismiss view, but not cancel the rest
188 | mGlobal.dismissView();
189 | } break;
190 |
191 | case R.id.btn_total_cancel: {
192 | // cancel all
193 | mDelegater.cancelAll();
194 | } break;
195 |
196 | case R.id.btn_board: {
197 | // open notification board
198 | mGlobal.openBoard();
199 | break;
200 | }
201 | }
202 | }
203 | };
204 |
205 |
206 | private Button mBtnRemoteSend;
207 | private Button mBtnRemoteCancel;
208 | private TextView mTvRemoteCount;
209 | private Button mBtnLocalSend;
210 | private Button mBtnLocalCancel;
211 | private Button mBtnLocalDismiss;
212 | private TextView mTvLocalCount;
213 | private Button mBtnGlobalSend;
214 | private Button mBtnGlobalCancel;
215 | private Button mBtnGlobalDismiss;
216 | private TextView mTvGlobalCount;
217 | private Button mBtnTotalCancel;
218 | private TextView mTvTotalCount;
219 | private int mTitleIdx;
220 | private int mTextIdx;
221 | private int mColorIdx;
222 | private int mLayoutIdx;
223 | private Button mBtnBoard;
224 |
225 | private static final String[] mTitleSet = new String[] {
226 | "Guess Who",
227 | "Meet Android",
228 | "I'm a Notification",
229 | };
230 |
231 | private static final String[] mTextSet = new String[] {
232 | "hello world",
233 | "welcome to the android world",
234 | "welcome to code samples for zemin-notification. " +
235 | "Here you can browse sample code and learn how to send, show and cancel a notification.",
236 | "zemin-notification library is available on GitHub under the Apache License v2.0. " +
237 | "You are free to make use of it.",
238 | };
239 |
240 | // if color is set to 0, the default will be used.
241 | private static final int[] mColorSet = new int[] {
242 | 0,
243 | 0xffc0ca33,
244 | 0xff8bc34a,
245 | 0xff00938d,
246 | 0xff607d8b,
247 | };
248 |
249 | // if layout is set to 0, the default will be used.
250 | private static final int[] mLayoutSet = new int[] {
251 | 0,
252 | zemin.notification.R.layout.notification_full,
253 | zemin.notification.R.layout.notification_simple,
254 | zemin.notification.R.layout.notification_large_icon,
255 | zemin.notification.R.layout.notification_simple_2,
256 | };
257 |
258 | private String getNextTitle() {
259 | if (mTitleIdx == mTitleSet.length) {
260 | mTitleIdx = 0;
261 | }
262 | return mTitleSet[mTitleIdx++];
263 | }
264 |
265 | private String getNextText() {
266 | if (mTextIdx == mTextSet.length) {
267 | mTextIdx = 0;
268 | }
269 | return mTextSet[mTextIdx++];
270 | }
271 |
272 | private int getNextColor() {
273 | if (mColorIdx == mColorSet.length) {
274 | mColorIdx = 0;
275 | }
276 | return mColorSet[mColorIdx++];
277 | }
278 |
279 | private int getNextLayout() {
280 | if (mLayoutIdx == mLayoutSet.length) {
281 | mLayoutIdx = 0;
282 | }
283 | return mLayoutSet[mLayoutIdx++];
284 | }
285 |
286 | private void initView() {
287 | setContentView(R.layout.main);
288 |
289 | mBtnRemoteSend = (Button) findViewById(R.id.btn_remote_send);
290 | mBtnRemoteSend.setOnClickListener(mOnClickListener);
291 | mBtnRemoteCancel = (Button) findViewById(R.id.btn_remote_cancel);
292 | mBtnRemoteCancel.setOnClickListener(mOnClickListener);
293 | mTvRemoteCount = (TextView) findViewById(R.id.tv_remote_count);
294 | mBtnLocalSend = (Button) findViewById(R.id.btn_local_send);
295 | mBtnLocalSend.setOnClickListener(mOnClickListener);
296 | mBtnLocalCancel = (Button) findViewById(R.id.btn_local_cancel);
297 | mBtnLocalCancel.setOnClickListener(mOnClickListener);
298 | mBtnLocalDismiss = (Button) findViewById(R.id.btn_local_dismiss);
299 | mBtnLocalDismiss.setOnClickListener(mOnClickListener);
300 | mTvLocalCount = (TextView) findViewById(R.id.tv_local_count);
301 | mBtnGlobalSend = (Button) findViewById(R.id.btn_global_send);
302 | mBtnGlobalSend.setOnClickListener(mOnClickListener);
303 | mBtnGlobalCancel = (Button) findViewById(R.id.btn_global_cancel);
304 | mBtnGlobalCancel.setOnClickListener(mOnClickListener);
305 | mBtnGlobalDismiss = (Button) findViewById(R.id.btn_global_dismiss);
306 | mBtnGlobalDismiss.setOnClickListener(mOnClickListener);
307 | mTvGlobalCount = (TextView) findViewById(R.id.tv_global_count);
308 | mBtnTotalCancel = (Button) findViewById(R.id.btn_total_cancel);
309 | mBtnTotalCancel.setOnClickListener(mOnClickListener);
310 | mTvTotalCount = (TextView) findViewById(R.id.tv_total_count);
311 | mBtnBoard = (Button) findViewById(R.id.btn_board);
312 | mBtnBoard.setOnClickListener(mOnClickListener);
313 | }
314 | }
315 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationGlobal.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.content.BroadcastReceiver;
20 | import android.content.Context;
21 | import android.content.Intent;
22 | import android.content.IntentFilter;
23 | import android.graphics.PixelFormat;
24 | import android.os.Looper;
25 | import android.util.AttributeSet;
26 | import android.util.Log;
27 | import android.view.Gravity;
28 | import android.view.KeyEvent;
29 | import android.view.MotionEvent;
30 | import android.view.View;
31 | import android.view.ViewGroup;
32 | import android.view.WindowManager;
33 | import android.widget.FrameLayout;
34 | import android.widget.TextView;
35 |
36 | /**
37 | * A floating notification window holds a notification root view {@link NotificationRootView},
38 | * where both view {@link NotificationView} and board {@link NotificationBoard} are sitting on.
39 | */
40 | public class NotificationGlobal extends NotificationHandler {
41 |
42 | public static final String SIMPLE_NAME = "Global";
43 | public static boolean DBG;
44 |
45 | /**
46 | * Implementaiton of {@link NotificationViewCallback} for floating {@link NotificationView}.
47 | */
48 | public static class ViewCallback extends NotificationViewCallback {
49 |
50 | @Override
51 | public int getContentViewDefaultLayoutId(NotificationView view) {
52 | return R.layout.notification_full;
53 | }
54 |
55 | @Override
56 | public void onViewSetup(NotificationView view) {
57 | view.setContentMargin(0, 0, 0, 0);
58 | view.setDefaultBackgroundColor(0xff000000);
59 | view.setDefaultBackgroundAlpha(0x7f);
60 | }
61 |
62 | @Override
63 | public void onContentViewChanged(NotificationView view, View contentView, int layoutId) {
64 | super.onContentViewChanged(view, contentView, layoutId);
65 |
66 | ChildViewManager mgr = view.getChildViewManager();
67 | mgr.setTextColor(TITLE, 0xffffffff);
68 | mgr.setTextColor(TEXT, 0xffffffff);
69 | mgr.setTextColor(WHEN, 0xffffffff);
70 | }
71 | }
72 |
73 | private NotificationView mView;
74 | private NotificationBoard mBoard;
75 | private NotificationWindow mWindow;
76 |
77 | /* package */ NotificationGlobal(Context context, Looper looper) {
78 | super(context, NotificationDelegater.GLOBAL, looper);
79 |
80 | mWindow = new NotificationWindow(context);
81 | }
82 |
83 | /**
84 | * Get notification view.
85 | *
86 | * @return NotificationView
87 | */
88 | public NotificationView getView() {
89 | return mView;
90 | }
91 |
92 | /**
93 | * Get notification board.
94 | *
95 | * @return NotificationBoard
96 | */
97 | public NotificationBoard getBoard() {
98 | return mBoard;
99 | }
100 |
101 | /**
102 | * Enable/disable notification view.
103 | *
104 | * @param enable
105 | */
106 | public void setViewEnabled(boolean enable) {
107 | final NotificationRootView root = mWindow.mRoot;
108 | root.setViewEnabled(enable);
109 | if (enable && mView == null) {
110 | mView = root.getView();
111 | mView.initialize(this);
112 | mView.addStateListener(new ViewStateListener());
113 | }
114 | }
115 |
116 | /**
117 | * Enable/disable notification board
118 | *
119 | * @param enable
120 | */
121 | public void setBoardEnabled(boolean enable) {
122 | final NotificationRootView root = mWindow.mRoot;
123 | root.setBoardEnabled(enable);
124 | if (enable && mBoard == null) {
125 | mBoard = root.getBoard();
126 | mBoard.addStateListener(new BoardStateListener());
127 | }
128 | }
129 |
130 | /**
131 | * Set callback for notification view.
132 | *
133 | * @param cb
134 | */
135 | public void setViewCallback(NotificationViewCallback cb) {
136 | if (mView != null) {
137 | mView.setCallback(cb);
138 | }
139 | }
140 |
141 | /**
142 | * Set callback for notification board.
143 | *
144 | * @param cb
145 | */
146 | public void setBoardCallback(NotificationBoardCallback cb) {
147 | if (mBoard != null) {
148 | mBoard.setCallback(cb);
149 | }
150 | }
151 |
152 | /**
153 | * Dismiss notification view.
154 | */
155 | public void dismissView() {
156 | if (mView != null) {
157 | mView.dismiss();
158 | }
159 | }
160 |
161 | /**
162 | * Open notification board.
163 | */
164 | public void openBoard() {
165 | if (mBoard != null) {
166 | mBoard.open(true);
167 | }
168 | }
169 |
170 | /**
171 | * Close notification board.
172 | */
173 | public void closeBoard() {
174 | if (mBoard != null) {
175 | mBoard.close(true);
176 | }
177 | }
178 |
179 | @Override
180 | protected void onCancel(NotificationEntry entry) {
181 | if (mView != null) {
182 | mView.onCancel(entry);
183 | } else {
184 | onCancelFinished(entry);
185 | }
186 | }
187 |
188 | @Override
189 | protected void onCancelAll() {
190 | if (mView != null) {
191 | mView.onCancelAll();
192 | } else {
193 | onCancelAllFinished();
194 | }
195 | }
196 |
197 | @Override
198 | protected void onArrival(NotificationEntry entry) {
199 | if (mView == null) {
200 | Log.w(TAG, "NotificationView not found.");
201 | onSendIgnored(entry);
202 | return;
203 | }
204 |
205 | if (!mView.hasCallback()) {
206 | if (DBG) Log.v(TAG, "set default NotificationViewCallback.");
207 | mView.setCallback(new ViewCallback());
208 | }
209 |
210 | if (!mView.isViewEnabled()) {
211 | if (DBG) Log.v(TAG, "NotificationView is currently disabled.");
212 | onSendIgnored(entry);
213 | return;
214 | }
215 |
216 | mView.onArrival(entry);
217 | }
218 |
219 | @Override
220 | protected void onUpdate(NotificationEntry entry) {
221 | if (mView == null) {
222 | Log.w(TAG, "NotificationView not found.");
223 | onUpdateIgnored(entry);
224 | return;
225 | }
226 |
227 | if (!mView.isViewEnabled()) {
228 | if (DBG) Log.v(TAG, "NotificationView is currently disabled.");
229 | onUpdateIgnored(entry);
230 | return;
231 | }
232 |
233 | mView.onUpdate(entry);
234 | }
235 |
236 | private final class ViewStateListener extends NotificationView.SimpleStateListener {
237 |
238 | @Override
239 | public void onViewTicking(NotificationView view) {
240 | if (DBG) Log.v(TAG, "onViewTicking");
241 | if (mBoard == null || !mBoard.isShowing()) {
242 | mWindow.attach();
243 | }
244 | }
245 |
246 | @Override
247 | public void onViewDismiss(NotificationView view) {
248 | if (DBG) Log.v(TAG, "onViewDismiss");
249 | if (mBoard == null || !mBoard.isShowing()) {
250 | mWindow.detach();
251 | }
252 | }
253 | }
254 |
255 | private final class BoardStateListener extends NotificationBoard.SimpleStateListener {
256 |
257 | @Override
258 | public void onBoardPrepare(NotificationBoard board) {
259 | if (DBG) Log.v(TAG, "onBoardPrepare");
260 | if (mView == null || !mView.isTicking()) {
261 | mWindow.attach();
262 | }
263 | mWindow.expand(true);
264 | }
265 |
266 | @Override
267 | public void onBoardEndOpen(NotificationBoard board) {
268 | if (DBG) Log.v(TAG, "onBoardEndOpen");
269 | }
270 |
271 | @Override
272 | public void onBoardEndClose(NotificationBoard board) {
273 | if (DBG) Log.v(TAG, "onBoardEndClose");
274 | if (mView == null || !mView.isTicking()) {
275 | mWindow.detach();
276 | }
277 | mWindow.expand(false);
278 | }
279 | }
280 |
281 | private final class NotificationWindow extends FrameLayout {
282 |
283 | private WindowManager mWindowManager;
284 | private WindowManager.LayoutParams mLayoutParams;
285 | private NotificationRootView mRoot;
286 | private NotificationBoard mBoard;
287 | private NotificationView mView;
288 | private boolean mAttached;
289 |
290 | NotificationWindow(Context context) {
291 | super(context);
292 |
293 | mWindowManager = (WindowManager)
294 | mContext.getSystemService(Context.WINDOW_SERVICE);
295 |
296 | mRoot = new NotificationRootView(mContext);
297 | addView(mRoot);
298 |
299 | IntentFilter filter = new IntentFilter();
300 | filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
301 | context.registerReceiver(mReceiver, filter);
302 | }
303 |
304 | void expand(boolean expand) {
305 | WindowManager.LayoutParams lp = getWindowLayoutParams();
306 | lp.height = expand ? WindowManager.LayoutParams.MATCH_PARENT :
307 | WindowManager.LayoutParams.WRAP_CONTENT;
308 | mWindowManager.updateViewLayout(this, lp);
309 | }
310 |
311 | private void attach() {
312 | if (!mAttached) mWindowManager.addView(this, getWindowLayoutParams());
313 | }
314 |
315 | private void detach() {
316 | if (mAttached) mWindowManager.removeView(this);
317 | }
318 |
319 | private void onBackKey() {
320 | mRoot.onBackKey();
321 | }
322 |
323 | private void onHomeKey() {
324 | mRoot.onHomeKey();
325 | }
326 |
327 | private WindowManager.LayoutParams getWindowLayoutParams() {
328 | if (mLayoutParams == null) {
329 | mLayoutParams = new WindowManager.LayoutParams();
330 | mLayoutParams.gravity = Gravity.TOP;
331 | mLayoutParams.setTitle(getClass().getSimpleName());
332 | mLayoutParams.packageName = mContext.getPackageName();
333 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
334 | mLayoutParams.format = PixelFormat.TRANSLUCENT;
335 | mLayoutParams.flags |=
336 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
337 | mLayoutParams.x = 0;
338 | mLayoutParams.y = 0;
339 | mLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
340 | mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
341 | }
342 | return mLayoutParams;
343 | }
344 |
345 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
346 |
347 | @Override
348 | public void onReceive(Context context, Intent intent) {
349 | if ("homekey".equals(intent.getStringExtra("reason"))) {
350 | onHomeKey();
351 | }
352 | }
353 | };
354 |
355 | @Override
356 | public boolean dispatchKeyEvent(KeyEvent event) {
357 | if (event.getKeyCode() == KeyEvent.KEYCODE_BACK &&
358 | event.getAction() == KeyEvent.ACTION_UP) {
359 | onBackKey();
360 | return true;
361 | }
362 | return super.dispatchKeyEvent(event);
363 | }
364 |
365 | @Override
366 | public void onAttachedToWindow() {
367 | super.onAttachedToWindow();
368 | if (DBG) Log.v(TAG, "attached.");
369 | mAttached = true;
370 | }
371 |
372 | @Override
373 | public void onDetachedFromWindow() {
374 | super.onDetachedFromWindow();
375 | if (DBG) Log.v(TAG, "detached.");
376 | mAttached = false;
377 | }
378 | }
379 |
380 | @Override public String toSimpleString() { return SIMPLE_NAME; }
381 | @Override public String toString() { return SIMPLE_NAME; }
382 | }
383 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.content.Context;
20 | import android.os.Handler;
21 | import android.os.Message;
22 | import android.os.Looper;
23 | import android.util.Log;
24 |
25 | import java.util.List;
26 |
27 | /**
28 | * Parent class of {@link NotificationLocal}, {@link NotificationGlobal} and {@link NotificationRemote}.
29 | */
30 | public class NotificationHandler extends Handler {
31 |
32 | public static boolean DBG;
33 | protected String TAG;
34 |
35 | public final int ID;
36 | protected Context mContext;
37 | protected NotificationCenter mCenter;
38 | protected NotificationEffect mEffect;
39 | protected boolean mEffectEnabled;
40 | protected boolean mEnabled = true;
41 |
42 | /* package */ NotificationHandler(Context context, int id, Looper looper) {
43 | super(looper != null ? looper : Looper.myLooper());
44 | mContext = context;
45 | ID = id;
46 | TAG = "zemin." + getClass().getSimpleName();
47 | if (DBG) Log.i(TAG, "init.");
48 | }
49 |
50 | final void setCenter(NotificationCenter center) {
51 | mCenter = center;
52 | mEffect = center.effect();
53 | }
54 |
55 | protected void onArrival(NotificationEntry entry) {
56 | }
57 |
58 | protected void onCancel(NotificationEntry entry) {
59 | }
60 |
61 | protected void onCancelAll() {
62 | }
63 |
64 | protected void onUpdate(NotificationEntry entry) {
65 | }
66 |
67 | /**
68 | * @return Context
69 | */
70 | public final Context getContext() {
71 | return mContext;
72 | }
73 |
74 | /**
75 | * Whether notification is disabled.
76 | *
77 | * @return boolean
78 | */
79 | public boolean isEnabled() {
80 | return mEnabled;
81 | }
82 |
83 | /**
84 | * Enable/disable notification.
85 | *
86 | * @param enable
87 | */
88 | public void setEnabled(boolean enable) {
89 | if (enable && !mEnabled) {
90 | if (DBG) Log.d(TAG, "Notification handler=" + ID + " is now enabled.");
91 | mEnabled = true;
92 | } else if (!enable && mEnabled) {
93 | if (DBG) Log.d(TAG, "Notification handler=" + ID + " is now disabled.");
94 | mEnabled = false;
95 | cancelAll();
96 | }
97 | }
98 |
99 | /**
100 | * Get the singleton object of {@link NotificationEffect}.
101 | *
102 | * @return NotificationEffect
103 | */
104 | public NotificationEffect effect() {
105 | return mEffect;
106 | }
107 |
108 | /**
109 | * Enable/disable notification effect.
110 | *
111 | * @param enable
112 | */
113 | public void enableEffect(boolean enable) {
114 | if (enable && !mEffectEnabled) {
115 | if (DBG) Log.d(TAG, "Notification effect is now enabled for handler=" + ID);
116 | mEffectEnabled = true;
117 | } else if (enable && !mEffectEnabled) {
118 | if (DBG) Log.d(TAG, "Notification effect is now disabled for handler=" + ID);
119 | mEffectEnabled = false;
120 | if (mEffect.isConsumer(ID)) {
121 | mEffect.cancel();
122 | }
123 | }
124 | }
125 |
126 | /**
127 | * Cancel notification effect.
128 | *
129 | * @param entry
130 | */
131 | public void cancelEffect(NotificationEntry entry) {
132 | entry.mEffectConsumers &= ~ID;
133 | }
134 |
135 | /**
136 | * Whether the notification with id exists.
137 | *
138 | * @param entryId
139 | * @return boolean
140 | */
141 | public boolean hasNotification(int entryId) {
142 | return mCenter.hasEntry(ID, entryId);
143 | }
144 |
145 | /**
146 | * Whether the notifications with tag exist.
147 | *
148 | * @param tag
149 | * @return boolean
150 | */
151 | public boolean hasNotifications(String tag) {
152 | return mCenter.hasEntries(ID, tag);
153 | }
154 |
155 | /**
156 | * Whether any notifications exist.
157 | *
158 | * @return boolean
159 | */
160 | public boolean hasNotifications() {
161 | return mCenter.hasEntries(ID);
162 | }
163 |
164 | /**
165 | * Get notification by its id.
166 | *
167 | * @param entryId
168 | * @return NotificationEntry
169 | */
170 | public NotificationEntry getNotification(int entryId) {
171 | return mCenter.getEntry(ID, entryId);
172 | }
173 |
174 | /**
175 | * Get notifications by its tag.
176 | *
177 | * @param tag
178 | * @param List
179 | */
180 | public List getNotifications(String tag) {
181 | return mCenter.getEntries(ID, tag);
182 | }
183 |
184 | /**
185 | * Get notifications.
186 | *
187 | * @return List
188 | */
189 | public List getNotifications() {
190 | return mCenter.mActives.getEntries(ID);
191 | }
192 |
193 | /**
194 | * Get notification count.
195 | *
196 | * @param tag
197 | * @return int
198 | */
199 | public int getNotificationCount(String tag) {
200 | return mCenter.mActives.getEntryCount(ID, tag);
201 | }
202 |
203 | /**
204 | * Get notification count.
205 | *
206 | * @return int
207 | */
208 | public int getNotificationCount() {
209 | return mCenter.mActives.getEntryCount(ID);
210 | }
211 |
212 | /**
213 | * Send notification.
214 | *
215 | * @param entry
216 | */
217 | public void send(NotificationEntry entry) {
218 | if (entry.isSentToTarget(ID)) {
219 | // final int targets = entry.mTargets;
220 | // entry.mTargets = ID;
221 | mCenter.send(entry);
222 | // entry.mTargets = targets;
223 | }
224 | }
225 |
226 | /**
227 | * Cancel notification by its id.
228 | *
229 | * @param entryId
230 | */
231 | public void cancel(int entryId) {
232 | NotificationEntry entry = mCenter.getEntry(ID, entryId);
233 | if (entry != null) {
234 | cancel(entry);
235 | }
236 | }
237 |
238 | /**
239 | * Cancel notifications by its tag.
240 | *
241 | * @param tag
242 | */
243 | public void cancel(String tag) {
244 | List entries = mCenter.getEntries(ID, tag);
245 | if (entries != null && !entries.isEmpty()) {
246 | for (NotificationEntry entry : entries) {
247 | cancel(entry);
248 | }
249 | }
250 | }
251 |
252 | /**
253 | * Cancel notification.
254 | *
255 | * @param entry
256 | */
257 | public void cancel(NotificationEntry entry) {
258 | if (entry.isSentToTarget(ID)) {
259 | // final int targets = entry.mTargets;
260 | // entry.mTargets = ID;
261 | mCenter.cancel(entry);
262 | // entry.mTargets = targets;
263 | }
264 | }
265 |
266 | /**
267 | * Cancel all notifications.
268 | */
269 | public void cancelAll() {
270 | if (DBG) Log.v(TAG, "prepare to cancel all");
271 | cancelSchedule(ARRIVE);
272 | schedule(CANCEL_ALL, 0, 0, null, 0);
273 | }
274 |
275 | void reportCanceled(NotificationEntry entry) {
276 | onCancelFinished(entry);
277 | mCenter.cancel(entry);
278 | }
279 |
280 | void onSendRequested(NotificationEntry entry) {
281 | if (entry.isSentToTarget(ID)) {
282 | if (mEnabled) {
283 | if (DBG) Log.v(TAG, "prepare to send - " + entry.ID);
284 | entry.mEffectConsumers |= ID;
285 | schedule(ARRIVE, 0, 0, entry, entry.delay);
286 | } else {
287 | onSendIgnored(entry);
288 | }
289 | }
290 | }
291 |
292 | void onUpdateRequested(NotificationEntry entry) {
293 | if (entry.isSentToTarget(ID)) {
294 | if (mEnabled) {
295 | if (DBG) Log.v(TAG, "prepare to update - " + entry.ID);
296 | entry.mEffectConsumers |= ID;
297 | schedule(UPDATE, 0, 0, entry, entry.delay);
298 | } else {
299 | onUpdateIgnored(entry);
300 | }
301 | }
302 | }
303 |
304 | void onCancelRequested(NotificationEntry entry) {
305 | if (entry.isSentToTarget(ID) && !entry.isCanceled(ID)) {
306 | if (DBG) Log.v(TAG, "prepare to cancel - " + entry.ID);
307 | schedule(CANCEL, 0, 0, entry, 0);
308 | }
309 | }
310 |
311 | void onSendFinished(NotificationEntry entry) {
312 | if (!entry.mSent) {
313 | if (DBG) Log.v(TAG, "send - " + entry.ID);
314 | playEffect(entry);
315 | entry.mFlag |= NotificationEntry.FLAG_SEND_FINISHED;
316 | updateEntryState(entry);
317 | }
318 | if (entry.mUpdate) {
319 | onUpdateFinished(entry);
320 | }
321 | }
322 |
323 | void onSendIgnored(NotificationEntry entry) {
324 | if (!entry.mSent) {
325 | if (DBG) Log.v(TAG, "ignore - " + entry.ID);
326 | entry.mEffectConsumers &= ~ID;
327 | entry.mFlag |= NotificationEntry.FLAG_SEND_IGNORED;
328 | entry.mIgnores |= ID;
329 | updateEntryState(entry);
330 | }
331 | if (entry.mUpdate) {
332 | onUpdateIgnored(entry);
333 | }
334 | }
335 |
336 | void onUpdateFinished(NotificationEntry entry) {
337 | if (DBG) Log.v(TAG, "update - " + entry.ID);
338 | playEffect(entry);
339 | entry.mFlag |= NotificationEntry.FLAG_UPDATE_FINISHED;
340 | entry.mUpdates |= ID;
341 | updateEntryState(entry);
342 | }
343 |
344 | void onUpdateIgnored(NotificationEntry entry) {
345 | if (DBG) Log.v(TAG, "ignore update - " + entry.ID);
346 | entry.mUpdates |= ID;
347 | }
348 |
349 | void onCancelFinished(NotificationEntry entry) {
350 | if (DBG) Log.v(TAG, "cancel - " + entry.ID);
351 | entry.mFlag |= NotificationEntry.FLAG_CANCEL_FINISHED;
352 | entry.mCancels |= ID;
353 | updateEntryState(entry);
354 | }
355 |
356 | void onCancelAllFinished() {
357 | mCenter.clearEntry(ID);
358 | }
359 |
360 | private void playEffect(NotificationEntry entry) {
361 | synchronized (mEffect.mLock) {
362 | if (mEffectEnabled) {
363 | if ((entry.mEffectConsumers & ID) != 0) {
364 | entry.mEffectConsumers = ID;
365 | mEffect.setConsumer(ID);
366 | mEffect.play(entry);
367 | }
368 | } else {
369 | entry.mEffectConsumers &= ~ID;
370 | }
371 | }
372 | }
373 |
374 | private void updateEntryState(NotificationEntry entry) {
375 | mCenter.updateEntryState(entry);
376 | }
377 |
378 | protected static final int ARRIVE = 0;
379 | protected static final int CANCEL = 1;
380 | protected static final int CANCEL_ALL = 2;
381 | protected static final int UPDATE = 3;
382 |
383 | protected void cancelSchedule(int what) {
384 | removeMessages(what);
385 | }
386 |
387 | protected void schedule(int what, int arg1, int arg2, Object obj, int delay) {
388 | sendMessageDelayed(obtainMessage(what, 0, 0, obj), delay);
389 | }
390 |
391 | protected void dispatchOnArrival(NotificationEntry entry) {
392 | updateEntryState(entry);
393 | onArrival(entry);
394 | updateEntryState(entry);
395 | }
396 |
397 | protected void dispatchOnCancel(NotificationEntry entry) {
398 | updateEntryState(entry);
399 | onCancel(entry);
400 | updateEntryState(entry);
401 | }
402 |
403 | protected void dispatchOnCancelAll() {
404 | if (mEffect.isConsumer(ID)) {
405 | mEffect.cancel();
406 | }
407 | onCancelAll();
408 | }
409 |
410 | protected void dispatchOnUpdate(NotificationEntry entry) {
411 | updateEntryState(entry);
412 | onUpdate(entry);
413 | updateEntryState(entry);
414 | }
415 |
416 | @Override
417 | public void handleMessage(Message msg) {
418 | switch (msg.what) {
419 | case ARRIVE:
420 | dispatchOnArrival((NotificationEntry) msg.obj);
421 | break;
422 | case CANCEL:
423 | dispatchOnCancel((NotificationEntry) msg.obj);
424 | break;
425 | case CANCEL_ALL:
426 | dispatchOnCancelAll();
427 | break;
428 | case UPDATE:
429 | dispatchOnUpdate((NotificationEntry) msg.obj);
430 | break;
431 | }
432 | }
433 |
434 | public String toSimpleString() { return null; }
435 | }
436 |
--------------------------------------------------------------------------------
/core/src/zemin/notification/NotificationRootView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Zemin Liu
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package zemin.notification;
18 |
19 | import android.animation.Animator;
20 | import android.content.Context;
21 | import android.util.AttributeSet;
22 | import android.util.Log;
23 | import android.view.View;
24 | import android.view.GestureDetector;
25 | import android.view.MotionEvent;
26 | import android.widget.FrameLayout;
27 | import android.support.v4.view.GestureDetectorCompat;
28 |
29 | /**
30 | * Root view handling both {@link NotificationView} and {@link NotificationBoard}.
31 | */
32 | public class NotificationRootView extends FrameLayout
33 | implements GestureDetector.OnGestureListener,
34 | GestureDetector.OnDoubleTapListener {
35 |
36 | private static final String TAG = "zemin.NotificationRootView";
37 | public static boolean DBG;
38 |
39 | private NotificationView mView;
40 | private NotificationBoard mBoard;
41 | private GestureDetectorCompat mGestureDetector;
42 | private GestureDetector.OnGestureListener mGestureConsumer;
43 |
44 | public NotificationRootView(Context context) {
45 | super(context);
46 | }
47 |
48 | public NotificationRootView(Context context, AttributeSet attrs) {
49 | super(context, attrs);
50 | }
51 |
52 | /**
53 | * Get notification view.
54 | *
55 | * @param NotificationView
56 | */
57 | public NotificationView getView() {
58 | return mView;
59 | }
60 |
61 | /**
62 | * Get notification board.
63 | *
64 | * @param NotificationBoard
65 | */
66 | public NotificationBoard getBoard() {
67 | return mBoard;
68 | }
69 |
70 | /**
71 | * Enable/disable notification view.
72 | *
73 | * @param enable
74 | */
75 | public void setViewEnabled(boolean enable) {
76 | if (enable && mView == null) {
77 | mView = makeView();
78 | }
79 | if (mView != null) {
80 | mView.setViewEnabled(enable);
81 | }
82 | }
83 |
84 | /**
85 | * Enable/disable notification board.
86 | *
87 | * @param enable
88 | */
89 | public void setBoardEnabled(boolean enable) {
90 | if (enable && mBoard == null) {
91 | mBoard = makeBoard();
92 | }
93 | if (mBoard != null) {
94 | mBoard.setBoardEnabled(enable);
95 | }
96 | }
97 |
98 | /**
99 | * Dismiss notification view.
100 | */
101 | public void dismissView() {
102 | if (mView != null) {
103 | mView.dismiss();
104 | }
105 | }
106 |
107 | /**
108 | * Open notification board.
109 | */
110 | public void openBoard() {
111 | if (mBoard != null) {
112 | mBoard.open(true);
113 | }
114 | }
115 |
116 | /**
117 | * Close notification board.
118 | */
119 | public void closeBoard() {
120 | if (mBoard != null) {
121 | mBoard.close(true);
122 | }
123 | }
124 |
125 | private NotificationView makeView() {
126 | NotificationView view = new NotificationView(getContext());
127 | view.setClipChildren(false);
128 | view.setClipToPadding(false);
129 | // view.setDismissOnHomeKey(true);
130 | int index = getChildCount() > 1 ? 1 : 0;
131 | addView(view, index, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
132 | return view;
133 | }
134 |
135 | private NotificationBoard makeBoard() {
136 | NotificationBoard board = new NotificationBoard(getContext());
137 | BoardListener listener = new BoardListener();
138 | board.addStateListener(listener);
139 | board.setVisibility(GONE);
140 | addView(board, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
141 | return board;
142 | }
143 |
144 | @Override
145 | public boolean onInterceptTouchEvent(MotionEvent event) {
146 | boolean intercept = false;
147 | if (mBoard != null && !mBoard.isShowing()) {
148 | intercept = true;
149 | }
150 | return intercept;
151 | }
152 |
153 | @Override
154 | public boolean onTouchEvent(MotionEvent event) {
155 | if (mBoard != null) {
156 | if (mBoard.isOpened()) {
157 | return mBoard.onTouchEvent(event);
158 | }
159 | } else if (mView != null) {
160 | return mView.onTouchEvent(event);
161 | }
162 |
163 | if (mGestureDetector == null) {
164 | mGestureDetector = new GestureDetectorCompat(getContext(), this);
165 | }
166 |
167 | boolean handled = mGestureDetector.onTouchEvent(event);
168 | switch (event.getAction()) {
169 | case MotionEvent.ACTION_UP:
170 | case MotionEvent.ACTION_CANCEL:
171 | onUpOrCancel(event, handled);
172 | break;
173 | }
174 |
175 | return handled ? handled : super.onTouchEvent(event);
176 | }
177 |
178 | @Override
179 | public boolean onDown(MotionEvent event) {
180 | mGestureConsumer = null;
181 | if (mBoard != null && mBoard.isBoardEnabled()) {
182 | if (mView != null && mView.isViewEnabled() && mView.isTicking()) {
183 | mBoard.setInitialTouchArea(
184 | mView.getLeft(), mView.getTop(), mView.getRight(), mView.getBottom());
185 | } else {
186 | mBoard.setInitialTouchArea(0, 0, 0, 0);
187 | }
188 | mBoard.onDown(event);
189 | }
190 | if (mView != null && mView.isViewEnabled()) {
191 | mView.onDown(event);
192 | }
193 | return true;
194 | }
195 |
196 | @Override
197 | public void onShowPress(MotionEvent event) {
198 | if (mView != null && mView.isViewEnabled()) {
199 | mView.onShowPress(event);
200 | }
201 | }
202 |
203 | @Override
204 | public boolean onSingleTapUp(MotionEvent event) {
205 | return mView != null && mView.isViewEnabled() ?
206 | mView.onSingleTapUp(event) : false;
207 | }
208 |
209 | @Override
210 | public boolean onSingleTapConfirmed(MotionEvent event) {
211 | return mView != null && mView.isViewEnabled() ?
212 | mView.onSingleTapConfirmed(event) : false;
213 | }
214 |
215 | @Override
216 | public boolean onDoubleTap(MotionEvent event) {
217 | return mView != null && mView.isViewEnabled() ?
218 | mView.onDoubleTap(event) : false;
219 | }
220 |
221 | @Override
222 | public boolean onDoubleTapEvent(MotionEvent event) {
223 | return mView != null && mView.isViewEnabled() ?
224 | mView.onDoubleTapEvent(event) : false;
225 | }
226 |
227 | @Override
228 | public void onLongPress(MotionEvent event) {
229 | if (mView != null && mView.isViewEnabled()) {
230 | mView.onLongPress(event);
231 | }
232 | }
233 |
234 | @Override
235 | public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) {
236 | if (mGestureConsumer != null) {
237 | return mGestureConsumer.onScroll(event1, event2, distanceX, distanceY);
238 | }
239 | if (mBoard != null && mBoard.isBoardEnabled() && mBoard.onScroll(event1, event2, distanceX, distanceY)) {
240 | mGestureConsumer = mBoard;
241 | return true;
242 | }
243 | if (mView != null && mView.isViewEnabled() && mView.onScroll(event1, event2, distanceX, distanceY)) {
244 | mGestureConsumer = mView;
245 | return true;
246 | }
247 | return false;
248 | }
249 |
250 | @Override
251 | public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
252 | if (mGestureConsumer != null) {
253 | return mGestureConsumer.onFling(event1, event2, velocityX, velocityY);
254 | }
255 | if (mBoard != null && mBoard.isBoardEnabled() && mBoard.onFling(event1, event2, velocityX, velocityY)) {
256 | return true;
257 | }
258 | if (mView != null && mView.isViewEnabled() && mView.onFling(event1, event2, velocityX, velocityY)) {
259 | return true;
260 | }
261 | return false;
262 | }
263 |
264 | private void onUpOrCancel(MotionEvent event, boolean handled) {
265 | mGestureConsumer = null;
266 | if (mBoard != null && mBoard.isBoardEnabled()) {
267 | mBoard.onUpOrCancel(event, handled);
268 | }
269 | if (mView != null && mView.isViewEnabled()) {
270 | mView.onUpOrCancel(event, handled);
271 | }
272 | }
273 |
274 | public void onBackKey() {
275 | if (mBoard != null) {
276 | mBoard.onBackKey();
277 | }
278 | if (mView != null) {
279 | mView.onBackKey();
280 | }
281 | }
282 |
283 | public void onHomeKey() {
284 | if (mBoard != null) {
285 | mBoard.onHomeKey();
286 | }
287 | if (mView != null) {
288 | mView.onHomeKey();
289 | }
290 | }
291 |
292 | private class BoardListener extends NotificationBoard.SimpleStateListener {
293 |
294 | @Override
295 | public void onBoardPrepare(NotificationBoard board) {
296 | if (mView != null) {
297 | mView.setViewEnabled(false);
298 | }
299 | }
300 |
301 | @Override
302 | public void onBoardStartOpen(NotificationBoard board) {
303 | if (mView != null && mView.isTicking()) {
304 | mView.pause();
305 | mView.animateContentViewRotationX(
306 | -90.0f, 0.0f, mViewDismissAnimatorListener, board.getOpenTransitionTime());
307 | }
308 | }
309 |
310 | @Override
311 | public void onBoardStartClose(NotificationBoard board) {
312 | if (mView != null) {
313 | mView.setViewEnabled(true);
314 | if (mView.isTicking()) {
315 | if (mView.getContentViewRotationX() < -90.0f) {
316 | mView.setContentViewRotationX(-90.0f);
317 | }
318 | mView.setContentViewVisibility(VISIBLE);
319 | mView.animateContentViewRotationX(
320 | 0.0f, 1.0f, mViewShowAnimatorListener, board.getCloseTransitionTime());
321 | }
322 | }
323 | }
324 |
325 | @Override
326 | public void onBoardEndClose(NotificationBoard board) {
327 | if (mView != null) {
328 | mView.resume();
329 | }
330 | }
331 |
332 | @Override
333 | public void onBoardTranslationY(NotificationBoard board, float y) {
334 | if (mView != null && mView.isTicking()) {
335 | final float destR = -90.0f - y * 90.0f / board.getBoardHeight();
336 | if (destR > 0.0f) {
337 | return;
338 | }
339 |
340 | if (mView.getContentViewRotationX() == 0.0f) {
341 | mView.pause();
342 | mView.setContentViewPivotY(0.0f);
343 | }
344 |
345 | if (destR < -90.0f) {
346 | if (mView.isPaused() && mView.isContentViewShown()) {
347 | mView.setContentViewVisibility(INVISIBLE);
348 | }
349 | } else {
350 | if (!mView.isContentViewShown()) {
351 | mView.setContentViewVisibility(VISIBLE);
352 | }
353 | }
354 |
355 | float alpha = Utils.getAlphaForOffset(1.0f, 0.0f, 0.0f, -90.0f, destR);
356 |
357 | mView.setContentViewRotationX(destR);
358 | mView.setContentViewAlpha(alpha);
359 | }
360 | }
361 | }
362 |
363 | private final AnimatorListener mViewDismissAnimatorListener = new AnimatorListener() {
364 |
365 | @Override
366 | public void onAnimationStart(Animator animation) {
367 | if (DBG) Log.v(TAG, "view dismiss start");
368 | }
369 |
370 | @Override
371 | public void onAnimationEnd(Animator animation) {
372 | if (DBG) Log.v(TAG, "view dismiss end");
373 | mView.sendPendings();
374 | mView.dismiss();
375 | }
376 | };
377 |
378 | private final AnimatorListener mViewShowAnimatorListener = new AnimatorListener() {
379 |
380 | @Override
381 | public void onAnimationStart(Animator animation) {
382 | if (DBG) Log.v(TAG, "view show start");
383 | }
384 |
385 | @Override
386 | public void onAnimationEnd(Animator animation) {
387 | if (DBG) Log.v(TAG, "view show end");
388 | mView.resume();
389 | }
390 | };
391 | }
392 |
--------------------------------------------------------------------------------