Jose Pascoal | 02b849e | 2014-06-26 17:07:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Fairphone Project |
| 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.fairphone.updater.tools; |
| 18 | |
Jose Pascoal | 4fe7ba5 | 2015-03-13 16:54:31 +0000 | [diff] [blame] | 19 | import android.app.DownloadManager; |
Jose Pascoal | 61e2476 | 2014-09-30 19:44:36 +0100 | [diff] [blame] | 20 | import android.content.Context; |
Jose Pascoal | e4c48e3 | 2014-10-03 19:55:39 +0100 | [diff] [blame] | 21 | import android.content.Intent; |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 22 | import android.content.res.Resources; |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 23 | import android.database.Cursor; |
Filipe Gonçalves | 7281778 | 2015-03-19 15:29:05 +0000 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 25 | import android.net.Uri; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 26 | import android.os.Build; |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 27 | import android.os.Environment; |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 28 | import android.os.PowerManager; |
Jose Pascoal | da00b38 | 2015-01-20 18:39:28 +0000 | [diff] [blame] | 29 | import android.text.TextUtils; |
Jose Pascoal | 02b849e | 2014-06-26 17:07:51 +0100 | [diff] [blame] | 30 | import android.util.Log; |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 31 | import android.widget.Toast; |
Jose Pascoal | 02b849e | 2014-06-26 17:07:51 +0100 | [diff] [blame] | 32 | |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 33 | import com.fairphone.updater.BetaEnabler; |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 34 | import com.fairphone.updater.R; |
Jose Pascoal | e4c48e3 | 2014-10-03 19:55:39 +0100 | [diff] [blame] | 35 | import com.fairphone.updater.UpdaterService; |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 36 | import com.fairphone.updater.data.DownloadableItem; |
| 37 | import com.fairphone.updater.data.Store; |
Jose Pascoal | 055562d | 2015-03-19 19:31:38 +0000 | [diff] [blame] | 38 | import com.fairphone.updater.data.UpdaterData; |
Jose Pascoal | 7bf83a0 | 2014-10-13 18:30:18 +0100 | [diff] [blame] | 39 | import com.fairphone.updater.data.Version; |
| 40 | import com.fairphone.updater.data.VersionParserHelper; |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 41 | import com.stericson.RootTools.RootTools; |
| 42 | import com.stericson.RootTools.exceptions.RootDeniedException; |
| 43 | import com.stericson.RootTools.execution.CommandCapture; |
| 44 | import com.stericson.RootTools.execution.Shell; |
Jose Pascoal | 61e2476 | 2014-09-30 19:44:36 +0100 | [diff] [blame] | 45 | |
Tiago Costa | d7e951f | 2015-03-02 17:37:33 +0000 | [diff] [blame] | 46 | import java.io.DataOutputStream; |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 47 | import java.io.File; |
| 48 | import java.io.FileInputStream; |
| 49 | import java.io.FileNotFoundException; |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 50 | import java.io.FileOutputStream; |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 51 | import java.io.IOException; |
| 52 | import java.io.InputStream; |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 53 | import java.io.PrintWriter; |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 54 | import java.math.BigInteger; |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 55 | import java.nio.channels.FileChannel; |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 56 | import java.security.MessageDigest; |
| 57 | import java.security.NoSuchAlgorithmException; |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 58 | import java.util.HashMap; |
| 59 | import java.util.Map; |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 60 | import java.util.NoSuchElementException; |
| 61 | import java.util.Scanner; |
| 62 | import java.util.concurrent.TimeoutException; |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 63 | import java.util.regex.Matcher; |
| 64 | import java.util.regex.Pattern; |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 65 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 66 | public class Utils |
| 67 | { |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 68 | private static final String TAG = Utils.class.getSimpleName(); |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 69 | private static final int DELAY_100_MILLIS = 100; |
| 70 | public static final int DELAY_HALF_SECOND = 500; |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 71 | public static final long SECONDS_IN_MINUTE = 60L; |
| 72 | public static final long MINUTES_IN_HOUR = 60L; |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 73 | |
| 74 | private static final double BUFFER_1024_BYTES = 1024d; |
Jose Pascoal | cdd8204 | 2015-02-06 13:04:26 +0000 | [diff] [blame] | 75 | // --Commented out by Inspection (06/02/2015 12:27):public static final int BUFFER_SIZE_4_KBYTES = 4096; |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 76 | public static final int BUFFER_SIZE_2_KBYTES = 2048; |
| 77 | private static final int BUFFER_SIZE_8_KBYTES = 8192; |
| 78 | public static final int BUFFER_SIZE_10_MBYTES = 10240; |
| 79 | private static final int RADIX_BASE_16 = 16; |
| 80 | private static final double PERCENT_100 = 100d; |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 81 | private static final char CHAR_SPACE = ' '; |
| 82 | private static final char CHAR_ZERO = '0'; |
Jose Pascoal | 055562d | 2015-03-19 19:31:38 +0000 | [diff] [blame] | 83 | public static final int GAPPS_STORE_NUMBER = 0; |
Jose Pascoal | 02b849e | 2014-06-26 17:07:51 +0100 | [diff] [blame] | 84 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 85 | private static double getPartitionSizeInGBytes(File path) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 86 | { |
| 87 | double availableBlocks = getPartitionSizeInBytes(path); |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 88 | double sizeInGB = ((availableBlocks / BUFFER_1024_BYTES) / BUFFER_1024_BYTES) / BUFFER_1024_BYTES; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 89 | Log.d(TAG, path.getPath() + " size(GB): " + sizeInGB); |
| 90 | return sizeInGB; |
| 91 | } |
Jose Pascoal | 02b849e | 2014-06-26 17:07:51 +0100 | [diff] [blame] | 92 | |
Jose Pascoal | cdd8204 | 2015-02-06 13:04:26 +0000 | [diff] [blame] | 93 | // --Commented out by Inspection START (06/02/2015 12:26): |
| 94 | // public static double getPartitionSizeInMBytes(File path) |
| 95 | // { |
| 96 | // double availableBlocks = getPartitionSizeInBytes(path); |
| 97 | // double sizeInMB = ((availableBlocks / BUFFER_1024_BYTES)) / BUFFER_1024_BYTES; |
| 98 | // return sizeInMB; |
| 99 | // } |
| 100 | // --Commented out by Inspection STOP (06/02/2015 12:26) |
Jose Pascoal | e4c48e3 | 2014-10-03 19:55:39 +0100 | [diff] [blame] | 101 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 102 | private static long getPartitionSizeInBytes(File path) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 103 | { |
| 104 | android.os.StatFs stat = new android.os.StatFs(path.getPath()); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 105 | long blockSize, blockCount; |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 106 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 107 | blockSize = stat.getBlockSizeLong(); |
| 108 | blockCount = stat.getBlockCountLong(); |
| 109 | } else { |
| 110 | // deprectation warnings disabled due to the need to support SDK 17 (FP1) |
| 111 | //noinspection deprecation |
| 112 | blockSize = stat.getBlockSize(); |
| 113 | //noinspection deprecation |
| 114 | blockCount = stat.getBlockCount(); |
| 115 | } |
| 116 | return blockCount * blockSize; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 117 | } |
Jose Pascoal | e4c48e3 | 2014-10-03 19:55:39 +0100 | [diff] [blame] | 118 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 119 | public static long getAvailablePartitionSizeInBytes(File path) |
| 120 | { |
| 121 | android.os.StatFs stat = new android.os.StatFs(path.getPath()); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 122 | |
| 123 | long blockSize, blockCount; |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 124 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 125 | blockSize = stat.getBlockSizeLong(); |
| 126 | blockCount = stat.getAvailableBlocksLong(); |
| 127 | } else { |
| 128 | // deprectation warnings disabled due to the need to support SDK 17 (FP1) |
| 129 | //noinspection deprecation |
| 130 | blockSize = stat.getBlockSize(); |
| 131 | //noinspection deprecation |
| 132 | blockCount = stat.getAvailableBlocks(); |
| 133 | } |
| 134 | return blockCount * blockSize; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 135 | } |
Jose Pascoal | e4c48e3 | 2014-10-03 19:55:39 +0100 | [diff] [blame] | 136 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 137 | public static void startUpdaterService(Context context, boolean forceDownload) |
| 138 | { |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 139 | Intent i = new Intent(context, UpdaterService.class); |
| 140 | i.putExtra(UpdaterService.EXTRA_FORCE_CONFIG_FILE_DOWNLOAD, forceDownload); |
| 141 | context.startService(i); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 142 | } |
Jose Pascoal | e4c48e3 | 2014-10-03 19:55:39 +0100 | [diff] [blame] | 143 | |
Jose Pascoal | cdd8204 | 2015-02-06 13:04:26 +0000 | [diff] [blame] | 144 | // --Commented out by Inspection START (06/02/2015 12:26): |
| 145 | // public static void stopUpdaterService(Context context) |
| 146 | // { |
| 147 | // boolean isRunning = isServiceRunning(context); |
| 148 | // |
| 149 | // if (isRunning) |
| 150 | // { |
| 151 | // Log.i(TAG, "Stoping Updater Service..."); |
| 152 | // Intent i = new Intent(context, UpdaterService.class); |
| 153 | // context.stopService(i); |
| 154 | // try |
| 155 | // { |
| 156 | // Thread.sleep(DELAY_100_MILLIS * 2); |
| 157 | // } catch (InterruptedException e) |
| 158 | // { |
| 159 | // Log.w(TAG, "Stop Updater service delay error: " + e.getLocalizedMessage()); |
| 160 | // } |
| 161 | // } |
| 162 | // } |
| 163 | // --Commented out by Inspection STOP (06/02/2015 12:26) |
Jose Pascoal | e4c48e3 | 2014-10-03 19:55:39 +0100 | [diff] [blame] | 164 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 165 | // ************************************************************************************************************** |
| 166 | // HELPERS |
| 167 | // ************************************************************************************************************** |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 168 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 169 | public static boolean checkMD5(String md5, File updateFile) |
| 170 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 171 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 172 | if (updateFile == null || !updateFile.exists()) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 173 | { |
| 174 | return false; |
| 175 | } |
| 176 | |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 177 | if (md5 == null || md5.isEmpty()) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 178 | { |
| 179 | Log.e(TAG, "MD5 String NULL or UpdateFile NULL"); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | String calculatedDigest = calculateMD5(updateFile); |
| 184 | if (calculatedDigest == null) |
| 185 | { |
| 186 | Log.e(TAG, "calculatedDigest NULL"); |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | return calculatedDigest.equalsIgnoreCase(md5); |
| 191 | } |
| 192 | |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 193 | public static String calculateMD5(File updateFile) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 194 | { |
| 195 | MessageDigest digest; |
| 196 | try |
| 197 | { |
| 198 | digest = MessageDigest.getInstance("MD5"); |
| 199 | } catch (NoSuchAlgorithmException e) |
| 200 | { |
| 201 | Log.e(TAG, "Exception while getting Digest", e); |
| 202 | return null; |
| 203 | } |
| 204 | |
| 205 | InputStream is; |
| 206 | try |
| 207 | { |
| 208 | is = new FileInputStream(updateFile); |
| 209 | } catch (FileNotFoundException e) |
| 210 | { |
| 211 | Log.e(TAG, "Exception while getting FileInputStream", e); |
| 212 | return null; |
| 213 | } |
| 214 | |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 215 | byte[] buffer = new byte[BUFFER_SIZE_8_KBYTES]; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 216 | int read; |
| 217 | try |
| 218 | { |
| 219 | while ((read = is.read(buffer)) > 0) |
| 220 | { |
| 221 | digest.update(buffer, 0, read); |
| 222 | } |
| 223 | byte[] md5sum = digest.digest(); |
| 224 | BigInteger bigInt = new BigInteger(1, md5sum); |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 225 | String output = bigInt.toString(RADIX_BASE_16); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 226 | // Fill to 32 chars |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 227 | output = String.format("%32s", output).replace(CHAR_SPACE, CHAR_ZERO); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 228 | return output; |
| 229 | } catch (IOException e) |
| 230 | { |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 231 | Log.e(TAG, "Error digesting MD5: " + e.getLocalizedMessage()); |
Tiago Costa | 7c47e2d | 2014-11-18 16:50:35 +0000 | [diff] [blame] | 232 | return null; |
| 233 | // throw new RuntimeException("Unable to process file for MD5", e); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 234 | } finally |
| 235 | { |
| 236 | try |
| 237 | { |
| 238 | is.close(); |
| 239 | } catch (IOException e) |
| 240 | { |
| 241 | Log.e(TAG, "Exception on closing MD5 input stream", e); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | public static String getModelAndOS(Context context) |
| 247 | { |
| 248 | StringBuilder sb = new StringBuilder(); |
| 249 | |
| 250 | // attach the model and the os |
| 251 | sb.append("?"); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 252 | sb.append("model=").append(Build.MODEL.replaceAll("\\s", "")); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 253 | Version currentVersion = VersionParserHelper.getDeviceVersion(context); |
| 254 | |
| 255 | if (currentVersion != null) |
| 256 | { |
| 257 | sb.append("&"); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 258 | sb.append("os=").append(currentVersion.getAndroidVersion()); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return sb.toString(); |
| 262 | } |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 263 | |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 264 | public static void copy(File src, File dst) throws IOException { |
| 265 | if (PrivilegeChecker.isPrivilegedApp()) { |
| 266 | copyPrivileged(src, dst); |
| 267 | } else { |
| 268 | copyUnprivileged(src, dst); |
| 269 | } |
| 270 | } |
| 271 | |
Filipe Gonçalves | 2ba0f55 | 2015-02-16 12:53:15 +0000 | [diff] [blame] | 272 | private static void copyUnprivileged(File src, File dst) throws IOException { |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 273 | if (RootTools.isAccessGiven()) { |
| 274 | RootTools.copyFile(src.getPath(), dst.getPath(), false, false); |
| 275 | } else { |
| 276 | throw new IOException("No root permissions granted."); |
| 277 | } |
| 278 | } |
| 279 | |
Filipe Gonçalves | 2ba0f55 | 2015-02-16 12:53:15 +0000 | [diff] [blame] | 280 | private static void copyPrivileged(File src, File dst) throws IOException { |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 281 | FileInputStream inStream = new FileInputStream(src); |
| 282 | FileOutputStream outStream = new FileOutputStream(dst); |
| 283 | FileChannel inChannel = inStream.getChannel(); |
| 284 | FileChannel outChannel = outStream.getChannel(); |
| 285 | inChannel.transferTo(0, inChannel.size(), outChannel); |
| 286 | inStream.close(); |
| 287 | outStream.close(); |
| 288 | } |
| 289 | |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 290 | public static void clearCache() |
| 291 | { |
Jose Pascoal | 288dd2f | 2015-02-20 19:19:04 +0000 | [diff] [blame] | 292 | if(PrivilegeChecker.isPrivilegedApp()) { |
| 293 | File f = Environment.getDownloadCacheDirectory(); |
| 294 | File[] files = f.listFiles(); |
| 295 | if (files != null) { |
| 296 | Log.d(TAG, "Size: " + files.length); |
| 297 | for (File file : files) { |
| 298 | String filename = file.getName(); |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 299 | |
Jose Pascoal | 288dd2f | 2015-02-20 19:19:04 +0000 | [diff] [blame] | 300 | if (filename.endsWith(".zip")) { |
| 301 | final boolean delete = file.delete(); |
| 302 | if (delete) { |
| 303 | Log.d(TAG, "Deleted file " + filename); |
| 304 | } else { |
| 305 | Log.d(TAG, "Failed to delete file " + filename); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } else { |
| 311 | if(RootTools.isAccessGiven()) { |
| 312 | try { |
| 313 | Shell.runRootCommand(new CommandCapture(0, "rm -f *.zip")); |
| 314 | } catch (IOException | TimeoutException |RootDeniedException e) { |
| 315 | Log.w(TAG, "Failed to clear cache: " + e.getLocalizedMessage()); |
| 316 | } |
| 317 | } |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | public static boolean hasUnifiedPartition(Resources resources) |
| 322 | { |
| 323 | File path = Environment.getDataDirectory(); |
| 324 | double sizeInGB = Utils.getPartitionSizeInGBytes(path); |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 325 | double roundedSize = Math.ceil(sizeInGB * PERCENT_100) / PERCENT_100; |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 326 | Log.d(TAG, "/data size: " + roundedSize + "Gb"); |
| 327 | |
Jose Pascoal | 8d4458f | 2015-03-17 20:03:59 +0000 | [diff] [blame] | 328 | double fp1DataPartitionSize = (double) resources.getInteger(R.integer.FP1DataPartitionSizeMb) / BUFFER_1024_BYTES; |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 329 | // Add a little buffer to the 1gb default just in case |
| 330 | return roundedSize > fp1DataPartitionSize; |
| 331 | } |
| 332 | |
| 333 | public static String getPartitionDownloadPath(Resources resources) |
| 334 | { |
| 335 | String downloadPath = ""; |
| 336 | if (Build.MODEL.equals(resources.getString(R.string.FP1Model))) |
| 337 | { |
| 338 | downloadPath = |
| 339 | Utils.hasUnifiedPartition(resources) ? resources.getString(R.string.unifiedDataPartition) : resources |
| 340 | .getString(R.string.oneGBDataPartition); |
| 341 | } |
| 342 | return downloadPath; |
| 343 | } |
| 344 | |
| 345 | public static boolean canCopyToCache(File file) |
| 346 | { |
| 347 | double fileSize = file.length(); |
| 348 | double cacheSize = Utils.getPartitionSizeInBytes(Environment.getDownloadCacheDirectory()); |
Jose Pascoal | b980c86 | 2015-03-12 20:22:02 +0000 | [diff] [blame] | 349 | return fileSize > 0 && cacheSize >= fileSize; |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 350 | } |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 351 | |
| 352 | public static String getFilenameFromDownloadableItem(DownloadableItem item, boolean isVersion) |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 353 | { |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 354 | StringBuilder filename; |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 355 | |
| 356 | if(isVersion) |
| 357 | { |
| 358 | filename = getFilenameForItem(item, "update_"); |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | filename = getFilenameForItem(item, "store_"); |
| 363 | } |
| 364 | |
| 365 | return filename.toString(); |
| 366 | } |
| 367 | |
| 368 | private static StringBuilder getFilenameForItem(DownloadableItem item, String type) { |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 369 | StringBuilder filename = new StringBuilder(); |
| 370 | filename.append("fp_"); |
| 371 | if (item != null) |
| 372 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 373 | filename.append(type); |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 374 | filename.append(item.getNumber()); |
| 375 | } |
| 376 | filename.append(".zip"); |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 377 | return filename; |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 380 | public static String getDownloadTitleFromDownloadableItem(Resources resources, DownloadableItem item, boolean isVersion){ |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 381 | String title = ""; |
| 382 | if (item != null) |
| 383 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 384 | if (isVersion) |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 385 | { |
| 386 | Version version = (Version) item; |
| 387 | title = version.getName() + " " + version.getImageTypeDescription(resources); |
| 388 | } |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 389 | else |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 390 | { |
| 391 | Store store = (Store) item; |
| 392 | title = store.getName(); |
| 393 | } |
| 394 | } |
| 395 | return title; |
| 396 | } |
Jose Pascoal | da00b38 | 2015-01-20 18:39:28 +0000 | [diff] [blame] | 397 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 398 | public static boolean isDeviceUnsupported(Context context) |
Jose Pascoal | da00b38 | 2015-01-20 18:39:28 +0000 | [diff] [blame] | 399 | { |
| 400 | Version deviceVersion = VersionParserHelper.getDeviceVersion(context); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 401 | return deviceVersion == null || TextUtils.isEmpty(deviceVersion.getName()); |
Jose Pascoal | da00b38 | 2015-01-20 18:39:28 +0000 | [diff] [blame] | 402 | } |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 403 | |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 404 | private static Map<String,String> buildProps; |
| 405 | public static Map<String,String> getpropAll(){ |
| 406 | |
| 407 | if(buildProps==null) { |
| 408 | buildProps = new HashMap<>(); |
| 409 | ProcessBuilder pb = new ProcessBuilder("/system/bin/getprop"); |
| 410 | pb.redirectErrorStream(true); |
| 411 | Pattern propRegex = Pattern.compile("\\[([^\\]]+)\\]: \\[([^\\]]+)\\]"); |
| 412 | |
| 413 | Process p; |
| 414 | InputStream is = null; |
| 415 | try { |
| 416 | p = pb.start(); |
| 417 | is = p.getInputStream(); |
| 418 | Scanner scan = new Scanner(is); |
| 419 | String prop; |
| 420 | do { |
| 421 | prop = scan.nextLine(); |
| 422 | Matcher match = propRegex.matcher(prop); |
| 423 | if(match.find()){ |
| 424 | buildProps.put(match.group(1), match.group(2)); |
| 425 | } |
| 426 | } while(!prop.isEmpty()); |
| 427 | } catch (NoSuchElementException e) { |
| 428 | } catch (IOException e) { |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 431 | return buildProps; |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 432 | } |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 433 | |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 434 | public static String getprop(String name, String defaultValue) |
| 435 | { |
| 436 | String result; |
| 437 | if (getpropAll().containsKey(name)){ |
| 438 | result = getpropAll().get(name); |
| 439 | } else { |
| 440 | result = defaultValue; |
| 441 | } |
| 442 | return result; |
| 443 | } |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 444 | |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 445 | public static void setBetaPropToEnable() |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 446 | { |
| 447 | if(RootTools.isAccessGiven()) { |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 448 | CommandCapture command = new CommandCapture(0, "/system/bin/setprop "+ BetaEnabler.FAIRPHONE_BETA_PROPERTY+" "+BetaEnabler.BETA_ENABLED); |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 449 | try { |
| 450 | Shell.runRootCommand(command); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 451 | } catch (IOException | TimeoutException | RootDeniedException e) { |
| 452 | Log.d(TAG, "Failed to setprop: " + e.getLocalizedMessage()); |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame^] | 455 | try { |
| 456 | Thread.sleep(200); |
| 457 | } catch (InterruptedException e) { |
| 458 | |
| 459 | } |
| 460 | buildProps = null; |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 461 | } |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 462 | |
Jose Pascoal | 60c9937 | 2015-03-17 15:52:59 +0000 | [diff] [blame] | 463 | public static String getOtaPackagePath(Resources resources, DownloadableItem item, boolean isVersion, boolean isZipInstall){ |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 464 | String path; |
| 465 | |
| 466 | if (Utils.hasUnifiedPartition(resources)) |
| 467 | { |
| 468 | path = resources.getString(R.string.recoveryCachePath) + Utils.getFilenameFromDownloadableItem(item, isVersion); |
| 469 | } |
| 470 | else |
| 471 | { |
Jose Pascoal | 60c9937 | 2015-03-17 15:52:59 +0000 | [diff] [blame] | 472 | if(isZipInstall && Build.MODEL.equalsIgnoreCase(resources.getString(R.string.FP1Model))) |
| 473 | { |
| 474 | //TODO: Find a way to not have this hardcoded |
| 475 | String zipPath = item.getDownloadLink(); |
| 476 | path = zipPath.replace("/storage/sdcard0", resources.getString(R.string.recoverySdCardPath)); |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | path = resources.getString(R.string.recoverySdCardPath) + resources.getString(R.string.updaterFolder) + Utils.getFilenameFromDownloadableItem(item, isVersion); |
| 481 | } |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | return path; |
| 485 | } |
| 486 | |
| 487 | public static void writeCacheCommand(Context context, String otaPackagePath) throws IOException, TimeoutException, RootDeniedException, Resources.NotFoundException { |
| 488 | if (PrivilegeChecker.isPrivilegedApp()) { |
| 489 | File recovery_dir = new File("/cache/recovery/"); |
| 490 | final boolean mkdirs = recovery_dir.mkdirs(); |
Filipe Gonçalves | 2ba0f55 | 2015-02-16 12:53:15 +0000 | [diff] [blame] | 491 | if(! (mkdirs || recovery_dir.exists()) ) { |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 492 | String errorMessage = context.getResources().getString(R.string.failed_mkdirs_cache_message); |
| 493 | Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show(); |
| 494 | throw new IOException(errorMessage); |
| 495 | } |
| 496 | |
| 497 | File command = new File("/cache/recovery/command"); |
| 498 | File extendedCommand = new File("/cache/recovery/extendedcommand"); |
Filipe Gonçalves | 2ba0f55 | 2015-02-16 12:53:15 +0000 | [diff] [blame] | 499 | final boolean deleteFailed = !extendedCommand.delete(); |
| 500 | if (deleteFailed) { |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 501 | Log.d(TAG, "Couldn't delete "+extendedCommand.getAbsolutePath()); |
| 502 | } |
| 503 | |
| 504 | String updateCommand = "--update_package=" + otaPackagePath; |
| 505 | PrintWriter writer = new PrintWriter(command, "UTF-8"); |
| 506 | writer.println("--wipe_cache"); |
| 507 | writer.println(updateCommand); |
| 508 | writer.flush(); |
| 509 | writer.close(); |
| 510 | }else { |
| 511 | if(RootTools.isAccessGiven()) { |
| 512 | Shell.runRootCommand(new CommandCapture(0, "rm -f /cache/recovery/command")); |
| 513 | Shell.runRootCommand(new CommandCapture(0, "rm -f /cache/recovery/extendedcommand")); |
| 514 | Shell.runRootCommand(new CommandCapture(0, "echo '--wipe_cache' >> /cache/recovery/command")); |
| 515 | Shell.runRootCommand(new CommandCapture(0, "echo '--update_package=" + otaPackagePath + "' >> /cache/recovery/command")); |
| 516 | }else{ |
| 517 | throw new RootDeniedException("Root Denied"); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
Filipe Gonçalves | 2ba0f55 | 2015-02-16 12:53:15 +0000 | [diff] [blame] | 522 | @SuppressWarnings("BooleanMethodIsAlwaysInverted") |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 523 | public static boolean rebootToRecovery(Context context) { |
| 524 | boolean result; |
| 525 | if (PrivilegeChecker.isPrivilegedApp()) { |
| 526 | ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).reboot("recovery"); |
| 527 | result = false; |
| 528 | } else { |
| 529 | if(RootTools.isAccessGiven()) { |
| 530 | try { |
| 531 | Shell.runRootCommand(new CommandCapture(0, "reboot recovery")); |
| 532 | result = true; |
| 533 | } catch (IOException | TimeoutException | RootDeniedException e) { |
| 534 | Log.e(TAG, "Error rebooting to recovery: " + e.getLocalizedMessage()); |
| 535 | result = false; |
| 536 | } |
| 537 | }else{ |
| 538 | result = false; |
| 539 | } |
| 540 | |
| 541 | } |
| 542 | return result; |
| 543 | } |
Jose Pascoal | 8775874 | 2015-01-28 20:00:22 +0000 | [diff] [blame] | 544 | |
Jose Pascoal | cdd8204 | 2015-02-06 13:04:26 +0000 | [diff] [blame] | 545 | // --Commented out by Inspection START (06/02/2015 12:25): |
| 546 | // public static void printStack(String moreLogs) |
| 547 | // { |
| 548 | // StringBuilder sb = new StringBuilder(moreLogs); |
| 549 | // sb.append("\nStack --> "); |
| 550 | // for (StackTraceElement ste : Thread.currentThread().getStackTrace()) |
| 551 | // { |
| 552 | // sb.append(ste.getFileName()).append(" : ").append(ste.getMethodName()).append(":").append(ste.getLineNumber()).append(" -- "); |
| 553 | // } |
| 554 | // Log.wtf(TAG, sb.toString()); |
| 555 | // } |
| 556 | // --Commented out by Inspection STOP (06/02/2015 12:25) |
Jose Pascoal | 288dd2f | 2015-02-20 19:19:04 +0000 | [diff] [blame] | 557 | |
| 558 | public static boolean fileExists(String otaPackagePath) { |
| 559 | boolean fileExists; |
| 560 | if(PrivilegeChecker.isPrivilegedApp()){ |
| 561 | File f = new File(otaPackagePath); |
| 562 | fileExists = f.exists(); |
| 563 | }else { |
| 564 | fileExists = RootTools.exists(otaPackagePath); |
| 565 | } |
| 566 | return fileExists; |
| 567 | } |
Tiago Costa | d7e951f | 2015-03-02 17:37:33 +0000 | [diff] [blame] | 568 | |
Tiago Costa | 3d30c7b | 2015-03-03 16:11:52 +0000 | [diff] [blame] | 569 | private final static String[] SHELL_COMMANDS_ERASE_DATA = { |
Tiago Costa | d7e951f | 2015-03-02 17:37:33 +0000 | [diff] [blame] | 570 | // remove data |
| 571 | "rm -rf /data/data/com.android.providers.media*", |
| 572 | "rm -rf /data/data/com.android.keychain*", |
| 573 | "rm -rf /data/data/com.android.location.fused*", |
| 574 | "rm -rf /data/data/com.android.providers.applications*", |
| 575 | "rm -rf /data/data/com.android.providers.media*", |
| 576 | "rm -rf /data/data/com.android.vending*", |
| 577 | "rm -rf /data/data/com.google.android.apps.genie.geniewidget*", |
| 578 | "rm -rf /data/data/com.google.android.apps.plus*", |
| 579 | "rm -rf /data/data/com.google.android.ears*", |
| 580 | "rm -rf /data/data/com.google.android.gms*", |
| 581 | "rm -rf /data/data/com.google.android.googlequicksearchbox*", |
| 582 | "rm -rf /data/data/com.google.android.location*", |
| 583 | "rm -rf /data/data/com.google.android.marvin.talkback*", |
| 584 | // remove cache |
Tiago Costa | 3d30c7b | 2015-03-03 16:11:52 +0000 | [diff] [blame] | 585 | // "rm -rf /data/dalvik-cache", |
Tiago Costa | d7e951f | 2015-03-02 17:37:33 +0000 | [diff] [blame] | 586 | // remove data/app |
| 587 | "rm -rf /data/app/com.android.apps.plus*", |
| 588 | "rm -rf /data/app/com.android.vending*", |
| 589 | "rm -rf /data/app/com.android.easr*", |
| 590 | "rm -rf /data/app/com.android.gms*", |
| 591 | "rm -rf /data/app/com.android.tts*" |
| 592 | }; |
| 593 | |
Tiago Costa | 3d30c7b | 2015-03-03 16:11:52 +0000 | [diff] [blame] | 594 | public final static String SHELL_COMMAND_ERASE_DALVIK_CACHE = "rm -rf /data/dalvik-cache"; |
Tiago Costa | d7e951f | 2015-03-02 17:37:33 +0000 | [diff] [blame] | 595 | public final static String SHELL_COMMAND_EXIT = "exit"; |
| 596 | |
| 597 | public static void clearGappsData() throws RootDeniedException, IOException, InterruptedException { |
| 598 | |
| 599 | if (PrivilegeChecker.isPrivilegedApp()) { |
Tiago Costa | 3d30c7b | 2015-03-03 16:11:52 +0000 | [diff] [blame] | 600 | Process p = Runtime.getRuntime().exec(SHELL_COMMAND_ERASE_DALVIK_CACHE); |
Tiago Costa | d7e951f | 2015-03-02 17:37:33 +0000 | [diff] [blame] | 601 | DataOutputStream os = new DataOutputStream(p.getOutputStream()); |
Tiago Costa | 3d30c7b | 2015-03-03 16:11:52 +0000 | [diff] [blame] | 602 | for (String tmpCmd : SHELL_COMMANDS_ERASE_DATA) { |
Tiago Costa | d7e951f | 2015-03-02 17:37:33 +0000 | [diff] [blame] | 603 | os.writeBytes(tmpCmd+"\n"); |
| 604 | } |
| 605 | os.writeBytes(SHELL_COMMAND_EXIT+"\n"); |
| 606 | os.flush(); |
| 607 | p.waitFor(); |
| 608 | }else { |
| 609 | if(RootTools.isAccessGiven()) { |
Tiago Costa | 3d30c7b | 2015-03-03 16:11:52 +0000 | [diff] [blame] | 610 | try { |
| 611 | Shell.runRootCommand(new CommandCapture(0, SHELL_COMMAND_ERASE_DALVIK_CACHE)); |
| 612 | } catch (TimeoutException e) { |
| 613 | e.printStackTrace(); |
| 614 | } |
| 615 | for (String tmpCmd : SHELL_COMMANDS_ERASE_DATA) { |
Tiago Costa | d7e951f | 2015-03-02 17:37:33 +0000 | [diff] [blame] | 616 | try { |
| 617 | Shell.runRootCommand(new CommandCapture(0, tmpCmd)); |
| 618 | } catch (TimeoutException e) { |
| 619 | e.printStackTrace(); |
| 620 | } |
| 621 | } |
| 622 | }else{ |
| 623 | throw new RootDeniedException("Root Denied"); |
| 624 | } |
| 625 | } |
| 626 | } |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 627 | |
Jose Pascoal | 4fe7ba5 | 2015-03-13 16:54:31 +0000 | [diff] [blame] | 628 | public static String getPath(final Context context, final Uri uri) |
| 629 | { |
| 630 | String filePath = uri.getPath(); |
| 631 | if ("content".equalsIgnoreCase(uri.getScheme())) { |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 632 | |
Jose Pascoal | 4fe7ba5 | 2015-03-13 16:54:31 +0000 | [diff] [blame] | 633 | //Get the zip file name |
| 634 | String[] path = filePath.split("/"); |
| 635 | String downloadIdStr = ""; |
| 636 | if (path != null && path.length > 0) { |
| 637 | downloadIdStr = path[path.length - 1]; |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 638 | } |
Jose Pascoal | 4fe7ba5 | 2015-03-13 16:54:31 +0000 | [diff] [blame] | 639 | long downloadId = 0; |
| 640 | try { |
| 641 | downloadId = Long.parseLong(downloadIdStr); |
| 642 | } catch (NumberFormatException nfe) { |
| 643 | Log.w(TAG, "NumberFormatException: " + nfe.getMessage()); |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 644 | } |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 645 | |
Jose Pascoal | 4fe7ba5 | 2015-03-13 16:54:31 +0000 | [diff] [blame] | 646 | DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |
| 647 | DownloadManager.Query query = new DownloadManager.Query(); |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 648 | |
Jose Pascoal | 4fe7ba5 | 2015-03-13 16:54:31 +0000 | [diff] [blame] | 649 | query.setFilterById(downloadId); |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 650 | |
Jose Pascoal | 4fe7ba5 | 2015-03-13 16:54:31 +0000 | [diff] [blame] | 651 | Cursor cursor = downloadManager != null ? downloadManager.query(query) : null; |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 652 | |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 653 | if (cursor != null && cursor.moveToFirst()) { |
Jose Pascoal | 4fe7ba5 | 2015-03-13 16:54:31 +0000 | [diff] [blame] | 654 | int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); |
| 655 | int status = cursor.getInt(columnIndex); |
| 656 | |
| 657 | switch (status) { |
| 658 | case DownloadManager.STATUS_SUCCESSFUL: { |
| 659 | filePath = downloadManager.getUriForDownloadedFile(downloadId).getPath(); |
| 660 | break; |
| 661 | } |
| 662 | case DownloadManager.STATUS_FAILED: |
| 663 | case DownloadManager.STATUS_PAUSED: |
| 664 | case DownloadManager.STATUS_PENDING: |
| 665 | case DownloadManager.STATUS_RUNNING: |
| 666 | default: |
| 667 | filePath = ""; |
| 668 | break; |
| 669 | } |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 670 | } |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 671 | } |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 672 | |
Jose Pascoal | 4fe7ba5 | 2015-03-13 16:54:31 +0000 | [diff] [blame] | 673 | return filePath; |
Jose Pascoal | 2a8283b | 2015-03-13 14:43:42 +0000 | [diff] [blame] | 674 | } |
Filipe Gonçalves | 7281778 | 2015-03-19 15:29:05 +0000 | [diff] [blame] | 675 | |
| 676 | public static boolean isWiFiEnabled(Context context) |
| 677 | { |
| 678 | |
| 679 | ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 680 | |
| 681 | return manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting(); |
| 682 | } |
Jose Pascoal | 055562d | 2015-03-19 19:31:38 +0000 | [diff] [blame] | 683 | |
| 684 | public static Store getGappsStore() |
| 685 | { |
| 686 | return UpdaterData.getInstance().getStore(GAPPS_STORE_NUMBER); |
| 687 | } |
Jose Pascoal | 02b849e | 2014-06-26 17:07:51 +0100 | [diff] [blame] | 688 | } |