1 /*
2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
3  * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 
20 package org.hedgewars.hedgeroid.Downloader;
21 
22 import org.hedgewars.hedgeroid.R;
23 
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.ServiceConnection;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.os.IBinder;
31 import android.os.Message;
32 import android.os.Messenger;
33 import android.os.RemoteException;
34 import android.support.v4.app.Fragment;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.View.OnClickListener;
38 import android.view.ViewGroup;
39 import android.widget.Button;
40 import android.widget.ProgressBar;
41 import android.widget.TextView;
42 
43 public class DownloadFragment extends Fragment{
44     public static final String EXTRA_TASK = "task";
45 
46     public static final int MSG_START = 0;
47     public static final int MSG_UPDATE = 1;
48     public static final int MSG_DONE = 2;
49     public static final int MSG_FAILED = 3;
50 
51     private boolean boundToService = false;
52 
53     private TextView progress_sub;
54     private ProgressBar progress;
55     private Button /*positive,*/ negative;
56 
57     private DownloadPackage pack;
58 
59     private Handler messageHandler;
60     private Messenger messenger, messengerService;
61 
getInstance(DownloadPackage task)62     public static DownloadFragment getInstance(DownloadPackage task){
63         DownloadFragment df = new DownloadFragment();
64         Bundle args = new Bundle();
65         args.putParcelable(DownloadFragment.EXTRA_TASK, task);
66 
67         df.setArguments(args);
68 
69         return df;
70     }
71 
onActivityCreated(Bundle savedInstanceState)72     public void onActivityCreated(Bundle savedInstanceState){
73         super.onActivityCreated(savedInstanceState);
74 
75         messageHandler = new Handler(messageCallback);
76         messenger = new Messenger(messageHandler);
77         Intent i = new Intent(getActivity().getApplicationContext(), DownloadService.class);
78         getActivity().startService(i);
79         getActivity().bindService(new Intent(getActivity().getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE);
80     }
81 
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);
84         progress_sub = (TextView)v.findViewById(R.id.progressbar_sub);
85         progress = (ProgressBar)v.findViewById(R.id.progressbar);
86 
87         //positive = (Button) v.findViewById(R.id.background);
88         negative = (Button) v.findViewById(R.id.cancelDownload);
89         //positive.setOnClickListener(backgroundClicker);
90         negative.setOnClickListener(cancelClicker);
91 
92         pack = getArguments().getParcelable(DownloadFragment.EXTRA_TASK);
93 
94         return v;
95     }
96 
97     private OnClickListener backgroundClicker = new OnClickListener(){
98         public void onClick(View v){
99             getActivity().finish();
100         }
101     };
102     private OnClickListener cancelClicker = new OnClickListener(){
103         public void onClick(View v){
104             if(messengerService != null){
105                 Message message = Message.obtain(messageHandler, DownloadService.MSG_CANCEL, pack);
106                 try {
107                     messengerService.send(message);
108                 } catch (RemoteException e) {}
109             }
110             //getActivity().finish();
111         }
112     };
113     private OnClickListener doneClicker = new OnClickListener(){
114         public void onClick(View v){
115             getActivity().finish();
116         }
117     };
118 
119     private OnClickListener tryAgainClicker = new OnClickListener(){
120         public void onClick(View v){
121             if(messengerService != null){
122                 Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack);
123                 message.replyTo = messenger;
124                 try {
125                     messengerService.send(message);
126                 } catch (RemoteException e) {
127                     e.printStackTrace();
128                 }
129             }
130         }
131     };
132 
onDestroy()133     public void onDestroy(){
134         unBindFromService();
135         super.onDestroy();
136     }
137 
138     private ServiceConnection connection = new ServiceConnection(){
139 
140         public void onServiceConnected(ComponentName name, IBinder service) {
141             messengerService = new Messenger(service);
142 
143             try{
144                 //give the service a task
145                 if(messengerService != null){
146                     Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack);
147                     message.replyTo = messenger;
148                     messengerService.send(message);
149                 }
150 
151             }catch (RemoteException e){}
152         }
153 
154         public void onServiceDisconnected(ComponentName name) {
155             messengerService = null;
156         }
157 
158     };
159 
unBindFromService()160     public void unBindFromService(){
161         if(messengerService != null){
162             try {
163                 Message message = Message.obtain(messageHandler, DownloadService.MSG_UNREGISTER_CLIENT, pack);
164                 message.replyTo = messenger;
165                 messengerService.send(message);
166             } catch (RemoteException e) {
167                 e.printStackTrace();
168             }
169         }
170 
171         getActivity().unbindService(connection);
172     }
173 
174     private Handler.Callback messageCallback = new Handler.Callback() {
175 
176         public boolean handleMessage(Message msg) {
177             switch(msg.what){
178             case MSG_START:
179                 progress.setMax(msg.arg1);
180                 progress_sub.setText(String.format("%dkb/%dkb\n%s", 0, msg.arg1, ""));
181                 //positive.setText(R.string.download_background);
182                 //positive.setOnClickListener(backgroundClicker);
183                 negative.setText(R.string.download_cancel);
184                 negative.setOnClickListener(cancelClicker);
185                 break;
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));
188                 progress.setProgress(msg.arg1);
189                 break;
190             case MSG_DONE:
191                 progress.setProgress(progress.getMax());
192                 progress_sub.setText(R.string.download_done);
193 
194                 //  positive.setText(R.string.download_back);
195                 //  positive.setOnClickListener(doneClicker);
196 
197                 negative.setVisibility(View.INVISIBLE);
198                 break;
199             case MSG_FAILED:
200                 progress.setProgress(progress.getMax());
201 
202                 String errorMsg = getString(R.string.download_failed);
203                 switch(msg.arg1){
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;
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;
208                 }
209                 negative.setText(R.string.download_tryagain);
210                 negative.setOnClickListener(tryAgainClicker);
211                 break;
212             }
213             return false;
214         }
215     };
216 
217 }
218