1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.backends.android; 18 19 import java.io.IOException; 20 21 import android.content.res.AssetManager; 22 import android.os.Environment; 23 24 import com.badlogic.gdx.Files; 25 import com.badlogic.gdx.Gdx; 26 import com.badlogic.gdx.files.FileHandle; 27 import com.badlogic.gdx.utils.GdxRuntimeException; 28 29 /** @author mzechner 30 * @author Nathan Sweet */ 31 public class AndroidFiles implements Files { 32 protected final String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"; 33 protected final String localpath; 34 35 protected final AssetManager assets; 36 private ZipResourceFile expansionFile = null; 37 AndroidFiles(AssetManager assets)38 public AndroidFiles (AssetManager assets) { 39 this.assets = assets; 40 localpath = sdcard; 41 } 42 AndroidFiles(AssetManager assets, String localpath)43 public AndroidFiles (AssetManager assets, String localpath) { 44 this.assets = assets; 45 this.localpath = localpath.endsWith("/") ? localpath : localpath + "/"; 46 } 47 48 @Override getFileHandle(String path, FileType type)49 public FileHandle getFileHandle (String path, FileType type) { 50 FileHandle handle = new AndroidFileHandle(type == FileType.Internal ? assets : null, path, type); 51 if (expansionFile != null && type == FileType.Internal) handle = getZipFileHandleIfExists(handle, path); 52 return handle; 53 } 54 getZipFileHandleIfExists(FileHandle handle, String path)55 private FileHandle getZipFileHandleIfExists (FileHandle handle, String path) { 56 try { 57 assets.open(path).close(); // Check if file exists. 58 return handle; 59 } catch (Exception ex) { 60 // try APK expansion instead 61 FileHandle zipHandle = new AndroidZipFileHandle(path); 62 if (!zipHandle.isDirectory()) 63 return zipHandle; 64 else if (zipHandle.exists()) return zipHandle; 65 } 66 return handle; 67 } 68 69 @Override classpath(String path)70 public FileHandle classpath (String path) { 71 return new AndroidFileHandle(null, path, FileType.Classpath); 72 } 73 74 @Override internal(String path)75 public FileHandle internal (String path) { 76 FileHandle handle = new AndroidFileHandle(assets, path, FileType.Internal); 77 if (expansionFile != null) handle = getZipFileHandleIfExists(handle, path); 78 return handle; 79 } 80 81 @Override external(String path)82 public FileHandle external (String path) { 83 return new AndroidFileHandle(null, path, FileType.External); 84 } 85 86 @Override absolute(String path)87 public FileHandle absolute (String path) { 88 return new AndroidFileHandle(null, path, FileType.Absolute); 89 } 90 91 @Override local(String path)92 public FileHandle local (String path) { 93 return new AndroidFileHandle(null, path, FileType.Local); 94 } 95 96 @Override getExternalStoragePath()97 public String getExternalStoragePath () { 98 return sdcard; 99 } 100 101 @Override isExternalStorageAvailable()102 public boolean isExternalStorageAvailable () { 103 return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); 104 } 105 106 @Override getLocalStoragePath()107 public String getLocalStoragePath () { 108 return localpath; 109 } 110 111 @Override isLocalStorageAvailable()112 public boolean isLocalStorageAvailable () { 113 return true; 114 } 115 116 /** 117 * This method can be called to set the version code of the APK expansion 118 * file(s) used by the application 119 * 120 * @param mainVersion 121 * - version code of the main expansion file 122 * @param patchVersion 123 * - version code of the patch expansion file 124 * 125 * @return true if the APK expansion file could be opened, false otherwise 126 */ setAPKExpansion(int mainVersion, int patchVersion)127 public boolean setAPKExpansion(int mainVersion, int patchVersion) { 128 try { 129 expansionFile = APKExpansionSupport.getAPKExpansionZipFile( 130 ((AndroidApplication) Gdx.app).getBaseContext(), 131 mainVersion, patchVersion); 132 } catch (IOException ex) { 133 throw new GdxRuntimeException("APK expansion main version " + mainVersion + " or patch version " + patchVersion + " couldn't be opened!"); 134 } 135 return expansionFile != null; 136 } 137 138 /** @return The application's APK extension file */ getExpansionFile()139 public ZipResourceFile getExpansionFile() { 140 return expansionFile; 141 } 142 } 143