39 import android.widget.Button; |
39 import android.widget.Button; |
40 import android.widget.ProgressBar; |
40 import android.widget.ProgressBar; |
41 import android.widget.TextView; |
41 import android.widget.TextView; |
42 |
42 |
43 public class DownloadFragment extends Fragment{ |
43 public class DownloadFragment extends Fragment{ |
44 public static final String EXTRA_TASK = "task"; |
44 public static final String EXTRA_TASK = "task"; |
45 |
45 |
46 public static final int MSG_START = 0; |
46 public static final int MSG_START = 0; |
47 public static final int MSG_UPDATE = 1; |
47 public static final int MSG_UPDATE = 1; |
48 public static final int MSG_DONE = 2; |
48 public static final int MSG_DONE = 2; |
49 public static final int MSG_FAILED = 3; |
49 public static final int MSG_FAILED = 3; |
50 |
50 |
51 private boolean boundToService = false; |
51 private boolean boundToService = false; |
52 |
52 |
53 private TextView progress_sub; |
53 private TextView progress_sub; |
54 private ProgressBar progress; |
54 private ProgressBar progress; |
55 private Button /*positive,*/ negative; |
55 private Button /*positive,*/ negative; |
56 |
56 |
57 private DownloadPackage pack; |
57 private DownloadPackage pack; |
58 |
58 |
59 private Handler messageHandler; |
59 private Handler messageHandler; |
60 private Messenger messenger, messengerService; |
60 private Messenger messenger, messengerService; |
61 |
61 |
62 public static DownloadFragment getInstance(DownloadPackage task){ |
62 public static DownloadFragment getInstance(DownloadPackage task){ |
63 DownloadFragment df = new DownloadFragment(); |
63 DownloadFragment df = new DownloadFragment(); |
64 Bundle args = new Bundle(); |
64 Bundle args = new Bundle(); |
65 args.putParcelable(DownloadFragment.EXTRA_TASK, task); |
65 args.putParcelable(DownloadFragment.EXTRA_TASK, task); |
66 |
66 |
67 df.setArguments(args); |
67 df.setArguments(args); |
68 |
68 |
69 return df; |
69 return df; |
70 } |
70 } |
71 |
71 |
72 public void onActivityCreated(Bundle savedInstanceState){ |
72 public void onActivityCreated(Bundle savedInstanceState){ |
73 super.onActivityCreated(savedInstanceState); |
73 super.onActivityCreated(savedInstanceState); |
74 |
74 |
75 messageHandler = new Handler(messageCallback); |
75 messageHandler = new Handler(messageCallback); |
76 messenger = new Messenger(messageHandler); |
76 messenger = new Messenger(messageHandler); |
77 Intent i = new Intent(getActivity().getApplicationContext(), DownloadService.class); |
77 Intent i = new Intent(getActivity().getApplicationContext(), DownloadService.class); |
78 getActivity().startService(i); |
78 getActivity().startService(i); |
79 getActivity().bindService(new Intent(getActivity().getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE); |
79 getActivity().bindService(new Intent(getActivity().getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE); |
80 } |
80 } |
81 |
81 |
82 public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup, Bundle savedInstanceState){ |
82 public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup, Bundle savedInstanceState){ |
83 View v = inflater.inflate(R.layout.download_progress, viewgroup, false); |
83 View v = inflater.inflate(R.layout.download_progress, viewgroup, false); |
84 progress_sub = (TextView)v.findViewById(R.id.progressbar_sub); |
84 progress_sub = (TextView)v.findViewById(R.id.progressbar_sub); |
85 progress = (ProgressBar)v.findViewById(R.id.progressbar); |
85 progress = (ProgressBar)v.findViewById(R.id.progressbar); |
86 |
86 |
87 //positive = (Button) v.findViewById(R.id.background); |
87 //positive = (Button) v.findViewById(R.id.background); |
88 negative = (Button) v.findViewById(R.id.cancelDownload); |
88 negative = (Button) v.findViewById(R.id.cancelDownload); |
89 //positive.setOnClickListener(backgroundClicker); |
89 //positive.setOnClickListener(backgroundClicker); |
90 negative.setOnClickListener(cancelClicker); |
90 negative.setOnClickListener(cancelClicker); |
91 |
91 |
92 pack = getArguments().getParcelable(DownloadFragment.EXTRA_TASK); |
92 pack = getArguments().getParcelable(DownloadFragment.EXTRA_TASK); |
93 |
93 |
94 return v; |
94 return v; |
95 } |
95 } |
96 |
96 |
97 private OnClickListener backgroundClicker = new OnClickListener(){ |
97 private OnClickListener backgroundClicker = new OnClickListener(){ |
98 public void onClick(View v){ |
98 public void onClick(View v){ |
99 getActivity().finish(); |
99 getActivity().finish(); |
100 } |
100 } |
101 }; |
101 }; |
102 private OnClickListener cancelClicker = new OnClickListener(){ |
102 private OnClickListener cancelClicker = new OnClickListener(){ |
103 public void onClick(View v){ |
103 public void onClick(View v){ |
104 if(messengerService != null){ |
104 if(messengerService != null){ |
105 Message message = Message.obtain(messageHandler, DownloadService.MSG_CANCEL, pack); |
105 Message message = Message.obtain(messageHandler, DownloadService.MSG_CANCEL, pack); |
106 try { |
106 try { |
107 messengerService.send(message); |
107 messengerService.send(message); |
108 } catch (RemoteException e) {} |
108 } catch (RemoteException e) {} |
109 } |
109 } |
110 //getActivity().finish(); |
110 //getActivity().finish(); |
111 } |
111 } |
112 }; |
112 }; |
113 private OnClickListener doneClicker = new OnClickListener(){ |
113 private OnClickListener doneClicker = new OnClickListener(){ |
114 public void onClick(View v){ |
114 public void onClick(View v){ |
115 getActivity().finish(); |
115 getActivity().finish(); |
116 } |
116 } |
117 }; |
117 }; |
118 |
118 |
119 private OnClickListener tryAgainClicker = new OnClickListener(){ |
119 private OnClickListener tryAgainClicker = new OnClickListener(){ |
120 public void onClick(View v){ |
120 public void onClick(View v){ |
121 if(messengerService != null){ |
121 if(messengerService != null){ |
122 Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack); |
122 Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack); |
123 message.replyTo = messenger; |
123 message.replyTo = messenger; |
124 try { |
124 try { |
125 messengerService.send(message); |
125 messengerService.send(message); |
126 } catch (RemoteException e) { |
126 } catch (RemoteException e) { |
127 e.printStackTrace(); |
127 e.printStackTrace(); |
128 } |
128 } |
129 } |
129 } |
130 } |
130 } |
131 }; |
131 }; |
132 |
132 |
133 public void onDestroy(){ |
133 public void onDestroy(){ |
134 unBindFromService(); |
134 unBindFromService(); |
135 super.onDestroy(); |
135 super.onDestroy(); |
136 } |
136 } |
137 |
137 |
138 private ServiceConnection connection = new ServiceConnection(){ |
138 private ServiceConnection connection = new ServiceConnection(){ |
139 |
139 |
140 public void onServiceConnected(ComponentName name, IBinder service) { |
140 public void onServiceConnected(ComponentName name, IBinder service) { |
141 messengerService = new Messenger(service); |
141 messengerService = new Messenger(service); |
142 |
142 |
143 try{ |
143 try{ |
144 //give the service a task |
144 //give the service a task |
145 if(messengerService != null){ |
145 if(messengerService != null){ |
146 Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack); |
146 Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack); |
147 message.replyTo = messenger; |
147 message.replyTo = messenger; |
148 messengerService.send(message); |
148 messengerService.send(message); |
149 } |
149 } |
150 |
150 |
151 }catch (RemoteException e){} |
151 }catch (RemoteException e){} |
152 } |
152 } |
153 |
153 |
154 public void onServiceDisconnected(ComponentName name) { |
154 public void onServiceDisconnected(ComponentName name) { |
155 messengerService = null; |
155 messengerService = null; |
156 } |
156 } |
157 |
157 |
158 }; |
158 }; |
159 |
159 |
160 public void unBindFromService(){ |
160 public void unBindFromService(){ |
161 if(messengerService != null){ |
161 if(messengerService != null){ |
162 try { |
162 try { |
163 Message message = Message.obtain(messageHandler, DownloadService.MSG_UNREGISTER_CLIENT, pack); |
163 Message message = Message.obtain(messageHandler, DownloadService.MSG_UNREGISTER_CLIENT, pack); |
164 message.replyTo = messenger; |
164 message.replyTo = messenger; |
165 messengerService.send(message); |
165 messengerService.send(message); |
166 } catch (RemoteException e) { |
166 } catch (RemoteException e) { |
167 e.printStackTrace(); |
167 e.printStackTrace(); |
168 } |
168 } |
169 } |
169 } |
170 |
170 |
171 getActivity().unbindService(connection); |
171 getActivity().unbindService(connection); |
172 } |
172 } |
173 |
173 |
174 private Handler.Callback messageCallback = new Handler.Callback() { |
174 private Handler.Callback messageCallback = new Handler.Callback() { |
175 |
175 |
176 public boolean handleMessage(Message msg) { |
176 public boolean handleMessage(Message msg) { |
177 switch(msg.what){ |
177 switch(msg.what){ |
178 case MSG_START: |
178 case MSG_START: |
179 progress.setMax(msg.arg1); |
179 progress.setMax(msg.arg1); |
180 progress_sub.setText(String.format("%dkb/%dkb\n%s", 0, msg.arg1, "")); |
180 progress_sub.setText(String.format("%dkb/%dkb\n%s", 0, msg.arg1, "")); |
181 //positive.setText(R.string.download_background); |
181 //positive.setText(R.string.download_background); |
182 //positive.setOnClickListener(backgroundClicker); |
182 //positive.setOnClickListener(backgroundClicker); |
183 negative.setText(R.string.download_cancel); |
183 negative.setText(R.string.download_cancel); |
184 negative.setOnClickListener(cancelClicker); |
184 negative.setOnClickListener(cancelClicker); |
185 break; |
185 break; |
186 case MSG_UPDATE: |
186 case MSG_UPDATE: |
187 progress_sub.setText(String.format("%d%% - %dkb/%dkb\n%s",(msg.arg1*100)/msg.arg2, msg.arg1, msg.arg2, msg.obj)); |
187 progress_sub.setText(String.format("%d%% - %dkb/%dkb\n%s",(msg.arg1*100)/msg.arg2, msg.arg1, msg.arg2, msg.obj)); |
188 progress.setProgress(msg.arg1); |
188 progress.setProgress(msg.arg1); |
189 break; |
189 break; |
190 case MSG_DONE: |
190 case MSG_DONE: |
191 progress.setProgress(progress.getMax()); |
191 progress.setProgress(progress.getMax()); |
192 progress_sub.setText(R.string.download_done); |
192 progress_sub.setText(R.string.download_done); |
193 |
193 |
194 // positive.setText(R.string.download_back); |
194 // positive.setText(R.string.download_back); |
195 // positive.setOnClickListener(doneClicker); |
195 // positive.setOnClickListener(doneClicker); |
196 |
196 |
197 negative.setVisibility(View.INVISIBLE); |
197 negative.setVisibility(View.INVISIBLE); |
198 break; |
198 break; |
199 case MSG_FAILED: |
199 case MSG_FAILED: |
200 progress.setProgress(progress.getMax()); |
200 progress.setProgress(progress.getMax()); |
201 |
201 |
202 String errorMsg = getString(R.string.download_failed); |
202 String errorMsg = getString(R.string.download_failed); |
203 switch(msg.arg1){ |
203 switch(msg.arg1){ |
204 case DownloadAsyncTask.EXIT_CONNERROR: progress_sub.setText(errorMsg + " " + "Connection error"); break; |
204 case DownloadAsyncTask.EXIT_CONNERROR: progress_sub.setText(errorMsg + " " + "Connection error"); break; |
205 case DownloadAsyncTask.EXIT_FNF: progress_sub.setText(errorMsg + " " + "File not found"); break; |
205 case DownloadAsyncTask.EXIT_FNF: progress_sub.setText(errorMsg + " " + "File not found"); break; |
206 case DownloadAsyncTask.EXIT_MD5: progress_sub.setText(errorMsg + " " + "MD5 check failed"); break; |
206 case DownloadAsyncTask.EXIT_MD5: progress_sub.setText(errorMsg + " " + "MD5 check failed"); break; |
207 case DownloadAsyncTask.EXIT_URLFAIL: progress_sub.setText(errorMsg + " " + "Invalid url"); break; |
207 case DownloadAsyncTask.EXIT_URLFAIL: progress_sub.setText(errorMsg + " " + "Invalid url"); break; |
208 } |
208 } |
209 negative.setText(R.string.download_tryagain); |
209 negative.setText(R.string.download_tryagain); |
210 negative.setOnClickListener(tryAgainClicker); |
210 negative.setOnClickListener(tryAgainClicker); |
211 break; |
211 break; |
212 } |
212 } |
213 return false; |
213 return false; |
214 } |
214 } |
215 }; |
215 }; |
216 |
216 |
217 } |
217 } |