From 773cc45dcde28e1671e35556a5db01dd818f5a06 Mon Sep 17 00:00:00 2001 From: Elad Keyshawn Date: Thu, 29 Dec 2016 17:12:56 +0200 Subject: [PATCH] reduced potential memory leaks --- src/org/kiwix/kiwixmobile/LibraryFragment.java | 3 ++- src/org/kiwix/kiwixmobile/ZimManageActivity.java | 4 ++++ .../kiwixmobile/downloader/DownloadFragment.java | 8 ++++---- .../kiwixmobile/downloader/DownloadService.java | 14 +++++++++----- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/org/kiwix/kiwixmobile/LibraryFragment.java b/src/org/kiwix/kiwixmobile/LibraryFragment.java index d6bc6e5bc..4ac2d7058 100644 --- a/src/org/kiwix/kiwixmobile/LibraryFragment.java +++ b/src/org/kiwix/kiwixmobile/LibraryFragment.java @@ -75,6 +75,7 @@ public class LibraryFragment extends Fragment implements AdapterView.OnItemClick private ZimManageActivity faActivity; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { faActivity = (ZimManageActivity) super.getActivity(); @@ -86,7 +87,7 @@ public class LibraryFragment extends Fragment implements AdapterView.OnItemClick // Don't use this method, it's handled by inflater.inflate() above : // setContentView(R.layout.activity_layout); ButterKnife.bind(this, llLayout); - + mService.setDownloadFragment(faActivity.mSectionsPagerAdapter.getDownloadFragment()); kiwixService = ((KiwixApplication) super.getActivity().getApplication()).getKiwixService(); conMan = (ConnectivityManager) super.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo network = conMan.getActiveNetworkInfo(); diff --git a/src/org/kiwix/kiwixmobile/ZimManageActivity.java b/src/org/kiwix/kiwixmobile/ZimManageActivity.java index 666ba54cc..fcd46b558 100644 --- a/src/org/kiwix/kiwixmobile/ZimManageActivity.java +++ b/src/org/kiwix/kiwixmobile/ZimManageActivity.java @@ -244,6 +244,10 @@ public class ZimManageActivity extends AppCompatActivity { private DownloadFragment downloadFragment = new DownloadFragment(); + public DownloadFragment getDownloadFragment() { + return downloadFragment; + } + public SectionsPagerAdapter(FragmentManager fm) { super(fm); } diff --git a/src/org/kiwix/kiwixmobile/downloader/DownloadFragment.java b/src/org/kiwix/kiwixmobile/downloader/DownloadFragment.java index 4a0067f18..1c68683cd 100644 --- a/src/org/kiwix/kiwixmobile/downloader/DownloadFragment.java +++ b/src/org/kiwix/kiwixmobile/downloader/DownloadFragment.java @@ -33,11 +33,11 @@ public class DownloadFragment extends Fragment { public static LinkedHashMap mDownloads = new LinkedHashMap<>(); public static LinkedHashMap mDownloadFiles = new LinkedHashMap<>(); public RelativeLayout relLayout; - public static ListView listView; + public ListView listView; public static DownloadAdapter downloadAdapter; private ZimManageActivity zimManageActivity; CoordinatorLayout mainLayout; - private static FragmentActivity faActivity; + private FragmentActivity faActivity; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -58,7 +58,7 @@ public class DownloadFragment extends Fragment { updateNoDownloads(); } - private static void updateNoDownloads() { + private void updateNoDownloads() { TextView noDownloadsText = (TextView) faActivity.findViewById(R.id.download_management_no_downloads); if (noDownloadsText == null) return; if (listView.getCount() == 0) { @@ -204,7 +204,7 @@ public class DownloadFragment extends Fragment { } - public static void addDownload(int position, LibraryNetworkEntity.Book book, String fileName) { + public void addDownload(int position, LibraryNetworkEntity.Book book, String fileName) { mDownloads.put(position, book); mDownloadFiles.put(position, fileName); downloadAdapter.notifyDataSetChanged(); diff --git a/src/org/kiwix/kiwixmobile/downloader/DownloadService.java b/src/org/kiwix/kiwixmobile/downloader/DownloadService.java index 95aa820f6..ba2372fec 100644 --- a/src/org/kiwix/kiwixmobile/downloader/DownloadService.java +++ b/src/org/kiwix/kiwixmobile/downloader/DownloadService.java @@ -61,17 +61,21 @@ public class DownloadService extends Service { private static String SD_CARD; public static String KIWIX_ROOT; public static int notificationCount = 1; - public static ArrayList notifications = new ArrayList(); + public static ArrayList notifications = new ArrayList<>(); public String notificationTitle; private HashMap notification = new HashMap<>(); private NotificationManager notificationManager; - public HashMap downloadStatus = new HashMap(); - public HashMap downloadProgress = new HashMap(); + public HashMap downloadStatus = new HashMap<>(); + public HashMap downloadProgress = new HashMap<>(); public static Object pauseLock = new Object(); public static BookDao bookDao; - + private static DownloadFragment downloadFragment; Handler handler = new Handler(Looper.getMainLooper()); + public static void setDownloadFragment(DownloadFragment dFragment) { + downloadFragment = dFragment; + } + @Override public void onCreate() { kiwixService = ((KiwixApplication) getApplication()).getKiwixService(); @@ -158,7 +162,7 @@ public class DownloadService extends Service { } private void downloadBook(String url, int notificationID, LibraryNetworkEntity.Book book) { - DownloadFragment.addDownload(notificationID, book, KIWIX_ROOT + StorageUtils.getFileNameFromUrl(book.getUrl())); + downloadFragment.addDownload(notificationID, book, KIWIX_ROOT + StorageUtils.getFileNameFromUrl(book.getUrl())); kiwixService.getMetaLinks(url) .subscribeOn(AndroidSchedulers.mainThread()) .flatMap(metaLink -> getMetaLinkContentLength(metaLink.getRelevantUrl().getValue()))