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