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