project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MapPreviewGenerator.java
changeset 7584 7831c84cc644
parent 7582 714310efad8f
child 7588 27e5857da6af
equal deleted inserted replaced
7582:714310efad8f 7584:7831c84cc644
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU General Public License
       
     7  * as published by the Free Software Foundation; either version 2
       
     8  * of the License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    18  */
       
    19 
     1 package org.hedgewars.hedgeroid;
    20 package org.hedgewars.hedgeroid;
     2 
    21 
     3 import java.io.File;
    22 import java.io.File;
     4 import java.io.FileNotFoundException;
    23 import java.io.FileNotFoundException;
     5 
    24 
    28 
    47 
    29 /**
    48 /**
    30  * A class that asynchronously generates a map preview from a MapRecipe.
    49  * A class that asynchronously generates a map preview from a MapRecipe.
    31  * 
    50  * 
    32  * For named maps, this will load the preview image from the filesystem. For others,
    51  * For named maps, this will load the preview image from the filesystem. For others,
    33  * it will call the engine to generate a preview image.
    52  * it will call the Hedgewars engine to generate a preview image. The result is sent
       
    53  * back to a listener on the UI thread.
    34  */
    54  */
    35 public final class MapPreviewGenerator implements Runnable {
    55 public final class MapPreviewGenerator implements Runnable {
    36 	private static final String TAG = MapPreviewGenerator.class.getSimpleName();
    56 	private static final String TAG = MapPreviewGenerator.class.getSimpleName();
    37 	private static final Handler mainHandler = new Handler(Looper.getMainLooper());
    57 	private static final Handler mainHandler = new Handler(Looper.getMainLooper());
       
    58 	private static final long TIMEOUT_NS = 20l * 1000 * 1000 * 1000;
    38 
    59 
    39 	private final Context appContext;
    60 	private final Context appContext;
    40 	private final MapRecipe map;
    61 	private final MapRecipe map;
    41 	private final Listener listener;
    62 	private final Listener listener;
    42 	
    63 	
    87 					try {
   108 					try {
    88 						Thread.sleep(50);
   109 						Thread.sleep(50);
    89 					} catch (InterruptedException e) {
   110 					} catch (InterruptedException e) {
    90 						// ignore
   111 						// ignore
    91 					}
   112 					}
    92 					if(System.nanoTime()-startTime > 15000000000l) {
   113 					if(System.nanoTime()-startTime > TIMEOUT_NS) {
    93 						Log.w(TAG, "Error generating map preview: timeout");
   114 						Log.w(TAG, "Error generating map preview: timeout");
    94 						resultAvailable = true;
   115 						resultAvailable = true;
    95 					}
   116 					}
    96 				} while(!resultAvailable); // 15 seconds timeout
   117 				} while(!resultAvailable); 
    97 			} finally {
   118 			} finally {
    98 				Flib.INSTANCE.flib_mapconn_destroy(conn);
   119 				Flib.INSTANCE.flib_mapconn_destroy(conn);
    99 				postToListener(result);
   120 				postToListener(result);
   100 			}
   121 			}
   101 		}
   122 		}