T
- The type of objects for which callbacks should be proceededpublic abstract class BaseCallbacks<T> extends Object
BaseCallbacks
is the one of the base classes for working with callbacks. Most implementations should not
derive directly from it, but instead inherit from
BaseActivityLifecycleProceed.BaseActivityCallbacks
or
BaseFragmentLifecycleProceed.BaseFragmentCallbacks
.
Since the BaseCallbacks
is a base class for callbacks handlers, the instances of it should be registered
(it's close to the process of registering Application.ActivityLifecycleCallbacks
).
The low-level registration goes via call to BaseProceed.register
, but most implementations
should do it using BaseActivityLifecycleProceed.register
or
BaseFragmentLifecycleProceed.register
.
After registering, the implemented callbacks are ready to be called for every object of type T.
To proceed callbacks the object should be annotated with Callbacks
or CallbacksInherited
.
Another possibility (to proceed all objects) is to set the "force proceed" flag
(see setForceProceed(boolean)
) to true
.
Usage example (creating callback handler for Activity.onActionModeStarted
;
for more examples please refer to Activity
,
Fragment
and
simple Activity
ones):
package com.mypackage; import akha.yakhont.callback.lifecycle.BaseActivityLifecycleProceed; public class MyCallbacks extends BaseCallbacks.BaseActivityCallbacks { private static final MyCallbacks sInstance = new MyCallbacks(); private MyCallbacks() { BaseActivityLifecycleProceed.register(this); } public static void myHandler(Activity activity, ActionMode mode) { // proceed annotated Activities only if (!BaseCallbacks.BaseProceed.proceed(sInstance, activity)) return; // your code here (NOTE: you don't have to call activity.onActionModeStarted() - // it's already done by the Weaver) } }Annotate necessary Activities:
import akha.yakhont.callback.annotation.CallbacksInherited; @CallbacksInherited(com.mypackage.MyCallbacks.class) public class MyActivity extends Activity { ... }And add the following line to the
weaver.config
(which says to the Yakhont Weaver to insert the call to
MyCallbacks.myHandler()
in the compiled Activity.onActionModeStarted()
method body - at the beginning or at the end, depending on the 2nd parameter - see below):
android.app.Activity.onActionModeStarted before 'com.mypackage.MyCallbacks.myHandler($0, $$);'Here $0 means 'this', $$ - the list of method arguments (for more info please visit the Javassist site, in particular, Inserting source text at the beginning/end of a method body).
The line in weaver.config
should contain 3 tokens delimited by whitespaces:
<fully qualified class name>.method[(signature)] <action> 'code'
The possible actions are:
Some other base classes for working with callbacks are BaseCallbacks.BaseProceed
, BaseCallbacks.BaseLifecycleProceed
,
BaseCallbacks.BaseCacheCallbacks
and annotations in the akha.yakhont.callback.annotation
package.
Modifier and Type | Class and Description |
---|---|
static class |
BaseCallbacks.BaseCacheCallbacks<T>
Extends the
BaseCallbacks class to provide cache (of already checked objects) support. |
static class |
BaseCallbacks.BaseLifecycleProceed
Extends the
BaseCallbacks.BaseProceed class to provide the base object's lifecycle support. |
static class |
BaseCallbacks.BaseProceed
The
BaseProceed is intended for support of BaseCallbacks instances registration. |
static interface |
BaseCallbacks.Validator
The callbacks annotations validation API.
|
Constructor and Description |
---|
BaseCallbacks()
Initialises a newly created
BaseCallbacks object. |
Modifier and Type | Method and Description |
---|---|
protected void |
onRegister()
The callback which is called when the instance of this class registered.
|
protected void |
onUnregister()
The callback which is called when the instance of this class unregistered.
|
protected boolean |
proceed(T object)
Checks whether the callbacks for the given object should be proceeded or not.
|
static <T> boolean |
proceed(T object,
Class callbackClass)
Checks whether the callbacks for the given object should be proceeded or not.
|
static <T> boolean |
proceed(T object,
Class callbackClass,
boolean forceProceed,
Map<T,Boolean> cache)
Checks whether the callbacks for the given object should be proceeded or not.
|
protected boolean |
proceed(T object,
Map<T,Boolean> cache)
Checks whether the callbacks for the given object should be proceeded or not.
|
BaseCallbacks |
setForceProceed(boolean forceProceed)
Sets the "force proceed" flag.
|
public BaseCallbacks()
BaseCallbacks
object.public BaseCallbacks setForceProceed(boolean forceProceed)
true
it forces to proceed callbacks for all objects of type T.
false
.forceProceed
- The value to setBaseCallbacks
objectprotected boolean proceed(@NonNull T object)
object
- The object for which callbacks could be proceededtrue
if callbacks should be proceeded, false
otherwiseprotected boolean proceed(@NonNull T object, Map<T,Boolean> cache)
object
- The object for which callbacks could be proceededcache
- The cache of already checked objectstrue
if callbacks should be proceeded, false
otherwisepublic static <T> boolean proceed(@NonNull T object, @NonNull Class callbackClass)
Usage example (creating callback handler for Service.onStartCommand
;
for more examples please refer to Activity
,
Fragment
and
general Activity
ones):
package com.mypackage; public class MyCallbacks { // you can create new class or add handler(s) to the existing one public static void myHandler(Service service, Intent intent, int flags, int startId) { // proceed annotated Services only if (!BaseCallbacks.proceed(service, MyCallbacks.class)) return; // your code here (NOTE: you don't have to call service.onStartCommand() - // it's already done by the Weaver) } }Annotate necessary Services:
import akha.yakhont.callback.annotation.CallbacksInherited; @CallbacksInherited(com.mypackage.MyCallbacks.class) public class MyService extends Service { ... }And add the following line to the
weaver.config
:
android.app.Service.onStartCommand true 'com.mypackage.MyCallbacks.myHandler($0, $$);'Please refer to the
BaseCallbacks
for more details.T
- The type of object for which callbacks should be proceededobject
- The object for which callbacks could be proceededcallbackClass
- The class of the callbacks handlertrue
if callbacks should be proceeded, false
otherwisepublic static <T> boolean proceed(@NonNull T object, @NonNull Class callbackClass, boolean forceProceed, Map<T,Boolean> cache)
T
- The type of object for which callbacks should be proceededobject
- The object for which callbacks could be proceededcallbackClass
- The class of the callbacks handlerforceProceed
- The "force proceed" flagcache
- The cache of already checked objectstrue
if callbacks should be proceeded, false
otherwiseprotected void onRegister()
protected void onUnregister()
Copyright © 2015-2017 akha, a.k.a. Alexander Kharitonov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.