|
| 1 | +/* |
| 2 | + * Copyright 2018 Mr Duy |
| 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.duy.ccppcompiler.pkgmanager; |
| 18 | + |
| 19 | +import android.content.Context; |
| 20 | +import android.content.SharedPreferences; |
| 21 | +import android.preference.PreferenceManager; |
| 22 | +import android.support.annotation.Nullable; |
| 23 | + |
| 24 | +import com.pdaxrom.utils.Utils; |
| 25 | + |
| 26 | +import java.io.BufferedReader; |
| 27 | +import java.io.DataInputStream; |
| 28 | +import java.io.File; |
| 29 | +import java.io.FileDescriptor; |
| 30 | +import java.io.FileInputStream; |
| 31 | +import java.io.FilenameFilter; |
| 32 | +import java.io.InputStreamReader; |
| 33 | + |
| 34 | +import jackpal.androidterm.TermSettings; |
| 35 | + |
| 36 | +/** |
| 37 | + * Created by Duy on 16-May-18. |
| 38 | + */ |
| 39 | + |
| 40 | +public class Environment { |
| 41 | + public static final String APPLICATION_DIR_NAME = "CCPlusPlusNIDE"; |
| 42 | + private static final String TAG = "Environment"; |
| 43 | + |
| 44 | + /** |
| 45 | + * Internal storage |
| 46 | + */ |
| 47 | + public static String getToolchainsDir(Context context) { |
| 48 | + File path = new File(context.getCacheDir().getParentFile(), "root"); |
| 49 | + return mkdirIfNotExist(path); |
| 50 | + } |
| 51 | + |
| 52 | + public static String getCCtoolsDir(Context c) { |
| 53 | + File path = new File(getToolchainsDir(c), "cctools"); |
| 54 | + return mkdirIfNotExist(path); |
| 55 | + } |
| 56 | + |
| 57 | + public static String getServiceDir(Context context) { |
| 58 | + String path = new File(getToolchainsDir(context), "/cctools/services").getAbsolutePath(); |
| 59 | + return mkdirIfNotExist(path); |
| 60 | + } |
| 61 | + |
| 62 | + public static String getHomeDir(Context context) { |
| 63 | + String path = new File(getToolchainsDir(context), "/cctools/home").getAbsolutePath(); |
| 64 | + return mkdirIfNotExist(path); |
| 65 | + } |
| 66 | + |
| 67 | + public static String getInstalledPackageDir(Context context) { |
| 68 | + String path = new File(getToolchainsDir(context), "installed").getAbsolutePath(); |
| 69 | + return mkdirIfNotExist(path); |
| 70 | + } |
| 71 | + |
| 72 | + public static String getDalvikCacheDir(Context context) { |
| 73 | + File path = new File(getToolchainsDir(context), "cctools/var/dalvik/dalvik-cache"); |
| 74 | + return mkdirIfNotExist(path); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return temp directory for execute file |
| 79 | + */ |
| 80 | + public static String getTmpExeDir(Context context) { |
| 81 | + File file = new File(Environment.getToolchainsDir(context), "tmpdir"); |
| 82 | + return mkdirIfNotExist(file); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * Return sdcard/CCPlusPlusNIDE folder |
| 88 | + */ |
| 89 | + public static String getSdCardHomeDir() { |
| 90 | + File path = new File(android.os.Environment.getExternalStorageDirectory().getPath(), APPLICATION_DIR_NAME); |
| 91 | + return mkdirIfNotExist(path); |
| 92 | + } |
| 93 | + |
| 94 | + public static String getSdCardBackupDir() { |
| 95 | + String path = getSdCardHomeDir() + "/backup"; |
| 96 | + return mkdirIfNotExist(path); |
| 97 | + } |
| 98 | + |
| 99 | + public static String getSdCardTmpDir() { |
| 100 | + String path = getSdCardHomeDir() + "/tmp"; |
| 101 | + return mkdirIfNotExist(path); |
| 102 | + } |
| 103 | + |
| 104 | + public static String getSdCardSourceDir() { |
| 105 | + return mkdirIfNotExist(new File(getSdCardHomeDir(), "src")); |
| 106 | + } |
| 107 | + |
| 108 | + private static String mkdirIfNotExist(String path) { |
| 109 | + File file = new File(path); |
| 110 | + return mkdirIfNotExist(file); |
| 111 | + } |
| 112 | + |
| 113 | + private static String mkdirIfNotExist(File file) { |
| 114 | + if (!file.exists()) { |
| 115 | + file.mkdir(); |
| 116 | + } |
| 117 | + return file.getAbsolutePath(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * ANDROID_SOCKET_zygote=9 |
| 122 | + * EMULATED_STORAGE_SOURCE=/mnt/shell/emulated |
| 123 | + * ANDROID_STORAGE=/storage |
| 124 | + * ANDROID_BOOTLOGO=1 |
| 125 | + * EXTERNAL_STORAGE=/storage/emulated/legacy |
| 126 | + * ANDROID_ASSETS=/system/app |
| 127 | + * ASEC_MOUNTPOINT=/mnt/asec |
| 128 | + * PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin |
| 129 | + * LOOP_MOUNTPOINT=/mnt/obb |
| 130 | + * BOOTCLASSPATH=/system/framework/core.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/framework2.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/webviewchromium.jar |
| 131 | + * EMULATED_STORAGE_TARGET=/storage/emulated |
| 132 | + * ANDROID_DATA=/data |
| 133 | + * ANDROID_PROPERTY_WORKSPACE=8,0 |
| 134 | + * ANDROID_ROOT=/system |
| 135 | + * LD_LIBRARY_PATH=/vendor/lib:/system/lib |
| 136 | + */ |
| 137 | + public static String[] buildDefaultEnv(Context context) { |
| 138 | + final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); |
| 139 | + final TermSettings settings = new TermSettings(context.getResources(), pref); |
| 140 | + final String cctoolsDir = getCCtoolsDir(context); |
| 141 | + final String tmpExeDir = getTmpExeDir(context); |
| 142 | + |
| 143 | + return new String[]{ |
| 144 | + "TMPDIR=" + tmpExeDir, |
| 145 | + "TMPEXEDIR=" + tmpExeDir, |
| 146 | + "TEMPDIR=" + tmpExeDir, |
| 147 | + "TEMP=" + tmpExeDir, |
| 148 | + "PATH=" + joinPath(cctoolsDir + "/bin", cctoolsDir + "/sbin", System.getenv("PATH")), |
| 149 | + "HOME=" + getHomeDir(context), |
| 150 | + |
| 151 | + "ANDROID_ASSETS=" + getEnv("ANDROID_ASSETS", "/system/app"), |
| 152 | + "ANDROID_BOOTLOGO=" + getEnv("ANDROID_BOOTLOGO", "1"), |
| 153 | + "ANDROID_DATA=" + joinPath(cctoolsDir + "/var/dalvik", getEnv("ANDROID_DATA", null)), |
| 154 | + "ANDROID_ROOT=" + getEnv("ANDROID_ROOT", "/system"), |
| 155 | + "ANDROID_PROPERTY_WORKSPACE=" + getEnv(context, "ANDROID_PROPERTY_WORKSPACE"), |
| 156 | + "BOOTCLASSPATH=" + getBootClassPath(), |
| 157 | + "EXTERNAL_STORAGE=" + android.os.Environment.getExternalStorageDirectory().getPath(), |
| 158 | + "LD_LIBRARY_PATH=" + joinPath(cctoolsDir + "/lib", getEnv("LD_LIBRARY_PATH", null)), |
| 159 | + |
| 160 | + "CCTOOLSDIR=" + cctoolsDir, |
| 161 | + "CCTOOLSRES=" + context.getPackageResourcePath(), |
| 162 | + "CFGDIR=" + getShareDir(context), |
| 163 | + "SHELL=" + getShell(context), |
| 164 | + "TERM=" + settings.getTermType(), |
| 165 | + "PS1=$ ", |
| 166 | + "SDDIR=" + getSdCardHomeDir(), |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + private static String getShareDir(Context context) { |
| 171 | + File file = new File(getCCtoolsDir(context), "share"); |
| 172 | + return mkdirIfNotExist(file); |
| 173 | + } |
| 174 | + |
| 175 | + private static String getBootClassPath() { |
| 176 | + String bootClassPath = getEnv("BOOTCLASSPATH", null); |
| 177 | + if (bootClassPath == null) { |
| 178 | + bootClassPath = findBootClassPath(); |
| 179 | + } |
| 180 | + if (bootClassPath == null || bootClassPath.isEmpty()) { |
| 181 | + bootClassPath = "/system/framework/core.jar:" + |
| 182 | + "/system/framework/ext.jar:" + |
| 183 | + "/system/framework/framework.jar:" + |
| 184 | + "/system/framework/android.policy.jar:" + |
| 185 | + "/system/framework/services.jar"; |
| 186 | + } |
| 187 | + return bootClassPath; |
| 188 | + } |
| 189 | + |
| 190 | + @Nullable |
| 191 | + private static String findBootClassPath() { |
| 192 | + String classPath = null; |
| 193 | + File dir = new File("/system/framework"); |
| 194 | + if (dir.exists() && dir.isDirectory()) { |
| 195 | + FilenameFilter filter = new FilenameFilter() { |
| 196 | + public boolean accept(File dir, String name) { |
| 197 | + String lowercaseName = name.toLowerCase(); |
| 198 | + return lowercaseName.endsWith(".jar"); |
| 199 | + } |
| 200 | + }; |
| 201 | + |
| 202 | + String[] list = dir.list(filter); |
| 203 | + for (int i = 0; i < list.length; i++) { |
| 204 | + String file = list[i]; |
| 205 | + if (i != 0) { |
| 206 | + classPath += ":"; |
| 207 | + } |
| 208 | + classPath += "/system/framework/" + file; |
| 209 | + } |
| 210 | + } |
| 211 | + return classPath; |
| 212 | + } |
| 213 | + |
| 214 | + private static String getEnv(String name, String defValue) { |
| 215 | + String value = System.getenv(name); |
| 216 | + if (value != null) { |
| 217 | + return value; |
| 218 | + } else { |
| 219 | + return defValue; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + private static String joinPath(String... paths) { |
| 224 | + StringBuilder sb = new StringBuilder(); |
| 225 | + for (String path : paths) { |
| 226 | + if (path != null && !path.isEmpty()) { |
| 227 | + if (sb.length() != 0) { |
| 228 | + sb.append(File.pathSeparator); |
| 229 | + } |
| 230 | + sb.append(path); |
| 231 | + } |
| 232 | + } |
| 233 | + return sb.toString(); |
| 234 | + } |
| 235 | + |
| 236 | + public static String getShell(Context context) { |
| 237 | + return "/system/bin/sh"; |
| 238 | + } |
| 239 | + |
| 240 | + protected static String getEnv(Context context, String variable) { |
| 241 | + String ret = null; |
| 242 | + final String cctoolsDir = getCCtoolsDir(context); |
| 243 | + String[] envp = { |
| 244 | + "TMPDIR=" + getSdCardTmpDir(), |
| 245 | + "PATH=" + joinPath(cctoolsDir + "/bin", cctoolsDir + "/sbin", System.getenv("PATH")), |
| 246 | + "ANDROID_ASSETS=" + getEnv("ANDROID_ASSETS", "/system/app"), |
| 247 | + "ANDROID_BOOTLOGO=" + getEnv("ANDROID_BOOTLOGO", "1"), |
| 248 | + "ANDROID_DATA=" + joinPath(cctoolsDir + "/var/dalvik", getEnv("ANDROID_DATA", null)), |
| 249 | + "ANDROID_ROOT=" + getEnv("ANDROID_ROOT", "/system"), |
| 250 | + "CCTOOLSDIR=" + cctoolsDir, |
| 251 | + "CCTOOLSRES=" + context.getPackageResourcePath(), |
| 252 | + "LD_LIBRARY_PATH=" + joinPath(cctoolsDir + "/lib", getEnv("LD_LIBRARY_PATH", null)), |
| 253 | + "HOME=" + getHomeDir(context), |
| 254 | + "SHELL=" + getShell(context), |
| 255 | + "TERM=xterm", |
| 256 | + "PS1=$ ", |
| 257 | + "SDDIR=" + getSdCardHomeDir(), |
| 258 | + "EXTERNAL_STORAGE=" + android.os.Environment.getExternalStorageDirectory().getPath(), |
| 259 | + }; |
| 260 | + String[] argv = {"/system/bin/sh", "-c", "set"}; |
| 261 | + int[] pId = new int[1]; |
| 262 | + FileDescriptor fd = Utils.createSubProcess(cctoolsDir, argv[0], argv, envp, pId); |
| 263 | + FileInputStream fis = new FileInputStream(fd); |
| 264 | + DataInputStream in = new DataInputStream(fis); |
| 265 | + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
| 266 | + String line; |
| 267 | + try { |
| 268 | + while ((line = reader.readLine()) != null) { |
| 269 | + if (line.startsWith(variable + "=")) { |
| 270 | + if (line.contains("=")) { |
| 271 | + ret = line.substring(line.indexOf("=") + 1); |
| 272 | + break; |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + in.close(); |
| 277 | + Utils.waitFor(pId[0]); |
| 278 | + } catch (Exception ignored) { |
| 279 | + } |
| 280 | + return ret; |
| 281 | + } |
| 282 | + |
| 283 | + public static String[] join(String[] env1, String[] env2) { |
| 284 | + String[] env = new String[env1.length + env2.length]; |
| 285 | + System.arraycopy(env1, 0, env, 0, env1.length); |
| 286 | + System.arraycopy(env2, 0, env, env1.length, env2.length); |
| 287 | + return env; |
| 288 | + } |
| 289 | + |
| 290 | + public static void mkdirs(Context context) { |
| 291 | + Environment.getServiceDir(context); |
| 292 | + Environment.getHomeDir(context); |
| 293 | + Environment.getToolchainsDir(context); |
| 294 | + |
| 295 | + Environment.getSdCardHomeDir(); |
| 296 | + Environment.getSdCardBackupDir(); |
| 297 | + Environment.getSdCardTmpDir(); |
| 298 | + |
| 299 | + } |
| 300 | + |
| 301 | + public static String getSdCardDir() { |
| 302 | + return android.os.Environment.getExternalStorageDirectory().getAbsolutePath(); |
| 303 | + } |
| 304 | + |
| 305 | + public static String getSdCardExampleDir() { |
| 306 | + File file = new File(getSdCardHomeDir(), "Examples"); |
| 307 | + return mkdirIfNotExist(file); |
| 308 | + } |
| 309 | + |
| 310 | +} |
0 commit comments