blob: 37dc5bea184b41d588171aa23652943cef7ab587 [file] [log] [blame]
Jose Pascoal91aff132014-10-02 17:19:47 +01001package com.fairphone.updater.fragments;
2
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +00003import android.annotation.SuppressLint;
Jose Pascoal54b3ae62014-10-07 20:29:58 +01004import android.app.AlertDialog;
5import android.app.DownloadManager;
6import android.app.DownloadManager.Request;
7import android.content.Context;
8import android.content.DialogInterface;
9import android.content.res.Resources;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010010import android.net.Uri;
Jose Pascoal91aff132014-10-02 17:19:47 +010011import android.os.Bundle;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010012import android.os.Environment;
Tiago Costae29f4cd2014-10-10 18:30:59 +010013import android.support.v4.app.FragmentManager;
Filipe Gonçalvesbc616bc2014-12-31 14:39:22 +000014import android.util.Log;
Jose Pascoal91aff132014-10-02 17:19:47 +010015import android.view.LayoutInflater;
16import android.view.View;
Jose Pascoale4c48e32014-10-03 19:55:39 +010017import android.view.View.OnClickListener;
Jose Pascoal91aff132014-10-02 17:19:47 +010018import android.view.ViewGroup;
Jose Pascoale4c48e32014-10-03 19:55:39 +010019import android.widget.Button;
Jose Pascoale4c48e32014-10-03 19:55:39 +010020import android.widget.TextView;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010021import android.widget.Toast;
Jose Pascoal91aff132014-10-02 17:19:47 +010022
Jose Pascoalcfc2dd42015-02-09 18:00:05 +000023import com.fairphone.updater.FairphoneUpdater;
Jose Pascoal7bf83a02014-10-13 18:30:18 +010024import com.fairphone.updater.FairphoneUpdater.HeaderType;
25import com.fairphone.updater.FairphoneUpdater.UpdaterState;
Jose Pascoal91aff132014-10-02 17:19:47 +010026import com.fairphone.updater.R;
Jose Pascoal02d86242014-12-17 18:50:08 +000027import com.fairphone.updater.data.DownloadableItem;
Tiago Costa3add03e2014-12-16 12:05:23 +000028import com.fairphone.updater.data.Store;
Jose Pascoal7bf83a02014-10-13 18:30:18 +010029import com.fairphone.updater.data.Version;
Tiago Costae29f4cd2014-10-10 18:30:59 +010030import com.fairphone.updater.fragments.ConfirmationPopupDialog.ConfirmationPopupDialogListener;
Jose Pascoal54b3ae62014-10-07 20:29:58 +010031import com.fairphone.updater.tools.Utils;
Jose Pascoal91aff132014-10-02 17:19:47 +010032
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000033import java.io.File;
34import java.net.MalformedURLException;
35import java.net.URL;
36import java.util.Locale;
37
38@SuppressLint("ValidFragment")
Pedro Arelo4b626232014-10-09 15:12:41 +010039public class VersionDetailFragment extends BaseFragment
40{
Jose Pascoal54b3ae62014-10-07 20:29:58 +010041
Pedro Arelo4b626232014-10-09 15:12:41 +010042 private static final String TAG = VersionDetailFragment.class.getSimpleName();
Jose Pascoal54b3ae62014-10-07 20:29:58 +010043
Pedro Arelo4b626232014-10-09 15:12:41 +010044 public static enum DetailLayoutType
45 {
Maarten Derksd2704262016-03-17 15:34:51 +010046 UPDATE_FAIRPHONE, UPDATE_ANDROID, LATEST_FAIRPHONE, FAIRPHONE, ANDROID, APP_STORE
Pedro Arelo4b626232014-10-09 15:12:41 +010047 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +010048
Pedro Arelo4b626232014-10-09 15:12:41 +010049 private HeaderType mHeaderType;
50 private String mHeaderText;
51 private TextView mVersion_details_title_text;
52 private TextView mVersion_release_notes_text;
Pedro Arelo4b626232014-10-09 15:12:41 +010053 private Button mDownload_and_update_button;
54 private TextView mVersion_details_name_text;
55 private String mVersionDetailsTitle;
56 private Version mSelectedVersion;
57 private DownloadManager mDownloadManager;
58 private DetailLayoutType mDetailLayoutType;
Jose Pascoaldc29afe2014-10-10 13:43:47 +010059 private boolean mIsOSChange;
Jose Pascoal391bd312014-10-13 18:16:27 +010060 private boolean mIsOlderVersion;
Tiago Costa3add03e2014-12-16 12:05:23 +000061 private Store mSelectedStore;
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000062 private final boolean mIsVersion;
Jose Pascoal02d86242014-12-17 18:50:08 +000063
64 public VersionDetailFragment(boolean isVersion)
65 {
66 super();
67
68 mIsVersion = isVersion;
69 }
Tiago Costa3add03e2014-12-16 12:05:23 +000070
Pedro Arelo4b626232014-10-09 15:12:41 +010071 @Override
72 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
73 {
74 // Inflate the layout for this fragment
75 View view = setLayoutType(inflater, container);
Jose Pascoal54b3ae62014-10-07 20:29:58 +010076
Pedro Arelo4b626232014-10-09 15:12:41 +010077 setupLayout(view);
Jose Pascoal54b3ae62014-10-07 20:29:58 +010078
Pedro Arelo4b626232014-10-09 15:12:41 +010079 return view;
80 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +010081
Pedro Arelo4b626232014-10-09 15:12:41 +010082 private View setLayoutType(LayoutInflater inflater, ViewGroup container)
83 {
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000084 View view;
Pedro Arelo4b626232014-10-09 15:12:41 +010085 switch (mDetailLayoutType)
86 {
Jose Pascoalee8a8eb2014-10-15 15:20:28 +010087 case UPDATE_ANDROID:
Pedro Arelo4b626232014-10-09 15:12:41 +010088 case ANDROID:
89 view = inflater.inflate(R.layout.fragment_version_detail_android, container, false);
90 break;
Tiago Costa3add03e2014-12-16 12:05:23 +000091 case APP_STORE:
Tiago Costa198bf3d2014-12-16 15:23:18 +000092 view = inflater.inflate(R.layout.fragment_app_store_detail, container, false);
Tiago Costa3add03e2014-12-16 12:05:23 +000093 break;
Jose Pascoalb01dd262014-10-17 17:21:19 +010094 case UPDATE_FAIRPHONE:
Maarten Derksd2704262016-03-17 15:34:51 +010095 case LATEST_FAIRPHONE:
Pedro Arelo4b626232014-10-09 15:12:41 +010096 case FAIRPHONE:
97 default:
98 view = inflater.inflate(R.layout.fragment_version_detail_fairphone, container, false);
99 break;
100 }
101 return view;
102 }
Jose Pascoale4c48e32014-10-03 19:55:39 +0100103
Pedro Arelo4b626232014-10-09 15:12:41 +0100104 private void setupLayout(View view)
105 {
106 mVersion_details_title_text = (TextView) view.findViewById(R.id.version_details_title_text);
107 mVersion_details_name_text = (TextView) view.findViewById(R.id.version_details_name_text);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100108
Pedro Arelo4b626232014-10-09 15:12:41 +0100109 mVersion_release_notes_text = (TextView) view.findViewById(R.id.version_release_notes_text);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100110
Pedro Arelo4b626232014-10-09 15:12:41 +0100111 mDownload_and_update_button = (Button) view.findViewById(R.id.download_and_update_button);
112 }
Jose Pascoale4c48e32014-10-03 19:55:39 +0100113
Pedro Arelo4b626232014-10-09 15:12:41 +0100114 private void setupDownloadAndUpdateButton()
115 {
Jose Pascoald6c19a72014-10-17 16:52:36 +0100116 setDownloadAndUpdateButtonText();
117
Pedro Arelo4b626232014-10-09 15:12:41 +0100118 mDownload_and_update_button.setOnClickListener(new OnClickListener()
119 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100120
Pedro Arelo4b626232014-10-09 15:12:41 +0100121 @Override
122 public void onClick(View v)
123 {
Maarten Derksc75843c2016-04-26 11:58:36 +0200124 boolean isWifiEnabled = Utils.isWiFiEnabled(mainActivity);
125 boolean isBatteryLevelOk = Utils.isBatteryLevelOk(mainActivity);
126
127 if (isWifiEnabled && isBatteryLevelOk)
128 {
129 startDownload();
130 }
131 else
132 {
133 if(!isWifiEnabled) {
134 AlertDialog.Builder wifiDialog = new AlertDialog.Builder(mainActivity);
135 wifiDialog.setIcon(R.drawable.ic_signal_wifi_4_bar_fpblue_24dp);
136 wifiDialog.setTitle(R.string.connect_to_wifi);
137 wifiDialog.setPositiveButton(R.string.got_it, new DialogInterface.OnClickListener() {
138 public void onClick(DialogInterface dialog, int id) {
139 // do nothing, since the state is still the same
140 }
141 });
142 wifiDialog.create();
143 wifiDialog.show();
144 }
145
146 if(!isBatteryLevelOk) {
147 AlertDialog.Builder batteryDialog = new AlertDialog.Builder(mainActivity);
148 batteryDialog.setIcon(R.drawable.ic_battery_std_fpblue_24dp);
149 batteryDialog.setTitle(R.string.charge_battery);
150 batteryDialog.setPositiveButton(R.string.got_it, new DialogInterface.OnClickListener() {
151 public void onClick(DialogInterface dialog, int id) {
152 // do nothing, since the state is still the same
153 }
154 });
155 batteryDialog.create();
156 batteryDialog.show();
157 }
158 }
Pedro Arelo4b626232014-10-09 15:12:41 +0100159 }
160 });
161 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100162
Jose Pascoald6c19a72014-10-17 16:52:36 +0100163 private void setDownloadAndUpdateButtonText()
164 {
165 switch (mDetailLayoutType)
166 {
167 case UPDATE_ANDROID:
168 case UPDATE_FAIRPHONE:
169 mDownload_and_update_button.setText(R.string.install_update);
170 break;
Tiago Costa3add03e2014-12-16 12:05:23 +0000171 case APP_STORE:
Maarten Derksd2704262016-03-17 15:34:51 +0100172 case LATEST_FAIRPHONE:
Jose Pascoald6c19a72014-10-17 16:52:36 +0100173 case FAIRPHONE:
174 case ANDROID:
175 default:
176 mDownload_and_update_button.setText(R.string.install);
177 break;
178 }
179 }
180
Pedro Arelo4b626232014-10-09 15:12:41 +0100181 private void updateReleaseNotesText()
182 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000183 DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore;
184 if (item != null)
Pedro Arelo4b626232014-10-09 15:12:41 +0100185 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000186 mVersion_release_notes_text.setText(item.getReleaseNotes(Locale.getDefault().getLanguage()));
Jose Pascoalad4ea572014-12-16 20:31:48 +0000187 }
Pedro Arelo4b626232014-10-09 15:12:41 +0100188 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100189
Pedro Arelo4b626232014-10-09 15:12:41 +0100190 private void updateVersionName()
191 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000192 DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore;
193
Pedro Arelo4b626232014-10-09 15:12:41 +0100194 mVersion_details_title_text.setText(mVersionDetailsTitle);
Jose Pascoal02d86242014-12-17 18:50:08 +0000195 if (item != null)
Jose Pascoalad4ea572014-12-16 20:31:48 +0000196 {
Jose Pascoal40916302015-02-06 18:43:47 +0000197 mVersion_details_name_text.setText(mainActivity.getItemName(item, mIsVersion));
Jose Pascoalad4ea572014-12-16 20:31:48 +0000198 }
Pedro Arelo4b626232014-10-09 15:12:41 +0100199 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100200
Pedro Arelo4b626232014-10-09 15:12:41 +0100201 public void setupFragment(Version selectedVersion, DetailLayoutType detailType)
202 {
203 mSelectedVersion = selectedVersion;
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100204
Pedro Arelo4b626232014-10-09 15:12:41 +0100205 mDetailLayoutType = detailType;
Jose Pascoalad4ea572014-12-16 20:31:48 +0000206 mSelectedStore = null;
Pedro Arelo4b626232014-10-09 15:12:41 +0100207 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100208
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000209 public void setupAppStoreFragment(Store selectedStore)
Tiago Costa3add03e2014-12-16 12:05:23 +0000210 {
211 mSelectedStore = selectedStore;
212
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000213 mDetailLayoutType = DetailLayoutType.APP_STORE;
Jose Pascoalad4ea572014-12-16 20:31:48 +0000214 mSelectedVersion = null;
Tiago Costa3add03e2014-12-16 12:05:23 +0000215 }
216
Pedro Arelo4b626232014-10-09 15:12:41 +0100217 private void setHeaderAndVersionDetailsTitles()
218 {
Jose Pascoal436915f2014-10-08 15:37:26 +0100219
Jose Pascoale866cef2014-10-09 19:46:21 +0100220 Resources resources = mainActivity.getResources();
Jose Pascoalad4ea572014-12-16 20:31:48 +0000221 Version deviceVersion = mainActivity.getDeviceVersion();
Jose Pascoal02d86242014-12-17 18:50:08 +0000222
223 if (mIsVersion && mSelectedVersion != null)
Jose Pascoalad4ea572014-12-16 20:31:48 +0000224 {
Jose Pascoalcfc2dd42015-02-09 18:00:05 +0000225 mHeaderType = FairphoneUpdater.getHeaderTypeFromImageType(mSelectedVersion.getImageType());
Jose Pascoalad4ea572014-12-16 20:31:48 +0000226 }
227 else if (mSelectedStore != null)
228 {
229 mHeaderType = HeaderType.APP_STORE;
230 }
231 else
232 {
233 mHeaderType = HeaderType.FAIRPHONE;
234 }
Jose Pascoal436915f2014-10-08 15:37:26 +0100235
Pedro Arelo4b626232014-10-09 15:12:41 +0100236 switch (mDetailLayoutType)
237 {
Jose Pascoalee8a8eb2014-10-15 15:20:28 +0100238 case UPDATE_FAIRPHONE:
239 case UPDATE_ANDROID:
Jose Pascoale866cef2014-10-09 19:46:21 +0100240 mHeaderText = resources.getString(R.string.install_update);
241 mVersionDetailsTitle = resources.getString(R.string.update_version);
Jose Pascoaldc29afe2014-10-10 13:43:47 +0100242 mIsOSChange = false;
Jose Pascoal391bd312014-10-13 18:16:27 +0100243 mIsOlderVersion = false;
Pedro Arelo4b626232014-10-09 15:12:41 +0100244 break;
Tiago Costae29f4cd2014-10-10 18:30:59 +0100245
Pedro Arelo4b626232014-10-09 15:12:41 +0100246 case ANDROID:
Maarten Derks4bb57892016-01-26 10:52:53 +0100247 mHeaderText = mSelectedVersion.getHumanReadableName();
Jose Pascoale866cef2014-10-09 19:46:21 +0100248 mVersionDetailsTitle = resources.getString(R.string.new_os);
Jose Pascoalad4ea572014-12-16 20:31:48 +0000249 mIsOSChange = deviceVersion.getImageType().equalsIgnoreCase(Version.IMAGE_TYPE_FAIRPHONE);
Jose Pascoal391bd312014-10-13 18:16:27 +0100250 mIsOlderVersion =
Jose Pascoalad4ea572014-12-16 20:31:48 +0000251 (deviceVersion.getImageType().equalsIgnoreCase(Version.IMAGE_TYPE_AOSP) && deviceVersion.isNewerVersionThan(mSelectedVersion));
Pedro Arelo4b626232014-10-09 15:12:41 +0100252 break;
Jose Pascoalad4ea572014-12-16 20:31:48 +0000253 case APP_STORE:
Jose Pascoalcfc2dd42015-02-09 18:00:05 +0000254 mHeaderText = FairphoneUpdater.getStoreName(mSelectedStore);
Jose Pascoalad4ea572014-12-16 20:31:48 +0000255 mVersionDetailsTitle = resources.getString(R.string.install);
256 mIsOSChange = false;
257 mIsOlderVersion = false;
258 break;
Maarten Derksd2704262016-03-17 15:34:51 +0100259 case LATEST_FAIRPHONE:
260 mHeaderText = mSelectedVersion.getHumanReadableName();
261 mVersionDetailsTitle = resources.getString(R.string.latest_version);
262 mIsOSChange = deviceVersion.getImageType().equalsIgnoreCase(Version.IMAGE_TYPE_AOSP);
263 mIsOlderVersion =
264 (deviceVersion.getImageType().equalsIgnoreCase(Version.IMAGE_TYPE_FAIRPHONE) && deviceVersion.isNewerVersionThan(mSelectedVersion));
265 break;
Pedro Arelo4b626232014-10-09 15:12:41 +0100266 case FAIRPHONE:
267 default:
Maarten Derks4bb57892016-01-26 10:52:53 +0100268 mHeaderText = mSelectedVersion.getHumanReadableName();
Jose Pascoale866cef2014-10-09 19:46:21 +0100269 mVersionDetailsTitle = resources.getString(R.string.older_version);
Jose Pascoalad4ea572014-12-16 20:31:48 +0000270 mIsOSChange = deviceVersion.getImageType().equalsIgnoreCase(Version.IMAGE_TYPE_AOSP);
Jose Pascoal391bd312014-10-13 18:16:27 +0100271 mIsOlderVersion =
Jose Pascoalad4ea572014-12-16 20:31:48 +0000272 (deviceVersion.getImageType().equalsIgnoreCase(Version.IMAGE_TYPE_FAIRPHONE) && deviceVersion.isNewerVersionThan(mSelectedVersion));
Pedro Arelo4b626232014-10-09 15:12:41 +0100273 break;
274 }
275 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100276
Pedro Arelo4b626232014-10-09 15:12:41 +0100277 private Request createDownloadRequest(String url, String fileName, String downloadTitle)
278 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100279
Pedro Arelo4b626232014-10-09 15:12:41 +0100280 Resources resources = getResources();
281 Request request;
282 try
283 {
284 request = new Request(Uri.parse(url));
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000285 final File externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder));
Jose Pascoal46fdb062015-02-05 18:59:32 +0000286 final boolean notMkDirs = !externalStoragePublicDirectory.mkdirs();
287 if(notMkDirs && !externalStoragePublicDirectory.exists()) {
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000288 throw new Exception("Couldn't create updater dir structures.");
289 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100290
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000291 request.setDestinationInExternalPublicDir(resources.getString(R.string.updaterFolder), fileName);
Pedro Arelo4b626232014-10-09 15:12:41 +0100292 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
293 request.setAllowedOverRoaming(false);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100294
Pedro Arelo4b626232014-10-09 15:12:41 +0100295 request.setTitle(downloadTitle);
296 } catch (Exception e)
297 {
Jose Pascoalcfc2dd42015-02-09 18:00:05 +0000298 Log.w(TAG, "Error setting the download request: " + e.getLocalizedMessage());
Pedro Arelo4b626232014-10-09 15:12:41 +0100299 request = null;
300 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100301
Pedro Arelo4b626232014-10-09 15:12:41 +0100302 return request;
303 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100304
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000305 void startUpdateDownload()
Pedro Arelo4b626232014-10-09 15:12:41 +0100306 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000307 DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore;
Maarten Derksc75843c2016-04-26 11:58:36 +0200308
309 boolean isWifiEnabled = Utils.isWiFiEnabled(mainActivity);
310 boolean isBatteryLevelOk = Utils.isBatteryLevelOk(mainActivity);
311
312 if (isWifiEnabled && isBatteryLevelOk)
Pedro Arelo4b626232014-10-09 15:12:41 +0100313 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000314 if (item != null)
Pedro Arelo4b626232014-10-09 15:12:41 +0100315 {
Jose Pascoalaa579a82014-11-05 22:17:16 +0000316 // set the download for the latest version on the download
317 // manager
Jose Pascoal40916302015-02-06 18:43:47 +0000318 String fileName = Utils.getFilenameFromDownloadableItem(item, mIsVersion);
319 String downloadTitle = Utils.getDownloadTitleFromDownloadableItem(getResources(), item, mIsVersion);
Filipe Gonçalvesbc616bc2014-12-31 14:39:22 +0000320 String download_link = item.getDownloadLink();
321 if (!(download_link.startsWith("http://") || download_link.startsWith("https://")))
322 {
323 // If the download URL is a relative path, make it an absolute
Jose Pascoal4fe7ba52015-03-13 16:54:31 +0000324 download_link = mainActivity.getPreferenceOtaDownloadUrl() + "/" + download_link;
Filipe Gonçalvesbc616bc2014-12-31 14:39:22 +0000325 // Sanitize URL - e.g. turn http://a.b//c/./d/../e to http://a.b/c/e
326 download_link = download_link.replaceAll("([^:])//", "/");
327 download_link = download_link.replaceAll("/([^/]+)/\\.\\./", "/");
328 download_link = download_link.replaceAll("/\\./", "/");
329 try {
330 download_link = new URL(download_link).toExternalForm();
331 } catch (MalformedURLException e) {
Jose Pascoalcfc2dd42015-02-09 18:00:05 +0000332 Log.w(TAG, "Potentially malformed download link " + download_link + ": " + e.getLocalizedMessage());
Filipe Gonçalvesbc616bc2014-12-31 14:39:22 +0000333 }
334 }
Maarten Derksab3a9862016-04-19 11:38:41 +0200335 Request request = createDownloadRequest(download_link, fileName, downloadTitle);
Jose Pascoalaa579a82014-11-05 22:17:16 +0000336 if (request != null && mDownloadManager != null)
337 {
Jose Pascoal87758742015-01-28 20:00:22 +0000338 //Guarantee that only we have only one download
339 long oldDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference();
340 if(oldDownloadId != 0){
341 mDownloadManager.remove(oldDownloadId);
342 mainActivity.saveLatestUpdateDownloadId(0);
343 }
344
Jose Pascoalaa579a82014-11-05 22:17:16 +0000345 long mLatestUpdateDownloadId = mDownloadManager.enqueue(request);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100346
Jose Pascoalaa579a82014-11-05 22:17:16 +0000347 // save it on the shared preferences
348 mainActivity.saveLatestUpdateDownloadId(mLatestUpdateDownloadId);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100349
Jose Pascoalaa579a82014-11-05 22:17:16 +0000350 // change state to download
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000351 mainActivity.updateStatePreference(UpdaterState.DOWNLOAD);
352 mainActivity.changeFragment(mainActivity.getFragmentFromState());
Jose Pascoalaa579a82014-11-05 22:17:16 +0000353 }
354 else
355 {
356 Toast.makeText(mainActivity, getResources().getString(R.string.error_downloading) + " " + downloadTitle, Toast.LENGTH_LONG).show();
357 }
Pedro Arelo4b626232014-10-09 15:12:41 +0100358 }
359 }
360 else
361 {
362 Resources resources = this.getResources();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100363
Maarten Derksc75843c2016-04-26 11:58:36 +0200364 if(!isWifiEnabled) {
365 AlertDialog.Builder wifiDialog = new AlertDialog.Builder(mainActivity);
366 wifiDialog.setIcon(R.drawable.ic_signal_wifi_4_bar_fpblue_24dp);
367 wifiDialog.setTitle(resources.getString(R.string.connect_to_wifi));
368 wifiDialog.setPositiveButton(resources.getString(R.string.got_it), new DialogInterface.OnClickListener() {
369 public void onClick(DialogInterface dialog, int id) {
370 // do nothing, since the state is still the same
371 }
372 });
373 wifiDialog.create();
374 wifiDialog.show();
375 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100376
Maarten Derksc75843c2016-04-26 11:58:36 +0200377 if(!isBatteryLevelOk) {
378 AlertDialog.Builder batteryDialog = new AlertDialog.Builder(mainActivity);
379 batteryDialog.setIcon(R.drawable.ic_battery_std_fpblue_24dp);
380 batteryDialog.setTitle(resources.getString(R.string.charge_battery));
381 batteryDialog.setPositiveButton(resources.getString(R.string.got_it), new DialogInterface.OnClickListener() {
382 public void onClick(DialogInterface dialog, int id) {
383 // do nothing, since the state is still the same
384 }
385 });
386 batteryDialog.create();
387 batteryDialog.show();
388 }
Pedro Arelo4b626232014-10-09 15:12:41 +0100389 }
390 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100391
Pedro Arelo4b626232014-10-09 15:12:41 +0100392 @Override
393 public void onResume()
394 {
395 super.onResume();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100396
Pedro Arelo4b626232014-10-09 15:12:41 +0100397 mDownloadManager = (DownloadManager) mainActivity.getSystemService(Context.DOWNLOAD_SERVICE);
Jose Pascoale866cef2014-10-09 19:46:21 +0100398
399 setHeaderAndVersionDetailsTitles();
Tiago Costa3855faa2014-11-14 17:55:05 +0000400 mainActivity.updateHeader(mHeaderType, mHeaderText, false);
Jose Pascoale866cef2014-10-09 19:46:21 +0100401 updateVersionName();
402 updateReleaseNotesText();
Jose Pascoale866cef2014-10-09 19:46:21 +0100403 setupDownloadAndUpdateButton();
Pedro Arelo4b626232014-10-09 15:12:41 +0100404 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100405
Jose Pascoald6c19a72014-10-17 16:52:36 +0100406 private void showPopupDialog(String version, boolean hasEraseAllDataWarning, ConfirmationPopupDialogListener listener)
Tiago Costae29f4cd2014-10-10 18:30:59 +0100407 {
408 FragmentManager fm = getActivity().getSupportFragmentManager();
Jose Pascoald6c19a72014-10-17 16:52:36 +0100409 ConfirmationPopupDialog popupDialog =
410 new ConfirmationPopupDialog(version, mIsOSChange, mIsOlderVersion, hasEraseAllDataWarning, mDetailLayoutType, listener);
Tiago Costae29f4cd2014-10-10 18:30:59 +0100411 popupDialog.show(fm, version);
412 }
413
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000414 void startDownload()
Tiago Costa3add03e2014-12-16 12:05:23 +0000415 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000416 if (mIsVersion && mSelectedVersion != null)
Tiago Costa3add03e2014-12-16 12:05:23 +0000417 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000418 if (mIsOSChange || mIsOlderVersion)
Jose Pascoaldc29afe2014-10-10 13:43:47 +0100419 {
Maarten Derks4bb57892016-01-26 10:52:53 +0100420 showPopupDialog(mSelectedVersion.getHumanReadableName(), mSelectedVersion.hasEraseAllPartitionWarning(),
Jose Pascoal02d86242014-12-17 18:50:08 +0000421 new ConfirmationPopupDialogListener()
422 {
423
424 @Override
425 public void onFinishPopUpDialog(boolean isOk)
Jose Pascoalaa579a82014-11-05 22:17:16 +0000426 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000427 if (isOk)
Tiago Costa3add03e2014-12-16 12:05:23 +0000428 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000429 mainActivity.setSelectedVersion(mSelectedVersion);
430 showEraseAllDataWarning(true);
Tiago Costa3add03e2014-12-16 12:05:23 +0000431 }
Jose Pascoal02d86242014-12-17 18:50:08 +0000432 }
433 });
Tiago Costa3add03e2014-12-16 12:05:23 +0000434 }
435 else
436 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000437 mainActivity.setSelectedVersion(mSelectedVersion);
438 showEraseAllDataWarning(false);
Tiago Costa3add03e2014-12-16 12:05:23 +0000439 }
440 }
Jose Pascoal02d86242014-12-17 18:50:08 +0000441 else if (mSelectedStore != null)
442 {
443 mainActivity.setSelectedStore(mSelectedStore);
444 showStoreDisclaimer();
445 }
Tiago Costa3add03e2014-12-16 12:05:23 +0000446 }
Jose Pascoal02d86242014-12-17 18:50:08 +0000447
Jose Pascoal75392162014-10-15 18:29:01 +0100448 private void showEraseAllDataWarning(boolean bypassEraseAllWarning)
Pedro Arelo4b626232014-10-09 15:12:41 +0100449 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100450
Pedro Arelo4b626232014-10-09 15:12:41 +0100451 final UpdaterState currentState = mainActivity.getCurrentUpdaterState();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100452
Jose Pascoalaa579a82014-11-05 22:17:16 +0000453 if (mSelectedVersion != null && mSelectedVersion.hasEraseAllPartitionWarning() && !bypassEraseAllWarning)
Pedro Arelo4b626232014-10-09 15:12:41 +0100454 {
Pedro Arelo773bd822014-10-10 11:57:34 +0100455 new AlertDialog.Builder(mainActivity).setTitle(android.R.string.dialog_alert_title).setMessage(R.string.erase_all_partitions_warning_message)
Pedro Arelo4b626232014-10-09 15:12:41 +0100456 .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
457 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100458
Pedro Arelo4b626232014-10-09 15:12:41 +0100459 @Override
460 public void onClick(DialogInterface dialog, int which)
461 {
462 if (currentState == UpdaterState.NORMAL)
463 {
464 startUpdateDownload();
465 }
466 }
467 }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
468 {
469 public void onClick(DialogInterface dialog, int which)
470 {
471 mainActivity.setSelectedVersion(null);
472 }
473 }).show();
474 }
475 else
476 {
477 if (currentState == UpdaterState.NORMAL)
478 {
479 startUpdateDownload();
480 }
481 else
482 {
483 mainActivity.setSelectedVersion(null);
484 }
485 }
486 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100487
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000488 void showStoreDisclaimer()
Pedro Arelo4b626232014-10-09 15:12:41 +0100489 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000490 final UpdaterState currentState = mainActivity.getCurrentUpdaterState();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100491
Jose Pascoal02d86242014-12-17 18:50:08 +0000492 if (mSelectedStore != null && mSelectedStore.showDisclaimer())
493 {
494 new AlertDialog.Builder(mainActivity).setTitle(R.string.google_apps_disclaimer_title).setMessage(R.string.google_apps_disclaimer_description)
495 .setPositiveButton(R.string.google_apps_disclaimer_agree, new DialogInterface.OnClickListener()
Pedro Arelo4b626232014-10-09 15:12:41 +0100496 {
Tiago Costa3add03e2014-12-16 12:05:23 +0000497
Jose Pascoal02d86242014-12-17 18:50:08 +0000498 @Override
499 public void onClick(DialogInterface dialog, int which)
500 {
501 if (currentState == UpdaterState.NORMAL)
502 {
503 startUpdateDownload();
504 }
505 }
506 }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
507 {
508 public void onClick(DialogInterface dialog, int which)
509 {
510 mainActivity.setSelectedStore(null);
511 }
512 }).show();
513 }
514 else
515 {
516 if (currentState == UpdaterState.NORMAL)
517 {
518 startUpdateDownload();
519 }
520 else
521 {
522 mainActivity.setSelectedStore(null);
523 }
524 }
525 }
Jose Pascoal91aff132014-10-02 17:19:47 +0100526}