project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/util/FileUtils.java
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 7586 33924ff4af50
permissions -rw-r--r--
fixwhitespace and dos2unix
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 {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    43
    private static final String ROOT_DIR = "Data";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    44
    private static final String TAG = FileUtils.class.getSimpleName();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    46
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    47
     * @return true if the data path is currently available. However, it can vanish at any time so
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    48
     * normally you should just try to use it and rely on the exceptions.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    49
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    50
    public static boolean isDataPathAvailable() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    51
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    52
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    53
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    54
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    55
     * get the path to which we should download all the data files
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    56
     * @param c context
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    57
     * @return The directory
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    58
     * @throws FileNotFoundException if external storage is not available at the moment
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    59
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    60
    public static File getCachePath(Context c) throws FileNotFoundException {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    61
        File cachePath = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    62
        if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    63
            cachePath = PreFroyoSDCardDir.getDownloadPath(c);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    64
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    65
            cachePath = FroyoSDCardDir.getDownloadPath(c);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    66
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    67
        if(cachePath==null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    68
            throw new FileNotFoundException("External storage is currently unavailable");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    69
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    70
            return cachePath;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    71
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    74
    public static File getDataPathFile(Context c, String...subpath) throws FileNotFoundException {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    75
        File file = new File(getCachePath(c), ROOT_DIR);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    76
        for(String pathcomponent : subpath) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    77
            file = new File(file, pathcomponent);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    78
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    79
        return file;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    80
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    81
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    82
    @TargetApi(8)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    83
    private static class FroyoSDCardDir{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    84
        public static File getDownloadPath(Context c){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    85
            return c.getExternalCacheDir();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    86
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    89
    private static class PreFroyoSDCardDir{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    90
        public static File getDownloadPath(Context c){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    91
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    92
                File extStorageDir = Environment.getExternalStorageDirectory();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    93
                if(extStorageDir != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    94
                    return new File(extStorageDir, "Hedgewars");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    95
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    96
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    97
            return null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    98
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    99
    }
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   100
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   101
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   102
     * Return a File array with all the files from dirName
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   103
     * @param c
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   104
     * @param dirName
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   105
     * @return
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   106
     * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dirName" does not exist
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   107
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   108
    public static File[] getFilesFromRelativeDir(Context c, String dirName) throws FileNotFoundException {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   109
        File f = 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
   110
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   111
        if(f.isDirectory()) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   112
            return f.listFiles();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   113
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   114
            throw new FileNotFoundException("Directory "+dirName+" does not exist.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   115
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   116
    }
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   117
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   118
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   119
     * Checks if this directory has a file with suffix suffix
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   120
     * @param f - directory
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   121
     * @return
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   122
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   123
    public static boolean hasFileWithSuffix(File f, String suffix){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   124
        if(f.isDirectory()){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   125
            for(String s : f.list()){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   126
                if(s.endsWith(suffix)) return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   127
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   128
            return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   129
        }else{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   130
            return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   131
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   132
    }
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   133
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   134
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   135
     * Gives back all dirs which contain a file with suffix fileSuffix
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   136
     * @param c
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   137
     * @param path
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   138
     * @param fileSuffix
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   139
     * @return
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   140
     * @throws FileNotFoundException If the sdcard is not available or the subdirectory "path" does not exist
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   141
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   142
    public static List<String> getDirsWithFileSuffix(Context c, String path, String fileSuffix) throws FileNotFoundException{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   143
        File[] files = getFilesFromRelativeDir(c,path);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   144
        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
   145
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   146
        for(File f : files){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   147
            if(hasFileWithSuffix(f, fileSuffix)) ret.add(f.getName());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   148
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   149
        return ret;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   150
    }
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   151
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   152
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   153
     * Get all files from directory dir which have the given suffix
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   154
     * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dir" does not exist
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   155
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   156
    public static List<String> getFileNamesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix) throws FileNotFoundException{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   157
        File[] files = FileUtils.getFilesFromRelativeDir(c, dir);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   158
        List<String> ret = new ArrayList<String>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   159
        for(File file : files){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   160
            String s = file.getName();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   161
            if(s.endsWith(suffix)){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   162
                if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   163
                else ret.add(s);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   164
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   165
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   166
        return ret;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   167
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   168
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   169
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   170
     * Close a resource (possibly null), ignoring any IOException.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   171
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   172
    public static void closeQuietly(Closeable c) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   173
        if(c!=null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   174
            try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   175
                c.close();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   176
            } catch(IOException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   177
                Log.w(TAG, e);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   178
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   179
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   180
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   181
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   182
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   183
     * Write all data from the input stream to the file, creating or overwriting it.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   184
     * The input stream will be closed.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   185
     *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   186
     * @throws IOException
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   187
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   188
    public static void writeStreamToFile(InputStream is, File file) throws IOException {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   189
        OutputStream os = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   190
        byte[] buffer = new byte[8192];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   191
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   192
            os = new FileOutputStream(file);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   193
            int size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   194
            while((size=is.read(buffer)) != -1) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   195
                os.write(buffer, 0, size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   196
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   197
            os.close(); // Important to close this non-quietly, in case of exceptions when flushing
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   198
        } finally {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   199
            FileUtils.closeQuietly(is);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   200
            FileUtils.closeQuietly(os);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   201
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   202
    }
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   203
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   204
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   205
     * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   206
     * @param c
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   207
     * @param sourceResId
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   208
     * @param directory
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   209
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   210
    public static void resRawToFilesDir(Context c, int sourceResId, int targetFilenames, String directory) throws IOException {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   211
        File targetDir = new File(c.getFilesDir(), directory);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   212
        targetDir.mkdirs();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   213
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   214
        //Get an array with the resource files ID
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   215
        Resources resources = c.getResources();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   216
        TypedArray ta = resources.obtainTypedArray(sourceResId);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   217
        TypedArray filenames = resources.obtainTypedArray(targetFilenames);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   218
        for(int i = 0; i < ta.length(); i++){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   219
            int resId =  ta.getResourceId(i, 0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   220
            String fileName = filenames.getString(i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   221
            File f = new File(targetDir, fileName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   222
            writeStreamToFile(resources.openRawResource(resId), f);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   223
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   224
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   225
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   226
    public static String readToString(InputStream is) throws IOException {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   227
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   228
            ByteArrayOutputStream os = new ByteArrayOutputStream();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   229
            byte[] buffer = new byte[8192];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   230
            int size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   231
            while((size=is.read(buffer)) != -1) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   232
                os.write(buffer, 0, size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   233
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   234
            return new String(os.toByteArray());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   235
        } finally {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   236
            closeQuietly(is);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   237
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   238
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   239
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   240
    private static final char[] badFilenameChars = new char[] { '/', '\\', ':', '*', '?', '\"', '<', '>', '|', '.', '\0' };
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
   241
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   242
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   243
     * Modify the given String so that it can be used as part of a filename
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   244
     * without causing problems from illegal/special characters.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   245
     *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   246
     * The result should be similar to the input, but isn't necessarily
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   247
     * reversible.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   248
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   249
    public static String replaceBadChars(String name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   250
        if (name == null || name.trim().length()==0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   251
            return "_";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   252
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   253
        name = name.trim();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   254
        for (char badChar : badFilenameChars) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   255
            name = name.replace(badChar, '_');
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   256
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   257
        return name;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   258
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   259
}