project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/util/FileUtils.java
author Medo <smaxein@googlemail.com>
Mon, 20 Aug 2012 21:05:57 +0200
changeset 7584 7831c84cc644
parent 7508 763d3961400b
child 7586 33924ff4af50
permissions -rw-r--r--
License change: With the agreement of Xeli, I changed the Hedgeroid license to GPLv2+ (from GPLv2).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     1
/*
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
6700
e04da46ee43c the most important commit of the year
koda
parents: 6486
diff changeset
     3
 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     4
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     5
 *
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     6
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     7
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     8
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     9
 * of the License, or (at your option) any later version.
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    10
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful,
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    14
 * GNU General Public License for more details.
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    15
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    17
 * along with this program; if not, write to the Free Software
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    19
 */
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    20
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    21
package org.hedgewars.hedgeroid.util;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    22
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    23
import java.io.ByteArrayOutputStream;
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    24
import java.io.Closeable;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    25
import java.io.File;
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    26
import java.io.FileNotFoundException;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    27
import java.io.FileOutputStream;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    28
import java.io.IOException;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    29
import java.io.InputStream;
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    30
import java.io.OutputStream;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    31
import java.util.ArrayList;
6486
2a3ee24764bb android: startgamemenu now has some default values, plus the themes are ordered alphabetically
Xeli
parents: 6456
diff changeset
    32
import java.util.List;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    33
6839
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6700
diff changeset
    34
import android.annotation.TargetApi;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    35
import android.content.Context;
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    36
import android.content.res.Resources;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    37
import android.content.res.TypedArray;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    38
import android.os.Build;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    39
import android.os.Environment;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6049
diff changeset
    40
