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