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