1
+ package org .andresoviedo .app .model3D .controller ;
2
+
3
+ import android .app .Activity ;
4
+ import android .app .ProgressDialog ;
5
+ import android .os .AsyncTask ;
6
+ import android .util .Log ;
7
+
8
+ import org .andresoviedo .app .model3D .model .Object3DBuilder ;
9
+ import org .andresoviedo .app .model3D .model .Object3DData ;
10
+
11
+ import java .io .File ;
12
+ import java .io .FileInputStream ;
13
+ import java .io .IOException ;
14
+ import java .io .InputStream ;
15
+ import java .net .URL ;
16
+
17
+ /**
18
+ * This component allows loading the model without blocking the UI.
19
+ *
20
+ * @author andresoviedo
21
+ */
22
+ public abstract class LoaderTask extends AsyncTask <Void , Integer , Object3DData > {
23
+
24
+ /**
25
+ * URL to the 3D model
26
+ */
27
+ protected final URL url ;
28
+ /**
29
+ * Callback to notify of events
30
+ */
31
+ protected final Object3DBuilder .Callback callback ;
32
+ /**
33
+ * The dialog that will show the progress of the loading
34
+ */
35
+ protected final ProgressDialog dialog ;
36
+ /**
37
+ * The parent activity
38
+ */
39
+ private final Activity parent ;
40
+ /**
41
+ * Directory where the model is located (null when its loaded from asset)
42
+ */
43
+ private final File currentDir ;
44
+ /**
45
+ * Asset directory where the model is loaded (null when its loaded from the filesystem)
46
+ */
47
+ private final String assetsDir ;
48
+ /**
49
+ * Id of the data being loaded
50
+ */
51
+ private final String modelId ;
52
+ /**
53
+ * Exception when loading data (if any)
54
+ */
55
+ protected Exception error ;
56
+
57
+ /**
58
+ * Build a new progress dialog for loading the data model asynchronously
59
+ *
60
+ * @param url the URL pointing to the 3d model
61
+ * @param currentDir the directory where the model is located (null when the model is an asset)
62
+ * @param modelId the id the data being loaded
63
+ */
64
+ public LoaderTask (Activity parent , URL url , File currentDir , String assetsDir , String modelId , Object3DBuilder .Callback callback ) {
65
+ this .parent = parent ;
66
+ this .url = url ;
67
+ this .currentDir = currentDir ;
68
+ this .assetsDir = assetsDir ;
69
+ this .modelId = modelId ;
70
+ this .dialog = new ProgressDialog (parent );
71
+ this .callback = callback ;
72
+ }
73
+
74
+
75
+ @ Override
76
+ protected void onPreExecute () {
77
+ super .onPreExecute ();
78
+ // this.dialog = ProgressDialog.show(this.parent, "Please wait ...", "Loading model data...", true);
79
+ // this.dialog.setTitle(modelId);
80
+ this .dialog .setMessage ("Loading..." );
81
+ this .dialog .setCancelable (false );
82
+ this .dialog .show ();
83
+ }
84
+
85
+
86
+
87
+ @ Override
88
+ protected Object3DData doInBackground (Void ... params ) {
89
+ try {
90
+ Object3DData data = build ();
91
+ callback .onLoadComplete (data );
92
+ build (data );
93
+ return data ;
94
+ } catch (Exception ex ) {
95
+ error = ex ;
96
+ return null ;
97
+ }
98
+ }
99
+
100
+ protected abstract Object3DData build () throws Exception ;
101
+
102
+ protected abstract void build (Object3DData data ) throws Exception ;
103
+
104
+ @ Override
105
+ protected void onProgressUpdate (Integer ... values ) {
106
+ super .onProgressUpdate (values );
107
+ switch (values [0 ]) {
108
+ case 0 :
109
+ this .dialog .setMessage ("Analyzing model..." );
110
+ break ;
111
+ case 1 :
112
+ this .dialog .setMessage ("Allocating memory..." );
113
+ break ;
114
+ case 2 :
115
+ this .dialog .setMessage ("Loading data..." );
116
+ break ;
117
+ case 3 :
118
+ this .dialog .setMessage ("Scaling object..." );
119
+ break ;
120
+ case 4 :
121
+ this .dialog .setMessage ("Building 3D model..." );
122
+ break ;
123
+ case 5 :
124
+ // Toast.makeText(parent, modelId + " Build!", Toast.LENGTH_LONG).show();
125
+ break ;
126
+ }
127
+ }
128
+
129
+ @ Override
130
+ protected void onPostExecute (Object3DData data ) {
131
+ super .onPostExecute (data );
132
+ if (dialog .isShowing ()) {
133
+ dialog .dismiss ();
134
+ }
135
+ if (error != null ) {
136
+ callback .onLoadError (error );
137
+ } else {
138
+ callback .onBuildComplete (data );
139
+ }
140
+ }
141
+
142
+
143
+ }
0 commit comments