blob: a2cf2ff1781f336e9618b994b413484bd46d0aca [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 Pascoalb690af12014-10-06 18:29:26 +01007import android.app.DownloadManager;
Jose Pascoal54b3ae62014-10-07 20:29:58 +01008import android.app.ProgressDialog;
9import android.content.BroadcastReceiver;
10import android.content.Context;
11import android.content.Intent;
12import android.content.IntentFilter;
13import android.content.res.Resources;
14import android.content.res.Resources.NotFoundException;
Jose Pascoalb690af12014-10-06 18:29:26 +010015import android.database.Cursor;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010016import android.os.AsyncTask;
Jose Pascoalb690af12014-10-06 18:29:26 +010017import android.os.Bundle;
18import android.os.Environment;
19import android.util.Log;
20import android.view.LayoutInflater;
21import android.view.View;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010022import android.view.View.OnClickListener;
Jose Pascoalb690af12014-10-06 18:29:26 +010023import android.view.ViewGroup;
24import android.widget.Button;
25import android.widget.LinearLayout;
26import android.widget.ProgressBar;
27import android.widget.TextView;
28import android.widget.Toast;
29
Jose Pascoal7bf83a02014-10-13 18:30:18 +010030import com.fairphone.updater.FairphoneUpdater.HeaderType;
31import com.fairphone.updater.FairphoneUpdater.UpdaterState;
Jose Pascoalb690af12014-10-06 18:29:26 +010032import com.fairphone.updater.R;
Jose Pascoal7bf83a02014-10-13 18:30:18 +010033import com.fairphone.updater.data.Version;
34import com.fairphone.updater.data.VersionParserHelper;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010035import com.fairphone.updater.gappsinstaller.GappsInstallerHelper;
Jose Pascoalb690af12014-10-06 18:29:26 +010036import com.fairphone.updater.tools.Utils;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010037import com.stericson.RootTools.RootTools;
38import com.stericson.RootTools.exceptions.RootDeniedException;
39import com.stericson.RootTools.execution.CommandCapture;
40import com.stericson.RootTools.execution.Shell;
Jose Pascoalb690af12014-10-06 18:29:26 +010041
Jose Pascoal810950b2014-10-09 17:16:08 +010042public class DownloadAndRestartFragment extends BaseFragment
43{
Jose Pascoalb690af12014-10-06 18:29:26 +010044
Jose Pascoal810950b2014-10-09 17:16:08 +010045 protected static final String TAG = DownloadAndRestartFragment.class.getSimpleName();
Jose Pascoalb690af12014-10-06 18:29:26 +010046
Jose Pascoal810950b2014-10-09 17:16:08 +010047 private TextView mDownloadVersionName;
48 private LinearLayout mVersionDownloadingGroup;
49 private ProgressBar mVersionDownloadProgressBar;
50 private LinearLayout mVersionInstallGroup;
51 private Button mRestartButton;
52 private Button mCancelButton;
53 private Version mSelectedVersion;
Jose Pascoalb690af12014-10-06 18:29:26 +010054
Jose Pascoal810950b2014-10-09 17:16:08 +010055 private DownloadManager mDownloadManager;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010056
Jose Pascoal810950b2014-10-09 17:16:08 +010057 private DownloadBroadCastReceiver mDownloadBroadCastReceiver;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010058
Jose Pascoal810950b2014-10-09 17:16:08 +010059 private long mLatestUpdateDownloadId;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010060
Jose Pascoal810950b2014-10-09 17:16:08 +010061 @Override
62 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
63 {
64 // Inflate the layout for this fragment
65 mSelectedVersion = mainActivity.getSelectedVersion();
66 View view = inflateViewByImageType(inflater, container);
Jose Pascoalb690af12014-10-06 18:29:26 +010067
Jose Pascoal810950b2014-10-09 17:16:08 +010068 setupLayout(view);
Jose Pascoalb690af12014-10-06 18:29:26 +010069
Jose Pascoal810950b2014-10-09 17:16:08 +010070 return view;
71 }
Jose Pascoalb690af12014-10-06 18:29:26 +010072
Jose Pascoal810950b2014-10-09 17:16:08 +010073 private void toggleDownloadProgressAndRestart()
74 {
Jose Pascoal8ed98d62014-10-09 20:41:11 +010075 UpdaterState state = mainActivity.getCurrentUpdaterState();
76 switch (state)
Jose Pascoal810950b2014-10-09 17:16:08 +010077 {
78 case DOWNLOAD:
79 setupDownloadState();
Jose Pascoalb690af12014-10-06 18:29:26 +010080
Jose Pascoal810950b2014-10-09 17:16:08 +010081 mVersionInstallGroup.setVisibility(View.GONE);
82 mVersionDownloadingGroup.setVisibility(View.VISIBLE);
83 break;
Jose Pascoalb690af12014-10-06 18:29:26 +010084
Jose Pascoal810950b2014-10-09 17:16:08 +010085 case PREINSTALL:
86 setupPreInstallState();
Jose Pascoalb690af12014-10-06 18:29:26 +010087
Jose Pascoal810950b2014-10-09 17:16:08 +010088 mVersionDownloadingGroup.setVisibility(View.GONE);
89 mVersionInstallGroup.setVisibility(View.VISIBLE);
Jose Pascoal8ed98d62014-10-09 20:41:11 +010090
91 mRestartButton.setOnClickListener(new OnClickListener()
92 {
93
94 @Override
95 public void onClick(View v)
96 {
97 startPreInstall();
98 }
99 });
100
Jose Pascoal810950b2014-10-09 17:16:08 +0100101 break;
Jose Pascoalb690af12014-10-06 18:29:26 +0100102
Jose Pascoal810950b2014-10-09 17:16:08 +0100103 default:
Jose Pascoal8ed98d62014-10-09 20:41:11 +0100104 Log.w(TAG, "Wrong State: " + state + "\nOnly DOWNLOAD and PREINSTALL are supported");
105 mainActivity.removeLastFragment();
106 return;
107
Jose Pascoal810950b2014-10-09 17:16:08 +0100108 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100109
Jose Pascoal810950b2014-10-09 17:16:08 +0100110 mCancelButton.setOnClickListener(new OnClickListener()
111 {
Jose Pascoalb690af12014-10-06 18:29:26 +0100112
Jose Pascoal810950b2014-10-09 17:16:08 +0100113 @Override
114 public void onClick(View v)
115 {
Jose Pascoalb397dc62014-10-13 19:26:58 +0100116 abortUpdateProccess();
Jose Pascoal810950b2014-10-09 17:16:08 +0100117 }
118 });
119 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100120
Jose Pascoal810950b2014-10-09 17:16:08 +0100121 private void updateHeader()
122 {
123 if (mSelectedVersion != null)
124 {
125 if (Version.IMAGE_TYPE_FAIRPHONE.equalsIgnoreCase(mSelectedVersion.getImageType()))
126 {
127 mainActivity.updateHeader(HeaderType.MAIN_FAIRPHONE, "");
128 }
129 else if (Version.IMAGE_TYPE_AOSP.equalsIgnoreCase(mSelectedVersion.getImageType()))
130 {
131 mainActivity.updateHeader(HeaderType.MAIN_ANDROID, "");
132 }
133 }
134 else
135 {
136 mainActivity.updateHeader(HeaderType.MAIN_FAIRPHONE, "");
137 }
138 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100139
Jose Pascoal810950b2014-10-09 17:16:08 +0100140 private void startDownloadProgressUpdateThread()
141 {
142 new Thread(new Runnable()
143 {
Jose Pascoalb690af12014-10-06 18:29:26 +0100144
Jose Pascoal810950b2014-10-09 17:16:08 +0100145 @Override
146 public void run()
147 {
Jose Pascoalb690af12014-10-06 18:29:26 +0100148
Jose Pascoal810950b2014-10-09 17:16:08 +0100149 boolean downloading = true;
Jose Pascoalb690af12014-10-06 18:29:26 +0100150
Jose Pascoal810950b2014-10-09 17:16:08 +0100151 long latestUpdateDownloadId = mainActivity.getLatestDownloadId();
152 while (latestUpdateDownloadId != 0 && downloading)
153 {
Jose Pascoalb690af12014-10-06 18:29:26 +0100154
Jose Pascoal810950b2014-10-09 17:16:08 +0100155 DownloadManager.Query q = new DownloadManager.Query();
156 q.setFilterById(latestUpdateDownloadId);
Jose Pascoalb690af12014-10-06 18:29:26 +0100157
Jose Pascoal810950b2014-10-09 17:16:08 +0100158 Cursor cursor = mDownloadManager.query(q);
159 if (cursor != null)
160 {
161 cursor.moveToFirst();
162 try
163 {
164 int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
165 int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
Jose Pascoalb690af12014-10-06 18:29:26 +0100166
Jose Pascoal810950b2014-10-09 17:16:08 +0100167 if ((bytes_total + 10000) > Utils.getAvailablePartitionSizeInBytes(Environment.getExternalStorageDirectory()))
168 {
169 downloading = false;
Pedro Arelo773bd822014-10-10 11:57:34 +0100170 Toast.makeText(mainActivity, getResources().getString(R.string.no_space_available_sd_card_message), Toast.LENGTH_LONG).show();
Jose Pascoal810950b2014-10-09 17:16:08 +0100171 mainActivity.changeState(UpdaterState.NORMAL);
172 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100173
Jose Pascoal810950b2014-10-09 17:16:08 +0100174 if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL)
175 {
176 downloading = false;
Jose Pascoalb690af12014-10-06 18:29:26 +0100177
Jose Pascoal810950b2014-10-09 17:16:08 +0100178 bytes_downloaded = 0;
179 bytes_total = 0;
180 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100181
Jose Pascoal810950b2014-10-09 17:16:08 +0100182 mVersionDownloadProgressBar.setProgress(bytes_downloaded);
183 mVersionDownloadProgressBar.setMax(bytes_total);
184 } catch (Exception e)
185 {
186 downloading = false;
187 Log.e(TAG, "Error updating download progress: " + e.getMessage());
188 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100189
Jose Pascoal810950b2014-10-09 17:16:08 +0100190 cursor.close();
191 try
192 {
193 // TODO WHYYYYYY???????
194 Thread.sleep(3000);
195 } catch (InterruptedException e)
196 {
197 e.printStackTrace();
198 }
199 }
200 }
201 }
202 }).start();
203 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100204
Jose Pascoal810950b2014-10-09 17:16:08 +0100205 private View inflateViewByImageType(LayoutInflater inflater, ViewGroup container)
206 {
207 View view = inflater.inflate(R.layout.fragment_download_fairphone, container, false);
208 if (mSelectedVersion != null)
209 {
210 if (Version.IMAGE_TYPE_AOSP.equalsIgnoreCase(mSelectedVersion.getImageType()))
211 {
212 view = inflater.inflate(R.layout.fragment_download_android, container, false);
213 }
214 else if (Version.IMAGE_TYPE_FAIRPHONE.equalsIgnoreCase(mSelectedVersion.getImageType()))
215 {
216 view = inflater.inflate(R.layout.fragment_download_fairphone, container, false);
217 }
218 }
219 return view;
220 }
Jose Pascoalb690af12014-10-06 18:29:26 +0100221
Jose Pascoal810950b2014-10-09 17:16:08 +0100222 private void setupLayout(View view)
223 {
224 mDownloadVersionName = (TextView) view.findViewById(R.id.download_version_name_text);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100225
Jose Pascoal810950b2014-10-09 17:16:08 +0100226 // download in progress group
227 mVersionDownloadingGroup = (LinearLayout) view.findViewById(R.id.version_downloading_group);
228 mVersionDownloadProgressBar = (ProgressBar) view.findViewById(R.id.version_download_progress_bar);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100229
Jose Pascoal810950b2014-10-09 17:16:08 +0100230 // restart group
231 mVersionInstallGroup = (LinearLayout) view.findViewById(R.id.version_install_group);
232 mRestartButton = (Button) view.findViewById(R.id.restart_button);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100233
Jose Pascoal810950b2014-10-09 17:16:08 +0100234 mCancelButton = (Button) view.findViewById(R.id.cancel_button);
235 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100236
Jose Pascoal810950b2014-10-09 17:16:08 +0100237 @Override
238 public void onResume()
239 {
240 super.onResume();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100241
Jose Pascoal810950b2014-10-09 17:16:08 +0100242 setupInstallationReceivers();
243 registerDownloadBroadCastReceiver();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100244
Jose Pascoal8ed98d62014-10-09 20:41:11 +0100245 updateHeader();
246 mDownloadVersionName.setText(mainActivity.getVersionName(mSelectedVersion));
Jose Pascoal810950b2014-10-09 17:16:08 +0100247 toggleDownloadProgressAndRestart();
248 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100249
Jose Pascoal810950b2014-10-09 17:16:08 +0100250 @Override
251 public void onPause()
252 {
253 super.onPause();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100254
Jose Pascoal810950b2014-10-09 17:16:08 +0100255 unregisterBroadCastReceiver();
256 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100257
Jose Pascoal810950b2014-10-09 17:16:08 +0100258 private void setupInstallationReceivers()
259 {
260 mDownloadManager = (DownloadManager) mainActivity.getSystemService(Context.DOWNLOAD_SERVICE);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100261
Jose Pascoal810950b2014-10-09 17:16:08 +0100262 mDownloadBroadCastReceiver = new DownloadBroadCastReceiver();
263 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100264
Jose Pascoal810950b2014-10-09 17:16:08 +0100265 private void registerDownloadBroadCastReceiver()
266 {
267 mainActivity.registerReceiver(mDownloadBroadCastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
268 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100269
Jose Pascoal810950b2014-10-09 17:16:08 +0100270 private void unregisterBroadCastReceiver()
271 {
272 mainActivity.unregisterReceiver(mDownloadBroadCastReceiver);
273 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100274
Jose Pascoal810950b2014-10-09 17:16:08 +0100275 private class DownloadBroadCastReceiver extends BroadcastReceiver
276 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100277
Jose Pascoal810950b2014-10-09 17:16:08 +0100278 @Override
279 public void onReceive(Context context, Intent intent)
280 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100281
Jose Pascoal810950b2014-10-09 17:16:08 +0100282 mainActivity.getLatestUpdateDownloadIdFromSharedPreference();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100283
Jose Pascoal810950b2014-10-09 17:16:08 +0100284 updateDownloadFile();
285 }
286 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100287
Jose Pascoal810950b2014-10-09 17:16:08 +0100288 private void updateDownloadFile()
289 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100290
Jose Pascoal810950b2014-10-09 17:16:08 +0100291 long downloadId = mainActivity.getLatestDownloadId();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100292
Jose Pascoal810950b2014-10-09 17:16:08 +0100293 if (downloadId != 0)
294 {
295 DownloadManager.Query query = new DownloadManager.Query();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100296
Jose Pascoal810950b2014-10-09 17:16:08 +0100297 query.setFilterById(downloadId);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100298
Jose Pascoal810950b2014-10-09 17:16:08 +0100299 Cursor cursor = mDownloadManager.query(query);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100300
Jose Pascoal810950b2014-10-09 17:16:08 +0100301 if (cursor.moveToFirst())
302 {
303 int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
304 int status = cursor.getInt(columnIndex);
305
306 switch (status)
307 {
308 case DownloadManager.STATUS_SUCCESSFUL:
309 mainActivity.updateStatePreference(UpdaterState.PREINSTALL);
310 toggleDownloadProgressAndRestart();
311 break;
312 case DownloadManager.STATUS_RUNNING:
313 startDownloadProgressUpdateThread();
314 break;
315 case DownloadManager.STATUS_FAILED:
316 Resources resources = getResources();
317 if (mSelectedVersion != null)
318 {
319 String downloadTitle = mSelectedVersion.getName() + " " + mSelectedVersion.getImageTypeDescription(resources);
Pedro Arelo773bd822014-10-10 11:57:34 +0100320 Toast.makeText(mainActivity, resources.getString(R.string.error_downloading) + " " + downloadTitle, Toast.LENGTH_LONG).show();
Jose Pascoal810950b2014-10-09 17:16:08 +0100321 }
322 else
323 {
Pedro Arelo773bd822014-10-10 11:57:34 +0100324 Toast.makeText(mainActivity, resources.getString(R.string.error_downloading), Toast.LENGTH_LONG).show();
Jose Pascoal810950b2014-10-09 17:16:08 +0100325 }
326 mainActivity.changeState(UpdaterState.NORMAL);
327 break;
328 }
329 }
330
331 cursor.close();
332 }
333 }
334
335 // ************************************************************************************
336 // PRE INSTALL
337 // ************************************************************************************
338
339 private void setupPreInstallState()
340 {
341
342 Resources resources = mainActivity.getResources();
343 // the latest version data must exist
344 if (mSelectedVersion != null)
345 {
346
347 // check the md5 of the file
348 File file = new File(getVersionDownloadPath(mSelectedVersion));
349
350 if (file.exists())
351 {
352 if (Utils.checkMD5(mSelectedVersion.getMd5Sum(), file))
353 {
354 copyUpdateToCache(file);
355 return;
356 }
357 else
358 {
Pedro Arelo773bd822014-10-10 11:57:34 +0100359 Toast.makeText(mainActivity, resources.getString(R.string.invalid_md5_download_message), Toast.LENGTH_LONG).show();
Jose Pascoal810950b2014-10-09 17:16:08 +0100360 removeLastUpdateDownload();
361 }
362 }
363 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100364
Jose Pascoal810950b2014-10-09 17:16:08 +0100365 // remove the updater directory
366 File fileDir = new File(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder));
367 fileDir.delete();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100368
Jose Pascoal810950b2014-10-09 17:16:08 +0100369 // else if the perfect case does not happen, reset the download
370 mainActivity.changeState(UpdaterState.NORMAL);
371 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100372
Jose Pascoal810950b2014-10-09 17:16:08 +0100373 // ************************************************************************************
374 // DOWNLOAD UPDATE
375 // ************************************************************************************
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100376
Jose Pascoal810950b2014-10-09 17:16:08 +0100377 public void setupDownloadState()
378 {
379 // setup the download state views
380 if (mSelectedVersion == null)
381 {
382 Resources resources = getResources();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100383
Jose Pascoal810950b2014-10-09 17:16:08 +0100384 // we don't have the lastest.xml so get back to initial state
385 File updateDir = new File(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder));
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100386
Jose Pascoal810950b2014-10-09 17:16:08 +0100387 updateDir.delete();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100388
Jose Pascoal810950b2014-10-09 17:16:08 +0100389 mainActivity.changeState(UpdaterState.NORMAL);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100390
Jose Pascoal810950b2014-10-09 17:16:08 +0100391 return;
392 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100393
Jose Pascoal810950b2014-10-09 17:16:08 +0100394 // if there is a download ID on the shared preferences
395 if (mLatestUpdateDownloadId == 0)
396 {
397 mLatestUpdateDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100398
Jose Pascoal810950b2014-10-09 17:16:08 +0100399 // invalid download Id
400 if (mLatestUpdateDownloadId == 0)
401 {
402 mainActivity.changeState(UpdaterState.NORMAL);
403 return;
404 }
405 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100406
Jose Pascoal810950b2014-10-09 17:16:08 +0100407 updateDownloadFile();
408 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100409
Jose Pascoal810950b2014-10-09 17:16:08 +0100410 private void startPreInstall()
411 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100412
Jose Pascoal810950b2014-10-09 17:16:08 +0100413 if (RootTools.isAccessGiven())
414 {
415 // set the command for the recovery
416 Resources resources = getResources();
417 // Process p;
418 try
419 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100420
Jose Pascoal810950b2014-10-09 17:16:08 +0100421 Shell.runRootCommand(new CommandCapture(0, "rm -f /cache/recovery/command"));
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100422
Jose Pascoal810950b2014-10-09 17:16:08 +0100423 Shell.runRootCommand(new CommandCapture(0, "rm -f /cache/recovery/extendedcommand"));
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100424
Jose Pascoal810950b2014-10-09 17:16:08 +0100425 Shell.runRootCommand(new CommandCapture(0, "echo '--wipe_cache' >> /cache/recovery/command"));
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100426
Jose Pascoal810950b2014-10-09 17:16:08 +0100427 if (canCopyToCache())
428 {
429 Shell.runRootCommand(new CommandCapture(0, "echo '--update_package=/" + resources.getString(R.string.recoveryCachePath) + "/"
430 + VersionParserHelper.getNameFromVersion(mSelectedVersion) + "' >> /cache/recovery/command"));
431 }
432 else
433 {
434 Shell.runRootCommand(new CommandCapture(0, "echo '--update_package=/" + resources.getString(R.string.recoverySdCardPath)
435 + resources.getString(R.string.updaterFolder) + VersionParserHelper.getNameFromVersion(mSelectedVersion)
436 + "' >> /cache/recovery/command"));
437 }
438 } catch (IOException e)
439 {
440 // TODO Auto-generated catch block
441 e.printStackTrace();
442 } catch (NotFoundException e)
443 {
444 // TODO Auto-generated catch block
445 e.printStackTrace();
446 } catch (TimeoutException e)
447 {
448 // TODO Auto-generated catch block
449 e.printStackTrace();
450 } catch (RootDeniedException e)
451 {
452 // TODO Auto-generated catch block
453 e.printStackTrace();
454 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100455
Jose Pascoal810950b2014-10-09 17:16:08 +0100456 // send broadcast intent
457 Intent broadcastIntent = new Intent();
458 broadcastIntent.setAction(GappsInstallerHelper.GAPPS_REINSTALATION);
459 mainActivity.sendBroadcast(broadcastIntent);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100460
Jose Pascoal810950b2014-10-09 17:16:08 +0100461 if (canCopyToCache())
462 {
463 removeLastUpdateDownload();
464 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100465
Jose Pascoal810950b2014-10-09 17:16:08 +0100466 // remove the update files from data
467 removeUpdateFilesFromData();
468 // reboot the device into recovery
469 // ((PowerManager)
470 // getSystemService(POWER_SERVICE)).reboot("recovery");
471 try
472 {
473 mainActivity.updateStatePreference(UpdaterState.NORMAL);
474 Shell.runRootCommand(new CommandCapture(0, "reboot recovery"));
475 } catch (IOException 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 }
488 }
489 else
490 {
491 // TODO: show warning
492 }
493 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100494
Jose Pascoal810950b2014-10-09 17:16:08 +0100495 private void copyUpdateToCache(File file)
496 {
497 if (canCopyToCache())
498 {
499 CopyFileToCacheTask copyTask = new CopyFileToCacheTask();
500 copyTask.execute(file.getPath(), Environment.getDownloadCacheDirectory() + "/" + VersionParserHelper.getNameFromVersion(mSelectedVersion));
501 }
502 else
503 {
504 Log.d(TAG, "No space on cache. Defaulting to Sdcard");
Pedro Arelo773bd822014-10-10 11:57:34 +0100505 Toast.makeText(mainActivity, getResources().getString(R.string.no_space_available_cache_message), Toast.LENGTH_LONG).show();
Jose Pascoal810950b2014-10-09 17:16:08 +0100506 }
507 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100508
Jose Pascoal810950b2014-10-09 17:16:08 +0100509 public boolean canCopyToCache()
510 {
511 Resources resources = getResources();
512 double cacheSize = Utils.getPartitionSizeInMBytes(Environment.getDownloadCacheDirectory());
513 return cacheSize > resources.getInteger(R.integer.FP1CachePartitionSizeMb) && cacheSize > resources.getInteger(R.integer.minimalCachePartitionSizeMb);
514 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100515
Jose Pascoal810950b2014-10-09 17:16:08 +0100516 private void clearCache()
517 {
518 File f = Environment.getDownloadCacheDirectory();
519 File files[] = f.listFiles();
520 if (files != null)
521 {
522 Log.d(TAG, "Size: " + files.length);
523 for (int i = 0; i < files.length; i++)
524 {
525 String filename = files[i].getName();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100526
Jose Pascoal810950b2014-10-09 17:16:08 +0100527 if (filename.endsWith(".zip"))
528 {
529 files[i].delete();
530 Log.d(TAG, "Deleted file " + filename);
531 }
532 }
533 }
534 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100535
Jose Pascoal810950b2014-10-09 17:16:08 +0100536 // ************************************************************************************
537 // Update Removal
538 // ************************************************************************************
539 private void removeUpdateFilesFromData()
540 {
541 try
542 {
543 Shell.runRootCommand(new CommandCapture(0, getResources().getString(R.string.removePlayStoreCommand), getResources().getString(
544 R.string.removeGooglePlusCommand), getResources().getString(R.string.removeSoundSearchCommand), getResources().getString(
545 R.string.removeGmailCommand), getResources().getString(R.string.removePlayServicesCommand), getResources().getString(
546 R.string.removeQuicksearchCommand), getResources().getString(R.string.removeTalkbackCommand), getResources().getString(
547 R.string.removeText2SpeechCommand)));
548 } catch (IOException e)
549 {
550 e.printStackTrace();
551 } catch (TimeoutException e)
552 {
553 e.printStackTrace();
554 } catch (RootDeniedException e)
555 {
556 e.printStackTrace();
557 }
558 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100559
Jose Pascoal810950b2014-10-09 17:16:08 +0100560 public void removeLastUpdateDownload()
561 {
562 long latestUpdateDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference();
563 if (latestUpdateDownloadId != 0)
564 {
565 // residue download ID
566 mDownloadManager.remove(latestUpdateDownloadId);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100567
Jose Pascoal810950b2014-10-09 17:16:08 +0100568 mainActivity.resetLastUpdateDownloadId();
569 }
570 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100571
Jose Pascoal810950b2014-10-09 17:16:08 +0100572 private class CopyFileToCacheTask extends AsyncTask<String, Integer, Integer>
573 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100574
Jose Pascoal810950b2014-10-09 17:16:08 +0100575 ProgressDialog mProgress;
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100576
Jose Pascoal810950b2014-10-09 17:16:08 +0100577 @Override
578 protected Integer doInBackground(String... params)
579 {
580 // check the correct number of
581 if (params.length != 2)
582 {
583 return -1;
584 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100585
Jose Pascoal810950b2014-10-09 17:16:08 +0100586 String originalFilePath = params[0];
587 String destinyFilePath = params[1];
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100588
Jose Pascoal810950b2014-10-09 17:16:08 +0100589 if (RootTools.isAccessGiven())
590 {
591 clearCache();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100592
Jose Pascoal810950b2014-10-09 17:16:08 +0100593 File otaFilePath = new File(originalFilePath);
594 File otaFileCache = new File(destinyFilePath);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100595
Jose Pascoal810950b2014-10-09 17:16:08 +0100596 if (!otaFileCache.exists())
597 {
598 RootTools.copyFile(otaFilePath.getPath(), otaFileCache.getPath(), false, false);
599 }
600 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100601
Jose Pascoal810950b2014-10-09 17:16:08 +0100602 return 1;
603 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100604
Jose Pascoal810950b2014-10-09 17:16:08 +0100605 protected void onProgressUpdate(Integer... progress)
606 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100607
Jose Pascoal810950b2014-10-09 17:16:08 +0100608 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100609
Jose Pascoal810950b2014-10-09 17:16:08 +0100610 protected void onPreExecute()
611 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100612
Jose Pascoal810950b2014-10-09 17:16:08 +0100613 if (mProgress == null)
614 {
615 String title = "";
Pedro Arelo773bd822014-10-10 11:57:34 +0100616 String message = mainActivity.getResources().getString(R.string.please_be_patient);
Jose Pascoal810950b2014-10-09 17:16:08 +0100617 mProgress = ProgressDialog.show(mainActivity, title, message, true, false);
618 }
619 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100620
Jose Pascoal810950b2014-10-09 17:16:08 +0100621 protected void onPostExecute(Integer result)
622 {
623 // disable the spinner
624 if (mProgress != null)
625 {
626 mProgress.dismiss();
627 mProgress = null;
628 }
629 }
630 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100631
Jose Pascoal810950b2014-10-09 17:16:08 +0100632 private String getVersionDownloadPath(Version version)
633 {
634 Resources resources = mainActivity.getResources();
635 return Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + VersionParserHelper.getNameFromVersion(version);
636 }
Jose Pascoalb397dc62014-10-13 19:26:58 +0100637
638 public void abortUpdateProccess()
639 {
640 removeLastUpdateDownload();
641 mainActivity.updateStatePreference(UpdaterState.NORMAL);
642 mainActivity.removeLastFragment();
643 }
644
Jose Pascoalb690af12014-10-06 18:29:26 +0100645}