project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadPackage.java
branchhedgeroid
changeset 6350 41b0a9955c47
child 6700 e04da46ee43c
equal deleted inserted replaced
6348:162fec525764 6350:41b0a9955c47
       
     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.hedgewars.hedgeroid.Utils;
       
    24 import org.xmlpull.v1.XmlPullParser;
       
    25 import org.xmlpull.v1.XmlPullParserException;
       
    26 
       
    27 import android.content.Context;
       
    28 import android.content.SharedPreferences;
       
    29 import android.os.Parcel;
       
    30 import android.os.Parcelable;
       
    31 import android.preference.PreferenceManager;
       
    32 import android.util.Log;
       
    33 
       
    34 public class DownloadPackage implements Parcelable{
       
    35 	private String url_without_suffix;
       
    36 	private String pathToStore;
       
    37 	private String representation;
       
    38 	private String description;
       
    39 	private int versionNumber;
       
    40 	private final Status status;
       
    41 	private int uniqueId;
       
    42 
       
    43 
       
    44 	public DownloadPackage(Parcel src){
       
    45 		url_without_suffix = src.readString();
       
    46 		pathToStore = src.readString();
       
    47 		representation = src.readString();
       
    48 		versionNumber = src.readInt();
       
    49 		status = Status.values()[src.readInt()];
       
    50 		description = src.readString();
       
    51 		uniqueId = src.readInt();
       
    52 	}
       
    53 
       
    54 	public DownloadPackage(Context c, String _url_without_suffix, String path, int version, String _representation, String _description, int _uniqueId){
       
    55 		url_without_suffix = _url_without_suffix;
       
    56 		pathToStore = path;
       
    57 		representation = _representation;
       
    58 		versionNumber = version;
       
    59 		description = _description;
       
    60 		uniqueId = _uniqueId;
       
    61 
       
    62 
       
    63 		//determine if the user has already downloaded this version
       
    64 		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(c);
       
    65 		int currentVersion = sharedPref.getInt(representation, -1);
       
    66 		if(currentVersion == versionNumber) status = Status.CURRENTVERSION;
       
    67 		else if (currentVersion < versionNumber) status = Status.NEWERVERSION;
       
    68 		else status = Status.OLDERVERSION;
       
    69 	}
       
    70 
       
    71 	public Status getStatus(){
       
    72 		return status;
       
    73 	}
       
    74 
       
    75 	public String getURL(){
       
    76 		return url_without_suffix;
       
    77 	}
       
    78 
       
    79 	public String getPathToStore(){
       
    80 		return pathToStore;
       
    81 	}
       
    82 
       
    83 	public String toString(){
       
    84 		return representation;
       
    85 	}
       
    86 
       
    87 	public int describeContents() {
       
    88 		return 0;
       
    89 	}
       
    90 	public int getId(){
       
    91 		return uniqueId;
       
    92 	}
       
    93 
       
    94 	public void writeToParcel(Parcel dest, int flags) {
       
    95 		dest.writeString(url_without_suffix);
       
    96 		dest.writeString(pathToStore);
       
    97 		dest.writeString(representation);
       
    98 		dest.writeInt(versionNumber);
       
    99 		dest.writeInt(status.ordinal());
       
   100 		dest.writeString(description);
       
   101 	}
       
   102 
       
   103 	public static final Parcelable.Creator<DownloadPackage> CREATOR = new Parcelable.Creator<DownloadPackage>() {
       
   104 		public DownloadPackage createFromParcel(Parcel source) {
       
   105 			return new DownloadPackage(source);
       
   106 		}
       
   107 		public DownloadPackage[] newArray(int size) {
       
   108 			return new DownloadPackage[size];
       
   109 		}
       
   110 	};
       
   111 
       
   112 	/*
       
   113 	 * We enter with a XmlPullParser.Start_tag with name "task"
       
   114 	 */
       
   115 	public static DownloadPackage getTaskFromXML(Context c, XmlPullParser xmlPuller) throws XmlPullParserException, IOException{
       
   116 		String url = null;
       
   117 		String path = null;
       
   118 		String representation = null;
       
   119 		String description = null;
       
   120 		int uniqueId = -1;
       
   121 		int version = -1;
       
   122 
       
   123 		int eventType = DownloadPackage.getEventType(xmlPuller);//get the next token, should be a start tag
       
   124 		while(eventType != XmlPullParser.END_DOCUMENT){
       
   125 			switch(eventType){
       
   126 			case XmlPullParser.START_TAG:
       
   127 				String name = xmlPuller.getName().toLowerCase();
       
   128 				if(DownloadPackage.getEventType(xmlPuller) == XmlPullParser.TEXT){
       
   129 					String text = xmlPuller.getText().trim();
       
   130 					if(name.equals("url")){
       
   131 						url = text;
       
   132 					}else if(name.equals("version")){
       
   133 						try{
       
   134 						version = Integer.parseInt(text);
       
   135 						}catch (NumberFormatException e){
       
   136 							e.printStackTrace();
       
   137 							version = -1;
       
   138 						}
       
   139 					}else if(name.equals("path")){
       
   140 						path = Utils.getDataPath(c) + text;
       
   141 					}else if(name.equals("representation")){
       
   142 						representation = text;
       
   143 					}else if(name.equals("description")){
       
   144 						description = text;
       
   145 					}else if(name.equals("uniqueid")){
       
   146 						try{
       
   147 							uniqueId = Integer.parseInt(text);
       
   148 							}catch (NumberFormatException e){
       
   149 								e.printStackTrace();
       
   150 								version = -1;
       
   151 							}
       
   152 					}
       
   153 				}				
       
   154 				DownloadPackage.getEventType(xmlPuller);//endtag
       
   155 				break;
       
   156 			case XmlPullParser.END_TAG:
       
   157 				if(xmlPuller.getName().toLowerCase().equals("task") && url != null && path != null && version != -1 && representation != null){
       
   158 					return new DownloadPackage(c, url, path, version, representation, description, uniqueId);
       
   159 				}else{
       
   160 					throw new XmlPullParserException("XML download parsing: missing tags");
       
   161 				}
       
   162 			case XmlPullParser.TEXT:
       
   163 				throw new XmlPullParserException("Wrong tag recieved got TEXT : " + xmlPuller.getText());
       
   164 			default:
       
   165 				throw new XmlPullParserException("Wrong tag recieved got: " + eventType);
       
   166 			}
       
   167 			eventType = DownloadPackage.getEventType(xmlPuller);
       
   168 		}
       
   169 		throw new XmlPullParserException("Xml: unexpected endofdocument tag");
       
   170 	}
       
   171 
       
   172 	/**
       
   173 	 * Skips whitespaces..
       
   174 	 */
       
   175 	private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{
       
   176 		int eventType = xmlPuller.next();
       
   177 		while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){
       
   178 			eventType = xmlPuller.next();
       
   179 		}
       
   180 		return eventType;
       
   181 	}
       
   182 }
       
   183 
       
   184 enum Status{
       
   185 	CURRENTVERSION, NEWERVERSION, OLDERVERSION;
       
   186 }