1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 android.telecom.Logging;
18 
19 import android.telecom.Log;
20 
21 /**
22  * Encapsulates session logging in a Runnable to reduce code duplication when continuing subsessions
23  * in a handler/thread.
24  * @hide
25  */
26 public abstract class Runnable {
27 
28     private Session mSubsession;
29     private final String mSubsessionName;
30     private final Object mLock;
31     private final java.lang.Runnable mRunnable = new java.lang.Runnable() {
32         @Override
33         public void run() {
34             synchronized (mLock) {
35                 try {
36                     Log.continueSession(mSubsession, mSubsessionName);
37                     loggedRun();
38                 } finally {
39                     if (mSubsession != null) {
40                         Log.endSession();
41                         mSubsession = null;
42                     }
43                 }
44             }
45         }
46     };
47 
48     /**
49      * Creates a new Telecom Runnable that incorporates Session Logging into it. Useful for carrying
50      * Logging Sessions through different threads as well as through handlers.
51      * @param subsessionName The name that will be used in the Logs to mark this Session
52      * @param lock The synchronization lock that will be used to lock loggedRun().
53      */
Runnable(String subsessionName, Object lock)54     public Runnable(String subsessionName, Object lock) {
55         if (lock == null) {
56             mLock = new Object();
57         } else {
58             mLock = lock;
59         }
60         mSubsessionName = subsessionName;
61     }
62 
63     /**
64      * Return the runnable that will be canceled in the handler queue.
65      * @return Runnable object to cancel.
66      */
getRunnableToCancel()67     public final java.lang.Runnable getRunnableToCancel() {
68         return mRunnable;
69     }
70 
71     /**
72      * Creates a Runnable and a logging subsession that can be used in a handler/thread. Be sure to
73      * call cancel() if this session is never going to be run (removed from a handler queue, for
74      * for example).
75      * @return A Java Runnable that can be used in a handler queue or thread.
76      */
prepare()77     public java.lang.Runnable prepare() {
78         cancel();
79         mSubsession = Log.createSubsession();
80         return mRunnable;
81     }
82 
83     /**
84      * This method is used to clean up the active session if the Runnable gets removed from a
85      * handler and is never run.
86      */
cancel()87     public void cancel() {
88         synchronized (mLock) {
89             Log.cancelSubsession(mSubsession);
90             mSubsession = null;
91         }
92     }
93 
94     /**
95      * The method that will be run in the handler/thread.
96      */
loggedRun()97     abstract public void loggedRun();
98 
99 }