From 92c8d9b142d2f6d839a7b710eecf4cf79708e18f Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Tue, 19 Jan 2016 11:17:54 +0100 Subject: [PATCH] Prevent potential crashes (based on Play Store console reports). --- .../app/settings/ApplicationSettings.java | 37 +++++++++------ .../transdroid/core/gui/DetailsFragment.java | 45 ++++++++++++------- .../core/gui/search/SearchActivity.java | 7 +-- 3 files changed, 57 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/transdroid/core/app/settings/ApplicationSettings.java b/app/src/main/java/org/transdroid/core/app/settings/ApplicationSettings.java index 65d88073..64d518af 100644 --- a/app/src/main/java/org/transdroid/core/app/settings/ApplicationSettings.java +++ b/app/src/main/java/org/transdroid/core/app/settings/ApplicationSettings.java @@ -43,6 +43,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.preference.PreferenceManager; +import android.text.TextUtils; /** * Singleton object to access all application settings, including stored servers, web search sites and RSS feeds. @@ -69,7 +70,7 @@ public class ApplicationSettings { * @return A list of all stored server settings objects */ public List getAllServerSettings() { - List all = new ArrayList(); + List all = new ArrayList<>(); all.addAll(getNormalServerSettings()); for (SeedboxProvider provider : SeedboxProvider.values()) { all.addAll(provider.getSettings().getAllServerSettings(prefs, all.size())); @@ -116,7 +117,7 @@ public class ApplicationSettings { * @return A list of all stored server settings objects */ public List getNormalServerSettings() { - List servers = new ArrayList(); + List servers = new ArrayList<>(); for (int i = 0; i <= getMaxNormalServer(); i++) { servers.add(getNormalServerSetting(i)); } @@ -145,19 +146,19 @@ public class ApplicationSettings { Daemon type = Daemon.fromCode(prefs.getString("server_type_" + order, null)); boolean ssl = prefs.getBoolean("server_sslenabled_" + order, false); - String port = prefs.getString("server_port_" + order, ""); - if (port.equals("")) + String port = prefs.getString("server_port_" + order, null); + if (TextUtils.isEmpty(port)) port = Integer.toString(Daemon.getDefaultPortNumber(type, ssl)); - String localPort = prefs.getString("server_localport_" + order, ""); - if (localPort.equals("")) + String localPort = prefs.getString("server_localport_" + order, null); + if (TextUtils.isEmpty(localPort)) localPort = port; // Default to the normal (non-local) port try { - Integer.parseInt(port); + parseInt(port, Daemon.getDefaultPortNumber(type, ssl)); } catch (NumberFormatException e) { port = Integer.toString(Daemon.getDefaultPortNumber(type, ssl)); } try { - Integer.parseInt(localPort); + parseInt(localPort, parseInt(port, Daemon.getDefaultPortNumber(type, ssl))); } catch (NumberFormatException e) { localPort = port; } @@ -167,9 +168,9 @@ public class ApplicationSettings { type, trim(prefs.getString("server_address_" + order, null)), trim(prefs.getString("server_localaddress_" + order, null)), - Integer.parseInt(localPort), + parseInt(localPort, parseInt(port, Daemon.getDefaultPortNumber(type, ssl))), prefs.getString("server_localnetwork_" + order, null), - Integer.parseInt(port), + parseInt(port, Daemon.getDefaultPortNumber(type, ssl)), ssl, prefs.getBoolean("server_ssltrustall_" + order, false), prefs.getString("server_ssltrustkey_" + order, null), @@ -182,7 +183,7 @@ public class ApplicationSettings { prefs.getString("server_downloaddir_" + order, null), prefs.getString("server_ftpurl_" + order, null), prefs.getString("server_ftppass_" + order, null), - Integer.parseInt(prefs.getString("server_timeout_" + order, "8")), + parseInt(prefs.getString("server_timeout_" + order, "8"), 8), prefs.getBoolean("server_alarmfinished_" + order, true), prefs.getBoolean("server_alarmnew_" + order, false), prefs.getString("server_alarmexclude_" + order, null), @@ -380,7 +381,7 @@ public class ApplicationSettings { * @return A list of all stored web search site settings objects */ public List getWebsearchSettings() { - List websearches = new ArrayList(); + List websearches = new ArrayList<>(); for (int i = 0; i <= getMaxWebsearch(); i++) { websearches.add(getWebsearchSetting(i)); } @@ -443,7 +444,7 @@ public class ApplicationSettings { * @return A list of all stored RSS feed settings objects */ public List getRssfeedSettings() { - List rssfeeds = new ArrayList(); + List rssfeeds = new ArrayList<>(); for (int i = 0; i <= getMaxRssfeed(); i++) { rssfeeds.add(getRssfeedSetting(i)); } @@ -566,7 +567,7 @@ public class ApplicationSettings { * @return A list of search settings, all of which are either a {@link SearchSite} or {@link WebsearchSetting} */ public List getSearchSettings() { - List all = new ArrayList(); + List all = new ArrayList<>(); all.addAll(searchHelper.getAvailableSites()); all.addAll(getWebsearchSettings()); return Collections.unmodifiableList(all); @@ -733,4 +734,12 @@ public class ApplicationSettings { return str.trim(); } + private int parseInt(String string, int defaultValue) { + try { + return Integer.parseInt(string); + } catch (NumberFormatException e) { + return defaultValue; + } + } + } diff --git a/app/src/main/java/org/transdroid/core/gui/DetailsFragment.java b/app/src/main/java/org/transdroid/core/gui/DetailsFragment.java index 66b88ac7..e5f3114d 100644 --- a/app/src/main/java/org/transdroid/core/gui/DetailsFragment.java +++ b/app/src/main/java/org/transdroid/core/gui/DetailsFragment.java @@ -182,8 +182,10 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen // Refresh the detailed statistics (errors) and list of files torrentDetails = null; torrentFiles = null; - getTasksExecutor().refreshTorrentDetails(torrent); - getTasksExecutor().refreshTorrentFiles(torrent); + if (getTasksExecutor() != null) { + getTasksExecutor().refreshTorrentDetails(torrent); + getTasksExecutor().refreshTorrentFiles(torrent); + } } /** @@ -366,42 +368,50 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen @OptionsItem(R.id.action_resume) protected void resumeTorrent() { - getTasksExecutor().resumeTorrent(torrent); + if (getTasksExecutor() != null) + getTasksExecutor().resumeTorrent(torrent); } @OptionsItem(R.id.action_pause) protected void pauseTorrent() { - getTasksExecutor().pauseTorrent(torrent); + if (getTasksExecutor() != null) + getTasksExecutor().pauseTorrent(torrent); } @OptionsItem(R.id.action_start_direct) protected void startTorrentDirect() { - getTasksExecutor().startTorrent(torrent, false); + if (getTasksExecutor() != null) + getTasksExecutor().startTorrent(torrent, false); } @OptionsItem(R.id.action_start_default) protected void startTorrentDefault() { - getTasksExecutor().startTorrent(torrent, false); + if (getTasksExecutor() != null) + getTasksExecutor().startTorrent(torrent, false); } @OptionsItem(R.id.action_start_forced) protected void startTorrentForced() { - getTasksExecutor().startTorrent(torrent, true); + if (getTasksExecutor() != null) + getTasksExecutor().startTorrent(torrent, true); } @OptionsItem(R.id.action_stop) protected void stopTorrent() { - getTasksExecutor().stopTorrent(torrent); + if (getTasksExecutor() != null) + getTasksExecutor().stopTorrent(torrent); } @OptionsItem(R.id.action_remove_default) protected void removeTorrentDefault() { - getTasksExecutor().removeTorrent(torrent, false); + if (getTasksExecutor() != null) + getTasksExecutor().removeTorrent(torrent, false); } @OptionsItem(R.id.action_remove_withdata) protected void removeTorrentWithData() { - getTasksExecutor().removeTorrent(torrent, true); + if (getTasksExecutor() != null) + getTasksExecutor().removeTorrent(torrent, true); } @OptionsItem(R.id.action_setlabel) @@ -413,7 +423,8 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen @OptionsItem(R.id.action_forcerecheck) protected void setForceRecheck() { - getTasksExecutor().forceRecheckTorrent(torrent); + if (getTasksExecutor() != null) + getTasksExecutor().forceRecheckTorrent(torrent); } @OptionsItem(R.id.action_updatetrackers) @@ -435,7 +446,8 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen if (torrent == null) { return; } - getTasksExecutor().updateLabel(torrent, newLabel); + if (getTasksExecutor() != null) + getTasksExecutor().updateLabel(torrent, newLabel); } @Override @@ -443,7 +455,8 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen if (torrent == null) { return; } - getTasksExecutor().updateTrackers(torrent, updatedTrackers); + if (getTasksExecutor() != null) + getTasksExecutor().updateTrackers(torrent, updatedTrackers); } @Override @@ -451,7 +464,8 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen if (torrent == null) { return; } - getTasksExecutor().updateLocation(torrent, newLocation); + if (getTasksExecutor() != null) + getTasksExecutor().updateLocation(torrent, newLocation); } @Click @@ -613,7 +627,8 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen if (itemId == R.id.action_priority_high) { priority = Priority.High; } - getTasksExecutor().updatePriority(torrent, checked, priority); + if (getTasksExecutor() != null) + getTasksExecutor().updatePriority(torrent, checked, priority); mode.finish(); return true; } diff --git a/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java b/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java index 97c13a05..f499839b 100644 --- a/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java +++ b/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java @@ -25,6 +25,7 @@ import android.os.Bundle; import android.provider.SearchRecentSuggestions; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AppCompatActivity; +import android.support.v7.view.ContextThemeWrapper; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; @@ -165,11 +166,11 @@ public class SearchActivity extends AppCompatActivity { searchToolbar.inflateMenu(R.menu.activity_search); // Add an expandable SearchView to the action bar MenuItem item = menu.findItem(R.id.action_search); - final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext()); + final SearchView searchView = new SearchView(searchToolbar.getContext()); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setQueryRefinementEnabled(true); - searchView.setIconified(false); - searchView.setIconifiedByDefault(false); + //searchView.setIconified(false); + //searchView.setIconifiedByDefault(false); MenuItemCompat.setActionView(item, searchView); searchMenu = item; return true;