Nextrek
@ 11
Nextrek / Android / Minstrek / MinstrekLib / src / nextrek / minstrek / activity / BookPageActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
package nextrek.minstrek.activity; import java.io.File; import java.io.IOException; import java.util.ArrayList; import nextrek.minstrek.R; import nextrek.minstrek.core.BookModel; import nextrek.minstrek.core.ButtonModel; import nextrek.minstrek.core.FlagList; import nextrek.minstrek.core.PageModel; import nextrek.minstrek.core.PageModelList; import nextrek.minstrek.utils.BookUtils; import org.json.JSONException; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.graphics.Bitmap; import android.os.Bundle; import android.text.Html; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; public class BookPageActivity extends Activity { public static final String TAG = "BookPageActivity"; private static final String TARGET_ID_KEY = "target_id"; private static final String EVENTS_KEY = "events"; private static final String OBJECTS_KEY = "objects"; private static final int ANIMATION_DURATION = 250; static BookModel bookModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.book_page); Context context = getApplicationContext(); SharedPreferences prefs = getPreferences(MODE_PRIVATE); String target_id = "init"; String saved_target_id = prefs.getString(TARGET_ID_KEY, null); if (saved_target_id != null) { Log.d(TAG, "target_id: " + saved_target_id); if (saved_target_id.compareToIgnoreCase("end") != 0) { target_id = saved_target_id; } } // quando la storia inizia o ricomincia cancella i progressi! if (target_id.equalsIgnoreCase("init")) { Editor editor = prefs.edit(); editor.remove(EVENTS_KEY); editor.remove(OBJECTS_KEY); editor.commit(); // unload current book and force reload if (bookModel != null) { bookModel.shutdown(); bookModel = null; } } if (bookModel == null) { Log.d(BookPageActivity.TAG, "loading book"); bookModel = getBookFromAsset(context, prefs); // bookModel.verifyImages(context); } Button creditsButton = (Button) findViewById(R.id.creditsButton); if (target_id.equalsIgnoreCase("init")) { // creditsButton.setVisibility(View.VISIBLE); addToAnimationList(creditsButton, android.R.anim.slide_in_left); creditsButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent newPageIntent = new Intent(getApplicationContext(), CreditsActivity.class); newPageIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(newPageIntent); finish(); overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right); } }); } else { creditsButton.setVisibility(View.GONE); } setupPageLayout(context, target_id); } /* * @Override * protected void onPostCreate(Bundle savedInstanceState) { * super.onPostCreate(savedInstanceState); * startAnimationList(); * } */ @Override public boolean onCreateOptionsMenu(Menu menu) { return false; } ArrayList<View> animatedViews = new ArrayList<View>(16); protected void addToAnimationList(View view, int animationId) { // view.setVisibility(View.INVISIBLE); Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), animationId); anim.setStartOffset(1000 + (animatedViews.size()) * 250); view.setAnimation(anim); animatedViews.add(view); } protected void setupPageLayout(Context context, String pageName) { PageModel pageModel = bookModel.getPage(pageName); if (pageModel == null) { Log.e(TAG, "page not found:" + pageName); pageName = "init"; pageModel = bookModel.getPage(pageName); } ImageView topImage = (ImageView) findViewById(R.id.topImage); String imageName = pageModel.getTopImage(); if ((imageName != null) && (!imageName.equalsIgnoreCase("undefined"))) { //imageName = imageName.replace(".jpg", ".jp2"); String fullpath = bookModel.getName() + File.separatorChar + "images" + File.separatorChar + imageName; long t = System.currentTimeMillis(); Log.e(TAG, "begin decode"); Bitmap topImageBitmap = BookUtils.getBitmapFromAsset(context, fullpath); long d = System.currentTimeMillis() - t; Log.e(TAG, "end decode " + d); topImage.setImageBitmap(topImageBitmap); } else { topImage.setImageBitmap(null); } View text = findViewById(R.id.text); if (text instanceof EditText) { ((EditText) text).setText(Html.fromHtml(pageModel.getText())); } if (text instanceof TextView) { ((TextView) text).setText(Html.fromHtml(pageModel.getText())); } // addToAnimationList(text, android.R.anim.fade_in); ViewGroup buttonsViewGroup = (ViewGroup) findViewById(R.id.buttons); buttonsViewGroup.removeAllViews(); if (pageModel.getButtons().isEmpty()) { /* * alcune pagine sono senza bottoni, molto spesso è perchè * sono pagine di GAME OVER.. in questo caso aggiungo * un bottone a mano */ ButtonModel button = ButtonModel.createGameOverButton(); addButtonToLayout(buttonsViewGroup, button); } for (ButtonModel button : pageModel.getButtons()) { addButtonToLayout(buttonsViewGroup, button); } } protected BookModel getBookFromAsset(Context context, SharedPreferences prefs) { String bookName = null; try { String[] fileList = context.getAssets().list("book"); if (fileList.length > 0) { bookName = "book" + File.separatorChar + fileList[0]; Log.d(TAG, "book -> " + bookName); } } catch (IOException e1) { e1.printStackTrace(); finish(); } String basePath = bookName + File.separatorChar + "json" + File.separatorChar; PageModelList pagesList = null; String pagesJavascript = BookUtils.readStringFile(context, basePath + "pages.js"); if (pagesJavascript != null) { pagesList = new PageModelList(pagesJavascript); } else { Log.e(TAG, "* NO BOOK FONUD *"); finish(); } FlagList eventsList = FlagList.createFromAssetsOrSharedPrefs(context, prefs, EVENTS_KEY, basePath + "events.js", "happened"); FlagList objectsList = FlagList.createFromAssetsOrSharedPrefs(context, prefs, OBJECTS_KEY, basePath + "objects.js", "had"); return new BookModel(bookName, pagesList, eventsList, objectsList); } protected void addButtonToLayout(ViewGroup viewGroup, final ButtonModel buttonModel) { if (bookModel.verifyPrecondition(buttonModel.getButtonPreCond())) { final Button buttonView = (Button) getLayoutInflater().inflate(R.layout.book_page_button, null); addToAnimationList(buttonView, android.R.anim.slide_in_left); buttonView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { bookModel.performPostCondition(buttonModel.getButtonPostCond()); String targetId = buttonModel.targetId(); Log.d(TAG, "calling target id = [" + targetId + "]"); saveBookmark(targetId); // remove selected element from list int index = animatedViews.indexOf(buttonView); animatedViews.remove(index); reloadBookActivityWithAnimations(); } }); buttonView.setText(buttonModel.getText()); viewGroup.addView(buttonView); } } protected void reloadBookActivityWithAnimations() { if (animatedViews.size() == 0) { reloadBookActivity(); } else { for (int i = 0; i < animatedViews.size(); i++) { final View view = animatedViews.get(i); Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right); anim.setStartOffset(i * 250); if (i == (animatedViews.size() - 1)) { anim.setAnimationListener(new AnimationListener() { @Override public void onAnimationEnd(Animation animation) { view.setVisibility(View.INVISIBLE); reloadBookActivity(); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationStart(Animation animation) { } }); } else { anim.setAnimationListener(new AnimationListener() { @Override public void onAnimationEnd(Animation animation) { view.setVisibility(View.INVISIBLE); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationStart(Animation animation) { } }); } view.startAnimation(anim); } } } protected void reloadBookActivity() { Intent newPageIntent = new Intent(getApplicationContext(), BookPageActivity.class); newPageIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(newPageIntent); finish(); overridePendingTransition(R.anim.fade_in, R.anim.fade_out); } protected void saveBookmark(String targetId) { Log.e(TAG, targetId); SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); editor.putString(TARGET_ID_KEY, targetId); if (bookModel.getEvents() != null) { String json = null; try { if (bookModel.getEvents().hasChanged()) { json = bookModel.getEvents().saveAsJsonString(); Log.e(TAG, json); } } catch (JSONException e) { e.printStackTrace(); } if (json != null) { editor.putString(EVENTS_KEY, json); bookModel.getEvents().clearChanged(); } } if (bookModel.getObjects() != null) { String json = null; try { if (bookModel.getObjects().hasChanged()) { json = bookModel.getObjects().saveAsJsonString(); Log.e(TAG, json); } } catch (JSONException e) { e.printStackTrace(); } if (json != null) { editor.putString(OBJECTS_KEY, json); bookModel.getObjects().clearChanged(); } } editor.commit(); } } |