Clover Coverage Report - Subsonic-Android Coverage Report
Coverage timestamp: ven dic 19 2014 17:57:13 EST
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
18   87   8   3
2   52   0,44   3
6     1,33  
2    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  CancellableTask       Line # 30 18 8 96,2% 0.96153843
  CancellableTask.OnCancelListener       Line # 84 0 0 - -1.0
 
No Tests
 
1    /*
2    This file is part of Subsonic.
3   
4    Subsonic is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8   
9    Subsonic is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12    GNU General Public License for more details.
13   
14    You should have received a copy of the GNU General Public License
15    along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
16   
17    Copyright 2009 (C) Sindre Mehus
18    */
19    package net.sourceforge.subsonic.androidapp.util;
20   
21    import java.util.concurrent.atomic.AtomicBoolean;
22    import java.util.concurrent.atomic.AtomicReference;
23   
24    import android.util.Log;
25   
26    /**
27    * @author Sindre Mehus
28    * @version $Id$
29    */
 
30    public abstract class CancellableTask {
31   
32    private static final String TAG = CancellableTask.class.getSimpleName();
33   
34    private final AtomicBoolean running = new AtomicBoolean(false);
35    private final AtomicBoolean cancelled = new AtomicBoolean(false);
36    private final AtomicReference<Thread> thread = new AtomicReference<Thread>();
37    private final AtomicReference<OnCancelListener> cancelListener = new AtomicReference<OnCancelListener>();
38   
 
39  70 toggle public void cancel() {
40  70 Log.d(TAG, "Cancelling " + CancellableTask.this);
41  70 cancelled.set(true);
42   
43  70 OnCancelListener listener = cancelListener.get();
44  70 if (listener != null) {
45  20 try {
46  20 listener.onCancel();
47    } catch (Throwable x) {
48  0 Log.w(TAG, "Error when invoking OnCancelListener.", x);
49    }
50    }
51    }
52   
 
53  17573 toggle public boolean isCancelled() {
54  17573 return cancelled.get();
55    }
56   
 
57  21 toggle public void setOnCancelListener(OnCancelListener listener) {
58  21 cancelListener.set(listener);
59    }
60   
 
61  166 toggle public boolean isRunning() {
62  166 return running.get();
63    }
64   
65    public abstract void execute();
66   
 
67  51 toggle public void start() {
68  51 thread.set(new Thread() {
 
69  51 toggle @Override
70    public void run() {
71  51 running.set(true);
72  51 Log.d(TAG, "Starting thread for " + CancellableTask.this);
73  51 try {
74  51 execute();
75    } finally {
76  50 running.set(false);
77  50 Log.d(TAG, "Stopping thread for " + CancellableTask.this);
78    }
79    }
80    });
81  51 thread.get().start();
82    }
83   
 
84    public static interface OnCancelListener {
85    void onCancel();
86    }
87    }