Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 1 | package com.fairphone.updater.fragments; |
| 2 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 3 | import android.annotation.SuppressLint; |
Jose Pascoal | 7539216 | 2014-10-15 18:29:01 +0100 | [diff] [blame] | 4 | import android.app.AlertDialog; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 5 | import android.app.DownloadManager; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 6 | import android.app.ProgressDialog; |
| 7 | import android.content.BroadcastReceiver; |
| 8 | import android.content.Context; |
Jose Pascoal | 7539216 | 2014-10-15 18:29:01 +0100 | [diff] [blame] | 9 | import android.content.DialogInterface; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 10 | import android.content.Intent; |
| 11 | import android.content.IntentFilter; |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 12 | import android.content.SharedPreferences.Editor; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 13 | import android.content.res.Resources; |
| 14 | import android.content.res.Resources.NotFoundException; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 15 | import android.database.Cursor; |
Filipe Gonçalves | d28bd62 | 2014-11-05 11:40:12 +0000 | [diff] [blame] | 16 | import android.net.ConnectivityManager; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 17 | import android.os.AsyncTask; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 18 | import android.os.Bundle; |
| 19 | import android.os.Environment; |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 20 | import android.text.TextUtils; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 21 | import android.util.Log; |
| 22 | import android.view.LayoutInflater; |
| 23 | import android.view.View; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 24 | import android.view.View.OnClickListener; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 25 | import android.view.ViewGroup; |
| 26 | import android.widget.Button; |
| 27 | import android.widget.LinearLayout; |
| 28 | import android.widget.ProgressBar; |
| 29 | import android.widget.TextView; |
| 30 | import android.widget.Toast; |
| 31 | |
Jose Pascoal | 7bf83a0 | 2014-10-13 18:30:18 +0100 | [diff] [blame] | 32 | import com.fairphone.updater.FairphoneUpdater.HeaderType; |
| 33 | import com.fairphone.updater.FairphoneUpdater.UpdaterState; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 34 | import com.fairphone.updater.R; |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 35 | import com.fairphone.updater.UpdaterService; |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 36 | import com.fairphone.updater.data.DownloadableItem; |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 37 | import com.fairphone.updater.data.Store; |
Jose Pascoal | 7bf83a0 | 2014-10-13 18:30:18 +0100 | [diff] [blame] | 38 | import com.fairphone.updater.data.Version; |
Jose Pascoal | c2545cc | 2014-12-18 16:51:52 +0000 | [diff] [blame] | 39 | import com.fairphone.updater.data.VersionParserHelper; |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 40 | import com.fairphone.updater.tools.PrivilegeChecker; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 41 | import com.fairphone.updater.tools.Utils; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 42 | import com.stericson.RootTools.exceptions.RootDeniedException; |
| 43 | import com.stericson.RootTools.execution.CommandCapture; |
| 44 | import com.stericson.RootTools.execution.Shell; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 45 | |
Maarten Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 46 | import java.io.File; |
| 47 | import java.io.IOException; |
| 48 | import java.util.concurrent.TimeoutException; |
| 49 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 50 | @SuppressLint("ValidFragment") |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 51 | public class DownloadAndRestartFragment extends BaseFragment |
| 52 | { |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 53 | |
Filipe Gonçalves | 8ad8bfb | 2015-01-15 14:14:50 +0000 | [diff] [blame] | 54 | private static final int PROGRESS_BAR_UPDATE_FREQUENCY_IN_MILLIS = 1000; |
| 55 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 56 | private static final String TAG = DownloadAndRestartFragment.class.getSimpleName(); |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 57 | private static final int GET_LATEST_DOWNLOAD_ID_RETRIES = 12; |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 58 | private boolean mIsZipInstall; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 59 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 60 | private TextView mDownloadVersionName; |
| 61 | private LinearLayout mVersionDownloadingGroup; |
| 62 | private ProgressBar mVersionDownloadProgressBar; |
| 63 | private LinearLayout mVersionInstallGroup; |
| 64 | private Button mRestartButton; |
| 65 | private Button mCancelButton; |
| 66 | private Version mSelectedVersion; |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 67 | private Store mSelectedStore; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 68 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 69 | private final boolean mIsVersion; |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 70 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 71 | private DownloadManager mDownloadManager; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 72 | |
Maarten Derks | 2f5e61e | 2016-04-12 14:17:55 +0200 | [diff] [blame] | 73 | private CopyFileToCacheTask copyTask; |
| 74 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 75 | private DownloadBroadCastReceiver mDownloadBroadCastReceiver; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 76 | |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 77 | private BroadcastReceiver mNetworkStateReceiver; |
| 78 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 79 | private long mLatestUpdateDownloadId; |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 80 | private TextView mDownloadCompleteLabel; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 81 | |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 82 | public DownloadAndRestartFragment(boolean isVersion) |
| 83 | { |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 84 | super(); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 85 | |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 86 | mIsVersion = isVersion; |
| 87 | } |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 88 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 89 | @Override |
| 90 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) |
| 91 | { |
| 92 | // Inflate the layout for this fragment |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 93 | mIsZipInstall = UpdaterState.ZIP_INSTALL == mainActivity.getCurrentUpdaterState(); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 94 | View view; |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 95 | if (mIsVersion) |
| 96 | { |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 97 | mSelectedVersion = getSelectedVersion(); |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 98 | view = inflateViewByImageType(inflater, container); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 99 | mSelectedStore = null; |
| 100 | } |
| 101 | else |
| 102 | { |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 103 | mSelectedStore = mainActivity.getSelectedStore(); |
| 104 | view = inflateStoreView(inflater, container); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 105 | mSelectedVersion = null; |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 106 | } |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 107 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 108 | setupLayout(view); |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 109 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 110 | return view; |
| 111 | } |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 112 | |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 113 | private Version getSelectedVersion() { |
| 114 | Version version; |
| 115 | if(mIsZipInstall) { |
| 116 | Resources resources = mainActivity.getResources(); |
| 117 | |
| 118 | //Get the zip file name |
| 119 | String[] zipPath = getDownloadPath(null).split("/"); |
| 120 | String zipName = ""; |
| 121 | if (zipPath != null && zipPath.length > 0) { |
| 122 | zipName = zipPath[zipPath.length - 1]; |
| 123 | } |
| 124 | |
| 125 | version = new Version(); |
| 126 | version.setName(resources.getString(R.string.install) + " " + zipName); |
Jose Pascoal | 60c9937 | 2015-03-17 15:52:59 +0000 | [diff] [blame] | 127 | version.setDownloadLink(mainActivity.getZipFilePath()); |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 128 | version.setId(null); |
Maarten Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 129 | } else |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 130 | { |
| 131 | version = mainActivity.getSelectedVersion(); |
| 132 | } |
| 133 | return version; |
| 134 | } |
| 135 | |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 136 | private View inflateViewByImageType(LayoutInflater inflater, ViewGroup container) |
| 137 | { |
| 138 | View view = inflater.inflate(R.layout.fragment_download_fairphone, container, false); |
| 139 | if (mSelectedVersion != null) |
| 140 | { |
| 141 | if (Version.IMAGE_TYPE_AOSP.equalsIgnoreCase(mSelectedVersion.getImageType())) |
| 142 | { |
| 143 | view = inflater.inflate(R.layout.fragment_download_android, container, false); |
| 144 | } |
| 145 | else if (Version.IMAGE_TYPE_FAIRPHONE.equalsIgnoreCase(mSelectedVersion.getImageType())) |
| 146 | { |
| 147 | view = inflater.inflate(R.layout.fragment_download_fairphone, container, false); |
| 148 | } |
| 149 | } |
| 150 | return view; |
| 151 | } |
| 152 | |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 153 | private static View inflateStoreView(LayoutInflater inflater, ViewGroup container) |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 154 | { |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 155 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 156 | return inflater.inflate(R.layout.fragment_download_app_store, container, false); |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 157 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 158 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 159 | private void toggleDownloadProgressAndRestart() |
| 160 | { |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 161 | UpdaterState state = mainActivity.getCurrentUpdaterState(); |
| 162 | switch (state) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 163 | { |
| 164 | case DOWNLOAD: |
Filipe Gonçalves | a36d0bd | 2015-03-19 15:00:42 +0000 | [diff] [blame] | 165 | mVersionInstallGroup.setVisibility(View.GONE); |
| 166 | mVersionDownloadingGroup.setVisibility(View.VISIBLE); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 167 | setupDownloadState(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 168 | break; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 169 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 170 | case PREINSTALL: |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 171 | case ZIP_INSTALL: |
Filipe Gonçalves | a36d0bd | 2015-03-19 15:00:42 +0000 | [diff] [blame] | 172 | mVersionDownloadingGroup.setVisibility(View.GONE); |
| 173 | mVersionInstallGroup.setVisibility(View.VISIBLE); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 174 | |
Filipe Gonçalves | a36d0bd | 2015-03-19 15:00:42 +0000 | [diff] [blame] | 175 | setupPreInstallState(); |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 176 | |
| 177 | mRestartButton.setOnClickListener(new OnClickListener() |
| 178 | { |
| 179 | |
| 180 | @Override |
| 181 | public void onClick(View v) |
| 182 | { |
Maarten Derks | c75843c | 2016-04-26 11:58:36 +0200 | [diff] [blame^] | 183 | if(Utils.isBatteryLevelOk(mainActivity)) { |
| 184 | if (mIsVersion) { |
| 185 | showEraseAllDataWarning(); |
| 186 | } else if (mSelectedStore != null) { |
| 187 | startPreInstall(); |
| 188 | } |
| 189 | } else { |
| 190 | AlertDialog.Builder batteryDialog = new AlertDialog.Builder(mainActivity); |
| 191 | batteryDialog.setIcon(R.drawable.ic_battery_std_fpblue_24dp); |
| 192 | batteryDialog.setTitle(R.string.charge_battery); |
| 193 | batteryDialog.setPositiveButton(R.string.got_it, new DialogInterface.OnClickListener() { |
| 194 | public void onClick(DialogInterface dialog, int id) { |
| 195 | // do nothing, since the state is still the same |
| 196 | } |
| 197 | }); |
| 198 | batteryDialog.create(); |
| 199 | batteryDialog.show(); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 200 | } |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 201 | } |
| 202 | }); |
| 203 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 204 | break; |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 205 | case NORMAL: |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 206 | default: |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 207 | Log.w(TAG, "Wrong State: " + state + "\nOnly DOWNLOAD and PREINSTALL are supported"); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 208 | mainActivity.runOnUiThread(new Runnable() { |
| 209 | @Override |
| 210 | public void run() { |
| 211 | mainActivity.onBackPressed(); |
| 212 | } |
| 213 | }); |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 214 | return; |
| 215 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 216 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 217 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 218 | mCancelButton.setOnClickListener(new OnClickListener() |
| 219 | { |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 220 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 221 | @Override |
| 222 | public void onClick(View v) |
| 223 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 224 | abortUpdateProcess(""); |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 225 | if(Utils.hasUnifiedPartition(mainActivity.getResources())) |
| 226 | { |
| 227 | Utils.clearCache(); |
| 228 | } |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 229 | mainActivity.runOnUiThread(new Runnable() { |
| 230 | @Override |
| 231 | public void run() { |
| 232 | mainActivity.onBackPressed(); |
| 233 | } |
| 234 | }); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 235 | } |
| 236 | }); |
| 237 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 238 | |
Jose Pascoal | 7539216 | 2014-10-15 18:29:01 +0100 | [diff] [blame] | 239 | private void showEraseAllDataWarning() |
| 240 | { |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 241 | if (mSelectedVersion != null && mSelectedVersion.hasEraseAllPartitionWarning()) |
Jose Pascoal | 7539216 | 2014-10-15 18:29:01 +0100 | [diff] [blame] | 242 | { |
| 243 | new AlertDialog.Builder(mainActivity).setTitle(android.R.string.dialog_alert_title).setMessage(R.string.erase_all_partitions_warning_message) |
| 244 | .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() |
| 245 | { |
| 246 | |
| 247 | @Override |
| 248 | public void onClick(DialogInterface dialog, int which) |
| 249 | { |
| 250 | startPreInstall(); |
| 251 | } |
| 252 | }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() |
| 253 | { |
| 254 | public void onClick(DialogInterface dialog, int which) |
| 255 | { |
| 256 | // do nothing |
| 257 | } |
| 258 | }).show(); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | startPreInstall(); |
| 263 | } |
| 264 | } |
| 265 | |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 266 | private void updateHeader() |
| 267 | { |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 268 | if (mIsVersion && mSelectedVersion != null) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 269 | { |
| 270 | if (Version.IMAGE_TYPE_FAIRPHONE.equalsIgnoreCase(mSelectedVersion.getImageType())) |
| 271 | { |
Tiago Costa | 3855faa | 2014-11-14 17:55:05 +0000 | [diff] [blame] | 272 | mainActivity.updateHeader(HeaderType.MAIN_FAIRPHONE, "", false); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 273 | } |
| 274 | else if (Version.IMAGE_TYPE_AOSP.equalsIgnoreCase(mSelectedVersion.getImageType())) |
| 275 | { |
Tiago Costa | 3855faa | 2014-11-14 17:55:05 +0000 | [diff] [blame] | 276 | mainActivity.updateHeader(HeaderType.MAIN_ANDROID, "", false); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 277 | } |
| 278 | } |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 279 | else if (mSelectedStore != null) |
| 280 | { |
| 281 | mainActivity.updateHeader(HeaderType.MAIN_APP_STORE, "", false); |
| 282 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 283 | else |
| 284 | { |
Tiago Costa | 3855faa | 2014-11-14 17:55:05 +0000 | [diff] [blame] | 285 | mainActivity.updateHeader(HeaderType.MAIN_FAIRPHONE, "", false); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 286 | } |
| 287 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 288 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 289 | private void startDownloadProgressUpdateThread() |
| 290 | { |
| 291 | new Thread(new Runnable() |
| 292 | { |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 293 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 294 | @Override |
| 295 | public void run() |
| 296 | { |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 297 | boolean downloading = true; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 298 | |
Filipe Gonçalves | 8ad8bfb | 2015-01-15 14:14:50 +0000 | [diff] [blame] | 299 | long latestUpdateDownloadId = mainActivity.getLatestDownloadId(); |
Tiago Costa | 7c47e2d | 2014-11-18 16:50:35 +0000 | [diff] [blame] | 300 | |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 301 | int count = GET_LATEST_DOWNLOAD_ID_RETRIES; |
Tiago Costa | 7c47e2d | 2014-11-18 16:50:35 +0000 | [diff] [blame] | 302 | |
Filipe Gonçalves | 8ad8bfb | 2015-01-15 14:14:50 +0000 | [diff] [blame] | 303 | // Wait a sensible amount of time to get a correct reference to the download |
| 304 | while ((latestUpdateDownloadId <= 0) && count > 0) |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 305 | { |
Tiago Costa | 7c47e2d | 2014-11-18 16:50:35 +0000 | [diff] [blame] | 306 | try |
| 307 | { |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 308 | Thread.sleep(Utils.DELAY_HALF_SECOND); |
Tiago Costa | 7c47e2d | 2014-11-18 16:50:35 +0000 | [diff] [blame] | 309 | count--; |
Filipe Gonçalves | 8ad8bfb | 2015-01-15 14:14:50 +0000 | [diff] [blame] | 310 | latestUpdateDownloadId = mainActivity.getLatestDownloadId(); |
Tiago Costa | 7c47e2d | 2014-11-18 16:50:35 +0000 | [diff] [blame] | 311 | } catch (InterruptedException e) |
| 312 | { |
| 313 | e.printStackTrace(); |
| 314 | } |
| 315 | } |
Tiago Costa | 7c47e2d | 2014-11-18 16:50:35 +0000 | [diff] [blame] | 316 | |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 317 | while (mDownloadManager != null && latestUpdateDownloadId != 0 && downloading) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 318 | { |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 319 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 320 | DownloadManager.Query q = new DownloadManager.Query(); |
| 321 | q.setFilterById(latestUpdateDownloadId); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 322 | |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 323 | Cursor cursor = mDownloadManager != null ? mDownloadManager.query(q) : null; |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 324 | |
Jose Pascoal | da015b1 | 2014-11-06 12:47:11 +0000 | [diff] [blame] | 325 | if (cursor != null && cursor.moveToFirst()) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 326 | { |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 327 | try |
| 328 | { |
| 329 | int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); |
| 330 | int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 331 | |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 332 | if ((bytes_total + Utils.BUFFER_SIZE_10_MBYTES) > Utils.getAvailablePartitionSizeInBytes(Environment.getExternalStorageDirectory())) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 333 | { |
| 334 | downloading = false; |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 335 | abortUpdateProcess(getResources().getString(R.string.no_space_available_sd_card_message)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 336 | } |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 337 | else |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 338 | { |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 339 | switch (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))) |
| 340 | { |
| 341 | case DownloadManager.STATUS_SUCCESSFUL: |
| 342 | case DownloadManager.STATUS_FAILED: |
| 343 | downloading = false; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 344 | |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 345 | bytes_downloaded = 0; |
| 346 | bytes_total = 0; |
| 347 | break; |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 348 | default: |
| 349 | break; |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | mVersionDownloadProgressBar.setProgress(bytes_downloaded); |
| 353 | mVersionDownloadProgressBar.setMax(bytes_total); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 354 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 355 | } catch (Exception e) |
| 356 | { |
| 357 | downloading = false; |
| 358 | Log.e(TAG, "Error updating download progress: " + e.getMessage()); |
| 359 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 360 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 361 | cursor.close(); |
| 362 | try |
| 363 | { |
Filipe Gonçalves | 8ad8bfb | 2015-01-15 14:14:50 +0000 | [diff] [blame] | 364 | Thread.sleep(PROGRESS_BAR_UPDATE_FREQUENCY_IN_MILLIS); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 365 | } catch (InterruptedException e) |
| 366 | { |
| 367 | e.printStackTrace(); |
| 368 | } |
| 369 | } |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 370 | else |
| 371 | { |
Jose Pascoal | da015b1 | 2014-11-06 12:47:11 +0000 | [diff] [blame] | 372 | if (cursor != null) |
| 373 | { |
| 374 | downloading = false; |
| 375 | cursor.close(); |
| 376 | } |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 377 | if (mDownloadManager == null) |
| 378 | { |
| 379 | downloading = false; |
| 380 | } |
| 381 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | }).start(); |
| 385 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 386 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 387 | private void setupLayout(View view) |
| 388 | { |
| 389 | mDownloadVersionName = (TextView) view.findViewById(R.id.download_version_name_text); |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 390 | mDownloadCompleteLabel = (TextView) view.findViewById(R.id.download_complete_label); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 391 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 392 | // download in progress group |
| 393 | mVersionDownloadingGroup = (LinearLayout) view.findViewById(R.id.version_downloading_group); |
| 394 | mVersionDownloadProgressBar = (ProgressBar) view.findViewById(R.id.version_download_progress_bar); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 395 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 396 | // restart group |
| 397 | mVersionInstallGroup = (LinearLayout) view.findViewById(R.id.version_install_group); |
| 398 | mRestartButton = (Button) view.findViewById(R.id.restart_button); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 399 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 400 | mCancelButton = (Button) view.findViewById(R.id.cancel_button); |
| 401 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 402 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 403 | @Override |
| 404 | public void onResume() |
| 405 | { |
| 406 | super.onResume(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 407 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 408 | setupInstallationReceivers(); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 409 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 410 | registerDownloadBroadCastReceiver(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 411 | |
Filipe | 97f2bec | 2015-02-11 14:52:49 +0000 | [diff] [blame] | 412 | registerNetworkStatusBroadcastReceiver(); |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 413 | |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 414 | updateHeader(); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 415 | |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 416 | DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore; |
| 417 | if (item != null) |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 418 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 419 | mDownloadVersionName.setText(mainActivity.getItemName(item, mIsVersion)); |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 420 | if(mIsZipInstall) |
| 421 | { |
| 422 | mDownloadCompleteLabel.setVisibility(View.GONE); |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | mDownloadCompleteLabel.setVisibility(View.VISIBLE); |
| 427 | } |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 428 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 429 | toggleDownloadProgressAndRestart(); |
| 430 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 431 | |
Filipe | 97f2bec | 2015-02-11 14:52:49 +0000 | [diff] [blame] | 432 | private void registerNetworkStatusBroadcastReceiver() |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 433 | { |
Filipe | 97f2bec | 2015-02-11 14:52:49 +0000 | [diff] [blame] | 434 | unregisterNetworkStatusBroadcastReceiver(); |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 435 | // Setup monitoring for future connectivity status changes |
Filipe | 97f2bec | 2015-02-11 14:52:49 +0000 | [diff] [blame] | 436 | mNetworkStateReceiver = new BroadcastReceiver() |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 437 | { |
Filipe | 97f2bec | 2015-02-11 14:52:49 +0000 | [diff] [blame] | 438 | @Override |
| 439 | public void onReceive(Context context, Intent intent) |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 440 | { |
Filipe | 97f2bec | 2015-02-11 14:52:49 +0000 | [diff] [blame] | 441 | if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 442 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 443 | Log.w(TAG, "Aborted due to connection failure."); |
| 444 | abortUpdateProcess(""); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 445 | mainActivity.runOnUiThread(new Runnable() { |
| 446 | @Override |
| 447 | public void run() { |
| 448 | mainActivity.onBackPressed(); |
| 449 | } |
| 450 | }); |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 451 | } |
Filipe | 97f2bec | 2015-02-11 14:52:49 +0000 | [diff] [blame] | 452 | } |
| 453 | }; |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 454 | |
| 455 | IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); |
| 456 | mainActivity.registerReceiver(mNetworkStateReceiver, filter); |
| 457 | } |
| 458 | |
Filipe | 97f2bec | 2015-02-11 14:52:49 +0000 | [diff] [blame] | 459 | private void unregisterNetworkStatusBroadcastReceiver() |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 460 | { |
| 461 | if (mNetworkStateReceiver != null) |
| 462 | { |
| 463 | mainActivity.unregisterReceiver(mNetworkStateReceiver); |
| 464 | |
| 465 | mNetworkStateReceiver = null; |
| 466 | } |
| 467 | } |
| 468 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 469 | @Override |
| 470 | public void onPause() |
| 471 | { |
| 472 | super.onPause(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 473 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 474 | unregisterBroadCastReceiver(); |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 475 | |
Filipe | 97f2bec | 2015-02-11 14:52:49 +0000 | [diff] [blame] | 476 | unregisterNetworkStatusBroadcastReceiver(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 477 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 478 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 479 | private void setupInstallationReceivers() |
| 480 | { |
| 481 | mDownloadManager = (DownloadManager) mainActivity.getSystemService(Context.DOWNLOAD_SERVICE); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 482 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 483 | mDownloadBroadCastReceiver = new DownloadBroadCastReceiver(); |
| 484 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 485 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 486 | private void registerDownloadBroadCastReceiver() |
| 487 | { |
| 488 | mainActivity.registerReceiver(mDownloadBroadCastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); |
| 489 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 490 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 491 | private void unregisterBroadCastReceiver() |
| 492 | { |
| 493 | mainActivity.unregisterReceiver(mDownloadBroadCastReceiver); |
| 494 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 495 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 496 | private class DownloadBroadCastReceiver extends BroadcastReceiver |
| 497 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 498 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 499 | @Override |
| 500 | public void onReceive(Context context, Intent intent) |
| 501 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 502 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 503 | mainActivity.getLatestUpdateDownloadIdFromSharedPreference(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 504 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 505 | updateDownloadFile(); |
| 506 | } |
| 507 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 508 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 509 | private void updateDownloadFile() |
| 510 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 511 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 512 | long downloadId = mainActivity.getLatestDownloadId(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 513 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 514 | if (downloadId != 0) |
| 515 | { |
| 516 | DownloadManager.Query query = new DownloadManager.Query(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 517 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 518 | query.setFilterById(downloadId); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 519 | |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 520 | Cursor cursor = mDownloadManager != null ? mDownloadManager.query(query) : null; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 521 | |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 522 | if (cursor != null && cursor.moveToFirst()) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 523 | { |
| 524 | int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); |
| 525 | int status = cursor.getInt(columnIndex); |
| 526 | |
| 527 | switch (status) |
| 528 | { |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 529 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 530 | case DownloadManager.STATUS_SUCCESSFUL: |
| 531 | mainActivity.updateStatePreference(UpdaterState.PREINSTALL); |
| 532 | toggleDownloadProgressAndRestart(); |
| 533 | break; |
| 534 | case DownloadManager.STATUS_RUNNING: |
Tiago Costa | 7c47e2d | 2014-11-18 16:50:35 +0000 | [diff] [blame] | 535 | case DownloadManager.STATUS_PENDING: |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 536 | startDownloadProgressUpdateThread(); |
| 537 | break; |
| 538 | case DownloadManager.STATUS_FAILED: |
| 539 | Resources resources = getResources(); |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 540 | DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore; |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 541 | String error; |
Jose Pascoal | 02d8624 | 2014-12-17 18:50:08 +0000 | [diff] [blame] | 542 | if (item != null) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 543 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 544 | String downloadTitle = Utils.getDownloadTitleFromDownloadableItem(getResources(), item, mIsVersion); |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 545 | error = resources.getString(R.string.error_downloading) + " " + downloadTitle; |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 546 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 547 | else |
| 548 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 549 | error = resources.getString(R.string.error_downloading); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 550 | } |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 551 | abortUpdateProcess(error); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 552 | mainActivity.runOnUiThread(new Runnable() { |
| 553 | @Override |
| 554 | public void run() { |
| 555 | mainActivity.onBackPressed(); |
| 556 | } |
| 557 | }); |
| 558 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 559 | break; |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 560 | default: |
| 561 | break; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 562 | } |
| 563 | } |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 564 | else |
| 565 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 566 | abortUpdateProcess(""); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 567 | |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 568 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 569 | |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 570 | if (cursor != null) |
| 571 | { |
| 572 | cursor.close(); |
| 573 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | // ************************************************************************************ |
| 578 | // PRE INSTALL |
| 579 | // ************************************************************************************ |
| 580 | |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 581 | private void setupPreInstallState() |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 582 | { |
| 583 | Resources resources = mainActivity.getResources(); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 584 | DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore; |
| 585 | if (item != null) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 586 | { |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 587 | File file = new File(getDownloadPath(item)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 588 | |
| 589 | if (file.exists()) |
| 590 | { |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 591 | copyUpdateToCache(file); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 592 | } |
| 593 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 594 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 595 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 596 | // ************************************************************************************ |
| 597 | // DOWNLOAD UPDATE |
| 598 | // ************************************************************************************ |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 599 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 600 | void setupDownloadState() |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 601 | { |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 602 | DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore; |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 603 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 604 | // setup the download state views |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 605 | if (item == null) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 606 | { |
| 607 | Resources resources = getResources(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 608 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 609 | // we don't have the lastest.xml so get back to initial state |
| 610 | File updateDir = new File(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder)); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 611 | |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 612 | final boolean notDeleted = !updateDir.delete(); |
| 613 | if(notDeleted) { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 614 | Log.d(TAG, "Unable to delete " + updateDir.getAbsolutePath()); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 615 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 616 | |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 617 | abortUpdateProcess(""); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 618 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 619 | return; |
| 620 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 621 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 622 | // if there is a download ID on the shared preferences |
| 623 | if (mLatestUpdateDownloadId == 0) |
| 624 | { |
| 625 | mLatestUpdateDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 626 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 627 | // invalid download Id |
| 628 | if (mLatestUpdateDownloadId == 0) |
| 629 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 630 | abortUpdateProcess(""); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 631 | return; |
| 632 | } |
| 633 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 634 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 635 | updateDownloadFile(); |
| 636 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 637 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 638 | private void startPreInstall() |
| 639 | { |
Jose Pascoal | 0b48f8d | 2015-02-06 16:06:41 +0000 | [diff] [blame] | 640 | final Resources resources = getResources(); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 641 | DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore; |
| 642 | |
Jose Pascoal | 60c9937 | 2015-03-17 15:52:59 +0000 | [diff] [blame] | 643 | String otaPackagePath = Utils.getOtaPackagePath(resources, item, mIsVersion, mIsZipInstall); |
Jose Pascoal | 288dd2f | 2015-02-20 19:19:04 +0000 | [diff] [blame] | 644 | |
| 645 | boolean fileNotExists = !Utils.fileExists(otaPackagePath); |
| 646 | |
| 647 | if (fileNotExists) |
Filipe Gonçalves | 72dac94 | 2014-12-15 18:05:58 +0000 | [diff] [blame] | 648 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 649 | abortUpdateProcess(resources.getString(R.string.file_not_found_message) + ": " + otaPackagePath); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 650 | mainActivity.runOnUiThread(new Runnable() { |
| 651 | @Override |
| 652 | public void run() { |
| 653 | mainActivity.onBackPressed(); |
| 654 | } |
| 655 | }); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 656 | } |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 657 | else if (item != null) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 658 | { |
| 659 | // set the command for the recovery |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 660 | try |
| 661 | { |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 662 | Utils.writeCacheCommand(mainActivity, otaPackagePath); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 663 | |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 664 | new Thread(new Runnable() { |
| 665 | @SuppressLint("CommitPrefEdits") |
| 666 | @Override |
| 667 | public void run() { |
| 668 | Editor editor = mSharedPreferences.edit(); |
| 669 | editor.remove(UpdaterService.PREFERENCE_REINSTALL_GAPPS); |
| 670 | editor.commit(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 671 | |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 672 | if (Utils.hasUnifiedPartition(resources)) |
| 673 | { |
| 674 | removeLastUpdateDownload(); |
| 675 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 676 | |
Tiago Costa | d7e951f | 2015-03-02 17:37:33 +0000 | [diff] [blame] | 677 | // remove the gapps stuff |
| 678 | // String model = Utils.getModelAndOS(getActivity()); |
| 679 | // if( model.contains("FP1") ) { |
| 680 | try { |
| 681 | |
| 682 | Utils.clearGappsData(); |
| 683 | } catch (RootDeniedException | InterruptedException | IOException e) { |
| 684 | e.printStackTrace(); |
| 685 | } |
| 686 | // } |
| 687 | |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 688 | // remove the update files from data |
| 689 | removeUpdateFilesFromData(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 690 | |
Jose Pascoal | 0b48f8d | 2015-02-06 16:06:41 +0000 | [diff] [blame] | 691 | mainActivity.updateStatePreference(UpdaterState.NORMAL); |
| 692 | mainActivity.clearSelectedItems(); |
| 693 | clearConfigFile(); |
| 694 | editor = mSharedPreferences.edit(); |
| 695 | editor.remove(UpdaterService.LAST_CONFIG_DOWNLOAD_IN_MS); |
| 696 | editor.remove(MainFragment.SHARED_PREFERENCES_ENABLE_GAPPS); |
| 697 | editor.commit(); |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 698 | |
| 699 | // reboot the device into recovery |
| 700 | if(!Utils.rebootToRecovery(mainActivity)) { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 701 | String error = resources.getString(R.string.reboot_failed); |
| 702 | Log.w(TAG, error); |
| 703 | abortUpdateProcess(error); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 704 | mainActivity.onBackPressed(); |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 705 | } |
Jose Pascoal | 0b48f8d | 2015-02-06 16:06:41 +0000 | [diff] [blame] | 706 | } |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 707 | }).start(); |
| 708 | } catch (IOException | NotFoundException | TimeoutException | RootDeniedException e) |
| 709 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 710 | String error = resources.getString(R.string.command_write_to_cache_failed); |
| 711 | Log.e(TAG, error + ": " + e.getLocalizedMessage()); |
| 712 | abortUpdateProcess(error); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 713 | mainActivity.runOnUiThread(new Runnable() { |
| 714 | @Override |
| 715 | public void run() { |
| 716 | mainActivity.onBackPressed(); |
| 717 | } |
| 718 | }); |
Jose Pascoal | 49f058a | 2015-02-13 16:58:01 +0000 | [diff] [blame] | 719 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 720 | } |
| 721 | else |
| 722 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 723 | Log.e(TAG, "Null item"); |
| 724 | abortUpdateProcess(""); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 725 | mainActivity.runOnUiThread(new Runnable() { |
| 726 | @Override |
| 727 | public void run() { |
| 728 | mainActivity.onBackPressed(); |
| 729 | } |
| 730 | }); |
| 731 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 732 | } |
| 733 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 734 | |
Jose Pascoal | c2545cc | 2014-12-18 16:51:52 +0000 | [diff] [blame] | 735 | private void clearConfigFile() |
| 736 | { |
| 737 | long cfgFile = mainActivity.getConfigFileDownloadIdFromSharedPreference(); |
| 738 | if (cfgFile != 0 && mDownloadManager != null) |
| 739 | { |
| 740 | mDownloadManager.remove(cfgFile); |
| 741 | } |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 742 | mainActivity.clearConfigFileDownloadId(); |
Jose Pascoal | c2545cc | 2014-12-18 16:51:52 +0000 | [diff] [blame] | 743 | VersionParserHelper.removeConfigFiles(mainActivity); |
| 744 | } |
| 745 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 746 | private void copyUpdateToCache(File file) |
| 747 | { |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 748 | if (Utils.canCopyToCache(file)) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 749 | { |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 750 | DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore; |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 751 | |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 752 | if (item != null) |
| 753 | { |
Maarten Derks | 2f5e61e | 2016-04-12 14:17:55 +0200 | [diff] [blame] | 754 | if (copyTask != null) { |
| 755 | return; |
| 756 | } |
| 757 | copyTask = new CopyFileToCacheTask(); |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 758 | copyTask.execute(file.getPath(), Environment.getDownloadCacheDirectory() + "/" + Utils.getFilenameFromDownloadableItem(item, mIsVersion)); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 759 | } |
| 760 | else |
| 761 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 762 | abortUpdateProcess(getResources().getString(R.string.no_space_available_cache_message)); |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 763 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 764 | } |
| 765 | else |
| 766 | { |
Jose Pascoal | 0a5be01 | 2014-11-17 16:55:40 +0000 | [diff] [blame] | 767 | if (Utils.hasUnifiedPartition(getResources())) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 768 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 769 | Log.w(TAG, "No space on cache. Defaulting to Sdcard"); |
| 770 | abortUpdateProcess(getResources().getString(R.string.no_space_available_cache_message)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 774 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 775 | // ************************************************************************************ |
| 776 | // Update Removal |
| 777 | // ************************************************************************************ |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 778 | private void removeUpdateFilesFromData() { |
| 779 | if (PrivilegeChecker.isPrivilegedApp()) { |
| 780 | removeUpdateFilesFromDataPrivileged(); |
| 781 | } else { |
| 782 | removeUpdateFilesFromDataUnprivileged(); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | private void removeUpdateFilesFromDataPrivileged() { |
| 787 | Resources resource = getResources(); |
| 788 | |
| 789 | try { |
| 790 | Process p = Runtime.getRuntime().exec(resource.getString(R.string.removePlayStoreCommand)); |
| 791 | p.waitFor(); |
| 792 | p = Runtime.getRuntime().exec(resource.getString(R.string.removeGooglePlusCommand)); |
| 793 | p.waitFor(); |
| 794 | p = Runtime.getRuntime().exec(resource.getString(R.string.removeSoundSearchCommand)); |
| 795 | p.waitFor(); |
| 796 | p = Runtime.getRuntime().exec(resource.getString(R.string.removeGmailCommand)); |
| 797 | p.waitFor(); |
| 798 | p = Runtime.getRuntime().exec(resource.getString(R.string.removePlayServicesCommand)); |
| 799 | p.waitFor(); |
| 800 | p = Runtime.getRuntime().exec(resource.getString(R.string.removeQuicksearchCommand)); |
| 801 | p.waitFor(); |
| 802 | p = Runtime.getRuntime().exec(resource.getString(R.string.removeTalkbackCommand)); |
| 803 | p.waitFor(); |
| 804 | p = Runtime.getRuntime().exec(resource.getString(R.string.removeText2SpeechCommand)); |
| 805 | p.waitFor(); |
| 806 | } catch (IOException | InterruptedException e) { |
| 807 | Log.d(TAG, "Failed to remove files from data:" +e); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | private void removeUpdateFilesFromDataUnprivileged() |
| 812 | { |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 813 | try |
| 814 | { |
| 815 | Shell.runRootCommand(new CommandCapture(0, getResources().getString(R.string.removePlayStoreCommand), getResources().getString( |
| 816 | R.string.removeGooglePlusCommand), getResources().getString(R.string.removeSoundSearchCommand), getResources().getString( |
| 817 | R.string.removeGmailCommand), getResources().getString(R.string.removePlayServicesCommand), getResources().getString( |
| 818 | R.string.removeQuicksearchCommand), getResources().getString(R.string.removeTalkbackCommand), getResources().getString( |
| 819 | R.string.removeText2SpeechCommand))); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 820 | } catch (IOException | TimeoutException | RootDeniedException e) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 821 | { |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 822 | Log.d(TAG, "Failed to remove files from data:" +e.getLocalizedMessage()); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 823 | } |
| 824 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 825 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 826 | void removeLastUpdateDownload() |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 827 | { |
| 828 | long latestUpdateDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference(); |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 829 | if (latestUpdateDownloadId != 0 && mDownloadManager != null) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 830 | { |
| 831 | // residue download ID |
| 832 | mDownloadManager.remove(latestUpdateDownloadId); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 833 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 834 | mainActivity.resetLastUpdateDownloadId(); |
| 835 | } |
| 836 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 837 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 838 | private class CopyFileToCacheTask extends AsyncTask<String, Integer, Integer> |
| 839 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 840 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 841 | ProgressDialog mProgress; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 842 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 843 | @Override |
| 844 | protected Integer doInBackground(String... params) |
| 845 | { |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 846 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 847 | // check the correct number of |
| 848 | if (params.length != 2) |
| 849 | { |
| 850 | return -1; |
| 851 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 852 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 853 | String originalFilePath = params[0]; |
| 854 | String destinyFilePath = params[1]; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 855 | |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 856 | Utils.clearCache(); |
| 857 | |
Jose Pascoal | 288dd2f | 2015-02-20 19:19:04 +0000 | [diff] [blame] | 858 | File otaOriginalFile = new File(originalFilePath); |
| 859 | File otaDestinyFile = new File(destinyFilePath); |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 860 | |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 861 | DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore; |
| 862 | |
| 863 | if (!(Utils.checkMD5(item.getMd5Sum(), otaOriginalFile) || mIsZipInstall)) |
| 864 | { |
| 865 | mainActivity.runOnUiThread(new Runnable() { |
| 866 | @Override |
| 867 | public void run() { |
| 868 | abortUpdateProcess(getResources().getString(R.string.invalid_md5_download_message)); |
| 869 | mainActivity.runOnUiThread(new Runnable() { |
| 870 | @Override |
| 871 | public void run() { |
| 872 | mainActivity.onBackPressed(); |
| 873 | } |
| 874 | }); |
| 875 | } |
| 876 | }); |
| 877 | removeLastUpdateDownload(); |
| 878 | } |
| 879 | else if (otaOriginalFile.exists()) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 880 | { |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 881 | try { |
Jose Pascoal | 288dd2f | 2015-02-20 19:19:04 +0000 | [diff] [blame] | 882 | Utils.copy(otaOriginalFile, otaDestinyFile); |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 883 | } catch (IOException e) { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 884 | String error = mainActivity.getResources().getString(R.string.copy_to_cache_failed_message); |
| 885 | Log.e(TAG, error + ": " + originalFilePath + ". " + e.getLocalizedMessage()); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 886 | abortUpdateProcess(error); |
| 887 | mainActivity.runOnUiThread(new Runnable() { |
| 888 | @Override |
| 889 | public void run() { |
| 890 | mainActivity.onBackPressed(); |
| 891 | } |
| 892 | }); |
Filipe Gonçalves | 49ce23c | 2015-02-13 16:33:52 +0000 | [diff] [blame] | 893 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 894 | } |
Jose Pascoal | 733b84e | 2014-10-17 14:49:02 +0100 | [diff] [blame] | 895 | else |
| 896 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 897 | Resources resources = mainActivity.getResources(); |
| 898 | String error = resources.getString(R.string.copy_to_cache_failed_message) + ". " + resources.getString(R.string.file_not_found_message) + ": " + originalFilePath; |
| 899 | Log.e(TAG, error); |
| 900 | abortUpdateProcess(error); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 901 | mainActivity.runOnUiThread(new Runnable() { |
| 902 | @Override |
| 903 | public void run() { |
| 904 | mainActivity.onBackPressed(); |
| 905 | } |
| 906 | }); |
Jose Pascoal | 733b84e | 2014-10-17 14:49:02 +0100 | [diff] [blame] | 907 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 908 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 909 | return 1; |
| 910 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 911 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 912 | protected void onProgressUpdate(Integer... progress) |
| 913 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 914 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 915 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 916 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 917 | protected void onPreExecute() |
| 918 | { |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 919 | if (mProgress == null) |
| 920 | { |
| 921 | String title = ""; |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 922 | String message = mainActivity.getResources().getString(R.string.please_be_patient); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 923 | mProgress = ProgressDialog.show(mainActivity, title, message, true, false); |
| 924 | } |
| 925 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 926 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 927 | protected void onPostExecute(Integer result) |
| 928 | { |
| 929 | // disable the spinner |
| 930 | if (mProgress != null) |
| 931 | { |
| 932 | mProgress.dismiss(); |
| 933 | mProgress = null; |
| 934 | } |
| 935 | } |
| 936 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 937 | |
Jose Pascoal | 1c049e0 | 2014-12-17 13:03:09 +0000 | [diff] [blame] | 938 | private String getDownloadPath(DownloadableItem item) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 939 | { |
Jose Pascoal | 79357c5 | 2015-03-12 20:14:29 +0000 | [diff] [blame] | 940 | String path; |
| 941 | if(mIsZipInstall) |
| 942 | { |
| 943 | path = mainActivity.getZipFilePath(); |
| 944 | } |
| 945 | else |
| 946 | { |
| 947 | Resources resources = mainActivity.getResources(); |
| 948 | path = Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + Utils.getFilenameFromDownloadableItem(item, mIsVersion); |
| 949 | } |
| 950 | return path; |
Tiago Costa | 198bf3d | 2014-12-16 15:23:18 +0000 | [diff] [blame] | 951 | } |
Jose Pascoal | b397dc6 | 2014-10-13 19:26:58 +0100 | [diff] [blame] | 952 | |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 953 | public void abortUpdateProcess(final String reason) |
Jose Pascoal | b397dc6 | 2014-10-13 19:26:58 +0100 | [diff] [blame] | 954 | { |
Jose Pascoal | 4adf319 | 2015-02-23 18:10:16 +0000 | [diff] [blame] | 955 | if(!TextUtils.isEmpty(reason)) { |
| 956 | mainActivity.runOnUiThread(new Runnable() { |
| 957 | @Override |
| 958 | public void run() { |
| 959 | Toast.makeText(mainActivity, reason, Toast.LENGTH_LONG).show(); |
| 960 | } |
| 961 | }); |
| 962 | } |
| 963 | |
Tiago Costa | 73eda41 | 2014-11-18 14:37:42 +0000 | [diff] [blame] | 964 | removeLastUpdateDownload(); |
| 965 | |
Jose Pascoal | 8775874 | 2015-01-28 20:00:22 +0000 | [diff] [blame] | 966 | mainActivity.clearSelectedItems(); |
| 967 | mainActivity.updateStatePreference(UpdaterState.NORMAL); |
Jose Pascoal | b397dc6 | 2014-10-13 19:26:58 +0100 | [diff] [blame] | 968 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 969 | } |