import android.util.Log;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    41
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    42
public class FileUtils {
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    43
	private static final String ROOT_DIR = "Data";
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    44
	private static final String TAG = FileUtils.class.getSimpleName();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    45
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    46
	/**
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    47
	 * @return true if the data path is currently available. However, it can vanish at any time so
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    48
	 * normally you should just try to use it and rely on the exceptions.
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    49
	 */
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    50
	public static boolean isDataPathAvailable() {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    51
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    52
	}
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    53
	
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    54
	/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    55
	 * get the path to which we should download all the data files
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    56
	 * @param c context 
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    57
	 * @return The directory
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    58
	 * @throws FileNotFoundException if external storage is not available at the moment
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    59
	 */
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    60
	public static File getCachePath(Context c) throws FileNotFoundException {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    61
		File cachePath = null;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    62
		if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    63
			cachePath = PreFroyoSDCardDir.getDownloadPath(c);
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    64
		} else {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    65
			cachePath = FroyoSDCardDir.getDownloadPath(c);
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    66
		}
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    67
		if(cachePath==null) {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    68
			throw new FileNotFoundException("External storage is currently unavailable");
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    69
		} else {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    70
			return cachePath;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    71
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    72
	}
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    73
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    74
	public static File getDataPathFile(Context c) throws FileNotFoundException {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    75
		return new File(getCachePath(c), ROOT_DIR);
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    76
	}
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    77
	
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    78
	// TODO Several callers are unaware that this may fail, so it throws an RTE now.
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    79
	// Should be handled better though.
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    80
	@Deprecated
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    81
	public static String getDataPath(Context c) {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    82
		try {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    83
			return getDataPathFile(c).getAbsolutePath()+"/";
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    84
		} catch(FileNotFoundException e) {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    85
			throw new RuntimeException(e);
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    86
		}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6049
diff changeset
    87
	}
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    88
6839
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6700
diff changeset
    89
	@TargetApi(8)
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    90
	private static class FroyoSDCardDir{
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    91
		public static File getDownloadPath(Context c){
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    92
			return c.getExternalCacheDir();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    93
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    94
	}
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    95
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    96
	private static class PreFroyoSDCardDir{
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    97
		public static File getDownloadPath(Context c){
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    98
			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
    99
				File extStorageDir = Environment.getExternalStorageDirectory();
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   100
				if(extStorageDir != null) {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   101
					return new File(extStorageDir, "Hedgewars");
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   102
				}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   103
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   104
			return null;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   105
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   106
	}
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   107
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   108
	/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   109
	 * Return a File array with all the files from dirName
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   110
	 * @param c
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   111
	 * @param dirName
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   112
	 * @return
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   113
	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dirName" does not exist
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   114
	 */
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   115
	public static File[] getFilesFromRelativeDir(Context c, String dirName) throws FileNotFoundException {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   116
		File f = new File(getDataPathFile(c), dirName);
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   117
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   118
		if(f.isDirectory()) {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   119
			return f.listFiles();
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   120
		} else {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   121
			throw new FileNotFoundException("Directory "+dirName+" does not exist.");
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6049
diff changeset
   122
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   123
	}
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   124
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   125
	/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   126
	 * Checks if this directory has a file with suffix suffix
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   127
	 * @param f - directory
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   128
	 * @return
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   129
	 */
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   130
	public static boolean hasFileWithSuffix(File f, String suffix){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   131
		if(f.isDirectory()){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   132
			for(String s : f.list()){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   133
				if(s.endsWith(suffix)) return true;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   134
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   135
			return false;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   136
		}else{
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   137
			return false;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   138
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   139
	}
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   140
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   141
	/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   142
	 * Gives back all dirs which contain a file with suffix fileSuffix
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   143
	 * @param c
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   144
	 * @param path
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   145
	 * @param fileSuffix
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   146
	 * @return
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   147
	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "path" does not exist
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   148
	 */
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   149
	public static List<String> getDirsWithFileSuffix(Context c, String path, String fileSuffix) throws FileNotFoundException{
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   150
		File[] files = getFilesFromRelativeDir(c,path);
6486
2a3ee24764bb android: startgamemenu now has some default values, plus the themes are ordered alphabetically
Xeli
parents: 6456
diff changeset
   151
		ArrayList<String> ret = new ArrayList<String>();
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   152
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   153
		for(File f : files){
6486
2a3ee24764bb android: startgamemenu now has some default values, plus the themes are ordered alphabetically
Xeli
parents: 6456
diff changeset
   154
			if(hasFileWithSuffix(f, fileSuffix)) ret.add(f.getName());
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   155
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   156
		return ret;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   157
	}
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   158
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   159
	/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   160
	 * Get all files from directory dir which have the given suffix
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   161
	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dir" does not exist
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   162
	 */
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   163
	public static ArrayList<String> getFileNamesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix) throws FileNotFoundException{
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   164
		File[] files = FileUtils.getFilesFromRelativeDir(c, dir);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   165
		ArrayList<String> ret = new ArrayList<String>();
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   166
		for(File file : files){
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   167
			String s = file.getName();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   168
			if(s.endsWith(suffix)){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   169
				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   170
				else ret.add(s);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   171
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   172
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   173
		return ret;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   174
	}
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   175
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   176
	/**
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   177
	 * Close a resource (possibly null), ignoring any IOException.
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   178
	 */
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   179
	public static void closeQuietly(Closeable c) {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   180
		if(c!=null) {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   181
			try {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   182
				c.close();
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   183
			} catch(IOException e) {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   184
				Log.w(TAG, e);
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   185
			}
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   186
		}
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   187
	}
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   188
	
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   189
	/**
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   190
	 * Write all data from the input stream to the file, creating or overwriting it.
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   191
	 * The input stream will be closed.
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   192
	 * 
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   193
	 * @throws IOException
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   194
	 */
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   195
	public static void writeStreamToFile(InputStream is, File file) throws IOException {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   196
		OutputStream os = null;
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   197
		byte[] buffer = new byte[8192];
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   198
		try {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   199
			os = new FileOutputStream(file);
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   200
			int size;
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   201
			while((size=is.read(buffer)) != -1) {
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   202
				os.write(buffer, 0, size);
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   203
			}
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   204
			os.close(); // Important to close this non-quietly, in case of exceptions when flushing
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   205
		} finally {
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   206
			FileUtils.closeQuietly(is);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   207
			FileUtils.closeQuietly(os);
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   208
		}
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   209
	}
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   210
	
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   211
	/**
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   212
	 * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   213
	 * @param c
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   214
	 * @param sourceResId
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   215
	 * @param directory
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   216
	 */
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   217
	public static void resRawToFilesDir(Context c, int sourceResId, int targetFilenames, String directory) throws IOException {
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   218
		File targetDir = new File(c.getFilesDir(), directory);
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   219
		targetDir.mkdirs();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   220
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   221
		//Get an array with the resource files ID
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   222
		Resources resources = c.getResources();
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   223
		TypedArray ta = resources.obtainTypedArray(sourceResId);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   224
		TypedArray filenames = resources.obtainTypedArray(targetFilenames);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   225
		for(int i = 0; i < ta.length(); i++){
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   226
			int resId =  ta.getResourceId(i, 0);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   227
			String fileName = filenames.getString(i);
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   228
			File f = new File(targetDir, fileName);
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6839
diff changeset
   229
			writeStreamToFile(resources.openRawResource(resId), f);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   230
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   231
	}
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   232
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   233
	public static String readToString(InputStream is) throws IOException {
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   234
		try {
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   235
			ByteArrayOutputStream os = new ByteArrayOutputStream();
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   236
			byte[] buffer = new byte[8192];
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   237
			int size;
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   238
			while((size=is.read(buffer)) != -1) {
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   239
				os.write(buffer, 0, size);
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   240
			}
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   241
			return new String(os.toByteArray());
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   242
		} finally {
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   243
			closeQuietly(is);
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   244
		}
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   245
	}
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   246
	
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   247
	private static final char[] badFilenameChars = new char[] { '/', '\\', ':', '*', '?', '\"', '<', '>', '|', '.', '\0' };
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   248
	
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   249
	/**
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   250
	 * Modify the given String so that it can be used as part of a filename
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   251
	 * without causing problems from illegal/special characters.
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   252
	 * 
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   253
	 * The result should be similar to the input, but isn't necessarily
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   254
	 * reversible.
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   255
	 */
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   256
	public static String replaceBadChars(String name) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   257
		if (name == null || name.trim().length()==0) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   258
			return "_";
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   259
		}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   260
		name = name.trim();
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   261
		for (char badChar : badFilenameChars) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   262
			name = name.replace(badChar, '_');
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   263
		}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   264
		return name;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
   265
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   266
}