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