project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadTask.java
branchhedgeroid
changeset 6343 9df5a486f41e
equal deleted inserted replaced
6340:9dd921c0c7e7 6343:9df5a486f41e
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program 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 this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 package org.hedgewars.hedgeroid.Downloader;
       
    20 
       
    21 import java.io.IOException;
       
    22 
       
    23 import org.xmlpull.v1.XmlPullParser;
       
    24 import org.xmlpull.v1.XmlPullParserException;
       
    25 
       
    26 import android.os.Parcel;
       
    27 import android.os.Parcelable;
       
    28 
       
    29 public class DownloadTask implements Parcelable{
       
    30 
       
    31 	private String url_without_suffix;
       
    32 	private String pathToStore;
       
    33 	private String representation;
       
    34 	private int attempts;
       
    35 	private int versionNumber;
       
    36 	
       
    37 	
       
    38 	public DownloadTask(Parcel in){
       
    39 		readFromParcel(in);
       
    40 	}
       
    41 	
       
    42 	public DownloadTask(String _url_without_suffix, String path, int version, String _representation){
       
    43 		url_without_suffix = _url_without_suffix;
       
    44 		pathToStore = path;
       
    45 		representation = _representation;
       
    46 		versionNumber = version;
       
    47 		attempts = 0;
       
    48 	}
       
    49 	
       
    50 	public int getAttempts(){
       
    51 		return attempts;
       
    52 	}
       
    53 	
       
    54 	public String getURL(){
       
    55 		return url_without_suffix;
       
    56 	}
       
    57 	
       
    58 	public String getPathToStore(){
       
    59 		return pathToStore;
       
    60 	}
       
    61 	
       
    62 	public void incrementAttempts(){
       
    63 		attempts++;
       
    64 	}
       
    65 	
       
    66 	public String toString(){
       
    67 		return representation;
       
    68 	}
       
    69 	
       
    70 	public int describeContents() {
       
    71 		return 0;
       
    72 	}
       
    73 
       
    74 	public void writeToParcel(Parcel dest, int flags) {
       
    75 		dest.writeString(url_without_suffix);
       
    76 		dest.writeString(pathToStore);
       
    77 		dest.writeString(representation);
       
    78 		dest.writeInt(versionNumber);
       
    79 		dest.writeInt(attempts);
       
    80 	}
       
    81 	
       
    82 	private void readFromParcel(Parcel src){
       
    83 		url_without_suffix = src.readString();
       
    84 		pathToStore = src.readString();
       
    85 		representation = src.readString();
       
    86 		versionNumber = src.readInt();
       
    87 		attempts = src.readInt();
       
    88 	}
       
    89 	
       
    90 	public static final Parcelable.Creator<DownloadTask> CREATOR = new Parcelable.Creator<DownloadTask>() {
       
    91 		public DownloadTask createFromParcel(Parcel source) {
       
    92 			return new DownloadTask(source);
       
    93 		}
       
    94 		public DownloadTask[] newArray(int size) {
       
    95 			return new DownloadTask[size];
       
    96 		}
       
    97 	};
       
    98 	
       
    99 	/*
       
   100 	 * We enter with a XmlPullParser.Start_tag with name "task"
       
   101 	 */
       
   102 	public static DownloadTask getTaskFromXML(XmlPullParser xmlPuller) throws XmlPullParserException, IOException{
       
   103 		String url = null;
       
   104 		String path = null;
       
   105 		String representation = null;
       
   106 		int version = -1;
       
   107 		
       
   108 		int eventType = xmlPuller.getEventType();//get the next token, should be a start tag
       
   109 		while(eventType != XmlPullParser.END_DOCUMENT){
       
   110 			switch(eventType){
       
   111 			case XmlPullParser.START_TAG:
       
   112 				String name = xmlPuller.getName().toLowerCase();
       
   113 				if(name.equals("url")){
       
   114 					if(xmlPuller.getEventType() == XmlPullParser.TEXT){
       
   115 						url = xmlPuller.getText();
       
   116 					}
       
   117 				}else if(name.equals("version")){
       
   118 					if(xmlPuller.getEventType() == XmlPullParser.TEXT){
       
   119 						version = Integer.parseInt(xmlPuller.getText());
       
   120 					}
       
   121 				}else if(name.equals("path")){
       
   122 					if(xmlPuller.getEventType() == XmlPullParser.TEXT){
       
   123 						path = xmlPuller.getText();
       
   124 					}
       
   125 				}else if(name.equals("representation")){
       
   126 					if(xmlPuller.getEventType() == XmlPullParser.TEXT){
       
   127 						representation = xmlPuller.getText();
       
   128 					}
       
   129 				}
       
   130 				
       
   131 				xmlPuller.getEventType();//endtag
       
   132 				break;
       
   133 			case XmlPullParser.END_TAG:
       
   134 				if(xmlPuller.getName().toLowerCase().equals("task") && url != null && path != null && version != -1 && representation != null){
       
   135 					return new DownloadTask(url, path, version, representation);
       
   136 				}else{
       
   137 					throw new XmlPullParserException(null);
       
   138 				}
       
   139 			default:
       
   140 				throw new XmlPullParserException(null);
       
   141 			}
       
   142 			eventType = getEventType(xmlPuller);
       
   143 		}
       
   144 		
       
   145 		throw new XmlPullParserException(null);
       
   146 	}
       
   147 	
       
   148 	/**
       
   149 	 * Skips whitespaces..
       
   150 	 */
       
   151 	private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{
       
   152 		int eventType = xmlPuller.next();
       
   153 		while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){
       
   154 			eventType = xmlPuller.next();
       
   155 		}
       
   156 		return eventType;
       
   157 	}
       
   158 }