Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [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; |
| 18 | |
| 19 | import android.app.DownloadManager; |
| 20 | import android.app.DownloadManager.Request; |
| 21 | import android.app.Notification; |
| 22 | import android.app.NotificationManager; |
| 23 | import android.app.PendingIntent; |
| 24 | import android.app.Service; |
| 25 | import android.app.TaskStackBuilder; |
| 26 | import android.content.BroadcastReceiver; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.content.IntentFilter; |
| 30 | import android.content.SharedPreferences; |
| 31 | import android.content.SharedPreferences.Editor; |
| 32 | import android.content.res.Resources; |
| 33 | import android.database.Cursor; |
| 34 | import android.graphics.BitmapFactory; |
| 35 | import android.net.ConnectivityManager; |
| 36 | import android.net.Uri; |
Jose Pascoal | cf13fde | 2014-05-21 20:17:17 +0100 | [diff] [blame] | 37 | import android.os.Build; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 38 | import android.os.Environment; |
Filipe Gonçalves | b2536c0 | 2015-01-12 11:50:25 +0000 | [diff] [blame] | 39 | import android.os.Handler; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 40 | import android.os.IBinder; |
Filipe Gonçalves | b2536c0 | 2015-01-12 11:50:25 +0000 | [diff] [blame] | 41 | import android.os.SystemClock; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 42 | import android.support.v4.app.NotificationCompat; |
Tiago Costa | c14ea24 | 2014-04-24 10:14:41 +0100 | [diff] [blame] | 43 | import android.util.Log; |
Jose Pascoal | 0e96628 | 2014-08-12 18:49:05 +0100 | [diff] [blame] | 44 | import android.widget.Toast; |
Filipe Gonçalves | cd0f33d | 2014-12-15 15:05:15 +0000 | [diff] [blame] | 45 | |
Filipe Gonçalves | a75e181 | 2015-03-19 12:12:07 +0000 | [diff] [blame] | 46 | import com.fairphone.updater.data.UpdaterData; |
Jose Pascoal | 7bf83a0 | 2014-10-13 18:30:18 +0100 | [diff] [blame] | 47 | import com.fairphone.updater.data.Version; |
| 48 | import com.fairphone.updater.data.VersionParserHelper; |
Jose Pascoal | ac6a846 | 2014-09-10 20:12:08 +0100 | [diff] [blame] | 49 | import com.fairphone.updater.gappsinstaller.GappsInstallerHelper; |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 50 | import com.fairphone.updater.tools.PrivilegeChecker; |
Jose Pascoal | 02b849e | 2014-06-26 17:07:51 +0100 | [diff] [blame] | 51 | import com.fairphone.updater.tools.RSAUtils; |
| 52 | import com.fairphone.updater.tools.Utils; |
Jose Pascoal | cf13fde | 2014-05-21 20:17:17 +0100 | [diff] [blame] | 53 | import com.stericson.RootTools.execution.CommandCapture; |
| 54 | import com.stericson.RootTools.execution.Shell; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 55 | |
Filipe Gonçalves | 4cc0b2e | 2015-05-27 18:12:40 +0100 | [diff] [blame] | 56 | import java.io.File; |
| 57 | import java.io.FileInputStream; |
| 58 | import java.io.FileOutputStream; |
| 59 | import java.io.IOException; |
| 60 | import java.io.UnsupportedEncodingException; |
| 61 | import java.net.URLEncoder; |
| 62 | import java.nio.channels.FileChannel; |
| 63 | import java.nio.charset.Charset; |
| 64 | import java.util.concurrent.TimeoutException; |
| 65 | |
Filipe Gonçalves | cd0f33d | 2014-12-15 15:05:15 +0000 | [diff] [blame] | 66 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 67 | public class UpdaterService extends Service |
| 68 | { |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 69 | |
Filipe Gonçalves | 6ce7b14 | 2015-01-22 14:39:29 +0000 | [diff] [blame] | 70 | public static final String LAST_CONFIG_DOWNLOAD_IN_MS = "LAST_CONFIG_DOWNLOAD_IN_MS"; |
Filipe Gonçalves | 8b263ad | 2015-01-12 12:30:11 +0000 | [diff] [blame] | 71 | private static final int CONFIG_FILE_DOWNLOAD_TIMEOUT_MILLIS = 23500; |
Filipe Gonçalves | 8f226b0 | 2014-12-12 11:21:58 +0000 | [diff] [blame] | 72 | public static final String EXTRA_FORCE_CONFIG_FILE_DOWNLOAD = "FORCE_DOWNLOAD"; |
| 73 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 74 | private static final String TAG = UpdaterService.class.getSimpleName(); |
| 75 | |
Jose Pascoal | c2545cc | 2014-12-18 16:51:52 +0000 | [diff] [blame] | 76 | public static final String PREFERENCE_LAST_CONFIG_DOWNLOAD_ID = "LastConfigDownloadId"; |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 77 | public static final String PREFERENCE_REINSTALL_GAPPS = "ReinstallGappsOmnStartUp"; |
Filipe Gonçalves | 80d4b15 | 2015-05-26 19:04:02 +0100 | [diff] [blame] | 78 | public static final String PREFERENCE_CONFIG_MD_5 = "CONFIG_MD5"; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 79 | private DownloadManager mDownloadManager = null; |
| 80 | private DownloadBroadCastReceiver mDownloadBroadCastReceiver = null; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 81 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 82 | private static final int MAX_DOWNLOAD_RETRIES = 3; |
| 83 | private int mDownloadRetries; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 84 | private long mLatestFileDownloadId; |
Filipe Gonçalves | d28bd62 | 2014-11-05 11:40:12 +0000 | [diff] [blame] | 85 | private boolean mInternetConnectionAvailable; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 86 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 87 | private SharedPreferences mSharedPreferences; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 88 | |
Jose Pascoal | b03b55c | 2015-02-19 17:34:07 +0000 | [diff] [blame] | 89 | private final static long DOWNLOAD_GRACE_PERIOD_IN_MS = 24 /* hour */ * Utils.MINUTES_IN_HOUR /* minute */ * Utils.SECONDS_IN_MINUTE /* second */ * 1000 /* millisecond */; |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 90 | private BroadcastReceiver networkStateReceiver; |
Jose Pascoal | 29ee101 | 2014-09-12 17:25:36 +0100 | [diff] [blame] | 91 | |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 92 | @Override |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 93 | public int onStartCommand(Intent intent, int flags, int startId) |
| 94 | { |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 95 | // remove the logs |
| 96 | clearDataLogs(); |
Jose Pascoal | da00b38 | 2015-01-20 18:39:28 +0000 | [diff] [blame] | 97 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 98 | if(Utils.isDeviceUnsupported(getApplicationContext())){ |
Jose Pascoal | da00b38 | 2015-01-20 18:39:28 +0000 | [diff] [blame] | 99 | stopSelf(); |
| 100 | return START_NOT_STICKY; |
| 101 | } |
Jose Pascoal | 29ee101 | 2014-09-12 17:25:36 +0100 | [diff] [blame] | 102 | |
Jose Pascoal | 7bf83a0 | 2014-10-13 18:30:18 +0100 | [diff] [blame] | 103 | mSharedPreferences = getApplicationContext().getSharedPreferences(FairphoneUpdater.FAIRPHONE_UPDATER_PREFERENCES, MODE_PRIVATE); |
Jose Pascoal | 29ee101 | 2014-09-12 17:25:36 +0100 | [diff] [blame] | 104 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 105 | mLatestFileDownloadId = mSharedPreferences.getLong(PREFERENCE_LAST_CONFIG_DOWNLOAD_ID, 0); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 106 | |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 107 | setupDownloadManager(); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 108 | |
Filipe Gonçalves | d28bd62 | 2014-11-05 11:40:12 +0000 | [diff] [blame] | 109 | setupConnectivityMonitoring(); |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 110 | |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 111 | if (Utils.isWiFiEnabled(getApplicationContext())) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 112 | { |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 113 | downloadConfigFile(intent != null && intent.getBooleanExtra(EXTRA_FORCE_CONFIG_FILE_DOWNLOAD, false)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // setup the gapps installer |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 117 | GappsInstallerHelper.checkGappsAreInstalled(getApplicationContext()); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 118 | |
Filipe Gonçalves | a75e181 | 2015-03-19 12:12:07 +0000 | [diff] [blame] | 119 | runInstallationDisclaimer(getApplicationContext()); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 120 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 121 | return Service.START_STICKY; |
| 122 | } |
| 123 | |
Filipe Gonçalves | a75e181 | 2015-03-19 12:12:07 +0000 | [diff] [blame] | 124 | private static void runInstallationDisclaimer(Context context) |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 125 | { |
Filipe Gonçalves | a75e181 | 2015-03-19 12:12:07 +0000 | [diff] [blame] | 126 | SharedPreferences sharedPreferences = context.getApplicationContext().getSharedPreferences(FairphoneUpdater.FAIRPHONE_UPDATER_PREFERENCES, MODE_PRIVATE); |
| 127 | if (sharedPreferences.getBoolean(PREFERENCE_REINSTALL_GAPPS, true) && !UpdaterData.getInstance().isAppStoreListEmpty()) |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 128 | { |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 129 | if(!GappsInstallerHelper.areGappsInstalled()){ |
Filipe Gonçalves | a75e181 | 2015-03-19 12:12:07 +0000 | [diff] [blame] | 130 | showReinstallAlert(context); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Filipe Gonçalves | a75e181 | 2015-03-19 12:12:07 +0000 | [diff] [blame] | 133 | Editor editor = sharedPreferences.edit(); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 134 | editor.putBoolean(PREFERENCE_REINSTALL_GAPPS, false); |
| 135 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 136 | editor.apply(); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Filipe Gonçalves | a75e181 | 2015-03-19 12:12:07 +0000 | [diff] [blame] | 140 | private static void showReinstallAlert(Context context) |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 141 | { |
Filipe Gonçalves | 825414a | 2015-01-26 14:52:48 +0000 | [diff] [blame] | 142 | if ( FairphoneUpdater.BETA_MODE_ENABLED ) |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 143 | { |
Filipe Gonçalves | 825414a | 2015-01-26 14:52:48 +0000 | [diff] [blame] | 144 | return; |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 145 | } |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 146 | |
| 147 | NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 148 | |
Tiago Costa | 73575aa | 2015-01-19 15:48:26 +0000 | [diff] [blame] | 149 | //Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(context.getResources().getString(R.string.supportAppStoreUrl))); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 150 | |
Tiago Costa | 73575aa | 2015-01-19 15:48:26 +0000 | [diff] [blame] | 151 | Intent notificationIntent = new Intent(context, FairphoneUpdater.class); |
| 152 | notificationIntent.setAction(GappsInstallerHelper.EXTRA_START_GAPPS_INSTALL); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 153 | |
Tiago Costa | 73575aa | 2015-01-19 15:48:26 +0000 | [diff] [blame] | 154 | PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 155 | |
| 156 | NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.updater_tray_icon) |
Filipe Gonçalves | 071b675 | 2015-06-24 12:37:32 +0100 | [diff] [blame^] | 157 | .setContentTitle(context.getResources().getString(R.string.app_full_name)) |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 158 | .setContentText(context.getResources().getString(R.string.appStoreReinstall)) |
| 159 | .setAutoCancel(true) |
| 160 | .setDefaults(Notification.DEFAULT_SOUND) |
| 161 | .setContentIntent(contentIntent); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 162 | |
| 163 | mNotificationManager.notify(0, mBuilder.build()); |
| 164 | } |
| 165 | |
Filipe Gonçalves | 8f226b0 | 2014-12-12 11:21:58 +0000 | [diff] [blame] | 166 | private void downloadConfigFile(boolean forceDownload) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 167 | { |
Filipe Gonçalves | 8f226b0 | 2014-12-12 11:21:58 +0000 | [diff] [blame] | 168 | long now = System.currentTimeMillis(); |
Filipe Gonçalves | 712d548 | 2015-01-13 17:00:44 +0000 | [diff] [blame] | 169 | long last_download = mSharedPreferences.getLong(LAST_CONFIG_DOWNLOAD_IN_MS, 0L); |
Filipe Gonçalves | 8f226b0 | 2014-12-12 11:21:58 +0000 | [diff] [blame] | 170 | if( forceDownload || now > (last_download + DOWNLOAD_GRACE_PERIOD_IN_MS) ) { |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 171 | Log.i(TAG, "Downloading updater configuration file."); |
Filipe Gonçalves | 8f226b0 | 2014-12-12 11:21:58 +0000 | [diff] [blame] | 172 | // remove the old file if its still there for some reason |
| 173 | removeLatestFileDownload(getApplicationContext()); |
| 174 | |
| 175 | // start the download of the latest file |
| 176 | startDownloadLatest(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 177 | |
Jose Pascoal | 0b48f8d | 2015-02-06 16:06:41 +0000 | [diff] [blame] | 178 | mSharedPreferences.edit().putLong(LAST_CONFIG_DOWNLOAD_IN_MS, now).apply(); |
Filipe Gonçalves | 8f226b0 | 2014-12-12 11:21:58 +0000 | [diff] [blame] | 179 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Jose Pascoal | cdd8204 | 2015-02-06 13:04:26 +0000 | [diff] [blame] | 182 | // --Commented out by Inspection START (06/02/2015 12:27): |
| 183 | // public void updateGoogleAppsIntallerWidgets() |
| 184 | // { |
| 185 | // AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); |
| 186 | // int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, GoogleAppsInstallerWidget.class)); |
| 187 | // if (appWidgetIds.length > 0) |
| 188 | // { |
| 189 | // new GoogleAppsInstallerWidget().onUpdate(this, appWidgetManager, appWidgetIds); |
| 190 | // } |
| 191 | // } |
| 192 | // --Commented out by Inspection STOP (06/02/2015 12:27) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 193 | |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 194 | private static void clearDataLogs() |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 195 | { |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 196 | if (PrivilegeChecker.isPrivilegedApp()) { |
| 197 | clearDataLogsPrivileged(); |
| 198 | } else { |
| 199 | clearDataLogsUnprivileged(); |
Tiago Costa | c14ea24 | 2014-04-24 10:14:41 +0100 | [diff] [blame] | 200 | } |
| 201 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 202 | |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 203 | private static void clearDataLogsPrivileged() |
| 204 | { |
| 205 | try |
| 206 | { |
| 207 | Log.d(TAG, "Clearing dump log data..."); |
| 208 | Process p = Runtime.getRuntime().exec("rm /data/log_other_mode/*_log"); |
| 209 | try |
| 210 | { |
| 211 | p.waitFor(); |
| 212 | } catch (InterruptedException e) |
| 213 | { |
| 214 | e.printStackTrace(); |
| 215 | } |
| 216 | } catch (IOException e) |
| 217 | { |
| 218 | Log.d(TAG, "Clearing dump log data failed: " + e.getLocalizedMessage()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private static void clearDataLogsUnprivileged() |
| 223 | { |
| 224 | try |
| 225 | { |
| 226 | Log.d(TAG, "Clearing dump log data..."); |
| 227 | Shell.runCommand(new CommandCapture(0, "rm /data/log_other_mode/*_log")); |
| 228 | } catch (IOException | TimeoutException e) |
| 229 | { |
| 230 | Log.d(TAG, "Clearing dump log data failed: " + e.getLocalizedMessage()); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 235 | @Override |
| 236 | public IBinder onBind(Intent intent) |
| 237 | { |
| 238 | return null; |
| 239 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 240 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 241 | void startDownloadLatest() |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 242 | { |
Filipe Gonçalves | 5b65c01 | 2015-01-08 13:49:42 +0000 | [diff] [blame] | 243 | Resources resources = getApplicationContext().getResources(); |
| 244 | String downloadLink = getConfigDownloadLink(getApplicationContext()); |
| 245 | // set the download for the latest version on the download manager |
| 246 | Request request = createDownloadRequest(downloadLink, resources.getString(R.string.configFilename) + resources.getString(R.string.config_zip)); |
Jose Pascoal | fd3e43c | 2014-05-22 20:29:17 +0100 | [diff] [blame] | 247 | |
Filipe Gonçalves | 5b65c01 | 2015-01-08 13:49:42 +0000 | [diff] [blame] | 248 | if (request != null && mDownloadManager != null) |
| 249 | { |
Jose Pascoal | 8775874 | 2015-01-28 20:00:22 +0000 | [diff] [blame] | 250 | //Guarantee that only we have only one download |
| 251 | long oldDownloadId = mSharedPreferences.getLong(PREFERENCE_LAST_CONFIG_DOWNLOAD_ID, 0); |
| 252 | if(oldDownloadId != 0){ |
| 253 | mDownloadManager.remove(oldDownloadId); |
| 254 | saveLatestDownloadId(0); |
| 255 | } |
| 256 | |
Filipe Gonçalves | 5b65c01 | 2015-01-08 13:49:42 +0000 | [diff] [blame] | 257 | mLatestFileDownloadId = mDownloadManager.enqueue(request); |
| 258 | saveLatestDownloadId(mLatestFileDownloadId); |
Filipe Gonçalves | b2536c0 | 2015-01-12 11:50:25 +0000 | [diff] [blame] | 259 | |
| 260 | final long currentId = mLatestFileDownloadId; |
| 261 | // Cancel download if it is stuck since DownloadManager doesn't seem able to do it. |
| 262 | new Handler().postAtTime(new Runnable() { |
| 263 | @Override |
| 264 | public void run() { |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 265 | onDownloadStatus(currentId, new Runnable() { |
Filipe Gonçalves | 712d548 | 2015-01-13 17:00:44 +0000 | [diff] [blame] | 266 | @Override |
| 267 | public void run() { |
Filipe Gonçalves | b2536c0 | 2015-01-12 11:50:25 +0000 | [diff] [blame] | 268 | Log.w(TAG, "Configuration file download timed out"); |
| 269 | mDownloadManager.remove(currentId); |
Jose Pascoal | 0b48f8d | 2015-02-06 16:06:41 +0000 | [diff] [blame] | 270 | mSharedPreferences.edit().remove(LAST_CONFIG_DOWNLOAD_IN_MS).apply(); |
Filipe Gonçalves | b2536c0 | 2015-01-12 11:50:25 +0000 | [diff] [blame] | 271 | } |
Filipe Gonçalves | 712d548 | 2015-01-13 17:00:44 +0000 | [diff] [blame] | 272 | }); |
Filipe Gonçalves | b2536c0 | 2015-01-12 11:50:25 +0000 | [diff] [blame] | 273 | } |
Filipe Gonçalves | 8b263ad | 2015-01-12 12:30:11 +0000 | [diff] [blame] | 274 | }, SystemClock.uptimeMillis() + CONFIG_FILE_DOWNLOAD_TIMEOUT_MILLIS); |
Filipe Gonçalves | 5b65c01 | 2015-01-08 13:49:42 +0000 | [diff] [blame] | 275 | } |
| 276 | else |
| 277 | { |
| 278 | Log.e(TAG, "Invalid request for link " + downloadLink); |
| 279 | Intent i = new Intent(FairphoneUpdater.FAIRPHONE_UPDATER_CONFIG_DOWNLOAD_FAILED); |
| 280 | i.putExtra(FairphoneUpdater.FAIRPHONE_UPDATER_CONFIG_DOWNLOAD_LINK, downloadLink); |
| 281 | sendBroadcast(i); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Jose Pascoal | c2545cc | 2014-12-18 16:51:52 +0000 | [diff] [blame] | 285 | private void saveLatestDownloadId(long id) |
| 286 | { |
| 287 | mLatestFileDownloadId = id; |
| 288 | Editor editor = mSharedPreferences.edit(); |
| 289 | editor.putLong(PREFERENCE_LAST_CONFIG_DOWNLOAD_ID, id); |
| 290 | editor.commit(); |
| 291 | } |
| 292 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 293 | private String getConfigDownloadLink(Context context) |
| 294 | { |
| 295 | |
| 296 | Resources resources = context.getResources(); |
| 297 | |
| 298 | StringBuilder sb = new StringBuilder(); |
Filipe Gonçalves | afeac2c | 2015-01-27 17:49:53 +0000 | [diff] [blame] | 299 | String download_url = mSharedPreferences.getString(FairphoneUpdater.PREFERENCE_OTA_DOWNLOAD_URL, getResources().getString(R.string.downloadUrl)); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 300 | |
| 301 | sb.append(download_url); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 302 | sb.append(Build.MODEL.replaceAll("\\s", "")); |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 303 | sb.append(Utils.getPartitionDownloadPath(resources)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 304 | sb.append("/"); |
| 305 | |
| 306 | sb.append(resources.getString(R.string.configFilename)); |
| 307 | |
| 308 | sb.append(resources.getString(R.string.config_zip)); |
| 309 | |
| 310 | addModelAndOS(context, sb); |
| 311 | |
| 312 | String downloadLink = sb.toString(); |
| 313 | |
| 314 | Log.d(TAG, "Download link: " + downloadLink); |
| 315 | |
| 316 | return downloadLink; |
| 317 | } |
Jose Pascoal | cf13fde | 2014-05-21 20:17:17 +0100 | [diff] [blame] | 318 | |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 319 | private static void addModelAndOS(Context context, StringBuilder sb) |
Tiago Costa | b24ef1c | 2014-07-25 12:02:34 +0100 | [diff] [blame] | 320 | { |
| 321 | // attach the model and the os |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 322 | sb.append("?"); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 323 | sb.append("model=").append(Build.MODEL.replaceAll("\\s", "")); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 324 | Version currentVersion = VersionParserHelper.getDeviceVersion(context.getApplicationContext()); |
| 325 | |
| 326 | if (currentVersion != null) |
| 327 | { |
Filipe Gonçalves | cd0f33d | 2014-12-15 15:05:15 +0000 | [diff] [blame] | 328 | try { |
| 329 | final String defaultCharset = Charset.defaultCharset().displayName(); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 330 | sb.append("&os=").append(URLEncoder.encode(currentVersion.getAndroidVersion(), defaultCharset)); |
| 331 | sb.append("&b_n=").append(URLEncoder.encode(currentVersion.getBuildNumber(), defaultCharset)); |
| 332 | sb.append("&ota_v_n=").append(URLEncoder.encode(String.valueOf(currentVersion.getNumber()), defaultCharset)); |
| 333 | sb.append("&d=").append(URLEncoder.encode(currentVersion.getReleaseDate(), defaultCharset)); |
| 334 | sb.append("&beta=").append(URLEncoder.encode(currentVersion.getBetaStatus(), defaultCharset)); |
| 335 | sb.append("&dev=").append(FairphoneUpdater.DEV_MODE_ENABLED ? "1" : "0"); |
Filipe Gonçalves | cd0f33d | 2014-12-15 15:05:15 +0000 | [diff] [blame] | 336 | } catch (UnsupportedEncodingException e) { |
| 337 | Log.e(TAG, "Failed to add extra info on update request: "+e.getLocalizedMessage()); |
| 338 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 339 | } |
Tiago Costa | b24ef1c | 2014-07-25 12:02:34 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 342 | private static void setNotification(Context currentContext) |
| 343 | { |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 344 | |
Filipe Gonçalves | 825414a | 2015-01-26 14:52:48 +0000 | [diff] [blame] | 345 | if ( FairphoneUpdater.BETA_MODE_ENABLED ) |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 346 | { |
Filipe Gonçalves | 825414a | 2015-01-26 14:52:48 +0000 | [diff] [blame] | 347 | return; |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 348 | } |
Filipe Gonçalves | 825414a | 2015-01-26 14:52:48 +0000 | [diff] [blame] | 349 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 350 | Context context = currentContext.getApplicationContext(); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 351 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 352 | NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 353 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 354 | NotificationCompat.Builder builder = |
Pedro Arelo | 259478b | 2014-10-10 12:10:37 +0100 | [diff] [blame] | 355 | new NotificationCompat.Builder(context).setSmallIcon(R.drawable.updater_tray_icon_small) |
| 356 | .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.updater_tray_icon)) |
Filipe Gonçalves | 071b675 | 2015-06-24 12:37:32 +0100 | [diff] [blame^] | 357 | .setContentTitle(context.getResources().getString(R.string.app_full_name)) |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 358 | .setContentText(context.getResources().getString(R.string.fairphone_update_message)); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 359 | |
Jose Pascoal | 7bf83a0 | 2014-10-13 18:30:18 +0100 | [diff] [blame] | 360 | Intent resultIntent = new Intent(context, FairphoneUpdater.class); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 361 | TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 362 | |
Jose Pascoal | 7bf83a0 | 2014-10-13 18:30:18 +0100 | [diff] [blame] | 363 | stackBuilder.addParentStack(FairphoneUpdater.class); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 364 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 365 | stackBuilder.addNextIntent(resultIntent); |
| 366 | PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 367 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 368 | builder.setContentIntent(resultPendingIntent); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 369 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 370 | Notification notificationWhileRunnig = builder.build(); |
Jose Pascoal | fd3e43c | 2014-05-22 20:29:17 +0100 | [diff] [blame] | 371 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 372 | // Add notification |
| 373 | manager.notify(0, notificationWhileRunnig); |
| 374 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 375 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 376 | private Request createDownloadRequest(String url, String fileName) |
| 377 | { |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 378 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 379 | Resources resources = getApplicationContext().getResources(); |
| 380 | Request request; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 381 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 382 | try |
| 383 | { |
| 384 | request = new Request(Uri.parse(url)); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 385 | final File externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder)); |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 386 | final boolean notMkDirs = !externalStoragePublicDirectory.mkdirs(); |
| 387 | if(notMkDirs && !externalStoragePublicDirectory.exists()) { |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 388 | throw new Exception("Couldn't create updater dir structures."); |
| 389 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 390 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 391 | request.setDestinationInExternalPublicDir(resources.getString(R.string.updaterFolder), fileName); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 392 | request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); |
| 393 | request.setAllowedOverRoaming(false); |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 394 | request.setTitle(resources.getString(R.string.fairphone_update_message_title)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 395 | } catch (Exception e) |
| 396 | { |
| 397 | Log.e(TAG, "Error creating request: " + e.getMessage()); |
| 398 | request = null; |
| 399 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 400 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 401 | return request; |
| 402 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 403 | |
Filipe Gonçalves | d28bd62 | 2014-11-05 11:40:12 +0000 | [diff] [blame] | 404 | private void setupConnectivityMonitoring() |
| 405 | { |
| 406 | |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 407 | if (networkStateReceiver == null) { |
| 408 | // Check current connectivity status |
| 409 | mInternetConnectionAvailable = Utils.isWiFiEnabled(getApplicationContext()); |
Filipe Gonçalves | d28bd62 | 2014-11-05 11:40:12 +0000 | [diff] [blame] | 410 | |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 411 | // Setup monitoring for future connectivity status changes |
| 412 | networkStateReceiver = new BroadcastReceiver() |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 413 | { |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 414 | @Override |
| 415 | public void onReceive(Context context, Intent intent) |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 416 | { |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 417 | if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) { |
| 418 | Log.i(TAG, "Lost network connectivity."); |
| 419 | mInternetConnectionAvailable = false; |
| 420 | if (mLatestFileDownloadId != 0 && mDownloadManager != null) |
| 421 | { |
| 422 | onDownloadStatus(mLatestFileDownloadId, new Runnable() { |
| 423 | @Override |
| 424 | public void run() { |
| 425 | Log.d(TAG, "Removing pending download."); |
| 426 | mDownloadManager.remove(mLatestFileDownloadId); |
| 427 | saveLatestDownloadId(0); |
| 428 | } |
| 429 | }); |
| 430 | } |
| 431 | } |
| 432 | else |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 433 | { |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 434 | int conn_type = intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_DUMMY); |
| 435 | if( conn_type == ConnectivityManager.TYPE_WIFI ) { |
| 436 | Log.i(TAG, "Network connectivity potentially available."); |
| 437 | if (!mInternetConnectionAvailable) { |
| 438 | downloadConfigFile(false); |
Filipe Gonçalves | 712d548 | 2015-01-13 17:00:44 +0000 | [diff] [blame] | 439 | } |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 440 | mInternetConnectionAvailable = true; |
| 441 | } |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 444 | }; |
Filipe Gonçalves | d28bd62 | 2014-11-05 11:40:12 +0000 | [diff] [blame] | 445 | |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 446 | IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); |
| 447 | registerReceiver(networkStateReceiver, filter); |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 448 | |
Filipe Gonçalves | 4089313 | 2015-05-25 17:45:44 +0100 | [diff] [blame] | 449 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | private void setupDownloadManager() |
| 453 | { |
| 454 | if (mDownloadManager == null) |
| 455 | { |
| 456 | mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); |
| 457 | } |
| 458 | |
Filipe Gonçalves | d0dfe91 | 2014-11-04 16:24:25 +0000 | [diff] [blame] | 459 | if (mDownloadBroadCastReceiver == null) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 460 | { |
| 461 | mDownloadBroadCastReceiver = new DownloadBroadCastReceiver(); |
| 462 | |
| 463 | getApplicationContext().registerReceiver(mDownloadBroadCastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); |
| 464 | } |
| 465 | } |
| 466 | |
Filipe Gonçalves | 4cc0b2e | 2015-05-27 18:12:40 +0100 | [diff] [blame] | 467 | private static void copyConfigToData(Context context) throws IOException { |
| 468 | Resources resources = context.getApplicationContext().getResources(); |
| 469 | String targetPath = Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder); |
| 470 | FileInputStream inStream = new FileInputStream(targetPath + resources.getString(R.string.configFilename) + resources.getString(R.string.config_xml)); |
| 471 | FileOutputStream outStream = context.openFileOutput(resources.getString(R.string.configFilename) + resources.getString(R.string.config_xml), MODE_PRIVATE); |
| 472 | FileChannel inChannel = inStream.getChannel(); |
| 473 | FileChannel outChannel = outStream.getChannel(); |
| 474 | inChannel.transferTo(0, inChannel.size(), outChannel); |
| 475 | inStream.close(); |
| 476 | outStream.close(); |
| 477 | } |
| 478 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 479 | private static void checkVersionValidation(Context context) |
| 480 | { |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 481 | Version latestVersion = VersionParserHelper.getLatestVersion(context.getApplicationContext()); |
| 482 | Version currentVersion = VersionParserHelper.getDeviceVersion(context.getApplicationContext()); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 483 | if (latestVersion != null) |
| 484 | { |
| 485 | if (latestVersion.isNewerVersionThan(currentVersion)) |
| 486 | { |
| 487 | setNotification(context); |
| 488 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 489 | } |
Filipe Gonçalves | a75e181 | 2015-03-19 12:12:07 +0000 | [diff] [blame] | 490 | runInstallationDisclaimer(context); |
Jose Pascoal | d5e4d4a | 2015-03-13 18:17:48 +0000 | [diff] [blame] | 491 | |
| 492 | // to update the activity |
| 493 | Intent updateIntent = new Intent(FairphoneUpdater.FAIRPHONE_UPDATER_NEW_VERSION_RECEIVED); |
| 494 | context.sendBroadcast(updateIntent); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Filipe Gonçalves | 4cc0b2e | 2015-05-27 18:12:40 +0100 | [diff] [blame] | 497 | public static boolean readUpdaterData(Context context){ |
| 498 | Version latestVersion = VersionParserHelper.getLatestVersion(context.getApplicationContext()); |
| 499 | return latestVersion != null; |
| 500 | } |
| 501 | |
| 502 | public static boolean updateUpdaterData(Context context) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 503 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 504 | |
| 505 | boolean retVal = false; |
| 506 | Resources resources = context.getApplicationContext().getResources(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 507 | String targetPath = Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder); |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 508 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 509 | String filePath = targetPath + resources.getString(R.string.configFilename) + resources.getString(R.string.config_zip); |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 510 | |
| 511 | File file = new File(filePath); |
| 512 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 513 | if (file.exists()) |
| 514 | { |
Filipe Gonçalves | 80d4b15 | 2015-05-26 19:04:02 +0100 | [diff] [blame] | 515 | String md5sum = Utils.calculateMD5(file); |
Filipe Gonçalves | 4cc0b2e | 2015-05-27 18:12:40 +0100 | [diff] [blame] | 516 | SharedPreferences sp = context.getApplicationContext().getSharedPreferences(FairphoneUpdater.FAIRPHONE_UPDATER_PREFERENCES, MODE_PRIVATE); |
Filipe Gonçalves | 80d4b15 | 2015-05-26 19:04:02 +0100 | [diff] [blame] | 517 | if(sp.getString(PREFERENCE_CONFIG_MD_5, "").equals(md5sum)){ |
| 518 | retVal = true; |
Filipe Gonçalves | 80d4b15 | 2015-05-26 19:04:02 +0100 | [diff] [blame] | 519 | } else { |
Filipe Gonçalves | 4cc0b2e | 2015-05-27 18:12:40 +0100 | [diff] [blame] | 520 | if (RSAUtils.checkFileSignature(context, filePath, targetPath)) { |
| 521 | try { |
| 522 | copyConfigToData(context); |
| 523 | checkVersionValidation(context); |
| 524 | retVal = true; |
| 525 | sp.edit().putString(PREFERENCE_CONFIG_MD_5, md5sum).apply(); |
| 526 | } catch (IOException e) { |
| 527 | Log.e(TAG, "Failed to store configuration " + e.getLocalizedMessage()); |
| 528 | retVal = false; |
| 529 | } |
| 530 | } else { |
| 531 | //Toast.makeText(context, resources.getString(R.string.invalid_signature_download_message), Toast.LENGTH_LONG).show(); |
| 532 | final boolean notDeleted = !file.delete(); |
| 533 | if(notDeleted) { |
| 534 | Log.d(TAG, "Unable to delete "+file.getAbsolutePath()); |
| 535 | } |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 536 | |
Filipe Gonçalves | 4cc0b2e | 2015-05-27 18:12:40 +0100 | [diff] [blame] | 537 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 538 | } |
Filipe Gonçalves | 4cc0b2e | 2015-05-27 18:12:40 +0100 | [diff] [blame] | 539 | } else { |
| 540 | Log.wtf(TAG, "No file"); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 541 | } |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 542 | |
| 543 | return retVal; |
| 544 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 545 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 546 | private void removeLatestFileDownload(Context context) |
| 547 | { |
| 548 | if (mLatestFileDownloadId != 0 && mDownloadManager != null) |
| 549 | { |
| 550 | mDownloadManager.remove(mLatestFileDownloadId); |
Jose Pascoal | c2545cc | 2014-12-18 16:51:52 +0000 | [diff] [blame] | 551 | saveLatestDownloadId(0); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 552 | } |
Jose Pascoal | c2545cc | 2014-12-18 16:51:52 +0000 | [diff] [blame] | 553 | VersionParserHelper.removeConfigFiles(context); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 556 | private boolean retryDownload(Context context) |
| 557 | { |
Filipe Gonçalves | 5b65c01 | 2015-01-08 13:49:42 +0000 | [diff] [blame] | 558 | Log.d(TAG, "Retry "+mDownloadRetries+" of "+MAX_DOWNLOAD_RETRIES); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 559 | // invalid file |
| 560 | boolean removeReceiver = true; |
| 561 | removeLatestFileDownload(context); |
| 562 | if (mDownloadRetries < MAX_DOWNLOAD_RETRIES) |
| 563 | { |
| 564 | startDownloadLatest(); |
| 565 | mDownloadRetries++; |
| 566 | removeReceiver = false; |
| 567 | } |
| 568 | if (removeReceiver) |
| 569 | { |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 570 | Toast.makeText(getApplicationContext(), getResources().getString(R.string.config_file_download_error_message), Toast.LENGTH_LONG).show(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 571 | } |
| 572 | return removeReceiver; |
| 573 | } |
Jose Pascoal | 0e96628 | 2014-08-12 18:49:05 +0100 | [diff] [blame] | 574 | |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 575 | private void onDownloadStatus(long id, Runnable ifRunning) { |
Filipe Gonçalves | 712d548 | 2015-01-13 17:00:44 +0000 | [diff] [blame] | 576 | Cursor cursor = mDownloadManager != null ? mDownloadManager.query(new DownloadManager.Query().setFilterById(id)) : null; |
| 577 | if (cursor != null && cursor.moveToFirst()) |
| 578 | { |
| 579 | int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); |
| 580 | switch(status){ |
Filipe Gonçalves | 712d548 | 2015-01-13 17:00:44 +0000 | [diff] [blame] | 581 | case DownloadManager.STATUS_PAUSED: |
| 582 | if (ifRunning != null) { |
| 583 | ifRunning.run(); |
| 584 | } |
| 585 | break; |
| 586 | case DownloadManager.STATUS_PENDING: |
| 587 | if (ifRunning != null) { |
| 588 | ifRunning.run(); |
| 589 | } |
| 590 | break; |
| 591 | case DownloadManager.STATUS_RUNNING: |
| 592 | if (ifRunning != null) { |
| 593 | ifRunning.run(); |
| 594 | } |
| 595 | break; |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 596 | case DownloadManager.STATUS_FAILED: |
Filipe Gonçalves | 712d548 | 2015-01-13 17:00:44 +0000 | [diff] [blame] | 597 | case DownloadManager.STATUS_SUCCESSFUL: |
Filipe Gonçalves | 712d548 | 2015-01-13 17:00:44 +0000 | [diff] [blame] | 598 | default: |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | if (cursor != null) |
| 603 | { |
| 604 | cursor.close(); |
| 605 | } |
| 606 | } |
| 607 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 608 | private class DownloadBroadCastReceiver extends BroadcastReceiver |
| 609 | { |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 610 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 611 | @Override |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 612 | public void onReceive(Context context, Intent intent) |
| 613 | { |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 614 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 615 | boolean removeReceiver = false; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 616 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 617 | DownloadManager.Query query = new DownloadManager.Query(); |
| 618 | |
| 619 | query.setFilterById(mLatestFileDownloadId); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 620 | |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 621 | Cursor cursor = mDownloadManager != null ? mDownloadManager.query(query) : null; |
| 622 | |
| 623 | if (cursor != null && cursor.moveToFirst()) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 624 | { |
| 625 | int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); |
| 626 | int status = cursor.getInt(columnIndex); |
| 627 | Resources resources = context.getApplicationContext().getResources(); |
| 628 | |
| 629 | switch (status) |
| 630 | { |
| 631 | case DownloadManager.STATUS_SUCCESSFUL: |
| 632 | { |
Filipe Gonçalves | 5b65c01 | 2015-01-08 13:49:42 +0000 | [diff] [blame] | 633 | Log.d(TAG, "Download successful."); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 634 | |
Filipe Gonçalves | 4cc0b2e | 2015-05-27 18:12:40 +0100 | [diff] [blame] | 635 | if (updateUpdaterData(context)) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 636 | { |
Filipe Gonçalves | 4cc0b2e | 2015-05-27 18:12:40 +0100 | [diff] [blame] | 637 | removeLatestFileDownload(context); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 638 | } |
| 639 | else |
| 640 | { |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 641 | Toast.makeText(getApplicationContext(), resources.getString(R.string.invalid_signature_download_message), Toast.LENGTH_LONG).show(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 642 | removeReceiver = retryDownload(context); |
| 643 | } |
| 644 | break; |
| 645 | } |
| 646 | case DownloadManager.STATUS_FAILED: |
| 647 | { |
Filipe Gonçalves | 5b65c01 | 2015-01-08 13:49:42 +0000 | [diff] [blame] | 648 | Log.d(TAG, "Download failed."); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 649 | removeReceiver = retryDownload(context); |
| 650 | break; |
| 651 | } |
Filipe Gonçalves | 42ffc32 | 2015-01-28 12:51:14 +0000 | [diff] [blame] | 652 | default: |
| 653 | { |
| 654 | Log.d(TAG, "Status broadcast on mLatestFileDownloadId ("+mLatestFileDownloadId+"): "+ status); |
| 655 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 656 | } |
| 657 | } |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 658 | |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 659 | if (cursor != null) |
| 660 | { |
| 661 | cursor.close(); |
| 662 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 663 | |
| 664 | if (removeReceiver) |
| 665 | { |
Filipe Gonçalves | 5b65c01 | 2015-01-08 13:49:42 +0000 | [diff] [blame] | 666 | Log.d(TAG, "Configuration download failed. Clearing grace period."); |
Jose Pascoal | 0b48f8d | 2015-02-06 16:06:41 +0000 | [diff] [blame] | 667 | mSharedPreferences.edit().remove(LAST_CONFIG_DOWNLOAD_IN_MS).apply(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 671 | } |