Clover Coverage Report - Subsonic-Android Coverage Report
Coverage timestamp: ven dic 19 2014 17:57:13 EST
../../../../../img/srcFileCovDistChart5.png 71% of files have more coverage
41   139   19   2,93
8   101   0,46   14
14     1,36  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  ModalBackgroundTask       Line # 30 41 19 49,2% 0.4920635
 
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 android.app.Activity;
22    import android.app.AlertDialog;
23    import android.content.DialogInterface;
24    import android.util.Log;
25    import net.sourceforge.subsonic.androidapp.R;
26   
27    /**
28    * @author Sindre Mehus
29    */
 
30    public abstract class ModalBackgroundTask<T> extends BackgroundTask<T> {
31   
32    private static final String TAG = ModalBackgroundTask.class.getSimpleName();
33   
34    private final AlertDialog progressDialog;
35    private Thread thread;
36    private final boolean finishActivityOnCancel;
37    private boolean cancelled;
38   
 
39  2 toggle public ModalBackgroundTask(Activity activity, boolean finishActivityOnCancel) {
40  2 super(activity);
41  2 this.finishActivityOnCancel = finishActivityOnCancel;
42  2 progressDialog = createProgressDialog();
43    }
44   
 
45  0 toggle public ModalBackgroundTask(Activity activity) {
46  0 this(activity, true);
47    }
48   
 
49  2 toggle private AlertDialog createProgressDialog() {
50  2 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
51  2 builder.setIcon(android.R.drawable.ic_dialog_info);
52  2 builder.setTitle(R.string.background_task_wait);
53  2 builder.setMessage(R.string.background_task_loading);
54  2 builder.setCancelable(true);
55  2 builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
 
56  0 toggle @Override
57    public void onCancel(DialogInterface dialogInterface) {
58  0 cancel();
59    }
60    });
61  2 builder.setPositiveButton(R.string.common_cancel, new DialogInterface.OnClickListener() {
 
62  0 toggle @Override
63    public void onClick(DialogInterface dialogInterface, int i) {
64  0 cancel();
65    }
66    });
67   
68  2 return builder.create();
69    }
70   
 
71  2 toggle public void execute() {
72  2 cancelled = false;
73  2 progressDialog.show();
74   
75  2 thread = new Thread() {
 
76  2 toggle @Override
77    public void run() {
78  2 try {
79  2 final T result = doInBackground();
80  2 if (cancelled) {
81  0 progressDialog.dismiss();
82  0 return;
83    }
84   
85  2 getHandler().post(new Runnable() {
 
86  2 toggle @Override
87    public void run() {
88  2 progressDialog.dismiss();
89  2 done(result);
90    }
91    });
92   
93    } catch (final Throwable t) {
94  0 if (cancelled) {
95  0 return;
96    }
97  0 getHandler().post(new Runnable() {
 
98  0 toggle @Override
99    public void run() {
100  0 progressDialog.dismiss();
101  0 error(t);
102    }
103    });
104    }
105    }
106    };
107  2 thread.start();
108    }
109   
 
110  0 toggle protected void cancel() {
111  0 cancelled = true;
112  0 if (thread != null) {
113  0 thread.interrupt();
114    }
115   
116  0 if (finishActivityOnCancel) {
117  0 getActivity().finish();
118    }
119    }
120   
 
121  0 toggle protected boolean isCancelled() {
122  0 return cancelled;
123    }
124   
 
125  0 toggle protected void error(Throwable error) {
126  0 Log.w(TAG, "Got exception: " + error, error);
127  0 new ErrorDialog(getActivity(), getErrorMessage(error), finishActivityOnCancel);
128    }
129   
 
130  4 toggle @Override
131    public void updateProgress(final String message) {
132  4 getHandler().post(new Runnable() {
 
133  4 toggle @Override
134    public void run() {
135  4 progressDialog.setMessage(message);
136    }
137    });
138    }
139    }