30 import android.os.IBinder; |
30 import android.os.IBinder; |
31 import android.os.Message; |
31 import android.os.Message; |
32 import android.os.Messenger; |
32 import android.os.Messenger; |
33 import android.os.RemoteException; |
33 import android.os.RemoteException; |
34 import android.support.v4.app.Fragment; |
34 import android.support.v4.app.Fragment; |
|
35 import android.util.Log; |
35 import android.view.LayoutInflater; |
36 import android.view.LayoutInflater; |
36 import android.view.View; |
37 import android.view.View; |
37 import android.view.View.OnClickListener; |
38 import android.view.View.OnClickListener; |
38 import android.view.ViewGroup; |
39 import android.view.ViewGroup; |
39 import android.widget.Button; |
40 import android.widget.Button; |
61 |
62 |
62 public static DownloadFragment getInstance(DownloadPackage task){ |
63 public static DownloadFragment getInstance(DownloadPackage task){ |
63 DownloadFragment df = new DownloadFragment(); |
64 DownloadFragment df = new DownloadFragment(); |
64 Bundle args = new Bundle(); |
65 Bundle args = new Bundle(); |
65 args.putParcelable(DownloadFragment.EXTRA_TASK, task); |
66 args.putParcelable(DownloadFragment.EXTRA_TASK, task); |
66 |
67 |
67 df.setArguments(args); |
68 df.setArguments(args); |
68 |
69 |
69 return df; |
70 return df; |
70 } |
71 } |
71 |
72 |
72 public void onActivityCreated(Bundle savedInstanceState){ |
73 public void onActivityCreated(Bundle savedInstanceState){ |
73 super.onActivityCreated(savedInstanceState); |
74 super.onActivityCreated(savedInstanceState); |
74 |
75 |
75 messageHandler = new Handler(messageCallback); |
76 messageHandler = new Handler(messageCallback); |
76 messenger = new Messenger(messageHandler); |
77 messenger = new Messenger(messageHandler); |
77 Intent i = new Intent(getActivity(), DownloadService.class); |
78 Intent i = new Intent(getActivity().getApplicationContext(), DownloadService.class); |
78 getActivity().startService(i); |
79 getActivity().startService(i); |
79 getActivity().bindService(new Intent(getActivity(), DownloadService.class), connection, Context.BIND_AUTO_CREATE); |
80 getActivity().bindService(new Intent(getActivity().getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE); |
80 } |
81 } |
81 |
82 |
82 public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup, Bundle savedInstanceState){ |
83 public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup, Bundle savedInstanceState){ |
83 View v = inflater.inflate(R.layout.download_progress, viewgroup, false); |
84 View v = inflater.inflate(R.layout.download_progress, viewgroup, false); |
84 progress_sub = (TextView)v.findViewById(R.id.progressbar_sub); |
85 progress_sub = (TextView)v.findViewById(R.id.progressbar_sub); |
85 progress = (ProgressBar)v.findViewById(R.id.progressbar); |
86 progress = (ProgressBar)v.findViewById(R.id.progressbar); |
86 |
87 |
99 getActivity().finish(); |
100 getActivity().finish(); |
100 } |
101 } |
101 }; |
102 }; |
102 private OnClickListener cancelClicker = new OnClickListener(){ |
103 private OnClickListener cancelClicker = new OnClickListener(){ |
103 public void onClick(View v){ |
104 public void onClick(View v){ |
104 if(messenger != null){ |
105 if(messengerService != null){ |
105 Message message = Message.obtain(messageHandler, DownloadService.MSG_CANCEL, pack); |
106 Message message = Message.obtain(messageHandler, DownloadService.MSG_CANCEL, pack); |
106 try { |
107 try { |
107 messengerService.send(message); |
108 messengerService.send(message); |
108 } catch (RemoteException e) {} |
109 } catch (RemoteException e) {} |
109 } |
110 } |
116 } |
117 } |
117 }; |
118 }; |
118 |
119 |
119 private OnClickListener tryAgainClicker = new OnClickListener(){ |
120 private OnClickListener tryAgainClicker = new OnClickListener(){ |
120 public void onClick(View v){ |
121 public void onClick(View v){ |
121 if(messenger != null){ |
122 if(messengerService != null){ |
122 Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack); |
123 Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack); |
123 message.replyTo = messenger; |
124 message.replyTo = messenger; |
124 try { |
125 try { |
125 messengerService.send(message); |
126 messengerService.send(message); |
126 } catch (RemoteException e) { |
127 } catch (RemoteException e) { |
128 } |
129 } |
129 } |
130 } |
130 } |
131 } |
131 }; |
132 }; |
132 |
133 |
133 public void onStop(){ |
134 public void onDestroy(){ |
134 super.onStop(); |
|
135 unBindFromService(); |
135 unBindFromService(); |
|
136 super.onDestroy(); |
136 } |
137 } |
137 |
138 |
138 private ServiceConnection connection = new ServiceConnection(){ |
139 private ServiceConnection connection = new ServiceConnection(){ |
139 |
140 |
140 public void onServiceConnected(ComponentName name, IBinder service) { |
141 public void onServiceConnected(ComponentName name, IBinder service) { |
141 messengerService = new Messenger(service); |
142 messengerService = new Messenger(service); |
142 |
143 |
143 try{ |
144 try{ |
144 //give the service a task |
145 //give the service a task |
145 if(messenger != null){ |
146 if(messengerService != null){ |
146 Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack); |
147 Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack); |
147 message.replyTo = messenger; |
148 message.replyTo = messenger; |
148 messengerService.send(message); |
149 messengerService.send(message); |
149 } |
150 } |
150 |
151 |
155 messengerService = null; |
156 messengerService = null; |
156 } |
157 } |
157 |
158 |
158 }; |
159 }; |
159 |
160 |
160 private void unBindFromService(){ |
161 public void unBindFromService(){ |
161 if(boundToService){ |
162 if(messengerService != null){ |
162 if(messenger != null){ |
163 try { |
163 try { |
164 Message message = Message.obtain(messageHandler, DownloadService.MSG_UNREGISTER_CLIENT, pack); |
164 Message message = Message.obtain(messageHandler, DownloadService.MSG_UNREGISTER_CLIENT, pack); |
165 message.replyTo = messenger; |
165 message.replyTo = messenger; |
166 messengerService.send(message); |
166 messengerService.send(message); |
167 } catch (RemoteException e) { |
167 } catch (RemoteException e) { |
168 e.printStackTrace(); |
168 e.printStackTrace(); |
169 } |
169 } |
170 } |
170 } |
171 |
171 |
172 getActivity().unbindService(connection); |
172 boundToService = false; |
|
173 getActivity().unbindService(connection); |
|
174 } |
|
175 } |
173 } |
176 |
174 |
177 private Handler.Callback messageCallback = new Handler.Callback() { |
175 private Handler.Callback messageCallback = new Handler.Callback() { |
178 |
176 |
179 public boolean handleMessage(Message msg) { |
177 public boolean handleMessage(Message msg) { |
192 break; |
190 break; |
193 case MSG_DONE: |
191 case MSG_DONE: |
194 progress.setProgress(progress.getMax()); |
192 progress.setProgress(progress.getMax()); |
195 progress_sub.setText(R.string.download_done); |
193 progress_sub.setText(R.string.download_done); |
196 |
194 |
197 // positive.setText(R.string.download_back); |
195 // positive.setText(R.string.download_back); |
198 // positive.setOnClickListener(doneClicker); |
196 // positive.setOnClickListener(doneClicker); |
199 |
197 |
200 negative.setVisibility(View.INVISIBLE); |
198 negative.setVisibility(View.INVISIBLE); |
201 break; |
199 break; |
202 case MSG_FAILED: |
200 case MSG_FAILED: |
203 progress.setProgress(progress.getMax()); |
201 progress.setProgress(progress.getMax()); |
204 progress_sub.setText(R.string.download_failed); |
202 progress_sub.setText(R.string.download_failed); |
205 // positive.setText(R.string.download_back); |
203 // positive.setText(R.string.download_back); |
206 // positive.setOnClickListener(doneClicker); |
204 // positive.setOnClickListener(doneClicker); |
207 |
205 |
208 negative.setText(R.string.download_tryagain); |
206 negative.setText(R.string.download_tryagain); |
209 negative.setOnClickListener(tryAgainClicker); |
207 negative.setOnClickListener(tryAgainClicker); |
210 break; |
208 break; |
211 } |
209 } |