From 6ebc807a92255caeed3db792e1c1e873741b1800 Mon Sep 17 00:00:00 2001 From: Piruin Panichphol Date: Thu, 10 Nov 2016 16:57:35 +0700 Subject: [PATCH] Doggy can work now --- build.gradle | 30 +-- doggy-activity-sample/build.gradle | 46 ++--- .../src/main/AndroidManifest.xml | 28 +-- .../doggyactivity/sample/SampleActivity.java | 36 +++- .../src/main/res/layout/activity_sample.xml | 31 +-- .../src/main/res/values-w820dp/dimens.xml | 8 +- .../src/main/res/values/colors.xml | 6 +- .../src/main/res/values/dimens.xml | 6 +- .../src/main/res/values/strings.xml | 2 +- .../src/main/res/values/styles.xml | 14 +- doggy-activity/build.gradle | 58 +++--- doggy-activity/src/main/AndroidManifest.xml | 4 +- .../piruin/doggyactivity/DoggyActivity.java | 190 +++++++++++++++++- .../piruin/doggyactivity/DoggyCallback.java | 14 ++ .../src/main/res/values/strings.xml | 3 - 15 files changed, 348 insertions(+), 128 deletions(-) create mode 100644 doggy-activity/src/main/java/me/piruin/doggyactivity/DoggyCallback.java delete mode 100644 doggy-activity/src/main/res/values/strings.xml diff --git a/build.gradle b/build.gradle index 5e36af0..6bf09f5 100644 --- a/build.gradle +++ b/build.gradle @@ -4,27 +4,27 @@ version = '0.1' buildscript { - repositories { - jcenter() - } - dependencies { - classpath 'com.android.tools.build:gradle:2.2.2' - } + repositories { + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:2.2.2' + } } allprojects { - repositories { - jcenter() - } + repositories { + jcenter() + } } ext { - JAVA_VERSION = JavaVersion.VERSION_1_7 + JAVA_VERSION = JavaVersion.VERSION_1_7 - ANDROID_MIN_SDK = 19 - ANDROID_TARGET_SDK = 25 - ANDROID_BUILD_TOOLS = "25.0.0" - ANDROID_SUPPORT_VERSION = "25.0.0" + ANDROID_MIN_SDK = 19 + ANDROID_TARGET_SDK = 25 + ANDROID_BUILD_TOOLS = "25.0.0" + ANDROID_SUPPORT_VERSION = "25.0.0" - githubRepo = 'piruin/doggy-activity' + githubRepo = 'piruin/doggy-activity' } diff --git a/doggy-activity-sample/build.gradle b/doggy-activity-sample/build.gradle index 630a05f..64a7470 100644 --- a/doggy-activity-sample/build.gradle +++ b/doggy-activity-sample/build.gradle @@ -1,34 +1,34 @@ apply plugin: 'com.android.application' android { - compileSdkVersion ANDROID_TARGET_SDK - buildToolsVersion ANDROID_BUILD_TOOLS + compileSdkVersion ANDROID_TARGET_SDK + buildToolsVersion ANDROID_BUILD_TOOLS - defaultConfig { - applicationId "me.piruin.doggyactivity.sample" - minSdkVersion ANDROID_MIN_SDK - targetSdkVersion ANDROID_TARGET_SDK - versionCode 1 - versionName rootProject.version - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - } + defaultConfig { + applicationId "me.piruin.doggyactivity.sample" + minSdkVersion ANDROID_MIN_SDK + targetSdkVersion ANDROID_TARGET_SDK + versionCode 1 + versionName rootProject.version + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } - compileOptions { - sourceCompatibility JAVA_VERSION - targetCompatibility JAVA_VERSION - } + compileOptions { + sourceCompatibility JAVA_VERSION + targetCompatibility JAVA_VERSION + } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } + } } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile project(':doggy-activity') - compile "com.android.support:appcompat-v7:$ANDROID_SUPPORT_VERSION" - testCompile 'junit:junit:4.12' + compile fileTree(dir: 'libs', include: ['*.jar']) + compile project(':doggy-activity') + compile "com.android.support:appcompat-v7:$ANDROID_SUPPORT_VERSION" + testCompile 'junit:junit:4.12' } diff --git a/doggy-activity-sample/src/main/AndroidManifest.xml b/doggy-activity-sample/src/main/AndroidManifest.xml index b7bb4d0..bf1557e 100644 --- a/doggy-activity-sample/src/main/AndroidManifest.xml +++ b/doggy-activity-sample/src/main/AndroidManifest.xml @@ -1,20 +1,20 @@ + package="me.piruin.doggyactivity.sample"> - - - - + + + + - - - - + + + + diff --git a/doggy-activity-sample/src/main/java/me/piruin/doggyactivity/sample/SampleActivity.java b/doggy-activity-sample/src/main/java/me/piruin/doggyactivity/sample/SampleActivity.java index fb852e2..94731fc 100644 --- a/doggy-activity-sample/src/main/java/me/piruin/doggyactivity/sample/SampleActivity.java +++ b/doggy-activity-sample/src/main/java/me/piruin/doggyactivity/sample/SampleActivity.java @@ -1,13 +1,35 @@ package me.piruin.doggyactivity.sample; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.widget.Toast; +import me.piruin.doggyactivity.DoggyActivity; -public class SampleActivity extends AppCompatActivity { +public class SampleActivity extends DoggyActivity { + + @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_sample); + } + + @Override public void onScreenOff() { + super.onScreenOff(); + Toast.makeText(this, "Who turn of the light", Toast.LENGTH_SHORT).show(); + } + + @Override public void onScreenOn() { + super.onScreenOn(); + Toast.makeText(this, "Screen on Again", Toast.LENGTH_SHORT).show(); + } + + @Override public void onUserLeave(boolean systemInterrupt) { + super.onUserLeave(systemInterrupt); + Toast.makeText(this, "User Leave", Toast.LENGTH_SHORT).show(); + + } + + @Override public void onUserBack() { + super.onUserBack(); + Toast.makeText(this, "User is Back, ha ha", Toast.LENGTH_SHORT).show(); + } - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_sample); - } } diff --git a/doggy-activity-sample/src/main/res/layout/activity_sample.xml b/doggy-activity-sample/src/main/res/layout/activity_sample.xml index 4d25dbf..6018df9 100644 --- a/doggy-activity-sample/src/main/res/layout/activity_sample.xml +++ b/doggy-activity-sample/src/main/res/layout/activity_sample.xml @@ -1,18 +1,19 @@ - + - + diff --git a/doggy-activity-sample/src/main/res/values-w820dp/dimens.xml b/doggy-activity-sample/src/main/res/values-w820dp/dimens.xml index 63fc816..1e656d6 100644 --- a/doggy-activity-sample/src/main/res/values-w820dp/dimens.xml +++ b/doggy-activity-sample/src/main/res/values-w820dp/dimens.xml @@ -1,6 +1,6 @@ - - 64dp + + 64dp diff --git a/doggy-activity-sample/src/main/res/values/colors.xml b/doggy-activity-sample/src/main/res/values/colors.xml index 3ab3e9c..5a077b3 100644 --- a/doggy-activity-sample/src/main/res/values/colors.xml +++ b/doggy-activity-sample/src/main/res/values/colors.xml @@ -1,6 +1,6 @@ - #3F51B5 - #303F9F - #FF4081 + #3F51B5 + #303F9F + #FF4081 diff --git a/doggy-activity-sample/src/main/res/values/dimens.xml b/doggy-activity-sample/src/main/res/values/dimens.xml index 47c8224..acf94cc 100644 --- a/doggy-activity-sample/src/main/res/values/dimens.xml +++ b/doggy-activity-sample/src/main/res/values/dimens.xml @@ -1,5 +1,5 @@ - - 16dp - 16dp + + 16dp + 16dp diff --git a/doggy-activity-sample/src/main/res/values/strings.xml b/doggy-activity-sample/src/main/res/values/strings.xml index f8f69a2..941f39e 100644 --- a/doggy-activity-sample/src/main/res/values/strings.xml +++ b/doggy-activity-sample/src/main/res/values/strings.xml @@ -1,3 +1,3 @@ - DoggyActivity + DoggyActivity diff --git a/doggy-activity-sample/src/main/res/values/styles.xml b/doggy-activity-sample/src/main/res/values/styles.xml index 5885930..705be27 100644 --- a/doggy-activity-sample/src/main/res/values/styles.xml +++ b/doggy-activity-sample/src/main/res/values/styles.xml @@ -1,11 +1,11 @@ - - + + diff --git a/doggy-activity/build.gradle b/doggy-activity/build.gradle index 367775c..e7094a3 100644 --- a/doggy-activity/build.gradle +++ b/doggy-activity/build.gradle @@ -1,42 +1,42 @@ apply plugin: 'com.android.library' android { - compileSdkVersion ANDROID_TARGET_SDK - buildToolsVersion ANDROID_BUILD_TOOLS + compileSdkVersion ANDROID_TARGET_SDK + buildToolsVersion ANDROID_BUILD_TOOLS - defaultConfig { - minSdkVersion ANDROID_MIN_SDK - targetSdkVersion ANDROID_TARGET_SDK - } + defaultConfig { + minSdkVersion ANDROID_MIN_SDK + targetSdkVersion ANDROID_TARGET_SDK + } - compileOptions { - sourceCompatibility JAVA_VERSION - targetCompatibility JAVA_VERSION - } + compileOptions { + sourceCompatibility JAVA_VERSION + targetCompatibility JAVA_VERSION + } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } + } - lintOptions { - abortOnError true - checkAllWarnings true - warningsAsErrors true - } + lintOptions { + abortOnError true + checkAllWarnings true + warningsAsErrors true + } - packagingOptions { - exclude 'LICENSE.txt' - exclude 'META-INF/DEPENDENCIES' - exclude 'META-INF/ASL2.0' - exclude 'META-INF/NOTICE' - exclude 'META-INF/LICENSE' - } + packagingOptions { + exclude 'LICENSE.txt' + exclude 'META-INF/DEPENDENCIES' + exclude 'META-INF/ASL2.0' + exclude 'META-INF/NOTICE' + exclude 'META-INF/LICENSE' + } } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile "com.android.support:appcompat-v7:$ANDROID_SUPPORT_VERSION" + compile fileTree(dir: 'libs', include: ['*.jar']) + compile "com.android.support:appcompat-v7:$ANDROID_SUPPORT_VERSION" } diff --git a/doggy-activity/src/main/AndroidManifest.xml b/doggy-activity/src/main/AndroidManifest.xml index 89ceb9c..e933e15 100644 --- a/doggy-activity/src/main/AndroidManifest.xml +++ b/doggy-activity/src/main/AndroidManifest.xml @@ -1,3 +1 @@ - - + diff --git a/doggy-activity/src/main/java/me/piruin/doggyactivity/DoggyActivity.java b/doggy-activity/src/main/java/me/piruin/doggyactivity/DoggyActivity.java index e67376e..89df1d8 100644 --- a/doggy-activity/src/main/java/me/piruin/doggyactivity/DoggyActivity.java +++ b/doggy-activity/src/main/java/me/piruin/doggyactivity/DoggyActivity.java @@ -1,6 +1,194 @@ package me.piruin.doggyactivity; +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Bundle; import android.support.v7.app.AppCompatActivity; -public class DoggyActivity extends AppCompatActivity { +public class DoggyActivity extends AppCompatActivity implements DoggyCallback { + + protected static final String USER_LEAVE = "isUserLeave"; + + protected boolean userLeave = false; + protected boolean userLeaveByThemself = false; + protected boolean startActivity = false; + protected boolean createDescription = false; + protected boolean screenReceiverRegisted = false; + protected BroadcastReceiver screenReceiver; + protected IntentFilter screenActionFilter; + protected DoggyCallback doggyCallback = this; + protected Bundle saveState; + + @Override protected void onCreate(Bundle savedInstanceState) { + //Log.d(TAG, "OnCreate()"); + + super.onCreate(savedInstanceState); + + if (savedInstanceState != null) { + userLeave = savedInstanceState.getBoolean(USER_LEAVE); + } + } + + @Override protected void onStart() { + //Log.d(TAG, "Onstart()"); + + if (!screenReceiverRegisted) { + screenReceiverRegisted = true; + IntentFilter screenActionFilter = new IntentFilter(); + screenActionFilter.addAction(Intent.ACTION_SCREEN_ON); + screenActionFilter.addAction(Intent.ACTION_SCREEN_OFF); + screenReceiver = new ScreenActionReceiver(); + registerReceiver(screenReceiver, screenActionFilter); + } + super.onStart(); + } + + @Override protected void onResume() { + //Log.d(TAG, "OnResume()"); + + startActivity = false; + userLeaveByThemself = false; + createDescription = false; + + super.onResume(); + } + + @Override public void onWindowFocusChanged(boolean hasFocus) { + //Log.d(TAG, "onWindowFocusChanged()"); + + if (hasFocus && userLeave && (doggyCallback != null)) { + doggyCallback.onUserBack(); + userLeave = false; + } + super.onWindowFocusChanged(hasFocus); + } + + @Override protected void onUserLeaveHint() { + //Log.d(TAG, "OnUserLeaveHint()"); + + userLeaveByThemself = true; + super.onUserLeaveHint(); + } + + @Override protected void onSaveInstanceState(Bundle outState) { + //Log.d(TAG, "onSaveInstanceState()"); + + //Copy outState's Pointer for Save Instance State in other method + super.onSaveInstanceState(outState); + saveState = outState; + } + + @Override public CharSequence onCreateDescription() { + //Log.d(TAG, "onCreateDescription()"); + + createDescription = true; + return super.onCreateDescription(); + } + + @Override protected void onStop() { + //Log.d(TAG, "OnStop()"); + + if (!startActivity && createDescription) { + /* + * User Leave this activity by somehow suck as Press Home Button, + * Press Notification in Notification bar Or Phone incoming + */ + userLeave = true; + if (saveState != null) + saveState.putBoolean(USER_LEAVE, userLeave); + + if (doggyCallback != null) { + boolean systemInterrupt = !userLeaveByThemself; + doggyCallback.onUserLeave(systemInterrupt); + /* + * If user leave this activity by Call in or Something that was + * not choice of user then call onSystemInterrupt() for more + * flexible + */ + if (systemInterrupt) + doggyCallback.onSystemInterrupt(); + } + } else { + + /* + * If start other Activity we must unregister ScreenReceiver before; + */ + unregisterReceiver(screenReceiver); + screenReceiverRegisted = false; + //Log.w(TAG, "UTA : Unregistered ScreenReceiver!"); + } + + super.onStop(); + } + + @Override protected void onDestroy() { + //Log.d(TAG, "OnDestroy()"); + /* + * if ScreenReceiver not unregister yet, do it here. + */ + if (screenReceiverRegisted) + unregisterReceiver(screenReceiver); + /* + * Avoid cause of memory leak + */ + saveState = null; + screenReceiver = null; + screenActionFilter = null; + doggyCallback = null; + super.onDestroy(); + } + + @Override public void startActivity(Intent intent) { + startActivity = true; + super.startActivity(intent); + } + + @Override public void startActivityForResult(Intent intent, int requestCode) { + startActivity = true; + super.startActivityForResult(intent, requestCode); + } + + @Override public void startActivityFromChild( + Activity child, Intent intent, int requestCode) + { + startActivity = true; + super.startActivityFromChild(child, intent, requestCode); + } + + @Override public void onScreenOn() { + + } + + @Override public void onScreenOff() { + + } + + @Override public void onUserLeave(boolean systemInterrupt) { + + } + + @Override public void onSystemInterrupt() { + + } + + @Override public void onUserBack() { + + } + + private class ScreenActionReceiver extends BroadcastReceiver { + + @Override public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { + userLeave = true; + if (doggyCallback != null) + doggyCallback.onScreenOff(); + } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { + if (doggyCallback != null) + doggyCallback.onScreenOn(); + } + } + } } diff --git a/doggy-activity/src/main/java/me/piruin/doggyactivity/DoggyCallback.java b/doggy-activity/src/main/java/me/piruin/doggyactivity/DoggyCallback.java new file mode 100644 index 0000000..443f5a9 --- /dev/null +++ b/doggy-activity/src/main/java/me/piruin/doggyactivity/DoggyCallback.java @@ -0,0 +1,14 @@ +package me.piruin.doggyactivity; + +interface DoggyCallback { + + void onScreenOn(); + + void onScreenOff(); + + void onUserLeave(boolean systemInterrupt); + + void onSystemInterrupt(); + + void onUserBack(); +} diff --git a/doggy-activity/src/main/res/values/strings.xml b/doggy-activity/src/main/res/values/strings.xml deleted file mode 100644 index fea017a..0000000 --- a/doggy-activity/src/main/res/values/strings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - Doggy-Activity -