Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.50 KB | None | 0 0
  1. package com.miz.loader;/*
  2. * Copyright (C) 2014 Michell Bak
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. import android.content.Context;
  18. import android.content.SharedPreferences;
  19. import android.preference.PreferenceManager;
  20.  
  21. import com.miz.db.DbAdapterTvShowEpisodes;
  22. import com.miz.functions.FileSource;
  23. import com.miz.functions.Filepath;
  24. import com.miz.functions.GridEpisode;
  25. import com.miz.functions.LibrarySectionAsyncTask;
  26. import com.miz.functions.MizLib;
  27. import com.miz.mizuu.MizuuApplication;
  28. import com.miz.mizuu.R;
  29. import com.miz.utils.FileUtils;
  30.  
  31. import java.io.File;
  32. import java.util.ArrayList;
  33. import java.util.Collections;
  34. import java.util.Comparator;
  35.  
  36. import jcifs.smb.SmbFile;
  37.  
  38. import static com.miz.functions.PreferenceKeys.TVSHOWS_EPISODE_ORDER;
  39.  
  40. public class TvShowEpisodeLoader {
  41.  
  42. public enum TvShowEpisodeSortType {
  43. ASCENDING(SORT_ASCENDING),
  44. DESCENDING(SORT_DESCENDING);
  45.  
  46. private final int mType;
  47.  
  48. TvShowEpisodeSortType(int type) {
  49. mType = type;
  50. }
  51.  
  52. public Comparator<GridEpisode> getComparator() {
  53. return new Comparator<GridEpisode>() {
  54. @Override
  55. public int compare(GridEpisode lhs, GridEpisode rhs) {
  56. int multiplier = mType == SORT_ASCENDING ? 1 : -1;
  57.  
  58. // Regular sorting
  59. if (lhs.getEpisode() < rhs.getEpisode())
  60. return -1 * multiplier;
  61. if (lhs.getEpisode() > rhs.getEpisode())
  62. return multiplier;
  63. return 0;
  64. }
  65. };
  66. }
  67. }
  68.  
  69. public enum TvShowEpisodeWatchedFilter {
  70. ALL, WATCHED, UNWATCHED
  71. }
  72.  
  73. public static final int SORT_ASCENDING = 0, SORT_DESCENDING = 1;
  74.  
  75. private final Context mContext;
  76. private final OnLoadCompletedCallback mCallback;
  77. private final String mShowId;
  78. private final int mShowSeason;
  79. private final DbAdapterTvShowEpisodes mTvShowEpisodeDatabase;
  80.  
  81. private TvShowEpisodeWatchedFilter mWatchedFilter;
  82. private ArrayList<GridEpisode> mResults = new ArrayList<>();
  83. private TvShowEpisodeSortType mSortType;
  84. private TvShowEpisodeLoaderAsyncTask mAsyncTask;
  85. private boolean mShowAvailableFiles = false;
  86.  
  87. public TvShowEpisodeLoader(Context context, String showId, int showSeason, OnLoadCompletedCallback callback) {
  88. mContext = context;
  89. mCallback = callback;
  90. mShowId = showId;
  91. mShowSeason = showSeason;
  92. mTvShowEpisodeDatabase = MizuuApplication.getTvEpisodeDbAdapter();
  93.  
  94. setWatchedFilter(TvShowEpisodeWatchedFilter.ALL);
  95. setupSortType();
  96. }
  97.  
  98. public String getShowId() {
  99. return mShowId;
  100. }
  101.  
  102. public int getShowSeason() {
  103. return mShowSeason;
  104. }
  105.  
  106. public TvShowEpisodeSortType getSortType() {
  107. return mSortType;
  108. }
  109.  
  110. public void setSortType(TvShowEpisodeSortType type) {
  111. mSortType = type;
  112.  
  113. SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
  114. editor.putString(TVSHOWS_EPISODE_ORDER, getSortType() == TvShowEpisodeSortType.ASCENDING ?
  115. mContext.getString(R.string.oldestFirst) : mContext.getString(R.string.newestFirst));
  116. editor.apply();
  117. }
  118.  
  119. public void setWatchedFilter(TvShowEpisodeWatchedFilter filter) {
  120. mWatchedFilter = filter;
  121. }
  122.  
  123. public TvShowEpisodeWatchedFilter getWatchedFilter() {
  124. return mWatchedFilter;
  125. }
  126.  
  127. public void setShowAvailableFiles(boolean showAvailableFiles) {
  128. mShowAvailableFiles = showAvailableFiles;
  129. }
  130.  
  131. public boolean showAvailableFiles() {
  132. return mShowAvailableFiles;
  133. }
  134.  
  135. public ArrayList<GridEpisode> getResults() {
  136. return mResults;
  137. }
  138.  
  139. public void load() {
  140. if (mAsyncTask != null) {
  141. mAsyncTask.cancel(true);
  142. }
  143.  
  144. mAsyncTask = new TvShowEpisodeLoaderAsyncTask();
  145. mAsyncTask.execute();
  146. }
  147.  
  148. /**
  149. * Handles everything related to loading, filtering, sorting
  150. * and delivering the callback when everything is finished.
  151. */
  152. private class TvShowEpisodeLoaderAsyncTask extends LibrarySectionAsyncTask<Void, Void, Void> {
  153.  
  154. private final ArrayList<GridEpisode> mEpisodeList;
  155.  
  156. public TvShowEpisodeLoaderAsyncTask() {
  157. mEpisodeList = new ArrayList<GridEpisode>();
  158. }
  159.  
  160. @Override
  161. protected Void doInBackground(Void... params) {
  162.  
  163. mEpisodeList.addAll(MizuuApplication.getTvEpisodeDbAdapter()
  164. .getEpisodesInSeason(mContext, getShowId(), getShowSeason()));
  165.  
  166. int totalSize = mEpisodeList.size();
  167.  
  168. switch (getWatchedFilter()) {
  169. case WATCHED:
  170. for (int i = 0; i < totalSize; i++) {
  171. if (!mEpisodeList.get(i).hasWatched()) {
  172. mEpisodeList.remove(i);
  173. i--;
  174. totalSize--;
  175. }
  176. }
  177. break;
  178.  
  179. case UNWATCHED:
  180. for (int i = 0; i < totalSize; i++) {
  181. if (mEpisodeList.get(i).hasWatched()) {
  182. mEpisodeList.remove(i);
  183. i--;
  184. totalSize--;
  185. }
  186. }
  187. break;
  188.  
  189. default:
  190. break;
  191.  
  192. }
  193.  
  194. if (showAvailableFiles()) {
  195. for (int i = 0; i < totalSize; i++) {
  196. ArrayList<FileSource> filesources = MizLib.getFileSources(MizLib.TYPE_SHOWS, true);
  197.  
  198. if (isCancelled())
  199. return null;
  200.  
  201. boolean condition = false;
  202.  
  203. for (Filepath path : mEpisodeList.get(i).getFilepaths()) {
  204. if (path.isNetworkFile())
  205. if (FileUtils.hasOfflineCopy(mContext, path)) {
  206. condition = true;
  207. break; // break inner loop to continue to the next episode
  208. } else {
  209. if (path.getType() == FileSource.SMB) {
  210. if (MizLib.isWifiConnected(mContext)) {
  211. FileSource source = null;
  212.  
  213. for (int j = 0; j < filesources.size(); j++)
  214. if (path.getFilepath().contains(filesources.get(j).getFilepath())) {
  215. source = filesources.get(j);
  216. break;
  217. }
  218.  
  219. if (source == null)
  220. continue;
  221.  
  222. try {
  223. final SmbFile file = new SmbFile(
  224. MizLib.createSmbLoginString(
  225. source.getDomain(),
  226. source.getUser(),
  227. source.getPassword(),
  228. path.getFilepath(),
  229. false
  230. ));
  231. if (file.exists()) {
  232. condition = true;
  233. break; // break inner loop to continue to the next episode
  234. }
  235. } catch (Exception e) {
  236. } // Do nothing - the file isn't available (either MalformedURLException or SmbException)
  237. }
  238. } else if (path.getType() == FileSource.UPNP) {
  239. if (MizLib.exists(path.getFilepath())) {
  240. condition = true;
  241. break; // break inner loop to continue to the next episode
  242. }
  243. }
  244. }
  245. else {
  246. if (new File(path.getFilepath()).exists()) {
  247. condition = true;
  248. break; // break inner loop to continue to the next episode
  249. }
  250. }
  251. }
  252.  
  253. if (!condition && mEpisodeList.size() > i) {
  254. mEpisodeList.remove(i);
  255. i--;
  256. totalSize--;
  257. }
  258. }
  259. }
  260.  
  261. Collections.sort(mEpisodeList, getSortType().getComparator());
  262.  
  263. return null;
  264. }
  265.  
  266. @Override
  267. protected void onPostExecute(Void result) {
  268. if (!isCancelled()) {
  269. mResults = new ArrayList<>(mEpisodeList);
  270. mCallback.onLoadCompleted();
  271. } else
  272. mEpisodeList.clear();
  273. }
  274. }
  275.  
  276. private void setupSortType() {
  277. boolean ascending = PreferenceManager.getDefaultSharedPreferences(mContext)
  278. .getString(TVSHOWS_EPISODE_ORDER, mContext.getString(R.string.oldestFirst))
  279. .equals(mContext.getString(R.string.oldestFirst));
  280.  
  281. if (ascending) {
  282. setSortType(TvShowEpisodeSortType.ASCENDING);
  283. } else {
  284. setSortType(TvShowEpisodeSortType.DESCENDING);
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement