aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/fragments/DownloadFragment.java
blob: 49f311415c4e8e14a23fa01058f5335725f08804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
  This file is part of Subsonic.
	Subsonic is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	Subsonic is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	You should have received a copy of the GNU General Public License
	along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
	Copyright 2014 (C) Scott Jackson
*/

package github.daneren2005.dsub.fragments;

import android.os.Bundle;
import android.os.Handler;
import androidx.recyclerview.widget.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import github.daneren2005.dsub.R;
import github.daneren2005.dsub.adapter.SectionAdapter;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.service.DownloadFile;
import github.daneren2005.dsub.service.DownloadService;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.util.DownloadFileItemHelperCallback;
import github.daneren2005.dsub.util.ProgressListener;
import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.adapter.DownloadFileAdapter;
import github.daneren2005.dsub.view.UpdateView;

public class DownloadFragment extends SelectRecyclerFragment<DownloadFile> implements SectionAdapter.OnItemClickedListener<DownloadFile> {
	private long currentRevision;
	private ScheduledExecutorService executorService;

	public DownloadFragment() {
		serialize = false;
		pullToRefresh = false;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
		super.onCreateView(inflater, container, bundle);

		ItemTouchHelper touchHelper = new ItemTouchHelper(new DownloadFileItemHelperCallback(this, false));
		touchHelper.attachToRecyclerView(recyclerView);

		return rootView;
	}

	@Override
	public void onStart() {
		super.onStart();

		final Handler handler = new Handler();
		Runnable runnable = () -> handler.post(this::update);

		executorService = Executors.newSingleThreadScheduledExecutor();
		executorService.scheduleWithFixedDelay(runnable, 0L, 1000L, TimeUnit.MILLISECONDS);
	}

	@Override
	public void onStop() {
		super.onStop();
		executorService.shutdown();
	}

	@Override
	public int getOptionsMenu() {
		return R.menu.downloading;
	}

	@Override
	public SectionAdapter getAdapter(List<DownloadFile> objs) {
		return new DownloadFileAdapter(context, objs, this);
	}

	@Override
	public List<DownloadFile> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
		DownloadService downloadService = getDownloadService();
		if(downloadService == null) {
			return new ArrayList<>();
		}

		List<DownloadFile> songList = new ArrayList<>(downloadService.getBackgroundDownloads());
		currentRevision = downloadService.getDownloadListUpdateRevision();
		return songList;
	}

	@Override
	public int getTitleResource() {
		return R.string.button_bar_downloading;
	}

	@Override
	public void onItemClicked(UpdateView<DownloadFile> updateView, DownloadFile item) {

	}

	@Override
	public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<DownloadFile> updateView, DownloadFile downloadFile) {
		MusicDirectory.Entry selectedItem = downloadFile.getSong();
		onCreateContextMenuSupport(menu, menuInflater, updateView, selectedItem);
		if(!selectedItem.isVideo() && !Util.isOffline(context)) {
			menu.removeItem(R.id.song_menu_remove_playlist);
		}

		recreateContextMenu(menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem menuItem, UpdateView<DownloadFile> updateView, DownloadFile downloadFile) {
		MusicDirectory.Entry selectedItem = downloadFile.getSong();
		return onContextItemSelected(menuItem, selectedItem);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem) {
		if(super.onOptionsItemSelected(menuItem)) {
			return true;
		}

		if (menuItem.getItemId() == R.id.menu_remove_all) {
			Util.confirmDialog(context, R.string.download_menu_remove_all, "", (dialog, which) -> new SilentBackgroundTask<Void>(context) {
				@Override
				protected Void doInBackground() {
					getDownloadService().clearBackground();
					return null;
				}

				@Override
				protected void done(Void result) {
					update();
				}
			}.execute());
			return true;
		}

		return false;
	}

	private void update() {
		DownloadService downloadService = getDownloadService();
		if (downloadService == null || objects == null || adapter == null) {
			return;
		}

		if (currentRevision != downloadService.getDownloadListUpdateRevision()) {
			List<DownloadFile> downloadFileList = downloadService.getBackgroundDownloads();
			objects.clear();
			objects.addAll(downloadFileList);
			adapter.notifyDataSetChanged();

			currentRevision = downloadService.getDownloadListUpdateRevision();
		}
	}
}