blob: 2b15c735b00d6ab3d761784a03adce3e93bf79a8 [file] [log] [blame]
Jose Pascoalb690af12014-10-06 18:29:26 +01001package com.fairphone.updater.fragments;
2
Jose Pascoal54b3ae62014-10-07 20:29:58 +01003import java.io.File;
4import java.io.IOException;
5import java.util.concurrent.TimeoutException;
6
Jose Pascoal75392162014-10-15 18:29:01 +01007import android.app.AlertDialog;
Jose Pascoalb690af12014-10-06 18:29:26 +01008import android.app.DownloadManager;
Jose Pascoal54b3ae62014-10-07 20:29:58 +01009import android.app.ProgressDialog;
10import android.content.BroadcastReceiver;
11import android.content.Context;
Jose Pascoal75392162014-10-15 18:29:01 +010012import android.content.DialogInterface;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010013import android.content.Intent;
14import android.content.IntentFilter;
15import android.content.res.Resources;
16import android.content.res.Resources.NotFoundException;
Jose Pascoalb690af12014-10-06 18:29:26 +010017import android.database.Cursor;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010018import android.os.AsyncTask;
Jose Pascoalb690af12014-10-06 18:29:26 +010019import android.os.Bundle;
20import android.os.Environment;
21import android.util.Log;
22import android.view.LayoutInflater;
23import android.view.View;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010024import android.view.View.OnClickListener;
Jose Pascoalb690af12014-10-06 18:29:26 +010025import android.view.ViewGroup;
26import android.widget.Button;
27import android.widget.LinearLayout;
28import android.widget.ProgressBar;
29import android.widget.TextView;
30import android.widget.Toast;
31
Jose Pascoal7bf83a02014-10-13 18:30:18 +010032import com.fairphone.updater.FairphoneUpdater.HeaderType;
33import com.fairphone.updater.FairphoneUpdater.UpdaterState;
Jose Pascoalb690af12014-10-06 18:29:26 +010034import com.fairphone.updater.R;
Jose Pascoal7bf83a02014-10-13 18:30:18 +010035import com.fairphone.updater.data.Version;
36import com.fairphone.updater.data.VersionParserHelper;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010037import com.fairphone.updater.gappsinstaller.GappsInstallerHelper;
Jose Pascoalb690af12014-10-06 18:29:26 +010038import com.fairphone.updater.tools.Utils;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010039import com.stericson.RootTools.RootTools;
40import com.stericson.RootTools.exceptions.RootDeniedException;
41import com.stericson.RootTools.execution.CommandCapture;
42import com.stericson.RootTools.execution.Shell;
Jose Pascoalb690af12014-10-06 18:29:26 +010043
Jose Pascoal810950b2014-10-09 17:16:08 +010044public class DownloadAndRestartFragment extends BaseFragment
45{
Jose Pascoalb690af12014-10-06 18:29:26 +010046
Jose Pascoal810950b2014-10-09 17:16:08 +010047 protected static final String TAG = DownloadAndRestartFragment.class.getSimpleName();
Jose Pascoalb690af12014-10-06 18:29:26 +010048
Jose Pascoal810950b2014-10-09 17:16:08 +010049 private TextView mDownloadVersionName;
50 private LinearLayout mVersionDownloadingGroup;
51 private ProgressBar mVersionDownloadProgressBar;
52 private LinearLayout mVersionInstallGroup;
53 private Button mRestartButton;
54 private Button mCancelButton;
55 private Version mSelectedVersion;
Jose Pascoalb690af12014-10-06 18:29:26 +010056
Jose Pascoal810950b2014-10-09 17:16:08 +010057 private DownloadManager mDownloadManager;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010058
Jose Pascoal810950b2014-10-09 17:16:08 +010059 private DownloadBroadCastReceiver mDownloadBroadCastReceiver;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010060
Jose Pascoal810950b2014-10-09 17:16:08 +010061 private long mLatestUpdateDownloadId;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010062
Jose Pascoal810950b2014-10-09 17:16:08 +010063 @Override
64 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
65 {
66 // Inflate the layout for this fragment
67 mSelectedVersion = mainActivity.getSelectedVersion();
68 View view = inflateViewByImageType(inflater, container);
Jose Pascoalb690af12014-10-06 18:29:26 +010069
Jose Pascoal810950b2014-10-09 17:16:08 +010070 setupLayout(view);
Jose Pascoalb690af12014-10-06 18:29:26 +010071
Jose Pascoal810950b2014-10-09 17:16:08 +010072 return view;
73 }
Jose Pascoalb690af12014-10-06 18:29:26 +010074
Jose Pascoal810950b2014-10-09 17:16:08 +010075 private void toggleDownloadProgressAndRestart()
76 {
Jose Pascoal8ed98d62014-10-09 20:41:11 +010077 UpdaterState state = mainActivity.getCurrentUpdaterState();
78 switch (state)
Jose Pascoal810950b2014-10-09 17:16:08 +010079 {
80 case DOWNLOAD:
81 setupDownloadState();
Jose Pascoalb690af12014-10-06 18:29:26 +010082
Jose Pascoal810950b2014-10-09 17:16:08 +010083 mVersionInstallGroup.setVisibility(View.GONE);
84 mVersionDownloadingGroup.setVisibility(View.VISIBLE);
85 break;
Jose Pascoalb690af12014-10-06 18:29:26 +010086
Jose Pascoal810950b2014-10-09 17:16:08 +010087 case PREINSTALL:
88 setupPreInstallState();
Jose Pascoalb690af12014-10-06 18:29:26 +010089
Jose Pascoal810950b2014-10-09 17:16:08 +010090 mVersionDownloadingGroup.setVisibility(View.GONE);
91 mVersionInstallGroup.setVisibility(View.VISIBLE);
Jose Pascoal8ed98d62014-10-09 20:41:11 +010092
93 mRestartButton.setOnClickListener(new OnClickListener()
94 {
95
96 @Override
97 public void onClick(View v)
98 {
Jose Pascoal75392162014-10-15 18:29:01 +010099 showEraseAllDataWarning();
Jose Pascoal8ed98d62014-10-09 20:41:11 +0100100 }
101 });
102
Jose Pascoal810950b2014-10-09 17:16:08 +0100103 break;
Jose Pascoalb690af12014-10-06 18:29:26 +0100104
Jose Pascoal810950b2014-10-09 17:16:08 +0100105 default:
Jose Pascoal8ed98d62014-10-09 20:41:11 +0100106 Log.w(TAG, "Wrong State: " + state + "\nOnly DOWNLOAD and PREINSTALL are supported");
Jose Pascoal3bc3a842014-10-16 19:03:21 +0100107 mainActivity.removeLastFragment(true);
Jose Pascoal8ed98d62014-10-09 20:41:11 +0100108 return;
109
Jose Pascoal810950b2014-10-09 17:16:08 +0100110 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100111
Jose Pascoal810950b2014-10-09 17:16:08 +0100112 mCancelButton.setOnClickListener(new OnClickListener()
113 {
Jose Pascoalb690af12014-10-06 18:29:26 +0100114
Jose Pascoal810950b2014-10-09 17:16:08 +0100115 @Override
116 public void onClick(View v)
117 {
Jose Pascoalb397dc62014-10-13 19:26:58 +0100118 abortUpdateProccess();
Jose Pascoal810950b2014-10-09 17:16:08 +0100119 }
120 });
121 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100122
Jose Pascoal75392162014-10-15 18:29:01 +0100123 private void showEraseAllDataWarning()
124 {
125 if (mSelectedVersion.hasEraseAllPartitionWarning())
126 {
127 new AlertDialog.Builder(mainActivity).setTitle(android.R.string.dialog_alert_title).setMessage(R.string.erase_all_partitions_warning_message)
128 .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
129 {
130
131 @Override
132 public void onClick(DialogInterface dialog, int which)
133 {
134 startPreInstall();
135 }
136 }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
137 {
138 public void onClick(DialogInterface dialog, int which)
139 {
140 // do nothing
141 }
142 }).show();
143 }
144 else
145 {
146 startPreInstall();
147 }
148 }
149
Jose Pascoal810950b2014-10-09 17:16:08 +0100150 private void updateHeader()
151 {
152 if (mSelectedVersion != null)
153 {
154 if (Version.IMAGE_TYPE_FAIRPHONE.equalsIgnoreCase(mSelectedVersion.getImageType()))
155 {
156 mainActivity.updateHeader(HeaderType.MAIN_FAIRPHONE, "");
157 }
158 else if (Version.IMAGE_TYPE_AOSP.equalsIgnoreCase(mSelectedVersion.getImageType()))
159 {
160 mainActivity.updateHeader(HeaderType.MAIN_ANDROID, "");
161 }
162 }
163 else
164 {
165 mainActivity.updateHeader(HeaderType.MAIN_FAIRPHONE, "");
166 }
167 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100168
Jose Pascoal810950b2014-10-09 17:16:08 +0100169 private void startDownloadProgressUpdateThread()
170 {
171 new Thread(new Runnable()
172 {
Jose Pascoalb690af12014-10-06 18:29:26 +0100173
Jose Pascoal810950b2014-10-09 17:16:08 +0100174 @Override
175 public void run()
176 {
Jose Pascoalb690af12014-10-06 18:29:26 +0100177
Jose Pascoal810950b2014-10-09 17:16:08 +0100178 boolean downloading = true;
Jose Pascoalb690af12014-10-06 18:29:26 +0100179
Jose Pascoal810950b2014-10-09 17:16:08 +0100180 long latestUpdateDownloadId = mainActivity.getLatestDownloadId();
181 while (latestUpdateDownloadId != 0 && downloading)
182 {
Jose Pascoalb690af12014-10-06 18:29:26 +0100183
Jose Pascoal810950b2014-10-09 17:16:08 +0100184 DownloadManager.Query q = new DownloadManager.Query();
185 q.setFilterById(latestUpdateDownloadId);
Jose Pascoalb690af12014-10-06 18:29:26 +0100186
Jose Pascoal810950b2014-10-09 17:16:08 +0100187 Cursor cursor = mDownloadManager.query(q);
188 if (cursor != null)
189 {
190 cursor.moveToFirst();
191 try
192 {
193 int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
194 int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
Jose Pascoalb690af12014-10-06 18:29:26 +0100195
Jose Pascoal810950b2014-10-09 17:16:08 +0100196 if ((bytes_total + 10000) > Utils.getAvailablePartitionSizeInBytes(Environment.getExternalStorageDirectory()))
197 {
198 downloading = false;
Pedro Arelo773bd822014-10-10 11:57:34 +0100199 Toast.makeText(mainActivity, getResources().getString(R.string.no_space_available_sd_card_message), Toast.LENGTH_LONG).show();
Jose Pascoal3bc3a842014-10-16 19:03:21 +0100200 abortUpdateProccess();
Jose Pascoal810950b2014-10-09 17:16:08 +0100201 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100202
Jose Pascoal810950b2014-10-09 17:16:08 +0100203 if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL)
204 {
205 downloading = false;
Jose Pascoalb690af12014-10-06 18:29:26 +0100206
Jose Pascoal810950b2014-10-09 17:16:08 +0100207 bytes_downloaded = 0;
208 bytes_total = 0;
209 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100210
Jose Pascoal810950b2014-10-09 17:16:08 +0100211 mVersionDownloadProgressBar.setProgress(bytes_downloaded);
212 mVersionDownloadProgressBar.setMax(bytes_total);
213 } catch (Exception e)
214 {
215 downloading = false;
216 Log.e(TAG, "Error updating download progress: " + e.getMessage());
217 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100218
Jose Pascoal810950b2014-10-09 17:16:08 +0100219 cursor.close();
220 try
221 {
222 // TODO WHYYYYYY???????
223 Thread.sleep(3000);
224 } catch (InterruptedException e)
225 {
226 e.printStackTrace();
227 }
228 }
229 }
230 }
231 }).start();
232 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100233
Jose Pascoal810950b2014-10-09 17:16:08 +0100234 private View inflateViewByImageType(LayoutInflater inflater, ViewGroup container)
235 {
236 View view = inflater.inflate(R.layout.fragment_download_fairphone, container, false);
237 if (mSelectedVersion != null)
238 {
239 if (Version.IMAGE_TYPE_AOSP.equalsIgnoreCase(mSelectedVersion.getImageType()))
240 {
241 view = inflater.inflate(R.layout.fragment_download_android, container, false);
242 }
243 else if (Version.IMAGE_TYPE_FAIRPHONE.equalsIgnoreCase(mSelectedVersion.getImageType()))
244 {
245 view = inflater.inflate(R.layout.fragment_download_fairphone, container, false);
246 }
247 }
248 return view;
249 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100250
Jose Pascoal810950b2014-10-09 17:16:08 +0100251 private void setupLayout(View view)
252 {
253 mDownloadVersionName = (TextView) view.findViewById(R.id.download_version_name_text);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100254
Jose Pascoal810950b2014-10-09 17:16:08 +0100255 // download in progress group
256 mVersionDownloadingGroup = (LinearLayout) view.findViewById(R.id.version_downloading_group);
257 mVersionDownloadProgressBar = (ProgressBar) view.findViewById(R.id.version_download_progress_bar);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100258
Jose Pascoal810950b2014-10-09 17:16:08 +0100259 // restart group
260 mVersionInstallGroup = (LinearLayout) view.findViewById(R.id.version_install_group);
261 mRestartButton = (Button) view.findViewById(R.id.restart_button);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100262
Jose Pascoal810950b2014-10-09 17:16:08 +0100263 mCancelButton = (Button) view.findViewById(R.id.cancel_button);
264 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100265
Jose Pascoal810950b2014-10-09 17:16:08 +0100266 @Override
267 public void onResume()
268 {
269 super.onResume();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100270
Jose Pascoal810950b2014-10-09 17:16:08 +0100271 setupInstallationReceivers();
272 registerDownloadBroadCastReceiver();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100273
Jose Pascoal8ed98d62014-10-09 20:41:11 +0100274 updateHeader();
275 mDownloadVersionName.setText(mainActivity.getVersionName(mSelectedVersion));
Jose Pascoal810950b2014-10-09 17:16:08 +0100276 toggleDownloadProgressAndRestart();
277 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100278
Jose Pascoal810950b2014-10-09 17:16:08 +0100279 @Override
280 public void onPause()
281 {
282 super.onPause();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100283
Jose Pascoal810950b2014-10-09 17:16:08 +0100284 unregisterBroadCastReceiver();
285 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100286
Jose Pascoal810950b2014-10-09 17:16:08 +0100287 private void setupInstallationReceivers()
288 {
289 mDownloadManager = (DownloadManager) mainActivity.getSystemService(Context.DOWNLOAD_SERVICE);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100290
Jose Pascoal810950b2014-10-09 17:16:08 +0100291 mDownloadBroadCastReceiver = new DownloadBroadCastReceiver();
292 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100293
Jose Pascoal810950b2014-10-09 17:16:08 +0100294 private void registerDownloadBroadCastReceiver()
295 {
296 mainActivity.registerReceiver(mDownloadBroadCastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
297 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100298
Jose Pascoal810950b2014-10-09 17:16:08 +0100299 private void unregisterBroadCastReceiver()
300 {
301 mainActivity.unregisterReceiver(mDownloadBroadCastReceiver);
302 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100303
Jose Pascoal810950b2014-10-09 17:16:08 +0100304 private class DownloadBroadCastReceiver extends BroadcastReceiver
305 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100306
Jose Pascoal810950b2014-10-09 17:16:08 +0100307 @Override
308 public void onReceive(Context context, Intent intent)
309 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100310
Jose Pascoal810950b2014-10-09 17:16:08 +0100311 mainActivity.getLatestUpdateDownloadIdFromSharedPreference();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100312
Jose Pascoal810950b2014-10-09 17:16:08 +0100313 updateDownloadFile();
314 }
315 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100316
Jose Pascoal810950b2014-10-09 17:16:08 +0100317 private void updateDownloadFile()
318 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100319
Jose Pascoal810950b2014-10-09 17:16:08 +0100320 long downloadId = mainActivity.getLatestDownloadId();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100321
Jose Pascoal810950b2014-10-09 17:16:08 +0100322 if (downloadId != 0)
323 {
324 DownloadManager.Query query = new DownloadManager.Query();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100325
Jose Pascoal810950b2014-10-09 17:16:08 +0100326 query.setFilterById(downloadId);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100327
Jose Pascoal810950b2014-10-09 17:16:08 +0100328 Cursor cursor = mDownloadManager.query(query);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100329
Jose Pascoal810950b2014-10-09 17:16:08 +0100330 if (cursor.moveToFirst())
331 {
332 int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
333 int status = cursor.getInt(columnIndex);
334
335 switch (status)
336 {
337 case DownloadManager.STATUS_SUCCESSFUL:
338 mainActivity.updateStatePreference(UpdaterState.PREINSTALL);
339 toggleDownloadProgressAndRestart();
340 break;
341 case DownloadManager.STATUS_RUNNING:
342 startDownloadProgressUpdateThread();
343 break;
344 case DownloadManager.STATUS_FAILED:
345 Resources resources = getResources();
346 if (mSelectedVersion != null)
347 {
348 String downloadTitle = mSelectedVersion.getName() + " " + mSelectedVersion.getImageTypeDescription(resources);
Pedro Arelo773bd822014-10-10 11:57:34 +0100349 Toast.makeText(mainActivity, resources.getString(R.string.error_downloading) + " " + downloadTitle, Toast.LENGTH_LONG).show();
Jose Pascoal810950b2014-10-09 17:16:08 +0100350 }
351 else
352 {
Pedro Arelo773bd822014-10-10 11:57:34 +0100353 Toast.makeText(mainActivity, resources.getString(R.string.error_downloading), Toast.LENGTH_LONG).show();
Jose Pascoal810950b2014-10-09 17:16:08 +0100354 }
Jose Pascoal3bc3a842014-10-16 19:03:21 +0100355 abortUpdateProccess();
Jose Pascoal810950b2014-10-09 17:16:08 +0100356 break;
357 }
358 }
Jose Pascoal3bc3a842014-10-16 19:03:21 +0100359 else
360 {
361 abortUpdateProccess();
362 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100363
364 cursor.close();
365 }
366 }
367
368 // ************************************************************************************
369 // PRE INSTALL
370 // ************************************************************************************
371
372 private void setupPreInstallState()
373 {
374
375 Resources resources = mainActivity.getResources();
376 // the latest version data must exist
377 if (mSelectedVersion != null)
378 {
379
380 // check the md5 of the file
381 File file = new File(getVersionDownloadPath(mSelectedVersion));
382
383 if (file.exists())
384 {
385 if (Utils.checkMD5(mSelectedVersion.getMd5Sum(), file))
386 {
387 copyUpdateToCache(file);
388 return;
389 }
390 else
391 {
Pedro Arelo773bd822014-10-10 11:57:34 +0100392 Toast.makeText(mainActivity, resources.getString(R.string.invalid_md5_download_message), Toast.LENGTH_LONG).show();
Jose Pascoal810950b2014-10-09 17:16:08 +0100393 removeLastUpdateDownload();
394 }
395 }
396 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100397
Jose Pascoal810950b2014-10-09 17:16:08 +0100398 // remove the updater directory
399 File fileDir = new File(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder));
400 fileDir.delete();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100401
Jose Pascoal810950b2014-10-09 17:16:08 +0100402 // else if the perfect case does not happen, reset the download
Jose Pascoal3bc3a842014-10-16 19:03:21 +0100403 abortUpdateProccess();
Jose Pascoal810950b2014-10-09 17:16:08 +0100404 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100405
Jose Pascoal810950b2014-10-09 17:16:08 +0100406 // ************************************************************************************
407 // DOWNLOAD UPDATE
408 // ************************************************************************************
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100409
Jose Pascoal810950b2014-10-09 17:16:08 +0100410 public void setupDownloadState()
411 {
412 // setup the download state views
413 if (mSelectedVersion == null)
414 {
415 Resources resources = getResources();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100416
Jose Pascoal810950b2014-10-09 17:16:08 +0100417 // we don't have the lastest.xml so get back to initial state
418 File updateDir = new File(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder));
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100419
Jose Pascoal810950b2014-10-09 17:16:08 +0100420 updateDir.delete();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100421
Jose Pascoal3bc3a842014-10-16 19:03:21 +0100422 abortUpdateProccess();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100423
Jose Pascoal810950b2014-10-09 17:16:08 +0100424 return;
425 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100426
Jose Pascoal810950b2014-10-09 17:16:08 +0100427 // if there is a download ID on the shared preferences
428 if (mLatestUpdateDownloadId == 0)
429 {
430 mLatestUpdateDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100431
Jose Pascoal810950b2014-10-09 17:16:08 +0100432 // invalid download Id
433 if (mLatestUpdateDownloadId == 0)
434 {
Jose Pascoal3bc3a842014-10-16 19:03:21 +0100435 abortUpdateProccess();
Jose Pascoal810950b2014-10-09 17:16:08 +0100436 return;
437 }
438 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100439
Jose Pascoal810950b2014-10-09 17:16:08 +0100440 updateDownloadFile();
441 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100442
Jose Pascoal810950b2014-10-09 17:16:08 +0100443 private void startPreInstall()
444 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100445
Jose Pascoal810950b2014-10-09 17:16:08 +0100446 if (RootTools.isAccessGiven())
447 {
448 // set the command for the recovery
449 Resources resources = getResources();
450 // Process p;
451 try
452 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100453
Jose Pascoal810950b2014-10-09 17:16:08 +0100454 Shell.runRootCommand(new CommandCapture(0, "rm -f /cache/recovery/command"));
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100455
Jose Pascoal810950b2014-10-09 17:16:08 +0100456 Shell.runRootCommand(new CommandCapture(0, "rm -f /cache/recovery/extendedcommand"));
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100457
Jose Pascoal810950b2014-10-09 17:16:08 +0100458 Shell.runRootCommand(new CommandCapture(0, "echo '--wipe_cache' >> /cache/recovery/command"));
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100459
Jose Pascoal810950b2014-10-09 17:16:08 +0100460 if (canCopyToCache())
461 {
462 Shell.runRootCommand(new CommandCapture(0, "echo '--update_package=/" + resources.getString(R.string.recoveryCachePath) + "/"
463 + VersionParserHelper.getNameFromVersion(mSelectedVersion) + "' >> /cache/recovery/command"));
464 }
465 else
466 {
467 Shell.runRootCommand(new CommandCapture(0, "echo '--update_package=/" + resources.getString(R.string.recoverySdCardPath)
468 + resources.getString(R.string.updaterFolder) + VersionParserHelper.getNameFromVersion(mSelectedVersion)
469 + "' >> /cache/recovery/command"));
470 }
471 } catch (IOException e)
472 {
473 // TODO Auto-generated catch block
474 e.printStackTrace();
475 } catch (NotFoundException e)
476 {
477 // TODO Auto-generated catch block
478 e.printStackTrace();
479 } catch (TimeoutException e)
480 {
481 // TODO Auto-generated catch block
482 e.printStackTrace();
483 } catch (RootDeniedException e)
484 {
485 // TODO Auto-generated catch block
486 e.printStackTrace();
487 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100488
Jose Pascoal810950b2014-10-09 17:16:08 +0100489 // send broadcast intent
490 Intent broadcastIntent = new Intent();
491 broadcastIntent.setAction(GappsInstallerHelper.GAPPS_REINSTALATION);
492 mainActivity.sendBroadcast(broadcastIntent);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100493
Jose Pascoal810950b2014-10-09 17:16:08 +0100494 if (canCopyToCache())
495 {
496 removeLastUpdateDownload();
497 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100498
Jose Pascoal810950b2014-10-09 17:16:08 +0100499 // remove the update files from data
500 removeUpdateFilesFromData();
Jose Pascoal733b84e2014-10-17 14:49:02 +0100501
Jose Pascoal810950b2014-10-09 17:16:08 +0100502 // reboot the device into recovery
Jose Pascoal810950b2014-10-09 17:16:08 +0100503 try
504 {
505 mainActivity.updateStatePreference(UpdaterState.NORMAL);
506 Shell.runRootCommand(new CommandCapture(0, "reboot recovery"));
507 } catch (IOException e)
508 {
509 // TODO Auto-generated catch block
510 e.printStackTrace();
511 } catch (TimeoutException e)
512 {
513 // TODO Auto-generated catch block
514 e.printStackTrace();
515 } catch (RootDeniedException e)
516 {
517 // TODO Auto-generated catch block
518 e.printStackTrace();
519 }
520 }
521 else
522 {
Jose Pascoal3bc3a842014-10-16 19:03:21 +0100523 abortUpdateProccess();
Jose Pascoal810950b2014-10-09 17:16:08 +0100524 }
525 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100526
Jose Pascoal810950b2014-10-09 17:16:08 +0100527 private void copyUpdateToCache(File file)
528 {
529 if (canCopyToCache())
530 {
Jose Pascoal733b84e2014-10-17 14:49:02 +0100531 CopyFileToCacheTask copyTask = new CopyFileToCacheTask();
532 copyTask.execute(file.getPath(), Environment.getDownloadCacheDirectory() + "/" + VersionParserHelper.getNameFromVersion(mSelectedVersion));
Jose Pascoal810950b2014-10-09 17:16:08 +0100533 }
534 else
535 {
536 Log.d(TAG, "No space on cache. Defaulting to Sdcard");
Pedro Arelo773bd822014-10-10 11:57:34 +0100537 Toast.makeText(mainActivity, getResources().getString(R.string.no_space_available_cache_message), Toast.LENGTH_LONG).show();
Jose Pascoal810950b2014-10-09 17:16:08 +0100538 }
539 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100540
Jose Pascoal810950b2014-10-09 17:16:08 +0100541 public boolean canCopyToCache()
542 {
543 Resources resources = getResources();
544 double cacheSize = Utils.getPartitionSizeInMBytes(Environment.getDownloadCacheDirectory());
545 return cacheSize > resources.getInteger(R.integer.FP1CachePartitionSizeMb) && cacheSize > resources.getInteger(R.integer.minimalCachePartitionSizeMb);
546 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100547
Jose Pascoal810950b2014-10-09 17:16:08 +0100548 private void clearCache()
549 {
550 File f = Environment.getDownloadCacheDirectory();
551 File files[] = f.listFiles();
552 if (files != null)
553 {
554 Log.d(TAG, "Size: " + files.length);
555 for (int i = 0; i < files.length; i++)
556 {
557 String filename = files[i].getName();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100558
Jose Pascoal810950b2014-10-09 17:16:08 +0100559 if (filename.endsWith(".zip"))
560 {
561 files[i].delete();
562 Log.d(TAG, "Deleted file " + filename);
563 }
564 }
565 }
566 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100567
Jose Pascoal810950b2014-10-09 17:16:08 +0100568 // ************************************************************************************
569 // Update Removal
570 // ************************************************************************************
571 private void removeUpdateFilesFromData()
572 {
573 try
574 {
575 Shell.runRootCommand(new CommandCapture(0, getResources().getString(R.string.removePlayStoreCommand), getResources().getString(
576 R.string.removeGooglePlusCommand), getResources().getString(R.string.removeSoundSearchCommand), getResources().getString(
577 R.string.removeGmailCommand), getResources().getString(R.string.removePlayServicesCommand), getResources().getString(
578 R.string.removeQuicksearchCommand), getResources().getString(R.string.removeTalkbackCommand), getResources().getString(
579 R.string.removeText2SpeechCommand)));
580 } catch (IOException e)
581 {
582 e.printStackTrace();
583 } catch (TimeoutException e)
584 {
585 e.printStackTrace();
586 } catch (RootDeniedException e)
587 {
588 e.printStackTrace();
589 }
590 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100591
Jose Pascoal810950b2014-10-09 17:16:08 +0100592 public void removeLastUpdateDownload()
593 {
594 long latestUpdateDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference();
595 if (latestUpdateDownloadId != 0)
596 {
597 // residue download ID
598 mDownloadManager.remove(latestUpdateDownloadId);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100599
Jose Pascoal810950b2014-10-09 17:16:08 +0100600 mainActivity.resetLastUpdateDownloadId();
601 }
602 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100603
Jose Pascoal810950b2014-10-09 17:16:08 +0100604 private class CopyFileToCacheTask extends AsyncTask<String, Integer, Integer>
605 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100606
Jose Pascoal810950b2014-10-09 17:16:08 +0100607 ProgressDialog mProgress;
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100608
Jose Pascoal810950b2014-10-09 17:16:08 +0100609 @Override
610 protected Integer doInBackground(String... params)
611 {
612 // check the correct number of
613 if (params.length != 2)
614 {
615 return -1;
616 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100617
Jose Pascoal810950b2014-10-09 17:16:08 +0100618 String originalFilePath = params[0];
619 String destinyFilePath = params[1];
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100620
Jose Pascoal810950b2014-10-09 17:16:08 +0100621 if (RootTools.isAccessGiven())
622 {
623 clearCache();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100624
Jose Pascoal810950b2014-10-09 17:16:08 +0100625 File otaFilePath = new File(originalFilePath);
626 File otaFileCache = new File(destinyFilePath);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100627
Jose Pascoal810950b2014-10-09 17:16:08 +0100628 if (!otaFileCache.exists())
629 {
630 RootTools.copyFile(otaFilePath.getPath(), otaFileCache.getPath(), false, false);
631 }
632 }
Jose Pascoal733b84e2014-10-17 14:49:02 +0100633 else
634 {
635 abortUpdateProccess();
636 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100637
Jose Pascoal810950b2014-10-09 17:16:08 +0100638 return 1;
639 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100640
Jose Pascoal810950b2014-10-09 17:16:08 +0100641 protected void onProgressUpdate(Integer... progress)
642 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100643
Jose Pascoal810950b2014-10-09 17:16:08 +0100644 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100645
Jose Pascoal810950b2014-10-09 17:16:08 +0100646 protected void onPreExecute()
647 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100648
Jose Pascoal810950b2014-10-09 17:16:08 +0100649 if (mProgress == null)
650 {
651 String title = "";
Pedro Arelo773bd822014-10-10 11:57:34 +0100652 String message = mainActivity.getResources().getString(R.string.please_be_patient);
Jose Pascoal810950b2014-10-09 17:16:08 +0100653 mProgress = ProgressDialog.show(mainActivity, title, message, true, false);
654 }
655 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100656
Jose Pascoal810950b2014-10-09 17:16:08 +0100657 protected void onPostExecute(Integer result)
658 {
659 // disable the spinner
660 if (mProgress != null)
661 {
662 mProgress.dismiss();
663 mProgress = null;
664 }
665 }
666 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100667
Jose Pascoal810950b2014-10-09 17:16:08 +0100668 private String getVersionDownloadPath(Version version)
669 {
670 Resources resources = mainActivity.getResources();
671 return Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + VersionParserHelper.getNameFromVersion(version);
672 }
Jose Pascoalb397dc62014-10-13 19:26:58 +0100673
674 public void abortUpdateProccess()
675 {
676 removeLastUpdateDownload();
Jose Pascoal3bc3a842014-10-16 19:03:21 +0100677
678 if (mainActivity.getFragmentCount() == 1 && mainActivity.getBackStackSize() == 0)
679 {
680 mainActivity.removeLastFragment(false);
681 mainActivity.changeState(UpdaterState.NORMAL);
682 }
683 else
684 {
685 mainActivity.removeLastFragment(false);
686 mainActivity.updateStatePreference(UpdaterState.NORMAL);
687 }
Jose Pascoalb397dc62014-10-13 19:26:58 +0100688 }
689
Jose Pascoalb690af12014-10-06 18:29:26 +0100690}