Home | History | Annotate | Download | only in replicaisland
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 package com.replica.replicaisland;
     19 
     20 import java.util.ArrayList;
     21 
     22 import org.xmlpull.v1.XmlPullParser;
     23 
     24 import android.content.Context;
     25 import android.content.res.XmlResourceParser;
     26 import android.text.TextUtils;
     27 
     28 public final class ConversationUtils {
     29 	private static final int MAX_CHARACTERS_PER_PAGE = 250;
     30 
     31 	public static class ConversationPage {
     32         public int imageResource;
     33         public CharSequence text;
     34         public String title;
     35     }
     36 
     37 	public static class Conversation {
     38 		public ArrayList<ConversationPage> pages = new ArrayList<ConversationPage>();
     39 		public boolean splittingComplete;
     40 	}
     41 
     42 	public final static ArrayList<Conversation> loadDialog(int resource, Context context) {
     43         XmlResourceParser parser = context.getResources().getXml(resource);
     44 
     45         ArrayList<Conversation> dialog = null;
     46         Conversation currentConversation = null;
     47 
     48         try {
     49             int eventType = parser.getEventType();
     50             while (eventType != XmlPullParser.END_DOCUMENT) {
     51                 if (eventType == XmlPullParser.START_TAG) {
     52                 	if (parser.getName().equals("conversation")) {
     53                 		if (dialog == null) {
     54                 			dialog = new ArrayList<Conversation>();
     55                 		}
     56                 		currentConversation = new Conversation();
     57                 		currentConversation.splittingComplete = false;
     58                 		dialog.add(currentConversation);
     59                 	} else if (parser.getName().equals("page")) {
     60                         ConversationPage page = new ConversationPage();
     61                         for (int i=0; i < parser.getAttributeCount(); i++) {
     62                                 final int value = parser.getAttributeResourceValue(i, -1);
     63                                 if (value != -1) {
     64                                     if (parser.getAttributeName(i).equals("image")) {
     65                                         page.imageResource = value;
     66                                     }
     67                                     if (parser.getAttributeName(i).equals("text")) {
     68                                         page.text = context.getText(value);
     69                                     }
     70                                     if (parser.getAttributeName(i).equals("title")) {
     71                                         page.title = context.getString(value);
     72                                     }
     73                                 }
     74                         }
     75                         currentConversation.pages.add(page);
     76                     }
     77                 }
     78                 eventType = parser.next();
     79             }
     80         } catch(Exception e) {
     81                 DebugLog.e("LoadDialog", e.getStackTrace().toString());
     82         } finally {
     83             parser.close();
     84         }
     85 
     86         return dialog;
     87     }
     88 
     89 }
     90