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