blob: 1dcfb0a1049f9af2548bb081bd3499f0f1b1b8cf [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 Derkse2f0ffc2016-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);
Maarten Derks3eac39c2016-06-07 14:22:31 +0200263 mIsOlderVersion = false;
Maarten Derksd2704262016-03-17 15:34:51 +0100264 break;
Pedro Arelo4b626232014-10-09 15:12:41 +0100265 case FAIRPHONE:
266 default:
Maarten Derks4bb57892016-01-26 10:52:53 +0100267 mHeaderText = mSelectedVersion.getHumanReadableName();
Jose Pascoale866cef2014-10-09 19:46:21 +0100268 mVersionDetailsTitle = resources.getString(R.string.older_version);
Jose Pascoalad4ea572014-12-16 20:31:48 +0000269 mIsOSChange = deviceVersion.getImageType().equalsIgnoreCase(Version.IMAGE_TYPE_AOSP);
Jose Pascoal391bd312014-10-13 18:16:27 +0100270 mIsOlderVersion =
Jose Pascoalad4ea572014-12-16 20:31:48 +0000271 (deviceVersion.getImageType().equalsIgnoreCase(Version.IMAGE_TYPE_FAIRPHONE) && deviceVersion.isNewerVersionThan(mSelectedVersion));
Pedro Arelo4b626232014-10-09 15:12:41 +0100272 break;
273 }
274 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100275
Pedro Arelo4b626232014-10-09 15:12:41 +0100276 private Request createDownloadRequest(String url, String fileName, String downloadTitle)
277 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100278
Pedro Arelo4b626232014-10-09 15:12:41 +0100279 Resources resources = getResources();
280 Request request;
281 try
282 {
283 request = new Request(Uri.parse(url));
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000284 final File externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder));
Jose Pascoal46fdb062015-02-05 18:59:32 +0000285 final boolean notMkDirs = !externalStoragePublicDirectory.mkdirs();
286 if(notMkDirs && !externalStoragePublicDirectory.exists()) {
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000287 throw new Exception("Couldn't create updater dir structures.");
288 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100289
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000290 request.setDestinationInExternalPublicDir(resources.getString(R.string.updaterFolder), fileName);
Pedro Arelo4b626232014-10-09 15:12:41 +0100291 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
292 request.setAllowedOverRoaming(false);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100293
Pedro Arelo4b626232014-10-09 15:12:41 +0100294 request.setTitle(downloadTitle);
295 } catch (Exception e)
296 {
Jose Pascoalcfc2dd42015-02-09 18:00:05 +0000297 Log.w(TAG, "Error setting the download request: " + e.getLocalizedMessage());
Pedro Arelo4b626232014-10-09 15:12:41 +0100298 request = null;
299 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100300
Pedro Arelo4b626232014-10-09 15:12:41 +0100301 return request;
302 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100303
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000304 void startUpdateDownload()
Pedro Arelo4b626232014-10-09 15:12:41 +0100305 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000306 DownloadableItem item = mIsVersion ? mSelectedVersion : mSelectedStore;
Maarten Derkse2f0ffc2016-04-26 11:58:36 +0200307
308 boolean isWifiEnabled = Utils.isWiFiEnabled(mainActivity);
309 boolean isBatteryLevelOk = Utils.isBatteryLevelOk(mainActivity);
310
311 if (isWifiEnabled && isBatteryLevelOk)
Pedro Arelo4b626232014-10-09 15:12:41 +0100312 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000313 if (item != null)
Pedro Arelo4b626232014-10-09 15:12:41 +0100314 {
Jose Pascoalaa579a82014-11-05 22:17:16 +0000315 // set the download for the latest version on the download
316 // manager
Jose Pascoal40916302015-02-06 18:43:47 +0000317 String fileName = Utils.getFilenameFromDownloadableItem(item, mIsVersion);
318 String downloadTitle = Utils.getDownloadTitleFromDownloadableItem(getResources(), item, mIsVersion);
Filipe Gonçalvesbc616bc2014-12-31 14:39:22 +0000319 String download_link = item.getDownloadLink();
320 if (!(download_link.startsWith("http://") || download_link.startsWith("https://")))
321 {
322 // If the download URL is a relative path, make it an absolute
Jose Pascoal4fe7ba52015-03-13 16:54:31 +0000323 download_link = mainActivity.getPreferenceOtaDownloadUrl() + "/" + download_link;
Filipe Gonçalvesbc616bc2014-12-31 14:39:22 +0000324 // Sanitize URL - e.g. turn http://a.b//c/./d/../e to http://a.b/c/e
325 download_link = download_link.replaceAll("([^:])//", "/");
326 download_link = download_link.replaceAll("/([^/]+)/\\.\\./", "/");
327 download_link = download_link.replaceAll("/\\./", "/");
328 try {
329 download_link = new URL(download_link).toExternalForm();
330 } catch (MalformedURLException e) {
Jose Pascoalcfc2dd42015-02-09 18:00:05 +0000331 Log.w(TAG, "Potentially malformed download link " + download_link + ": " + e.getLocalizedMessage());
Filipe Gonçalvesbc616bc2014-12-31 14:39:22 +0000332 }
333 }
Maarten Derksab3a9862016-04-19 11:38:41 +0200334 Request request = createDownloadRequest(download_link, fileName, downloadTitle);
Jose Pascoalaa579a82014-11-05 22:17:16 +0000335 if (request != null && mDownloadManager != null)
336 {
Jose Pascoal87758742015-01-28 20:00:22 +0000337 //Guarantee that only we have only one download
338 long oldDownloadId = mainActivity.getLatestUpdateDownloadIdFromSharedPreference();
339 if(oldDownloadId != 0){
340 mDownloadManager.remove(oldDownloadId);
341 mainActivity.saveLatestUpdateDownloadId(0);
342 }
343
Jose Pascoalaa579a82014-11-05 22:17:16 +0000344 long mLatestUpdateDownloadId = mDownloadManager.enqueue(request);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100345
Jose Pascoalaa579a82014-11-05 22:17:16 +0000346 // save it on the shared preferences
347 mainActivity.saveLatestUpdateDownloadId(mLatestUpdateDownloadId);
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100348
Jose Pascoalaa579a82014-11-05 22:17:16 +0000349 // change state to download
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000350 mainActivity.updateStatePreference(UpdaterState.DOWNLOAD);
351 mainActivity.changeFragment(mainActivity.getFragmentFromState());
Jose Pascoalaa579a82014-11-05 22:17:16 +0000352 }
353 else
354 {
355 Toast.makeText(mainActivity, getResources().getString(R.string.error_downloading) + " " + downloadTitle, Toast.LENGTH_LONG).show();
356 }
Pedro Arelo4b626232014-10-09 15:12:41 +0100357 }
358 }
359 else
360 {
361 Resources resources = this.getResources();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100362
Maarten Derkse2f0ffc2016-04-26 11:58:36 +0200363 if(!isWifiEnabled) {
364 AlertDialog.Builder wifiDialog = new AlertDialog.Builder(mainActivity);
365 wifiDialog.setIcon(R.drawable.ic_signal_wifi_4_bar_fpblue_24dp);
366 wifiDialog.setTitle(resources.getString(R.string.connect_to_wifi));
367 wifiDialog.setPositiveButton(resources.getString(R.string.got_it), new DialogInterface.OnClickListener() {
368 public void onClick(DialogInterface dialog, int id) {
369 // do nothing, since the state is still the same
370 }
371 });
372 wifiDialog.create();
373 wifiDialog.show();
374 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100375
Maarten Derkse2f0ffc2016-04-26 11:58:36 +0200376 if(!isBatteryLevelOk) {
377 AlertDialog.Builder batteryDialog = new AlertDialog.Builder(mainActivity);
378 batteryDialog.setIcon(R.drawable.ic_battery_std_fpblue_24dp);
379 batteryDialog.setTitle(resources.getString(R.string.charge_battery));
380 batteryDialog.setPositiveButton(resources.getString(R.string.got_it), new DialogInterface.OnClickListener() {
381 public void onClick(DialogInterface dialog, int id) {
382 // do nothing, since the state is still the same
383 }
384 });
385 batteryDialog.create();
386 batteryDialog.show();
387 }
Pedro Arelo4b626232014-10-09 15:12:41 +0100388 }
389 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100390
Pedro Arelo4b626232014-10-09 15:12:41 +0100391 @Override
392 public void onResume()
393 {
394 super.onResume();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100395
Pedro Arelo4b626232014-10-09 15:12:41 +0100396 mDownloadManager = (DownloadManager) mainActivity.getSystemService(Context.DOWNLOAD_SERVICE);
Jose Pascoale866cef2014-10-09 19:46:21 +0100397
398 setHeaderAndVersionDetailsTitles();
Tiago Costa3855faa2014-11-14 17:55:05 +0000399 mainActivity.updateHeader(mHeaderType, mHeaderText, false);
Jose Pascoale866cef2014-10-09 19:46:21 +0100400 updateVersionName();
401 updateReleaseNotesText();
Jose Pascoale866cef2014-10-09 19:46:21 +0100402 setupDownloadAndUpdateButton();
Pedro Arelo4b626232014-10-09 15:12:41 +0100403 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100404
Jose Pascoald6c19a72014-10-17 16:52:36 +0100405 private void showPopupDialog(String version, boolean hasEraseAllDataWarning, ConfirmationPopupDialogListener listener)
Tiago Costae29f4cd2014-10-10 18:30:59 +0100406 {
407 FragmentManager fm = getActivity().getSupportFragmentManager();
Jose Pascoald6c19a72014-10-17 16:52:36 +0100408 ConfirmationPopupDialog popupDialog =
409 new ConfirmationPopupDialog(version, mIsOSChange, mIsOlderVersion, hasEraseAllDataWarning, mDetailLayoutType, listener);
Tiago Costae29f4cd2014-10-10 18:30:59 +0100410 popupDialog.show(fm, version);
411 }
412
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000413 void startDownload()
Tiago Costa3add03e2014-12-16 12:05:23 +0000414 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000415 if (mIsVersion && mSelectedVersion != null)
Tiago Costa3add03e2014-12-16 12:05:23 +0000416 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000417 if (mIsOSChange || mIsOlderVersion)
Jose Pascoaldc29afe2014-10-10 13:43:47 +0100418 {
Maarten Derks4bb57892016-01-26 10:52:53 +0100419 showPopupDialog(mSelectedVersion.getHumanReadableName(), mSelectedVersion.hasEraseAllPartitionWarning(),
Jose Pascoal02d86242014-12-17 18:50:08 +0000420 new ConfirmationPopupDialogListener()
421 {
422
423 @Override
424 public void onFinishPopUpDialog(boolean isOk)
Jose Pascoalaa579a82014-11-05 22:17:16 +0000425 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000426 if (isOk)
Tiago Costa3add03e2014-12-16 12:05:23 +0000427 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000428 mainActivity.setSelectedVersion(mSelectedVersion);
429 showEraseAllDataWarning(true);
Tiago Costa3add03e2014-12-16 12:05:23 +0000430 }
Jose Pascoal02d86242014-12-17 18:50:08 +0000431 }
432 });
Tiago Costa3add03e2014-12-16 12:05:23 +0000433 }
434 else
435 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000436 mainActivity.setSelectedVersion(mSelectedVersion);
437 showEraseAllDataWarning(false);
Tiago Costa3add03e2014-12-16 12:05:23 +0000438 }
439 }
Jose Pascoal02d86242014-12-17 18:50:08 +0000440 else if (mSelectedStore != null)
441 {
442 mainActivity.setSelectedStore(mSelectedStore);
443 showStoreDisclaimer();
444 }
Tiago Costa3add03e2014-12-16 12:05:23 +0000445 }
Jose Pascoal02d86242014-12-17 18:50:08 +0000446
Jose Pascoal75392162014-10-15 18:29:01 +0100447 private void showEraseAllDataWarning(boolean bypassEraseAllWarning)
Pedro Arelo4b626232014-10-09 15:12:41 +0100448 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100449
Pedro Arelo4b626232014-10-09 15:12:41 +0100450 final UpdaterState currentState = mainActivity.getCurrentUpdaterState();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100451
Jose Pascoalaa579a82014-11-05 22:17:16 +0000452 if (mSelectedVersion != null && mSelectedVersion.hasEraseAllPartitionWarning() && !bypassEraseAllWarning)
Pedro Arelo4b626232014-10-09 15:12:41 +0100453 {
Pedro Arelo773bd822014-10-10 11:57:34 +0100454 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 +0100455 .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
456 {
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100457
Pedro Arelo4b626232014-10-09 15:12:41 +0100458 @Override
459 public void onClick(DialogInterface dialog, int which)
460 {
461 if (currentState == UpdaterState.NORMAL)
462 {
463 startUpdateDownload();
464 }
465 }
466 }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
467 {
468 public void onClick(DialogInterface dialog, int which)
469 {
470 mainActivity.setSelectedVersion(null);
471 }
472 }).show();
473 }
474 else
475 {
476 if (currentState == UpdaterState.NORMAL)
477 {
478 startUpdateDownload();
479 }
480 else
481 {
482 mainActivity.setSelectedVersion(null);
483 }
484 }
485 }
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100486
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000487 void showStoreDisclaimer()
Pedro Arelo4b626232014-10-09 15:12:41 +0100488 {
Jose Pascoal02d86242014-12-17 18:50:08 +0000489 final UpdaterState currentState = mainActivity.getCurrentUpdaterState();
Jose Pascoal54b3ae62014-10-07 20:29:58 +0100490
Jose Pascoal02d86242014-12-17 18:50:08 +0000491 if (mSelectedStore != null && mSelectedStore.showDisclaimer())
492 {
493 new AlertDialog.Builder(mainActivity).setTitle(R.string.google_apps_disclaimer_title).setMessage(R.string.google_apps_disclaimer_description)
494 .setPositiveButton(R.string.google_apps_disclaimer_agree, new DialogInterface.OnClickListener()
Pedro Arelo4b626232014-10-09 15:12:41 +0100495 {
Tiago Costa3add03e2014-12-16 12:05:23 +0000496
Jose Pascoal02d86242014-12-17 18:50:08 +0000497 @Override
498 public void onClick(DialogInterface dialog, int which)
499 {
500 if (currentState == UpdaterState.NORMAL)
501 {
502 startUpdateDownload();
503 }
504 }
505 }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
506 {
507 public void onClick(DialogInterface dialog, int which)
508 {
509 mainActivity.setSelectedStore(null);
510 }
511 }).show();
512 }
513 else
514 {
515 if (currentState == UpdaterState.NORMAL)
516 {
517 startUpdateDownload();
518 }
519 else
520 {
521 mainActivity.setSelectedStore(null);
522 }
523 }
524 }
Jose Pascoal91aff132014-10-02 17:19:47 +0100525}