Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 1 | package com.fairphone.updater.fragments; |
| 2 | |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.util.concurrent.TimeoutException; |
| 6 | |
Jose Pascoal | 7539216 | 2014-10-15 18:29:01 +0100 | [diff] [blame] | 7 | import android.app.AlertDialog; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 8 | import android.app.DownloadManager; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 9 | import android.app.ProgressDialog; |
| 10 | import android.content.BroadcastReceiver; |
| 11 | import android.content.Context; |
Jose Pascoal | 7539216 | 2014-10-15 18:29:01 +0100 | [diff] [blame] | 12 | import android.content.DialogInterface; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 13 | import android.content.Intent; |
| 14 | import android.content.IntentFilter; |
| 15 | import android.content.res.Resources; |
| 16 | import android.content.res.Resources.NotFoundException; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 17 | import android.database.Cursor; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 18 | import android.os.AsyncTask; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 19 | import android.os.Bundle; |
| 20 | import android.os.Environment; |
| 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 | 7bf83a0 | 2014-10-13 18:30:18 +0100 | [diff] [blame] | 35 | import com.fairphone.updater.data.Version; |
| 36 | import com.fairphone.updater.data.VersionParserHelper; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 37 | import com.fairphone.updater.gappsinstaller.GappsInstallerHelper; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 38 | import com.fairphone.updater.tools.Utils; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 39 | import com.stericson.RootTools.RootTools; |
| 40 | import com.stericson.RootTools.exceptions.RootDeniedException; |
| 41 | import com.stericson.RootTools.execution.CommandCapture; |
| 42 | import com.stericson.RootTools.execution.Shell; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 43 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 44 | public class DownloadAndRestartFragment extends BaseFragment |
| 45 | { |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 46 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 47 | protected static final String TAG = DownloadAndRestartFragment.class.getSimpleName(); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 48 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 49 | private TextView mDownloadVersionName; |
| 50 | private LinearLayout mVersionDownloadingGroup; |
| 51 | private ProgressBar mVersionDownloadProgressBar; |
| 52 | private LinearLayout mVersionInstallGroup; |
| 53 | private Button mRestartButton; |
| 54 | private Button mCancelButton; |
| 55 | private Version mSelectedVersion; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 56 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 57 | private DownloadManager mDownloadManager; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 58 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 59 | private DownloadBroadCastReceiver mDownloadBroadCastReceiver; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 60 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 61 | private long mLatestUpdateDownloadId; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 62 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 63 | @Override |
| 64 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) |
| 65 | { |
| 66 | // Inflate the layout for this fragment |
| 67 | mSelectedVersion = mainActivity.getSelectedVersion(); |
| 68 | View view = inflateViewByImageType(inflater, container); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 69 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 70 | setupLayout(view); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 71 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 72 | return view; |
| 73 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 74 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 75 | private void toggleDownloadProgressAndRestart() |
| 76 | { |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 77 | UpdaterState state = mainActivity.getCurrentUpdaterState(); |
| 78 | switch (state) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 79 | { |
| 80 | case DOWNLOAD: |
| 81 | setupDownloadState(); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 82 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 83 | mVersionInstallGroup.setVisibility(View.GONE); |
| 84 | mVersionDownloadingGroup.setVisibility(View.VISIBLE); |
| 85 | break; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 86 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 87 | case PREINSTALL: |
| 88 | setupPreInstallState(); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 89 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 90 | mVersionDownloadingGroup.setVisibility(View.GONE); |
| 91 | mVersionInstallGroup.setVisibility(View.VISIBLE); |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 92 | |
| 93 | mRestartButton.setOnClickListener(new OnClickListener() |
| 94 | { |
| 95 | |
| 96 | @Override |
| 97 | public void onClick(View v) |
| 98 | { |
Jose Pascoal | 7539216 | 2014-10-15 18:29:01 +0100 | [diff] [blame] | 99 | showEraseAllDataWarning(); |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 100 | } |
| 101 | }); |
| 102 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 103 | break; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 104 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 105 | default: |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 106 | Log.w(TAG, "Wrong State: " + state + "\nOnly DOWNLOAD and PREINSTALL are supported"); |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 107 | mainActivity.removeLastFragment(true); |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 108 | return; |
| 109 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 110 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 111 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 112 | mCancelButton.setOnClickListener(new OnClickListener() |
| 113 | { |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 114 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 115 | @Override |
| 116 | public void onClick(View v) |
| 117 | { |
Jose Pascoal | b397dc6 | 2014-10-13 19:26:58 +0100 | [diff] [blame] | 118 | abortUpdateProccess(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 119 | } |
| 120 | }); |
| 121 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 122 | |
Jose Pascoal | 7539216 | 2014-10-15 18:29:01 +0100 | [diff] [blame] | 123 | private void showEraseAllDataWarning() |
| 124 | { |
| 125 | if (mSelectedVersion.hasEraseAllPartitionWarning()) |
| 126 | { |
| 127 | new AlertDialog.Builder(mainActivity).setTitle(android.R.string.dialog_alert_title).setMessage(R.string.erase_all_partitions_warning_message) |
| 128 | .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() |
| 129 | { |
| 130 | |
| 131 | @Override |
| 132 | public void onClick(DialogInterface dialog, int which) |
| 133 | { |
| 134 | startPreInstall(); |
| 135 | } |
| 136 | }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() |
| 137 | { |
| 138 | public void onClick(DialogInterface dialog, int which) |
| 139 | { |
| 140 | // do nothing |
| 141 | } |
| 142 | }).show(); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | startPreInstall(); |
| 147 | } |
| 148 | } |
| 149 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 150 | private void updateHeader() |
| 151 | { |
| 152 | if (mSelectedVersion != null) |
| 153 | { |
| 154 | if (Version.IMAGE_TYPE_FAIRPHONE.equalsIgnoreCase(mSelectedVersion.getImageType())) |
| 155 | { |
| 156 | mainActivity.updateHeader(HeaderType.MAIN_FAIRPHONE, ""); |
| 157 | } |
| 158 | else if (Version.IMAGE_TYPE_AOSP.equalsIgnoreCase(mSelectedVersion.getImageType())) |
| 159 | { |
| 160 | mainActivity.updateHeader(HeaderType.MAIN_ANDROID, ""); |
| 161 | } |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | mainActivity.updateHeader(HeaderType.MAIN_FAIRPHONE, ""); |
| 166 | } |
| 167 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 168 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 169 | private void startDownloadProgressUpdateThread() |
| 170 | { |
| 171 | new Thread(new Runnable() |
| 172 | { |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 173 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 174 | @Override |
| 175 | public void run() |
| 176 | { |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 177 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 178 | boolean downloading = true; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 179 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 180 | long latestUpdateDownloadId = mainActivity.getLatestDownloadId(); |
| 181 | while (latestUpdateDownloadId != 0 && downloading) |
| 182 | { |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 183 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 184 | DownloadManager.Query q = new DownloadManager.Query(); |
| 185 | q.setFilterById(latestUpdateDownloadId); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 186 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 187 | Cursor cursor = mDownloadManager.query(q); |
| 188 | if (cursor != null) |
| 189 | { |
| 190 | cursor.moveToFirst(); |
| 191 | try |
| 192 | { |
| 193 | int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); |
| 194 | int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 195 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 196 | if ((bytes_total + 10000) > Utils.getAvailablePartitionSizeInBytes(Environment.getExternalStorageDirectory())) |
| 197 | { |
| 198 | downloading = false; |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 199 | Toast.makeText(mainActivity, getResources().getString(R.string.no_space_available_sd_card_message), Toast.LENGTH_LONG).show(); |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 200 | abortUpdateProccess(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 201 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 202 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 203 | if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) |
| 204 | { |
| 205 | downloading = false; |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 206 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 207 | bytes_downloaded = 0; |
| 208 | bytes_total = 0; |
| 209 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 210 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 211 | mVersionDownloadProgressBar.setProgress(bytes_downloaded); |
| 212 | mVersionDownloadProgressBar.setMax(bytes_total); |
| 213 | } catch (Exception e) |
| 214 | { |
| 215 | downloading = false; |
| 216 | Log.e(TAG, "Error updating download progress: " + e.getMessage()); |
| 217 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 218 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 219 | cursor.close(); |
| 220 | try |
| 221 | { |
| 222 | // TODO WHYYYYYY??????? |
| 223 | Thread.sleep(3000); |
| 224 | } catch (InterruptedException e) |
| 225 | { |
| 226 | e.printStackTrace(); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | }).start(); |
| 232 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 233 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 234 | private View inflateViewByImageType(LayoutInflater inflater, ViewGroup container) |
| 235 | { |
| 236 | View view = inflater.inflate(R.layout.fragment_download_fairphone, container, false); |
| 237 | if (mSelectedVersion != null) |
| 238 | { |
| 239 | if (Version.IMAGE_TYPE_AOSP.equalsIgnoreCase(mSelectedVersion.getImageType())) |
| 240 | { |
| 241 | view = inflater.inflate(R.layout.fragment_download_android, container, false); |
| 242 | } |
| 243 | else if (Version.IMAGE_TYPE_FAIRPHONE.equalsIgnoreCase(mSelectedVersion.getImageType())) |
| 244 | { |
| 245 | view = inflater.inflate(R.layout.fragment_download_fairphone, container, false); |
| 246 | } |
| 247 | } |
| 248 | return view; |
| 249 | } |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 250 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 251 | private void setupLayout(View view) |
| 252 | { |
| 253 | mDownloadVersionName = (TextView) view.findViewById(R.id.download_version_name_text); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 254 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 255 | // download in progress group |
| 256 | mVersionDownloadingGroup = (LinearLayout) view.findViewById(R.id.version_downloading_group); |
| 257 | mVersionDownloadProgressBar = (ProgressBar) view.findViewById(R.id.version_download_progress_bar); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 258 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 259 | // restart group |
| 260 | mVersionInstallGroup = (LinearLayout) view.findViewById(R.id.version_install_group); |
| 261 | mRestartButton = (Button) view.findViewById(R.id.restart_button); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 262 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 263 | mCancelButton = (Button) view.findViewById(R.id.cancel_button); |
| 264 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 265 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 266 | @Override |
| 267 | public void onResume() |
| 268 | { |
| 269 | super.onResume(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 270 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 271 | setupInstallationReceivers(); |
| 272 | registerDownloadBroadCastReceiver(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 273 | |
Jose Pascoal | 8ed98d6 | 2014-10-09 20:41:11 +0100 | [diff] [blame] | 274 | updateHeader(); |
| 275 | mDownloadVersionName.setText(mainActivity.getVersionName(mSelectedVersion)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 276 | toggleDownloadProgressAndRestart(); |
| 277 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 278 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 279 | @Override |
| 280 | public void onPause() |
| 281 | { |
| 282 | super.onPause(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 283 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 284 | unregisterBroadCastReceiver(); |
| 285 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 286 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 287 | private void setupInstallationReceivers() |
| 288 | { |
| 289 | mDownloadManager = (DownloadManager) mainActivity.getSystemService(Context.DOWNLOAD_SERVICE); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 290 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 291 | mDownloadBroadCastReceiver = new DownloadBroadCastReceiver(); |
| 292 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 293 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 294 | private void registerDownloadBroadCastReceiver() |
| 295 | { |
| 296 | mainActivity.registerReceiver(mDownloadBroadCastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); |
| 297 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 298 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 299 | private void unregisterBroadCastReceiver() |
| 300 | { |
| 301 | mainActivity.unregisterReceiver(mDownloadBroadCastReceiver); |
| 302 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 303 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 304 | private class DownloadBroadCastReceiver extends BroadcastReceiver |
| 305 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 306 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 307 | @Override |
| 308 | public void onReceive(Context context, Intent intent) |
| 309 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 310 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 311 | mainActivity.getLatestUpdateDownloadIdFromSharedPreference(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 312 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 313 | updateDownloadFile(); |
| 314 | } |
| 315 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 316 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 317 | private void updateDownloadFile() |
| 318 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 319 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 320 | long downloadId = mainActivity.getLatestDownloadId(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 321 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 322 | if (downloadId != 0) |
| 323 | { |
| 324 | DownloadManager.Query query = new DownloadManager.Query(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 325 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 326 | query.setFilterById(downloadId); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 327 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 328 | Cursor cursor = mDownloadManager.query(query); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 329 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 330 | if (cursor.moveToFirst()) |
| 331 | { |
| 332 | int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); |
| 333 | int status = cursor.getInt(columnIndex); |
| 334 | |
| 335 | switch (status) |
| 336 | { |
| 337 | case DownloadManager.STATUS_SUCCESSFUL: |
| 338 | mainActivity.updateStatePreference(UpdaterState.PREINSTALL); |
| 339 | toggleDownloadProgressAndRestart(); |
| 340 | break; |
| 341 | case DownloadManager.STATUS_RUNNING: |
| 342 | startDownloadProgressUpdateThread(); |
| 343 | break; |
| 344 | case DownloadManager.STATUS_FAILED: |
| 345 | Resources resources = getResources(); |
| 346 | if (mSelectedVersion != null) |
| 347 | { |
| 348 | String downloadTitle = mSelectedVersion.getName() + " " + mSelectedVersion.getImageTypeDescription(resources); |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 349 | Toast.makeText(mainActivity, resources.getString(R.string.error_downloading) + " " + downloadTitle, Toast.LENGTH_LONG).show(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 350 | } |
| 351 | else |
| 352 | { |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 353 | Toast.makeText(mainActivity, resources.getString(R.string.error_downloading), Toast.LENGTH_LONG).show(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 354 | } |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 355 | abortUpdateProccess(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 356 | break; |
| 357 | } |
| 358 | } |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 359 | else |
| 360 | { |
| 361 | abortUpdateProccess(); |
| 362 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 363 | |
| 364 | cursor.close(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // ************************************************************************************ |
| 369 | // PRE INSTALL |
| 370 | // ************************************************************************************ |
| 371 | |
| 372 | private void setupPreInstallState() |
| 373 | { |
| 374 | |
| 375 | Resources resources = mainActivity.getResources(); |
| 376 | // the latest version data must exist |
| 377 | if (mSelectedVersion != null) |
| 378 | { |
| 379 | |
| 380 | // check the md5 of the file |
| 381 | File file = new File(getVersionDownloadPath(mSelectedVersion)); |
| 382 | |
| 383 | if (file.exists()) |
| 384 | { |
| 385 | if (Utils.checkMD5(mSelectedVersion.getMd5Sum(), file)) |
| 386 | { |
| 387 | copyUpdateToCache(file); |
| 388 | return; |
| 389 | } |
| 390 | else |
| 391 | { |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 392 | Toast.makeText(mainActivity, resources.getString(R.string.invalid_md5_download_message), Toast.LENGTH_LONG).show(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 393 | removeLastUpdateDownload(); |
| 394 | } |
| 395 | } |
| 396 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 397 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 398 | // remove the updater directory |
| 399 | File fileDir = new File(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder)); |
| 400 | fileDir.delete(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 401 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 402 | // else if the perfect case does not happen, reset the download |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 403 | abortUpdateProccess(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 404 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 405 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 406 | // ************************************************************************************ |
| 407 | // DOWNLOAD UPDATE |
| 408 | // ************************************************************************************ |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 409 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 410 | public void setupDownloadState() |
| 411 | { |
| 412 | // setup the download state views |
| 413 | if (mSelectedVersion == null) |
| 414 | { |
| 415 | Resources resources = getResources(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 416 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 417 | // we don't have the lastest.xml so get back to initial state |
| 418 | File updateDir = new File(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder)); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 419 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 420 | updateDir.delete(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 421 | |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 422 | abortUpdateProccess(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 423 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 424 | return; |
| 425 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 426 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 427 | // if there is a download ID on the shared preferences |
| 428 | if (mLatestUpdateDownloadId == 0) |
| 429 | { |
| 430 | mLatestUpdateDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 431 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 432 | // invalid download Id |
| 433 | if (mLatestUpdateDownloadId == 0) |
| 434 | { |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 435 | abortUpdateProccess(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 439 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 440 | updateDownloadFile(); |
| 441 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 442 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 443 | private void startPreInstall() |
| 444 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 445 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 446 | if (RootTools.isAccessGiven()) |
| 447 | { |
| 448 | // set the command for the recovery |
| 449 | Resources resources = getResources(); |
| 450 | // Process p; |
| 451 | try |
| 452 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 453 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 454 | Shell.runRootCommand(new CommandCapture(0, "rm -f /cache/recovery/command")); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 455 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 456 | Shell.runRootCommand(new CommandCapture(0, "rm -f /cache/recovery/extendedcommand")); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 457 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 458 | Shell.runRootCommand(new CommandCapture(0, "echo '--wipe_cache' >> /cache/recovery/command")); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 459 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 460 | if (canCopyToCache()) |
| 461 | { |
| 462 | Shell.runRootCommand(new CommandCapture(0, "echo '--update_package=/" + resources.getString(R.string.recoveryCachePath) + "/" |
| 463 | + VersionParserHelper.getNameFromVersion(mSelectedVersion) + "' >> /cache/recovery/command")); |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | Shell.runRootCommand(new CommandCapture(0, "echo '--update_package=/" + resources.getString(R.string.recoverySdCardPath) |
| 468 | + resources.getString(R.string.updaterFolder) + VersionParserHelper.getNameFromVersion(mSelectedVersion) |
| 469 | + "' >> /cache/recovery/command")); |
| 470 | } |
| 471 | } catch (IOException e) |
| 472 | { |
| 473 | // TODO Auto-generated catch block |
| 474 | e.printStackTrace(); |
| 475 | } catch (NotFoundException e) |
| 476 | { |
| 477 | // TODO Auto-generated catch block |
| 478 | e.printStackTrace(); |
| 479 | } catch (TimeoutException e) |
| 480 | { |
| 481 | // TODO Auto-generated catch block |
| 482 | e.printStackTrace(); |
| 483 | } catch (RootDeniedException e) |
| 484 | { |
| 485 | // TODO Auto-generated catch block |
| 486 | e.printStackTrace(); |
| 487 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 488 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 489 | // send broadcast intent |
| 490 | Intent broadcastIntent = new Intent(); |
| 491 | broadcastIntent.setAction(GappsInstallerHelper.GAPPS_REINSTALATION); |
| 492 | mainActivity.sendBroadcast(broadcastIntent); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 493 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 494 | if (canCopyToCache()) |
| 495 | { |
| 496 | removeLastUpdateDownload(); |
| 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 | // remove the update files from data |
| 500 | removeUpdateFilesFromData(); |
Jose Pascoal | 733b84e | 2014-10-17 14:49:02 +0100 | [diff] [blame^] | 501 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 502 | // reboot the device into recovery |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 503 | try |
| 504 | { |
| 505 | mainActivity.updateStatePreference(UpdaterState.NORMAL); |
| 506 | Shell.runRootCommand(new CommandCapture(0, "reboot recovery")); |
| 507 | } catch (IOException e) |
| 508 | { |
| 509 | // TODO Auto-generated catch block |
| 510 | e.printStackTrace(); |
| 511 | } catch (TimeoutException e) |
| 512 | { |
| 513 | // TODO Auto-generated catch block |
| 514 | e.printStackTrace(); |
| 515 | } catch (RootDeniedException e) |
| 516 | { |
| 517 | // TODO Auto-generated catch block |
| 518 | e.printStackTrace(); |
| 519 | } |
| 520 | } |
| 521 | else |
| 522 | { |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 523 | abortUpdateProccess(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 524 | } |
| 525 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 526 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 527 | private void copyUpdateToCache(File file) |
| 528 | { |
| 529 | if (canCopyToCache()) |
| 530 | { |
Jose Pascoal | 733b84e | 2014-10-17 14:49:02 +0100 | [diff] [blame^] | 531 | CopyFileToCacheTask copyTask = new CopyFileToCacheTask(); |
| 532 | copyTask.execute(file.getPath(), Environment.getDownloadCacheDirectory() + "/" + VersionParserHelper.getNameFromVersion(mSelectedVersion)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 533 | } |
| 534 | else |
| 535 | { |
| 536 | Log.d(TAG, "No space on cache. Defaulting to Sdcard"); |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 537 | Toast.makeText(mainActivity, getResources().getString(R.string.no_space_available_cache_message), Toast.LENGTH_LONG).show(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 538 | } |
| 539 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 540 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 541 | public boolean canCopyToCache() |
| 542 | { |
| 543 | Resources resources = getResources(); |
| 544 | double cacheSize = Utils.getPartitionSizeInMBytes(Environment.getDownloadCacheDirectory()); |
| 545 | return cacheSize > resources.getInteger(R.integer.FP1CachePartitionSizeMb) && cacheSize > resources.getInteger(R.integer.minimalCachePartitionSizeMb); |
| 546 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 547 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 548 | private void clearCache() |
| 549 | { |
| 550 | File f = Environment.getDownloadCacheDirectory(); |
| 551 | File files[] = f.listFiles(); |
| 552 | if (files != null) |
| 553 | { |
| 554 | Log.d(TAG, "Size: " + files.length); |
| 555 | for (int i = 0; i < files.length; i++) |
| 556 | { |
| 557 | String filename = files[i].getName(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 558 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 559 | if (filename.endsWith(".zip")) |
| 560 | { |
| 561 | files[i].delete(); |
| 562 | Log.d(TAG, "Deleted file " + filename); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 567 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 568 | // ************************************************************************************ |
| 569 | // Update Removal |
| 570 | // ************************************************************************************ |
| 571 | private void removeUpdateFilesFromData() |
| 572 | { |
| 573 | try |
| 574 | { |
| 575 | Shell.runRootCommand(new CommandCapture(0, getResources().getString(R.string.removePlayStoreCommand), getResources().getString( |
| 576 | R.string.removeGooglePlusCommand), getResources().getString(R.string.removeSoundSearchCommand), getResources().getString( |
| 577 | R.string.removeGmailCommand), getResources().getString(R.string.removePlayServicesCommand), getResources().getString( |
| 578 | R.string.removeQuicksearchCommand), getResources().getString(R.string.removeTalkbackCommand), getResources().getString( |
| 579 | R.string.removeText2SpeechCommand))); |
| 580 | } catch (IOException e) |
| 581 | { |
| 582 | e.printStackTrace(); |
| 583 | } catch (TimeoutException e) |
| 584 | { |
| 585 | e.printStackTrace(); |
| 586 | } catch (RootDeniedException e) |
| 587 | { |
| 588 | e.printStackTrace(); |
| 589 | } |
| 590 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 591 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 592 | public void removeLastUpdateDownload() |
| 593 | { |
| 594 | long latestUpdateDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference(); |
| 595 | if (latestUpdateDownloadId != 0) |
| 596 | { |
| 597 | // residue download ID |
| 598 | mDownloadManager.remove(latestUpdateDownloadId); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 599 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 600 | mainActivity.resetLastUpdateDownloadId(); |
| 601 | } |
| 602 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 603 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 604 | private class CopyFileToCacheTask extends AsyncTask<String, Integer, Integer> |
| 605 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 606 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 607 | ProgressDialog mProgress; |
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 | @Override |
| 610 | protected Integer doInBackground(String... params) |
| 611 | { |
| 612 | // check the correct number of |
| 613 | if (params.length != 2) |
| 614 | { |
| 615 | return -1; |
| 616 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 617 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 618 | String originalFilePath = params[0]; |
| 619 | String destinyFilePath = params[1]; |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 620 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 621 | if (RootTools.isAccessGiven()) |
| 622 | { |
| 623 | clearCache(); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 624 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 625 | File otaFilePath = new File(originalFilePath); |
| 626 | File otaFileCache = new File(destinyFilePath); |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 627 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 628 | if (!otaFileCache.exists()) |
| 629 | { |
| 630 | RootTools.copyFile(otaFilePath.getPath(), otaFileCache.getPath(), false, false); |
| 631 | } |
| 632 | } |
Jose Pascoal | 733b84e | 2014-10-17 14:49:02 +0100 | [diff] [blame^] | 633 | else |
| 634 | { |
| 635 | abortUpdateProccess(); |
| 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 | return 1; |
| 639 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 640 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 641 | protected void onProgressUpdate(Integer... progress) |
| 642 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 643 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 644 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 645 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 646 | protected void onPreExecute() |
| 647 | { |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 648 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 649 | if (mProgress == null) |
| 650 | { |
| 651 | String title = ""; |
Pedro Arelo | 773bd82 | 2014-10-10 11:57:34 +0100 | [diff] [blame] | 652 | String message = mainActivity.getResources().getString(R.string.please_be_patient); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 653 | mProgress = ProgressDialog.show(mainActivity, title, message, true, false); |
| 654 | } |
| 655 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 656 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 657 | protected void onPostExecute(Integer result) |
| 658 | { |
| 659 | // disable the spinner |
| 660 | if (mProgress != null) |
| 661 | { |
| 662 | mProgress.dismiss(); |
| 663 | mProgress = null; |
| 664 | } |
| 665 | } |
| 666 | } |
Jose Pascoal | 54b3ae6 | 2014-10-07 20:29:58 +0100 | [diff] [blame] | 667 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 668 | private String getVersionDownloadPath(Version version) |
| 669 | { |
| 670 | Resources resources = mainActivity.getResources(); |
| 671 | return Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + VersionParserHelper.getNameFromVersion(version); |
| 672 | } |
Jose Pascoal | b397dc6 | 2014-10-13 19:26:58 +0100 | [diff] [blame] | 673 | |
| 674 | public void abortUpdateProccess() |
| 675 | { |
| 676 | removeLastUpdateDownload(); |
Jose Pascoal | 3bc3a84 | 2014-10-16 19:03:21 +0100 | [diff] [blame] | 677 | |
| 678 | if (mainActivity.getFragmentCount() == 1 && mainActivity.getBackStackSize() == 0) |
| 679 | { |
| 680 | mainActivity.removeLastFragment(false); |
| 681 | mainActivity.changeState(UpdaterState.NORMAL); |
| 682 | } |
| 683 | else |
| 684 | { |
| 685 | mainActivity.removeLastFragment(false); |
| 686 | mainActivity.updateStatePreference(UpdaterState.NORMAL); |
| 687 | } |
Jose Pascoal | b397dc6 | 2014-10-13 19:26:58 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Jose Pascoal | b690af1 | 2014-10-06 18:29:26 +0100 | [diff] [blame] | 690 | } |