Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.56 KB | None | 0 0
  1. /*
  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. package com.miz.mizuu.fragments;
  18.  
  19. import android.animation.Animator;
  20. import android.annotation.SuppressLint;
  21. import android.app.Activity;
  22. import android.app.AlertDialog;
  23. import android.app.Dialog;
  24. import android.content.BroadcastReceiver;
  25. import android.content.Context;
  26. import android.content.DialogInterface;
  27. import android.content.Intent;
  28. import android.content.IntentFilter;
  29. import android.database.Cursor;
  30. import android.graphics.Color;
  31. import android.graphics.Typeface;
  32. import android.net.Uri;
  33. import android.os.Bundle;
  34. import android.preference.PreferenceManager;
  35. import android.support.v4.app.Fragment;
  36. import android.support.v4.content.LocalBroadcastManager;
  37. import android.text.Html;
  38. import android.text.TextUtils;
  39. import android.view.LayoutInflater;
  40. import android.view.Menu;
  41. import android.view.MenuInflater;
  42. import android.view.MenuItem;
  43. import android.view.View;
  44. import android.view.View.OnClickListener;
  45. import android.view.ViewGroup;
  46. import android.view.ViewTreeObserver;
  47. import android.widget.CheckBox;
  48. import android.widget.ImageView;
  49. import android.widget.ScrollView;
  50. import android.widget.TextView;
  51. import android.widget.Toast;
  52.  
  53. import com.melnykov.fab.FloatingActionButton;
  54. import com.miz.apis.trakt.Trakt;
  55. import com.miz.db.DbAdapterTvShowEpisodes;
  56. import com.miz.functions.BlurTransformation;
  57. import com.miz.functions.FileSource;
  58. import com.miz.functions.Filepath;
  59. import com.miz.functions.MizLib;
  60. import com.miz.functions.PaletteLoader;
  61. import com.miz.functions.SimpleAnimatorListener;
  62. import com.miz.mizuu.EditTvShowEpisode;
  63. import com.miz.mizuu.IdentifyTvShowEpisode;
  64. import com.miz.mizuu.Main;
  65. import com.miz.mizuu.MizuuApplication;
  66. import com.miz.mizuu.R;
  67. import com.miz.mizuu.TvShowEpisode;
  68. import com.miz.remoteplayback.RemotePlayback;
  69. import com.miz.service.DeleteFile;
  70. import com.miz.service.MakeAvailableOffline;
  71. import com.miz.utils.LocalBroadcastUtils;
  72. import com.miz.utils.TvShowDatabaseUtils;
  73. import com.miz.utils.TypefaceUtils;
  74. import com.miz.utils.VideoUtils;
  75. import com.miz.utils.ViewUtils;
  76. import com.miz.views.ObservableScrollView;
  77. import com.miz.views.ObservableScrollView.OnScrollChangedListener;
  78. import com.squareup.otto.Bus;
  79. import com.squareup.picasso.Callback;
  80. import com.squareup.picasso.Picasso;
  81.  
  82. import java.util.ArrayList;
  83.  
  84. import static com.miz.functions.PreferenceKeys.ALWAYS_DELETE_FILE;
  85. import static com.miz.functions.PreferenceKeys.CHROMECAST_BETA_SUPPORT;
  86. import static com.miz.functions.PreferenceKeys.SHOW_FILE_LOCATION;
  87.  
  88. @SuppressLint("InflateParams") public class TvShowEpisodeDetailsFragment extends Fragment {
  89.  
  90. private Activity mContext;
  91. private TvShowEpisode mEpisode;
  92. private ImageView mBackdrop, mEpisodePhoto;
  93. private TextView mTitle, mDescription, mFileSource, mAirDate, mRating, mDirector, mWriter, mGuestStars, mSeasonEpisodeNumber;
  94. private View mDetailsArea;
  95. private Picasso mPicasso;
  96. private Typeface mMediumItalic, mMedium, mCondensedRegular;
  97. private DbAdapterTvShowEpisodes mDatabaseHelper;
  98. private long mVideoPlaybackStarted, mVideoPlaybackEnded;
  99. private boolean mShowFileLocation;
  100. private Bus mBus;
  101. private int mToolbarColor = 0;
  102. private FloatingActionButton mFab;
  103. private PaletteLoader mPaletteLoader;
  104. private ObservableScrollView mScrollView;
  105.  
  106. /**
  107. * Empty constructor as per the Fragment documentation
  108. */
  109. public TvShowEpisodeDetailsFragment() {}
  110.  
  111. public static TvShowEpisodeDetailsFragment newInstance(String showId, int season, int episode) {
  112. TvShowEpisodeDetailsFragment pageFragment = new TvShowEpisodeDetailsFragment();
  113. Bundle bundle = new Bundle();
  114. bundle.putString("showId", showId);
  115. bundle.putInt("season", season);
  116. bundle.putInt("episode", episode);
  117. pageFragment.setArguments(bundle);
  118. return pageFragment;
  119. }
  120.  
  121. @Override
  122. public void onCreate(Bundle savedInstanceState) {
  123. super.onCreate(savedInstanceState);
  124.  
  125. setRetainInstance(true);
  126. setHasOptionsMenu(true);
  127.  
  128. mContext = getActivity();
  129.  
  130. mBus = MizuuApplication.getBus();
  131.  
  132. mShowFileLocation = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean(SHOW_FILE_LOCATION, true);
  133.  
  134. mPicasso = MizuuApplication.getPicassoDetailsView(getActivity());
  135.  
  136. mMediumItalic = TypefaceUtils.getRobotoMediumItalic(mContext);
  137. mMedium = TypefaceUtils.getRobotoMedium(mContext);
  138. mCondensedRegular = TypefaceUtils.getRobotoCondensedRegular(mContext);
  139.  
  140. mDatabaseHelper = MizuuApplication.getTvEpisodeDbAdapter();
  141.  
  142. LocalBroadcastManager.getInstance(mContext).registerReceiver(mBroadcastReceiver,
  143. new IntentFilter(LocalBroadcastUtils.UPDATE_TV_SHOW_EPISODE_DETAILS_OVERVIEW));
  144.  
  145. loadEpisode();
  146. }
  147.  
  148. @Override
  149. public void onDestroy() {
  150. super.onDestroy();
  151.  
  152. LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mBroadcastReceiver);
  153. }
  154.  
  155. private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
  156. @Override
  157. public void onReceive(Context context, Intent intent) {
  158. loadEpisode();
  159. loadData();
  160. }
  161. };
  162.  
  163. private void loadEpisode() {
  164. if (!getArguments().getString("showId").isEmpty() && getArguments().getInt("season") >= 0 && getArguments().getInt("episode") >= 0) {
  165. Cursor cursor = mDatabaseHelper.getEpisode(getArguments().getString("showId"), getArguments().getInt("season"), getArguments().getInt("episode"));
  166.  
  167. if (cursor.moveToFirst()) {
  168. mEpisode = new TvShowEpisode(getActivity(),
  169. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_SHOW_ID)),
  170. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_EPISODE_TITLE)),
  171. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_EPISODE_PLOT)),
  172. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_SEASON)),
  173. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_EPISODE)),
  174. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_EPISODE_AIRDATE)),
  175. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_EPISODE_DIRECTOR)),
  176. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_EPISODE_WRITER)),
  177. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_EPISODE_GUESTSTARS)),
  178. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_EPISODE_RATING)),
  179. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_HAS_WATCHED)),
  180. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_FAVOURITE))
  181. );
  182.  
  183. mEpisode.setFilepaths(MizuuApplication.getTvShowEpisodeMappingsDbAdapter().getFilepathsForEpisode(
  184. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_SHOW_ID)),
  185. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_SEASON)),
  186. cursor.getString(cursor.getColumnIndex(DbAdapterTvShowEpisodes.KEY_EPISODE))
  187. ));
  188. }
  189. cursor.close();
  190. }
  191. }
  192.  
  193. @Override
  194. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  195. return inflater.inflate(R.layout.episode_details, container, false);
  196. }
  197.  
  198. @Override
  199. public void onViewCreated(final View view, Bundle savedInstanceState) {
  200. super.onViewCreated(view, savedInstanceState);
  201.  
  202. mBackdrop = (ImageView) view.findViewById(R.id.imageBackground);
  203. mEpisodePhoto = (ImageView) view.findViewById(R.id.episodePhoto);
  204. mDetailsArea = view.findViewById(R.id.details_area);
  205.  
  206. mTitle = (TextView) view.findViewById(R.id.movieTitle);
  207. mSeasonEpisodeNumber = (TextView) view.findViewById(R.id.textView7);
  208. mDescription = (TextView) view.findViewById(R.id.textView2);
  209. mFileSource = (TextView) view.findViewById(R.id.textView3);
  210. mAirDate = (TextView) view.findViewById(R.id.textReleaseDate);
  211. mRating = (TextView) view.findViewById(R.id.textView12);
  212. mDirector = (TextView) view.findViewById(R.id.director);
  213. mWriter = (TextView) view.findViewById(R.id.writer);
  214. mGuestStars = (TextView) view.findViewById(R.id.guest_stars);
  215. mScrollView = (ObservableScrollView) view.findViewById(R.id.observableScrollView);
  216. mFab = (FloatingActionButton) view.findViewById(R.id.fab);
  217.  
  218. mFab.setOnClickListener(new OnClickListener() {
  219. @Override
  220. public void onClick(View v) {
  221. ViewUtils.animateFabJump(v, new SimpleAnimatorListener() {
  222. @Override
  223. public void onAnimationEnd(Animator animation) {
  224. play();
  225. }
  226. });
  227. }
  228. });
  229. if (MizLib.isTablet(mContext))
  230. mFab.setType(FloatingActionButton.TYPE_NORMAL);
  231.  
  232. final int height = MizLib.getActionBarAndStatusBarHeight(getActivity());
  233.  
  234. mScrollView = (ObservableScrollView) view.findViewById(R.id.observableScrollView);
  235. mScrollView.setOnScrollChangedListener(new OnScrollChangedListener() {
  236. @Override
  237. public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
  238. final int headerHeight = mEpisodePhoto.getHeight() - height;
  239. final float ratio = (float) Math.min(Math.max(t, 0), headerHeight) / headerHeight;
  240. final int newAlpha = (int) (ratio * 255);
  241.  
  242. mBus.post(new BusToolbarColorObject(mToolbarColor, newAlpha));
  243.  
  244. if (MizLib.isPortrait(mContext)) {
  245. // Such parallax, much wow
  246. mEpisodePhoto.setPadding(0, (int) (t / 1.5), 0, 0);
  247. }
  248. }
  249. });
  250. mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  251. @Override
  252. public void onGlobalLayout() {
  253. ViewUtils.setLayoutParamsForDetailsEmptyView(mContext, view,
  254. mBackdrop, mScrollView, this);
  255. }
  256. });
  257.  
  258. loadData();
  259.  
  260. mPicasso.load(mEpisode.getEpisodePhoto()).placeholder(R.drawable.bg).config(MizuuApplication.getBitmapConfig()).into(mEpisodePhoto, new Callback() {
  261. @Override
  262. public void onError() {
  263. if (!isAdded())
  264. return;
  265. int width = getActivity().getResources().getDimensionPixelSize(R.dimen.episode_details_background_overlay_width);
  266. int height = getActivity().getResources().getDimensionPixelSize(R.dimen.episode_details_background_overlay_height);
  267. mPicasso.load(mEpisode.getTvShowBackdrop()).placeholder(R.drawable.bg).error(R.drawable.nobackdrop).resize(width, height).config(MizuuApplication.getBitmapConfig()).into(mEpisodePhoto);
  268. }
  269.  
  270. @Override
  271. public void onSuccess() {
  272. if (mPaletteLoader == null) {
  273. mPaletteLoader = new PaletteLoader(mPicasso, Uri.fromFile(mEpisode.getEpisodePhoto()), new PaletteLoader.OnPaletteLoadedCallback() {
  274. @Override
  275. public void onPaletteLoaded(int swatchColor) {
  276. mToolbarColor = swatchColor;
  277. }
  278. });
  279.  
  280. mPaletteLoader.addView(mDetailsArea);
  281. mPaletteLoader.setFab(mFab);
  282.  
  283. mPaletteLoader.execute();
  284. } else {
  285. // Clear old views after configuration change
  286. mPaletteLoader.clearViews();
  287.  
  288. // Add views after configuration change
  289. mPaletteLoader.addView(mDetailsArea);
  290. mPaletteLoader.setFab(mFab);
  291.  
  292. // Re-color the views
  293. mPaletteLoader.colorViews();
  294. }
  295. }
  296. });
  297.  
  298. if (!MizLib.isPortrait(getActivity()))
  299. mPicasso.load(mEpisode.getEpisodePhoto()).placeholder(R.drawable.bg).error(R.drawable.bg).transform(new BlurTransformation(getActivity().getApplicationContext(), mEpisode.getEpisodePhoto().getAbsolutePath() + "-blur", 4)).into(mBackdrop, new Callback() {
  300. @Override public void onError() {
  301. if (!isAdded())
  302. return;
  303.  
  304. mPicasso.load(mEpisode.getTvShowBackdrop()).placeholder(R.drawable.bg).error(R.drawable.nobackdrop).transform(new BlurTransformation(getActivity().getApplicationContext(), mEpisode.getTvShowBackdrop().getAbsolutePath() + "-blur", 4)).into(mBackdrop, new Callback() {
  305. @Override
  306. public void onError() {}
  307.  
  308. @Override
  309. public void onSuccess() {
  310. if (!isAdded())
  311. return;
  312. mBackdrop.setColorFilter(Color.parseColor("#aa181818"), android.graphics.PorterDuff.Mode.SRC_OVER);
  313. }
  314. });
  315. }
  316.  
  317. @Override
  318. public void onSuccess() {
  319. if (!isAdded())
  320. return;
  321. mBackdrop.setColorFilter(Color.parseColor("#aa181818"), android.graphics.PorterDuff.Mode.SRC_OVER);
  322. }
  323. });
  324. }
  325.  
  326. private void loadData() {
  327. // Set the episode title
  328. mTitle.setVisibility(View.VISIBLE);
  329. mTitle.setText(mEpisode.getTitle());
  330. mTitle.setTypeface(mCondensedRegular);
  331.  
  332. mDescription.setTypeface(mCondensedRegular);
  333. mFileSource.setTypeface(mCondensedRegular);
  334. mDirector.setTypeface(mCondensedRegular);
  335. mWriter.setTypeface(mCondensedRegular);
  336. mGuestStars.setTypeface(mCondensedRegular);
  337.  
  338. mAirDate.setTypeface(mMedium);
  339. mRating.setTypeface(mMedium);
  340. mSeasonEpisodeNumber.setTypeface(mMediumItalic);
  341. mSeasonEpisodeNumber.setText(getString(R.string.showSeason) + " " + mEpisode.getSeason() + ", " + getString(R.string.showEpisode) + " " + mEpisode.getEpisode());
  342.  
  343. // Set the movie plot
  344. if (!MizLib.isPortrait(getActivity())) {
  345. mDescription.setBackgroundResource(R.drawable.selectable_background);
  346. mDescription.setMaxLines(getActivity().getResources().getInteger(R.integer.episode_details_max_lines));
  347. mDescription.setTag(true); // true = collapsed
  348. mDescription.setOnClickListener(new OnClickListener() {
  349. @Override
  350. public void onClick(View v) {
  351. if (((Boolean) mDescription.getTag())) {
  352. mDescription.setMaxLines(1000);
  353. mDescription.setTag(false);
  354. } else {
  355. mDescription.setMaxLines(getActivity().getResources().getInteger(R.integer.episode_details_max_lines));
  356. mDescription.setTag(true);
  357. }
  358. }
  359. });
  360. mDescription.setEllipsize(TextUtils.TruncateAt.END);
  361. mDescription.setFocusable(true);
  362. } else {
  363. if (MizLib.isTablet(getActivity()))
  364. mDescription.setLineSpacing(0, 1.15f);
  365. }
  366. mDescription.setText(mEpisode.getDescription());
  367.  
  368. if (mShowFileLocation) {
  369. mFileSource.setText(mEpisode.getAllFilepaths());
  370. } else {
  371. mFileSource.setVisibility(View.GONE);
  372. }
  373.  
  374. // Set the episode air date
  375. mAirDate.setText(MizLib.getPrettyDatePrecise(getActivity(), mEpisode.getReleasedate()));
  376.  
  377. // Set the movie rating
  378. if (!mEpisode.getRating().equals("0.0")) {
  379. try {
  380. int rating = (int) (Double.parseDouble(mEpisode.getRating()) * 10);
  381. mRating.setText(Html.fromHtml(rating + "<small> %</small>"));
  382. } catch (NumberFormatException e) {
  383. mRating.setText(mEpisode.getRating());
  384. }
  385. } else {
  386. mRating.setText(R.string.stringNA);
  387. }
  388.  
  389. if (TextUtils.isEmpty(mEpisode.getDirector()) || mEpisode.getDirector().equals(getString(R.string.stringNA))) {
  390. mDirector.setVisibility(View.GONE);
  391. } else {
  392. mDirector.setText(mEpisode.getDirector());
  393. }
  394.  
  395. if (TextUtils.isEmpty(mEpisode.getWriter()) || mEpisode.getWriter().equals(getString(R.string.stringNA))) {
  396. mWriter.setVisibility(View.GONE);
  397. } else {
  398. mWriter.setText(mEpisode.getWriter());
  399. }
  400.  
  401. if (TextUtils.isEmpty(mEpisode.getGuestStars()) || mEpisode.getGuestStars().equals(getString(R.string.stringNA))) {
  402. mGuestStars.setVisibility(View.GONE);
  403. } else {
  404. mGuestStars.setText(mEpisode.getGuestStars());
  405. }
  406. }
  407.  
  408. private void play() {
  409. ArrayList<Filepath> paths = mEpisode.getFilepaths();
  410. if (paths.size() == 1) {
  411. Filepath path = paths.get(0);
  412. if (mEpisode.hasOfflineCopy(path)) {
  413. boolean playbackStarted = VideoUtils.playVideo(getActivity(), mEpisode.getOfflineCopyUri(path), FileSource.FILE, mEpisode);
  414. if (playbackStarted) {
  415. mVideoPlaybackStarted = System.currentTimeMillis();
  416. checkIn();
  417. }
  418. } else {
  419. boolean playbackStarted = VideoUtils.playVideo(getActivity(), path.getFilepath(), path.getType(), mEpisode);
  420. if (playbackStarted) {
  421. mVideoPlaybackStarted = System.currentTimeMillis();
  422. checkIn();
  423. }
  424. }
  425. } else {
  426. boolean hasOfflineCopy = false;
  427. for (Filepath path : paths) {
  428. if (mEpisode.hasOfflineCopy(path)) {
  429. boolean playbackStarted = VideoUtils.playVideo(getActivity(), mEpisode.getOfflineCopyUri(path), FileSource.FILE, mEpisode);
  430. if (playbackStarted) {
  431. mVideoPlaybackStarted = System.currentTimeMillis();
  432. checkIn();
  433. }
  434.  
  435. hasOfflineCopy = true;
  436. break;
  437. }
  438. }
  439.  
  440. if (!hasOfflineCopy) {
  441. MizLib.showSelectFileDialog(getActivity(), mEpisode.getFilepaths(), new Dialog.OnClickListener() {
  442. @Override
  443. public void onClick(DialogInterface dialog, int which) {
  444. Filepath path = mEpisode.getFilepaths().get(which);
  445. boolean playbackStarted = VideoUtils.playVideo(getActivity(), path.getFilepath(), path.getType(), mEpisode);
  446. if (playbackStarted) {
  447. mVideoPlaybackStarted = System.currentTimeMillis();
  448. checkIn();
  449. }
  450. }
  451. });
  452. }
  453. }
  454. }
  455.  
  456. public void onResume() {
  457. super.onResume();
  458.  
  459. mBus.register(getActivity());
  460.  
  461. mVideoPlaybackEnded = System.currentTimeMillis();
  462.  
  463. if (mVideoPlaybackStarted > 0 && mVideoPlaybackEnded - mVideoPlaybackStarted > (1000 * 60 * 5)) {
  464. if (!mEpisode.hasWatched())
  465. watched(false); // Mark it as watched
  466. }
  467. }
  468.  
  469. @Override
  470. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  471. inflater.inflate(R.menu.episode_details, menu);
  472.  
  473. if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean(CHROMECAST_BETA_SUPPORT, false)) {
  474.  
  475. boolean add = false;
  476. for (Filepath path : mEpisode.getFilepaths()) {
  477. if (path.isNetworkFile()) {
  478. add = true;
  479. break;
  480. }
  481. }
  482.  
  483. if (add) {
  484. menu.add("Remote play").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
  485. @Override
  486. public boolean onMenuItemClick(MenuItem item) {
  487. final ArrayList<Filepath> networkFiles = new ArrayList<Filepath>();
  488.  
  489. for (Filepath path : mEpisode.getFilepaths()) {
  490. if (path.isNetworkFile()) {
  491. networkFiles.add(path);
  492. }
  493. }
  494.  
  495. MizLib.showSelectFileDialog(getActivity(), networkFiles, new DialogInterface.OnClickListener() {
  496. @Override
  497. public void onClick(DialogInterface dialog, int which) {
  498.  
  499. String showName = MizuuApplication.getTvDbAdapter().getShowTitle(mEpisode.getShowId());
  500.  
  501. Intent i = new Intent(getActivity(), RemotePlayback.class);
  502. i.putExtra("coverUrl", "");
  503. i.putExtra("title", showName + " (S" + MizLib.addIndexZero(mEpisode.getSeason()) + "E" + MizLib.addIndexZero(mEpisode.getEpisode()) + "): " + mEpisode.getTitle());
  504. i.putExtra("id", mEpisode.getShowId());
  505. i.putExtra("type", "tv");
  506.  
  507. if (networkFiles.get(which).getType() == FileSource.SMB) {
  508. String url = VideoUtils.startSmbServer(getActivity(), networkFiles.get(which).getFilepath(), mEpisode);
  509. i.putExtra("videoUrl", url);
  510. } else {
  511. i.putExtra("videoUrl", networkFiles.get(which).getFilepath());
  512. }
  513.  
  514. startActivity(i);
  515. }
  516. });
  517.  
  518. return false;
  519. }
  520. });
  521. }
  522. }
  523.  
  524. try {
  525. if (mEpisode.hasWatched()) {
  526. menu.findItem(R.id.watched).setTitle(R.string.stringMarkAsUnwatched);
  527. } else {
  528. menu.findItem(R.id.watched).setTitle(R.string.stringMarkAsWatched);
  529. }
  530.  
  531. for (Filepath path : mEpisode.getFilepaths()) {
  532. if (path.isNetworkFile()) {
  533.  
  534. // Set the menu item visibility
  535. menu.findItem(R.id.watchOffline).setVisible(true);
  536.  
  537. if (mEpisode.hasOfflineCopy(path))
  538. // There's already an offline copy, so let's allow the user to remove it
  539. menu.findItem(R.id.watchOffline).setTitle(R.string.removeOfflineCopy);
  540. else
  541. // There's no offline copy, so let the user download one
  542. menu.findItem(R.id.watchOffline).setTitle(R.string.watchOffline);
  543.  
  544. break;
  545. }
  546. }
  547. } catch (Exception e) {}
  548. }
  549.  
  550. @Override
  551. public boolean onOptionsItemSelected(MenuItem item) {
  552. switch (item.getItemId()) {
  553. case R.id.menuDeleteEpisode:
  554. deleteEpisode();
  555. break;
  556. case R.id.watched:
  557. watched(true);
  558. break;
  559. case R.id.identify:
  560. identifyEpisode();
  561. break;
  562. case R.id.watchOffline:
  563. watchOffline();
  564. break;
  565. case R.id.editTvShowEpisode:
  566. editEpisode();
  567. break;
  568. }
  569. return false;
  570. }
  571.  
  572. private void editEpisode() {
  573. Intent intent = new Intent(getActivity(), EditTvShowEpisode.class);
  574. intent.putExtra("showId", mEpisode.getShowId());
  575. intent.putExtra("season", MizLib.getInteger(mEpisode.getSeason()));
  576. intent.putExtra("episode", MizLib.getInteger(mEpisode.getEpisode()));
  577. startActivityForResult(intent, 0);
  578. }
  579.  
  580. public void watchOffline() {
  581.  
  582. if (mEpisode.getFilepaths().size() == 1) {
  583. watchOffline(mEpisode.getFilepaths().get(0));
  584. } else {
  585. MizLib.showSelectFileDialog(getActivity(), mEpisode.getFilepaths(), new Dialog.OnClickListener() {
  586. @Override
  587. public void onClick(DialogInterface dialog, int which) {
  588. watchOffline(mEpisode.getFilepaths().get(which));
  589.  
  590. // Dismiss the dialog
  591. dialog.dismiss();
  592. }
  593. });
  594. }
  595. }
  596.  
  597. private void watchOffline(final Filepath path) {
  598. if (mEpisode.hasOfflineCopy(path)) {
  599. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  600. builder.setMessage(getString(R.string.areYouSure))
  601. .setTitle(getString(R.string.removeOfflineCopy))
  602. .setCancelable(false)
  603. .setPositiveButton(getString(android.R.string.yes), new DialogInterface.OnClickListener() {
  604. public void onClick(DialogInterface dialog, int id) {
  605. boolean success = mEpisode.getOfflineCopyFile(path).delete();
  606. if (!success)
  607. mEpisode.getOfflineCopyFile(path).delete();
  608. getActivity().invalidateOptionsMenu();
  609. }
  610. })
  611. .setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {
  612. public void onClick(DialogInterface dialog, int id) {
  613. dialog.cancel();
  614. }
  615. })
  616. .create().show();
  617. } else {
  618. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  619. builder.setMessage(getString(R.string.downloadOfflineCopy))
  620. .setTitle(getString(R.string.watchOffline))
  621. .setCancelable(false)
  622. .setPositiveButton(getString(android.R.string.yes), new DialogInterface.OnClickListener() {
  623. public void onClick(DialogInterface dialog, int id) {
  624. if (MizLib.isLocalCopyBeingDownloaded(getActivity()))
  625. Toast.makeText(getActivity(), R.string.addedToDownloadQueue, Toast.LENGTH_SHORT).show();
  626.  
  627. Intent i = new Intent(getActivity(), MakeAvailableOffline.class);
  628. i.putExtra(MakeAvailableOffline.FILEPATH, path.getFilepath());
  629. i.putExtra(MakeAvailableOffline.TYPE, MizLib.TYPE_SHOWS);
  630. i.putExtra("thumb", mEpisode.getThumbnail().getAbsolutePath());
  631. i.putExtra("backdrop", mEpisode.getEpisodePhoto().getAbsolutePath());
  632. getActivity().startService(i);
  633. }
  634. })
  635. .setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {
  636. public void onClick(DialogInterface dialog, int id) {
  637. dialog.cancel();
  638. }
  639. })
  640. .create().show();
  641. }
  642. }
  643.  
  644. private void identifyEpisode() {
  645. if (mEpisode.getFilepaths().size() == 1) {
  646. getActivity().startActivityForResult(getIdentifyIntent(mEpisode.getFilepaths().get(0).getFullFilepath()), 0);
  647.  
  648. } else {
  649. MizLib.showSelectFileDialog(getActivity(), mEpisode.getFilepaths(), new Dialog.OnClickListener() {
  650. @Override
  651. public void onClick(DialogInterface dialog, int which) {
  652. getActivity().startActivityForResult(getIdentifyIntent(mEpisode.getFilepaths().get(which).getFullFilepath()), 0);
  653.  
  654. // Dismiss the dialog
  655. dialog.dismiss();
  656. }
  657. });
  658. }
  659. }
  660.  
  661. private Intent getIdentifyIntent(String filepath) {
  662. Intent i = new Intent(getActivity(), IdentifyTvShowEpisode.class);
  663. ArrayList<String> filepaths = new ArrayList<String>();
  664. filepaths.add(filepath);
  665. i.putExtra("filepaths", filepaths);
  666. i.putExtra("showId", mEpisode.getShowId());
  667. i.putExtra("showTitle", MizuuApplication.getTvDbAdapter().getShowTitle(mEpisode.getShowId()));
  668. return i;
  669. }
  670.  
  671. private void deleteEpisode() {
  672. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  673.  
  674. View dialogLayout = getActivity().getLayoutInflater().inflate(R.layout.delete_file_dialog_layout, null);
  675. final CheckBox cb = (CheckBox) dialogLayout.findViewById(R.id.deleteFile);
  676. cb.setChecked(PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean(ALWAYS_DELETE_FILE, true));
  677.  
  678. builder.setTitle(getString(R.string.removeEpisode) + " S" + mEpisode.getSeason() + "E" + mEpisode.getEpisode())
  679. .setView(dialogLayout)
  680. .setCancelable(false)
  681. .setPositiveButton(getString(android.R.string.yes), new DialogInterface.OnClickListener() {
  682. public void onClick(DialogInterface dialog, int id) {
  683. TvShowDatabaseUtils.deleteEpisode(mContext, mEpisode.getShowId(),
  684. MizLib.getInteger(mEpisode.getSeason()), MizLib.getInteger(mEpisode.getEpisode()));
  685.  
  686. if (cb.isChecked()) {
  687. for (Filepath path : mEpisode.getFilepaths()) {
  688. Intent deleteIntent = new Intent(getActivity(), DeleteFile.class);
  689. deleteIntent.putExtra("filepath", path.getFilepath());
  690. getActivity().startService(deleteIntent);
  691. }
  692. }
  693.  
  694. if (MizuuApplication.getTvEpisodeDbAdapter().getEpisodeCount(mEpisode.getShowId()) == 0) {
  695. // The show has been deleted! Let's show the TV show library overview
  696.  
  697. Intent i = new Intent(mContext, Main.class);
  698. i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  699. i.putExtra("startup", String.valueOf(Main.SHOWS));
  700. startActivity(i);
  701. } else {
  702. LocalBroadcastUtils.updateTvShowSeasonsOverview(mContext);
  703. LocalBroadcastUtils.updateTvShowEpisodesOverview(mContext);
  704. }
  705.  
  706. notifyDatasetChanges();
  707. getActivity().finish();
  708. }
  709. })
  710. .setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {
  711. public void onClick(DialogInterface dialog, int id) {
  712. dialog.cancel();
  713. }
  714. })
  715. .show();
  716. }
  717.  
  718. private void watched(boolean showToast) {
  719. // Create and open database
  720. mDatabaseHelper = MizuuApplication.getTvEpisodeDbAdapter();
  721.  
  722. mEpisode.setHasWatched(!mEpisode.hasWatched()); // Reverse the hasWatched boolean
  723.  
  724. if (mDatabaseHelper.setEpisodeWatchStatus(mEpisode.getShowId(), mEpisode.getSeason(), mEpisode.getEpisode(), mEpisode.hasWatched())) {
  725. getActivity().invalidateOptionsMenu();
  726.  
  727. if (showToast)
  728. if (mEpisode.hasWatched()) {
  729. Toast.makeText(getActivity(), getString(R.string.markedAsWatched), Toast.LENGTH_SHORT).show();
  730. } else {
  731. Toast.makeText(getActivity(), getString(R.string.markedAsUnwatched), Toast.LENGTH_SHORT).show();
  732. }
  733. } else {
  734. if (showToast)
  735. Toast.makeText(getActivity(), getString(R.string.errorOccured), Toast.LENGTH_SHORT).show();
  736. }
  737.  
  738. mBus.post(mEpisode);
  739.  
  740. new Thread() {
  741. @Override
  742. public void run() {
  743. ArrayList<com.miz.functions.TvShowEpisode> episode = new ArrayList<com.miz.functions.TvShowEpisode>();
  744. episode.add(new com.miz.functions.TvShowEpisode(mEpisode.getShowId(), Integer.valueOf(mEpisode.getEpisode()), Integer.valueOf(mEpisode.getSeason())));
  745. Trakt.markEpisodeAsWatched(mEpisode.getShowId(), episode, getActivity(), false);
  746. }
  747. }.start();
  748. }
  749.  
  750. private void notifyDatasetChanges() {
  751. LocalBroadcastUtils.updateTvShowLibrary(getActivity());
  752. }
  753.  
  754. private void checkIn() {
  755. new Thread() {
  756. @Override
  757. public void run() {
  758. Trakt.performEpisodeCheckin(mEpisode, getActivity());
  759. }
  760. }.start();
  761. }
  762.  
  763. @Override
  764. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  765. if (requestCode == 0) {
  766. if (resultCode == Activity.RESULT_OK) {
  767. loadEpisode();
  768. loadData();
  769. }
  770. }
  771. }
  772.  
  773. public class BusToolbarColorObject {
  774.  
  775. private final int mToolbarColor, mAlpha;
  776.  
  777. public BusToolbarColorObject(int toolbarColor, int alpha) {
  778. mToolbarColor = toolbarColor;
  779. mAlpha = alpha;
  780. }
  781.  
  782. public int getToolbarColor() {
  783. return mToolbarColor;
  784. }
  785.  
  786. public int getAlpha() {
  787. return mAlpha;
  788. }
  789. }
  790. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement